Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1331
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1331 ! raeburn 4: # $Id: lonnet.pm,v 1.1330 2016/11/15 20:46:35 raeburn Exp $
1.178 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.169 harris41 28: ###
29:
1.971 jms 30: =pod
31:
1.972 jms 32: =head1 NAME
33:
34: Apache::lonnet.pm
35:
36: =head1 SYNOPSIS
37:
38: This file is an interface to the lonc processes of
39: the LON-CAPA network as well as set of elaborated functions for handling information
40: necessary for navigating through a given cluster of LON-CAPA machines within a
41: domain. There are over 40 specialized functions in this module which handle the
42: reading and transmission of metadata, user information (ids, names, environments, roles,
43: logs), file information (storage, reading, directories, extensions, replication, embedded
44: styles and descriptors), educational resources (course descriptions, section names and
45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
46: and from more descriptive phrases or explanations.
47:
48: This is part of the LearningOnline Network with CAPA project
49: described at http://www.lon-capa.org.
50:
1.971 jms 51: =head1 Package Variables
52:
53: These are largely undocumented, so if you decipher one please note it here.
54:
55: =over 4
56:
57: =item $processmarker
58:
59: Contains the time this process was started and this servers host id.
60:
61: =item $dumpcount
62:
63: Counts the number of times a message log flush has been attempted (regardless
64: of success) by this process. Used as part of the filename when messages are
65: delayed.
66:
67: =back
68:
69: =cut
70:
1.1 albertel 71: package Apache::lonnet;
72:
73: use strict;
1.8 www 74: use LWP::UserAgent();
1.486 www 75: use HTTP::Date;
1.977 amueller 76: use Image::Magick;
77:
1.1182 foxr 78:
1.1173 foxr 79: use Encode;
80:
1.1245 raeburn 81: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1138 raeburn 82: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
83: %managerstab);
1.871 albertel 84:
85: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
86: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
87: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 88: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 89:
1.1 albertel 90: use IO::Socket;
1.31 www 91: use GDBM_File;
1.208 albertel 92: use HTML::LCParser;
1.88 www 93: use Fcntl qw(:flock);
1.870 albertel 94: use Storable qw(thaw nfreeze);
1.1289 damieng 95: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599 albertel 96: use Cache::Memcached;
1.676 albertel 97: use Digest::MD5;
1.790 albertel 98: use Math::Random;
1.1024 raeburn 99: use File::MMagic;
1.807 albertel 100: use LONCAPA qw(:DEFAULT :match);
1.740 www 101: use LONCAPA::Configuration;
1.1160 www 102: use LONCAPA::lonmetadata;
1.1167 droeschl 103: use LONCAPA::Lond;
1.1117 foxr 104:
1.1090 raeburn 105: use File::Copy;
1.676 albertel 106:
1.195 www 107: my $readit;
1.1288 damieng 108: my $max_connection_retries = 20; # Or some such value.
1.1 albertel 109:
1.619 albertel 110: require Exporter;
111:
112: our @ISA = qw (Exporter);
113: our @EXPORT = qw(%env);
114:
1.449 matthew 115:
1.1187 raeburn 116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729 www 117: {
118: my $logid;
1.1187 raeburn 119: sub write_log {
1.1188 raeburn 120: my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
1.1184 raeburn 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.1184 raeburn 127: $logid ++;
1.957 raeburn 128: my $now = time();
129: my $id=$now.'00000'.$$.'00000'.$logid;
1.1184 raeburn 130: my $logentry = {
131: $id => {
132: 'exe_uname' => $env{'user.name'},
133: 'exe_udom' => $env{'user.domain'},
134: 'exe_time' => $now,
135: 'exe_ip' => $ENV{'REMOTE_ADDR'},
136: 'delflag' => $delflag,
137: 'logentry' => $storehash,
138: 'uname' => $uname,
139: 'udom' => $udom,
140: }
141: };
142: return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729 www 143: }
144: }
1.1 albertel 145:
1.163 harris41 146: sub logtouch {
147: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 148: unless (-e "$execdir/logs/lonnet.log") {
149: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 150: close $fh;
151: }
152: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
153: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
154: }
155:
1.1 albertel 156: sub logthis {
157: my $message=shift;
158: my $execdir=$perlvar{'lonDaemons'};
159: my $now=time;
160: my $local=localtime($now);
1.448 albertel 161: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986 foxr 162: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
163: print $fh $logstring;
1.448 albertel 164: close($fh);
165: }
1.1 albertel 166: return 1;
167: }
168:
169: sub logperm {
170: my $message=shift;
171: my $execdir=$perlvar{'lonDaemons'};
172: my $now=time;
173: my $local=localtime($now);
1.448 albertel 174: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
175: print $fh "$now:$message:$local\n";
176: close($fh);
177: }
1.1 albertel 178: return 1;
179: }
180:
1.850 albertel 181: sub create_connection {
1.853 albertel 182: my ($hostname,$lonid) = @_;
1.851 albertel 183: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 184: Type => SOCK_STREAM,
185: Timeout => 10);
186: return 0 if (!$client);
1.890 albertel 187: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 188: my $result = <$client>;
189: chomp($result);
190: return 1 if ($result eq 'done');
191: return 0;
192: }
193:
1.983 raeburn 194: sub get_server_timezone {
195: my ($cnum,$cdom) = @_;
196: my $home=&homeserver($cnum,$cdom);
197: if ($home ne 'no_host') {
198: my $cachetime = 24*3600;
199: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
200: if (defined($cached)) {
201: return $timezone;
202: } else {
203: my $timezone = &reply('servertimezone',$home);
204: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
205: }
206: }
207: }
1.850 albertel 208:
1.1106 raeburn 209: sub get_server_distarch {
210: my ($lonhost,$ignore_cache) = @_;
211: if (defined($lonhost)) {
212: if (!defined(&hostname($lonhost))) {
213: return;
214: }
215: my $cachetime = 12*3600;
216: if (!$ignore_cache) {
217: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
218: if (defined($cached)) {
219: return $distarch;
220: }
221: }
222: my $rep = &reply('serverdistarch',$lonhost);
223: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
224: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
225: $rep eq '') {
226: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
227: }
228: }
229: return;
230: }
231:
1.1315 raeburn 232: sub get_servercerts_info {
233: my ($lonhost,$context) = @_;
234: my ($rep,$uselocal);
235: if (grep { $_ eq $lonhost } ¤t_machine_ids()) {
236: $uselocal = 1;
237: }
1.1316 raeburn 238: if (($context ne 'cgi') && ($uselocal)) {
1.1315 raeburn 239: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
1.1323 raeburn 240: if ($distro eq '') {
241: $uselocal = 0;
242: } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
1.1315 raeburn 243: if ($1 < 6) {
244: $uselocal = 0;
245: }
246: }
247: }
248: if ($uselocal) {
249: $rep = LONCAPA::Lond::server_certs(\%perlvar);
250: } else {
251: $rep=&reply('servercerts',$lonhost);
252: }
253: my ($result,%returnhash);
254: if (defined($lonhost)) {
255: if (!defined(&hostname($lonhost))) {
256: return;
257: }
258: }
259: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
260: ($rep eq 'unknown_cmd')) {
261: $result = $rep;
262: } else {
263: $result = 'ok';
264: my @pairs=split(/\&/,$rep);
265: foreach my $item (@pairs) {
266: my ($key,$value)=split(/=/,$item,2);
267: my $what = &unescape($key);
268: $returnhash{$what}=&thaw_unescape($value);
269: }
270: }
271: return ($result,\%returnhash);
272: }
273:
1.993 raeburn 274: sub get_server_loncaparev {
1.1073 raeburn 275: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 276: if (defined($lonhost)) {
277: if (!defined(&hostname($lonhost))) {
278: undef($lonhost);
279: }
280: }
281: if (!defined($lonhost)) {
282: if (defined(&domain($dom,'primary'))) {
283: $lonhost=&domain($dom,'primary');
284: if ($lonhost eq 'no_host') {
285: undef($lonhost);
286: }
287: }
288: }
289: if (defined($lonhost)) {
1.1073 raeburn 290: my $cachetime = 12*3600;
291: if (!$ignore_cache) {
292: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
293: if (defined($cached)) {
294: return $loncaparev;
295: }
296: }
297: my ($answer,$loncaparev);
298: my @ids=¤t_machine_ids();
299: if (grep(/^\Q$lonhost\E$/,@ids)) {
300: $answer = $perlvar{'lonVersion'};
1.1081 raeburn 301: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 302: $loncaparev = $1;
303: }
304: } else {
305: $answer = &reply('serverloncaparev',$lonhost);
306: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
307: if ($caller eq 'loncron') {
308: my $ua=new LWP::UserAgent;
1.1082 raeburn 309: $ua->timeout(4);
1.1073 raeburn 310: my $protocol = $protocol{$lonhost};
311: $protocol = 'http' if ($protocol ne 'https');
312: my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
313: my $request=new HTTP::Request('GET',$url);
314: my $response=$ua->request($request);
315: unless ($response->is_error()) {
316: my $content = $response->content;
1.1081 raeburn 317: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073 raeburn 318: $loncaparev = $1;
319: }
320: }
321: } else {
322: $loncaparev = $loncaparevs{$lonhost};
323: }
1.1081 raeburn 324: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073 raeburn 325: $loncaparev = $1;
326: }
1.993 raeburn 327: }
1.1073 raeburn 328: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993 raeburn 329: }
330: }
331:
1.1074 raeburn 332: sub get_server_homeID {
333: my ($hostname,$ignore_cache,$caller) = @_;
334: unless ($ignore_cache) {
335: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
336: if (defined($cached)) {
337: return $serverhomeID;
338: }
339: }
340: my $cachetime = 12*3600;
341: my $serverhomeID;
342: if ($caller eq 'loncron') {
343: my @machine_ids = &machine_ids($hostname);
344: foreach my $id (@machine_ids) {
345: my $response = &reply('serverhomeID',$id);
346: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
347: $serverhomeID = $response;
348: last;
349: }
350: }
351: if ($serverhomeID eq '') {
352: $serverhomeID = $machine_ids[-1];
353: }
354: } else {
355: $serverhomeID = $serverhomeIDs{$hostname};
356: }
357: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
358: }
359:
1.1121 raeburn 360: sub get_remote_globals {
361: my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125 raeburn 362: my ($result,%returnhash,%whatneeded);
363: if (ref($whathash) eq 'HASH') {
1.1121 raeburn 364: foreach my $what (sort(keys(%{$whathash}))) {
365: my $hashid = $lonhost.'-'.$what;
1.1125 raeburn 366: my ($response,$cached);
1.1121 raeburn 367: unless ($ignore_cache) {
1.1125 raeburn 368: ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121 raeburn 369: }
370: if (defined($cached)) {
1.1125 raeburn 371: $returnhash{$what} = $response;
1.1121 raeburn 372: } else {
1.1125 raeburn 373: $whatneeded{$what} = 1;
1.1121 raeburn 374: }
375: }
1.1125 raeburn 376: if (keys(%whatneeded) == 0) {
377: $result = 'ok';
378: } else {
1.1121 raeburn 379: my $requested = &freeze_escape(\%whatneeded);
380: my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125 raeburn 381: if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
382: ($rep eq 'unknown_cmd')) {
383: $result = $rep;
384: } else {
385: $result = 'ok';
1.1121 raeburn 386: my @pairs=split(/\&/,$rep);
1.1125 raeburn 387: foreach my $item (@pairs) {
388: my ($key,$value)=split(/=/,$item,2);
389: my $what = &unescape($key);
390: my $hashid = $lonhost.'-'.$what;
391: $returnhash{$what}=&thaw_unescape($value);
392: &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121 raeburn 393: }
394: }
395: }
396: }
1.1125 raeburn 397: return ($result,\%returnhash);
1.1121 raeburn 398: }
399:
1.1124 raeburn 400: sub remote_devalidate_cache {
1.1241 raeburn 401: my ($lonhost,$cachekeys) = @_;
402: my $items;
403: return unless (ref($cachekeys) eq 'ARRAY');
404: my $cachestr = join('&',@{$cachekeys});
405: my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124 raeburn 406: return $response;
407: }
408:
1.1 albertel 409: # -------------------------------------------------- Non-critical communication
410: sub subreply {
411: my ($cmd,$server)=@_;
1.838 albertel 412: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 413: #
414: # With loncnew process trimming, there's a timing hole between lonc server
415: # process exit and the master server picking up the listen on the AF_UNIX
416: # socket. In that time interval, a lock file will exist:
417:
418: my $lockfile=$peerfile.".lock";
419: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
1.1289 damieng 420: sleep(0.1);
1.549 foxr 421: }
422: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 423: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 424: #
1.550 foxr 425: # We'll give the connection a few tries before abandoning it. If
426: # connection is not possible, we'll con_lost back to the client.
427: #
428: my $client;
429: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
430: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
431: Type => SOCK_STREAM,
432: Timeout => 10);
1.869 albertel 433: if ($client) {
1.550 foxr 434: last; # Connected!
1.850 albertel 435: } else {
1.853 albertel 436: &create_connection(&hostname($server),$server);
1.550 foxr 437: }
1.1289 damieng 438: sleep(0.1); # Try again later if failed connection.
1.550 foxr 439: }
440: my $answer;
441: if ($client) {
1.704 albertel 442: print $client "sethost:$server:$cmd\n";
1.550 foxr 443: $answer=<$client>;
444: if (!$answer) { $answer="con_lost"; }
445: chomp($answer);
446: } else {
447: $answer = 'con_lost'; # Failed connection.
448: }
1.1 albertel 449: return $answer;
450: }
451:
452: sub reply {
453: my ($cmd,$server)=@_;
1.838 albertel 454: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 455: my $answer=subreply($cmd,$server);
1.65 www 456: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 457: &logthis("<font color=\"blue\">WARNING:".
1.12 www 458: " $cmd to $server returned $answer</font>");
459: }
1.1 albertel 460: return $answer;
461: }
462:
463: # ----------------------------------------------------------- Send USR1 to lonc
464:
465: sub reconlonc {
1.891 albertel 466: my ($lonid) = @_;
467: if ($lonid) {
1.1295 raeburn 468: my $hostname = &hostname($lonid);
1.891 albertel 469: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
470: if ($hostname && -e $peerfile) {
471: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
472: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
473: Type => SOCK_STREAM,
474: Timeout => 10);
475: if ($client) {
476: print $client ("reset_retries\n");
477: my $answer=<$client>;
478: #reset just this one.
479: }
480: }
481: return;
482: }
483:
1.836 www 484: &logthis("Trying to reconnect lonc");
1.1 albertel 485: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 486: if (open(my $fh,"<$loncfile")) {
1.1 albertel 487: my $loncpid=<$fh>;
488: chomp($loncpid);
489: if (kill 0 => $loncpid) {
490: &logthis("lonc at pid $loncpid responding, sending USR1");
491: kill USR1 => $loncpid;
492: sleep 1;
1.1295 raeburn 493: } else {
1.12 www 494: &logthis(
1.672 albertel 495: "<font color=\"blue\">WARNING:".
1.12 www 496: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 497: }
498: } else {
1.836 www 499: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 500: }
501: }
502:
503: # ------------------------------------------------------ Critical communication
1.12 www 504:
1.1 albertel 505: sub critical {
506: my ($cmd,$server)=@_;
1.838 albertel 507: unless (&hostname($server)) {
1.672 albertel 508: &logthis("<font color=\"blue\">WARNING:".
1.89 www 509: " Critical message to unknown server ($server)</font>");
510: return 'no_such_host';
511: }
1.1 albertel 512: my $answer=reply($cmd,$server);
513: if ($answer eq 'con_lost') {
1.1295 raeburn 514: &reconlonc($server);
1.589 albertel 515: my $answer=reply($cmd,$server);
1.1 albertel 516: if ($answer eq 'con_lost') {
517: my $now=time;
518: my $middlename=$cmd;
1.5 www 519: $middlename=substr($middlename,0,16);
1.1 albertel 520: $middlename=~s/\W//g;
521: my $dfilename=
1.305 www 522: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
523: $dumpcount++;
1.1 albertel 524: {
1.448 albertel 525: my $dfh;
526: if (open($dfh,">$dfilename")) {
527: print $dfh "$cmd\n";
528: close($dfh);
529: }
1.1 albertel 530: }
1.1288 damieng 531: sleep 1;
1.1 albertel 532: my $wcmd='';
533: {
1.448 albertel 534: my $dfh;
535: if (open($dfh,"<$dfilename")) {
536: $wcmd=<$dfh>;
537: close($dfh);
538: }
1.1 albertel 539: }
540: chomp($wcmd);
1.7 www 541: if ($wcmd eq $cmd) {
1.672 albertel 542: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 543: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 544: &logperm("D:$server:$cmd");
545: return 'con_delayed';
546: } else {
1.672 albertel 547: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 548: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 549: &logperm("F:$server:$cmd");
550: return 'con_failed';
551: }
552: }
553: }
554: return $answer;
1.405 albertel 555: }
556:
1.755 albertel 557: # ------------------------------------------- check if return value is an error
558:
559: sub error {
560: my ($result) = @_;
1.756 albertel 561: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 562: if ($2 == 2) { return undef; }
563: return $1;
564: }
565: return undef;
566: }
567:
1.783 albertel 568: sub convert_and_load_session_env {
569: my ($lonidsdir,$handle)=@_;
570: my @profile;
571: {
1.917 albertel 572: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
573: if (!$opened) {
1.915 albertel 574: return 0;
575: }
1.783 albertel 576: flock($idf,LOCK_SH);
577: @profile=<$idf>;
578: close($idf);
579: }
580: my %temp_env;
581: foreach my $line (@profile) {
1.786 albertel 582: if ($line !~ m/=/) {
583: return 0;
584: }
1.783 albertel 585: chomp($line);
586: my ($envname,$envvalue)=split(/=/,$line,2);
587: $temp_env{&unescape($envname)} = &unescape($envvalue);
588: }
589: unlink("$lonidsdir/$handle.id");
590: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
591: 0640)) {
592: %disk_env = %temp_env;
593: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
594: untie(%disk_env);
595: }
1.786 albertel 596: return 1;
1.783 albertel 597: }
598:
1.374 www 599: # ------------------------------------------- Transfer profile into environment
1.780 albertel 600: my $env_loaded;
601: sub transfer_profile_to_env {
1.788 albertel 602: my ($lonidsdir,$handle,$force_transfer) = @_;
603: if (!$force_transfer && $env_loaded) { return; }
1.374 www 604:
1.720 albertel 605: if (!defined($lonidsdir)) {
606: $lonidsdir = $perlvar{'lonIDsDir'};
607: }
608: if (!defined($handle)) {
609: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
610: }
611:
1.786 albertel 612: my $convert;
613: {
1.917 albertel 614: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
615: if (!$opened) {
1.915 albertel 616: return;
617: }
1.786 albertel 618: flock($idf,LOCK_SH);
619: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
620: &GDBM_READER(),0640)) {
621: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
622: untie(%disk_env);
623: } else {
624: $convert = 1;
625: }
626: }
627: if ($convert) {
628: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
629: &logthis("Failed to load session, or convert session.");
630: }
1.374 www 631: }
1.783 albertel 632:
1.786 albertel 633: my %remove;
1.783 albertel 634: while ( my $envname = each(%env) ) {
1.433 matthew 635: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
636: if ($time < time-300) {
1.783 albertel 637: $remove{$key}++;
1.433 matthew 638: }
639: }
640: }
1.783 albertel 641:
1.619 albertel 642: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 643: $env_loaded=1;
1.783 albertel 644: foreach my $expired_key (keys(%remove)) {
1.433 matthew 645: &delenv($expired_key);
1.374 www 646: }
1.1 albertel 647: }
648:
1.916 albertel 649: # ---------------------------------------------------- Check for valid session
650: sub check_for_valid_session {
1.1245 raeburn 651: my ($r,$name,$userhashref) = @_;
1.916 albertel 652: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1155 raeburn 653: if ($name eq '') {
654: $name = 'lonID';
655: }
656: my $lonid=$cookies{$name};
1.916 albertel 657: return undef if (!$lonid);
658:
659: my $handle=&LONCAPA::clean_handle($lonid->value);
1.1155 raeburn 660: my $lonidsdir;
661: if ($name eq 'lonDAV') {
662: $lonidsdir=$r->dir_config('lonDAVsessDir');
663: } else {
664: $lonidsdir=$r->dir_config('lonIDsDir');
665: }
1.916 albertel 666: return undef if (!-e "$lonidsdir/$handle.id");
667:
1.917 albertel 668: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
669: return undef if (!$opened);
1.916 albertel 670:
671: flock($idf,LOCK_SH);
672: my %disk_env;
673: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
674: &GDBM_READER(),0640)) {
675: return undef;
676: }
677:
678: if (!defined($disk_env{'user.name'})
679: || !defined($disk_env{'user.domain'})) {
680: return undef;
681: }
1.1245 raeburn 682:
683: if (ref($userhashref) eq 'HASH') {
684: $userhashref->{'name'} = $disk_env{'user.name'};
685: $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1212 raeburn 686: }
1.1245 raeburn 687:
1.916 albertel 688: return $handle;
689: }
690:
1.830 albertel 691: sub timed_flock {
692: my ($file,$lock_type) = @_;
693: my $failed=0;
694: eval {
695: local $SIG{__DIE__}='DEFAULT';
696: local $SIG{ALRM}=sub {
697: $failed=1;
698: die("failed lock");
699: };
700: alarm(13);
701: flock($file,$lock_type);
702: alarm(0);
703: };
704: if ($failed) {
705: return undef;
706: } else {
707: return 1;
708: }
709: }
710:
1.5 www 711: # ---------------------------------------------------------- Append Environment
712:
713: sub appenv {
1.949 raeburn 714: my ($newenv,$roles) = @_;
715: if (ref($newenv) eq 'HASH') {
716: foreach my $key (keys(%{$newenv})) {
717: my $refused = 0;
718: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
719: $refused = 1;
720: if (ref($roles) eq 'ARRAY') {
1.1250 raeburn 721: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949 raeburn 722: if (grep(/^\Q$role\E$/,@{$roles})) {
723: $refused = 0;
724: }
725: }
726: }
727: if ($refused) {
728: &logthis("<font color=\"blue\">WARNING: ".
729: "Attempt to modify environment ".$key." to ".$newenv->{$key}
730: .'</font>');
731: delete($newenv->{$key});
732: } else {
733: $env{$key}=$newenv->{$key};
734: }
735: }
736: my $opened = open(my $env_file,'+<',$env{'user.environment'});
737: if ($opened
738: && &timed_flock($env_file,LOCK_EX)
739: &&
740: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
741: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
742: while (my ($key,$value) = each(%{$newenv})) {
743: $disk_env{$key} = $value;
744: }
745: untie(%disk_env);
1.35 www 746: }
1.191 harris41 747: }
1.56 www 748: return 'ok';
749: }
750: # ----------------------------------------------------- Delete from Environment
751:
752: sub delenv {
1.1104 raeburn 753: my ($delthis,$regexp,$roles) = @_;
754: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
755: my $refused = 1;
756: if (ref($roles) eq 'ARRAY') {
757: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
758: if (grep(/^\Q$role\E$/,@{$roles})) {
759: $refused = 0;
760: }
761: }
762: if ($refused) {
763: &logthis("<font color=\"blue\">WARNING: ".
764: "Attempt to delete from environment ".$delthis);
765: return 'error';
766: }
1.56 www 767: }
1.917 albertel 768: my $opened = open(my $env_file,'+<',$env{'user.environment'});
769: if ($opened
1.915 albertel 770: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 771: &&
772: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
773: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 774: foreach my $key (keys(%disk_env)) {
1.987 raeburn 775: if ($regexp) {
776: if ($key=~/^$delthis/) {
777: delete($env{$key});
778: delete($disk_env{$key});
779: }
780: } else {
781: if ($key=~/^\Q$delthis\E/) {
782: delete($env{$key});
783: delete($disk_env{$key});
784: }
785: }
1.448 albertel 786: }
1.783 albertel 787: untie(%disk_env);
1.5 www 788: }
789: return 'ok';
1.369 albertel 790: }
791:
1.790 albertel 792: sub get_env_multiple {
793: my ($name) = @_;
794: my @values;
795: if (defined($env{$name})) {
796: # exists is it an array
797: if (ref($env{$name})) {
798: @values=@{ $env{$name} };
799: } else {
800: $values[0]=$env{$name};
801: }
802: }
803: return(@values);
804: }
805:
1.958 www 806: # ------------------------------------------------------------------- Locking
807:
808: sub set_lock {
809: my ($text)=@_;
810: $locknum++;
811: my $id=$$.'-'.$locknum;
812: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
813: 'session.lock.'.$id => $text});
814: return $id;
815: }
816:
817: sub get_locks {
818: my $num=0;
819: my %texts=();
820: foreach my $lock (split(/\,/,$env{'session.locks'})) {
821: if ($lock=~/\w/) {
822: $num++;
823: $texts{$lock}=$env{'session.lock.'.$lock};
824: }
825: }
826: return ($num,%texts);
827: }
828:
829: sub remove_lock {
830: my ($id)=@_;
831: my $newlocks='';
832: foreach my $lock (split(/\,/,$env{'session.locks'})) {
833: if (($lock=~/\w/) && ($lock ne $id)) {
834: $newlocks.=','.$lock;
835: }
836: }
837: &appenv({'session.locks' => $newlocks});
838: &delenv('session.lock.'.$id);
839: }
840:
841: sub remove_all_locks {
842: my $activelocks=$env{'session.locks'};
843: foreach my $lock (split(/\,/,$env{'session.locks'})) {
844: if ($lock=~/\w/) {
845: &remove_lock($lock);
846: }
847: }
848: }
849:
850:
1.369 albertel 851: # ------------------------------------------ Find out current server userload
852: sub userload {
853: my $numusers=0;
854: {
855: opendir(LONIDS,$perlvar{'lonIDsDir'});
856: my $filename;
857: my $curtime=time;
858: while ($filename=readdir(LONIDS)) {
1.925 albertel 859: next if ($filename eq '.' || $filename eq '..');
860: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 861: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 862: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 863: }
864: closedir(LONIDS);
865: }
866: my $userloadpercent=0;
867: my $maxuserload=$perlvar{'lonUserLoadLim'};
868: if ($maxuserload) {
1.371 albertel 869: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 870: }
1.372 albertel 871: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 872: return $userloadpercent;
1.283 www 873: }
874:
1.1 albertel 875: # ------------------------------ Find server with least workload from spare.tab
1.11 www 876:
1.1 albertel 877: sub spareserver {
1.1083 raeburn 878: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 879: my $spare_server;
1.370 albertel 880: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 881: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
882: : $userloadpercent;
1.1083 raeburn 883: my ($uint_dom,$remotesessions);
884: if (($udom ne '') && (&domain($udom) ne '')) {
885: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
886: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
887: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
888: $remotesessions = $udomdefaults{'remotesessions'};
889: }
1.1123 raeburn 890: my $spareshash = &this_host_spares($udom);
891: if (ref($spareshash) eq 'HASH') {
892: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
893: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279 raeburn 894: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
895: $try_server));
1.1123 raeburn 896: ($spare_server, $lowest_load) =
897: &compare_server_load($try_server, $spare_server, $lowest_load);
898: }
1.1083 raeburn 899: }
1.784 albertel 900:
1.1123 raeburn 901: my $found_server = ($spare_server ne '' && $lowest_load < 100);
902:
903: if (!$found_server) {
904: if (ref($spareshash->{'default'}) eq 'ARRAY') {
905: foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279 raeburn 906: next unless (&spare_can_host($udom,$uint_dom,
907: $remotesessions,$try_server));
1.1123 raeburn 908: ($spare_server, $lowest_load) =
909: &compare_server_load($try_server, $spare_server, $lowest_load);
910: }
911: }
912: }
1.784 albertel 913: }
914:
915: if (!$want_server_name) {
1.968 raeburn 916: my $protocol = 'http';
917: if ($protocol{$spare_server} eq 'https') {
918: $protocol = $protocol{$spare_server};
919: }
1.1001 raeburn 920: if (defined($spare_server)) {
921: my $hostname = &hostname($spare_server);
1.1083 raeburn 922: if (defined($hostname)) {
1.1001 raeburn 923: $spare_server = $protocol.'://'.$hostname;
924: }
925: }
1.784 albertel 926: }
927: return $spare_server;
928: }
929:
930: sub compare_server_load {
1.1253 raeburn 931: my ($try_server, $spare_server, $lowest_load, $required) = @_;
932:
933: if ($required) {
934: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
935: my $remoterev = &get_server_loncaparev(undef,$try_server);
936: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
937: if (($major eq '' && $minor eq '') ||
938: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
939: return ($spare_server,$lowest_load);
940: }
941: }
1.784 albertel 942:
943: my $loadans = &reply('load', $try_server);
944: my $userloadans = &reply('userload',$try_server);
945:
946: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114 raeburn 947: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 948: }
949:
950: my $load;
951: if ($loadans =~ /\d/) {
952: if ($userloadans =~ /\d/) {
953: #both are numbers, pick the bigger one
954: $load = ($loadans > $userloadans) ? $loadans
955: : $userloadans;
1.411 albertel 956: } else {
1.784 albertel 957: $load = $loadans;
1.411 albertel 958: }
1.784 albertel 959: } else {
960: $load = $userloadans;
961: }
962:
963: if (($load =~ /\d/) && ($load < $lowest_load)) {
964: $spare_server = $try_server;
965: $lowest_load = $load;
1.370 albertel 966: }
1.784 albertel 967: return ($spare_server,$lowest_load);
1.202 matthew 968: }
1.914 albertel 969:
970: # --------------------------- ask offload servers if user already has a session
971: sub find_existing_session {
972: my ($udom,$uname) = @_;
1.1123 raeburn 973: my $spareshash = &this_host_spares($udom);
974: if (ref($spareshash) eq 'HASH') {
975: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
976: foreach my $try_server (@{ $spareshash->{'primary'} }) {
977: return $try_server if (&has_user_session($try_server, $udom, $uname));
978: }
979: }
980: if (ref($spareshash->{'default'}) eq 'ARRAY') {
981: foreach my $try_server (@{ $spareshash->{'default'} }) {
982: return $try_server if (&has_user_session($try_server, $udom, $uname));
983: }
984: }
1.914 albertel 985: }
986: return;
987: }
988:
989: # -------------------------------- ask if server already has a session for user
990: sub has_user_session {
991: my ($lonid,$udom,$uname) = @_;
992: my $result = &reply(join(':','userhassession',
993: map {&escape($_)} ($udom,$uname)),$lonid);
994: return 1 if ($result eq 'ok');
995:
996: return 0;
997: }
998:
1.1076 raeburn 999: # --------- determine least loaded server in a user's domain which allows login
1000:
1001: sub choose_server {
1.1259 raeburn 1002: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076 raeburn 1003: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077 raeburn 1004: my %servers = &get_servers($udom);
1.1076 raeburn 1005: my $lowest_load = 30000;
1.1259 raeburn 1006: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1007: if ($skiploadbal) {
1008: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1009: unless (defined($cached)) {
1010: my $cachetime = 60*60*24;
1011: my %domconfig =
1012: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1013: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1014: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1015: $cachetime);
1016: }
1017: }
1018: }
1.1076 raeburn 1019: foreach my $lonhost (keys(%servers)) {
1.1259 raeburn 1020: if ($skiploadbal) {
1021: if (ref($balancers) eq 'HASH') {
1022: next if (exists($balancers->{$lonhost}));
1023: }
1024: }
1.1115 raeburn 1025: my $loginvia;
1026: if ($checkloginvia) {
1027: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116 raeburn 1028: if ($loginvia) {
1029: my ($server,$path) = split(/:/,$loginvia);
1030: ($login_host, $lowest_load) =
1.1253 raeburn 1031: &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116 raeburn 1032: if ($login_host eq $server) {
1033: $portal_path = $path;
1.1151 raeburn 1034: $isredirect = 1;
1.1116 raeburn 1035: }
1036: } else {
1037: ($login_host, $lowest_load) =
1.1253 raeburn 1038: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116 raeburn 1039: if ($login_host eq $lonhost) {
1040: $portal_path = '';
1.1151 raeburn 1041: $isredirect = '';
1.1116 raeburn 1042: }
1043: }
1044: } else {
1.1076 raeburn 1045: ($login_host, $lowest_load) =
1.1253 raeburn 1046: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076 raeburn 1047: }
1048: }
1049: if ($login_host ne '') {
1.1116 raeburn 1050: $hostname = &hostname($login_host);
1.1076 raeburn 1051: }
1.1331 ! raeburn 1052: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076 raeburn 1053: }
1054:
1.202 matthew 1055: # --------------------------------------------- Try to change a user's password
1056:
1057: sub changepass {
1.799 raeburn 1058: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 1059: $currentpass = &escape($currentpass);
1060: $newpass = &escape($newpass);
1.1030 raeburn 1061: my $lonhost = $perlvar{'lonHostID'};
1062: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 1063: $server);
1064: if (! $answer) {
1065: &logthis("No reply on password change request to $server ".
1066: "by $uname in domain $udom.");
1067: } elsif ($answer =~ "^ok") {
1068: &logthis("$uname in $udom successfully changed their password ".
1069: "on $server.");
1070: } elsif ($answer =~ "^pwchange_failure") {
1071: &logthis("$uname in $udom was unable to change their password ".
1072: "on $server. The action was blocked by either lcpasswd ".
1073: "or pwchange");
1074: } elsif ($answer =~ "^non_authorized") {
1075: &logthis("$uname in $udom did not get their password correct when ".
1076: "attempting to change it on $server.");
1077: } elsif ($answer =~ "^auth_mode_error") {
1078: &logthis("$uname in $udom attempted to change their password despite ".
1079: "not being locally or internally authenticated on $server.");
1080: } elsif ($answer =~ "^unknown_user") {
1081: &logthis("$uname in $udom attempted to change their password ".
1082: "on $server but were unable to because $server is not ".
1083: "their home server.");
1084: } elsif ($answer =~ "^refused") {
1085: &logthis("$server refused to change $uname in $udom password because ".
1086: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 1087: } elsif ($answer =~ "invalid_client") {
1088: &logthis("$server refused to change $uname in $udom password because ".
1089: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 1090: }
1091: return $answer;
1.1 albertel 1092: }
1093:
1.169 harris41 1094: # ----------------------- Try to determine user's current authentication scheme
1095:
1096: sub queryauthenticate {
1097: my ($uname,$udom)=@_;
1.456 albertel 1098: my $uhome=&homeserver($uname,$udom);
1099: if (!$uhome) {
1100: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1101: return 'no_host';
1102: }
1103: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1104: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1105: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 1106: }
1.456 albertel 1107: return $answer;
1.169 harris41 1108: }
1109:
1.1 albertel 1110: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1111:
1.1 albertel 1112: sub authenticate {
1.1073 raeburn 1113: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1114: $upass=&escape($upass);
1115: $uname= &LONCAPA::clean_username($uname);
1.836 www 1116: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1117: my $newhome;
1.836 www 1118: if ((!$uhome) || ($uhome eq 'no_host')) {
1119: # Maybe the machine was offline and only re-appeared again recently?
1120: &reconlonc();
1121: # One more
1.952 raeburn 1122: $uhome=&homeserver($uname,$udom,1);
1123: if (($uhome eq 'no_host') && $checkdefauth) {
1124: if (defined(&domain($udom,'primary'))) {
1125: $newhome=&domain($udom,'primary');
1126: }
1127: if ($newhome ne '') {
1128: $uhome = $newhome;
1129: }
1130: }
1.836 www 1131: if ((!$uhome) || ($uhome eq 'no_host')) {
1132: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1133: return 'no_host';
1134: }
1.1 albertel 1135: }
1.1073 raeburn 1136: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1137: if ($answer eq 'authorized') {
1.952 raeburn 1138: if ($newhome) {
1139: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1140: return 'no_account_on_host';
1141: } else {
1142: &logthis("User $uname at $udom authorized by $uhome");
1143: return $uhome;
1144: }
1.471 albertel 1145: }
1146: if ($answer eq 'non_authorized') {
1147: &logthis("User $uname at $udom rejected by $uhome");
1148: return 'no_host';
1.9 www 1149: }
1.471 albertel 1150: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1151: return 'no_host';
1152: }
1153:
1.1073 raeburn 1154: sub can_host_session {
1.1074 raeburn 1155: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073 raeburn 1156: my $canhost = 1;
1.1074 raeburn 1157: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073 raeburn 1158: if (ref($remotesessions) eq 'HASH') {
1159: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074 raeburn 1160: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073 raeburn 1161: $canhost = 0;
1162: } else {
1163: $canhost = 1;
1164: }
1165: }
1166: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074 raeburn 1167: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073 raeburn 1168: $canhost = 1;
1169: } else {
1170: $canhost = 0;
1171: }
1172: }
1173: if ($canhost) {
1174: if ($remotesessions->{'version'} ne '') {
1175: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1176: if ($reqmajor ne '' && $reqminor ne '') {
1177: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1178: my $major = $1;
1179: my $minor = $2;
1180: if (($major < $reqmajor ) ||
1181: (($major == $reqmajor) && ($minor < $reqminor))) {
1182: $canhost = 0;
1183: }
1184: } else {
1185: $canhost = 0;
1186: }
1187: }
1188: }
1189: }
1190: }
1191: if ($canhost) {
1192: if (ref($hostedsessions) eq 'HASH') {
1.1120 raeburn 1193: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1194: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073 raeburn 1195: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120 raeburn 1196: if (($uint_dom ne '') &&
1197: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073 raeburn 1198: $canhost = 0;
1199: } else {
1200: $canhost = 1;
1201: }
1202: }
1203: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120 raeburn 1204: if (($uint_dom ne '') &&
1205: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073 raeburn 1206: $canhost = 1;
1207: } else {
1208: $canhost = 0;
1209: }
1210: }
1211: }
1212: }
1213: return $canhost;
1214: }
1215:
1.1083 raeburn 1216: sub spare_can_host {
1217: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1218: my $canhost=1;
1.1279 raeburn 1219: my $try_server_hostname = &hostname($try_server);
1220: my $serverhomeID = &get_server_homeID($try_server_hostname);
1221: my $serverhomedom = &host_domain($serverhomeID);
1222: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1223: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1224: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1225: $canhost = 0;
1226: }
1227: }
1228: if (($canhost) && ($uint_dom)) {
1229: my @intdoms;
1230: my $internet_names = &get_internet_names($try_server);
1231: if (ref($internet_names) eq 'ARRAY') {
1232: @intdoms = @{$internet_names};
1233: }
1234: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1235: my $remoterev = &get_server_loncaparev(undef,$try_server);
1236: $canhost = &can_host_session($udom,$try_server,$remoterev,
1237: $remotesessions,
1238: $defdomdefaults{'hostedsessions'});
1239: }
1.1083 raeburn 1240: }
1241: return $canhost;
1242: }
1243:
1.1123 raeburn 1244: sub this_host_spares {
1245: my ($dom) = @_;
1.1126 raeburn 1246: my ($dom_in_use,$lonhost_in_use,$result);
1.1123 raeburn 1247: my @hosts = ¤t_machine_ids();
1248: foreach my $lonhost (@hosts) {
1249: if (&host_domain($lonhost) eq $dom) {
1.1126 raeburn 1250: $dom_in_use = $dom;
1251: $lonhost_in_use = $lonhost;
1.1123 raeburn 1252: last;
1253: }
1254: }
1.1126 raeburn 1255: if ($dom_in_use ne '') {
1256: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1257: }
1258: if (ref($result) ne 'HASH') {
1259: $lonhost_in_use = $perlvar{'lonHostID'};
1260: $dom_in_use = &host_domain($lonhost_in_use);
1261: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1262: if (ref($result) ne 'HASH') {
1263: $result = \%spareid;
1264: }
1265: }
1266: return $result;
1267: }
1268:
1269: sub spares_for_offload {
1270: my ($dom_in_use,$lonhost_in_use) = @_;
1271: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123 raeburn 1272: if (defined($cached)) {
1273: return $result;
1274: } else {
1.1126 raeburn 1275: my $cachetime = 60*60*24;
1276: my %domconfig =
1277: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1278: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1279: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1280: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1281: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123 raeburn 1282: }
1283: }
1284: }
1285: }
1.1126 raeburn 1286: return;
1.1123 raeburn 1287: }
1288:
1.1129 raeburn 1289: sub get_lonbalancer_config {
1290: my ($servers) = @_;
1291: my ($currbalancer,$currtargets);
1292: if (ref($servers) eq 'HASH') {
1293: foreach my $server (keys(%{$servers})) {
1294: my %what = (
1295: spareid => 1,
1296: perlvar => 1,
1297: );
1298: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1299: if ($result eq 'ok') {
1300: if (ref($returnhash) eq 'HASH') {
1301: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1302: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1303: $currbalancer = $server;
1304: $currtargets = {};
1305: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1306: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1307: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1308: }
1309: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1310: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1311: }
1312: }
1313: last;
1314: }
1315: }
1316: }
1317: }
1318: }
1319: }
1320: return ($currbalancer,$currtargets);
1321: }
1322:
1323: sub check_loadbalancing {
1.1331 ! raeburn 1324: my ($uname,$udom,$caller) = @_;
1.1191 raeburn 1325: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1326: $rule_in_effect,$offloadto,$otherserver);
1.1129 raeburn 1327: my $lonhost = $perlvar{'lonHostID'};
1.1175 raeburn 1328: my @hosts = ¤t_machine_ids();
1.1129 raeburn 1329: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1330: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1331: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1332: my $serverhomedom = &host_domain($lonhost);
1.1307 raeburn 1333: my $domneedscache;
1.1129 raeburn 1334: my $cachetime = 60*60*24;
1335:
1336: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1337: $dom_in_use = $udom;
1338: $homeintdom = 1;
1339: } else {
1340: $dom_in_use = $serverhomedom;
1341: }
1342: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1343: unless (defined($cached)) {
1344: my %domconfig =
1345: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1346: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130 raeburn 1347: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307 raeburn 1348: } else {
1349: $domneedscache = $dom_in_use;
1.1129 raeburn 1350: }
1351: }
1352: if (ref($result) eq 'HASH') {
1.1191 raeburn 1353: ($is_balancer,$currtargets,$currrules) =
1354: &check_balancer_result($result,@hosts);
1.1129 raeburn 1355: if ($is_balancer) {
1356: if (ref($currrules) eq 'HASH') {
1357: if ($homeintdom) {
1358: if ($uname ne '') {
1359: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1360: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1361: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1362: $rule_in_effect = $currrules->{'_LC_author'};
1363: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1364: $rule_in_effect = $currrules->{'_LC_adv'}
1365: }
1366: }
1367: if ($rule_in_effect eq '') {
1368: my %userenv = &userenvironment($udom,$uname,'inststatus');
1369: if ($userenv{'inststatus'} ne '') {
1370: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1371: my ($othertitle,$usertypes,$types) =
1372: &Apache::loncommon::sorted_inst_types($udom);
1373: if (ref($types) eq 'ARRAY') {
1374: foreach my $type (@{$types}) {
1375: if (grep(/^\Q$type\E$/,@statuses)) {
1376: if (exists($currrules->{$type})) {
1377: $rule_in_effect = $currrules->{$type};
1378: }
1379: }
1380: }
1381: }
1382: } else {
1383: if (exists($currrules->{'default'})) {
1384: $rule_in_effect = $currrules->{'default'};
1385: }
1386: }
1387: }
1388: } else {
1389: if (exists($currrules->{'default'})) {
1390: $rule_in_effect = $currrules->{'default'};
1391: }
1392: }
1393: } else {
1394: if ($currrules->{'_LC_external'} ne '') {
1395: $rule_in_effect = $currrules->{'_LC_external'};
1396: }
1397: }
1398: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1399: $uname,$udom);
1400: }
1401: }
1402: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239 raeburn 1403: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129 raeburn 1404: unless (defined($cached)) {
1405: my %domconfig =
1406: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1407: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307 raeburn 1408: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1409: } else {
1410: $domneedscache = $serverhomedom;
1.1129 raeburn 1411: }
1412: }
1413: if (ref($result) eq 'HASH') {
1.1191 raeburn 1414: ($is_balancer,$currtargets,$currrules) =
1415: &check_balancer_result($result,@hosts);
1416: if ($is_balancer) {
1.1129 raeburn 1417: if (ref($currrules) eq 'HASH') {
1418: if ($currrules->{'_LC_internetdom'} ne '') {
1419: $rule_in_effect = $currrules->{'_LC_internetdom'};
1420: }
1421: }
1422: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1423: $uname,$udom);
1424: }
1425: } else {
1426: if ($perlvar{'lonBalancer'} eq 'yes') {
1427: $is_balancer = 1;
1428: $offloadto = &this_host_spares($dom_in_use);
1429: }
1.1307 raeburn 1430: unless (defined($cached)) {
1431: $domneedscache = $serverhomedom;
1432: }
1.1129 raeburn 1433: }
1434: } else {
1435: if ($perlvar{'lonBalancer'} eq 'yes') {
1436: $is_balancer = 1;
1437: $offloadto = &this_host_spares($dom_in_use);
1438: }
1.1307 raeburn 1439: unless (defined($cached)) {
1440: $domneedscache = $serverhomedom;
1441: }
1442: }
1443: if ($domneedscache) {
1444: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129 raeburn 1445: }
1.1176 raeburn 1446: if ($is_balancer) {
1447: my $lowest_load = 30000;
1448: if (ref($offloadto) eq 'HASH') {
1449: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1450: foreach my $try_server (@{$offloadto->{'primary'}}) {
1451: ($otherserver,$lowest_load) =
1452: &compare_server_load($try_server,$otherserver,$lowest_load);
1453: }
1.1129 raeburn 1454: }
1.1176 raeburn 1455: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129 raeburn 1456:
1.1176 raeburn 1457: if (!$found_server) {
1458: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1459: foreach my $try_server (@{$offloadto->{'default'}}) {
1460: ($otherserver,$lowest_load) =
1461: &compare_server_load($try_server,$otherserver,$lowest_load);
1462: }
1463: }
1464: }
1465: } elsif (ref($offloadto) eq 'ARRAY') {
1466: if (@{$offloadto} == 1) {
1467: $otherserver = $offloadto->[0];
1468: } elsif (@{$offloadto} > 1) {
1469: foreach my $try_server (@{$offloadto}) {
1.1129 raeburn 1470: ($otherserver,$lowest_load) =
1471: &compare_server_load($try_server,$otherserver,$lowest_load);
1472: }
1473: }
1474: }
1.1331 ! raeburn 1475: unless ($caller eq 'login') {
! 1476: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
! 1477: $is_balancer = 0;
! 1478: if ($uname ne '' && $udom ne '') {
! 1479: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1176 raeburn 1480:
1.1331 ! raeburn 1481: &appenv({'user.loadbalexempt' => $lonhost,
! 1482: 'user.loadbalcheck.time' => time});
! 1483: }
1.1176 raeburn 1484: }
1.1129 raeburn 1485: }
1486: }
1487: }
1488: return ($is_balancer,$otherserver);
1489: }
1490:
1.1191 raeburn 1491: sub check_balancer_result {
1492: my ($result,@hosts) = @_;
1493: my ($is_balancer,$currtargets,$currrules);
1494: if (ref($result) eq 'HASH') {
1495: if ($result->{'lonhost'} ne '') {
1496: my $currbalancer = $result->{'lonhost'};
1497: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1498: $is_balancer = 1;
1499: $currtargets = $result->{'targets'};
1500: $currrules = $result->{'rules'};
1501: }
1502: } else {
1503: foreach my $key (keys(%{$result})) {
1504: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1505: (ref($result->{$key}) eq 'HASH')) {
1506: $is_balancer = 1;
1507: $currrules = $result->{$key}{'rules'};
1508: $currtargets = $result->{$key}{'targets'};
1509: last;
1510: }
1511: }
1512: }
1513: }
1514: return ($is_balancer,$currtargets,$currrules);
1515: }
1516:
1.1129 raeburn 1517: sub get_loadbalancer_targets {
1518: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1519: my $offloadto;
1.1175 raeburn 1520: if ($rule_in_effect eq 'none') {
1521: return [$perlvar{'lonHostID'}];
1522: } elsif ($rule_in_effect eq '') {
1.1129 raeburn 1523: $offloadto = $currtargets;
1524: } else {
1525: if ($rule_in_effect eq 'homeserver') {
1526: my $homeserver = &homeserver($uname,$udom);
1527: if ($homeserver ne 'no_host') {
1528: $offloadto = [$homeserver];
1529: }
1530: } elsif ($rule_in_effect eq 'externalbalancer') {
1531: my %domconfig =
1532: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1533: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1534: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1535: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1536: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1537: }
1538: }
1539: } else {
1.1178 raeburn 1540: my %servers = &internet_dom_servers($udom);
1.1129 raeburn 1541: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1542: if (&hostname($remotebalancer) ne '') {
1543: $offloadto = [$remotebalancer];
1544: }
1545: }
1546: } elsif (&hostname($rule_in_effect) ne '') {
1547: $offloadto = [$rule_in_effect];
1548: }
1549: }
1550: return $offloadto;
1551: }
1552:
1.1127 raeburn 1553: sub internet_dom_servers {
1554: my ($dom) = @_;
1555: my (%uniqservers,%servers);
1556: my $primaryserver = &hostname(&domain($dom,'primary'));
1557: my @machinedoms = &machine_domains($primaryserver);
1558: foreach my $mdom (@machinedoms) {
1559: my %currservers = %servers;
1560: my %server = &get_servers($mdom);
1561: %servers = (%currservers,%server);
1562: }
1563: my %by_hostname;
1564: foreach my $id (keys(%servers)) {
1565: push(@{$by_hostname{$servers{$id}}},$id);
1566: }
1567: foreach my $hostname (sort(keys(%by_hostname))) {
1568: if (@{$by_hostname{$hostname}} > 1) {
1569: my $match = 0;
1570: foreach my $id (@{$by_hostname{$hostname}}) {
1571: if (&host_domain($id) eq $dom) {
1572: $uniqservers{$id} = $hostname;
1573: $match = 1;
1574: }
1575: }
1576: unless ($match) {
1577: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1578: }
1579: } else {
1580: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1581: }
1582: }
1583: return %uniqservers;
1584: }
1585:
1.1 albertel 1586: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1587:
1.599 albertel 1588: my %homecache;
1.1 albertel 1589: sub homeserver {
1.230 stredwic 1590: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1591: my $index="$uname:$udom";
1.426 albertel 1592:
1.599 albertel 1593: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1594:
1595: my %servers = &get_servers($udom,'library');
1596: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1597: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1598: exists($badServerCache{$tryserver}));
1.841 albertel 1599:
1600: my $answer=reply("home:$udom:$uname",$tryserver);
1601: if ($answer eq 'found') {
1602: delete($badServerCache{$tryserver});
1603: return $homecache{$index}=$tryserver;
1604: } elsif ($answer eq 'no_host') {
1605: $badServerCache{$tryserver}=1;
1606: }
1.1 albertel 1607: }
1608: return 'no_host';
1.70 www 1609: }
1610:
1.1300 raeburn 1611: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70 www 1612:
1613: sub idget {
1.1300 raeburn 1614: my ($udom,$idsref,$namespace)=@_;
1.70 www 1615: my %returnhash=();
1.1300 raeburn 1616: my @ids=();
1617: if (ref($idsref) eq 'ARRAY') {
1618: @ids = @{$idsref};
1619: } else {
1620: return %returnhash;
1621: }
1622: if ($namespace eq '') {
1623: $namespace = 'ids';
1624: }
1.70 www 1625:
1.841 albertel 1626: my %servers = &get_servers($udom,'library');
1627: foreach my $tryserver (keys(%servers)) {
1.1299 raeburn 1628: my $idlist=join('&', map { &escape($_); } @ids);
1.1300 raeburn 1629: if ($namespace eq 'ids') {
1630: $idlist=~tr/A-Z/a-z/;
1631: }
1632: my $reply;
1633: if ($namespace eq 'ids') {
1634: $reply=&reply("idget:$udom:".$idlist,$tryserver);
1635: } else {
1636: $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
1637: }
1.841 albertel 1638: my @answer=();
1639: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1640: @answer=split(/\&/,$reply);
1641: } ;
1642: my $i;
1643: for ($i=0;$i<=$#ids;$i++) {
1644: if ($answer[$i]) {
1.1299 raeburn 1645: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300 raeburn 1646: }
1.841 albertel 1647: }
1.1300 raeburn 1648: }
1.70 www 1649: return %returnhash;
1650: }
1651:
1652: # ------------------------------------- Find the IDs behind a list of usernames
1653:
1654: sub idrget {
1655: my ($udom,@unames)=@_;
1656: my %returnhash=();
1.800 albertel 1657: foreach my $uname (@unames) {
1658: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1659: }
1.70 www 1660: return %returnhash;
1661: }
1662:
1.1300 raeburn 1663: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70 www 1664:
1665: sub idput {
1.1300 raeburn 1666: my ($udom,$idsref,$uhom,$namespace)=@_;
1.70 www 1667: my %servers=();
1.1300 raeburn 1668: my %ids=();
1669: my %byid = ();
1670: if (ref($idsref) eq 'HASH') {
1671: %ids=%{$idsref};
1672: }
1673: if ($namespace eq '') {
1674: $namespace = 'ids';
1675: }
1.800 albertel 1676: foreach my $uname (keys(%ids)) {
1677: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300 raeburn 1678: if ($uhom eq '') {
1679: $uhom=&homeserver($uname,$udom);
1680: }
1.70 www 1681: if ($uhom ne 'no_host') {
1.800 albertel 1682: my $esc_unam=&escape($uname);
1.1300 raeburn 1683: if ($namespace eq 'ids') {
1684: my $id=&escape($ids{$uname});
1685: $id=~tr/A-Z/a-z/;
1686: my $esc_unam=&escape($uname);
1687: $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70 www 1688: } else {
1.1300 raeburn 1689: my @currids = split(/,/,$ids{$uname});
1690: foreach my $id (@currids) {
1691: $byid{$uhom}{$id} .= $uname.',';
1692: }
1693: }
1694: }
1695: }
1696: if ($namespace eq 'clickers') {
1697: foreach my $server (keys(%byid)) {
1698: if (ref($byid{$server}) eq 'HASH') {
1699: foreach my $id (keys(%{$byid{$server}})) {
1700: $byid{$server} =~ s/,$//;
1701: $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&';
1702: }
1.70 www 1703: }
1704: }
1.191 harris41 1705: }
1.800 albertel 1706: foreach my $server (keys(%servers)) {
1.1300 raeburn 1707: $servers{$server} =~ s/\&$//;
1708: if ($namespace eq 'ids') {
1709: &critical('idput:'.$udom.':'.$servers{$server},$server);
1710: } else {
1711: &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
1712: }
1.191 harris41 1713: }
1.344 www 1714: }
1715:
1.1300 raeburn 1716: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231 raeburn 1717:
1718: sub iddel {
1.1300 raeburn 1719: my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231 raeburn 1720: my %result=();
1.1300 raeburn 1721: my %ids=();
1722: my %byid = ();
1723: if (ref($idshashref) eq 'HASH') {
1724: %ids=%{$idshashref};
1725: } else {
1.1231 raeburn 1726: return %result;
1727: }
1.1300 raeburn 1728: if ($namespace eq '') {
1729: $namespace = 'ids';
1730: }
1.1231 raeburn 1731: my %servers=();
1.1300 raeburn 1732: while (my ($id,$unamestr) = each(%ids)) {
1733: if ($namespace eq 'ids') {
1734: my $uhom = $uhome;
1735: if ($uhom eq '') {
1736: $uhom=&homeserver($unamestr,$udom);
1737: }
1738: if ($uhom ne 'no_host') {
1739: $servers{$uhom}.='&'.&escape($id);
1740: }
1741: } else {
1742: my @curritems = split(/,/,$ids{$id});
1743: foreach my $uname (@curritems) {
1744: my $uhom = $uhome;
1745: if ($uhom eq '') {
1746: $uhom=&homeserver($uname,$udom);
1747: }
1748: if ($uhom ne 'no_host') {
1749: $byid{$uhom}{$id} .= $uname.',';
1750: }
1751: }
1.1231 raeburn 1752: }
1.1300 raeburn 1753: }
1754: if ($namespace eq 'clickers') {
1755: foreach my $server (keys(%byid)) {
1756: if (ref($byid{$server}) eq 'HASH') {
1757: foreach my $id (keys(%{$byid{$server}})) {
1758: $byid{$server}{$id} =~ s/,$//;
1759: $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
1760: }
1.1231 raeburn 1761: }
1762: }
1763: }
1764: foreach my $server (keys(%servers)) {
1.1300 raeburn 1765: $servers{$server} =~ s/\&$//;
1766: if ($namespace eq 'ids') {
1767: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1768: } elsif ($namespace eq 'clickers') {
1769: $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
1770: }
1.1231 raeburn 1771: }
1772: return %result;
1773: }
1774:
1.1300 raeburn 1775: # ----- Update clicker ID-to-username look-ups in clickers.db on library server
1776:
1777: sub updateclickers {
1778: my ($udom,$action,$idshashref,$uhome,$critical) = @_;
1779: my %clickers;
1780: if (ref($idshashref) eq 'HASH') {
1781: %clickers=%{$idshashref};
1782: } else {
1783: return;
1784: }
1785: my $items='';
1786: foreach my $item (keys(%clickers)) {
1787: $items.=&escape($item).'='.&escape($clickers{$item}).'&';
1788: }
1789: $items=~s/\&$//;
1790: my $request = "updateclickers:$udom:$action:$items";
1791: if ($critical) {
1792: return &critical($request,$uhome);
1793: } else {
1794: return &reply($request,$uhome);
1795: }
1796: }
1797:
1.1023 raeburn 1798: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1799: sub dump_dom {
1.1165 droeschl 1800: my ($namespace, $udom, $regexp) = @_;
1801:
1802: $udom ||= $env{'user.domain'};
1803:
1804: return () unless $udom;
1805:
1806: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012 raeburn 1807: }
1808:
1.1023 raeburn 1809: # ------------------------------------------ get items from domain db files
1.806 raeburn 1810:
1811: sub get_dom {
1.860 raeburn 1812: my ($namespace,$storearr,$udom,$uhome)=@_;
1.1267 raeburn 1813: return if ($udom eq 'public');
1.806 raeburn 1814: my $items='';
1815: foreach my $item (@$storearr) {
1816: $items.=&escape($item).'&';
1817: }
1818: $items=~s/\&$//;
1.860 raeburn 1819: if (!$udom) {
1820: $udom=$env{'user.domain'};
1.1267 raeburn 1821: return if ($udom eq 'public');
1.860 raeburn 1822: if (defined(&domain($udom,'primary'))) {
1823: $uhome=&domain($udom,'primary');
1824: } else {
1.874 albertel 1825: undef($uhome);
1.860 raeburn 1826: }
1827: } else {
1828: if (!$uhome) {
1829: if (defined(&domain($udom,'primary'))) {
1830: $uhome=&domain($udom,'primary');
1831: }
1832: }
1833: }
1834: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1835: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1836: my %returnhash;
1.875 albertel 1837: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1838: return %returnhash;
1839: }
1.806 raeburn 1840: my @pairs=split(/\&/,$rep);
1841: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1842: return @pairs;
1843: }
1844: my $i=0;
1845: foreach my $item (@$storearr) {
1846: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1847: $i++;
1848: }
1849: return %returnhash;
1850: } else {
1.880 banghart 1851: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1852: }
1853: }
1854:
1855: # -------------------------------------------- put items in domain db files
1856:
1857: sub put_dom {
1.860 raeburn 1858: my ($namespace,$storehash,$udom,$uhome)=@_;
1859: if (!$udom) {
1860: $udom=$env{'user.domain'};
1861: if (defined(&domain($udom,'primary'))) {
1862: $uhome=&domain($udom,'primary');
1863: } else {
1.874 albertel 1864: undef($uhome);
1.860 raeburn 1865: }
1866: } else {
1867: if (!$uhome) {
1868: if (defined(&domain($udom,'primary'))) {
1869: $uhome=&domain($udom,'primary');
1870: }
1871: }
1872: }
1873: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1874: my $items='';
1875: foreach my $item (keys(%$storehash)) {
1876: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1877: }
1878: $items=~s/\&$//;
1879: return &reply("putdom:$udom:$namespace:$items",$uhome);
1880: } else {
1.860 raeburn 1881: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1882: }
1883: }
1884:
1.1023 raeburn 1885: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1886: sub newput_dom {
1.1023 raeburn 1887: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1888: my $result;
1889: if (!$udom) {
1890: $udom=$env{'user.domain'};
1891: }
1.1023 raeburn 1892: if ($udom) {
1893: my $uname = &get_domainconfiguser($udom);
1894: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1895: }
1896: return $result;
1897: }
1898:
1.1023 raeburn 1899: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1900: sub del_dom {
1.1023 raeburn 1901: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1902: if (ref($storearr) eq 'ARRAY') {
1903: if (!$udom) {
1904: $udom=$env{'user.domain'};
1905: }
1.1023 raeburn 1906: if ($udom) {
1907: my $uname = &get_domainconfiguser($udom);
1908: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1909: }
1910: }
1911: }
1912:
1.1023 raeburn 1913: # ----------------------------------construct domainconfig user for a domain
1914: sub get_domainconfiguser {
1915: my ($udom) = @_;
1916: return $udom.'-domainconfig';
1917: }
1918:
1.837 raeburn 1919: sub retrieve_inst_usertypes {
1920: my ($udom) = @_;
1921: my (%returnhash,@order);
1.989 raeburn 1922: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1923: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1924: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256 raeburn 1925: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989 raeburn 1926: } else {
1927: if (defined(&domain($udom,'primary'))) {
1928: my $uhome=&domain($udom,'primary');
1929: my $rep=&reply("inst_usertypes:$udom",$uhome);
1930: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256 raeburn 1931: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989 raeburn 1932: return (\%returnhash,\@order);
1933: }
1934: my ($hashitems,$orderitems) = split(/:/,$rep);
1935: my @pairs=split(/\&/,$hashitems);
1936: foreach my $item (@pairs) {
1937: my ($key,$value)=split(/=/,$item,2);
1938: $key = &unescape($key);
1939: next if ($key =~ /^error: 2 /);
1940: $returnhash{$key}=&thaw_unescape($value);
1941: }
1942: my @esc_order = split(/\&/,$orderitems);
1943: foreach my $item (@esc_order) {
1944: push(@order,&unescape($item));
1945: }
1946: } else {
1.1256 raeburn 1947: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837 raeburn 1948: }
1.1256 raeburn 1949: return (\%returnhash,\@order);
1.837 raeburn 1950: }
1951: }
1952:
1.868 raeburn 1953: sub is_domainimage {
1954: my ($url) = @_;
1.1306 raeburn 1955: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
1.868 raeburn 1956: if (&domain($1) ne '') {
1957: return '1';
1958: }
1959: }
1960: return;
1961: }
1962:
1.899 raeburn 1963: sub inst_directory_query {
1964: my ($srch) = @_;
1965: my $udom = $srch->{'srchdomain'};
1966: my %results;
1967: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1968: my $outcome;
1.899 raeburn 1969: if ($homeserver ne '') {
1.904 albertel 1970: my $queryid=&reply("querysend:instdirsearch:".
1971: &escape($srch->{'srchby'}).':'.
1972: &escape($srch->{'srchterm'}).':'.
1973: &escape($srch->{'srchtype'}),$homeserver);
1974: my $host=&hostname($homeserver);
1975: if ($queryid !~/^\Q$host\E\_/) {
1976: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1977: return;
1978: }
1979: my $response = &get_query_reply($queryid);
1980: my $maxtries = 5;
1981: my $tries = 1;
1982: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1983: $response = &get_query_reply($queryid);
1984: $tries ++;
1985: }
1986:
1987: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 1988: if ($response eq 'unavailable') {
1989: $outcome = $response;
1990: } else {
1991: $outcome = 'ok';
1992: my @matches = split(/\n/,$response);
1993: foreach my $match (@matches) {
1994: my ($key,$value) = split(/=/,$match);
1995: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
1996: }
1.899 raeburn 1997: }
1998: }
1999: }
1.909 raeburn 2000: return ($outcome,%results);
1.899 raeburn 2001: }
2002:
2003: sub usersearch {
2004: my ($srch) = @_;
2005: my $dom = $srch->{'srchdomain'};
2006: my %results;
2007: my %libserv = &all_library();
2008: my $query = 'usersearch';
2009: foreach my $tryserver (keys(%libserv)) {
2010: if (&host_domain($tryserver) eq $dom) {
2011: my $host=&hostname($tryserver);
2012: my $queryid=
1.911 raeburn 2013: &reply("querysend:".&escape($query).':'.
2014: &escape($srch->{'srchby'}).':'.
1.899 raeburn 2015: &escape($srch->{'srchtype'}).':'.
2016: &escape($srch->{'srchterm'}),$tryserver);
2017: if ($queryid !~/^\Q$host\E\_/) {
2018: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 2019: next;
1.899 raeburn 2020: }
2021: my $reply = &get_query_reply($queryid);
2022: my $maxtries = 1;
2023: my $tries = 1;
2024: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2025: $reply = &get_query_reply($queryid);
2026: $tries ++;
2027: }
2028: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2029: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2030: } else {
1.911 raeburn 2031: my @matches;
2032: if ($reply =~ /\n/) {
2033: @matches = split(/\n/,$reply);
2034: } else {
2035: @matches = split(/\&/,$reply);
2036: }
1.899 raeburn 2037: foreach my $match (@matches) {
2038: my ($uname,$udom,%userhash);
1.911 raeburn 2039: foreach my $entry (split(/:/,$match)) {
2040: my ($key,$value) =
2041: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 2042: $userhash{$key} = $value;
2043: if ($key eq 'username') {
2044: $uname = $value;
2045: } elsif ($key eq 'domain') {
2046: $udom = $value;
1.911 raeburn 2047: }
1.899 raeburn 2048: }
2049: $results{$uname.':'.$udom} = \%userhash;
2050: }
2051: }
2052: }
2053: }
2054: return %results;
2055: }
2056:
1.912 raeburn 2057: sub get_instuser {
2058: my ($udom,$uname,$id) = @_;
2059: my $homeserver = &domain($udom,'primary');
2060: my ($outcome,%results);
2061: if ($homeserver ne '') {
2062: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2063: &escape($id).':'.&escape($udom),$homeserver);
2064: my $host=&hostname($homeserver);
2065: if ($queryid !~/^\Q$host\E\_/) {
2066: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2067: return;
2068: }
2069: my $response = &get_query_reply($queryid);
2070: my $maxtries = 5;
2071: my $tries = 1;
2072: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2073: $response = &get_query_reply($queryid);
2074: $tries ++;
2075: }
2076: if (!&error($response) && $response ne 'refused') {
2077: if ($response eq 'unavailable') {
2078: $outcome = $response;
2079: } else {
2080: $outcome = 'ok';
2081: my @matches = split(/\n/,$response);
2082: foreach my $match (@matches) {
2083: my ($key,$value) = split(/=/,$match);
2084: $results{&unescape($key)} = &thaw_unescape($value);
2085: }
2086: }
2087: }
2088: }
2089: my %userinfo;
2090: if (ref($results{$uname}) eq 'HASH') {
2091: %userinfo = %{$results{$uname}};
2092: }
2093: return ($outcome,%userinfo);
2094: }
2095:
1.1290 raeburn 2096: sub get_multiple_instusers {
2097: my ($udom,$users,$caller) = @_;
2098: my ($outcome,$results);
2099: if (ref($users) eq 'HASH') {
2100: my $count = keys(%{$users});
2101: my $requested = &freeze_escape($users);
2102: my $homeserver = &domain($udom,'primary');
2103: if ($homeserver ne '') {
2104: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2105: my $host=&hostname($homeserver);
2106: if ($queryid !~/^\Q$host\E\_/) {
2107: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2108: ' for host: '.$homeserver.'in domain '.$udom);
2109: return ($outcome,$results);
2110: }
2111: my $response = &get_query_reply($queryid);
2112: my $maxtries = 5;
2113: if ($count > 100) {
2114: $maxtries = 1+int($count/20);
2115: }
2116: my $tries = 1;
2117: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2118: $response = &get_query_reply($queryid);
2119: $tries ++;
2120: }
2121: if ($response eq '') {
2122: $results = {};
2123: foreach my $key (keys(%{$users})) {
2124: my ($uname,$id);
2125: if ($caller eq 'id') {
2126: $id = $key;
2127: } else {
2128: $uname = $key;
2129: }
2130: my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291 raeburn 2131: $outcome = $resp;
1.1290 raeburn 2132: if ($resp eq 'ok') {
2133: %{$results} = (%{$results}, %info);
2134: } else {
2135: last;
2136: }
2137: }
2138: } elsif(!&error($response) && ($response ne 'refused')) {
2139: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2140: $outcome = $response;
2141: } else {
1.1291 raeburn 2142: ($outcome,my $userdata) = split(/=/,$response,2);
1.1290 raeburn 2143: if ($outcome eq 'ok') {
2144: $results = &thaw_unescape($userdata);
2145: }
2146: }
2147: }
2148: }
2149: }
2150: return ($outcome,$results);
2151: }
2152:
1.912 raeburn 2153: sub inst_rulecheck {
1.923 raeburn 2154: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 2155: my %returnhash;
2156: if ($udom ne '') {
2157: if (ref($rules) eq 'ARRAY') {
2158: @{$rules} = map {&escape($_);} (@{$rules});
2159: my $rulestr = join(':',@{$rules});
2160: my $homeserver=&domain($udom,'primary');
2161: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2162: my $response;
2163: if ($item eq 'username') {
2164: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2165: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 2166: $homeserver));
1.923 raeburn 2167: } elsif ($item eq 'id') {
2168: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2169: ':'.&escape($id).':'.$rulestr,
2170: $homeserver));
1.945 raeburn 2171: } elsif ($item eq 'selfcreate') {
2172: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 2173: &escape($udom).':'.&escape($uname).
2174: ':'.$rulestr,$homeserver));
1.923 raeburn 2175: }
1.912 raeburn 2176: if ($response ne 'refused') {
2177: my @pairs=split(/\&/,$response);
2178: foreach my $item (@pairs) {
2179: my ($key,$value)=split(/=/,$item,2);
2180: $key = &unescape($key);
2181: next if ($key =~ /^error: 2 /);
2182: $returnhash{$key}=&thaw_unescape($value);
2183: }
2184: }
2185: }
2186: }
2187: }
2188: return %returnhash;
2189: }
2190:
2191: sub inst_userrules {
1.923 raeburn 2192: my ($udom,$check) = @_;
1.912 raeburn 2193: my (%ruleshash,@ruleorder);
2194: if ($udom ne '') {
2195: my $homeserver=&domain($udom,'primary');
2196: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2197: my $response;
2198: if ($check eq 'id') {
2199: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 2200: $homeserver);
1.943 raeburn 2201: } elsif ($check eq 'email') {
2202: $response=&reply('instemailrules:'.&escape($udom),
2203: $homeserver);
1.923 raeburn 2204: } else {
2205: $response=&reply('instuserrules:'.&escape($udom),
2206: $homeserver);
2207: }
1.912 raeburn 2208: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 2209: ($response ne 'unknown_cmd') &&
1.912 raeburn 2210: ($response ne 'no_such_host')) {
2211: my ($hashitems,$orderitems) = split(/:/,$response);
2212: my @pairs=split(/\&/,$hashitems);
2213: foreach my $item (@pairs) {
2214: my ($key,$value)=split(/=/,$item,2);
2215: $key = &unescape($key);
2216: next if ($key =~ /^error: 2 /);
2217: $ruleshash{$key}=&thaw_unescape($value);
2218: }
2219: my @esc_order = split(/\&/,$orderitems);
2220: foreach my $item (@esc_order) {
2221: push(@ruleorder,&unescape($item));
2222: }
2223: }
2224: }
2225: }
2226: return (\%ruleshash,\@ruleorder);
2227: }
2228:
1.976 raeburn 2229: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 2230:
2231: sub get_domain_defaults {
1.1240 raeburn 2232: my ($domain,$ignore_cache) = @_;
1.1242 raeburn 2233: return if (($domain eq '') || ($domain eq 'public'));
1.943 raeburn 2234: my $cachetime = 60*60*24;
1.1240 raeburn 2235: unless ($ignore_cache) {
2236: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2237: if (defined($cached)) {
2238: if (ref($result) eq 'HASH') {
2239: return %{$result};
2240: }
1.943 raeburn 2241: }
2242: }
2243: my %domdefaults;
2244: my %domconfig =
1.989 raeburn 2245: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 2246: 'requestcourses','inststatus',
1.1183 raeburn 2247: 'coursedefaults','usersessions',
1.1258 raeburn 2248: 'requestauthor','selfenrollment',
1.1320 raeburn 2249: 'coursecategories','ssl','autoenroll',
2250: 'trust'],$domain);
1.1305 raeburn 2251: my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943 raeburn 2252: if (ref($domconfig{'defaults'}) eq 'HASH') {
2253: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2254: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2255: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 2256: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 2257: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147 raeburn 2258: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943 raeburn 2259: } else {
2260: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2261: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2262: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2263: }
1.976 raeburn 2264: if (ref($domconfig{'quotas'}) eq 'HASH') {
2265: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2266: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2267: } else {
2268: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229 raeburn 2269: }
1.1177 raeburn 2270: my @usertools = ('aboutme','blog','webdav','portfolio');
1.976 raeburn 2271: foreach my $item (@usertools) {
2272: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2273: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2274: }
2275: }
1.1229 raeburn 2276: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2277: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2278: }
1.976 raeburn 2279: }
1.985 raeburn 2280: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305 raeburn 2281: foreach my $item ('official','unofficial','community','textbook','placement') {
1.985 raeburn 2282: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2283: }
2284: }
1.1183 raeburn 2285: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2286: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2287: }
1.989 raeburn 2288: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256 raeburn 2289: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989 raeburn 2290: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2291: }
2292: }
1.1047 raeburn 2293: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230 raeburn 2294: $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277 raeburn 2295: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
2296: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2297: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2298: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2299: }
1.1254 raeburn 2300: foreach my $type (@coursetypes) {
2301: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2302: unless ($type eq 'community') {
2303: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2304: }
2305: }
2306: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2307: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2308: }
1.1277 raeburn 2309: if ($domdefaults{'postsubmit'} eq 'on') {
2310: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2311: $domdefaults{$type.'postsubtimeout'} =
2312: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2313: }
2314: }
1.1230 raeburn 2315: }
1.1286 raeburn 2316: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2317: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2318: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2319: if (@clonecodes) {
2320: $domdefaults{'canclone'} = join('+',@clonecodes);
2321: }
2322: }
2323: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2324: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2325: }
1.1047 raeburn 2326: }
1.1073 raeburn 2327: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2328: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2329: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2330: }
2331: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2332: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2333: }
1.1279 raeburn 2334: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2335: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2336: }
1.1073 raeburn 2337: }
1.1254 raeburn 2338: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2339: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2340: my @settings = ('types','registered','enroll_dates','access_dates','section',
2341: 'approval','limit');
2342: foreach my $type (@coursetypes) {
2343: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2344: my @mgrdc = ();
2345: foreach my $item (@settings) {
2346: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2347: push(@mgrdc,$item);
2348: }
2349: }
2350: if (@mgrdc) {
2351: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2352: }
2353: }
2354: }
2355: }
2356: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2357: foreach my $type (@coursetypes) {
2358: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2359: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2360: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2361: }
2362: }
2363: }
2364: }
2365: }
1.1258 raeburn 2366: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2367: $domdefaults{'catauth'} = 'std';
2368: $domdefaults{'catunauth'} = 'std';
2369: if ($domconfig{'coursecategories'}{'auth'}) {
2370: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2371: }
2372: if ($domconfig{'coursecategories'}{'unauth'}) {
2373: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2374: }
2375: }
1.1315 raeburn 2376: if (ref($domconfig{'ssl'}) eq 'HASH') {
2377: if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
2378: $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
2379: }
2380: if (ref($domconfig{'ssl'}{'connect'}) eq 'HASH') {
2381: $domdefaults{'connect'} = $domconfig{'ssl'}{'connect'};
2382: }
2383: }
1.1320 raeburn 2384: if (ref($domconfig{'trust'}) eq 'HASH') {
2385: my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
2386: foreach my $prefix (@prefixes) {
2387: if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
2388: $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
2389: }
2390: }
2391: }
1.1314 raeburn 2392: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2393: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
2394: }
1.1219 raeburn 2395: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943 raeburn 2396: return %domdefaults;
2397: }
2398:
1.1311 raeburn 2399: sub course_portal_url {
2400: my ($cnum,$cdom) = @_;
2401: my $chome = &homeserver($cnum,$cdom);
2402: my $hostname = &hostname($chome);
2403: my $protocol = $protocol{$chome};
2404: $protocol = 'http' if ($protocol ne 'https');
2405: my %domdefaults = &get_domain_defaults($cdom);
2406: my $firsturl;
2407: if ($domdefaults{'portal_def'}) {
2408: $firsturl = $domdefaults{'portal_def'};
2409: } else {
2410: $firsturl = $protocol.'://'.$hostname;
2411: }
2412: return $firsturl;
2413: }
2414:
1.344 www 2415: # --------------------------------------------------- Assign a key to a student
2416:
2417: sub assign_access_key {
1.364 www 2418: #
2419: # a valid key looks like uname:udom#comments
2420: # comments are being appended
2421: #
1.498 www 2422: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2423: $kdom=
1.620 albertel 2424: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 2425: $knum=
1.620 albertel 2426: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 2427: $cdom=
1.620 albertel 2428: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2429: $cnum=
1.620 albertel 2430: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2431: $udom=$env{'user.name'} unless (defined($udom));
2432: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 2433: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 2434: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 2435: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 2436: # assigned to this person
2437: # - this should not happen,
1.345 www 2438: # unless something went wrong
2439: # the first time around
2440: # ready to assign
1.364 www 2441: $logentry=$1.'; '.$logentry;
1.496 www 2442: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 2443: $kdom,$knum) eq 'ok') {
1.345 www 2444: # key now belongs to user
1.346 www 2445: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 2446: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 2447: &appenv({'environment.'.$envkey => $ckey});
1.345 www 2448: return 'ok';
2449: } else {
2450: return
2451: 'error: Count not permanently assign key, will need to be re-entered later.';
2452: }
2453: } else {
2454: return 'error: Could not assign key, try again later.';
2455: }
1.364 www 2456: } elsif (!$existing{$ckey}) {
1.345 www 2457: # the key does not exist
2458: return 'error: The key does not exist';
2459: } else {
2460: # the key is somebody else's
2461: return 'error: The key is already in use';
2462: }
1.344 www 2463: }
2464:
1.364 www 2465: # ------------------------------------------ put an additional comment on a key
2466:
2467: sub comment_access_key {
2468: #
2469: # a valid key looks like uname:udom#comments
2470: # comments are being appended
2471: #
2472: my ($ckey,$cdom,$cnum,$logentry)=@_;
2473: $cdom=
1.620 albertel 2474: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 2475: $cnum=
1.620 albertel 2476: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 2477: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2478: if ($existing{$ckey}) {
2479: $existing{$ckey}.='; '.$logentry;
2480: # ready to assign
1.367 www 2481: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 2482: $cdom,$cnum) eq 'ok') {
2483: return 'ok';
2484: } else {
2485: return 'error: Count not store comment.';
2486: }
2487: } else {
2488: # the key does not exist
2489: return 'error: The key does not exist';
2490: }
2491: }
2492:
1.344 www 2493: # ------------------------------------------------------ Generate a set of keys
2494:
2495: sub generate_access_keys {
1.364 www 2496: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 2497: $cdom=
1.620 albertel 2498: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2499: $cnum=
1.620 albertel 2500: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 2501: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 2502: unless (($cdom) && ($cnum)) { return 0; }
2503: if ($number>10000) { return 0; }
2504: sleep(2); # make sure don't get same seed twice
2505: srand(time()^($$+($$<<15))); # from "Programming Perl"
2506: my $total=0;
2507: for (my $i=1;$i<=$number;$i++) {
2508: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2509: sprintf("%lx",int(100000*rand)).'-'.
2510: sprintf("%lx",int(100000*rand));
2511: $newkey=~s/1/g/g; # folks mix up 1 and l
2512: $newkey=~s/0/h/g; # and also 0 and O
2513: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2514: if ($existing{$newkey}) {
2515: $i--;
2516: } else {
1.364 www 2517: if (&put('accesskeys',
2518: { $newkey => '# generated '.localtime().
1.620 albertel 2519: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 2520: '; '.$logentry },
2521: $cdom,$cnum) eq 'ok') {
1.344 www 2522: $total++;
2523: }
2524: }
2525: }
1.620 albertel 2526: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 2527: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2528: return $total;
2529: }
2530:
2531: # ------------------------------------------------------- Validate an accesskey
2532:
2533: sub validate_access_key {
2534: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2535: $cdom=
1.620 albertel 2536: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2537: $cnum=
1.620 albertel 2538: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2539: $udom=$env{'user.domain'} unless (defined($udom));
2540: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 2541: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 2542: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 2543: }
2544:
2545: # ------------------------------------- Find the section of student in a course
1.652 albertel 2546: sub devalidate_getsection_cache {
2547: my ($udom,$unam,$courseid)=@_;
2548: my $hashid="$udom:$unam:$courseid";
2549: &devalidate_cache_new('getsection',$hashid);
2550: }
1.298 matthew 2551:
1.815 albertel 2552: sub courseid_to_courseurl {
2553: my ($courseid) = @_;
2554: #already url style courseid
2555: return $courseid if ($courseid =~ m{^/});
2556:
2557: if (exists($env{'course.'.$courseid.'.num'})) {
2558: my $cnum = $env{'course.'.$courseid.'.num'};
2559: my $cdom = $env{'course.'.$courseid.'.domain'};
2560: return "/$cdom/$cnum";
2561: }
2562:
2563: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
2564: if (exists($courseinfo{'num'})) {
2565: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
2566: }
2567:
2568: return undef;
2569: }
2570:
1.298 matthew 2571: sub getsection {
2572: my ($udom,$unam,$courseid)=@_;
1.599 albertel 2573: my $cachetime=1800;
1.551 albertel 2574:
2575: my $hashid="$udom:$unam:$courseid";
1.599 albertel 2576: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 2577: if (defined($cached)) { return $result; }
2578:
1.298 matthew 2579: my %Pending;
2580: my %Expired;
2581: #
2582: # Each role can either have not started yet (pending), be active,
2583: # or have expired.
2584: #
2585: # If there is an active role, we are done.
2586: #
2587: # If there is more than one role which has not started yet,
2588: # choose the one which will start sooner
2589: # If there is one role which has not started yet, return it.
2590: #
2591: # If there is more than one expired role, choose the one which ended last.
2592: # If there is a role which has expired, return it.
2593: #
1.815 albertel 2594: $courseid = &courseid_to_courseurl($courseid);
1.1166 raeburn 2595: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817 raeburn 2596: foreach my $key (keys(%roleshash)) {
1.479 albertel 2597: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 2598: my $section=$1;
2599: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 2600: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 2601: my $now=time;
1.548 albertel 2602: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 2603: $Expired{$end}=$section;
2604: next;
2605: }
1.548 albertel 2606: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 2607: $Pending{$start}=$section;
2608: next;
2609: }
1.599 albertel 2610: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 2611: }
2612: #
2613: # Presumedly there will be few matching roles from the above
2614: # loop and the sorting time will be negligible.
2615: if (scalar(keys(%Pending))) {
2616: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 2617: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 2618: }
2619: if (scalar(keys(%Expired))) {
2620: my @sorted = sort {$a <=> $b} keys(%Expired);
2621: my $time = pop(@sorted);
1.599 albertel 2622: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 2623: }
1.599 albertel 2624: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 2625: }
1.70 www 2626:
1.599 albertel 2627: sub save_cache {
2628: &purge_remembered();
1.722 albertel 2629: #&Apache::loncommon::validate_page();
1.620 albertel 2630: undef(%env);
1.780 albertel 2631: undef($env_loaded);
1.599 albertel 2632: }
1.452 albertel 2633:
1.599 albertel 2634: my $to_remember=-1;
2635: my %remembered;
2636: my %accessed;
2637: my $kicks=0;
2638: my $hits=0;
1.849 albertel 2639: sub make_key {
2640: my ($name,$id) = @_;
1.872 albertel 2641: if (length($id) > 65
2642: && length(&escape($id)) > 200) {
2643: $id=length($id).':'.&Digest::MD5::md5_hex($id);
2644: }
1.849 albertel 2645: return &escape($name.':'.$id);
2646: }
2647:
1.599 albertel 2648: sub devalidate_cache_new {
2649: my ($name,$id,$debug) = @_;
2650: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319 damieng 2651: my $remembered_id=$name.':'.$id;
1.849 albertel 2652: $id=&make_key($name,$id);
1.599 albertel 2653: $memcache->delete($id);
1.1319 damieng 2654: delete($remembered{$remembered_id});
2655: delete($accessed{$remembered_id});
1.599 albertel 2656: }
2657:
2658: sub is_cached_new {
2659: my ($name,$id,$debug) = @_;
1.1319 damieng 2660: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
2661: if (exists($remembered{$remembered_id})) {
2662: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
2663: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 2664: $hits++;
1.1319 damieng 2665: return ($remembered{$remembered_id},1);
1.599 albertel 2666: }
1.1319 damieng 2667: $id=&make_key($name,$id);
1.599 albertel 2668: my $value = $memcache->get($id);
2669: if (!(defined($value))) {
2670: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 2671: return (undef,undef);
1.416 albertel 2672: }
1.599 albertel 2673: if ($value eq '__undef__') {
2674: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
2675: $value=undef;
2676: }
1.1319 damieng 2677: &make_room($remembered_id,$value,$debug);
1.599 albertel 2678: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
2679: return ($value,1);
2680: }
2681:
2682: sub do_cache_new {
2683: my ($name,$id,$value,$time,$debug) = @_;
1.1319 damieng 2684: my $remembered_id=$name.':'.$id;
1.849 albertel 2685: $id=&make_key($name,$id);
1.599 albertel 2686: my $setvalue=$value;
2687: if (!defined($setvalue)) {
2688: $setvalue='__undef__';
2689: }
1.623 albertel 2690: if (!defined($time) ) {
2691: $time=600;
2692: }
1.599 albertel 2693: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 2694: my $result = $memcache->set($id,$setvalue,$time);
2695: if (! $result) {
1.872 albertel 2696: &logthis("caching of id -> $id failed");
1.910 albertel 2697: $memcache->disconnect_all();
1.872 albertel 2698: }
1.600 albertel 2699: # need to make a copy of $value
1.1319 damieng 2700: &make_room($remembered_id,$value,$debug);
1.599 albertel 2701: return $value;
2702: }
2703:
2704: sub make_room {
1.1319 damieng 2705: my ($remembered_id,$value,$debug)=@_;
1.919 albertel 2706:
1.1319 damieng 2707: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919 albertel 2708: : $value;
1.599 albertel 2709: if ($to_remember<0) { return; }
1.1319 damieng 2710: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 2711: if (scalar(keys(%remembered)) <= $to_remember) { return; }
2712: my $to_kick;
2713: my $max_time=0;
2714: foreach my $other (keys(%accessed)) {
2715: if (&tv_interval($accessed{$other}) > $max_time) {
2716: $to_kick=$other;
2717: $max_time=&tv_interval($accessed{$other});
2718: }
2719: }
2720: delete($remembered{$to_kick});
2721: delete($accessed{$to_kick});
2722: $kicks++;
2723: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 2724: return;
2725: }
2726:
1.599 albertel 2727: sub purge_remembered {
1.604 albertel 2728: #&logthis("Tossing ".scalar(keys(%remembered)));
2729: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 2730: undef(%remembered);
2731: undef(%accessed);
1.428 albertel 2732: }
1.70 www 2733: # ------------------------------------- Read an entry from a user's environment
2734:
2735: sub userenvironment {
2736: my ($udom,$unam,@what)=@_;
1.976 raeburn 2737: my $items;
2738: foreach my $item (@what) {
2739: $items.=&escape($item).'&';
2740: }
2741: $items=~s/\&$//;
1.70 www 2742: my %returnhash=();
1.1009 raeburn 2743: my $uhome = &homeserver($unam,$udom);
2744: unless ($uhome eq 'no_host') {
2745: my @answer=split(/\&/,
2746: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 2747: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
2748: return %returnhash;
2749: }
1.1009 raeburn 2750: my $i;
2751: for ($i=0;$i<=$#what;$i++) {
2752: $returnhash{$what[$i]}=&unescape($answer[$i]);
2753: }
1.70 www 2754: }
2755: return %returnhash;
1.1 albertel 2756: }
2757:
1.617 albertel 2758: # ---------------------------------------------------------- Get a studentphoto
2759: sub studentphoto {
2760: my ($udom,$unam,$ext) = @_;
2761: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 2762: if (defined($env{'request.course.id'})) {
1.708 raeburn 2763: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 2764: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
2765: return(&retrievestudentphoto($udom,$unam,$ext));
2766: } else {
2767: my ($result,$perm_reqd)=
1.707 albertel 2768: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2769: if ($result eq 'ok') {
2770: if (!($perm_reqd eq 'yes')) {
2771: return(&retrievestudentphoto($udom,$unam,$ext));
2772: }
2773: }
2774: }
2775: }
2776: } else {
2777: my ($result,$perm_reqd) =
1.707 albertel 2778: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2779: if ($result eq 'ok') {
2780: if (!($perm_reqd eq 'yes')) {
2781: return(&retrievestudentphoto($udom,$unam,$ext));
2782: }
2783: }
2784: }
2785: return '/adm/lonKaputt/lonlogo_broken.gif';
2786: }
2787:
2788: sub retrievestudentphoto {
2789: my ($udom,$unam,$ext,$type) = @_;
2790: my $home=&Apache::lonnet::homeserver($unam,$udom);
2791: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2792: if ($ret eq 'ok') {
2793: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2794: if ($type eq 'thumbnail') {
2795: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2796: }
2797: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2798: return $tokenurl;
2799: } else {
2800: if ($type eq 'thumbnail') {
2801: return '/adm/lonKaputt/genericstudent_tn.gif';
2802: } else {
2803: return '/adm/lonKaputt/lonlogo_broken.gif';
2804: }
1.617 albertel 2805: }
2806: }
2807:
1.263 www 2808: # -------------------------------------------------------------------- New chat
2809:
2810: sub chatsend {
1.724 raeburn 2811: my ($newentry,$anon,$group)=@_;
1.620 albertel 2812: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2813: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2814: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 2815: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 2816: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 2817: &escape($newentry)).':'.$group,$chome);
1.292 www 2818: }
2819:
2820: # ------------------------------------------ Find current version of a resource
2821:
2822: sub getversion {
2823: my $fname=&clutter(shift);
1.1189 raeburn 2824: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292 www 2825: return ¤tversion(&filelocation('',$fname));
2826: }
2827:
2828: sub currentversion {
2829: my $fname=shift;
2830: my $author=$fname;
2831: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2832: my ($udom,$uname)=split(/\//,$author);
1.1112 www 2833: my $home=&homeserver($uname,$udom);
1.292 www 2834: if ($home eq 'no_host') {
2835: return -1;
2836: }
1.1112 www 2837: my $answer=&reply("currentversion:$fname",$home);
1.292 www 2838: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2839: return -1;
2840: }
1.1112 www 2841: return $answer;
1.263 www 2842: }
2843:
1.1111 www 2844: #
2845: # Return special version number of resource if set by override, empty otherwise
2846: #
2847: sub usedversion {
2848: my $fname=shift;
2849: unless ($fname) { $fname=$env{'request.uri'}; }
2850: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
2851: if ($urlversion) { return $urlversion; }
2852: return '';
2853: }
2854:
1.1 albertel 2855: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 2856:
1.1 albertel 2857: sub subscribe {
2858: my $fname=shift;
1.761 raeburn 2859: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 2860: $fname=~s/[\n\r]//g;
1.1 albertel 2861: my $author=$fname;
2862: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2863: my ($udom,$uname)=split(/\//,$author);
2864: my $home=homeserver($uname,$udom);
1.335 albertel 2865: if ($home eq 'no_host') {
2866: return 'not_found';
1.1 albertel 2867: }
2868: my $answer=reply("sub:$fname",$home);
1.64 www 2869: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2870: $answer.=' by '.$home;
2871: }
1.1 albertel 2872: return $answer;
2873: }
2874:
1.8 www 2875: # -------------------------------------------------------------- Replicate file
2876:
2877: sub repcopy {
2878: my $filename=shift;
1.23 www 2879: $filename=~s/\/+/\//g;
1.1142 raeburn 2880: my $londocroot = $perlvar{'lonDocRoot'};
2881: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164 raeburn 2882: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142 raeburn 2883: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
2884: $filename=~m{^/*(uploaded|editupload)/}) {
1.538 albertel 2885: return &repcopy_userfile($filename);
2886: }
1.532 albertel 2887: $filename=~s/[\n\r]//g;
1.8 www 2888: my $transname="$filename.in.transfer";
1.828 www 2889: # FIXME: this should flock
1.607 raeburn 2890: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 2891: my $remoteurl=subscribe($filename);
1.64 www 2892: if ($remoteurl =~ /^con_lost by/) {
2893: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2894: return 'unavailable';
1.8 www 2895: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 2896: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 2897: return 'not_found';
1.64 www 2898: } elsif ($remoteurl =~ /^rejected by/) {
2899: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2900: return 'forbidden';
1.20 www 2901: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 2902: return 'ok';
1.8 www 2903: } else {
1.290 www 2904: my $author=$filename;
2905: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2906: my ($udom,$uname)=split(/\//,$author);
2907: my $home=homeserver($uname,$udom);
2908: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 2909: my @parts=split(/\//,$filename);
2910: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142 raeburn 2911: if ($path ne "$londocroot/res") {
1.8 www 2912: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 2913: return 'bad_request';
1.8 www 2914: }
2915: my $count;
2916: for ($count=5;$count<$#parts;$count++) {
2917: $path.="/$parts[$count]";
2918: if ((-e $path)!=1) {
2919: mkdir($path,0777);
2920: }
2921: }
2922: my $ua=new LWP::UserAgent;
2923: my $request=new HTTP::Request('GET',"$remoteurl");
2924: my $response=$ua->request($request,$transname);
2925: if ($response->is_error()) {
2926: unlink($transname);
2927: my $message=$response->status_line;
1.672 albertel 2928: &logthis("<font color=\"blue\">WARNING:"
1.12 www 2929: ." LWP get: $message: $filename</font>");
1.607 raeburn 2930: return 'unavailable';
1.8 www 2931: } else {
1.16 www 2932: if ($remoteurl!~/\.meta$/) {
2933: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2934: my $mresponse=$ua->request($mrequest,$filename.'.meta');
2935: if ($mresponse->is_error()) {
2936: unlink($filename.'.meta');
2937: &logthis(
1.672 albertel 2938: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 2939: }
2940: }
1.8 www 2941: rename($transname,$filename);
1.607 raeburn 2942: return 'ok';
1.8 www 2943: }
1.290 www 2944: }
1.8 www 2945: }
1.330 www 2946: }
2947:
2948: # ------------------------------------------------ Get server side include body
2949: sub ssi_body {
1.381 albertel 2950: my ($filelink,%form)=@_;
1.606 matthew 2951: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
2952: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
2953: }
1.953 www 2954: my $output='';
2955: my $response;
1.980 raeburn 2956: if ($filelink=~/^https?\:/) {
1.954 raeburn 2957: ($output,$response)=&externalssi($filelink);
1.953 www 2958: } else {
1.1004 droeschl 2959: $filelink .= $filelink=~/\?/ ? '&' : '?';
2960: $filelink .= 'inhibitmenu=yes';
1.953 www 2961: ($output,$response)=&ssi($filelink,%form);
2962: }
1.778 albertel 2963: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2964: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2965: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2966: if (wantarray) {
2967: return ($output, $response);
2968: } else {
2969: return $output;
2970: }
1.8 www 2971: }
2972:
1.15 www 2973: # --------------------------------------------------------- Server Side Include
2974:
1.782 albertel 2975: sub absolute_url {
2976: my ($host_name) = @_;
2977: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
2978: if ($host_name eq '') {
2979: $host_name = $ENV{'SERVER_NAME'};
2980: }
2981: return $protocol.$host_name;
2982: }
2983:
1.942 foxr 2984: #
2985: # Server side include.
2986: # Parameters:
2987: # fn Possibly encrypted resource name/id.
2988: # form Hash that describes how the rendering should be done
2989: # and other things.
1.944 foxr 2990: # Returns:
1.950 raeburn 2991: # Scalar context: The content of the response.
2992: # Array context: 2 element list of the content and the full response object.
1.942 foxr 2993: #
1.15 www 2994: sub ssi {
2995:
1.944 foxr 2996: my ($fn,%form)=@_;
1.15 www 2997: my $ua=new LWP::UserAgent;
1.23 www 2998: my $request;
1.711 albertel 2999:
3000: $form{'no_update_last_known'}=1;
1.895 albertel 3001: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 3002: if (%form) {
1.782 albertel 3003: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1272 droeschl 3004: $request->content(join('&',map {
3005: my $name = escape($_);
3006: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3007: ? join("&$name=", map {escape($_) } @{$form{$_}})
3008: : &escape($form{$_}) );
3009: } keys(%form)));
1.23 www 3010: } else {
1.782 albertel 3011: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 3012: }
3013:
1.15 www 3014: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1173 foxr 3015: my $response= $ua->request($request);
1.1182 foxr 3016: my $content = $response->content;
3017:
3018:
1.944 foxr 3019: if (wantarray) {
1.1173 foxr 3020: return ($content, $response);
1.944 foxr 3021: } else {
1.1173 foxr 3022: return $content;
1.942 foxr 3023: }
1.324 www 3024: }
3025:
3026: sub externalssi {
3027: my ($url)=@_;
3028: my $ua=new LWP::UserAgent;
3029: my $request=new HTTP::Request('GET',$url);
3030: my $response=$ua->request($request);
1.954 raeburn 3031: if (wantarray) {
3032: return ($response->content, $response);
3033: } else {
3034: return $response->content;
3035: }
1.15 www 3036: }
1.254 www 3037:
1.492 albertel 3038: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3039:
3040: sub allowuploaded {
3041: my ($srcurl,$url)=@_;
3042: $url=&clutter(&declutter($url));
3043: my $dir=$url;
3044: $dir=~s/\/[^\/]+$//;
3045: my %httpref=();
3046: my $httpurl=&hreflocation('',$url);
3047: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 3048: &Apache::lonnet::appenv(\%httpref);
1.254 www 3049: }
1.477 raeburn 3050:
1.1193 raeburn 3051: #
3052: # Determine if the current user should be able to edit a particular resource,
3053: # when viewing in course context.
3054: # (a) When viewing resource used to determine if "Edit" item is included in
3055: # Functions.
3056: # (b) When displaying folder contents in course editor, used to determine if
3057: # "Edit" link will be displayed alongside resource.
3058: #
1.1196 raeburn 3059: # input: six args -- filename (decluttered), course number, course domain,
3060: # url, symb (if registered) and group (if this is a group
3061: # item -- e.g., bulletin board, group page etc.).
3062: # output: array of five scalars --
1.1193 raeburn 3063: # $cfile -- url for file editing if editable on current server
3064: # $home -- homeserver of resource (i.e., for author if published,
3065: # or course if uploaded.).
3066: # $switchserver -- 1 if server switch will be needed.
1.1196 raeburn 3067: # $forceedit -- 1 if icon/link should be to go to edit mode
3068: # $forceview -- 1 if icon/link should be to go to view mode
1.1193 raeburn 3069: #
3070:
3071: sub can_edit_resource {
1.1194 raeburn 3072: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3073: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3074: #
3075: # For aboutme pages user can only edit his/her own.
3076: #
1.1199 raeburn 3077: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194 raeburn 3078: my ($sdom,$sname) = ($1,$2);
3079: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3080: $home = $env{'user.home'};
3081: $cfile = $resurl;
3082: if ($env{'form.forceedit'}) {
3083: $forceview = 1;
3084: } else {
3085: $forceedit = 1;
3086: }
3087: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3088: } else {
3089: return;
3090: }
3091: }
3092:
3093: if ($env{'request.course.id'}) {
3094: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3095: if ($group ne '') {
3096: # if this is a group homepage or group bulletin board, check group privs
3097: my $allowed = 0;
1.1197 raeburn 3098: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3099: if ((&allowed('mdg',$env{'request.course.id'}.
1.1198 raeburn 3100: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194 raeburn 3101: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3102: $allowed = 1;
3103: }
1.1197 raeburn 3104: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3105: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3106: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194 raeburn 3107: $allowed = 1;
3108: }
3109: }
3110: if ($allowed) {
3111: $home=&homeserver($cnum,$cdom);
3112: if ($env{'form.forceedit'}) {
3113: $forceview = 1;
3114: } else {
3115: $forceedit = 1;
3116: }
3117: $cfile = $resurl;
3118: } else {
3119: return;
3120: }
3121: } else {
1.1208 raeburn 3122: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3123: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3124: return;
3125: }
3126: } elsif (!$crsedit) {
1.1194 raeburn 3127: #
3128: # No edit allowed where CC has switched to student role.
3129: #
3130: return;
3131: }
3132: }
3133: }
3134:
1.1193 raeburn 3135: if ($file ne '') {
3136: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194 raeburn 3137: if (&is_course_upload($file,$cnum,$cdom)) {
3138: $uploaded = 1;
3139: $incourse = 1;
1.1193 raeburn 3140: if ($file =~/\.(htm|html|css|js|txt)$/) {
3141: $cfile = &hreflocation('',$file);
1.1201 raeburn 3142: if ($env{'form.forceedit'}) {
3143: $forceview = 1;
3144: } else {
3145: $forceedit = 1;
3146: }
1.1194 raeburn 3147: }
3148: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3149: $incourse = 1;
3150: if ($env{'form.forceedit'}) {
3151: $forceview = 1;
3152: } else {
3153: $forceedit = 1;
3154: }
3155: $cfile = $resurl;
3156: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3157: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3158: $incourse = 1;
3159: if ($env{'form.forceedit'}) {
3160: $forceview = 1;
3161: } else {
3162: $forceedit = 1;
3163: }
3164: $cfile = $resurl;
1.1199 raeburn 3165: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194 raeburn 3166: $incourse = 1;
3167: $cfile = $resurl.'/smpedit';
1.1199 raeburn 3168: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194 raeburn 3169: $incourse = 1;
1.1199 raeburn 3170: if ($env{'form.forceedit'}) {
3171: $forceview = 1;
3172: } else {
3173: $forceedit = 1;
3174: }
3175: $cfile = $resurl;
1.1298 raeburn 3176: } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/exttools?$}) {
3177: $incourse = 1;
3178: if ($env{'form.forceedit'}) {
3179: $forceview = 1;
3180: } else {
3181: $forceedit = 1;
3182: }
3183: $cfile = $resurl;
1.1208 raeburn 3184: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3185: $incourse = 1;
3186: if ($env{'form.forceedit'}) {
3187: $forceview = 1;
3188: } else {
3189: $forceedit = 1;
3190: }
3191: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194 raeburn 3192: }
3193: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3194: my $template = '/res/lib/templates/simpleproblem.problem';
3195: if (&is_on_map($template)) {
3196: $incourse = 1;
3197: $forceview = 1;
3198: $cfile = $template;
1.1193 raeburn 3199: }
1.1199 raeburn 3200: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3201: $incourse = 1;
3202: if ($env{'form.forceedit'}) {
3203: $forceview = 1;
3204: } else {
3205: $forceedit = 1;
3206: }
3207: $cfile = $resurl;
1.1298 raeburn 3208: } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/exttools?$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3209: $incourse = 1;
3210: if ($env{'form.forceedit'}) {
3211: $forceview = 1;
3212: } else {
3213: $forceedit = 1;
3214: }
3215: $cfile = $resurl;
1.1199 raeburn 3216: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3217: $incourse = 1;
3218: $forceview = 1;
3219: if ($symb) {
3220: my ($map,$id,$res)=&decode_symb($symb);
3221: $env{'request.symb'} = $symb;
3222: $cfile = &clutter($res);
3223: } else {
3224: $cfile = $env{'form.suppurl'};
1.1298 raeburn 3225: my $escfile = &unescape($cfile);
3226: if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) {
3227: $cfile = '/adm/wrapper'.$escfile;
3228: } else {
3229: $escfile =~ s{^http://}{};
3230: $cfile = &escape("/adm/wrapper/ext/$escfile");
3231: }
1.1199 raeburn 3232: }
1.1234 raeburn 3233: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3234: if ($env{'form.forceedit'}) {
3235: $forceview = 1;
3236: } else {
3237: $forceedit = 1;
3238: }
3239: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193 raeburn 3240: }
3241: }
1.1194 raeburn 3242: if ($uploaded || $incourse) {
3243: $home=&homeserver($cnum,$cdom);
1.1207 raeburn 3244: } elsif ($file !~ m{/$}) {
1.1193 raeburn 3245: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3246: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3247: # Check that the user has permission to edit this resource
3248: my $setpriv = 1;
3249: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3250: if (defined($cfudom)) {
3251: $home=&homeserver($cfuname,$cfudom);
3252: $cfile=$file;
3253: }
3254: }
1.1194 raeburn 3255: if (($cfile ne '') && (!$incourse || $uploaded) &&
3256: (($home ne '') && ($home ne 'no_host'))) {
1.1193 raeburn 3257: my @ids=¤t_machine_ids();
3258: unless (grep(/^\Q$home\E$/,@ids)) {
3259: $switchserver=1;
3260: }
3261: }
3262: }
1.1194 raeburn 3263: return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193 raeburn 3264: }
3265:
3266: sub is_course_upload {
3267: my ($file,$cnum,$cdom) = @_;
3268: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3269: $uploadpath =~ s{^\/}{};
1.1201 raeburn 3270: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3271: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193 raeburn 3272: return 1;
3273: }
3274: return;
3275: }
3276:
1.1194 raeburn 3277: sub in_course {
1.1195 raeburn 3278: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3279: if ($hideprivileged) {
3280: my $skipuser;
1.1219 raeburn 3281: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3282: my @possdoms = ($cdom);
3283: if ($coursehash{'checkforpriv'}) {
3284: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3285: }
3286: if (&privileged($uname,$udom,\@possdoms)) {
1.1195 raeburn 3287: $skipuser = 1;
3288: if ($coursehash{'nothideprivileged'}) {
3289: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3290: my $user;
3291: if ($item =~ /:/) {
3292: $user = $item;
3293: } else {
3294: $user = join(':',split(/[\@]/,$item));
3295: }
3296: if ($user eq $uname.':'.$udom) {
3297: undef($skipuser);
3298: last;
3299: }
3300: }
3301: }
3302: if ($skipuser) {
3303: return 0;
3304: }
3305: }
3306: }
1.1194 raeburn 3307: $type ||= 'any';
3308: if (!defined($cdom) || !defined($cnum)) {
3309: my $cid = $env{'request.course.id'};
3310: $cdom = $env{'course.'.$cid.'.domain'};
3311: $cnum = $env{'course.'.$cid.'.num'};
3312: }
3313: my $typesref;
1.1195 raeburn 3314: if (($type eq 'any') || ($type eq 'all')) {
1.1194 raeburn 3315: $typesref = ['active','previous','future'];
3316: } elsif ($type eq 'previous' || $type eq 'future') {
3317: $typesref = [$type];
3318: }
3319: my %roles = &get_my_roles($uname,$udom,'userroles',
3320: $typesref,undef,[$cdom]);
3321: my ($tmp) = keys(%roles);
3322: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3323: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3324: if (@course_roles > 0) {
3325: return 1;
3326: }
3327: return 0;
3328: }
3329:
1.478 albertel 3330: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 3331: # input: action, courseID, current domain, intended
1.637 raeburn 3332: # path to file, source of file, instruction to parse file for objects,
3333: # ref to hash for embedded objects,
3334: # ref to hash for codebase of java objects.
1.1095 raeburn 3335: # reference to scalar to accommodate mime type determined
3336: # from File::MMagic if $parser = parse.
1.637 raeburn 3337: #
1.485 raeburn 3338: # output: url to file (if action was uploaddoc),
3339: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 3340: #
1.478 albertel 3341: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3342: # course.
1.477 raeburn 3343: #
1.478 albertel 3344: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3345: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3346: # course's home server.
1.477 raeburn 3347: #
1.478 albertel 3348: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3349: # be copied from $source (current location) to
3350: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3351: # and will then be copied to
3352: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3353: # course's home server.
1.485 raeburn 3354: #
1.481 raeburn 3355: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 3356: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 3357: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3358: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3359: # in course's home server.
1.637 raeburn 3360: #
1.477 raeburn 3361:
3362: sub process_coursefile {
1.1095 raeburn 3363: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3364: $mimetype)=@_;
1.477 raeburn 3365: my $fetchresult;
1.638 albertel 3366: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 3367: if ($action eq 'propagate') {
1.638 albertel 3368: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3369: $home);
1.481 raeburn 3370: } else {
1.477 raeburn 3371: my $fpath = '';
3372: my $fname = $file;
1.478 albertel 3373: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 3374: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 3375: my $filepath = &build_filepath($fpath);
1.481 raeburn 3376: if ($action eq 'copy') {
3377: if ($source eq '') {
3378: $fetchresult = 'no source file';
3379: return $fetchresult;
3380: } else {
3381: my $destination = $filepath.'/'.$fname;
3382: rename($source,$destination);
3383: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3384: $home);
1.481 raeburn 3385: }
3386: } elsif ($action eq 'uploaddoc') {
3387: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 3388: print $fh $env{'form.'.$source};
1.481 raeburn 3389: close($fh);
1.637 raeburn 3390: if ($parser eq 'parse') {
1.1024 raeburn 3391: my $mm = new File::MMagic;
1.1095 raeburn 3392: my $type = $mm->checktype_filename($filepath.'/'.$fname);
3393: if ($type eq 'text/html') {
1.1024 raeburn 3394: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
3395: unless ($parse_result eq 'ok') {
3396: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
3397: }
1.637 raeburn 3398: }
1.1095 raeburn 3399: if (ref($mimetype)) {
3400: $$mimetype = $type;
3401: }
1.637 raeburn 3402: }
1.477 raeburn 3403: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3404: $home);
1.481 raeburn 3405: if ($fetchresult eq 'ok') {
3406: return '/uploaded/'.$fpath.'/'.$fname;
3407: } else {
3408: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 3409: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 3410: return '/adm/notfound.html';
3411: }
1.477 raeburn 3412: }
3413: }
1.485 raeburn 3414: unless ( $fetchresult eq 'ok') {
1.477 raeburn 3415: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 3416: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 3417: }
3418: return $fetchresult;
3419: }
3420:
1.637 raeburn 3421: sub build_filepath {
3422: my ($fpath) = @_;
3423: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
3424: unless ($fpath eq '') {
3425: my @parts=split('/',$fpath);
3426: foreach my $part (@parts) {
3427: $filepath.= '/'.$part;
3428: if ((-e $filepath)!=1) {
3429: mkdir($filepath,0777);
3430: }
3431: }
3432: }
3433: return $filepath;
3434: }
3435:
3436: sub store_edited_file {
1.638 albertel 3437: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 3438: my $file = $primary_url;
3439: $file =~ s#^/uploaded/$docudom/$docuname/##;
3440: my $fpath = '';
3441: my $fname = $file;
3442: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
3443: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
3444: my $filepath = &build_filepath($fpath);
3445: open(my $fh,'>'.$filepath.'/'.$fname);
3446: print $fh $content;
3447: close($fh);
1.638 albertel 3448: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 3449: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3450: $home);
1.637 raeburn 3451: if ($$fetchresult eq 'ok') {
3452: return '/uploaded/'.$fpath.'/'.$fname;
3453: } else {
1.638 albertel 3454: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
3455: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 3456: return '/adm/notfound.html';
3457: }
3458: }
3459:
1.531 albertel 3460: sub clean_filename {
1.831 albertel 3461: my ($fname,$args)=@_;
1.315 www 3462: # Replace Windows backslashes by forward slashes
1.257 www 3463: $fname=~s/\\/\//g;
1.831 albertel 3464: if (!$args->{'keep_path'}) {
3465: # Get rid of everything but the actual filename
3466: $fname=~s/^.*\/([^\/]+)$/$1/;
3467: }
1.315 www 3468: # Replace spaces by underscores
3469: $fname=~s/\s+/\_/g;
3470: # Replace all other weird characters by nothing
1.831 albertel 3471: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 3472: # Replace all .\d. sequences with _\d. so they no longer look like version
3473: # numbers
3474: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 3475: return $fname;
3476: }
1.1051 raeburn 3477: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
3478: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
3479: # image with the same aspect ratio as the original, but with dimensions which do
3480: # not exceed $resizewidth and $resizeheight.
3481:
1.984 neumanie 3482: sub resizeImage {
1.1051 raeburn 3483: my ($img_path,$resizewidth,$resizeheight) = @_;
3484: my $ima = Image::Magick->new;
3485: my $resized;
3486: if (-e $img_path) {
3487: $ima->Read($img_path);
3488: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
3489: my $width = $ima->Get('width');
3490: my $height = $ima->Get('height');
3491: if ($width > $resizewidth) {
3492: my $factor = $width/$resizewidth;
3493: my $newheight = $height/$factor;
3494: $ima->Scale(width=>$resizewidth,height=>$newheight);
3495: $resized = 1;
3496: }
3497: }
3498: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
3499: my $width = $ima->Get('width');
3500: my $height = $ima->Get('height');
3501: if ($height > $resizeheight) {
3502: my $factor = $height/$resizeheight;
3503: my $newwidth = $width/$factor;
3504: $ima->Scale(width=>$newwidth,height=>$resizeheight);
3505: $resized = 1;
3506: }
3507: }
3508: if ($resized) {
3509: $ima->Write($img_path);
3510: }
3511: }
3512: return;
1.977 amueller 3513: }
3514:
1.608 albertel 3515: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 3516: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093 raeburn 3517: # the desired filename is in $env{"form.$formname.filename"}
1.1090 raeburn 3518: # $context - possible values: coursedoc, existingfile, overwrite,
3519: # canceloverwrite, or ''.
3520: # if 'coursedoc': upload to the current course
3521: # if 'existingfile': write file to tmp/overwrites directory
3522: # if 'canceloverwrite': delete file written to tmp/overwrites directory
3523: # $context is passed as argument to &finishuserfileupload
1.686 albertel 3524: # $subdir - directory in userfile to store the file into
1.858 raeburn 3525: # $parser - instruction to parse file for objects ($parser = parse)
3526: # $allfiles - reference to hash for embedded objects
3527: # $codebase - reference to hash for codebase of java objects
3528: # $desuname - username for permanent storage of uploaded file
3529: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 3530: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
3531: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 3532: # $resizewidth - width (pixels) to which to resize uploaded image
3533: # $resizeheight - height (pixels) to which to resize uploaded image
1.1095 raeburn 3534: # $mimetype - reference to scalar to accommodate mime type determined
1.1152 raeburn 3535: # from File::MMagic.
1.858 raeburn 3536: #
1.686 albertel 3537: # output: url of file in userspace, or error: <message>
3538: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 3539:
1.531 albertel 3540: sub userfileupload {
1.1090 raeburn 3541: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095 raeburn 3542: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 3543: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 3544: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 3545: $fname=&clean_filename($fname);
1.1090 raeburn 3546: # See if there is anything left
1.257 www 3547: unless ($fname) { return 'error: no uploaded file'; }
1.1090 raeburn 3548: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
3549: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
3550: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
3551: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 3552: my $now = time;
1.1090 raeburn 3553: my $filepath;
1.1095 raeburn 3554: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090 raeburn 3555: $filepath = 'tmp/helprequests/'.$now;
3556: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
3557: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
3558: '_'.$env{'user.domain'}.'/pending';
3559: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
3560: my ($docuname,$docudom);
3561: if ($destudom) {
3562: $docudom = $destudom;
3563: } else {
3564: $docudom = $env{'user.domain'};
3565: }
3566: if ($destuname) {
3567: $docuname = $destuname;
3568: } else {
3569: $docuname = $env{'user.name'};
3570: }
3571: if (exists($env{'form.group'})) {
3572: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3573: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3574: }
3575: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
3576: if ($context eq 'canceloverwrite') {
3577: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
3578: if (-e $tempfile) {
3579: my @info = stat($tempfile);
3580: if ($info[9] eq $env{'form.timestamp'}) {
3581: unlink($tempfile);
3582: }
3583: }
3584: return;
1.523 raeburn 3585: }
3586: }
1.1090 raeburn 3587: # Create the directory if not present
1.741 raeburn 3588: my @parts=split(/\//,$filepath);
3589: my $fullpath = $perlvar{'lonDaemons'};
3590: for (my $i=0;$i<@parts;$i++) {
3591: $fullpath .= '/'.$parts[$i];
3592: if ((-e $fullpath)!=1) {
3593: mkdir($fullpath,0777);
3594: }
3595: }
3596: open(my $fh,'>'.$fullpath.'/'.$fname);
3597: print $fh $env{'form.'.$formname};
3598: close($fh);
1.1090 raeburn 3599: if ($context eq 'existingfile') {
3600: my @info = stat($fullpath.'/'.$fname);
3601: return ($fullpath.'/'.$fname,$info[9]);
3602: } else {
3603: return $fullpath.'/'.$fname;
3604: }
1.523 raeburn 3605: }
1.995 raeburn 3606: if ($subdir eq 'scantron') {
3607: $fname = 'scantron_orig_'.$fname;
1.1093 raeburn 3608: } else {
1.995 raeburn 3609: $fname="$subdir/$fname";
3610: }
1.1090 raeburn 3611: if ($context eq 'coursedoc') {
1.638 albertel 3612: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3613: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 3614: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 3615: return &finishuserfileupload($docuname,$docudom,
3616: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 3617: $codebase,$thumbwidth,$thumbheight,
1.1095 raeburn 3618: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 3619: } else {
1.1218 raeburn 3620: if ($env{'form.folder'}) {
3621: $fname=$env{'form.folder'}.'/'.$fname;
3622: }
1.638 albertel 3623: return &process_coursefile('uploaddoc',$docuname,$docudom,
3624: $fname,$formname,$parser,
1.1095 raeburn 3625: $allfiles,$codebase,$mimetype);
1.481 raeburn 3626: }
1.719 banghart 3627: } elsif (defined($destuname)) {
3628: my $docuname=$destuname;
3629: my $docudom=$destudom;
1.860 raeburn 3630: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3631: $parser,$allfiles,$codebase,
1.1051 raeburn 3632: $thumbwidth,$thumbheight,
1.1095 raeburn 3633: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 3634: } else {
1.638 albertel 3635: my $docuname=$env{'user.name'};
3636: my $docudom=$env{'user.domain'};
1.1220 raeburn 3637: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714 raeburn 3638: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3639: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3640: }
1.860 raeburn 3641: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3642: $parser,$allfiles,$codebase,
1.1051 raeburn 3643: $thumbwidth,$thumbheight,
1.1095 raeburn 3644: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 3645: }
1.271 www 3646: }
3647:
3648: sub finishuserfileupload {
1.860 raeburn 3649: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095 raeburn 3650: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 3651: my $path=$docudom.'/'.$docuname.'/';
1.258 www 3652: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 3653:
1.860 raeburn 3654: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 3655: $file=$fname;
3656: if ($fname=~m|/|) {
3657: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
3658: $path.=$fnamepath.'/';
3659: }
1.259 www 3660: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 3661: my $count;
3662: for ($count=4;$count<=$#parts;$count++) {
3663: $filepath.="/$parts[$count]";
3664: if ((-e $filepath)!=1) {
3665: mkdir($filepath,0777);
3666: }
3667: }
1.984 neumanie 3668:
1.258 www 3669: # Save the file
3670: {
1.701 albertel 3671: if (!open(FH,'>'.$filepath.'/'.$file)) {
3672: &logthis('Failed to create '.$filepath.'/'.$file);
3673: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
3674: return '/adm/notfound.html';
3675: }
1.1090 raeburn 3676: if ($context eq 'overwrite') {
1.1117 foxr 3677: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090 raeburn 3678: my $target = $filepath.'/'.$file;
3679: if (-e $source) {
3680: my @info = stat($source);
3681: if ($info[9] eq $env{'form.timestamp'}) {
3682: unless (&File::Copy::move($source,$target)) {
3683: &logthis('Failed to overwrite '.$filepath.'/'.$file);
3684: return "Moving from $source failed";
3685: }
3686: } else {
3687: return "Temporary file: $source had unexpected date/time for last modification";
3688: }
3689: } else {
3690: return "Temporary file: $source missing";
3691: }
3692: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 3693: &logthis('Failed to write to '.$filepath.'/'.$file);
3694: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
3695: return '/adm/notfound.html';
3696: }
1.570 albertel 3697: close(FH);
1.1051 raeburn 3698: if ($resizewidth && $resizeheight) {
3699: my $mm = new File::MMagic;
3700: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
3701: if ($mime_type =~ m{^image/}) {
3702: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
3703: }
1.977 amueller 3704: }
1.258 www 3705: }
1.1152 raeburn 3706: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
3707: if (ref($mimetype)) {
3708: if ($$mimetype eq '') {
3709: my $mm = new File::MMagic;
3710: my $type = $mm->checktype_filename($filepath.'/'.$file);
3711: $$mimetype = $type;
3712: }
3713: }
3714: }
1.637 raeburn 3715: if ($parser eq 'parse') {
1.1152 raeburn 3716: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024 raeburn 3717: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
3718: $allfiles,$codebase);
3719: unless ($parse_result eq 'ok') {
3720: &logthis('Failed to parse '.$filepath.$file.
3721: ' for embedded media: '.$parse_result);
3722: }
1.637 raeburn 3723: }
3724: }
1.860 raeburn 3725: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
3726: my $input = $filepath.'/'.$file;
3727: my $output = $filepath.'/'.'tn-'.$file;
3728: my $thumbsize = $thumbwidth.'x'.$thumbheight;
3729: system("convert -sample $thumbsize $input $output");
3730: if (-e $filepath.'/'.'tn-'.$file) {
3731: $fetchthumb = 1;
3732: }
3733: }
1.858 raeburn 3734:
1.259 www 3735: # Notify homeserver to grep it
3736: #
1.984 neumanie 3737: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 3738: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 3739: if ($fetchresult eq 'ok') {
1.860 raeburn 3740: if ($fetchthumb) {
3741: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
3742: if ($thumbresult ne 'ok') {
3743: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
3744: $docuhome.': '.$thumbresult);
3745: }
3746: }
1.259 www 3747: #
1.258 www 3748: # Return the URL to it
1.494 albertel 3749: return '/uploaded/'.$path.$file;
1.263 www 3750: } else {
1.494 albertel 3751: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
3752: ': '.$fetchresult);
1.263 www 3753: return '/adm/notfound.html';
1.858 raeburn 3754: }
1.493 albertel 3755: }
3756:
1.637 raeburn 3757: sub extract_embedded_items {
1.961 raeburn 3758: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 3759: my @state = ();
1.1164 raeburn 3760: my (%lastids,%related,%shockwave,%flashvars);
1.637 raeburn 3761: my %javafiles = (
3762: codebase => '',
3763: code => '',
3764: archive => ''
3765: );
3766: my %mediafiles = (
3767: src => '',
3768: movie => '',
3769: );
1.648 raeburn 3770: my $p;
3771: if ($content) {
3772: $p = HTML::LCParser->new($content);
3773: } else {
1.961 raeburn 3774: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 3775: }
1.641 albertel 3776: while (my $t=$p->get_token()) {
1.640 albertel 3777: if ($t->[0] eq 'S') {
3778: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 3779: push(@state, $tagname);
1.648 raeburn 3780: if (lc($tagname) eq 'allow') {
3781: &add_filetype($allfiles,$attr->{'src'},'src');
3782: }
1.640 albertel 3783: if (lc($tagname) eq 'img') {
3784: &add_filetype($allfiles,$attr->{'src'},'src');
3785: }
1.886 albertel 3786: if (lc($tagname) eq 'a') {
1.1222 raeburn 3787: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221 raeburn 3788: &add_filetype($allfiles,$attr->{'href'},'href');
3789: }
1.886 albertel 3790: }
1.645 raeburn 3791: if (lc($tagname) eq 'script') {
1.1164 raeburn 3792: my $src;
1.645 raeburn 3793: if ($attr->{'archive'} =~ /\.jar$/i) {
3794: &add_filetype($allfiles,$attr->{'archive'},'archive');
3795: } else {
1.1164 raeburn 3796: if ($attr->{'src'} ne '') {
3797: $src = $attr->{'src'};
3798: &add_filetype($allfiles,$src,'src');
3799: }
3800: }
3801: my $text = $p->get_trimmed_text();
3802: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
3803: my @swfargs = split(/,/,$1);
3804: foreach my $item (@swfargs) {
3805: $item =~ s/["']//g;
3806: $item =~ s/^\s+//;
3807: $item =~ s/\s+$//;
3808: }
3809: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
3810: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
3811: push(@{$related{$swfargs[0]}},$swfargs[2]);
3812: } else {
3813: $related{$swfargs[0]} = [$swfargs[2]];
3814: }
3815: }
1.645 raeburn 3816: }
3817: }
3818: if (lc($tagname) eq 'link') {
3819: if (lc($attr->{'rel'}) eq 'stylesheet') {
3820: &add_filetype($allfiles,$attr->{'href'},'href');
3821: }
3822: }
1.640 albertel 3823: if (lc($tagname) eq 'object' ||
3824: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
3825: foreach my $item (keys(%javafiles)) {
3826: $javafiles{$item} = '';
3827: }
1.1164 raeburn 3828: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
3829: $lastids{lc($tagname)} = $attr->{'id'};
3830: }
1.640 albertel 3831: }
3832: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
3833: my $name = lc($attr->{'name'});
3834: foreach my $item (keys(%javafiles)) {
3835: if ($name eq $item) {
3836: $javafiles{$item} = $attr->{'value'};
3837: last;
3838: }
3839: }
1.1164 raeburn 3840: my $pathfrom;
1.640 albertel 3841: foreach my $item (keys(%mediafiles)) {
3842: if ($name eq $item) {
1.1164 raeburn 3843: $pathfrom = $attr->{'value'};
3844: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
3845: &add_filetype($allfiles,$pathfrom,$name);
1.640 albertel 3846: last;
3847: }
3848: }
1.1164 raeburn 3849: if ($name eq 'flashvars') {
3850: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
3851: }
3852: if ($pathfrom ne '') {
3853: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
3854: $pathfrom);
3855: }
1.640 albertel 3856: }
3857: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
3858: foreach my $item (keys(%javafiles)) {
3859: if ($attr->{$item}) {
3860: $javafiles{$item} = $attr->{$item};
3861: last;
3862: }
3863: }
3864: foreach my $item (keys(%mediafiles)) {
3865: if ($attr->{$item}) {
3866: &add_filetype($allfiles,$attr->{$item},$item);
3867: last;
3868: }
3869: }
1.1164 raeburn 3870: if (lc($tagname) eq 'embed') {
3871: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
3872: &embedded_dependency($allfiles,\%related,$attr->{'name'},
3873: $attr->{'src'});
3874: }
3875: }
1.640 albertel 3876: }
1.1243 raeburn 3877: if (lc($tagname) eq 'iframe') {
3878: my $src = $attr->{'src'} ;
3879: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
3880: &add_filetype($allfiles,$src,'src');
3881: } elsif ($src =~ m{^/}) {
3882: if ($env{'request.course.id'}) {
3883: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
3884: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
3885: my $url = &hreflocation('',$fullpath);
3886: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
3887: my $relpath = $1;
3888: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
3889: &add_filetype($allfiles,$1,'src');
3890: }
3891: }
3892: }
3893: }
3894: }
1.1164 raeburn 3895: if ($t->[4] =~ m{/>$}) {
1.1243 raeburn 3896: pop(@state);
1.1164 raeburn 3897: }
1.640 albertel 3898: } elsif ($t->[0] eq 'E') {
3899: my ($tagname) = ($t->[1]);
3900: if ($javafiles{'codebase'} ne '') {
3901: $javafiles{'codebase'} .= '/';
3902: }
3903: if (lc($tagname) eq 'applet' ||
3904: lc($tagname) eq 'object' ||
3905: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
3906: ) {
3907: foreach my $item (keys(%javafiles)) {
3908: if ($item ne 'codebase' && $javafiles{$item} ne '') {
3909: my $file=$javafiles{'codebase'}.$javafiles{$item};
3910: &add_filetype($allfiles,$file,$item);
3911: }
3912: }
3913: }
3914: pop @state;
3915: }
3916: }
1.1164 raeburn 3917: foreach my $id (sort(keys(%flashvars))) {
3918: if ($shockwave{$id} ne '') {
3919: my @pairs = split(/\&/,$flashvars{$id});
3920: foreach my $pair (@pairs) {
3921: my ($key,$value) = split(/\=/,$pair);
3922: if ($key eq 'thumb') {
3923: &add_filetype($allfiles,$value,$key);
3924: } elsif ($key eq 'content') {
3925: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
3926: my ($ext) = ($value =~ /\.([^.]+)$/);
3927: if ($ext ne '') {
3928: &add_filetype($allfiles,$path.$value,$ext);
3929: }
3930: }
3931: }
3932: }
3933: }
1.637 raeburn 3934: return 'ok';
3935: }
3936:
1.639 albertel 3937: sub add_filetype {
3938: my ($allfiles,$file,$type)=@_;
3939: if (exists($allfiles->{$file})) {
3940: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
3941: push(@{$allfiles->{$file}}, &escape($type));
3942: }
3943: } else {
3944: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 3945: }
3946: }
3947:
1.1164 raeburn 3948: sub embedded_dependency {
3949: my ($allfiles,$related,$identifier,$pathfrom) = @_;
3950: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
3951: if (($identifier ne '') &&
3952: (ref($related->{$identifier}) eq 'ARRAY') &&
3953: ($pathfrom ne '')) {
3954: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
3955: foreach my $dep (@{$related->{$identifier}}) {
3956: &add_filetype($allfiles,$path.$dep,'object');
3957: }
3958: }
3959: }
3960: return;
3961: }
3962:
1.493 albertel 3963: sub removeuploadedurl {
1.984 neumanie 3964: my ($url)=@_;
3965: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 3966: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 3967: }
3968:
3969: sub removeuserfile {
3970: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 3971: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 3972: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 3973: if ($result eq 'ok') {
1.798 raeburn 3974: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
3975: my $metafile = $fname.'.meta';
3976: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 3977: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 3978: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 3979: my $sqlresult =
1.823 albertel 3980: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 3981: 'portfolio_metadata',$group,
3982: 'delete');
1.798 raeburn 3983: }
3984: }
3985: return $result;
1.257 www 3986: }
1.15 www 3987:
1.530 albertel 3988: sub mkdiruserfile {
3989: my ($docuname,$docudom,$dir)=@_;
3990: my $home=&homeserver($docuname,$docudom);
3991: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
3992: }
3993:
1.531 albertel 3994: sub renameuserfile {
3995: my ($docuname,$docudom,$old,$new)=@_;
3996: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 3997: my $result = &reply("renameuserfile:$docudom:$docuname:".
3998: &escape("$old").':'.&escape("$new"),$home);
3999: if ($result eq 'ok') {
4000: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4001: my $oldmeta = $old.'.meta';
4002: my $newmeta = $new.'.meta';
4003: my $metaresult =
4004: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 4005: my $url = "/uploaded/$docudom/$docuname/$old";
4006: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4007: my $sqlresult =
1.823 albertel 4008: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4009: 'portfolio_metadata',$group,
4010: 'delete');
1.798 raeburn 4011: }
4012: }
4013: return $result;
1.531 albertel 4014: }
4015:
1.14 www 4016: # ------------------------------------------------------------------------- Log
4017:
4018: sub log {
4019: my ($dom,$nam,$hom,$what)=@_;
1.47 www 4020: return critical("log:$dom:$nam:$what",$hom);
1.157 www 4021: }
4022:
4023: # ------------------------------------------------------------------ Course Log
1.352 www 4024: #
4025: # This routine flushes several buffers of non-mission-critical nature
4026: #
1.157 www 4027:
4028: sub flushcourselogs {
1.352 www 4029: &logthis('Flushing log buffers');
4030: #
4031: # course logs
4032: # This is a log of all transactions in a course, which can be used
4033: # for data mining purposes
4034: #
4035: # It also collects the courseid database, which lists last transaction
4036: # times and course titles for all courseids
4037: #
4038: my %courseidbuffer=();
1.921 raeburn 4039: foreach my $crsid (keys(%courselogs)) {
1.352 www 4040: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 4041: &escape($courselogs{$crsid}),
4042: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 4043: delete $courselogs{$crsid};
4044: } else {
4045: &logthis('Failed to flush log buffer for '.$crsid);
4046: if (length($courselogs{$crsid})>40000) {
1.672 albertel 4047: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 4048: " exceeded maximum size, deleting.</font>");
4049: delete $courselogs{$crsid};
4050: }
1.352 www 4051: }
1.920 raeburn 4052: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 4053: 'description' => $coursedescrbuf{$crsid},
4054: 'inst_code' => $courseinstcodebuf{$crsid},
4055: 'type' => $coursetypebuf{$crsid},
4056: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 4057: };
1.191 harris41 4058: }
1.352 www 4059: #
4060: # Write course id database (reverse lookup) to homeserver of courses
4061: # Is used in pickcourse
4062: #
1.840 albertel 4063: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 4064: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 4065: $courseidbuffer{$crs_home},
4066: $crs_home,'timeonly');
1.352 www 4067: }
4068: #
4069: # File accesses
4070: # Writes to the dynamic metadata of resources to get hit counts, etc.
4071: #
1.449 matthew 4072: foreach my $entry (keys(%accesshash)) {
1.458 matthew 4073: if ($entry =~ /___count$/) {
4074: my ($dom,$name);
1.807 albertel 4075: ($dom,$name,undef)=
1.811 albertel 4076: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 4077: if (! defined($dom) || $dom eq '' ||
4078: ! defined($name) || $name eq '') {
1.620 albertel 4079: my $cid = $env{'request.course.id'};
4080: $dom = $env{'request.'.$cid.'.domain'};
4081: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 4082: }
1.450 matthew 4083: my $value = $accesshash{$entry};
4084: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
4085: my %temphash=($url => $value);
1.449 matthew 4086: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
4087: if ($result eq 'ok') {
4088: delete $accesshash{$entry};
4089: }
4090: } else {
1.811 albertel 4091: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159 www 4092: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450 matthew 4093: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 4094: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
4095: delete $accesshash{$entry};
4096: }
1.185 www 4097: }
1.191 harris41 4098: }
1.352 www 4099: #
4100: # Roles
4101: # Reverse lookup of user roles for course faculty/staff and co-authorship
4102: #
1.800 albertel 4103: foreach my $entry (keys(%userrolehash)) {
1.351 www 4104: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 4105: split(/\:/,$entry);
4106: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 4107: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 4108: $rudom,$runame) eq 'ok') {
4109: delete $userrolehash{$entry};
4110: }
4111: }
1.662 raeburn 4112: #
1.1327 raeburn 4113: # Reverse lookup of domain roles (dc, ad, li, sc, dh, au)
1.662 raeburn 4114: #
4115: my %domrolebuffer = ();
1.1000 raeburn 4116: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 4117: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 4118: if ($domrolebuffer{$rudom}) {
4119: $domrolebuffer{$rudom}.='&'.&escape($entry).
4120: '='.&escape($domainrolehash{$entry});
4121: } else {
4122: $domrolebuffer{$rudom}.=&escape($entry).
4123: '='.&escape($domainrolehash{$entry});
4124: }
4125: delete $domainrolehash{$entry};
4126: }
4127: foreach my $dom (keys(%domrolebuffer)) {
1.1324 raeburn 4128: my %servers;
4129: if (defined(&domain($dom,'primary'))) {
4130: my $primary=&domain($dom,'primary');
4131: my $hostname=&hostname($primary);
4132: $servers{$primary} = $hostname;
4133: } else {
4134: %servers = &get_servers($dom,'library');
4135: }
1.841 albertel 4136: foreach my $tryserver (keys(%servers)) {
1.1324 raeburn 4137: if (&reply('domroleput:'.$dom.':'.
4138: $domrolebuffer{$dom},$tryserver) eq 'ok') {
4139: last;
4140: } else {
1.841 albertel 4141: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
4142: }
1.662 raeburn 4143: }
4144: }
1.186 www 4145: $dumpcount++;
1.157 www 4146: }
4147:
4148: sub courselog {
4149: my $what=shift;
1.158 www 4150: $what=time.':'.$what;
1.620 albertel 4151: unless ($env{'request.course.id'}) { return ''; }
4152: $coursedombuf{$env{'request.course.id'}}=
4153: $env{'course.'.$env{'request.course.id'}.'.domain'};
4154: $coursenumbuf{$env{'request.course.id'}}=
4155: $env{'course.'.$env{'request.course.id'}.'.num'};
4156: $coursehombuf{$env{'request.course.id'}}=
4157: $env{'course.'.$env{'request.course.id'}.'.home'};
4158: $coursedescrbuf{$env{'request.course.id'}}=
4159: $env{'course.'.$env{'request.course.id'}.'.description'};
4160: $courseinstcodebuf{$env{'request.course.id'}}=
4161: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
4162: $courseownerbuf{$env{'request.course.id'}}=
4163: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 4164: $coursetypebuf{$env{'request.course.id'}}=
4165: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 4166: if (defined $courselogs{$env{'request.course.id'}}) {
4167: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 4168: } else {
1.620 albertel 4169: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 4170: }
1.620 albertel 4171: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 4172: &flushcourselogs();
4173: }
1.158 www 4174: }
4175:
4176: sub courseacclog {
4177: my $fnsymb=shift;
1.620 albertel 4178: unless ($env{'request.course.id'}) { return ''; }
4179: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144 www 4180: if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187 www 4181: $what.=':POST';
1.583 matthew 4182: # FIXME: Probably ought to escape things....
1.800 albertel 4183: foreach my $key (keys(%env)) {
4184: if ($key=~/^form\.(.*)/) {
1.975 raeburn 4185: my $formitem = $1;
4186: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
4187: $what.=':'.$formitem.'='.$env{$key};
4188: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
4189: $what.=':'.$formitem.'='.$env{$key};
4190: }
1.158 www 4191: }
1.191 harris41 4192: }
1.583 matthew 4193: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
4194: # FIXME: We should not be depending on a form parameter that someone
4195: # editing lonsearchcat.pm might change in the future.
1.620 albertel 4196: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 4197: $what.= ':POST';
4198: # FIXME: Probably ought to escape things....
4199: foreach my $element ('courseexp','crsfulltext','crsrelated',
4200: 'crsdiscuss') {
1.620 albertel 4201: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 4202: }
4203: }
1.158 www 4204: }
4205: &courselog($what);
1.149 www 4206: }
4207:
1.185 www 4208: sub countacc {
4209: my $url=&declutter(shift);
1.458 matthew 4210: return if (! defined($url) || $url eq '');
1.620 albertel 4211: unless ($env{'request.course.id'}) { return ''; }
1.1158 www 4212: #
4213: # Mark that this url was used in this course
4214: #
1.620 albertel 4215: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158 www 4216: #
4217: # Increase the access count for this resource in this child process
4218: #
1.281 www 4219: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 4220: $accesshash{$key}++;
1.185 www 4221: }
1.349 www 4222:
1.361 www 4223: sub linklog {
4224: my ($from,$to)=@_;
4225: $from=&declutter($from);
4226: $to=&declutter($to);
4227: $accesshash{$from.'___'.$to.'___comefrom'}=1;
4228: $accesshash{$to.'___'.$from.'___goto'}=1;
4229: }
1.1160 www 4230:
4231: sub statslog {
4232: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
4233: if ($users<2) { return; }
4234: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
4235: 'course' => $env{'request.course.id'},
4236: 'sections' => '"all"',
4237: 'num_students' => $users,
4238: 'part' => $part,
4239: 'symb' => $symb,
4240: 'mean_tries' => $av_attempts,
4241: 'deg_of_diff' => $degdiff});
4242: foreach my $key (keys(%dynstore)) {
4243: $accesshash{$key}=$dynstore{$key};
4244: }
4245: }
1.361 www 4246:
1.349 www 4247: sub userrolelog {
4248: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169 droeschl 4249: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350 www 4250: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4251: $userrolehash
4252: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 4253: =$tend.':'.$tstart;
1.662 raeburn 4254: }
1.1169 droeschl 4255: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898 albertel 4256: $userrolehash
4257: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
4258: =$tend.':'.$tstart;
4259: }
1.1327 raeburn 4260: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh)/ ) {
1.662 raeburn 4261: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4262: $domainrolehash
4263: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
4264: = $tend.':'.$tstart;
4265: }
1.351 www 4266: }
4267:
1.957 raeburn 4268: sub courserolelog {
4269: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184 raeburn 4270: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
4271: my $cdom = $1;
4272: my $cnum = $2;
4273: my $sec = $3;
4274: my $namespace = 'rolelog';
4275: my %storehash = (
4276: role => $trole,
4277: start => $tstart,
4278: end => $tend,
4279: selfenroll => $selfenroll,
4280: context => $context,
4281: );
4282: if ($trole eq 'gr') {
4283: $namespace = 'groupslog';
4284: $storehash{'group'} = $sec;
4285: } else {
4286: $storehash{'section'} = $sec;
4287: }
1.1188 raeburn 4288: &write_log('course',$namespace,\%storehash,$delflag,$username,
4289: $domain,$cnum,$cdom);
1.1184 raeburn 4290: if (($trole ne 'st') || ($sec ne '')) {
4291: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957 raeburn 4292: }
4293: }
4294: return;
4295: }
4296:
1.1184 raeburn 4297: sub domainrolelog {
4298: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4299: if ($area =~ m{^/($match_domain)/$}) {
4300: my $cdom = $1;
4301: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
4302: my $namespace = 'rolelog';
4303: my %storehash = (
4304: role => $trole,
4305: start => $tstart,
4306: end => $tend,
4307: context => $context,
4308: );
1.1188 raeburn 4309: &write_log('domain',$namespace,\%storehash,$delflag,$username,
4310: $domain,$domconfiguser,$cdom);
1.1184 raeburn 4311: }
4312: return;
4313:
4314: }
4315:
4316: sub coauthorrolelog {
4317: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4318: if ($area =~ m{^/($match_domain)/($match_username)$}) {
4319: my $audom = $1;
4320: my $auname = $2;
4321: my $namespace = 'rolelog';
4322: my %storehash = (
4323: role => $trole,
4324: start => $tstart,
4325: end => $tend,
4326: context => $context,
4327: );
1.1188 raeburn 4328: &write_log('author',$namespace,\%storehash,$delflag,$username,
4329: $domain,$auname,$audom);
1.1184 raeburn 4330: }
4331: return;
4332: }
4333:
1.351 www 4334: sub get_course_adv_roles {
1.948 raeburn 4335: my ($cid,$codes) = @_;
1.620 albertel 4336: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 4337: my %coursehash=&coursedescription($cid);
1.988 raeburn 4338: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 4339: my %nothide=();
1.800 albertel 4340: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 4341: if ($user !~ /:/) {
4342: $nothide{join(':',split(/[\@]/,$user))}=1;
4343: } else {
4344: $nothide{$user}=1;
4345: }
1.470 www 4346: }
1.1219 raeburn 4347: my @possdoms = ($coursehash{'domain'});
4348: if ($coursehash{'checkforpriv'}) {
4349: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
4350: }
1.351 www 4351: my %returnhash=();
4352: my %dumphash=
4353: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
4354: my $now=time;
1.997 raeburn 4355: my %privileged;
1.1000 raeburn 4356: foreach my $entry (keys(%dumphash)) {
1.800 albertel 4357: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 4358: if (($tstart) && ($tstart<0)) { next; }
4359: if (($tend) && ($tend<$now)) { next; }
4360: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 4361: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 4362: if ($username eq '' || $domain eq '') { next; }
1.1219 raeburn 4363: if ((&privileged($username,$domain,\@possdoms)) &&
1.997 raeburn 4364: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 4365: if ($role eq 'cr') { next; }
1.948 raeburn 4366: if ($codes) {
4367: if ($section) { $role .= ':'.$section; }
4368: if ($returnhash{$role}) {
4369: $returnhash{$role}.=','.$username.':'.$domain;
4370: } else {
4371: $returnhash{$role}=$username.':'.$domain;
4372: }
1.351 www 4373: } else {
1.988 raeburn 4374: my $key=&plaintext($role,$crstype);
1.973 bisitz 4375: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 4376: if ($returnhash{$key}) {
4377: $returnhash{$key}.=','.$username.':'.$domain;
4378: } else {
4379: $returnhash{$key}=$username.':'.$domain;
4380: }
1.351 www 4381: }
1.948 raeburn 4382: }
1.400 www 4383: return %returnhash;
4384: }
4385:
4386: sub get_my_roles {
1.937 raeburn 4387: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 4388: unless (defined($uname)) { $uname=$env{'user.name'}; }
4389: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 4390: my (%dumphash,%nothide);
1.1086 raeburn 4391: if ($context eq 'userroles') {
1.1166 raeburn 4392: %dumphash = &dump('roles',$udom,$uname);
1.858 raeburn 4393: } else {
1.1219 raeburn 4394: %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 4395: if ($hidepriv) {
4396: my %coursehash=&coursedescription($udom.'_'.$uname);
4397: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
4398: if ($user !~ /:/) {
4399: $nothide{join(':',split(/[\@]/,$user))} = 1;
4400: } else {
4401: $nothide{$user} = 1;
4402: }
4403: }
4404: }
1.858 raeburn 4405: }
1.400 www 4406: my %returnhash=();
4407: my $now=time;
1.999 raeburn 4408: my %privileged;
1.800 albertel 4409: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 4410: my ($role,$tend,$tstart);
4411: if ($context eq 'userroles') {
1.1149 raeburn 4412: next if ($entry =~ /^rolesdef/);
1.867 raeburn 4413: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
4414: } else {
4415: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
4416: }
1.400 www 4417: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 4418: my $status = 'active';
1.939 raeburn 4419: if (($tend) && ($tend<=$now)) {
1.832 raeburn 4420: $status = 'previous';
4421: }
4422: if (($tstart) && ($now<$tstart)) {
4423: $status = 'future';
4424: }
4425: if (ref($types) eq 'ARRAY') {
4426: if (!grep(/^\Q$status\E$/,@{$types})) {
4427: next;
4428: }
4429: } else {
4430: if ($status ne 'active') {
4431: next;
4432: }
4433: }
1.867 raeburn 4434: my ($rolecode,$username,$domain,$section,$area);
4435: if ($context eq 'userroles') {
1.1186 raeburn 4436: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867 raeburn 4437: (undef,$domain,$username,$section) = split(/\//,$area);
4438: } else {
4439: ($role,$username,$domain,$section) = split(/\:/,$entry);
4440: }
1.832 raeburn 4441: if (ref($roledoms) eq 'ARRAY') {
4442: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
4443: next;
4444: }
4445: }
4446: if (ref($roles) eq 'ARRAY') {
4447: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 4448: if ($role =~ /^cr\//) {
4449: if (!grep(/^cr$/,@{$roles})) {
4450: next;
4451: }
1.1104 raeburn 4452: } elsif ($role =~ /^gr\//) {
4453: if (!grep(/^gr$/,@{$roles})) {
4454: next;
4455: }
1.922 raeburn 4456: } else {
4457: next;
4458: }
1.832 raeburn 4459: }
1.867 raeburn 4460: }
1.937 raeburn 4461: if ($hidepriv) {
1.1219 raeburn 4462: my @privroles = ('dc','su');
1.999 raeburn 4463: if ($context eq 'userroles') {
1.1219 raeburn 4464: next if (grep(/^\Q$role\E$/,@privroles));
1.999 raeburn 4465: } else {
1.1219 raeburn 4466: my $possdoms = [$domain];
4467: if (ref($roledoms) eq 'ARRAY') {
4468: push(@{$possdoms},@{$roledoms});
1.999 raeburn 4469: }
1.1219 raeburn 4470: if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999 raeburn 4471: if (!$nothide{$username.':'.$domain}) {
4472: next;
4473: }
4474: }
1.937 raeburn 4475: }
4476: }
1.933 raeburn 4477: if ($withsec) {
4478: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
4479: $tstart.':'.$tend;
4480: } else {
4481: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
4482: }
1.832 raeburn 4483: }
1.373 www 4484: return %returnhash;
1.399 www 4485: }
4486:
4487: # ----------------------------------------------------- Frontpage Announcements
4488: #
4489: #
4490:
4491: sub postannounce {
4492: my ($server,$text)=@_;
1.844 albertel 4493: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 4494: unless ($text=~/\w/) { $text=''; }
4495: return &reply('setannounce:'.&escape($text),$server);
4496: }
4497:
4498: sub getannounce {
1.448 albertel 4499:
4500: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 4501: my $announcement='';
1.800 albertel 4502: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 4503: close($fh);
1.399 www 4504: if ($announcement=~/\w/) {
4505: return
4506: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 4507: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 4508: } else {
4509: return '';
4510: }
4511: } else {
4512: return '';
4513: }
1.351 www 4514: }
1.353 www 4515:
4516: # ---------------------------------------------------------- Course ID routines
4517: # Deal with domain's nohist_courseid.db files
4518: #
4519:
4520: sub courseidput {
1.921 raeburn 4521: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 4522: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 4523: my $outcome;
4524: if ($caller eq 'timeonly') {
4525: my $cids = '';
4526: foreach my $item (keys(%$storehash)) {
4527: $cids.=&escape($item).'&';
4528: }
4529: $cids=~s/\&$//;
4530: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
4531: $coursehome);
4532: } else {
4533: my $items = '';
4534: foreach my $item (keys(%$storehash)) {
4535: $items.= &escape($item).'='.
4536: &freeze_escape($$storehash{$item}).'&';
4537: }
4538: $items=~s/\&$//;
4539: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
4540: $coursehome);
1.918 raeburn 4541: }
4542: if ($outcome eq 'unknown_cmd') {
4543: my $what;
4544: foreach my $cid (keys(%$storehash)) {
4545: $what .= &escape($cid).'=';
1.921 raeburn 4546: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 4547: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 4548: }
4549: $what =~ s/\:$/&/;
4550: }
4551: $what =~ s/\&$//;
4552: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
4553: } else {
4554: return $outcome;
4555: }
1.353 www 4556: }
4557:
4558: sub courseiddump {
1.921 raeburn 4559: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 4560: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 4561: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247 raeburn 4562: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287 raeburn 4563: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918 raeburn 4564: my $as_hash = 1;
4565: my %returnhash;
4566: if (!$domfilter) { $domfilter=''; }
1.845 albertel 4567: my %libserv = &all_library();
4568: foreach my $tryserver (keys(%libserv)) {
4569: if ( ( $hostidflag == 1
4570: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
4571: || (!defined($hostidflag)) ) {
4572:
1.918 raeburn 4573: if (($domfilter eq '') ||
4574: (&host_domain($tryserver) eq $domfilter)) {
1.1180 droeschl 4575: my $rep;
4576: if (grep { $_ eq $tryserver } current_machine_ids()) {
4577: $rep = LONCAPA::Lond::dump_course_id_handler(
4578: join(":", (&host_domain($tryserver), $sincefilter,
4579: &escape($descfilter), &escape($instcodefilter),
4580: &escape($ownerfilter), &escape($coursefilter),
4581: &escape($typefilter), &escape($regexp_ok),
4582: $as_hash, &escape($selfenrollonly),
4583: &escape($catfilter), $showhidden, $caller,
4584: &escape($cloner), &escape($cc_clone), $cloneonly,
4585: &escape($createdbefore), &escape($createdafter),
1.1287 raeburn 4586: &escape($creationcontext),$domcloner,$hasuniquecode,
4587: $reqcrsdom,&escape($reqinstcode))));
1.1180 droeschl 4588: } else {
4589: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
4590: $sincefilter.':'.&escape($descfilter).':'.
4591: &escape($instcodefilter).':'.&escape($ownerfilter).
4592: ':'.&escape($coursefilter).':'.&escape($typefilter).
4593: ':'.&escape($regexp_ok).':'.$as_hash.':'.
4594: &escape($selfenrollonly).':'.&escape($catfilter).':'.
4595: $showhidden.':'.$caller.':'.&escape($cloner).':'.
4596: &escape($cc_clone).':'.$cloneonly.':'.
4597: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287 raeburn 4598: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
4599: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180 droeschl 4600: }
4601:
1.918 raeburn 4602: my @pairs=split(/\&/,$rep);
4603: foreach my $item (@pairs) {
4604: my ($key,$value)=split(/\=/,$item,2);
4605: $key = &unescape($key);
4606: next if ($key =~ /^error: 2 /);
4607: my $result = &thaw_unescape($value);
4608: if (ref($result) eq 'HASH') {
4609: $returnhash{$key}=$result;
4610: } else {
1.921 raeburn 4611: my @responses = split(/:/,$value);
4612: my @items = ('description','inst_code','owner','type');
1.918 raeburn 4613: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 4614: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 4615: }
1.1008 raeburn 4616: }
1.353 www 4617: }
4618: }
4619: }
4620: }
4621: return %returnhash;
4622: }
4623:
1.1055 raeburn 4624: sub courselastaccess {
4625: my ($cdom,$cnum,$hostidref) = @_;
4626: my %returnhash;
4627: if ($cdom && $cnum) {
4628: my $chome = &homeserver($cnum,$cdom);
4629: if ($chome ne 'no_host') {
4630: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
4631: &extract_lastaccess(\%returnhash,$rep);
4632: }
4633: } else {
4634: if (!$cdom) { $cdom=''; }
4635: my %libserv = &all_library();
4636: foreach my $tryserver (keys(%libserv)) {
4637: if (ref($hostidref) eq 'ARRAY') {
4638: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
4639: }
4640: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
4641: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
4642: &extract_lastaccess(\%returnhash,$rep);
4643: }
4644: }
4645: }
4646: return %returnhash;
4647: }
4648:
4649: sub extract_lastaccess {
4650: my ($returnhash,$rep) = @_;
4651: if (ref($returnhash) eq 'HASH') {
4652: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
4653: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
4654: $rep eq '') {
4655: my @pairs=split(/\&/,$rep);
4656: foreach my $item (@pairs) {
4657: my ($key,$value)=split(/\=/,$item,2);
4658: $key = &unescape($key);
4659: next if ($key =~ /^error: 2 /);
4660: $returnhash->{$key} = &thaw_unescape($value);
4661: }
4662: }
4663: }
4664: return;
4665: }
4666:
1.658 raeburn 4667: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 4668:
4669: sub dcmailput {
1.685 raeburn 4670: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 4671: my $status = &Apache::lonnet::critical(
1.740 www 4672: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
4673: &escape($message),$server);
1.662 raeburn 4674: return $status;
4675: }
4676:
1.658 raeburn 4677: sub dcmaildump {
4678: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 4679: my %returnhash=();
1.846 albertel 4680:
4681: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 4682: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
4683: &escape($enddate).':';
4684: my @esc_senders=map { &escape($_)} @$senders;
4685: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 4686: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 4687: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 4688: if (($key) && ($value)) {
4689: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 4690: }
4691: }
4692: }
4693: return %returnhash;
4694: }
1.662 raeburn 4695: # ---------------------------------------------------------- Domain roles
4696:
4697: sub get_domain_roles {
4698: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 4699: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 4700: $startdate = '.';
4701: }
1.1018 raeburn 4702: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 4703: $enddate = '.';
4704: }
1.922 raeburn 4705: my $rolelist;
4706: if (ref($roles) eq 'ARRAY') {
1.1219 raeburn 4707: $rolelist = join('&',@{$roles});
1.922 raeburn 4708: }
1.662 raeburn 4709: my %personnel = ();
1.841 albertel 4710:
4711: my %servers = &get_servers($dom,'library');
4712: foreach my $tryserver (keys(%servers)) {
4713: %{$personnel{$tryserver}}=();
4714: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
4715: &escape($startdate).':'.
4716: &escape($enddate).':'.
4717: &escape($rolelist), $tryserver))) {
4718: my ($key,$value) = split(/\=/,$line,2);
4719: if (($key) && ($value)) {
4720: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
4721: }
4722: }
1.662 raeburn 4723: }
4724: return %personnel;
4725: }
1.658 raeburn 4726:
1.1057 www 4727: # ----------------------------------------------------------- Interval timing
1.149 www 4728:
1.1153 www 4729: {
4730: # Caches needed for speedup of navmaps
4731: # We don't want to cache this for very long at all (5 seconds at most)
4732: #
4733: # The user for whom we cache
4734: my $cachedkey='';
4735: # The cached times for this user
4736: my %cachedtimes=();
4737: # When this was last done
1.1282 raeburn 4738: my $cachedtime='';
1.1153 www 4739:
4740: sub load_all_first_access {
1.1308 raeburn 4741: my ($uname,$udom,$ignorecache)=@_;
1.1156 www 4742: if (($cachedkey eq $uname.':'.$udom) &&
1.1308 raeburn 4743: (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
4744: (!$ignorecache)) {
1.1154 raeburn 4745: return;
4746: }
4747: $cachedtime=time;
4748: $cachedkey=$uname.':'.$udom;
4749: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153 www 4750: }
4751:
1.504 albertel 4752: sub get_first_access {
1.1308 raeburn 4753: my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790 albertel 4754: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 4755: if ($argsymb) { $symb=$argsymb; }
4756: my ($map,$id,$res)=&decode_symb($symb);
1.1162 raeburn 4757: if ($argmap) { $map = $argmap; }
1.926 albertel 4758: if ($type eq 'course') {
4759: $res='course';
4760: } elsif ($type eq 'map') {
1.588 albertel 4761: $res=&symbread($map);
4762: } else {
4763: $res=$symb;
4764: }
1.1308 raeburn 4765: &load_all_first_access($uname,$udom,$ignorecache);
1.1153 www 4766: return $cachedtimes{"$courseid\0$res"};
1.504 albertel 4767: }
4768:
4769: sub set_first_access {
1.1162 raeburn 4770: my ($type,$interval)=@_;
1.790 albertel 4771: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 4772: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 4773: if ($type eq 'course') {
4774: $res='course';
4775: } elsif ($type eq 'map') {
1.588 albertel 4776: $res=&symbread($map);
4777: } else {
4778: $res=$symb;
4779: }
1.1153 www 4780: $cachedkey='';
1.1162 raeburn 4781: my $firstaccess=&get_first_access($type,$symb,$map);
1.505 albertel 4782: if (!$firstaccess) {
1.1162 raeburn 4783: my $start = time;
4784: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
4785: $udom,$uname);
4786: if ($putres eq 'ok') {
4787: &put('timerinterval',{"$courseid\0$res"=>$interval},
4788: $udom,$uname);
4789: &appenv(
4790: {
4791: 'course.'.$courseid.'.firstaccess.'.$res => $start,
4792: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
4793: }
4794: );
4795: }
4796: return $putres;
1.505 albertel 4797: }
4798: return 'already_set';
1.504 albertel 4799: }
1.1153 www 4800: }
1.1282 raeburn 4801:
1.110 www 4802: # --------------------------------------------- Set Expire Date for Spreadsheet
4803:
4804: sub expirespread {
4805: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 4806: my $cid=$env{'request.course.id'};
1.110 www 4807: if ($cid) {
4808: my $now=time;
4809: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 4810: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
4811: $env{'course.'.$cid.'.num'}.
1.110 www 4812: ':nohist_expirationdates:'.
4813: &escape($key).'='.$now,
1.620 albertel 4814: $env{'course.'.$cid.'.home'})
1.110 www 4815: }
4816: return 'ok';
1.14 www 4817: }
4818:
1.109 www 4819: # ----------------------------------------------------- Devalidate Spreadsheets
4820:
4821: sub devalidate {
1.325 www 4822: my ($symb,$uname,$udom)=@_;
1.620 albertel 4823: my $cid=$env{'request.course.id'};
1.109 www 4824: if ($cid) {
1.391 matthew 4825: # delete the stored spreadsheets for
4826: # - the student level sheet of this user in course's homespace
4827: # - the assessment level sheet for this resource
4828: # for this user in user's homespace
1.553 albertel 4829: # - current conditional state info
1.325 www 4830: my $key=$uname.':'.$udom.':';
1.109 www 4831: my $status=
1.299 matthew 4832: &del('nohist_calculatedsheets',
1.391 matthew 4833: [$key.'studentcalc:'],
1.620 albertel 4834: $env{'course.'.$cid.'.domain'},
4835: $env{'course.'.$cid.'.num'})
1.133 albertel 4836: .' '.
4837: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 4838: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 4839: unless ($status eq 'ok ok') {
4840: &logthis('Could not devalidate spreadsheet '.
1.325 www 4841: $uname.' at '.$udom.' for '.
1.109 www 4842: $symb.': '.$status);
1.133 albertel 4843: }
1.553 albertel 4844: &delenv('user.state.'.$cid);
1.109 www 4845: }
4846: }
4847:
1.265 albertel 4848: sub get_scalar {
4849: my ($string,$end) = @_;
4850: my $value;
4851: if ($$string =~ s/^([^&]*?)($end)/$2/) {
4852: $value = $1;
4853: } elsif ($$string =~ s/^([^&]*?)&//) {
4854: $value = $1;
4855: }
4856: return &unescape($value);
4857: }
4858:
4859: sub array2str {
4860: my (@array) = @_;
4861: my $result=&arrayref2str(\@array);
4862: $result=~s/^__ARRAY_REF__//;
4863: $result=~s/__END_ARRAY_REF__$//;
4864: return $result;
4865: }
4866:
1.204 albertel 4867: sub arrayref2str {
4868: my ($arrayref) = @_;
1.265 albertel 4869: my $result='__ARRAY_REF__';
1.204 albertel 4870: foreach my $elem (@$arrayref) {
1.265 albertel 4871: if(ref($elem) eq 'ARRAY') {
4872: $result.=&arrayref2str($elem).'&';
4873: } elsif(ref($elem) eq 'HASH') {
4874: $result.=&hashref2str($elem).'&';
4875: } elsif(ref($elem)) {
4876: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 4877: } else {
4878: $result.=&escape($elem).'&';
4879: }
4880: }
4881: $result=~s/\&$//;
1.265 albertel 4882: $result .= '__END_ARRAY_REF__';
1.204 albertel 4883: return $result;
4884: }
4885:
1.168 albertel 4886: sub hash2str {
1.204 albertel 4887: my (%hash) = @_;
4888: my $result=&hashref2str(\%hash);
1.265 albertel 4889: $result=~s/^__HASH_REF__//;
4890: $result=~s/__END_HASH_REF__$//;
1.204 albertel 4891: return $result;
4892: }
4893:
4894: sub hashref2str {
4895: my ($hashref)=@_;
1.265 albertel 4896: my $result='__HASH_REF__';
1.800 albertel 4897: foreach my $key (sort(keys(%$hashref))) {
4898: if (ref($key) eq 'ARRAY') {
4899: $result.=&arrayref2str($key).'=';
4900: } elsif (ref($key) eq 'HASH') {
4901: $result.=&hashref2str($key).'=';
4902: } elsif (ref($key)) {
1.265 albertel 4903: $result.='=';
1.800 albertel 4904: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 4905: } else {
1.1132 raeburn 4906: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 4907: }
4908:
1.800 albertel 4909: if(ref($hashref->{$key}) eq 'ARRAY') {
4910: $result.=&arrayref2str($hashref->{$key}).'&';
4911: } elsif(ref($hashref->{$key}) eq 'HASH') {
4912: $result.=&hashref2str($hashref->{$key}).'&';
4913: } elsif(ref($hashref->{$key})) {
1.265 albertel 4914: $result.='&';
1.800 albertel 4915: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 4916: } else {
1.800 albertel 4917: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 4918: }
4919: }
1.168 albertel 4920: $result=~s/\&$//;
1.265 albertel 4921: $result .= '__END_HASH_REF__';
1.168 albertel 4922: return $result;
4923: }
4924:
4925: sub str2hash {
1.265 albertel 4926: my ($string)=@_;
4927: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
4928: return %$hash;
4929: }
4930:
4931: sub str2hashref {
1.168 albertel 4932: my ($string) = @_;
1.265 albertel 4933:
4934: my %hash;
4935:
4936: if($string !~ /^__HASH_REF__/) {
4937: if (! ($string eq '' || !defined($string))) {
4938: $hash{'error'}='Not hash reference';
4939: }
4940: return (\%hash, $string);
4941: }
4942:
4943: $string =~ s/^__HASH_REF__//;
4944:
4945: while($string !~ /^__END_HASH_REF__/) {
4946: #key
4947: my $key='';
4948: if($string =~ /^__HASH_REF__/) {
4949: ($key, $string)=&str2hashref($string);
4950: if(defined($key->{'error'})) {
4951: $hash{'error'}='Bad data';
4952: return (\%hash, $string);
4953: }
4954: } elsif($string =~ /^__ARRAY_REF__/) {
4955: ($key, $string)=&str2arrayref($string);
4956: if($key->[0] eq 'Array reference error') {
4957: $hash{'error'}='Bad data';
4958: return (\%hash, $string);
4959: }
4960: } else {
4961: $string =~ s/^(.*?)=//;
1.267 albertel 4962: $key=&unescape($1);
1.265 albertel 4963: }
4964: $string =~ s/^=//;
4965:
4966: #value
4967: my $value='';
4968: if($string =~ /^__HASH_REF__/) {
4969: ($value, $string)=&str2hashref($string);
4970: if(defined($value->{'error'})) {
4971: $hash{'error'}='Bad data';
4972: return (\%hash, $string);
4973: }
4974: } elsif($string =~ /^__ARRAY_REF__/) {
4975: ($value, $string)=&str2arrayref($string);
4976: if($value->[0] eq 'Array reference error') {
4977: $hash{'error'}='Bad data';
4978: return (\%hash, $string);
4979: }
4980: } else {
4981: $value=&get_scalar(\$string,'__END_HASH_REF__');
4982: }
4983: $string =~ s/^&//;
4984:
4985: $hash{$key}=$value;
1.204 albertel 4986: }
1.265 albertel 4987:
4988: $string =~ s/^__END_HASH_REF__//;
4989:
4990: return (\%hash, $string);
1.204 albertel 4991: }
4992:
4993: sub str2array {
1.265 albertel 4994: my ($string)=@_;
4995: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
4996: return @$array;
4997: }
4998:
4999: sub str2arrayref {
1.204 albertel 5000: my ($string) = @_;
1.265 albertel 5001: my @array;
5002:
5003: if($string !~ /^__ARRAY_REF__/) {
5004: if (! ($string eq '' || !defined($string))) {
5005: $array[0]='Array reference error';
5006: }
5007: return (\@array, $string);
5008: }
5009:
5010: $string =~ s/^__ARRAY_REF__//;
5011:
5012: while($string !~ /^__END_ARRAY_REF__/) {
5013: my $value='';
5014: if($string =~ /^__HASH_REF__/) {
5015: ($value, $string)=&str2hashref($string);
5016: if(defined($value->{'error'})) {
5017: $array[0] ='Array reference error';
5018: return (\@array, $string);
5019: }
5020: } elsif($string =~ /^__ARRAY_REF__/) {
5021: ($value, $string)=&str2arrayref($string);
5022: if($value->[0] eq 'Array reference error') {
5023: $array[0] ='Array reference error';
5024: return (\@array, $string);
5025: }
5026: } else {
5027: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
5028: }
5029: $string =~ s/^&//;
5030:
5031: push(@array, $value);
1.191 harris41 5032: }
1.265 albertel 5033:
5034: $string =~ s/^__END_ARRAY_REF__//;
5035:
5036: return (\@array, $string);
1.168 albertel 5037: }
5038:
1.167 albertel 5039: # -------------------------------------------------------------------Temp Store
5040:
1.168 albertel 5041: sub tmpreset {
5042: my ($symb,$namespace,$domain,$stuname) = @_;
5043: if (!$symb) {
5044: $symb=&symbread();
1.620 albertel 5045: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5046: }
5047: $symb=escape($symb);
5048:
1.620 albertel 5049: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 5050: $namespace=~s/\//\_/g;
5051: $namespace=~s/\W//g;
5052:
1.620 albertel 5053: if (!$domain) { $domain=$env{'user.domain'}; }
5054: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5055: if ($domain eq 'public' && $stuname eq 'public') {
5056: $stuname=$ENV{'REMOTE_ADDR'};
5057: }
1.1117 foxr 5058: my $path=LONCAPA::tempdir();
1.168 albertel 5059: my %hash;
5060: if (tie(%hash,'GDBM_File',
5061: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5062: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 5063: foreach my $key (keys(%hash)) {
1.180 albertel 5064: if ($key=~ /:$symb/) {
1.168 albertel 5065: delete($hash{$key});
5066: }
5067: }
5068: }
5069: }
5070:
1.167 albertel 5071: sub tmpstore {
1.168 albertel 5072: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
5073:
5074: if (!$symb) {
5075: $symb=&symbread();
1.620 albertel 5076: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5077: }
5078: $symb=escape($symb);
5079:
5080: if (!$namespace) {
5081: # I don't think we would ever want to store this for a course.
5082: # it seems this will only be used if we don't have a course.
1.620 albertel 5083: #$namespace=$env{'request.course.id'};
1.168 albertel 5084: #if (!$namespace) {
1.620 albertel 5085: $namespace=$env{'request.state'};
1.168 albertel 5086: #}
5087: }
5088: $namespace=~s/\//\_/g;
5089: $namespace=~s/\W//g;
1.620 albertel 5090: if (!$domain) { $domain=$env{'user.domain'}; }
5091: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5092: if ($domain eq 'public' && $stuname eq 'public') {
5093: $stuname=$ENV{'REMOTE_ADDR'};
5094: }
1.168 albertel 5095: my $now=time;
5096: my %hash;
1.1117 foxr 5097: my $path=LONCAPA::tempdir();
1.168 albertel 5098: if (tie(%hash,'GDBM_File',
5099: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5100: &GDBM_WRCREAT(),0640)) {
1.168 albertel 5101: $hash{"version:$symb"}++;
5102: my $version=$hash{"version:$symb"};
5103: my $allkeys='';
5104: foreach my $key (keys(%$storehash)) {
5105: $allkeys.=$key.':';
1.591 albertel 5106: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 5107: }
5108: $hash{"$version:$symb:timestamp"}=$now;
5109: $allkeys.='timestamp';
5110: $hash{"$version:keys:$symb"}=$allkeys;
5111: if (untie(%hash)) {
5112: return 'ok';
5113: } else {
5114: return "error:$!";
5115: }
5116: } else {
5117: return "error:$!";
5118: }
5119: }
1.167 albertel 5120:
1.168 albertel 5121: # -----------------------------------------------------------------Temp Restore
1.167 albertel 5122:
1.168 albertel 5123: sub tmprestore {
5124: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 5125:
1.168 albertel 5126: if (!$symb) {
5127: $symb=&symbread();
1.620 albertel 5128: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5129: }
5130: $symb=escape($symb);
5131:
1.620 albertel 5132: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 5133:
1.620 albertel 5134: if (!$domain) { $domain=$env{'user.domain'}; }
5135: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5136: if ($domain eq 'public' && $stuname eq 'public') {
5137: $stuname=$ENV{'REMOTE_ADDR'};
5138: }
1.168 albertel 5139: my %returnhash;
5140: $namespace=~s/\//\_/g;
5141: $namespace=~s/\W//g;
5142: my %hash;
1.1117 foxr 5143: my $path=LONCAPA::tempdir();
1.168 albertel 5144: if (tie(%hash,'GDBM_File',
5145: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5146: &GDBM_READER(),0640)) {
1.168 albertel 5147: my $version=$hash{"version:$symb"};
5148: $returnhash{'version'}=$version;
5149: my $scope;
5150: for ($scope=1;$scope<=$version;$scope++) {
5151: my $vkeys=$hash{"$scope:keys:$symb"};
5152: my @keys=split(/:/,$vkeys);
5153: my $key;
5154: $returnhash{"$scope:keys"}=$vkeys;
5155: foreach $key (@keys) {
1.591 albertel 5156: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
5157: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 5158: }
5159: }
1.168 albertel 5160: if (!(untie(%hash))) {
5161: return "error:$!";
5162: }
5163: } else {
5164: return "error:$!";
5165: }
5166: return %returnhash;
1.167 albertel 5167: }
5168:
1.9 www 5169: # ----------------------------------------------------------------------- Store
5170:
5171: sub store {
1.1269 raeburn 5172: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 5173: my $home='';
5174:
1.168 albertel 5175: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5176:
1.213 www 5177: $symb=&symbclean($symb);
1.122 albertel 5178: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 5179:
1.620 albertel 5180: if (!$domain) { $domain=$env{'user.domain'}; }
5181: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 5182:
5183: &devalidate($symb,$stuname,$domain);
1.109 www 5184:
5185: $symb=escape($symb);
1.187 www 5186: if (!$namespace) {
1.620 albertel 5187: unless ($namespace=$env{'request.course.id'}) {
1.187 www 5188: return '';
5189: }
5190: }
1.620 albertel 5191: if (!$home) { $home=$env{'user.home'}; }
1.447 www 5192:
5193: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5194: $$storehash{'host'}=$perlvar{'lonHostID'};
5195:
1.12 www 5196: my $namevalue='';
1.800 albertel 5197: foreach my $key (keys(%$storehash)) {
5198: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 5199: }
1.12 www 5200: $namevalue=~s/\&$//;
1.187 www 5201: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269 raeburn 5202: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9 www 5203: }
5204:
1.47 www 5205: # -------------------------------------------------------------- Critical Store
5206:
5207: sub cstore {
1.1269 raeburn 5208: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 5209: my $home='';
5210:
1.168 albertel 5211: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5212:
1.213 www 5213: $symb=&symbclean($symb);
1.122 albertel 5214: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 5215:
1.620 albertel 5216: if (!$domain) { $domain=$env{'user.domain'}; }
5217: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 5218:
5219: &devalidate($symb,$stuname,$domain);
1.109 www 5220:
5221: $symb=escape($symb);
1.187 www 5222: if (!$namespace) {
1.620 albertel 5223: unless ($namespace=$env{'request.course.id'}) {
1.187 www 5224: return '';
5225: }
5226: }
1.620 albertel 5227: if (!$home) { $home=$env{'user.home'}; }
1.447 www 5228:
5229: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5230: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 5231:
1.47 www 5232: my $namevalue='';
1.800 albertel 5233: foreach my $key (keys(%$storehash)) {
5234: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 5235: }
1.47 www 5236: $namevalue=~s/\&$//;
1.187 www 5237: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 5238: return critical
1.1269 raeburn 5239: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47 www 5240: }
5241:
1.9 www 5242: # --------------------------------------------------------------------- Restore
5243:
5244: sub restore {
1.124 www 5245: my ($symb,$namespace,$domain,$stuname) = @_;
5246: my $home='';
5247:
1.168 albertel 5248: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5249:
1.122 albertel 5250: if (!$symb) {
1.1224 raeburn 5251: return if ($namespace eq 'courserequests');
5252: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 5253: } else {
1.1224 raeburn 5254: unless ($namespace eq 'courserequests') {
5255: $symb=&escape(&symbclean($symb));
5256: }
1.122 albertel 5257: }
1.188 www 5258: if (!$namespace) {
1.620 albertel 5259: unless ($namespace=$env{'request.course.id'}) {
1.188 www 5260: return '';
5261: }
5262: }
1.620 albertel 5263: if (!$domain) { $domain=$env{'user.domain'}; }
5264: if (!$stuname) { $stuname=$env{'user.name'}; }
5265: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 5266: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
5267:
1.12 www 5268: my %returnhash=();
1.800 albertel 5269: foreach my $line (split(/\&/,$answer)) {
5270: my ($name,$value)=split(/\=/,$line);
1.591 albertel 5271: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 5272: }
1.75 www 5273: my $version;
5274: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 5275: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
5276: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 5277: }
1.75 www 5278: }
1.13 www 5279: return %returnhash;
1.34 www 5280: }
5281:
5282: # ---------------------------------------------------------- Course Description
1.1118 foxr 5283: #
5284: #
1.34 www 5285:
5286: sub coursedescription {
1.731 albertel 5287: my ($courseid,$args)=@_;
1.34 www 5288: $courseid=~s/^\///;
1.49 www 5289: $courseid=~s/\_/\//g;
1.34 www 5290: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 5291: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 5292: my $normalid=$cdomain.'_'.$cnum;
5293: # need to always cache even if we get errors otherwise we keep
5294: # trying and trying and trying to get the course description.
5295: my %envhash=();
5296: my %returnhash=();
1.731 albertel 5297:
5298: my $expiretime=600;
5299: if ($env{'request.course.id'} eq $normalid) {
5300: $expiretime=120;
5301: }
5302:
5303: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
5304: if (!$args->{'freshen_cache'}
5305: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
5306: foreach my $key (keys(%env)) {
5307: next if ($key !~ /^\Q$prefix\E(.*)/);
5308: my ($setting) = $1;
5309: $returnhash{$setting} = $env{$key};
5310: }
5311: return %returnhash;
5312: }
5313:
1.1118 foxr 5314: # get the data again
5315:
1.731 albertel 5316: if (!$args->{'one_time'}) {
5317: $envhash{'course.'.$normalid.'.last_cache'}=time;
5318: }
1.811 albertel 5319:
1.34 www 5320: if ($chome ne 'no_host') {
1.302 albertel 5321: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 5322: if (!exists($returnhash{'con_lost'})) {
1.1118 foxr 5323: my $username = $env{'user.name'}; # Defult username
5324: if(defined $args->{'user'}) {
5325: $username = $args->{'user'};
5326: }
1.129 albertel 5327: $returnhash{'home'}= $chome;
5328: $returnhash{'domain'} = $cdomain;
5329: $returnhash{'num'} = $cnum;
1.741 raeburn 5330: if (!defined($returnhash{'type'})) {
5331: $returnhash{'type'} = 'Course';
5332: }
1.130 albertel 5333: while (my ($name,$value) = each %returnhash) {
1.53 www 5334: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 5335: }
1.270 www 5336: $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117 foxr 5337: $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118 foxr 5338: $username.'_'.$cdomain.'_'.$cnum;
1.60 www 5339: $envhash{'course.'.$normalid.'.home'}=$chome;
5340: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
5341: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 5342: }
5343: }
1.731 albertel 5344: if (!$args->{'one_time'}) {
1.949 raeburn 5345: &appenv(\%envhash);
1.731 albertel 5346: }
1.302 albertel 5347: return %returnhash;
1.461 www 5348: }
5349:
1.1080 raeburn 5350: sub update_released_required {
5351: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
5352: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
5353: $cid = $env{'request.course.id'};
5354: $cdom = $env{'course.'.$cid.'.domain'};
5355: $cnum = $env{'course.'.$cid.'.num'};
5356: $chome = $env{'course.'.$cid.'.home'};
5357: }
5358: if ($needsrelease) {
5359: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
5360: my $needsupdate;
5361: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
5362: $needsupdate = 1;
5363: } else {
5364: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
5365: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
5366: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
5367: $needsupdate = 1;
5368: }
5369: }
5370: if ($needsupdate) {
5371: my %needshash = (
5372: 'internal.releaserequired' => $needsrelease,
5373: );
5374: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
5375: if ($putresult eq 'ok') {
5376: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
5377: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
5378: if (ref($crsinfo{$cid}) eq 'HASH') {
5379: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
5380: &courseidput($cdom,\%crsinfo,$chome,'notime');
5381: }
5382: }
5383: }
5384: }
5385: return;
5386: }
5387:
1.461 www 5388: # -------------------------------------------------See if a user is privileged
5389:
5390: sub privileged {
1.1219 raeburn 5391: my ($username,$domain,$possdomains,$possroles)=@_;
1.1170 droeschl 5392: my $now = time;
1.1219 raeburn 5393: my $roles;
5394: if (ref($possroles) eq 'ARRAY') {
5395: $roles = $possroles;
5396: } else {
5397: $roles = ['dc','su'];
5398: }
5399: if (ref($possdomains) eq 'ARRAY') {
5400: my %privileged = &privileged_by_domain($possdomains,$roles);
5401: foreach my $dom (@{$possdomains}) {
5402: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
5403: (ref($privileged{$dom}) eq 'HASH')) {
5404: foreach my $role (@{$roles}) {
5405: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5406: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
5407: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
5408: return 1 unless (($end && $end < $now) ||
5409: ($start && $start > $now));
5410: }
5411: }
5412: }
5413: }
5414: }
5415: } else {
5416: my %rolesdump = &dump("roles", $domain, $username) or return 0;
5417: my $now = time;
1.1170 droeschl 5418:
1.1275 musolffc 5419: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170 droeschl 5420: my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219 raeburn 5421: if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170 droeschl 5422: return 1 unless ($tend && $tend < $now)
1.1219 raeburn 5423: or ($tstart && $tstart > $now);
1.1170 droeschl 5424: }
1.1219 raeburn 5425: }
5426: }
5427: return 0;
5428: }
1.1170 droeschl 5429:
1.1219 raeburn 5430: sub privileged_by_domain {
5431: my ($domains,$roles) = @_;
5432: my %privileged = ();
5433: my $cachetime = 60*60*24;
5434: my $now = time;
5435: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
5436: return %privileged;
5437: }
5438: foreach my $dom (@{$domains}) {
5439: next if (ref($privileged{$dom}) eq 'HASH');
5440: my $needroles;
5441: foreach my $role (@{$roles}) {
5442: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
5443: if (defined($cached)) {
5444: if (ref($result) eq 'HASH') {
5445: $privileged{$dom}{$role} = $result;
5446: }
5447: } else {
5448: $needroles = 1;
5449: }
5450: }
5451: if ($needroles) {
5452: my %dompersonnel = &get_domain_roles($dom,$roles);
5453: $privileged{$dom} = {};
5454: foreach my $server (keys(%dompersonnel)) {
5455: if (ref($dompersonnel{$server}) eq 'HASH') {
5456: foreach my $item (keys(%{$dompersonnel{$server}})) {
5457: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
5458: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
5459: next if ($end && $end < $now);
5460: $privileged{$dom}{$trole}{$uname.':'.$udom} =
5461: $dompersonnel{$server}{$item};
5462: }
5463: }
5464: }
5465: if (ref($privileged{$dom}) eq 'HASH') {
5466: foreach my $role (@{$roles}) {
5467: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5468: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
5469: } else {
5470: my %hash = ();
5471: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
5472: }
5473: }
5474: }
5475: }
5476: }
5477: return %privileged;
1.9 www 5478: }
1.1 albertel 5479:
1.103 harris41 5480: # -------------------------------------------------------- Get user privileges
1.11 www 5481:
5482: sub rolesinit {
1.1169 droeschl 5483: my ($domain, $username) = @_;
5484: my %userroles = ('user.login.time' => time);
5485: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
5486:
5487: # firstaccess and timerinterval are related to timed maps/resources.
5488: # also, blocking can be triggered by an activating timer
5489: # it's saved in the user's %env.
5490: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
5491: my %timerinterval = &dump('timerinterval', $domain, $username);
5492: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
5493: %timerintchk, %timerintenv);
5494:
1.1162 raeburn 5495: foreach my $key (keys(%firstaccess)) {
1.1169 droeschl 5496: my ($cid, $rest) = split(/\0/, $key);
1.1162 raeburn 5497: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
5498: }
1.1169 droeschl 5499:
1.1162 raeburn 5500: foreach my $key (keys(%timerinterval)) {
5501: my ($cid,$rest) = split(/\0/,$key);
5502: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
5503: }
1.1169 droeschl 5504:
1.11 www 5505: my %allroles=();
1.1162 raeburn 5506: my %allgroups=();
1.11 www 5507:
1.1274 raeburn 5508: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169 droeschl 5509: my $role = $rolesdump{$area};
5510: $area =~ s/\_\w\w$//;
5511:
5512: my ($trole, $tend, $tstart, $group_privs);
5513:
5514: if ($role =~ /^cr/) {
5515: # Custom role, defined by a user
5516: # e.g., user.role.cr/msu/smith/mynewrole
5517: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
5518: $trole = $1;
5519: ($tend, $tstart) = split('_', $2);
5520: } else {
5521: $trole = $role;
5522: }
5523: } elsif ($role =~ m|^gr/|) {
5524: # Role of member in a group, defined within a course/community
5525: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
5526: ($trole, $tend, $tstart) = split(/_/, $role);
5527: next if $tstart eq '-1';
5528: ($trole, $group_privs) = split(/\//, $trole);
5529: $group_privs = &unescape($group_privs);
5530: } else {
5531: # Just a normal role, defined in roles.tab
5532: ($trole, $tend, $tstart) = split(/_/,$role);
5533: }
5534:
5535: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
5536: $username);
5537: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
5538:
5539: # role expired or not available yet?
5540: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
5541: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
5542:
5543: next if $area eq '' or $trole eq '';
5544:
5545: my $spec = "$trole.$area";
5546: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
5547:
5548: if ($trole =~ /^cr\//) {
5549: # Custom role, defined by a user
5550: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5551: } elsif ($trole eq 'gr') {
5552: # Role of a member in a group, defined within a course/community
5553: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
5554: next;
5555: } else {
5556: # Normal role, defined in roles.tab
5557: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5558: }
5559:
5560: my $cid = $tdomain.'_'.$trest;
5561: unless ($firstaccchk{$cid}) {
5562: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
5563: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
5564: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
5565: $coursetimerstarts{$cid}{$item};
5566: }
5567: }
5568: $firstaccchk{$cid} = 1;
5569: }
5570: unless ($timerintchk{$cid}) {
5571: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
5572: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
5573: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
5574: $coursetimerintervals{$cid}{$item};
1.1162 raeburn 5575: }
1.12 www 5576: }
1.1169 droeschl 5577: $timerintchk{$cid} = 1;
1.191 harris41 5578: }
1.11 www 5579: }
1.1169 droeschl 5580:
5581: @userroles{'user.author', 'user.adv'} = &set_userprivs(\%userroles,
5582: \%allroles, \%allgroups);
5583: $env{'user.adv'} = $userroles{'user.adv'};
5584:
1.1162 raeburn 5585: return (\%userroles,\%firstaccenv,\%timerintenv);
1.11 www 5586: }
5587:
1.567 raeburn 5588: sub set_arearole {
1.1215 raeburn 5589: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
5590: unless ($nolog) {
1.567 raeburn 5591: # log the associated role with the area
1.1215 raeburn 5592: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
5593: }
1.743 albertel 5594: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 5595: }
5596:
5597: sub custom_roleprivs {
5598: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
5599: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250 raeburn 5600: my $homsvr = &homeserver($rauthor,$rdomain);
1.838 albertel 5601: if (&hostname($homsvr) ne '') {
1.567 raeburn 5602: my ($rdummy,$roledef)=
5603: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
5604: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
5605: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
5606: if (defined($syspriv)) {
1.1043 raeburn 5607: if ($trest =~ /^$match_community$/) {
5608: $syspriv =~ s/bre\&S//;
5609: }
1.567 raeburn 5610: $$allroles{'cm./'}.=':'.$syspriv;
5611: $$allroles{$spec.'./'}.=':'.$syspriv;
5612: }
5613: if ($tdomain ne '') {
5614: if (defined($dompriv)) {
5615: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
5616: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
5617: }
5618: if (($trest ne '') && (defined($coursepriv))) {
5619: $$allroles{'cm.'.$area}.=':'.$coursepriv;
5620: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
5621: }
5622: }
5623: }
5624: }
5625: }
5626:
1.678 raeburn 5627: sub group_roleprivs {
5628: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
5629: my $access = 1;
5630: my $now = time;
5631: if (($tend!=0) && ($tend<$now)) { $access = 0; }
5632: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
5633: if ($access) {
1.811 albertel 5634: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 5635: $$allgroups{$course}{$group} .=':'.$group_privs;
5636: }
5637: }
1.567 raeburn 5638:
5639: sub standard_roleprivs {
5640: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
5641: if (defined($pr{$trole.':s'})) {
5642: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
5643: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
5644: }
5645: if ($tdomain ne '') {
5646: if (defined($pr{$trole.':d'})) {
5647: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
5648: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
5649: }
5650: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
5651: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
5652: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
5653: }
5654: }
5655: }
5656:
5657: sub set_userprivs {
1.1064 raeburn 5658: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 5659: my $author=0;
5660: my $adv=0;
1.678 raeburn 5661: my %grouproles = ();
5662: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 5663: my @groupkeys;
1.1000 raeburn 5664: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 5665: push(@groupkeys,$role);
5666: }
5667: if (ref($groups_roles) eq 'HASH') {
5668: foreach my $key (keys(%{$groups_roles})) {
5669: unless (grep(/^\Q$key\E$/,@groupkeys)) {
5670: push(@groupkeys,$key);
5671: }
5672: }
5673: }
5674: if (@groupkeys > 0) {
5675: foreach my $role (@groupkeys) {
5676: my ($trole,$area,$sec,$extendedarea);
5677: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
5678: $trole = $1;
5679: $area = $2;
5680: $sec = $3;
5681: $extendedarea = $area.$sec;
5682: if (exists($$allgroups{$area})) {
5683: foreach my $group (keys(%{$$allgroups{$area}})) {
5684: my $spec = $trole.'.'.$extendedarea;
5685: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 5686: $$allgroups{$area}{$group};
1.1064 raeburn 5687: }
1.678 raeburn 5688: }
5689: }
5690: }
5691: }
5692: }
1.800 albertel 5693: foreach my $group (keys(%grouproles)) {
5694: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 5695: }
1.800 albertel 5696: foreach my $role (keys(%{$allroles})) {
5697: my %thesepriv;
1.941 raeburn 5698: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 5699: foreach my $item (split(/:/,$$allroles{$role})) {
5700: if ($item ne '') {
5701: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 5702: if ($restrictions eq '') {
5703: $thesepriv{$privilege}='F';
5704: } elsif ($thesepriv{$privilege} ne 'F') {
5705: $thesepriv{$privilege}.=$restrictions;
5706: }
5707: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
5708: }
5709: }
5710: my $thesestr='';
1.1104 raeburn 5711: foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 5712: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
5713: }
5714: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 5715: }
5716: return ($author,$adv);
5717: }
5718:
1.994 raeburn 5719: sub role_status {
1.1104 raeburn 5720: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 5721: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250 raeburn 5722: my ($one,$two) = split(m{\./},$rolekey,2);
5723: (undef,undef,$$role) = split(/\./,$one,3);
1.994 raeburn 5724: unless (!defined($$role) || $$role eq '') {
1.1251 raeburn 5725: $$where = '/'.$two;
1.994 raeburn 5726: $$trolecode=$$role.'.'.$$where;
5727: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
5728: $$tstatus='is';
1.1104 raeburn 5729: if ($$tstart && $$tstart>$update) {
1.994 raeburn 5730: $$tstatus='future';
1.1034 raeburn 5731: if ($$tstart<$now) {
5732: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 5733: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 5734: my (%allroles,%allgroups,$group_privs,
5735: %groups_roles,@rolecodes);
1.1002 raeburn 5736: my %userroles = (
5737: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
5738: );
1.1064 raeburn 5739: @rolecodes = ('cm');
1.1002 raeburn 5740: my $spec=$$role.'.'.$$where;
5741: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
5742: if ($$role =~ /^cr\//) {
5743: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 5744: push(@rolecodes,'cr');
1.1002 raeburn 5745: } elsif ($$role eq 'gr') {
1.1064 raeburn 5746: push(@rolecodes,$$role);
1.1002 raeburn 5747: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
5748: $env{'user.name'});
1.1064 raeburn 5749: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 5750: (undef,my $group_privs) = split(/\//,$trole);
5751: $group_privs = &unescape($group_privs);
5752: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 5753: 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 5754: &get_groups_roles($tdomain,$trest,
5755: \%course_roles,\@rolecodes,
5756: \%groups_roles);
1.1002 raeburn 5757: } else {
1.1064 raeburn 5758: push(@rolecodes,$$role);
1.1002 raeburn 5759: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
5760: }
1.1064 raeburn 5761: my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
5762: &appenv(\%userroles,\@rolecodes);
1.1002 raeburn 5763: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
5764: }
5765: }
1.1034 raeburn 5766: $$tstatus = 'is';
1.1002 raeburn 5767: }
1.994 raeburn 5768: }
5769: if ($$tend) {
1.1104 raeburn 5770: if ($$tend<$update) {
1.994 raeburn 5771: $$tstatus='expired';
5772: } elsif ($$tend<$now) {
5773: $$tstatus='will_not';
5774: }
5775: }
5776: }
5777: }
5778: }
5779:
1.1104 raeburn 5780: sub get_groups_roles {
5781: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
5782: return unless((ref($cdom_courseroles) eq 'HASH') &&
5783: (ref($rolecodes) eq 'ARRAY') &&
5784: (ref($groups_roles) eq 'HASH'));
5785: if (keys(%{$cdom_courseroles}) > 0) {
5786: my ($cnum) = ($rest =~ /^($match_courseid)/);
5787: if ($cdom ne '' && $cnum ne '') {
5788: foreach my $key (keys(%{$cdom_courseroles})) {
5789: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
5790: my $crsrole = $1;
5791: my $crssec = $2;
5792: if ($crsrole =~ /^cr/) {
5793: unless (grep(/^cr$/,@{$rolecodes})) {
5794: push(@{$rolecodes},'cr');
5795: }
5796: } else {
5797: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
5798: push(@{$rolecodes},$crsrole);
5799: }
5800: }
5801: my $rolekey = "$crsrole./$cdom/$cnum";
5802: if ($crssec ne '') {
5803: $rolekey .= "/$crssec";
5804: }
5805: $rolekey .= './';
5806: $groups_roles->{$rolekey} = $rolecodes;
5807: }
5808: }
5809: }
5810: }
5811: return;
5812: }
5813:
5814: sub delete_env_groupprivs {
5815: my ($where,$courseroles,$possroles) = @_;
5816: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
5817: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
5818: unless (ref($courseroles->{$udom}) eq 'HASH') {
5819: %{$courseroles->{$udom}} =
5820: &get_my_roles('','','userroles',['active'],
5821: $possroles,[$udom],1);
5822: }
5823: if (ref($courseroles->{$udom}) eq 'HASH') {
5824: foreach my $item (keys(%{$courseroles->{$udom}})) {
5825: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
5826: my $area = '/'.$cdom.'/'.$cnum;
5827: my $privkey = "user.priv.$crsrole.$area";
5828: if ($crssec ne '') {
5829: $privkey .= '/'.$crssec;
5830: }
5831: $privkey .= ".$area/$group";
5832: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
5833: }
5834: }
5835: return;
5836: }
5837:
1.994 raeburn 5838: sub check_adhoc_privs {
1.1329 raeburn 5839: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994 raeburn 5840: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329 raeburn 5841: if ($sec) {
5842: $cckey .= '/'.$sec;
5843: }
1.1185 raeburn 5844: my $setprivs;
1.994 raeburn 5845: if ($env{$cckey}) {
5846: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104 raeburn 5847: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 5848: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329 raeburn 5849: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185 raeburn 5850: $setprivs = 1;
1.994 raeburn 5851: }
5852: } else {
1.1330 raeburn 5853: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185 raeburn 5854: $setprivs = 1;
1.994 raeburn 5855: }
1.1185 raeburn 5856: return $setprivs;
1.994 raeburn 5857: }
5858:
5859: sub set_adhoc_privileges {
1.1326 raeburn 5860: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329 raeburn 5861: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994 raeburn 5862: my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329 raeburn 5863: if ($sec ne '') {
5864: $area .= '/'.$sec;
5865: }
1.994 raeburn 5866: my $spec = $role.'.'.$area;
5867: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215 raeburn 5868: $env{'user.name'},1);
1.1326 raeburn 5869: my %rolehash = ();
5870: if ($role =~ m{^cr/$dcdom/$dcdom\Q-domainconfig\E/}) {
5871: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
5872: } else {
5873: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
5874: }
5875: my ($author,$adv)= &set_userprivs(\%userroles,\%rolehash);
1.994 raeburn 5876: &appenv(\%userroles,[$role,'cm']);
5877: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1088 raeburn 5878: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
5879: &appenv( {'request.role' => $spec,
5880: 'request.role.domain' => $dcdom,
5881: 'request.course.sec' => ''
5882: }
5883: );
5884: my $tadv=0;
5885: if (&allowed('adv') eq 'F') { $tadv=1; }
5886: &appenv({'request.role.adv' => $tadv});
5887: }
1.994 raeburn 5888: }
5889:
1.12 www 5890: # --------------------------------------------------------------- get interface
5891:
5892: sub get {
1.131 albertel 5893: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 5894: my $items='';
1.800 albertel 5895: foreach my $item (@$storearr) {
5896: $items.=&escape($item).'&';
1.191 harris41 5897: }
1.12 www 5898: $items=~s/\&$//;
1.620 albertel 5899: if (!$udomain) { $udomain=$env{'user.domain'}; }
5900: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 5901: my $uhome=&homeserver($uname,$udomain);
5902:
1.133 albertel 5903: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 5904: my @pairs=split(/\&/,$rep);
1.273 albertel 5905: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
5906: return @pairs;
5907: }
1.15 www 5908: my %returnhash=();
1.42 www 5909: my $i=0;
1.800 albertel 5910: foreach my $item (@$storearr) {
5911: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 5912: $i++;
1.191 harris41 5913: }
1.15 www 5914: return %returnhash;
1.27 www 5915: }
5916:
5917: # --------------------------------------------------------------- del interface
5918:
5919: sub del {
1.133 albertel 5920: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 5921: my $items='';
1.800 albertel 5922: foreach my $item (@$storearr) {
5923: $items.=&escape($item).'&';
1.191 harris41 5924: }
1.984 neumanie 5925:
1.27 www 5926: $items=~s/\&$//;
1.620 albertel 5927: if (!$udomain) { $udomain=$env{'user.domain'}; }
5928: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 5929: my $uhome=&homeserver($uname,$udomain);
5930: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 5931: }
5932:
5933: # -------------------------------------------------------------- dump interface
5934:
1.1180 droeschl 5935: sub unserialize {
5936: my ($rep, $escapedkeys) = @_;
5937:
5938: return {} if $rep =~ /^error/;
5939:
5940: my %returnhash=();
1.1252 raeburn 5941: foreach my $item (split(/\&/,$rep)) {
1.1180 droeschl 5942: my ($key, $value) = split(/=/, $item, 2);
5943: $key = unescape($key) unless $escapedkeys;
5944: next if $key =~ /^error: 2 /;
1.1252 raeburn 5945: $returnhash{$key} = &thaw_unescape($value);
1.1180 droeschl 5946: }
5947: #return %returnhash;
5948: return \%returnhash;
5949: }
5950:
5951: # see Lond::dump_with_regexp
5952: # if $escapedkeys hash keys won't get unescaped.
1.15 www 5953: sub dump {
1.1180 droeschl 5954: my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755 albertel 5955: if (!$udomain) { $udomain=$env{'user.domain'}; }
5956: if (!$uname) { $uname=$env{'user.name'}; }
5957: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 5958:
1.1266 raeburn 5959: if ($regexp) {
5960: $regexp=&escape($regexp);
5961: } else {
5962: $regexp='.';
5963: }
1.1180 droeschl 5964: if (grep { $_ eq $uhome } current_machine_ids()) {
5965: # user is hosted on this machine
1.1266 raeburn 5966: my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226 raeburn 5967: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180 droeschl 5968: return %{unserialize($reply, $escapedkeys)};
5969: }
1.1166 raeburn 5970: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755 albertel 5971: my @pairs=split(/\&/,$rep);
5972: my %returnhash=();
1.1098 foxr 5973: if (!($rep =~ /^error/ )) {
5974: foreach my $item (@pairs) {
5975: my ($key,$value)=split(/=/,$item,2);
1.1180 droeschl 5976: $key = unescape($key) unless $escapedkeys;
5977: #$key = &unescape($key);
1.1098 foxr 5978: next if ($key =~ /^error: 2 /);
5979: $returnhash{$key}=&thaw_unescape($value);
5980: }
1.755 albertel 5981: }
5982: return %returnhash;
1.407 www 5983: }
5984:
1.1098 foxr 5985:
1.717 albertel 5986: # --------------------------------------------------------- dumpstore interface
5987:
5988: sub dumpstore {
5989: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180 droeschl 5990: # same as dump but keys must be escaped. They may contain colon separated
5991: # lists of values that may themself contain colons (e.g. symbs).
5992: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 5993: }
5994:
1.407 www 5995: # -------------------------------------------------------------- keys interface
5996:
5997: sub getkeys {
5998: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 5999: if (!$udomain) { $udomain=$env{'user.domain'}; }
6000: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 6001: my $uhome=&homeserver($uname,$udomain);
6002: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
6003: my @keyarray=();
1.800 albertel 6004: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 6005: next if ($key =~ /^error: 2 /);
1.800 albertel 6006: push(@keyarray,&unescape($key));
1.407 www 6007: }
6008: return @keyarray;
1.318 matthew 6009: }
6010:
1.319 matthew 6011: # --------------------------------------------------------------- currentdump
6012: sub currentdump {
1.328 matthew 6013: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 6014: $courseid = $env{'request.course.id'} if (! defined($courseid));
6015: $sdom = $env{'user.domain'} if (! defined($sdom));
6016: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 6017: my $uhome = &homeserver($sname,$sdom);
1.1180 droeschl 6018: my $rep;
6019:
6020: if (grep { $_ eq $uhome } current_machine_ids()) {
6021: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
6022: $courseid)));
6023: } else {
6024: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
6025: }
6026:
1.318 matthew 6027: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 6028: #
1.318 matthew 6029: my %returnhash=();
1.319 matthew 6030: #
6031: if ($rep eq "unknown_cmd") {
6032: # an old lond will not know currentdump
6033: # Do a dump and make it look like a currentdump
1.822 albertel 6034: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 6035: return if ($tmp[0] =~ /^(error:|no_such_host)/);
6036: my %hash = @tmp;
6037: @tmp=();
1.424 matthew 6038: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 6039: } else {
6040: my @pairs=split(/\&/,$rep);
1.800 albertel 6041: foreach my $pair (@pairs) {
6042: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 6043: my ($symb,$param) = split(/:/,$key);
6044: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 6045: &thaw_unescape($value);
1.319 matthew 6046: }
1.191 harris41 6047: }
1.12 www 6048: return %returnhash;
1.424 matthew 6049: }
6050:
6051: sub convert_dump_to_currentdump{
6052: my %hash = %{shift()};
6053: my %returnhash;
6054: # Code ripped from lond, essentially. The only difference
6055: # here is the unescaping done by lonnet::dump(). Conceivably
6056: # we might run in to problems with parameter names =~ /^v\./
6057: while (my ($key,$value) = each(%hash)) {
6058: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 6059: $symb = &unescape($symb);
6060: $param = &unescape($param);
1.424 matthew 6061: next if ($v eq 'version' || $symb eq 'keys');
6062: next if (exists($returnhash{$symb}) &&
6063: exists($returnhash{$symb}->{$param}) &&
6064: $returnhash{$symb}->{'v.'.$param} > $v);
6065: $returnhash{$symb}->{$param}=$value;
6066: $returnhash{$symb}->{'v.'.$param}=$v;
6067: }
6068: #
6069: # Remove all of the keys in the hashes which keep track of
6070: # the version of the parameter.
6071: while (my ($symb,$param_hash) = each(%returnhash)) {
6072: # use a foreach because we are going to delete from the hash.
6073: foreach my $key (keys(%$param_hash)) {
6074: delete($param_hash->{$key}) if ($key =~ /^v\./);
6075: }
6076: }
6077: return \%returnhash;
1.12 www 6078: }
6079:
1.627 albertel 6080: # ------------------------------------------------------ critical inc interface
6081:
6082: sub cinc {
6083: return &inc(@_,'critical');
6084: }
6085:
1.449 matthew 6086: # --------------------------------------------------------------- inc interface
6087:
6088: sub inc {
1.627 albertel 6089: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 6090: if (!$udomain) { $udomain=$env{'user.domain'}; }
6091: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 6092: my $uhome=&homeserver($uname,$udomain);
6093: my $items='';
6094: if (! ref($store)) {
6095: # got a single value, so use that instead
6096: $items = &escape($store).'=&';
6097: } elsif (ref($store) eq 'SCALAR') {
6098: $items = &escape($$store).'=&';
6099: } elsif (ref($store) eq 'ARRAY') {
6100: $items = join('=&',map {&escape($_);} @{$store});
6101: } elsif (ref($store) eq 'HASH') {
6102: while (my($key,$value) = each(%{$store})) {
6103: $items.= &escape($key).'='.&escape($value).'&';
6104: }
6105: }
6106: $items=~s/\&$//;
1.627 albertel 6107: if ($critical) {
6108: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
6109: } else {
6110: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
6111: }
1.449 matthew 6112: }
6113:
1.12 www 6114: # --------------------------------------------------------------- put interface
6115:
6116: sub put {
1.134 albertel 6117: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 6118: if (!$udomain) { $udomain=$env{'user.domain'}; }
6119: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 6120: my $uhome=&homeserver($uname,$udomain);
1.12 www 6121: my $items='';
1.800 albertel 6122: foreach my $item (keys(%$storehash)) {
6123: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 6124: }
1.12 www 6125: $items=~s/\&$//;
1.134 albertel 6126: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 6127: }
6128:
1.631 albertel 6129: # ------------------------------------------------------------ newput interface
6130:
6131: sub newput {
6132: my ($namespace,$storehash,$udomain,$uname)=@_;
6133: if (!$udomain) { $udomain=$env{'user.domain'}; }
6134: if (!$uname) { $uname=$env{'user.name'}; }
6135: my $uhome=&homeserver($uname,$udomain);
6136: my $items='';
6137: foreach my $key (keys(%$storehash)) {
6138: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
6139: }
6140: $items=~s/\&$//;
6141: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
6142: }
6143:
6144: # --------------------------------------------------------- putstore interface
6145:
1.524 raeburn 6146: sub putstore {
1.1269 raeburn 6147: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 6148: if (!$udomain) { $udomain=$env{'user.domain'}; }
6149: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 6150: my $uhome=&homeserver($uname,$udomain);
6151: my $items='';
1.715 albertel 6152: foreach my $key (keys(%$storehash)) {
6153: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 6154: }
1.715 albertel 6155: $items=~s/\&$//;
1.716 albertel 6156: my $esc_symb=&escape($symb);
6157: my $esc_v=&escape($version);
1.715 albertel 6158: my $reply =
1.716 albertel 6159: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 6160: $uhome);
1.1269 raeburn 6161: if (($tolog) && ($reply eq 'ok')) {
6162: my $namevalue='';
6163: foreach my $key (keys(%{$storehash})) {
6164: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
6165: }
6166: $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
6167: '&host='.&escape($perlvar{'lonHostID'}).
6168: '&version='.$esc_v.
6169: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
6170: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
6171: }
1.715 albertel 6172: if ($reply eq 'unknown_cmd') {
1.716 albertel 6173: # gfall back to way things use to be done
1.715 albertel 6174: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
6175: $uname);
1.524 raeburn 6176: }
1.715 albertel 6177: return $reply;
6178: }
6179:
6180: sub old_putstore {
1.716 albertel 6181: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
6182: if (!$udomain) { $udomain=$env{'user.domain'}; }
6183: if (!$uname) { $uname=$env{'user.name'}; }
6184: my $uhome=&homeserver($uname,$udomain);
6185: my %newstorehash;
1.800 albertel 6186: foreach my $item (keys(%$storehash)) {
6187: my $key = $version.':'.&escape($symb).':'.$item;
6188: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 6189: }
6190: my $items='';
6191: my %allitems = ();
1.800 albertel 6192: foreach my $item (keys(%newstorehash)) {
6193: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 6194: my $key = $1.':keys:'.$2;
6195: $allitems{$key} .= $3.':';
6196: }
1.800 albertel 6197: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 6198: }
1.800 albertel 6199: foreach my $item (keys(%allitems)) {
6200: $allitems{$item} =~ s/\:$//;
6201: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 6202: }
6203: $items=~s/\&$//;
6204: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 6205: }
6206:
1.47 www 6207: # ------------------------------------------------------ critical put interface
6208:
6209: sub cput {
1.134 albertel 6210: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 6211: if (!$udomain) { $udomain=$env{'user.domain'}; }
6212: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 6213: my $uhome=&homeserver($uname,$udomain);
1.47 www 6214: my $items='';
1.800 albertel 6215: foreach my $item (keys(%$storehash)) {
6216: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 6217: }
1.47 www 6218: $items=~s/\&$//;
1.134 albertel 6219: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 6220: }
6221:
6222: # -------------------------------------------------------------- eget interface
6223:
6224: sub eget {
1.133 albertel 6225: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 6226: my $items='';
1.800 albertel 6227: foreach my $item (@$storearr) {
6228: $items.=&escape($item).'&';
1.191 harris41 6229: }
1.12 www 6230: $items=~s/\&$//;
1.620 albertel 6231: if (!$udomain) { $udomain=$env{'user.domain'}; }
6232: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 6233: my $uhome=&homeserver($uname,$udomain);
6234: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 6235: my @pairs=split(/\&/,$rep);
6236: my %returnhash=();
1.42 www 6237: my $i=0;
1.800 albertel 6238: foreach my $item (@$storearr) {
6239: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 6240: $i++;
1.191 harris41 6241: }
1.12 www 6242: return %returnhash;
6243: }
6244:
1.667 albertel 6245: # ------------------------------------------------------------ tmpput interface
6246: sub tmpput {
1.802 raeburn 6247: my ($storehash,$server,$context)=@_;
1.667 albertel 6248: my $items='';
1.800 albertel 6249: foreach my $item (keys(%$storehash)) {
6250: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 6251: }
6252: $items=~s/\&$//;
1.802 raeburn 6253: if (defined($context)) {
6254: $items .= ':'.&escape($context);
6255: }
1.667 albertel 6256: return &reply("tmpput:$items",$server);
6257: }
6258:
6259: # ------------------------------------------------------------ tmpget interface
6260: sub tmpget {
1.688 albertel 6261: my ($token,$server)=@_;
6262: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6263: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 6264: my %returnhash;
1.1328 raeburn 6265: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
6266: return %returnhash;
6267: }
1.667 albertel 6268: foreach my $item (split(/\&/,$rep)) {
6269: my ($key,$value)=split(/=/,$item);
6270: $returnhash{&unescape($key)}=&thaw_unescape($value);
6271: }
6272: return %returnhash;
6273: }
6274:
1.1113 raeburn 6275: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 6276: sub tmpdel {
6277: my ($token,$server)=@_;
6278: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6279: return &reply("tmpdel:$token",$server);
6280: }
6281:
1.1198 raeburn 6282: # ------------------------------------------------------------ get_timebased_id
6283:
6284: sub get_timebased_id {
6285: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
6286: $maxtries) = @_;
6287: my ($newid,$error,$dellock);
6288: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
6289: return ('','ok','invalid call to get suffix');
6290: }
6291:
6292: # set defaults for any optional args for which values were not supplied
6293: if ($who eq '') {
6294: $who = $env{'user.name'}.':'.$env{'user.domain'};
6295: }
6296: if (!$locktries) {
6297: $locktries = 3;
6298: }
6299: if (!$maxtries) {
6300: $maxtries = 10;
6301: }
6302:
6303: if (($cdom eq '') || ($cnum eq '')) {
6304: if ($env{'request.course.id'}) {
6305: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
6306: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
6307: }
6308: if (($cdom eq '') || ($cnum eq '')) {
6309: return ('','ok','call to get suffix not in course context');
6310: }
6311: }
6312:
6313: # construct locking item
6314: my $lockhash = {
6315: $prefix."\0".'locked_'.$keyid => $who,
6316: };
6317: my $tries = 0;
6318:
6319: # attempt to get lock on nohist_$namespace file
6320: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6321: while (($gotlock ne 'ok') && $tries <$locktries) {
6322: $tries ++;
6323: sleep 1;
6324: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6325: }
6326:
6327: # attempt to get unique identifier, based on current timestamp
6328: if ($gotlock eq 'ok') {
6329: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
6330: my $id = time;
6331: $newid = $id;
1.1268 raeburn 6332: if ($idtype eq 'addcode') {
6333: $newid .= &sixnum_code();
6334: }
1.1198 raeburn 6335: my $idtries = 0;
6336: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
6337: if ($idtype eq 'concat') {
6338: $newid = $id.$idtries;
1.1268 raeburn 6339: } elsif ($idtype eq 'addcode') {
6340: $newid = $newid.&sixnum_code();
1.1198 raeburn 6341: } else {
6342: $newid ++;
6343: }
6344: $idtries ++;
6345: }
6346: if (!exists($inuse{$prefix."\0".$newid})) {
6347: my %new_item = (
6348: $prefix."\0".$newid => $who,
6349: );
6350: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
6351: $cdom,$cnum);
6352: if ($putresult ne 'ok') {
6353: undef($newid);
6354: $error = 'error saving new item: '.$putresult;
6355: }
6356: } else {
1.1268 raeburn 6357: undef($newid);
1.1198 raeburn 6358: $error = ('error: no unique suffix available for the new item ');
6359: }
6360: # remove lock
6361: my @del_lock = ($prefix."\0".'locked_'.$keyid);
6362: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
6363: } else {
6364: $error = "error: could not obtain lockfile\n";
6365: $dellock = 'ok';
1.1276 raeburn 6366: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
6367: $dellock = 'nolock';
6368: }
1.1198 raeburn 6369: }
6370: return ($newid,$dellock,$error);
6371: }
6372:
1.1268 raeburn 6373: sub sixnum_code {
6374: my $code;
6375: for (0..6) {
6376: $code .= int( rand(9) );
6377: }
6378: return $code;
6379: }
6380:
1.765 albertel 6381: # -------------------------------------------------- portfolio access checking
6382:
6383: sub portfolio_access {
1.1270 raeburn 6384: my ($requrl,$clientip) = @_;
1.765 albertel 6385: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270 raeburn 6386: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 6387: if ($result) {
6388: my %setters;
6389: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6390: my ($startblock,$endblock) =
6391: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
6392: if ($startblock && $endblock) {
6393: return 'B';
6394: }
6395: } else {
6396: my ($startblock,$endblock) =
6397: &Apache::loncommon::blockcheck(\%setters,'port');
6398: if ($startblock && $endblock) {
6399: return 'B';
6400: }
6401: }
6402: }
1.765 albertel 6403: if ($result eq 'ok') {
1.766 albertel 6404: return 'F';
1.765 albertel 6405: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 6406: return 'A';
1.765 albertel 6407: }
1.766 albertel 6408: return '';
1.765 albertel 6409: }
6410:
6411: sub get_portfolio_access {
1.1270 raeburn 6412: my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767 albertel 6413:
6414: if (!ref($access_hash)) {
6415: my $current_perms = &get_portfile_permissions($udom,$unum);
6416: my %access_controls = &get_access_controls($current_perms,$group,
6417: $file_name);
6418: $access_hash = $access_controls{$file_name};
6419: }
6420:
1.1270 raeburn 6421: my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765 albertel 6422: my $now = time;
6423: if (ref($access_hash) eq 'HASH') {
6424: foreach my $key (keys(%{$access_hash})) {
6425: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
6426: if ($start > $now) {
6427: next;
6428: }
6429: if ($end && $end<$now) {
6430: next;
6431: }
6432: if ($scope eq 'public') {
6433: $public = $key;
6434: last;
6435: } elsif ($scope eq 'guest') {
6436: $guest = $key;
6437: } elsif ($scope eq 'domains') {
6438: push(@domains,$key);
6439: } elsif ($scope eq 'users') {
6440: push(@users,$key);
6441: } elsif ($scope eq 'course') {
6442: push(@courses,$key);
6443: } elsif ($scope eq 'group') {
6444: push(@groups,$key);
1.1270 raeburn 6445: } elsif ($scope eq 'ip') {
6446: push(@ips,$key);
1.765 albertel 6447: }
6448: }
6449: if ($public) {
6450: return 'ok';
1.1270 raeburn 6451: } elsif (@ips > 0) {
6452: my $allowed;
6453: foreach my $ipkey (@ips) {
6454: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
6455: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
6456: $allowed = 1;
6457: last;
6458: }
6459: }
6460: }
6461: if ($allowed) {
6462: return 'ok';
6463: }
1.765 albertel 6464: }
6465: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6466: if ($guest) {
6467: return $guest;
6468: }
6469: } else {
6470: if (@domains > 0) {
6471: foreach my $domkey (@domains) {
6472: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
6473: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
6474: return 'ok';
6475: }
6476: }
6477: }
6478: }
6479: if (@users > 0) {
6480: foreach my $userkey (@users) {
1.865 raeburn 6481: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
6482: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
6483: if (ref($item) eq 'HASH') {
6484: if (($item->{'uname'} eq $env{'user.name'}) &&
6485: ($item->{'udom'} eq $env{'user.domain'})) {
6486: return 'ok';
6487: }
6488: }
6489: }
6490: }
1.765 albertel 6491: }
6492: }
6493: my %roleshash;
6494: my @courses_and_groups = @courses;
6495: push(@courses_and_groups,@groups);
6496: if (@courses_and_groups > 0) {
6497: my (%allgroups,%allroles);
6498: my ($start,$end,$role,$sec,$group);
6499: foreach my $envkey (%env) {
1.1060 raeburn 6500: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 6501: my $cid = $2.'_'.$3;
6502: if ($1 eq 'gr') {
6503: $group = $4;
6504: $allgroups{$cid}{$group} = $env{$envkey};
6505: } else {
6506: if ($4 eq '') {
6507: $sec = 'none';
6508: } else {
6509: $sec = $4;
6510: }
6511: $allroles{$cid}{$1}{$sec} = $env{$envkey};
6512: }
1.811 albertel 6513: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 6514: my $cid = $2.'_'.$3;
6515: if ($4 eq '') {
6516: $sec = 'none';
6517: } else {
6518: $sec = $4;
6519: }
6520: $allroles{$cid}{$1}{$sec} = $env{$envkey};
6521: }
6522: }
6523: if (keys(%allroles) == 0) {
6524: return;
6525: }
6526: foreach my $key (@courses_and_groups) {
6527: my %content = %{$$access_hash{$key}};
6528: my $cnum = $content{'number'};
6529: my $cdom = $content{'domain'};
6530: my $cid = $cdom.'_'.$cnum;
6531: if (!exists($allroles{$cid})) {
6532: next;
6533: }
6534: foreach my $role_id (keys(%{$content{'roles'}})) {
6535: my @sections = @{$content{'roles'}{$role_id}{'section'}};
6536: my @groups = @{$content{'roles'}{$role_id}{'group'}};
6537: my @status = @{$content{'roles'}{$role_id}{'access'}};
6538: my @roles = @{$content{'roles'}{$role_id}{'role'}};
6539: foreach my $role (keys(%{$allroles{$cid}})) {
6540: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
6541: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
6542: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
6543: if (grep/^all$/,@sections) {
6544: return 'ok';
6545: } else {
6546: if (grep/^$sec$/,@sections) {
6547: return 'ok';
6548: }
6549: }
6550: }
6551: }
6552: if (keys(%{$allgroups{$cid}}) == 0) {
6553: if (grep/^none$/,@groups) {
6554: return 'ok';
6555: }
6556: } else {
6557: if (grep/^all$/,@groups) {
6558: return 'ok';
6559: }
6560: foreach my $group (keys(%{$allgroups{$cid}})) {
6561: if (grep/^$group$/,@groups) {
6562: return 'ok';
6563: }
6564: }
6565: }
6566: }
6567: }
6568: }
6569: }
6570: }
6571: if ($guest) {
6572: return $guest;
6573: }
6574: }
6575: }
6576: return;
6577: }
6578:
6579: sub course_group_datechecker {
6580: my ($dates,$now,$status) = @_;
6581: my ($start,$end) = split(/\./,$dates);
6582: if (!$start && !$end) {
6583: return 'ok';
6584: }
6585: if (grep/^active$/,@{$status}) {
6586: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
6587: return 'ok';
6588: }
6589: }
6590: if (grep/^previous$/,@{$status}) {
6591: if ($end > $now ) {
6592: return 'ok';
6593: }
6594: }
6595: if (grep/^future$/,@{$status}) {
6596: if ($start > $now) {
6597: return 'ok';
6598: }
6599: }
6600: return;
6601: }
6602:
6603: sub parse_portfolio_url {
6604: my ($url) = @_;
6605:
6606: my ($type,$udom,$unum,$group,$file_name);
6607:
1.823 albertel 6608: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 6609: $type = 1;
6610: $udom = $1;
6611: $unum = $2;
6612: $file_name = $3;
1.823 albertel 6613: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 6614: $type = 2;
6615: $udom = $1;
6616: $unum = $2;
6617: $group = $3;
6618: $file_name = $3.'/'.$4;
6619: }
6620: if (wantarray) {
6621: return ($type,$udom,$unum,$file_name,$group);
6622: }
6623: return $type;
6624: }
6625:
6626: sub is_portfolio_url {
6627: my ($url) = @_;
6628: return scalar(&parse_portfolio_url($url));
6629: }
6630:
1.798 raeburn 6631: sub is_portfolio_file {
6632: my ($file) = @_;
1.820 raeburn 6633: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 6634: return 1;
6635: }
6636: return;
6637: }
6638:
1.976 raeburn 6639: sub usertools_access {
1.1084 raeburn 6640: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 6641: my ($access,%tools);
6642: if ($context eq '') {
6643: $context = 'tools';
6644: }
6645: if ($context eq 'requestcourses') {
6646: %tools = (
6647: official => 1,
6648: unofficial => 1,
1.1006 raeburn 6649: community => 1,
1.1246 raeburn 6650: textbook => 1,
1.1305 raeburn 6651: placement => 1,
1.985 raeburn 6652: );
1.1183 raeburn 6653: } elsif ($context eq 'requestauthor') {
6654: %tools = (
6655: requestauthor => 1,
6656: );
1.985 raeburn 6657: } else {
6658: %tools = (
6659: aboutme => 1,
6660: blog => 1,
1.1177 raeburn 6661: webdav => 1,
1.985 raeburn 6662: portfolio => 1,
6663: );
6664: }
1.976 raeburn 6665: return if (!defined($tools{$tool}));
6666:
1.1242 raeburn 6667: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 6668: $udom = $env{'user.domain'};
6669: $uname = $env{'user.name'};
6670: }
6671:
1.978 raeburn 6672: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
6673: if ($action ne 'reload') {
1.985 raeburn 6674: if ($context eq 'requestcourses') {
6675: return $env{'environment.canrequest.'.$tool};
1.1183 raeburn 6676: } elsif ($context eq 'requestauthor') {
6677: return $env{'environment.canrequest.author'};
1.985 raeburn 6678: } else {
6679: return $env{'environment.availabletools.'.$tool};
6680: }
6681: }
1.976 raeburn 6682: }
6683:
1.1183 raeburn 6684: my ($toolstatus,$inststatus,$envkey);
6685: if ($context eq 'requestauthor') {
6686: $envkey = $context;
6687: } else {
6688: $envkey = $context.'.'.$tool;
6689: }
1.976 raeburn 6690:
1.985 raeburn 6691: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
6692: ($action ne 'reload')) {
1.1183 raeburn 6693: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 6694: $inststatus = $env{'environment.inststatus'};
6695: } else {
1.1084 raeburn 6696: if (ref($userenvref) eq 'HASH') {
1.1183 raeburn 6697: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 6698: $inststatus = $userenvref->{'inststatus'};
6699: } else {
1.1183 raeburn 6700: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
6701: $toolstatus = $userenv{$envkey};
1.1084 raeburn 6702: $inststatus = $userenv{'inststatus'};
6703: }
1.976 raeburn 6704: }
6705:
6706: if ($toolstatus ne '') {
6707: if ($toolstatus) {
6708: $access = 1;
6709: } else {
6710: $access = 0;
6711: }
6712: return $access;
6713: }
6714:
1.1084 raeburn 6715: my ($is_adv,%domdef);
6716: if (ref($is_advref) eq 'HASH') {
6717: $is_adv = $is_advref->{'is_adv'};
6718: } else {
6719: $is_adv = &is_advanced_user($udom,$uname);
6720: }
6721: if (ref($domdefref) eq 'HASH') {
6722: %domdef = %{$domdefref};
6723: } else {
6724: %domdef = &get_domain_defaults($udom);
6725: }
1.976 raeburn 6726: if (ref($domdef{$tool}) eq 'HASH') {
6727: if ($is_adv) {
6728: if ($domdef{$tool}{'_LC_adv'} ne '') {
6729: if ($domdef{$tool}{'_LC_adv'}) {
6730: $access = 1;
6731: } else {
6732: $access = 0;
6733: }
6734: return $access;
6735: }
6736: }
6737: if ($inststatus ne '') {
6738: my ($hasaccess,$hasnoaccess);
6739: foreach my $affiliation (split(/:/,$inststatus)) {
6740: if ($domdef{$tool}{$affiliation} ne '') {
6741: if ($domdef{$tool}{$affiliation}) {
6742: $hasaccess = 1;
6743: } else {
6744: $hasnoaccess = 1;
6745: }
6746: }
6747: }
6748: if ($hasaccess || $hasnoaccess) {
6749: if ($hasaccess) {
6750: $access = 1;
6751: } elsif ($hasnoaccess) {
6752: $access = 0;
6753: }
6754: return $access;
6755: }
6756: } else {
6757: if ($domdef{$tool}{'default'} ne '') {
6758: if ($domdef{$tool}{'default'}) {
6759: $access = 1;
6760: } elsif ($domdef{$tool}{'default'} == 0) {
6761: $access = 0;
6762: }
6763: return $access;
6764: }
6765: }
6766: } else {
1.1177 raeburn 6767: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 6768: $access = 1;
6769: } else {
6770: $access = 0;
6771: }
1.976 raeburn 6772: return $access;
6773: }
6774: }
6775:
1.1050 raeburn 6776: sub is_course_owner {
6777: my ($cdom,$cnum,$udom,$uname) = @_;
6778: if (($udom eq '') || ($uname eq '')) {
6779: $udom = $env{'user.domain'};
6780: $uname = $env{'user.name'};
6781: }
6782: unless (($udom eq '') || ($uname eq '')) {
6783: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
6784: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
6785: return 1;
6786: } else {
6787: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
6788: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
6789: return 1;
6790: }
6791: }
6792: }
6793: }
6794: return;
6795: }
6796:
1.976 raeburn 6797: sub is_advanced_user {
6798: my ($udom,$uname) = @_;
1.1085 raeburn 6799: if ($udom ne '' && $uname ne '') {
6800: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 6801: if (wantarray) {
6802: return ($env{'user.adv'},$env{'user.author'});
6803: } else {
6804: return $env{'user.adv'};
6805: }
1.1085 raeburn 6806: }
6807: }
1.976 raeburn 6808: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
6809: my %allroles;
1.1128 raeburn 6810: my ($is_adv,$is_author);
1.976 raeburn 6811: foreach my $role (keys(%roleshash)) {
6812: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
6813: my $area = '/'.$tdomain.'/'.$trest;
6814: if ($sec ne '') {
6815: $area .= '/'.$sec;
6816: }
6817: if (($area ne '') && ($trole ne '')) {
6818: my $spec=$trole.'.'.$area;
6819: if ($trole =~ /^cr\//) {
6820: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
6821: } elsif ($trole ne 'gr') {
6822: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
6823: }
1.1128 raeburn 6824: if ($trole eq 'au') {
6825: $is_author = 1;
6826: }
1.976 raeburn 6827: }
6828: }
6829: foreach my $role (keys(%allroles)) {
6830: last if ($is_adv);
6831: foreach my $item (split(/:/,$allroles{$role})) {
6832: if ($item ne '') {
6833: my ($privilege,$restrictions)=split(/&/,$item);
6834: if ($privilege eq 'adv') {
6835: $is_adv = 1;
6836: last;
6837: }
6838: }
6839: }
6840: }
1.1128 raeburn 6841: if (wantarray) {
6842: return ($is_adv,$is_author);
6843: }
1.976 raeburn 6844: return $is_adv;
6845: }
1.798 raeburn 6846:
1.1035 raeburn 6847: sub check_can_request {
1.1036 raeburn 6848: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 6849: my $canreq = 0;
6850: my ($types,$typename) = &Apache::loncommon::course_types();
6851: my @options = ('approval','validate','autolimit');
6852: my $optregex = join('|',@options);
6853: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
6854: foreach my $type (@{$types}) {
6855: if (&usertools_access($env{'user.name'},
6856: $env{'user.domain'},
6857: $type,undef,'requestcourses')) {
6858: $canreq ++;
1.1036 raeburn 6859: if (ref($request_domains) eq 'HASH') {
6860: push(@{$request_domains->{$type}},$env{'user.domain'});
6861: }
1.1035 raeburn 6862: if ($dom eq $env{'user.domain'}) {
6863: $can_request->{$type} = 1;
6864: }
6865: }
6866: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
6867: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
6868: if (@curr > 0) {
1.1036 raeburn 6869: foreach my $item (@curr) {
6870: if (ref($request_domains) eq 'HASH') {
6871: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
6872: if ($otherdom ne '') {
6873: if (ref($request_domains->{$type}) eq 'ARRAY') {
6874: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
6875: push(@{$request_domains->{$type}},$otherdom);
6876: }
6877: } else {
6878: push(@{$request_domains->{$type}},$otherdom);
6879: }
6880: }
6881: }
6882: }
6883: unless($dom eq $env{'user.domain'}) {
6884: $canreq ++;
1.1035 raeburn 6885: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
6886: $can_request->{$type} = 1;
6887: }
6888: }
6889: }
6890: }
6891: }
6892: }
6893: return $canreq;
6894: }
6895:
1.341 www 6896: # ---------------------------------------------- Custom access rule evaluation
6897:
6898: sub customaccess {
6899: my ($priv,$uri)=@_;
1.807 albertel 6900: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 6901: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 6902: $udom = &LONCAPA::clean_domain($udom);
6903: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 6904: my $access=0;
1.800 albertel 6905: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 6906: my ($effect,$realm,$role,$type)=split(/\:/,$right);
6907: if ($type eq 'user') {
6908: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 6909: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 6910: if ($tdom) {
6911: if ($tdom ne $env{'user.domain'}) { next; }
6912: }
1.896 albertel 6913: if ($tuname) {
6914: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 6915: }
6916: $access=($effect eq 'allow');
6917: last;
6918: }
6919: } else {
6920: if ($role) {
6921: if ($role ne $urole) { next; }
6922: }
6923: foreach my $scope (split(/\s*\,\s*/,$realm)) {
6924: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
6925: if ($tdom) {
6926: if ($tdom ne $udom) { next; }
6927: }
6928: if ($tcrs) {
6929: if ($tcrs ne $ucrs) { next; }
6930: }
6931: if ($tsec) {
6932: if ($tsec ne $usec) { next; }
6933: }
6934: $access=($effect eq 'allow');
6935: last;
6936: }
6937: if ($realm eq '' && $role eq '') {
6938: $access=($effect eq 'allow');
6939: }
1.402 bowersj2 6940: }
1.341 www 6941: }
6942: return $access;
6943: }
6944:
1.103 harris41 6945: # ------------------------------------------------- Check for a user privilege
1.12 www 6946:
6947: sub allowed {
1.1281 raeburn 6948: my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
1.705 albertel 6949: my $ver_orguri=$uri;
1.439 www 6950: $uri=&deversion($uri);
1.152 www 6951: my $orguri=$uri;
1.52 www 6952: $uri=&declutter($uri);
1.809 raeburn 6953:
1.810 raeburn 6954: if ($priv eq 'evb') {
6955: # Evade communication block restrictions for specified role in a course
6956: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
6957: return $1;
6958: } else {
6959: return;
6960: }
6961: }
6962:
1.620 albertel 6963: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 6964: # Free bre access to adm and meta resources
1.775 albertel 6965: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 6966: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
6967: && ($priv eq 'bre')) {
1.14 www 6968: return 'F';
1.159 www 6969: }
6970:
1.545 banghart 6971: # Free bre access to user's own portfolio contents
1.714 raeburn 6972: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 6973: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 6974: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 6975: my %setters;
6976: my ($startblock,$endblock) =
6977: &Apache::loncommon::blockcheck(\%setters,'port');
6978: if ($startblock && $endblock) {
6979: return 'B';
6980: } else {
6981: return 'F';
6982: }
1.545 banghart 6983: }
6984:
1.762 raeburn 6985: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 6986: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
6987: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
6988: if (exists($env{'request.course.id'})) {
6989: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
6990: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
6991: if (($domain eq $cdom) && ($name eq $cnum)) {
6992: my $courseprivid=$env{'request.course.id'};
6993: $courseprivid=~s/\_/\//;
6994: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
6995: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
6996: return $1;
1.762 raeburn 6997: } else {
6998: if ($env{'request.course.sec'}) {
6999: $courseprivid.='/'.$env{'request.course.sec'};
7000: }
7001: if ($env{'user.priv.'.$env{'request.role'}.'./'.
7002: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
7003: return $2;
7004: }
1.714 raeburn 7005: }
7006: }
7007: }
7008: }
7009:
1.159 www 7010: # Free bre to public access
7011:
7012: if ($priv eq 'bre') {
1.238 www 7013: my $copyright=&metadata($uri,'copyright');
1.620 albertel 7014: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 7015: return 'F';
7016: }
1.238 www 7017: if ($copyright eq 'priv') {
7018: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 7019: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 7020: return '';
7021: }
7022: }
7023: if ($copyright eq 'domain') {
7024: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 7025: unless (($env{'user.domain'} eq $1) ||
7026: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 7027: return '';
7028: }
1.262 matthew 7029: }
1.620 albertel 7030: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 7031: # Library role, so allow browsing of resources in this domain.
7032: return 'F';
1.238 www 7033: }
1.341 www 7034: if ($copyright eq 'custom') {
7035: unless (&customaccess($priv,$uri)) { return ''; }
7036: }
1.14 www 7037: }
1.264 matthew 7038: # Domain coordinator is trying to create a course
1.620 albertel 7039: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 7040: # uri is the requested domain in this case.
7041: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 7042: # a role of dc for the domain in question.
1.620 albertel 7043: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 7044: }
1.29 www 7045:
1.52 www 7046: my $thisallowed='';
7047: my $statecond=0;
7048: my $courseprivid='';
7049:
1.1039 raeburn 7050: my $ownaccess;
1.1043 raeburn 7051: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 7052: if (($priv eq 'bro') && ($env{'user.author'})) {
7053: if ($uri eq '') {
7054: $ownaccess = 1;
7055: } else {
7056: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
7057: my $udom = $env{'user.domain'};
7058: my $uname = $env{'user.name'};
7059: if ($uri =~ m{^\Q$udom\E/?$}) {
7060: $ownaccess = 1;
1.1040 raeburn 7061: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 7062: unless ($uri =~ m{\.\./}) {
7063: $ownaccess = 1;
7064: }
7065: } elsif (($udom ne 'public') && ($uname ne 'public')) {
7066: my $now = time;
7067: if ($uri =~ m{^([^/]+)/?$}) {
7068: my $adom = $1;
7069: foreach my $key (keys(%env)) {
1.1042 raeburn 7070: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 7071: my ($start,$end) = split('.',$env{$key});
7072: if (($now >= $start) && (!$end || $end < $now)) {
7073: $ownaccess = 1;
7074: last;
7075: }
7076: }
7077: }
7078: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
7079: my $adom = $1;
7080: my $aname = $2;
1.1042 raeburn 7081: foreach my $role ('ca','aa') {
7082: if ($env{"user.role.$role./$adom/$aname"}) {
7083: my ($start,$end) =
7084: split('.',$env{"user.role.$role./$adom/$aname"});
7085: if (($now >= $start) && (!$end || $end < $now)) {
7086: $ownaccess = 1;
7087: last;
7088: }
1.1039 raeburn 7089: }
7090: }
7091: }
7092: }
7093: }
7094: }
7095: }
7096:
1.52 www 7097: # Course
7098:
1.620 albertel 7099: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7100: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7101: $thisallowed.=$1;
7102: }
1.52 www 7103: }
1.29 www 7104:
1.52 www 7105: # Domain
7106:
1.620 albertel 7107: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 7108: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7109: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7110: $thisallowed.=$1;
7111: }
1.12 www 7112: }
1.52 www 7113:
1.1141 raeburn 7114: # User who is not author or co-author might still be able to edit
7115: # resource of an author in the domain (e.g., if Domain Coordinator).
7116: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
7117: (&allowed('mdc',$env{'request.course.id'}))) {
7118: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
7119: $thisallowed.=$1;
7120: }
7121: }
7122:
1.52 www 7123: # Course: uri itself is a course
1.66 www 7124: my $courseuri=$uri;
7125: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 7126: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 7127:
1.620 albertel 7128: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 7129: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7130: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7131: $thisallowed.=$1;
7132: }
1.12 www 7133: }
1.29 www 7134:
1.665 albertel 7135: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 7136: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 7137: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 7138: $thisallowed='';
1.671 raeburn 7139: my ($match)=&is_on_map($uri);
7140: if ($match) {
7141: if ($env{'user.priv.'.$env{'request.role'}.'./'}
7142: =~/\Q$priv\E\&([^\:]*)/) {
1.1281 raeburn 7143: my $value = $1;
7144: if ($noblockcheck) {
7145: $thisallowed.=$value;
1.1162 raeburn 7146: } else {
1.1281 raeburn 7147: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7148: if (@blockers > 0) {
7149: $thisallowed = 'B';
7150: } else {
7151: $thisallowed.=$value;
7152: }
1.1162 raeburn 7153: }
1.671 raeburn 7154: }
7155: } else {
1.705 albertel 7156: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 7157: if ($refuri) {
7158: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 7159: $thisallowed='F';
1.671 raeburn 7160: } else {
7161: $refuri=&declutter($refuri);
7162: my ($match) = &is_on_map($refuri);
7163: if ($match) {
1.1281 raeburn 7164: if ($noblockcheck) {
7165: $thisallowed='F';
1.1162 raeburn 7166: } else {
1.1281 raeburn 7167: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7168: if (@blockers > 0) {
7169: $thisallowed = 'B';
7170: } else {
7171: $thisallowed='F';
7172: }
1.1162 raeburn 7173: }
1.671 raeburn 7174: }
1.669 raeburn 7175: }
1.671 raeburn 7176: }
7177: }
1.314 www 7178: }
1.492 albertel 7179:
1.766 albertel 7180: if ($priv eq 'bre'
7181: && $thisallowed ne 'F'
7182: && $thisallowed ne '2'
7183: && &is_portfolio_url($uri)) {
1.1270 raeburn 7184: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 7185: }
1.1250 raeburn 7186:
1.52 www 7187: # Full access at system, domain or course-wide level? Exit.
1.29 www 7188: if ($thisallowed=~/F/) {
7189: return 'F';
7190: }
7191:
1.52 www 7192: # If this is generating or modifying users, exit with special codes
1.29 www 7193:
1.643 www 7194: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
7195: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 7196: my ($audom,$auname)=split('/',$uri);
1.643 www 7197: # no author name given, so this just checks on the general right to make a co-author in this domain
7198: unless ($auname) { return $thisallowed; }
7199: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 7200: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
7201: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
7202: ($audom ne $env{'request.role.domain'}))) { return ''; }
7203: }
1.52 www 7204: return $thisallowed;
7205: }
7206: #
1.103 harris41 7207: # Gathered so far: system, domain and course wide privileges
1.52 www 7208: #
7209: # Course: See if uri or referer is an individual resource that is part of
7210: # the course
7211:
1.620 albertel 7212: if ($env{'request.course.id'}) {
1.232 www 7213:
1.620 albertel 7214: $courseprivid=$env{'request.course.id'};
7215: if ($env{'request.course.sec'}) {
7216: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 7217: }
7218: $courseprivid=~s/\_/\//;
7219: my $checkreferer=1;
1.232 www 7220: my ($match,$cond)=&is_on_map($uri);
7221: if ($match) {
7222: $statecond=$cond;
1.620 albertel 7223: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 7224: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 7225: my $value = $1;
7226: if ($priv eq 'bre') {
1.1281 raeburn 7227: if ($noblockcheck) {
7228: $thisallowed.=$value;
1.1162 raeburn 7229: } else {
1.1281 raeburn 7230: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7231: if (@blockers > 0) {
7232: $thisallowed = 'B';
7233: } else {
7234: $thisallowed.=$value;
7235: }
1.1162 raeburn 7236: }
7237: } else {
7238: $thisallowed.=$value;
7239: }
1.52 www 7240: $checkreferer=0;
7241: }
1.29 www 7242: }
1.83 www 7243:
1.148 www 7244: if ($checkreferer) {
1.620 albertel 7245: my $refuri=$env{'httpref.'.$orguri};
1.148 www 7246: unless ($refuri) {
1.800 albertel 7247: foreach my $key (keys(%env)) {
7248: if ($key=~/^httpref\..*\*/) {
7249: my $pattern=$key;
1.156 www 7250: $pattern=~s/^httpref\.\/res\///;
1.148 www 7251: $pattern=~s/\*/\[\^\/\]\+/g;
7252: $pattern=~s/\//\\\//g;
1.152 www 7253: if ($orguri=~/$pattern/) {
1.800 albertel 7254: $refuri=$env{$key};
1.148 www 7255: }
7256: }
1.191 harris41 7257: }
1.148 www 7258: }
1.232 www 7259:
1.148 www 7260: if ($refuri) {
1.152 www 7261: $refuri=&declutter($refuri);
1.232 www 7262: my ($match,$cond)=&is_on_map($refuri);
7263: if ($match) {
7264: my $refstatecond=$cond;
1.620 albertel 7265: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 7266: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 7267: my $value = $1;
7268: if ($priv eq 'bre') {
1.1281 raeburn 7269: if ($noblockcheck) {
7270: $thisallowed.=$value;
1.1162 raeburn 7271: } else {
1.1281 raeburn 7272: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7273: if (@blockers > 0) {
7274: $thisallowed = 'B';
7275: } else {
7276: $thisallowed.=$value;
7277: }
1.1162 raeburn 7278: }
7279: } else {
7280: $thisallowed.=$value;
7281: }
1.53 www 7282: $uri=$refuri;
7283: $statecond=$refstatecond;
1.52 www 7284: }
7285: }
1.148 www 7286: }
1.29 www 7287: }
1.52 www 7288: }
1.29 www 7289:
1.52 www 7290: #
1.103 harris41 7291: # Gathered now: all privileges that could apply, and condition number
1.52 www 7292: #
7293: #
7294: # Full or no access?
7295: #
1.29 www 7296:
1.52 www 7297: if ($thisallowed=~/F/) {
7298: return 'F';
7299: }
1.29 www 7300:
1.52 www 7301: unless ($thisallowed) {
7302: return '';
7303: }
1.29 www 7304:
1.52 www 7305: # Restrictions exist, deal with them
7306: #
7307: # C:according to course preferences
7308: # R:according to resource settings
7309: # L:unless locked
7310: # X:according to user session state
7311: #
7312:
7313: # Possibly locked functionality, check all courses
1.54 www 7314: # Locks might take effect only after 10 minutes cache expiration for other
7315: # courses, and 2 minutes for current course
1.52 www 7316:
7317: my $envkey;
7318: if ($thisallowed=~/L/) {
1.1000 raeburn 7319: foreach $envkey (keys(%env)) {
1.54 www 7320: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
7321: my $courseid=$2;
7322: my $roleid=$1.'.'.$2;
1.92 www 7323: $courseid=~s/^\///;
1.54 www 7324: my $expiretime=600;
1.620 albertel 7325: if ($env{'request.role'} eq $roleid) {
1.54 www 7326: $expiretime=120;
7327: }
7328: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
7329: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 7330: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 7331: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 7332: }
1.620 albertel 7333: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
7334: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
7335: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
7336: &log($env{'user.domain'},$env{'user.name'},
7337: $env{'user.home'},
1.57 www 7338: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 7339: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 7340: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 7341: return '';
7342: }
7343: }
1.620 albertel 7344: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
7345: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
7346: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
7347: &log($env{'user.domain'},$env{'user.name'},
7348: $env{'user.home'},
1.57 www 7349: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 7350: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 7351: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 7352: return '';
7353: }
7354: }
7355: }
1.29 www 7356: }
1.52 www 7357: }
7358:
7359: #
7360: # Rest of the restrictions depend on selected course
7361: #
7362:
1.620 albertel 7363: unless ($env{'request.course.id'}) {
1.766 albertel 7364: if ($thisallowed eq 'A') {
7365: return 'A';
1.814 raeburn 7366: } elsif ($thisallowed eq 'B') {
7367: return 'B';
1.766 albertel 7368: } else {
7369: return '1';
7370: }
1.52 www 7371: }
1.29 www 7372:
1.52 www 7373: #
7374: # Now user is definitely in a course
7375: #
1.53 www 7376:
7377:
7378: # Course preferences
7379:
7380: if ($thisallowed=~/C/) {
1.620 albertel 7381: my $rolecode=(split(/\./,$env{'request.role'}))[0];
7382: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
7383: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 7384: =~/\Q$rolecode\E/) {
1.1304 raeburn 7385: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689 albertel 7386: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7387: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
7388: $env{'request.course.id'});
7389: }
1.237 www 7390: return '';
7391: }
7392:
1.620 albertel 7393: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 7394: =~/\Q$unamedom\E/) {
1.1304 raeburn 7395: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689 albertel 7396: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
7397: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
7398: $env{'request.course.id'});
7399: }
1.54 www 7400: return '';
7401: }
1.53 www 7402: }
7403:
7404: # Resource preferences
7405:
7406: if ($thisallowed=~/R/) {
1.620 albertel 7407: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 7408: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 7409: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 7410: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7411: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
7412: }
7413: return '';
1.54 www 7414: }
1.53 www 7415: }
1.30 www 7416:
1.246 www 7417: # Restricted by state or randomout?
1.30 www 7418:
1.52 www 7419: if ($thisallowed=~/X/) {
1.620 albertel 7420: if ($env{'acc.randomout'}) {
1.579 albertel 7421: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 7422: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 7423: return '';
7424: }
1.247 www 7425: }
7426: if (&condval($statecond)) {
1.52 www 7427: return '2';
7428: } else {
7429: return '';
7430: }
7431: }
1.30 www 7432:
1.766 albertel 7433: if ($thisallowed eq 'A') {
7434: return 'A';
1.814 raeburn 7435: } elsif ($thisallowed eq 'B') {
7436: return 'B';
1.766 albertel 7437: }
1.52 www 7438: return 'F';
1.232 www 7439: }
1.1162 raeburn 7440:
1.1192 raeburn 7441: # ------------------------------------------- Check construction space access
7442:
7443: sub constructaccess {
7444: my ($url,$setpriv)=@_;
7445:
7446: # We do not allow editing of previous versions of files
7447: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
7448:
7449: # Get username and domain from URL
7450: my ($ownername,$ownerdomain,$ownerhome);
7451:
7452: ($ownerdomain,$ownername) =
1.1325 raeburn 7453: ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192 raeburn 7454:
7455: # The URL does not really point to any authorspace, forget it
7456: unless (($ownername) && ($ownerdomain)) { return ''; }
7457:
7458: # Now we need to see if the user has access to the authorspace of
7459: # $ownername at $ownerdomain
7460:
7461: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
7462: # Real author for this?
7463: $ownerhome = $env{'user.home'};
7464: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
7465: return ($ownername,$ownerdomain,$ownerhome);
7466: }
7467: } else {
7468: # Co-author for this?
7469: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
7470: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
7471: $ownerhome = &homeserver($ownername,$ownerdomain);
7472: return ($ownername,$ownerdomain,$ownerhome);
7473: }
1.1312 raeburn 7474: if ($env{'request.course.id'}) {
7475: if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
7476: ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
7477: if (&allowed('mdc',$env{'request.course.id'})) {
7478: $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
7479: return ($ownername,$ownerdomain,$ownerhome);
7480: }
7481: }
7482: }
1.1192 raeburn 7483: }
7484:
7485: # We don't have any access right now. If we are not possibly going to do anything about this,
7486: # we might as well leave
7487: unless ($setpriv) { return ''; }
7488:
7489: # Backdoor access?
7490: my $allowed=&allowed('eco',$ownerdomain);
7491: # Nope
7492: unless ($allowed) { return ''; }
7493: # Looks like we may have access, but could be locked by the owner of the construction space
7494: if ($allowed eq 'U') {
7495: my %blocked=&get('environment',['domcoord.author'],
7496: $ownerdomain,$ownername);
7497: # Is blocked by owner
7498: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
7499: }
7500: if (($allowed eq 'F') || ($allowed eq 'U')) {
7501: # Grant temporary access
7502: my $then=$env{'user.login.time'};
1.1209 raeburn 7503: my $update=$env{'user.update.time'};
1.1192 raeburn 7504: if (!$update) { $update = $then; }
7505: my $refresh=$env{'user.refresh.time'};
7506: if (!$refresh) { $refresh = $update; }
7507: my $now = time;
7508: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
7509: $now,'ca','constructaccess');
7510: $ownerhome = &homeserver($ownername,$ownerdomain);
7511: return($ownername,$ownerdomain,$ownerhome);
7512: }
7513: # No business here
7514: return '';
7515: }
7516:
1.1282 raeburn 7517: # ----------------------------------------------------------- Content Blocking
7518:
7519: {
7520: # Caches for faster Course Contents display where content blocking
7521: # is in operation (i.e., interval param set) for timed quiz.
7522: #
7523: # User for whom data are being temporarily cached.
7524: my $cacheduser='';
7525: # Cached blockers for this user (a hash of blocking items).
7526: my %cachedblockers=();
7527: # When the data were last cached.
7528: my $cachedlast='';
7529:
7530: sub load_all_blockers {
7531: my ($uname,$udom,$blocks)=@_;
7532: if (($uname ne '') && ($udom ne '')) {
7533: if (($cacheduser eq $uname.':'.$udom) &&
7534: (abs($cachedlast-time)<5)) {
7535: return;
7536: }
7537: }
7538: $cachedlast=time;
7539: $cacheduser=$uname.':'.$udom;
7540: %cachedblockers = &get_commblock_resources($blocks);
7541: }
7542:
1.1162 raeburn 7543: sub get_comm_blocks {
7544: my ($cdom,$cnum) = @_;
7545: if ($cdom eq '' || $cnum eq '') {
7546: return unless ($env{'request.course.id'});
7547: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7548: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7549: }
7550: my %commblocks;
7551: my $hashid=$cdom.'_'.$cnum;
7552: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
7553: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
7554: %commblocks = %{$blocksref};
7555: } else {
7556: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
7557: my $cachetime = 600;
7558: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
7559: }
7560: return %commblocks;
7561: }
7562:
1.1282 raeburn 7563: sub get_commblock_resources {
7564: my ($blocks) = @_;
7565: my %blockers = ();
7566: return %blockers unless ($env{'request.course.id'});
7567: return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
1.1162 raeburn 7568: my %commblocks;
7569: if (ref($blocks) eq 'HASH') {
7570: %commblocks = %{$blocks};
7571: } else {
7572: %commblocks = &get_comm_blocks();
7573: }
1.1282 raeburn 7574: return %blockers unless (keys(%commblocks) > 0);
7575: my $navmap = Apache::lonnavmaps::navmap->new();
7576: return %blockers unless (ref($navmap));
1.1162 raeburn 7577: my $now = time;
7578: foreach my $block (keys(%commblocks)) {
7579: if ($block =~ /^(\d+)____(\d+)$/) {
7580: my ($start,$end) = ($1,$2);
7581: if ($start <= $now && $end >= $now) {
7582: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
7583: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
7584: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282 raeburn 7585: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
7586: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 7587: }
7588: }
7589: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282 raeburn 7590: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
7591: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 7592: }
7593: }
7594: }
7595: }
7596: }
7597: } elsif ($block =~ /^firstaccess____(.+)$/) {
7598: my $item = $1;
1.1163 raeburn 7599: my @to_test;
1.1162 raeburn 7600: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
7601: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1282 raeburn 7602: my @interval;
7603: my $type = 'map';
7604: if ($item eq 'course') {
7605: $type = 'course';
7606: @interval=&EXT("resource.0.interval");
7607: } else {
7608: if ($item =~ /___\d+___/) {
7609: $type = 'resource';
7610: @interval=&EXT("resource.0.interval",$item);
7611: if (ref($navmap)) {
7612: my $res = $navmap->getBySymb($item);
7613: push(@to_test,$res);
7614: }
1.1162 raeburn 7615: } else {
1.1282 raeburn 7616: my $mapsymb = &symbread($item,1);
7617: if ($mapsymb) {
7618: if (ref($navmap)) {
7619: my $mapres = $navmap->getBySymb($mapsymb);
7620: @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
7621: foreach my $res (@to_test) {
7622: my $symb = $res->symb();
7623: next if ($symb eq $mapsymb);
7624: if ($symb ne '') {
7625: @interval=&EXT("resource.0.interval",$symb);
7626: if ($interval[1] eq 'map') {
7627: last;
1.1162 raeburn 7628: }
7629: }
7630: }
7631: }
7632: }
7633: }
1.1282 raeburn 7634: }
1.1310 raeburn 7635: if ($interval[0] =~ /^(\d+)/) {
1.1308 raeburn 7636: my $timelimit = $1;
1.1282 raeburn 7637: my $first_access;
7638: if ($type eq 'resource') {
7639: $first_access=&get_first_access($interval[1],$item);
7640: } elsif ($type eq 'map') {
7641: $first_access=&get_first_access($interval[1],undef,$item);
7642: } else {
7643: $first_access=&get_first_access($interval[1]);
7644: }
7645: if ($first_access) {
1.1292 raeburn 7646: my $timesup = $first_access+$timelimit;
1.1282 raeburn 7647: if ($timesup > $now) {
7648: my $activeblock;
7649: foreach my $res (@to_test) {
1.1283 raeburn 7650: if ($res->answerable()) {
1.1282 raeburn 7651: $activeblock = 1;
7652: last;
7653: }
7654: }
7655: if ($activeblock) {
7656: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
7657: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
7658: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
7659: }
7660: }
7661: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
7662: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
7663: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 7664: }
1.1162 raeburn 7665: }
7666: }
7667: }
7668: }
7669: }
7670: }
7671: }
7672: }
7673: }
1.1282 raeburn 7674: return %blockers;
1.1162 raeburn 7675: }
7676:
1.1282 raeburn 7677: sub has_comm_blocking {
7678: my ($priv,$symb,$uri,$blocks) = @_;
7679: my @blockers;
7680: return unless ($env{'request.course.id'});
7681: return unless ($priv eq 'bre');
7682: return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
7683: return if ($env{'request.state'} eq 'construct');
7684: &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
7685: return unless (keys(%cachedblockers) > 0);
7686: my (%possibles,@symbs);
7687: if (!$symb) {
7688: $symb = &symbread($uri,1,1,1,\%possibles);
1.1162 raeburn 7689: }
1.1282 raeburn 7690: if ($symb) {
7691: @symbs = ($symb);
7692: } elsif (keys(%possibles)) {
7693: @symbs = keys(%possibles);
7694: }
7695: my $noblock;
7696: foreach my $symb (@symbs) {
7697: last if ($noblock);
7698: my ($map,$resid,$resurl)=&decode_symb($symb);
7699: foreach my $block (keys(%cachedblockers)) {
7700: if ($block =~ /^firstaccess____(.+)$/) {
7701: my $item = $1;
7702: if (($item eq $map) || ($item eq $symb)) {
7703: $noblock = 1;
7704: last;
7705: }
7706: }
7707: if (ref($cachedblockers{$block}) eq 'HASH') {
7708: if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
7709: if ($cachedblockers{$block}{'resources'}{$symb}) {
7710: unless (grep(/^\Q$block\E$/,@blockers)) {
7711: push(@blockers,$block);
7712: }
7713: }
7714: }
1.1162 raeburn 7715: }
1.1282 raeburn 7716: if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
7717: if ($cachedblockers{$block}{'maps'}{$map}) {
7718: unless (grep(/^\Q$block\E$/,@blockers)) {
7719: push(@blockers,$block);
7720: }
7721: }
1.1162 raeburn 7722: }
7723: }
7724: }
1.1282 raeburn 7725: return if ($noblock);
7726: return @blockers;
7727: }
1.1162 raeburn 7728: }
7729:
1.1282 raeburn 7730: # -------------------------------- Deversion and split uri into path an filename
7731:
1.1133 foxr 7732: #
1.1282 raeburn 7733: # Removes the version from a URI and
1.1133 foxr 7734: # splits it in to its filename and path to the filename.
7735: # Seems like File::Basename could have done this more clearly.
7736: # Parameters:
7737: # $uri - input URI
7738: # Returns:
7739: # Two element list consisting of
7740: # $pathname - the URI up to and excluding the trailing /
7741: # $filename - The part of the URI following the last /
7742: # NOTE:
7743: # Another realization of this is simply:
7744: # use File::Basename;
7745: # ...
7746: # $uri = shift;
7747: # $filename = basename($uri);
7748: # $path = dirname($uri);
7749: # return ($filename, $path);
7750: #
7751: # The implementation below is probably faster however.
7752: #
1.710 albertel 7753: sub split_uri_for_cond {
7754: my $uri=&deversion(&declutter(shift));
7755: my @uriparts=split(/\//,$uri);
7756: my $filename=pop(@uriparts);
7757: my $pathname=join('/',@uriparts);
7758: return ($pathname,$filename);
7759: }
1.232 www 7760: # --------------------------------------------------- Is a resource on the map?
7761:
7762: sub is_on_map {
1.710 albertel 7763: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 7764: #Trying to find the conditional for the file
1.620 albertel 7765: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 7766: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 7767: if ($match) {
1.289 bowersj2 7768: return (1,$1);
7769: } else {
1.434 www 7770: return (0,0);
1.289 bowersj2 7771: }
1.12 www 7772: }
7773:
1.427 www 7774: # --------------------------------------------------------- Get symb from alias
7775:
7776: sub get_symb_from_alias {
7777: my $symb=shift;
7778: my ($map,$resid,$url)=&decode_symb($symb);
7779: # Already is a symb
7780: if ($url) { return $symb; }
7781: # Must be an alias
7782: my $aliassymb='';
7783: my %bighash;
1.620 albertel 7784: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 7785: &GDBM_READER(),0640)) {
7786: my $rid=$bighash{'mapalias_'.$symb};
7787: if ($rid) {
7788: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 7789: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
7790: $resid,$bighash{'src_'.$rid});
1.427 www 7791: }
7792: untie %bighash;
7793: }
7794: return $aliassymb;
7795: }
7796:
1.12 www 7797: # ----------------------------------------------------------------- Define Role
7798:
7799: sub definerole {
7800: if (allowed('mcr','/')) {
1.1326 raeburn 7801: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 7802: foreach my $role (split(':',$sysrole)) {
7803: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 7804: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
7805: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
7806: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 7807: return "refused:s:$crole&$cqual";
7808: }
7809: }
1.191 harris41 7810: }
1.800 albertel 7811: foreach my $role (split(':',$domrole)) {
7812: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 7813: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
7814: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
7815: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 7816: return "refused:d:$crole&$cqual";
7817: }
7818: }
1.191 harris41 7819: }
1.800 albertel 7820: foreach my $role (split(':',$courole)) {
7821: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 7822: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
7823: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
7824: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 7825: return "refused:c:$crole&$cqual";
7826: }
7827: }
1.191 harris41 7828: }
1.1326 raeburn 7829: my $uhome;
7830: if (($uname ne '') && ($udom ne '')) {
7831: $uhome = &homeserver($uname,$udom);
7832: return $uhome if ($uhome eq 'no_host');
7833: } else {
7834: $uname = $env{'user.name'};
7835: $udom = $env{'user.domain'};
7836: $uhome = $env{'user.home'};
7837: }
1.620 albertel 7838: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326 raeburn 7839: "$udom:$uname:rolesdef_$rolename=".
1.21 www 7840: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326 raeburn 7841: return reply($command,$uhome);
1.12 www 7842: } else {
7843: return 'refused';
7844: }
1.105 harris41 7845: }
7846:
7847: # ---------------- Make a metadata query against the network of library servers
7848:
7849: sub metadata_query {
1.1237 raeburn 7850: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 7851: my %rhash;
1.845 albertel 7852: my %libserv = &all_library();
1.244 matthew 7853: my @server_list = (defined($server_array) ? @$server_array
7854: : keys(%libserv) );
7855: for my $server (@server_list) {
1.1237 raeburn 7856: my $domains = '';
7857: if (ref($domains_hash) eq 'HASH') {
7858: $domains = $domains_hash->{$server};
7859: }
1.118 harris41 7860: unless ($custom or $customshow) {
1.1237 raeburn 7861: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 7862: $rhash{$server}=$reply;
7863: }
7864: else {
7865: my $reply=&reply("querysend:".&escape($query).':'.
1.1237 raeburn 7866: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 7867: $server);
7868: $rhash{$server}=$reply;
7869: }
1.112 harris41 7870: }
1.118 harris41 7871: return \%rhash;
1.240 www 7872: }
7873:
7874: # ----------------------------------------- Send log queries and wait for reply
7875:
7876: sub log_query {
7877: my ($uname,$udom,$query,%filters)=@_;
7878: my $uhome=&homeserver($uname,$udom);
7879: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 7880: my $uhost=&hostname($uhome);
1.800 albertel 7881: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 7882: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
7883: $uhome);
1.479 albertel 7884: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 7885: return get_query_reply($queryid);
7886: }
7887:
1.818 raeburn 7888: # -------------------------- Update MySQL table for portfolio file
7889:
7890: sub update_portfolio_table {
1.821 raeburn 7891: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 7892: if ($group ne '') {
7893: $file_name =~s /^\Q$group\E//;
7894: }
1.818 raeburn 7895: my $homeserver = &homeserver($uname,$udom);
7896: my $queryid=
1.821 raeburn 7897: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
7898: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 7899: my $reply = &get_query_reply($queryid);
7900: return $reply;
7901: }
7902:
1.899 raeburn 7903: # -------------------------- Update MySQL allusers table
7904:
7905: sub update_allusers_table {
7906: my ($uname,$udom,$names) = @_;
7907: my $homeserver = &homeserver($uname,$udom);
7908: my $queryid=
7909: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
7910: 'lastname='.&escape($names->{'lastname'}).'%%'.
7911: 'firstname='.&escape($names->{'firstname'}).'%%'.
7912: 'middlename='.&escape($names->{'middlename'}).'%%'.
7913: 'generation='.&escape($names->{'generation'}).'%%'.
7914: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
7915: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 7916: return;
1.899 raeburn 7917: }
7918:
1.508 raeburn 7919: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 7920:
7921: sub fetch_enrollment_query {
1.511 raeburn 7922: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317 raeburn 7923: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 7924: my $maxtries = 1;
1.508 raeburn 7925: if ($context eq 'automated') {
7926: $homeserver = $perlvar{'lonHostID'};
1.1317 raeburn 7927: $sleep = 2;
7928: $loopmax = 100;
1.547 raeburn 7929: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 7930: } else {
7931: $homeserver = &homeserver($cnum,$dom);
7932: }
1.838 albertel 7933: my $host=&hostname($homeserver);
1.506 raeburn 7934: my $cmd = '';
1.1000 raeburn 7935: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 7936: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 7937: }
7938: $cmd =~ s/%%$//;
7939: $cmd = &escape($cmd);
7940: my $query = 'fetchenrollment';
1.620 albertel 7941: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 7942: unless ($queryid=~/^\Q$host\E\_/) {
7943: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
7944: return 'error: '.$queryid;
7945: }
1.1317 raeburn 7946: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 7947: my $tries = 1;
7948: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317 raeburn 7949: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 7950: $tries ++;
7951: }
1.526 raeburn 7952: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 7953: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 7954: } else {
1.901 albertel 7955: my @responses = split(/:/,$reply);
1.1322 raeburn 7956: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 7957: foreach my $line (@responses) {
7958: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 7959: $$replyref{$key} = $value;
7960: }
7961: } else {
1.1117 foxr 7962: my $pathname = LONCAPA::tempdir();
1.800 albertel 7963: foreach my $line (@responses) {
7964: my ($key,$value) = split(/=/,$line);
1.506 raeburn 7965: $$replyref{$key} = $value;
7966: if ($value > 0) {
1.800 albertel 7967: foreach my $item (@{$$affiliatesref{$key}}) {
7968: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 7969: my $destname = $pathname.'/'.$filename;
7970: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 7971: if ($xml_classlist =~ /^error/) {
7972: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
7973: } else {
1.506 raeburn 7974: if ( open(FILE,">$destname") ) {
7975: print FILE &unescape($xml_classlist);
7976: close(FILE);
1.526 raeburn 7977: } else {
7978: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 7979: }
7980: }
7981: }
7982: }
7983: }
7984: }
7985: return 'ok';
7986: }
7987: return 'error';
7988: }
7989:
1.242 www 7990: sub get_query_reply {
1.1317 raeburn 7991: my ($queryid,$sleep,$loopmax) = @_;;
7992: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
7993: $sleep = 0.2;
7994: }
7995: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
7996: $loopmax = 100;
7997: }
1.1117 foxr 7998: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 7999: my $reply='';
1.1317 raeburn 8000: for (1..$loopmax) {
8001: sleep($sleep);
1.240 www 8002: if (-e $replyfile.'.end') {
1.448 albertel 8003: if (open(my $fh,$replyfile)) {
1.904 albertel 8004: $reply = join('',<$fh>);
8005: close($fh);
1.240 www 8006: } else { return 'error: reply_file_error'; }
1.242 www 8007: return &unescape($reply);
8008: }
1.240 www 8009: }
1.242 www 8010: return 'timeout:'.$queryid;
1.240 www 8011: }
8012:
8013: sub courselog_query {
1.241 www 8014: #
8015: # possible filters:
8016: # url: url or symb
8017: # username
8018: # domain
8019: # action: view, submit, grade
8020: # start: timestamp
8021: # end: timestamp
8022: #
1.240 www 8023: my (%filters)=@_;
1.620 albertel 8024: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 8025: if ($filters{'url'}) {
8026: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
8027: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
8028: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
8029: }
1.620 albertel 8030: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
8031: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 8032: return &log_query($cname,$cdom,'courselog',%filters);
8033: }
8034:
8035: sub userlog_query {
1.858 raeburn 8036: #
8037: # possible filters:
8038: # action: log check role
8039: # start: timestamp
8040: # end: timestamp
8041: #
1.240 www 8042: my ($uname,$udom,%filters)=@_;
8043: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 8044: }
8045:
1.506 raeburn 8046: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
8047:
8048: sub auto_run {
1.508 raeburn 8049: my ($cnum,$cdom) = @_;
1.876 raeburn 8050: my $response = 0;
8051: my $settings;
8052: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
8053: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
8054: $settings = $domconfig{'autoenroll'};
8055: if ($settings->{'run'} eq '1') {
8056: $response = 1;
8057: }
8058: } else {
1.934 raeburn 8059: my $homeserver;
8060: if (&is_course($cdom,$cnum)) {
8061: $homeserver = &homeserver($cnum,$cdom);
8062: } else {
8063: $homeserver = &domain($cdom,'primary');
8064: }
8065: if ($homeserver ne 'no_host') {
8066: $response = &reply('autorun:'.$cdom,$homeserver);
8067: }
1.876 raeburn 8068: }
1.506 raeburn 8069: return $response;
8070: }
1.776 albertel 8071:
1.506 raeburn 8072: sub auto_get_sections {
1.508 raeburn 8073: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 8074: my $homeserver;
8075: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8076: $homeserver = &homeserver($cnum,$cdom);
8077: }
8078: if (!defined($homeserver)) {
8079: if ($cdom =~ /^$match_domain$/) {
8080: $homeserver = &domain($cdom,'primary');
8081: }
8082: }
8083: my @secs;
8084: if (defined($homeserver)) {
8085: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
8086: unless ($response eq 'refused') {
8087: @secs = split(/:/,$response);
8088: }
1.506 raeburn 8089: }
8090: return @secs;
8091: }
1.776 albertel 8092:
1.506 raeburn 8093: sub auto_new_course {
1.1099 raeburn 8094: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 8095: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 8096: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 8097: return $response;
8098: }
1.776 albertel 8099:
1.506 raeburn 8100: sub auto_validate_courseID {
1.508 raeburn 8101: my ($cnum,$cdom,$inst_course_id) = @_;
8102: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 8103: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 8104: return $response;
8105: }
1.776 albertel 8106:
1.1007 raeburn 8107: sub auto_validate_instcode {
1.1020 raeburn 8108: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 8109: my ($homeserver,$response);
8110: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8111: $homeserver = &homeserver($cnum,$cdom);
8112: }
8113: if (!defined($homeserver)) {
8114: if ($cdom =~ /^$match_domain$/) {
8115: $homeserver = &domain($cdom,'primary');
8116: }
8117: }
1.1065 raeburn 8118: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
8119: &escape($instcode).':'.&escape($owner),$homeserver));
1.1216 raeburn 8120: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
8121: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 8122: }
8123:
1.506 raeburn 8124: sub auto_create_password {
1.873 raeburn 8125: my ($cnum,$cdom,$authparam,$udom) = @_;
8126: my ($homeserver,$response);
1.506 raeburn 8127: my $create_passwd = 0;
8128: my $authchk = '';
1.873 raeburn 8129: if ($udom =~ /^$match_domain$/) {
8130: $homeserver = &domain($udom,'primary');
8131: }
8132: if ($homeserver eq '') {
8133: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8134: $homeserver = &homeserver($cnum,$cdom);
8135: }
8136: }
8137: if ($homeserver eq '') {
8138: $authchk = 'nodomain';
1.506 raeburn 8139: } else {
1.873 raeburn 8140: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
8141: if ($response eq 'refused') {
8142: $authchk = 'refused';
8143: } else {
1.901 albertel 8144: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 8145: }
1.506 raeburn 8146: }
8147: return ($authparam,$create_passwd,$authchk);
8148: }
8149:
1.706 raeburn 8150: sub auto_photo_permission {
8151: my ($cnum,$cdom,$students) = @_;
8152: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 8153: my ($outcome,$perm_reqd,$conditions) =
8154: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 8155: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8156: return (undef,undef);
8157: }
1.706 raeburn 8158: return ($outcome,$perm_reqd,$conditions);
8159: }
8160:
8161: sub auto_checkphotos {
8162: my ($uname,$udom,$pid) = @_;
8163: my $homeserver = &homeserver($uname,$udom);
8164: my ($result,$resulttype);
8165: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 8166: &escape($uname).':'.&escape($pid),
8167: $homeserver));
1.709 albertel 8168: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8169: return (undef,undef);
8170: }
1.706 raeburn 8171: if ($outcome) {
8172: ($result,$resulttype) = split(/:/,$outcome);
8173: }
8174: return ($result,$resulttype);
8175: }
8176:
8177: sub auto_photochoice {
8178: my ($cnum,$cdom) = @_;
8179: my $homeserver = &homeserver($cnum,$cdom);
8180: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 8181: &escape($cdom),
8182: $homeserver)));
1.709 albertel 8183: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8184: return (undef,undef);
8185: }
1.706 raeburn 8186: return ($update,$comment);
8187: }
8188:
8189: sub auto_photoupdate {
8190: my ($affiliatesref,$dom,$cnum,$photo) = @_;
8191: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 8192: my $host=&hostname($homeserver);
1.706 raeburn 8193: my $cmd = '';
8194: my $maxtries = 1;
1.800 albertel 8195: foreach my $affiliate (keys(%{$affiliatesref})) {
8196: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 8197: }
8198: $cmd =~ s/%%$//;
8199: $cmd = &escape($cmd);
8200: my $query = 'institutionalphotos';
8201: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
8202: unless ($queryid=~/^\Q$host\E\_/) {
8203: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
8204: return 'error: '.$queryid;
8205: }
8206: my $reply = &get_query_reply($queryid);
8207: my $tries = 1;
8208: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
8209: $reply = &get_query_reply($queryid);
8210: $tries ++;
8211: }
8212: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
8213: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
8214: } else {
8215: my @responses = split(/:/,$reply);
8216: my $outcome = shift(@responses);
8217: foreach my $item (@responses) {
8218: my ($key,$value) = split(/=/,$item);
8219: $$photo{$key} = $value;
8220: }
8221: return $outcome;
8222: }
8223: return 'error';
8224: }
8225:
1.521 raeburn 8226: sub auto_instcode_format {
1.793 albertel 8227: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
8228: $cat_order) = @_;
1.521 raeburn 8229: my $courses = '';
1.772 raeburn 8230: my @homeservers;
1.521 raeburn 8231: if ($caller eq 'global') {
1.841 albertel 8232: my %servers = &get_servers($codedom,'library');
8233: foreach my $tryserver (keys(%servers)) {
8234: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8235: push(@homeservers,$tryserver);
8236: }
1.584 raeburn 8237: }
1.1022 raeburn 8238: } elsif ($caller eq 'requests') {
8239: if ($codedom =~ /^$match_domain$/) {
8240: my $chome = &domain($codedom,'primary');
8241: unless ($chome eq 'no_host') {
8242: push(@homeservers,$chome);
8243: }
8244: }
1.521 raeburn 8245: } else {
1.772 raeburn 8246: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 8247: }
1.793 albertel 8248: foreach my $code (keys(%{$instcodes})) {
8249: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 8250: }
8251: chop($courses);
1.772 raeburn 8252: my $ok_response = 0;
8253: my $response;
8254: while (@homeservers > 0 && $ok_response == 0) {
8255: my $server = shift(@homeservers);
8256: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
8257: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
8258: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 8259: split(/:/,$response);
1.772 raeburn 8260: %{$codes} = (%{$codes},&str2hash($codes_str));
8261: push(@{$codetitles},&str2array($codetitles_str));
8262: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
8263: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
8264: $ok_response = 1;
8265: }
8266: }
8267: if ($ok_response) {
1.521 raeburn 8268: return 'ok';
1.772 raeburn 8269: } else {
8270: return $response;
1.521 raeburn 8271: }
8272: }
8273:
1.792 raeburn 8274: sub auto_instcode_defaults {
8275: my ($domain,$returnhash,$code_order) = @_;
8276: my @homeservers;
1.841 albertel 8277:
8278: my %servers = &get_servers($domain,'library');
8279: foreach my $tryserver (keys(%servers)) {
8280: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8281: push(@homeservers,$tryserver);
8282: }
1.792 raeburn 8283: }
1.841 albertel 8284:
1.792 raeburn 8285: my $response;
1.841 albertel 8286: foreach my $server (@homeservers) {
1.792 raeburn 8287: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 8288: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
8289:
8290: foreach my $pair (split(/\&/,$response)) {
8291: my ($name,$value)=split(/\=/,$pair);
8292: if ($name eq 'code_order') {
8293: @{$code_order} = split(/\&/,&unescape($value));
8294: } else {
8295: $returnhash->{&unescape($name)}=&unescape($value);
8296: }
8297: }
8298: return 'ok';
1.792 raeburn 8299: }
1.841 albertel 8300:
8301: return $response;
1.1003 raeburn 8302: }
8303:
8304: sub auto_possible_instcodes {
1.1007 raeburn 8305: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
8306: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
8307: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8308: return;
8309: }
1.1003 raeburn 8310: my (@homeservers,$uhome);
8311: if (defined(&domain($domain,'primary'))) {
8312: $uhome=&domain($domain,'primary');
8313: push(@homeservers,&domain($domain,'primary'));
8314: } else {
8315: my %servers = &get_servers($domain,'library');
8316: foreach my $tryserver (keys(%servers)) {
8317: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8318: push(@homeservers,$tryserver);
8319: }
8320: }
8321: }
8322: my $response;
8323: foreach my $server (@homeservers) {
8324: $response=&reply('autopossibleinstcodes:'.$domain,$server);
8325: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 8326: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
8327: split(':',$response);
8328: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
8329: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 8330: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 8331: my ($name,$value)=split('=',$item);
8332: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 8333: }
8334: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 8335: my ($name,$value)=split('=',$item);
8336: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 8337: }
8338: return 'ok';
8339: }
8340: return $response;
8341: }
1.792 raeburn 8342:
1.1010 raeburn 8343: sub auto_courserequest_checks {
8344: my ($dom) = @_;
1.1020 raeburn 8345: my ($homeserver,%validations);
8346: if ($dom =~ /^$match_domain$/) {
8347: $homeserver = &domain($dom,'primary');
8348: }
8349: unless ($homeserver eq 'no_host') {
8350: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
8351: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8352: my @items = split(/&/,$response);
8353: foreach my $item (@items) {
8354: my ($key,$value) = split('=',$item);
8355: $validations{&unescape($key)} = &thaw_unescape($value);
8356: }
8357: }
8358: }
1.1010 raeburn 8359: return %validations;
8360: }
8361:
1.1020 raeburn 8362: sub auto_courserequest_validation {
1.1255 raeburn 8363: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 8364: my ($homeserver,$response);
8365: if ($dom =~ /^$match_domain$/) {
8366: $homeserver = &domain($dom,'primary');
8367: }
1.1255 raeburn 8368: unless ($homeserver eq 'no_host') {
8369: my $customdata;
8370: if (ref($custominfo) eq 'HASH') {
8371: $customdata = &freeze_escape($custominfo);
8372: }
1.1020 raeburn 8373: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 8374: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255 raeburn 8375: ':'.&escape($instcode).':'.&escape($instseclist).':'.
8376: $customdata,$homeserver));
1.1020 raeburn 8377: }
8378: return $response;
8379: }
8380:
1.777 albertel 8381: sub auto_validate_class_sec {
1.918 raeburn 8382: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 8383: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 8384: my $ownerlist;
8385: if (ref($owners) eq 'ARRAY') {
8386: $ownerlist = join(',',@{$owners});
8387: } else {
8388: $ownerlist = $owners;
8389: }
1.773 raeburn 8390: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 8391: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 8392: return $response;
8393: }
8394:
1.1248 raeburn 8395: sub auto_crsreq_update {
8396: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257 raeburn 8397: $code,$accessstart,$accessend,$inbound) = @_;
1.1248 raeburn 8398: my ($homeserver,%crsreqresponse);
8399: if ($cdom =~ /^$match_domain$/) {
8400: $homeserver = &domain($cdom,'primary');
8401: }
8402: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8403: my $info;
8404: if (ref($inbound) eq 'HASH') {
8405: $info = &freeze_escape($inbound);
8406: }
8407: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
8408: ':'.&escape($action).':'.&escape($ownername).':'.
8409: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257 raeburn 8410: &escape($title).':'.&escape($code).':'.
8411: &escape($accessstart).':'.&escape($accessend).':'.$info,
8412: $homeserver);
1.1248 raeburn 8413: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8414: my @items = split(/&/,$response);
8415: foreach my $item (@items) {
8416: my ($key,$value) = split('=',$item);
8417: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
8418: }
8419: }
8420: }
8421: return \%crsreqresponse;
8422: }
8423:
1.1305 raeburn 8424: sub auto_export_grades {
1.1309 raeburn 8425: my ($cdom,$cnum,$inforef,$gradesref) = @_;
8426: my ($homeserver,%exportresponse);
8427: if ($cdom =~ /^$match_domain$/) {
8428: $homeserver = &domain($cdom,'primary');
8429: }
8430: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8431: my $info;
8432: if (ref($inforef) eq 'HASH') {
8433: $info = &freeze_escape($inforef);
8434: }
8435: if (ref($gradesref) eq 'HASH') {
8436: my $grades = &freeze_escape($gradesref);
8437: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
8438: $info.':'.$grades,$homeserver);
8439: unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
8440: my @items = split(/&/,$response);
8441: foreach my $item (@items) {
8442: my ($key,$value) = split('=',$item);
8443: $exportresponse{&unescape($key)} = &thaw_unescape($value);
8444: }
8445: }
8446: }
8447: }
8448: return \%exportresponse;
1.1305 raeburn 8449: }
8450:
1.1287 raeburn 8451: sub check_instcode_cloning {
8452: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
8453: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8454: return;
8455: }
8456: my $canclone;
8457: if (@{$code_order} > 0) {
8458: my $instcoderegexp ='^';
8459: my @clonecodes = split(/\&/,$cloner);
8460: foreach my $item (@{$code_order}) {
8461: if (grep(/^\Q$item\E=/,@clonecodes)) {
8462: foreach my $pair (@clonecodes) {
8463: my ($key,$val) = split(/\=/,$pair,2);
8464: $val = &unescape($val);
8465: if ($key eq $item) {
8466: $instcoderegexp .= '('.$val.')';
8467: last;
8468: }
8469: }
8470: } else {
8471: $instcoderegexp .= $codedefaults->{$item};
8472: }
8473: }
8474: $instcoderegexp .= '$';
8475: my (@from,@to);
8476: eval {
8477: (@from) = ($clonefromcode =~ /$instcoderegexp/);
8478: (@to) = ($clonetocode =~ /$instcoderegexp/);
8479: };
8480: if ((@from > 0) && (@to > 0)) {
8481: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
8482: if (!@diffs) {
8483: $canclone = 1;
8484: }
8485: }
8486: }
8487: return $canclone;
8488: }
8489:
8490: sub default_instcode_cloning {
8491: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
8492: my (%codedefaults,@code_order,$canclone);
8493: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
8494: %codedefaults = %{$codedefaultsref};
8495: @code_order = @{$codeorderref};
8496: } elsif ($clonedom) {
8497: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
8498: }
8499: if (($domdefclone) && (@code_order)) {
8500: my @clonecodes = split(/\+/,$domdefclone);
8501: my $instcoderegexp ='^';
8502: foreach my $item (@code_order) {
8503: if (grep(/^\Q$item\E$/,@clonecodes)) {
8504: $instcoderegexp .= '('.$codedefaults{$item}.')';
8505: } else {
8506: $instcoderegexp .= $codedefaults{$item};
8507: }
8508: }
8509: $instcoderegexp .= '$';
8510: my (@from,@to);
8511: eval {
8512: (@from) = ($clonefromcode =~ /$instcoderegexp/);
8513: (@to) = ($clonetocode =~ /$instcoderegexp/);
8514: };
8515: if ((@from > 0) && (@to > 0)) {
8516: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
8517: if (!@diffs) {
8518: $canclone = 1;
8519: }
8520: }
8521: }
8522: return $canclone;
8523: }
8524:
1.679 raeburn 8525: # ------------------------------------------------------- Course Group routines
8526:
8527: sub get_coursegroups {
1.809 raeburn 8528: my ($cdom,$cnum,$group,$namespace) = @_;
8529: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 8530: }
8531:
1.679 raeburn 8532: sub modify_coursegroup {
8533: my ($cdom,$cnum,$groupsettings) = @_;
8534: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
8535: }
8536:
1.809 raeburn 8537: sub toggle_coursegroup_status {
8538: my ($cdom,$cnum,$group,$action) = @_;
8539: my ($from_namespace,$to_namespace);
8540: if ($action eq 'delete') {
8541: $from_namespace = 'coursegroups';
8542: $to_namespace = 'deleted_groups';
8543: } else {
8544: $from_namespace = 'deleted_groups';
8545: $to_namespace = 'coursegroups';
8546: }
8547: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 8548: if (my $tmp = &error(%curr_group)) {
8549: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
8550: return ('read error',$tmp);
8551: } else {
8552: my %savedsettings = %curr_group;
1.809 raeburn 8553: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 8554: my $deloutcome;
8555: if ($result eq 'ok') {
1.809 raeburn 8556: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 8557: } else {
8558: return ('write error',$result);
8559: }
8560: if ($deloutcome eq 'ok') {
8561: return 'ok';
8562: } else {
8563: return ('delete error',$deloutcome);
8564: }
8565: }
8566: }
8567:
1.679 raeburn 8568: sub modify_group_roles {
1.957 raeburn 8569: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 8570: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
8571: my $role = 'gr/'.&escape($userprivs);
8572: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 8573: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 8574: if ($result eq 'ok') {
8575: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
8576: }
1.679 raeburn 8577: return $result;
8578: }
8579:
8580: sub modify_coursegroup_membership {
8581: my ($cdom,$cnum,$membership) = @_;
8582: my $result = &put('groupmembership',$membership,$cdom,$cnum);
8583: return $result;
8584: }
8585:
1.682 raeburn 8586: sub get_active_groups {
8587: my ($udom,$uname,$cdom,$cnum) = @_;
8588: my $now = time;
8589: my %groups = ();
8590: foreach my $key (keys(%env)) {
1.811 albertel 8591: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 8592: my ($start,$end) = split(/\./,$env{$key});
8593: if (($end!=0) && ($end<$now)) { next; }
8594: if (($start!=0) && ($start>$now)) { next; }
8595: if ($1 eq $cdom && $2 eq $cnum) {
8596: $groups{$3} = $env{$key} ;
8597: }
8598: }
8599: }
8600: return %groups;
8601: }
8602:
1.683 raeburn 8603: sub get_group_membership {
8604: my ($cdom,$cnum,$group) = @_;
8605: return(&dump('groupmembership',$cdom,$cnum,$group));
8606: }
8607:
8608: sub get_users_groups {
8609: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 8610: my @usersgroups;
1.683 raeburn 8611: my $cachetime=1800;
8612:
8613: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 8614: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
8615: if (defined($cached)) {
1.734 albertel 8616: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 8617: } else {
8618: $grouplist = '';
1.816 raeburn 8619: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 8620: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 8621: my $access_end = $env{'course.'.$courseid.
8622: '.default_enrollment_end_date'};
8623: my $now = time;
8624: foreach my $key (keys(%roleshash)) {
8625: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
8626: my $group = $1;
8627: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
8628: my $start = $2;
8629: my $end = $1;
8630: if ($start == -1) { next; } # deleted from group
8631: if (($start!=0) && ($start>$now)) { next; }
8632: if (($end!=0) && ($end<$now)) {
8633: if ($access_end && $access_end < $now) {
8634: if ($access_end - $end < 86400) {
8635: push(@usersgroups,$group);
1.733 raeburn 8636: }
8637: }
1.817 raeburn 8638: next;
1.733 raeburn 8639: }
1.817 raeburn 8640: push(@usersgroups,$group);
1.683 raeburn 8641: }
8642: }
8643: }
1.817 raeburn 8644: @usersgroups = &sort_course_groups($courseid,@usersgroups);
8645: $grouplist = join(':',@usersgroups);
8646: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 8647: }
1.733 raeburn 8648: return @usersgroups;
1.683 raeburn 8649: }
8650:
8651: sub devalidate_getgroups_cache {
8652: my ($udom,$uname,$cdom,$cnum)=@_;
8653: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 8654:
1.683 raeburn 8655: my $hashid="$udom:$uname:$courseid";
8656: &devalidate_cache_new('getgroups',$hashid);
8657: }
8658:
1.12 www 8659: # ------------------------------------------------------------------ Plain Text
8660:
8661: sub plaintext {
1.988 raeburn 8662: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 8663: if ($short =~ m{^cr/}) {
1.758 albertel 8664: return (split('/',$short))[-1];
8665: }
1.742 raeburn 8666: if (!defined($cid)) {
8667: $cid = $env{'request.course.id'};
8668: }
8669: my %rolenames = (
1.1008 raeburn 8670: Course => 'std',
8671: Community => 'alt1',
1.1305 raeburn 8672: Placement => 'std',
1.742 raeburn 8673: );
1.1037 raeburn 8674: if ($cid ne '') {
8675: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
8676: unless ($forcedefault) {
8677: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
8678: &Apache::lonlocal::mt_escape(\$roletext);
8679: return &Apache::lonlocal::mt($roletext);
8680: }
8681: }
8682: }
8683: if ((defined($type)) && (defined($rolenames{$type})) &&
8684: (defined($rolenames{$type})) &&
8685: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 8686: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 8687: } elsif ($cid ne '') {
8688: my $crstype = $env{'course.'.$cid.'.type'};
8689: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
8690: (defined($prp{$short}{$rolenames{$crstype}}))) {
8691: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
8692: }
1.742 raeburn 8693: }
1.1037 raeburn 8694: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 8695: }
8696:
8697: # ----------------------------------------------------------------- Assign Role
8698:
8699: sub assignrole {
1.957 raeburn 8700: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
8701: $context)=@_;
1.21 www 8702: my $mrole;
8703: if ($role =~ /^cr\//) {
1.393 www 8704: my $cwosec=$url;
1.811 albertel 8705: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 8706: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 8707: my $refused = 1;
8708: if ($context eq 'requestcourses') {
8709: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8710: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
8711: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
8712: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
8713: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
8714: if ($crsenv{'internal.courseowner'} eq
8715: $env{'user.name'}.':'.$env{'user.domain'}) {
8716: $refused = '';
8717: }
8718: }
8719: }
8720: }
8721: }
8722: if ($refused) {
8723: &logthis('Refused custom assignrole: '.
8724: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
8725: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
8726: return 'refused';
8727: }
1.104 www 8728: }
1.21 www 8729: $mrole='cr';
1.678 raeburn 8730: } elsif ($role =~ /^gr\//) {
8731: my $cwogrp=$url;
1.811 albertel 8732: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 8733: unless (&allowed('mdg',$cwogrp)) {
8734: &logthis('Refused group assignrole: '.
8735: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
8736: $env{'user.name'}.' at '.$env{'user.domain'});
8737: return 'refused';
8738: }
8739: $mrole='gr';
1.21 www 8740: } else {
1.82 www 8741: my $cwosec=$url;
1.811 albertel 8742: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 8743: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
8744: my $refused;
8745: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
8746: if (!(&allowed('c'.$role,$url))) {
8747: $refused = 1;
8748: }
8749: } else {
8750: $refused = 1;
8751: }
1.947 raeburn 8752: if ($refused) {
1.1045 raeburn 8753: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
8754: if (!$selfenroll && $context eq 'course') {
8755: my %crsenv;
8756: if ($role eq 'cc' || $role eq 'co') {
8757: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
8758: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
8759: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
8760: if ($crsenv{'internal.courseowner'} eq
8761: $env{'user.name'}.':'.$env{'user.domain'}) {
8762: $refused = '';
8763: }
8764: }
8765: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
8766: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
8767: if ($crsenv{'internal.courseowner'} eq
8768: $env{'user.name'}.':'.$env{'user.domain'}) {
8769: $refused = '';
8770: }
8771: }
8772: }
8773: }
8774: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 8775: $refused = '';
1.1017 raeburn 8776: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 8777: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 8778: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 8779: my $wrongcc;
8780: if ($cnum =~ /^$match_community$/) {
8781: $wrongcc = 1 if ($role eq 'cc');
8782: } else {
8783: $wrongcc = 1 if ($role eq 'co');
8784: }
8785: unless ($wrongcc) {
8786: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
8787: if ($crsenv{'internal.courseowner'} eq
8788: $env{'user.name'}.':'.$env{'user.domain'}) {
8789: $refused = '';
8790: }
1.1017 raeburn 8791: }
8792: }
1.1183 raeburn 8793: } elsif ($context eq 'requestauthor') {
8794: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
8795: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
8796: if ($env{'environment.requestauthor'} eq 'automatic') {
8797: $refused = '';
8798: } else {
8799: my %domdefaults = &get_domain_defaults($udom);
8800: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
8801: my $checkbystatus;
8802: if ($env{'user.adv'}) {
8803: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
8804: if ($disposition eq 'automatic') {
8805: $refused = '';
8806: } elsif ($disposition eq '') {
8807: $checkbystatus = 1;
8808: }
8809: } else {
8810: $checkbystatus = 1;
8811: }
8812: if ($checkbystatus) {
8813: if ($env{'environment.inststatus'}) {
8814: my @inststatuses = split(/,/,$env{'environment.inststatus'});
8815: foreach my $type (@inststatuses) {
8816: if (($type ne '') &&
8817: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
8818: $refused = '';
8819: }
8820: }
8821: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
8822: $refused = '';
8823: }
8824: }
8825: }
8826: }
8827: }
1.1017 raeburn 8828: }
8829: if ($refused) {
1.947 raeburn 8830: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
8831: ' '.$role.' '.$end.' '.$start.' by '.
8832: $env{'user.name'}.' at '.$env{'user.domain'});
8833: return 'refused';
8834: }
1.932 raeburn 8835: }
1.1131 raeburn 8836: } elsif ($role eq 'au') {
8837: if ($url ne '/'.$udom.'/') {
8838: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
8839: ' to assign author role for '.$uname.':'.$udom.
8840: ' in domain: '.$url.' refused (wrong domain).');
8841: return 'refused';
8842: }
1.104 www 8843: }
1.21 www 8844: $mrole=$role;
8845: }
1.620 albertel 8846: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 8847: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 8848: if ($end) { $command.='_'.$end; }
1.21 www 8849: if ($start) {
8850: if ($end) {
1.81 www 8851: $command.='_'.$start;
1.21 www 8852: } else {
1.81 www 8853: $command.='_0_'.$start;
1.21 www 8854: }
8855: }
1.739 raeburn 8856: my $origstart = $start;
8857: my $origend = $end;
1.957 raeburn 8858: my $delflag;
1.357 www 8859: # actually delete
8860: if ($deleteflag) {
1.373 www 8861: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 8862: # modify command to delete the role
1.620 albertel 8863: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 8864: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 8865: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 8866: # set start and finish to negative values for userrolelog
8867: $start=-1;
8868: $end=-1;
1.957 raeburn 8869: $delflag = 1;
1.357 www 8870: }
8871: }
8872: # send command
1.349 www 8873: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 8874: # log new user role if status is ok
1.349 www 8875: if ($answer eq 'ok') {
1.663 raeburn 8876: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184 raeburn 8877: if (($role eq 'cc') || ($role eq 'in') ||
8878: ($role eq 'ep') || ($role eq 'ad') ||
8879: ($role eq 'ta') || ($role eq 'st') ||
8880: ($role=~/^cr/) || ($role eq 'gr') ||
8881: ($role eq 'co')) {
1.1200 raeburn 8882: # for course roles, perform group memberships changes triggered by role change.
8883: unless ($role =~ /^gr/) {
8884: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
8885: $origstart,$selfenroll,$context);
8886: }
1.1184 raeburn 8887: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
8888: $selfenroll,$context);
8889: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1327 raeburn 8890: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh')) {
1.1184 raeburn 8891: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
8892: $context);
8893: } elsif (($role eq 'ca') || ($role eq 'aa')) {
8894: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
8895: $context);
8896: }
1.1053 raeburn 8897: if ($role eq 'cc') {
8898: &autoupdate_coowners($url,$end,$start,$uname,$udom);
8899: }
1.349 www 8900: }
8901: return $answer;
1.169 harris41 8902: }
8903:
1.1053 raeburn 8904: sub autoupdate_coowners {
8905: my ($url,$end,$start,$uname,$udom) = @_;
8906: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
8907: if (($cdom ne '') && ($cnum ne '')) {
8908: my $now = time;
8909: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
8910: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
8911: my %coursehash = &coursedescription($cdom.'_'.$cnum);
8912: my $instcode = $coursehash{'internal.coursecode'};
8913: if ($instcode ne '') {
1.1056 raeburn 8914: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
8915: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 8916: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 8917: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
8918: if ($result eq 'valid') {
8919: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 8920: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
8921: push(@newcoowners,$coowner);
8922: }
8923: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
8924: push(@newcoowners,$uname.':'.$udom);
8925: }
8926: @newcoowners = sort(@newcoowners);
8927: } else {
8928: push(@newcoowners,$uname.':'.$udom);
8929: }
8930: } else {
8931: if ($coursehash{'internal.co-owners'}) {
8932: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
8933: unless ($coowner eq $uname.':'.$udom) {
8934: push(@newcoowners,$coowner);
8935: }
8936: }
8937: unless (@newcoowners > 0) {
8938: $delcoowners = 1;
8939: $coowners = '';
8940: }
8941: }
8942: }
8943: if (@newcoowners || $delcoowners) {
8944: &store_coowners($cdom,$cnum,$coursehash{'home'},
8945: $delcoowners,@newcoowners);
8946: }
8947: }
8948: }
8949: }
8950: }
8951: }
8952: }
8953:
8954: sub store_coowners {
8955: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
8956: my $cid = $cdom.'_'.$cnum;
8957: my ($coowners,$delresult,$putresult);
8958: if (@newcoowners) {
8959: $coowners = join(',',@newcoowners);
8960: my %coownershash = (
8961: 'internal.co-owners' => $coowners,
8962: );
8963: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
8964: if ($putresult eq 'ok') {
8965: if ($env{'course.'.$cid.'.num'} eq $cnum) {
8966: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
8967: }
8968: }
8969: }
8970: if ($delcoowners) {
8971: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
8972: if ($delresult eq 'ok') {
8973: if ($env{'course.'.$cid.'.internal.co-owners'}) {
8974: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
8975: }
8976: }
8977: }
8978: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
8979: my %crsinfo =
8980: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
8981: if (ref($crsinfo{$cid}) eq 'HASH') {
8982: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
8983: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
8984: }
8985: }
8986: }
8987:
1.169 harris41 8988: # -------------------------------------------------- Modify user authentication
1.197 www 8989: # Overrides without validation
8990:
1.169 harris41 8991: sub modifyuserauth {
8992: my ($udom,$uname,$umode,$upass)=@_;
8993: my $uhome=&homeserver($uname,$udom);
1.197 www 8994: unless (&allowed('mau',$udom)) { return 'refused'; }
8995: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 8996: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
8997: ' in domain '.$env{'request.role.domain'});
1.169 harris41 8998: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
8999: &escape($upass),$uhome);
1.620 albertel 9000: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 9001: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
9002: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
9003: &log($udom,,$uname,$uhome,
1.620 albertel 9004: 'Authentication changed by '.$env{'user.domain'}.', '.
9005: $env{'user.name'}.', '.$umode.
1.197 www 9006: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 9007: unless ($reply eq 'ok') {
1.197 www 9008: &logthis('Authentication mode error: '.$reply);
1.169 harris41 9009: return 'error: '.$reply;
9010: }
1.170 harris41 9011: return 'ok';
1.80 www 9012: }
9013:
1.81 www 9014: # --------------------------------------------------------------- Modify a user
1.80 www 9015:
1.81 www 9016: sub modifyuser {
1.206 matthew 9017: my ($udom, $uname, $uid,
9018: $umode, $upass, $first,
9019: $middle, $last, $gene,
1.1058 raeburn 9020: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 9021: $udom= &LONCAPA::clean_domain($udom);
9022: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 9023: my $showcandelete = 'none';
9024: if (ref($candelete) eq 'ARRAY') {
9025: if (@{$candelete} > 0) {
9026: $showcandelete = join(', ',@{$candelete});
9027: }
9028: }
1.81 www 9029: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 9030: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 9031: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 9032: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
9033: ' desiredhome not specified').
1.620 albertel 9034: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
9035: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 9036: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 9037: my $newuser;
9038: if ($uhome eq 'no_host') {
9039: $newuser = 1;
9040: }
1.80 www 9041: # ----------------------------------------------------------------- Create User
1.406 albertel 9042: if (($uhome eq 'no_host') &&
9043: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 9044: my $unhome='';
1.844 albertel 9045: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 9046: $unhome = $desiredhome;
1.620 albertel 9047: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
9048: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 9049: } else { # load balancing routine for determining $unhome
1.81 www 9050: my $loadm=10000000;
1.841 albertel 9051: my %servers = &get_servers($udom,'library');
9052: foreach my $tryserver (keys(%servers)) {
9053: my $answer=reply('load',$tryserver);
9054: if (($answer=~/\d+/) && ($answer<$loadm)) {
9055: $loadm=$answer;
9056: $unhome=$tryserver;
9057: }
1.80 www 9058: }
9059: }
9060: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 9061: return 'error: unable to find a home server for '.$uname.
9062: ' in domain '.$udom;
1.80 www 9063: }
9064: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
9065: &escape($upass),$unhome);
9066: unless ($reply eq 'ok') {
9067: return 'error: '.$reply;
9068: }
1.230 stredwic 9069: $uhome=&homeserver($uname,$udom,'true');
1.80 www 9070: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 9071: return 'error: unable verify users home machine.';
1.80 www 9072: }
1.209 matthew 9073: } # End of creation of new user
1.80 www 9074: # ---------------------------------------------------------------------- Add ID
9075: if ($uid) {
9076: $uid=~tr/A-Z/a-z/;
9077: my %uidhash=&idrget($udom,$uname);
1.196 www 9078: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
9079: && (!$forceid)) {
1.80 www 9080: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 9081: return 'error: user id "'.$uid.'" does not match '.
9082: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 9083: }
9084: } else {
1.1300 raeburn 9085: &idput($udom,{$uname => $uid},$uhome,'ids');
1.80 www 9086: }
9087: }
9088: # -------------------------------------------------------------- Add names, etc
1.313 matthew 9089: my @tmp=&get('environment',
1.899 raeburn 9090: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 9091: 'permanentemail','inststatus'],
1.134 albertel 9092: $udom,$uname);
1.1075 raeburn 9093: my (%names,%oldnames);
1.313 matthew 9094: if ($tmp[0] =~ m/^error:.*/) {
9095: %names=();
9096: } else {
9097: %names = @tmp;
1.1075 raeburn 9098: %oldnames = %names;
1.313 matthew 9099: }
1.388 www 9100: #
1.1058 raeburn 9101: # If name, email and/or uid are blank (e.g., because an uploaded file
9102: # of users did not contain them), do not overwrite existing values
9103: # unless field is in $candelete array ref.
9104: #
9105:
9106: my @fields = ('firstname','middlename','lastname','generation',
9107: 'permanentemail','id');
9108: my %newvalues;
9109: if (ref($candelete) eq 'ARRAY') {
9110: foreach my $field (@fields) {
9111: if (grep(/^\Q$field\E$/,@{$candelete})) {
9112: if ($field eq 'firstname') {
9113: $names{$field} = $first;
9114: } elsif ($field eq 'middlename') {
9115: $names{$field} = $middle;
9116: } elsif ($field eq 'lastname') {
9117: $names{$field} = $last;
9118: } elsif ($field eq 'generation') {
9119: $names{$field} = $gene;
9120: } elsif ($field eq 'permanentemail') {
9121: $names{$field} = $email;
9122: } elsif ($field eq 'id') {
9123: $names{$field} = $uid;
9124: }
9125: }
9126: }
9127: }
1.388 www 9128: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 9129: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 9130: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 9131: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 9132: if ($email) {
9133: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 9134: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 9135: }
1.899 raeburn 9136: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 9137: if (defined($inststatus)) {
9138: $names{'inststatus'} = '';
9139: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
9140: if (ref($usertypes) eq 'HASH') {
9141: my @okstatuses;
9142: foreach my $item (split(/:/,$inststatus)) {
9143: if (defined($usertypes->{$item})) {
9144: push(@okstatuses,$item);
9145: }
9146: }
9147: if (@okstatuses) {
9148: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
9149: }
9150: }
9151: }
1.1075 raeburn 9152: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 9153: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 9154: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 9155: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
9156: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
9157: } else {
9158: $logmsg .= ' during self creation';
9159: }
1.1075 raeburn 9160: my $changed;
9161: if ($newuser) {
9162: $changed = 1;
9163: } else {
9164: foreach my $field (@fields) {
9165: if ($names{$field} ne $oldnames{$field}) {
9166: $changed = 1;
9167: last;
9168: }
9169: }
9170: }
9171: unless ($changed) {
9172: $logmsg = 'No changes in user information needed for: '.$logmsg;
9173: &logthis($logmsg);
9174: return 'ok';
9175: }
9176: my $reply = &put('environment', \%names, $udom,$uname);
9177: if ($reply ne 'ok') {
9178: return 'error: '.$reply;
9179: }
1.1087 raeburn 9180: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
9181: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
9182: }
1.1075 raeburn 9183: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
9184: &devalidate_cache_new('namescache',$uname.':'.$udom);
9185: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 9186: &logthis($logmsg);
1.134 albertel 9187: return 'ok';
1.80 www 9188: }
9189:
1.81 www 9190: # -------------------------------------------------------------- Modify student
1.80 www 9191:
1.81 www 9192: sub modifystudent {
9193: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 9194: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314 raeburn 9195: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 9196: if (!$cid) {
1.620 albertel 9197: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 9198: return 'not_in_class';
9199: }
1.80 www 9200: }
9201: # --------------------------------------------------------------- Make the user
1.81 www 9202: my $reply=&modifyuser
1.209 matthew 9203: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 9204: $desiredhome,$email,$inststatus);
1.80 www 9205: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 9206: # This will cause &modify_student_enrollment to get the uid from the
1.1235 raeburn 9207: # student's environment
1.297 matthew 9208: $uid = undef if (!$forceid);
1.455 albertel 9209: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216 raeburn 9210: $gene,$usec,$end,$start,$type,$locktype,
1.1314 raeburn 9211: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 9212: return $reply;
9213: }
9214:
9215: sub modify_student_enrollment {
1.1216 raeburn 9216: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314 raeburn 9217: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 9218: my ($cdom,$cnum,$chome);
9219: if (!$cid) {
1.620 albertel 9220: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 9221: return 'not_in_class';
9222: }
1.620 albertel 9223: $cdom=$env{'course.'.$cid.'.domain'};
9224: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 9225: } else {
9226: ($cdom,$cnum)=split(/_/,$cid);
9227: }
1.620 albertel 9228: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 9229: if (!$chome) {
1.457 raeburn 9230: $chome=&homeserver($cnum,$cdom);
1.297 matthew 9231: }
1.455 albertel 9232: if (!$chome) { return 'unknown_course'; }
1.297 matthew 9233: # Make sure the user exists
1.81 www 9234: my $uhome=&homeserver($uname,$udom);
9235: if (($uhome eq '') || ($uhome eq 'no_host')) {
9236: return 'error: no such user';
9237: }
1.297 matthew 9238: # Get student data if we were not given enough information
9239: if (!defined($first) || $first eq '' ||
9240: !defined($last) || $last eq '' ||
9241: !defined($uid) || $uid eq '' ||
9242: !defined($middle) || $middle eq '' ||
9243: !defined($gene) || $gene eq '') {
1.294 matthew 9244: # They did not supply us with enough data to enroll the student, so
9245: # we need to pick up more information.
1.297 matthew 9246: my %tmp = &get('environment',
1.294 matthew 9247: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 9248: ,$udom,$uname);
9249:
1.800 albertel 9250: #foreach my $key (keys(%tmp)) {
9251: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 9252: #}
1.294 matthew 9253: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
9254: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
9255: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 9256: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 9257: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
9258: }
1.556 albertel 9259: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 9260: my $user = "$uname:$udom";
9261: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 9262: my $reply=cput('classlist',
1.1148 raeburn 9263: {$user =>
1.1314 raeburn 9264: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 9265: $cdom,$cnum);
1.1148 raeburn 9266: if (($reply eq 'ok') || ($reply eq 'delayed')) {
9267: &devalidate_getsection_cache($udom,$uname,$cid);
9268: } else {
1.81 www 9269: return 'error: '.$reply;
9270: }
1.297 matthew 9271: # Add student role to user
1.83 www 9272: my $uurl='/'.$cid;
1.81 www 9273: $uurl=~s/\_/\//g;
9274: if ($usec) {
9275: $uurl.='/'.$usec;
9276: }
1.1148 raeburn 9277: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
9278: $selfenroll,$context);
9279: if ($result ne 'ok') {
9280: if ($old_entry{$user} ne '') {
9281: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
9282: } else {
9283: $reply = &del('classlist',[$user],$cdom,$cnum);
9284: }
9285: }
9286: return $result;
1.21 www 9287: }
9288:
1.556 albertel 9289: sub format_name {
9290: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
9291: my $name;
9292: if ($first ne 'lastname') {
9293: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
9294: } else {
9295: if ($lastname=~/\S/) {
9296: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
9297: $name=~s/\s+,/,/;
9298: } else {
9299: $name.= $firstname.' '.$middlename.' '.$generation;
9300: }
9301: }
9302: $name=~s/^\s+//;
9303: $name=~s/\s+$//;
9304: $name=~s/\s+/ /g;
9305: return $name;
9306: }
9307:
1.84 www 9308: # ------------------------------------------------- Write to course preferences
9309:
9310: sub writecoursepref {
9311: my ($courseid,%prefs)=@_;
9312: $courseid=~s/^\///;
9313: $courseid=~s/\_/\//g;
9314: my ($cdomain,$cnum)=split(/\//,$courseid);
9315: my $chome=homeserver($cnum,$cdomain);
9316: if (($chome eq '') || ($chome eq 'no_host')) {
9317: return 'error: no such course';
9318: }
9319: my $cstring='';
1.800 albertel 9320: foreach my $pref (keys(%prefs)) {
9321: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 9322: }
1.84 www 9323: $cstring=~s/\&$//;
9324: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
9325: }
9326:
9327: # ---------------------------------------------------------- Make/modify course
9328:
9329: sub createcourse {
1.741 raeburn 9330: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 9331: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 9332: $url=&declutter($url);
9333: my $cid='';
1.1028 raeburn 9334: if ($context eq 'requestcourses') {
9335: my $can_create = 0;
9336: my ($ownername,$ownerdom) = split(':',$course_owner);
9337: if ($udom eq $ownerdom) {
9338: if (&usertools_access($ownername,$ownerdom,$category,undef,
9339: $context)) {
9340: $can_create = 1;
9341: }
9342: } else {
9343: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
9344: $category);
9345: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
9346: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
9347: if (@curr > 0) {
9348: my @options = qw(approval validate autolimit);
9349: my $optregex = join('|',@options);
9350: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
9351: $can_create = 1;
9352: }
9353: }
9354: }
9355: }
9356: if ($can_create) {
9357: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
9358: unless (&allowed('ccc',$udom)) {
9359: return 'refused';
9360: }
1.1017 raeburn 9361: }
9362: } else {
9363: return 'refused';
9364: }
1.1028 raeburn 9365: } elsif (!&allowed('ccc',$udom)) {
9366: return 'refused';
1.84 www 9367: }
1.1011 raeburn 9368: # --------------------------------------------------------------- Get Unique ID
9369: my $uname;
9370: if ($cnum =~ /^$match_courseid$/) {
9371: my $chome=&homeserver($cnum,$udom,'true');
9372: if (($chome eq '') || ($chome eq 'no_host')) {
9373: $uname = $cnum;
9374: } else {
1.1038 raeburn 9375: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 9376: }
9377: } else {
1.1038 raeburn 9378: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 9379: }
9380: return $uname if ($uname =~ /^error/);
9381: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 9382: if (!defined($course_server)) {
9383: if (defined(&domain($udom,'primary'))) {
9384: $course_server = &domain($udom,'primary');
9385: } else {
9386: $course_server = $env{'user.home'};
9387: }
9388: }
9389: my %host_servers =
9390: &Apache::lonnet::get_servers($udom,'library');
9391: unless ($host_servers{$course_server}) {
9392: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 9393: }
1.84 www 9394: # ------------------------------------------------------------- Make the course
9395: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 9396: $course_server);
1.84 www 9397: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 9398: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 9399: if (($uhome eq '') || ($uhome eq 'no_host')) {
9400: return 'error: no such course';
9401: }
1.271 www 9402: # ----------------------------------------------------------------- Course made
1.516 raeburn 9403: # log existence
1.1029 raeburn 9404: my $now = time;
1.918 raeburn 9405: my $newcourse = {
9406: $udom.'_'.$uname => {
1.921 raeburn 9407: description => $description,
9408: inst_code => $inst_code,
9409: owner => $course_owner,
9410: type => $crstype,
1.1029 raeburn 9411: creator => $env{'user.name'}.':'.
9412: $env{'user.domain'},
9413: created => $now,
9414: context => $context,
1.918 raeburn 9415: },
9416: };
1.921 raeburn 9417: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 9418: # set toplevel url
1.271 www 9419: my $topurl=$url;
9420: unless ($nonstandard) {
9421: # ------------------------------------------ For standard courses, make top url
9422: my $mapurl=&clutter($url);
1.278 www 9423: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 9424: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 9425: <map>
9426: <resource id="1" type="start"></resource>
9427: <resource id="2" src="$mapurl"></resource>
9428: <resource id="3" type="finish"></resource>
9429: <link index="1" from="1" to="2"></link>
9430: <link index="2" from="2" to="3"></link>
9431: </map>
9432: ENDINITMAP
9433: $topurl=&declutter(
1.638 albertel 9434: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 9435: );
9436: }
9437: # ----------------------------------------------------------- Write preferences
1.84 www 9438: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 9439: ('description' => $description,
9440: 'url' => $topurl,
9441: 'internal.creator' => $env{'user.name'}.':'.
9442: $env{'user.domain'},
9443: 'internal.created' => $now,
9444: 'internal.creationcontext' => $context)
9445: );
1.84 www 9446: return '/'.$udom.'/'.$uname;
9447: }
9448:
1.1011 raeburn 9449: # ------------------------------------------------------------------- Create ID
9450: sub generate_coursenum {
1.1038 raeburn 9451: my ($udom,$crstype) = @_;
1.1011 raeburn 9452: my $domdesc = &domain($udom);
9453: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 9454: my $first;
9455: if ($crstype eq 'Community') {
9456: $first = '0';
9457: } else {
9458: $first = int(1+rand(9));
9459: }
9460: my $uname=$first.
1.1011 raeburn 9461: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
9462: substr($$.time,0,5).unpack("H8",pack("I32",time)).
9463: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
9464: # ----------------------------------------------- Make sure that does not exist
9465: my $uhome=&homeserver($uname,$udom,'true');
9466: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 9467: if ($crstype eq 'Community') {
9468: $first = '0';
9469: } else {
9470: $first = int(1+rand(9));
9471: }
9472: $uname=$first.
1.1011 raeburn 9473: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
9474: substr($$.time,0,5).unpack("H8",pack("I32",time)).
9475: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
9476: $uhome=&homeserver($uname,$udom,'true');
9477: unless (($uhome eq '') || ($uhome eq 'no_host')) {
9478: return 'error: unable to generate unique course-ID';
9479: }
9480: }
9481: return $uname;
9482: }
9483:
1.813 albertel 9484: sub is_course {
1.1167 droeschl 9485: my ($cdom, $cnum) = scalar(@_) == 1 ?
9486: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
9487:
9488: return unless $cdom and $cnum;
9489:
9490: my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
9491: '.');
9492:
1.1219 raeburn 9493: return unless(exists($courses{$cdom.'_'.$cnum}));
1.1167 droeschl 9494: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 9495: }
9496:
1.1015 raeburn 9497: sub store_userdata {
9498: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 9499: my $result;
1.1016 raeburn 9500: if ($datakey ne '') {
1.1013 raeburn 9501: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 9502: if ($udom eq '' || $uname eq '') {
9503: $udom = $env{'user.domain'};
9504: $uname = $env{'user.name'};
9505: }
9506: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 9507: if (($uhome eq '') || ($uhome eq 'no_host')) {
9508: $result = 'error: no_host';
9509: } else {
9510: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
9511: $storehash->{'host'} = $perlvar{'lonHostID'};
9512:
9513: my $namevalue='';
9514: foreach my $key (keys(%{$storehash})) {
9515: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
9516: }
9517: $namevalue=~s/\&$//;
1.1224 raeburn 9518: unless ($namespace eq 'courserequests') {
9519: $datakey = &escape($datakey);
9520: }
1.1105 raeburn 9521: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
9522: $namevalue,$uhome);
1.1013 raeburn 9523: }
9524: } else {
9525: $result = 'error: data to store was not a hash reference';
9526: }
9527: } else {
9528: $result= 'error: invalid requestkey';
9529: }
9530: return $result;
9531: }
9532:
1.21 www 9533: # ---------------------------------------------------------- Assign Custom Role
9534:
9535: sub assigncustomrole {
1.957 raeburn 9536: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9537: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 9538: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 9539: }
9540:
9541: # ----------------------------------------------------------------- Revoke Role
9542:
9543: sub revokerole {
1.957 raeburn 9544: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9545: my $now=time;
1.965 raeburn 9546: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 9547: }
9548:
9549: # ---------------------------------------------------------- Revoke Custom Role
9550:
9551: sub revokecustomrole {
1.957 raeburn 9552: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9553: my $now=time;
1.357 www 9554: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 9555: $deleteflag,$selfenroll,$context);
1.17 www 9556: }
9557:
1.533 banghart 9558: # ------------------------------------------------------------ Disk usage
1.535 albertel 9559: sub diskusage {
1.955 raeburn 9560: my ($udom,$uname,$directorypath,$getpropath)=@_;
9561: $directorypath =~ s/\/$//;
9562: my $listing=&reply('du2:'.&escape($directorypath).':'
9563: .&escape($getpropath).':'.&escape($uname).':'
9564: .&escape($udom),homeserver($uname,$udom));
9565: if ($listing eq 'unknown_cmd') {
9566: if ($getpropath) {
9567: $directorypath = &propath($udom,$uname).'/'.$directorypath;
9568: }
9569: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
9570: }
1.514 albertel 9571: return $listing;
1.512 banghart 9572: }
9573:
1.566 banghart 9574: sub is_locked {
1.1096 raeburn 9575: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 9576: my @check;
9577: my $is_locked;
1.1093 raeburn 9578: push (@check,$file_name);
1.613 albertel 9579: my %locked = &get('file_permissions',\@check,
1.620 albertel 9580: $env{'user.domain'},$env{'user.name'});
1.615 albertel 9581: my ($tmp)=keys(%locked);
9582: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 9583:
1.566 banghart 9584: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 9585: $is_locked = 'false';
9586: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 9587: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 9588: $is_locked = 'true';
1.1096 raeburn 9589: if (ref($which) eq 'ARRAY') {
9590: push(@{$which},$entry);
9591: } else {
9592: last;
9593: }
1.745 raeburn 9594: }
9595: }
1.566 banghart 9596: } else {
9597: $is_locked = 'false';
9598: }
1.1093 raeburn 9599: return $is_locked;
1.566 banghart 9600: }
9601:
1.759 albertel 9602: sub declutter_portfile {
9603: my ($file) = @_;
1.833 albertel 9604: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 9605: return $file;
9606: }
9607:
1.559 banghart 9608: # ------------------------------------------------------------- Mark as Read Only
9609:
9610: sub mark_as_readonly {
9611: my ($domain,$user,$files,$what) = @_;
1.613 albertel 9612: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 9613: my ($tmp)=keys(%current_permissions);
9614: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 9615: foreach my $file (@{$files}) {
1.759 albertel 9616: $file = &declutter_portfile($file);
1.561 banghart 9617: push(@{$current_permissions{$file}},$what);
1.559 banghart 9618: }
1.613 albertel 9619: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 9620: return;
9621: }
9622:
1.572 banghart 9623: # ------------------------------------------------------------Save Selected Files
9624:
9625: sub save_selected_files {
9626: my ($user, $path, @files) = @_;
9627: my $filename = $user."savedfiles";
1.573 banghart 9628: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 9629: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 9630: foreach my $file (@files) {
1.620 albertel 9631: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 9632: }
9633: foreach my $file (@other_files) {
1.574 banghart 9634: print (OUT $file."\n");
1.572 banghart 9635: }
1.574 banghart 9636: close (OUT);
1.572 banghart 9637: return 'ok';
9638: }
9639:
1.574 banghart 9640: sub clear_selected_files {
9641: my ($user) = @_;
9642: my $filename = $user."savedfiles";
1.1117 foxr 9643: open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574 banghart 9644: print (OUT undef);
9645: close (OUT);
9646: return ("ok");
9647: }
9648:
1.572 banghart 9649: sub files_in_path {
9650: my ($user, $path) = @_;
9651: my $filename = $user."savedfiles";
9652: my %return_files;
1.1117 foxr 9653: open (IN, '<'.LONCAPA::tempdir().$filename);
1.573 banghart 9654: while (my $line_in = <IN>) {
1.574 banghart 9655: chomp ($line_in);
9656: my @paths_and_file = split (m!/!, $line_in);
9657: my $file_part = pop (@paths_and_file);
9658: my $path_part = join ('/', @paths_and_file);
1.573 banghart 9659: $path_part.='/';
9660: my $path_and_file = $path_part.$file_part;
9661: if ($path_part eq $path) {
9662: $return_files{$file_part}= 'selected';
9663: }
9664: }
1.574 banghart 9665: close (IN);
9666: return (\%return_files);
1.572 banghart 9667: }
9668:
9669: # called in portfolio select mode, to show files selected NOT in current directory
9670: sub files_not_in_path {
9671: my ($user, $path) = @_;
9672: my $filename = $user."savedfiles";
9673: my @return_files;
9674: my $path_part;
1.1117 foxr 9675: open(IN, '<'.LONCAPA::.$filename);
1.800 albertel 9676: while (my $line = <IN>) {
1.572 banghart 9677: #ok, I know it's clunky, but I want it to work
1.800 albertel 9678: my @paths_and_file = split(m|/|, $line);
9679: my $file_part = pop(@paths_and_file);
9680: chomp($file_part);
9681: my $path_part = join('/', @paths_and_file);
1.572 banghart 9682: $path_part .= '/';
9683: my $path_and_file = $path_part.$file_part;
9684: if ($path_part ne $path) {
1.800 albertel 9685: push(@return_files, ($path_and_file));
1.572 banghart 9686: }
9687: }
1.800 albertel 9688: close(OUT);
1.574 banghart 9689: return (@return_files);
1.572 banghart 9690: }
9691:
1.1273 raeburn 9692: #------------------------------Submitted/Handedback Portfolio Files Versioning
9693:
9694: sub portfiles_versioning {
9695: my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
9696: my $portfolio_root = '/userfiles/portfolio';
9697: return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
9698: foreach my $file (@{$portfiles}) {
9699: &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
9700: my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
9701: my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
9702: my $getpropath = 1;
9703: my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
9704: $stu_name,$getpropath);
9705: my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
9706: my $new_answer =
9707: &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
9708: if ($new_answer ne 'problem getting file') {
9709: push(@{$versioned_portfiles}, $directory.$new_answer);
9710: &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
9711: [$symb,$env{'request.course.id'},'graded']);
9712: }
9713: }
9714: }
9715:
9716: sub get_next_version {
9717: my ($answer_name, $answer_ext, $dir_list) = @_;
9718: my $version;
9719: if (ref($dir_list) eq 'ARRAY') {
9720: foreach my $row (@{$dir_list}) {
9721: my ($file) = split(/\&/,$row,2);
9722: my ($file_name,$file_version,$file_ext) =
9723: &file_name_version_ext($file);
9724: if (($file_name eq $answer_name) &&
9725: ($file_ext eq $answer_ext)) {
9726: # gets here if filename and extension match,
9727: # regardless of version
9728: if ($file_version ne '') {
9729: # a versioned file is found so save it for later
9730: if ($file_version > $version) {
9731: $version = $file_version;
9732: }
9733: }
9734: }
9735: }
9736: }
9737: $version ++;
9738: return($version);
9739: }
9740:
9741: sub version_selected_portfile {
9742: my ($domain,$stu_name,$directory,$file_name,$version) = @_;
9743: my ($answer_name,$answer_ver,$answer_ext) =
9744: &file_name_version_ext($file_name);
9745: my $new_answer;
9746: $env{'form.copy'} =
9747: &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
9748: if($env{'form.copy'} eq '-1') {
9749: $new_answer = 'problem getting file';
9750: } else {
9751: $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
9752: my $copy_result =
9753: &finishuserfileupload($stu_name,$domain,'copy',
9754: '/portfolio'.$directory.$new_answer);
9755: }
9756: undef($env{'form.copy'});
9757: return ($new_answer);
9758: }
9759:
9760: sub file_name_version_ext {
9761: my ($file)=@_;
9762: my @file_parts = split(/\./, $file);
9763: my ($name,$version,$ext);
9764: if (@file_parts > 1) {
9765: $ext=pop(@file_parts);
9766: if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
9767: $version=pop(@file_parts);
9768: }
9769: $name=join('.',@file_parts);
9770: } else {
9771: $name=join('.',@file_parts);
9772: }
9773: return($name,$version,$ext);
9774: }
9775:
1.745 raeburn 9776: #----------------------------------------------Get portfolio file permissions
1.629 banghart 9777:
1.745 raeburn 9778: sub get_portfile_permissions {
9779: my ($domain,$user) = @_;
1.613 albertel 9780: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 9781: my ($tmp)=keys(%current_permissions);
9782: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 9783: return \%current_permissions;
9784: }
9785:
9786: #---------------------------------------------Get portfolio file access controls
9787:
1.749 raeburn 9788: sub get_access_controls {
1.745 raeburn 9789: my ($current_permissions,$group,$file) = @_;
1.769 albertel 9790: my %access;
9791: my $real_file = $file;
9792: $file =~ s/\.meta$//;
1.745 raeburn 9793: if (defined($file)) {
1.749 raeburn 9794: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
9795: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 9796: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 9797: }
9798: }
1.745 raeburn 9799: } else {
1.749 raeburn 9800: foreach my $key (keys(%{$current_permissions})) {
9801: if ($key =~ /\0accesscontrol$/) {
9802: if (defined($group)) {
9803: if ($key !~ m-^\Q$group\E/-) {
9804: next;
9805: }
9806: }
9807: my ($fullpath) = split(/\0/,$key);
9808: if (ref($$current_permissions{$key}) eq 'HASH') {
9809: foreach my $control (keys(%{$$current_permissions{$key}})) {
9810: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
9811: }
9812: }
9813: }
9814: }
9815: }
9816: return %access;
9817: }
9818:
9819: sub modify_access_controls {
9820: my ($file_name,$changes,$domain,$user)=@_;
9821: my ($outcome,$deloutcome);
9822: my %store_permissions;
9823: my %new_values;
9824: my %new_control;
9825: my %translation;
9826: my @deletions = ();
9827: my $now = time;
9828: if (exists($$changes{'activate'})) {
9829: if (ref($$changes{'activate'}) eq 'HASH') {
9830: my @newitems = sort(keys(%{$$changes{'activate'}}));
9831: my $numnew = scalar(@newitems);
9832: for (my $i=0; $i<$numnew; $i++) {
9833: my $newkey = $newitems[$i];
9834: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 9835: if ($newkey =~ /^\d+:/) {
9836: $newkey =~ s/^(\d+)/$newid/;
9837: $translation{$1} = $newid;
9838: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
9839: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
9840: $translation{$1} = $newid;
9841: }
1.749 raeburn 9842: $new_values{$file_name."\0".$newkey} =
9843: $$changes{'activate'}{$newitems[$i]};
9844: $new_control{$newkey} = $now;
9845: }
9846: }
9847: }
9848: my %todelete;
9849: my %changed_items;
9850: foreach my $action ('delete','update') {
9851: if (exists($$changes{$action})) {
9852: if (ref($$changes{$action}) eq 'HASH') {
9853: foreach my $key (keys(%{$$changes{$action}})) {
9854: my ($itemnum) = ($key =~ /^([^:]+):/);
9855: if ($action eq 'delete') {
9856: $todelete{$itemnum} = 1;
9857: } else {
9858: $changed_items{$itemnum} = $key;
9859: }
9860: }
1.745 raeburn 9861: }
9862: }
1.749 raeburn 9863: }
9864: # get lock on access controls for file.
9865: my $lockhash = {
9866: $file_name."\0".'locked_access_records' => $env{'user.name'}.
9867: ':'.$env{'user.domain'},
9868: };
9869: my $tries = 0;
9870: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
9871:
1.1288 damieng 9872: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 9873: $tries ++;
1.1289 damieng 9874: sleep(0.1);
1.749 raeburn 9875: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
9876: }
9877: if ($gotlock eq 'ok') {
9878: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
9879: my ($tmp)=keys(%curr_permissions);
9880: if ($tmp=~/^error:/) { undef(%curr_permissions); }
9881: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
9882: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
9883: if (ref($curr_controls) eq 'HASH') {
9884: foreach my $control_item (keys(%{$curr_controls})) {
9885: my ($itemnum) = ($control_item =~ /^([^:]+):/);
9886: if (defined($todelete{$itemnum})) {
9887: push(@deletions,$file_name."\0".$control_item);
9888: } else {
9889: if (defined($changed_items{$itemnum})) {
9890: $new_control{$changed_items{$itemnum}} = $now;
9891: push(@deletions,$file_name."\0".$control_item);
9892: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
9893: } else {
9894: $new_control{$control_item} = $$curr_controls{$control_item};
9895: }
9896: }
1.745 raeburn 9897: }
9898: }
9899: }
1.970 raeburn 9900: my ($group);
9901: if (&is_course($domain,$user)) {
9902: ($group,my $file) = split(/\//,$file_name,2);
9903: }
1.749 raeburn 9904: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
9905: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
9906: $outcome = &put('file_permissions',\%new_values,$domain,$user);
9907: # remove lock
9908: my @del_lock = ($file_name."\0".'locked_access_records');
9909: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 9910: my $sqlresult =
1.970 raeburn 9911: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 9912: $group);
1.749 raeburn 9913: } else {
9914: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 9915: }
1.749 raeburn 9916: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 9917: }
9918:
1.827 raeburn 9919: sub make_public_indefinitely {
1.1271 raeburn 9920: my (@requrl) = @_;
9921: return &automated_portfile_access('public',\@requrl);
9922: }
9923:
9924: sub automated_portfile_access {
9925: my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273 raeburn 9926: unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
9927: return 'invalid';
9928: }
1.1271 raeburn 9929: my %urls;
9930: if (ref($addsref) eq 'ARRAY') {
9931: foreach my $requrl (@{$addsref}) {
9932: if (&is_portfolio_url($requrl)) {
9933: unless (exists($urls{$requrl})) {
9934: $urls{$requrl} = 'add';
9935: }
9936: }
9937: }
9938: }
9939: if (ref($delsref) eq 'ARRAY') {
9940: foreach my $requrl (@{$delsref}) {
9941: if (&is_portfolio_url($requrl)) {
9942: unless (exists($urls{$requrl})) {
9943: $urls{$requrl} = 'delete';
9944: }
9945: }
9946: }
9947: }
9948: unless (keys(%urls)) {
9949: return 'invalid';
9950: }
9951: my $ip;
9952: if ($accesstype eq 'ip') {
9953: if (ref($info) eq 'HASH') {
9954: if ($info->{'ip'} ne '') {
9955: $ip = $info->{'ip'};
9956: }
9957: }
9958: if ($ip eq '') {
9959: return 'invalid';
9960: }
9961: }
9962: my $errors;
1.827 raeburn 9963: my $now = time;
1.1271 raeburn 9964: my %current_perms;
9965: foreach my $requrl (sort(keys(%urls))) {
9966: my $action;
9967: if ($urls{$requrl} eq 'add') {
9968: $action = 'activate';
9969: } else {
9970: $action = 'none';
9971: }
9972: my $aclnum = 0;
1.827 raeburn 9973: my (undef,$udom,$unum,$file_name,$group) =
9974: &parse_portfolio_url($requrl);
1.1271 raeburn 9975: unless (exists($current_perms{$unum.':'.$udom})) {
9976: $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
9977: }
9978: my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827 raeburn 9979: $group,$file_name);
9980: foreach my $key (keys(%{$access_controls{$file_name}})) {
9981: my ($num,$scope,$end,$start) =
9982: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271 raeburn 9983: if ($scope eq $accesstype) {
9984: if (($start <= $now) && ($end == 0)) {
9985: if ($accesstype eq 'ip') {
9986: if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
9987: if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
9988: if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
9989: if ($urls{$requrl} eq 'add') {
9990: $action = 'none';
9991: last;
9992: } else {
9993: $action = 'delete';
9994: $aclnum = $num;
9995: last;
9996: }
9997: }
9998: }
9999: }
10000: } elsif ($accesstype eq 'public') {
10001: if ($urls{$requrl} eq 'add') {
10002: $action = 'none';
10003: last;
10004: } else {
10005: $action = 'delete';
10006: $aclnum = $num;
10007: last;
10008: }
10009: }
10010: } elsif ($accesstype eq 'public') {
1.827 raeburn 10011: $action = 'update';
10012: $aclnum = $num;
1.1271 raeburn 10013: last;
1.827 raeburn 10014: }
10015: }
10016: }
10017: if ($action eq 'none') {
1.1271 raeburn 10018: next;
1.827 raeburn 10019: } else {
10020: my %changes;
10021: my $newend = 0;
10022: my $newstart = $now;
1.1271 raeburn 10023: my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827 raeburn 10024: $changes{$action}{$newkey} = {
1.1271 raeburn 10025: type => $accesstype,
1.827 raeburn 10026: time => {
10027: start => $newstart,
10028: end => $newend,
10029: },
10030: };
1.1271 raeburn 10031: if ($accesstype eq 'ip') {
10032: $changes{$action}{$newkey}{'ip'} = [$ip];
10033: }
1.827 raeburn 10034: my ($outcome,$deloutcome,$new_values,$translation) =
10035: &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271 raeburn 10036: unless ($outcome eq 'ok') {
10037: $errors .= $outcome.' ';
10038: }
1.827 raeburn 10039: }
1.1271 raeburn 10040: }
10041: if ($errors) {
10042: $errors =~ s/\s$//;
10043: return $errors;
1.827 raeburn 10044: } else {
1.1271 raeburn 10045: return 'ok';
1.827 raeburn 10046: }
10047: }
10048:
1.745 raeburn 10049: #------------------------------------------------------Get Marked as Read Only
10050:
10051: sub get_marked_as_readonly {
10052: my ($domain,$user,$what,$group) = @_;
10053: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 10054: my @readonly_files;
1.629 banghart 10055: my $cmp1=$what;
10056: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 10057: while (my ($file_name,$value) = each(%{$current_permissions})) {
10058: if (defined($group)) {
10059: if ($file_name !~ m-^\Q$group\E/-) {
10060: next;
10061: }
10062: }
1.561 banghart 10063: if (ref($value) eq "ARRAY"){
10064: foreach my $stored_what (@{$value}) {
1.629 banghart 10065: my $cmp2=$stored_what;
1.759 albertel 10066: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 10067: $cmp2=join('',@{$stored_what});
1.745 raeburn 10068: }
1.629 banghart 10069: if ($cmp1 eq $cmp2) {
1.561 banghart 10070: push(@readonly_files, $file_name);
1.745 raeburn 10071: last;
1.563 banghart 10072: } elsif (!defined($what)) {
10073: push(@readonly_files, $file_name);
1.745 raeburn 10074: last;
1.561 banghart 10075: }
10076: }
1.745 raeburn 10077: }
1.561 banghart 10078: }
10079: return @readonly_files;
10080: }
1.577 banghart 10081: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 10082:
1.577 banghart 10083: sub get_marked_as_readonly_hash {
1.745 raeburn 10084: my ($current_permissions,$group,$what) = @_;
1.577 banghart 10085: my %readonly_files;
1.745 raeburn 10086: while (my ($file_name,$value) = each(%{$current_permissions})) {
10087: if (defined($group)) {
10088: if ($file_name !~ m-^\Q$group\E/-) {
10089: next;
10090: }
10091: }
1.577 banghart 10092: if (ref($value) eq "ARRAY"){
10093: foreach my $stored_what (@{$value}) {
1.745 raeburn 10094: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 10095: foreach my $lock_descriptor(@{$stored_what}) {
10096: if ($lock_descriptor eq 'graded') {
10097: $readonly_files{$file_name} = 'graded';
10098: } elsif ($lock_descriptor eq 'handback') {
10099: $readonly_files{$file_name} = 'handback';
10100: } else {
10101: if (!exists($readonly_files{$file_name})) {
10102: $readonly_files{$file_name} = 'locked';
10103: }
10104: }
1.745 raeburn 10105: }
1.750 banghart 10106: }
1.577 banghart 10107: }
10108: }
10109: }
10110: return %readonly_files;
10111: }
1.559 banghart 10112: # ------------------------------------------------------------ Unmark as Read Only
10113:
10114: sub unmark_as_readonly {
1.629 banghart 10115: # unmarks $file_name (if $file_name is defined), or all files locked by $what
10116: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 10117: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 10118: $file_name = &declutter_portfile($file_name);
1.634 albertel 10119: my $symb_crs = $what;
10120: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 10121: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 10122: my ($tmp)=keys(%current_permissions);
10123: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 10124: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 10125: foreach my $file (@readonly_files) {
1.759 albertel 10126: my $clean_file = &declutter_portfile($file);
10127: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 10128: my $current_locks = $current_permissions{$file};
1.563 banghart 10129: my @new_locks;
10130: my @del_keys;
10131: if (ref($current_locks) eq "ARRAY"){
10132: foreach my $locker (@{$current_locks}) {
1.632 albertel 10133: my $compare=$locker;
1.749 raeburn 10134: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 10135: $compare=join('',@{$locker});
1.746 raeburn 10136: if ($compare ne $symb_crs) {
10137: push(@new_locks, $locker);
10138: }
1.563 banghart 10139: }
10140: }
1.650 albertel 10141: if (scalar(@new_locks) > 0) {
1.563 banghart 10142: $current_permissions{$file} = \@new_locks;
10143: } else {
10144: push(@del_keys, $file);
1.613 albertel 10145: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 10146: delete($current_permissions{$file});
1.563 banghart 10147: }
10148: }
1.561 banghart 10149: }
1.613 albertel 10150: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 10151: return;
10152: }
1.512 banghart 10153:
1.17 www 10154: # ------------------------------------------------------------ Directory lister
10155:
10156: sub dirlist {
1.955 raeburn 10157: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 10158: $uri=~s/^\///;
10159: $uri=~s/\/$//;
1.253 stredwic 10160: my ($udom, $uname);
1.955 raeburn 10161: if ($getuserdir) {
1.253 stredwic 10162: $udom = $userdomain;
10163: $uname = $username;
1.955 raeburn 10164: } else {
10165: (undef,$udom,$uname)=split(/\//,$uri);
10166: if(defined($userdomain)) {
10167: $udom = $userdomain;
10168: }
10169: if(defined($username)) {
10170: $uname = $username;
10171: }
1.253 stredwic 10172: }
1.955 raeburn 10173: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 10174:
1.955 raeburn 10175: $dirRoot = $perlvar{'lonDocRoot'};
10176: if (defined($getpropath)) {
10177: $dirRoot = &propath($udom,$uname);
1.253 stredwic 10178: $dirRoot =~ s/\/$//;
1.955 raeburn 10179: } elsif (defined($getuserdir)) {
10180: my $subdir=$uname.'__';
10181: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
10182: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
10183: ."/$udom/$subdir/$uname";
10184: } elsif (defined($alternateRoot)) {
10185: $dirRoot = $alternateRoot;
1.751 banghart 10186: }
1.253 stredwic 10187:
10188: if($udom) {
10189: if($uname) {
1.1135 raeburn 10190: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 10191: if ($uhome eq 'no_host') {
10192: return ([],'no_host');
10193: }
1.955 raeburn 10194: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 10195: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 10196: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 10197: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 10198: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 10199: } else {
10200: @listing_results = map { &unescape($_); } split(/:/,$listing);
10201: }
1.605 matthew 10202: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 10203: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 10204: @listing_results = split(/:/,$listing);
10205: } else {
10206: @listing_results = map { &unescape($_); } split(/:/,$listing);
10207: }
1.1135 raeburn 10208: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 10209: ($listing eq 'rejected') || ($listing eq 'refused') ||
10210: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10211: return ([],$listing);
10212: } else {
10213: return (\@listing_results);
1.1135 raeburn 10214: }
1.955 raeburn 10215: } elsif(!$alternateRoot) {
1.1136 raeburn 10216: my (%allusers,%listerror);
1.841 albertel 10217: my %servers = &get_servers($udom,'library');
1.955 raeburn 10218: foreach my $tryserver (keys(%servers)) {
10219: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
10220: &escape($udom),$tryserver);
10221: if ($listing eq 'unknown_cmd') {
10222: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
10223: $udom, $tryserver);
10224: } else {
10225: @listing_results = map { &unescape($_); } split(/:/,$listing);
10226: }
1.841 albertel 10227: if ($listing eq 'unknown_cmd') {
10228: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
10229: $udom, $tryserver);
10230: @listing_results = split(/:/,$listing);
10231: } else {
10232: @listing_results =
10233: map { &unescape($_); } split(/:/,$listing);
10234: }
1.1136 raeburn 10235: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
10236: ($listing eq 'rejected') || ($listing eq 'refused') ||
10237: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10238: $listerror{$tryserver} = $listing;
10239: } else {
1.841 albertel 10240: foreach my $line (@listing_results) {
10241: my ($entry) = split(/&/,$line,2);
10242: $allusers{$entry} = 1;
10243: }
10244: }
1.253 stredwic 10245: }
1.1136 raeburn 10246: my @alluserslist=();
1.800 albertel 10247: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 10248: push(@alluserslist,$user.'&user');
1.253 stredwic 10249: }
1.1318 droeschl 10250:
10251: if (!%listerror) {
10252: # no errors
10253: return (\@alluserslist);
10254: } elsif (scalar(keys(%servers)) == 1) {
10255: # one library server, one error
10256: my ($key) = keys(%listerror);
10257: return (\@alluserslist, $listerror{$key});
10258: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
10259: # con_lost indicates that we might miss data from at least one
10260: # library server
10261: return (\@alluserslist, 'con_lost');
10262: } else {
10263: # multiple library servers and no con_lost -> data should be
10264: # complete.
10265: return (\@alluserslist);
10266: }
10267:
1.253 stredwic 10268: } else {
1.1136 raeburn 10269: return ([],'missing username');
1.253 stredwic 10270: }
1.955 raeburn 10271: } elsif(!defined($getpropath)) {
1.1136 raeburn 10272: my $path = $perlvar{'lonDocRoot'}.'/res/';
10273: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
10274: return (\@all_domains);
1.955 raeburn 10275: } else {
1.1136 raeburn 10276: return ([],'missing domain');
1.275 stredwic 10277: }
10278: }
10279:
10280: # --------------------------------------------- GetFileTimestamp
10281: # This function utilizes dirlist and returns the date stamp for
10282: # when it was last modified. It will also return an error of -1
10283: # if an error occurs
10284:
10285: sub GetFileTimestamp {
1.955 raeburn 10286: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 10287: $studentDomain = &LONCAPA::clean_domain($studentDomain);
10288: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 10289: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
10290: undef,$getuserdir);
10291: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10292: return -1;
10293: }
10294: if (ref($fileref) eq 'ARRAY') {
10295: my @stats = split('&',$fileref->[0]);
1.375 matthew 10296: # @stats contains first the filename, then the stat output
10297: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 10298: } else {
10299: return -1;
1.253 stredwic 10300: }
1.26 www 10301: }
10302:
1.712 albertel 10303: sub stat_file {
10304: my ($uri) = @_;
1.787 albertel 10305: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 10306:
1.955 raeburn 10307: my ($udom,$uname,$file);
1.712 albertel 10308: if ($uri =~ m-^/(uploaded|editupload)/-) {
10309: ($udom,$uname,$file) =
1.811 albertel 10310: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 10311: $file = 'userfiles/'.$file;
10312: }
10313: if ($uri =~ m-^/res/-) {
10314: ($udom,$uname) =
1.807 albertel 10315: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 10316: $file = $uri;
10317: }
10318:
10319: if (!$udom || !$uname || !$file) {
10320: # unable to handle the uri
10321: return ();
10322: }
1.956 raeburn 10323: my $getpropath;
10324: if ($file =~ /^userfiles\//) {
10325: $getpropath = 1;
10326: }
1.1136 raeburn 10327: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
10328: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10329: return ();
10330: } else {
10331: if (ref($listref) eq 'ARRAY') {
10332: my @stats = split('&',$listref->[0]);
10333: shift(@stats); #filename is first
10334: return @stats;
10335: }
1.712 albertel 10336: }
10337: return ();
10338: }
10339:
1.1313 raeburn 10340: # --------------------------------------------------------- recursedirs
10341: # Recursive function to traverse either a specific user's Authoring Space
10342: # or corresponding Published Resource Space, and populate the hash ref:
10343: # $dirhashref with URLs of all directories, and if $filehashref hash
10344: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
10345: # or .rights files in resource space, and .meta, .save, .log, and .bak
10346: # files in Authoring Space.
10347: #
10348: # Inputs:
10349: #
10350: # $is_home - true if current server is home server for user's space
10351: # $context - either: priv, or res respectively for Authoring or Resource Space.
10352: # $docroot - Document root (i.e., /home/httpd/html
10353: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
10354: # $relpath - Current path (relative to top level).
10355: # $dirhashref - reference to hash to populate with URLs of directories (Required)
10356: # $filehashref - reference to hash to populate with URLs of files (Optional)
10357: #
10358: # Returns: nothing
10359: #
10360: # Side Effects: populates $dirhashref, and $filehashref (if provided).
10361: #
10362: # Currently used by interface/londocs.pm to create linked select boxes for
10363: # directory and filename to import a Course "Author" resource into a course, and
10364: # also to create linked select boxes for Authoring Space and Directory to choose
10365: # save location for creation of a new "standard" problem from the Course Editor.
10366: #
10367:
10368: sub recursedirs {
10369: my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
10370: return unless (ref($dirhashref) eq 'HASH');
10371: my $currpath = $docroot.$toppath;
10372: if ($relpath) {
10373: $currpath .= "/$relpath";
10374: }
10375: my $savefile;
10376: if (ref($filehashref)) {
10377: $savefile = 1;
10378: }
10379: if ($is_home) {
10380: if (opendir(my $dirh,$currpath)) {
10381: foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
10382: next if ($item eq '');
10383: if (-d "$currpath/$item") {
10384: my $newpath;
10385: if ($relpath) {
10386: $newpath = "$relpath/$item";
10387: } else {
10388: $newpath = $item;
10389: }
10390: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10391: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10392: } elsif ($savefile) {
10393: if ($context eq 'priv') {
10394: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10395: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10396: }
10397: } else {
10398: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
10399: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10400: }
10401: }
10402: }
10403: }
10404: closedir($dirh);
10405: }
10406: } else {
10407: my ($dirlistref,$listerror) =
10408: &dirlist($toppath.$relpath);
10409: my @dir_lines;
10410: my $dirptr=16384;
10411: if (ref($dirlistref) eq 'ARRAY') {
10412: foreach my $dir_line (sort
10413: {
10414: my ($afile)=split('&',$a,2);
10415: my ($bfile)=split('&',$b,2);
10416: return (lc($afile) cmp lc($bfile));
10417: } (@{$dirlistref})) {
10418: my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
10419: split(/\&/,$dir_line,16);
10420: $item =~ s/\s+$//;
10421: next if (($item =~ /^\.\.?$/) || ($obs));
10422: if ($dirptr&$testdir) {
10423: my $newpath;
10424: if ($relpath) {
10425: $newpath = "$relpath/$item";
10426: } else {
10427: $relpath = '/';
10428: $newpath = $item;
10429: }
10430: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10431: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10432: } elsif ($savefile) {
10433: if ($context eq 'priv') {
10434: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10435: $filehashref->{$relpath}{$item} = 1;
10436: }
10437: } else {
10438: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
10439: $filehashref->{$relpath}{$item} = 1;
10440: }
10441: }
10442: }
10443: }
10444: }
10445: }
10446: return;
10447: }
10448:
1.26 www 10449: # -------------------------------------------------------- Value of a Condition
10450:
1.713 albertel 10451: # gets the value of a specific preevaluated condition
10452: # stored in the string $env{user.state.<cid>}
10453: # or looks up a condition reference in the bighash and if if hasn't
10454: # already been evaluated recurses into docondval to get the value of
10455: # the condition, then memoizing it to
10456: # $env{user.state.<cid>.<condition>}
1.40 www 10457: sub directcondval {
10458: my $number=shift;
1.620 albertel 10459: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 10460: &Apache::lonuserstate::evalstate();
10461: }
1.713 albertel 10462: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
10463: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
10464: } elsif ($number =~ /^_/) {
10465: my $sub_condition;
10466: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
10467: &GDBM_READER(),0640)) {
10468: $sub_condition=$bighash{'conditions'.$number};
10469: untie(%bighash);
10470: }
10471: my $value = &docondval($sub_condition);
1.949 raeburn 10472: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 10473: return $value;
10474: }
1.620 albertel 10475: if ($env{'user.state.'.$env{'request.course.id'}}) {
10476: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 10477: } else {
10478: return 2;
10479: }
10480: }
10481:
1.713 albertel 10482: # get the collection of conditions for this resource
1.26 www 10483: sub condval {
10484: my $condidx=shift;
1.54 www 10485: my $allpathcond='';
1.713 albertel 10486: foreach my $cond (split(/\|/,$condidx)) {
10487: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
10488: $allpathcond.=
10489: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
10490: }
1.191 harris41 10491: }
1.54 www 10492: $allpathcond=~s/\|$//;
1.713 albertel 10493: return &docondval($allpathcond);
10494: }
10495:
10496: #evaluates an expression of conditions
10497: sub docondval {
10498: my ($allpathcond) = @_;
10499: my $result=0;
10500: if ($env{'request.course.id'}
10501: && defined($allpathcond)) {
10502: my $operand='|';
10503: my @stack;
10504: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
10505: if ($chunk eq '(') {
10506: push @stack,($operand,$result);
10507: } elsif ($chunk eq ')') {
10508: my $before=pop @stack;
10509: if (pop @stack eq '&') {
10510: $result=$result>$before?$before:$result;
10511: } else {
10512: $result=$result>$before?$result:$before;
10513: }
10514: } elsif (($chunk eq '&') || ($chunk eq '|')) {
10515: $operand=$chunk;
10516: } else {
10517: my $new=directcondval($chunk);
10518: if ($operand eq '&') {
10519: $result=$result>$new?$new:$result;
10520: } else {
10521: $result=$result>$new?$result:$new;
10522: }
10523: }
10524: }
1.26 www 10525: }
10526: return $result;
1.421 albertel 10527: }
10528:
10529: # ---------------------------------------------------- Devalidate courseresdata
10530:
10531: sub devalidatecourseresdata {
10532: my ($coursenum,$coursedomain)=@_;
10533: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 10534: &devalidate_cache_new('courseres',$hashid);
1.28 www 10535: }
10536:
1.763 www 10537:
1.200 www 10538: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 10539: #
10540: # Parameters:
10541: # $coursenum - Number of the course.
10542: # $coursedomain - Domain at which the course was created.
10543: # Returns:
10544: # A hash of the course parameters along (I think) with timestamps
10545: # and version info.
1.877 foxr 10546:
1.624 albertel 10547: sub get_courseresdata {
10548: my ($coursenum,$coursedomain)=@_;
1.200 www 10549: my $coursehom=&homeserver($coursenum,$coursedomain);
10550: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 10551: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 10552: my %dumpreply;
1.417 albertel 10553: unless (defined($cached)) {
1.624 albertel 10554: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 10555: $result=\%dumpreply;
1.251 albertel 10556: my ($tmp) = keys(%dumpreply);
10557: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 10558: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 10559: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
10560: return $tmp;
1.416 albertel 10561: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 10562: $result=undef;
1.599 albertel 10563: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 10564: }
10565: }
1.624 albertel 10566: return $result;
10567: }
10568:
1.633 albertel 10569: sub devalidateuserresdata {
10570: my ($uname,$udom)=@_;
10571: my $hashid="$udom:$uname";
10572: &devalidate_cache_new('userres',$hashid);
10573: }
10574:
1.624 albertel 10575: sub get_userresdata {
10576: my ($uname,$udom)=@_;
10577: #most student don\'t have any data set, check if there is some data
10578: if (&EXT_cache_status($udom,$uname)) { return undef; }
10579:
10580: my $hashid="$udom:$uname";
10581: my ($result,$cached)=&is_cached_new('userres',$hashid);
10582: if (!defined($cached)) {
10583: my %resourcedata=&dump('resourcedata',$udom,$uname);
10584: $result=\%resourcedata;
10585: &do_cache_new('userres',$hashid,$result,600);
10586: }
10587: my ($tmp)=keys(%$result);
10588: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
10589: return $result;
10590: }
10591: #error 2 occurs when the .db doesn't exist
10592: if ($tmp!~/error: 2 /) {
1.1294 raeburn 10593: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
10594: &logthis("<font color=\"blue\">WARNING:".
10595: " Trying to get resource data for ".
10596: $uname." at ".$udom.": ".
10597: $tmp."</font>");
10598: }
1.624 albertel 10599: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 10600: #&EXT_cache_set($udom,$uname);
10601: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 10602: undef($tmp); # not really an error so don't send it back
1.624 albertel 10603: }
10604: return $tmp;
10605: }
1.879 foxr 10606: #----------------------------------------------- resdata - return resource data
10607: # Purpose:
10608: # Return resource data for either users or for a course.
10609: # Parameters:
10610: # $name - Course/user name.
10611: # $domain - Name of the domain the user/course is registered on.
1.1311 raeburn 10612: # $type - Type of thing $name is (must be 'course' or 'user')
1.1301 raeburn 10613: # $mapp - decluttered URL of enclosing map
10614: # $recursed - Ref to scalar -- set to 1, if nested maps have been recursed.
10615: # $recurseup - Ref to array of map URLs, starting with map containing
10616: # $mapp up through hierarchy of nested maps to top level map.
10617: # $courseid - CourseID (first part of param identifier).
10618: # $modifier - Middle part of param identifier.
10619: # $what - Last part of param identifier.
1.879 foxr 10620: # @which - Array of names of resources desired.
10621: # Returns:
10622: # The value of the first reasource in @which that is found in the
10623: # resource hash.
10624: # Exceptional Conditions:
10625: # If the $type passed in is not valid (not the string 'course' or
10626: # 'user', an undefined reference is returned.
10627: # If none of the resources are found, an undef is returned
1.624 albertel 10628: sub resdata {
1.1301 raeburn 10629: my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
10630: $modifier,$what,@which)=@_;
1.624 albertel 10631: my $result;
10632: if ($type eq 'course') {
10633: $result=&get_courseresdata($name,$domain);
10634: } elsif ($type eq 'user') {
10635: $result=&get_userresdata($name,$domain);
10636: }
10637: if (!ref($result)) { return $result; }
1.251 albertel 10638: foreach my $item (@which) {
1.1301 raeburn 10639: if ($item->[1] eq 'course') {
10640: if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
10641: unless ($$recursed) {
1.1302 raeburn 10642: @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301 raeburn 10643: $$recursed = 1;
10644: }
10645: foreach my $item (@${recurseup}) {
10646: my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
10647: last if (defined($result->{$norecursechk}));
10648: my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
10649: if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
10650: }
10651: }
10652: }
10653: if (defined($result->{$item->[0]})) {
1.927 albertel 10654: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 10655: }
1.250 albertel 10656: }
1.291 albertel 10657: return undef;
1.200 www 10658: }
10659:
1.1298 raeburn 10660: sub get_domain_ltitools {
10661: my ($cdom) = @_;
10662: my %ltitools;
10663: my ($result,$cached)=&is_cached_new('ltitools',$cdom);
10664: if (defined($cached)) {
10665: if (ref($result) eq 'HASH') {
10666: %ltitools = %{$result};
10667: }
10668: } else {
10669: my %domconfig = &get_dom('configuration',['ltitools'],$cdom);
10670: if (ref($domconfig{'ltitools'}) eq 'HASH') {
10671: %ltitools = %{$domconfig{'ltitools'}};
10672: }
10673: my $cachetime = 24*60*60;
10674: &do_cache_new('ltitools',$cdom,\%ltitools,$cachetime);
10675: }
10676: return %ltitools;
10677: }
10678:
1.1236 raeburn 10679: sub get_numsuppfiles {
10680: my ($cnum,$cdom,$ignorecache)=@_;
10681: my $hashid=$cnum.':'.$cdom;
10682: my ($suppcount,$cached);
10683: unless ($ignorecache) {
10684: ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
10685: }
10686: unless (defined($cached)) {
10687: my $chome=&homeserver($cnum,$cdom);
10688: unless ($chome eq 'no_host') {
10689: ($suppcount,my $errors) = (0,0);
10690: my $suppmap = 'supplemental.sequence';
10691: ($suppcount,$errors) =
10692: &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,$errors);
10693: }
10694: &do_cache_new('suppcount',$hashid,$suppcount,600);
10695: }
10696: return $suppcount;
10697: }
10698:
1.379 matthew 10699: #
10700: # EXT resource caching routines
10701: #
10702:
1.1302 raeburn 10703: {
10704: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
10705: #
10706: # The course for which we cache
10707: my $cachedmapkey='';
10708: # The cached recursive maps for this course
10709: my %cachedmaps=();
10710: # When this was last done
10711: my $cachedmaptime='';
10712:
1.379 matthew 10713: sub clear_EXT_cache_status {
1.383 albertel 10714: &delenv('cache.EXT.');
1.379 matthew 10715: }
10716:
10717: sub EXT_cache_status {
10718: my ($target_domain,$target_user) = @_;
1.383 albertel 10719: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 10720: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 10721: # We know already the user has no data
10722: return 1;
10723: } else {
10724: return 0;
10725: }
10726: }
10727:
10728: sub EXT_cache_set {
10729: my ($target_domain,$target_user) = @_;
1.383 albertel 10730: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 10731: #&appenv({$cachename => time});
1.379 matthew 10732: }
10733:
1.28 www 10734: # --------------------------------------------------------- Value of a Variable
1.58 www 10735: sub EXT {
1.715 albertel 10736:
1.1228 raeburn 10737: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 10738: unless ($varname) { return ''; }
1.218 albertel 10739: #get real user name/domain, courseid and symb
10740: my $courseid;
1.359 albertel 10741: my $publicuser;
1.427 www 10742: if ($symbparm) {
10743: $symbparm=&get_symb_from_alias($symbparm);
10744: }
1.218 albertel 10745: if (!($uname && $udom)) {
1.790 albertel 10746: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 10747: if (!$symbparm) { $symbparm=$cursymb; }
10748: } else {
1.620 albertel 10749: $courseid=$env{'request.course.id'};
1.218 albertel 10750: }
1.48 www 10751: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
10752: my $rest;
1.320 albertel 10753: if (defined($therest[0])) {
1.48 www 10754: $rest=join('.',@therest);
10755: } else {
10756: $rest='';
10757: }
1.320 albertel 10758:
1.57 www 10759: my $qualifierrest=$qualifier;
10760: if ($rest) { $qualifierrest.='.'.$rest; }
10761: my $spacequalifierrest=$space;
10762: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 10763: if ($realm eq 'user') {
1.48 www 10764: # --------------------------------------------------------------- user.resource
10765: if ($space eq 'resource') {
1.651 albertel 10766: if ( (defined($Apache::lonhomework::parsing_a_problem)
10767: || defined($Apache::lonhomework::parsing_a_task))
10768: &&
1.744 albertel 10769: ($symbparm eq &symbread()) ) {
10770: # if we are in the middle of processing the resource the
10771: # get the value we are planning on committing
10772: if (defined($Apache::lonhomework::results{$qualifierrest})) {
10773: return $Apache::lonhomework::results{$qualifierrest};
10774: } else {
10775: return $Apache::lonhomework::history{$qualifierrest};
10776: }
1.335 albertel 10777: } else {
1.359 albertel 10778: my %restored;
1.620 albertel 10779: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 10780: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
10781: } else {
10782: %restored=&restore($symbparm,$courseid,$udom,$uname);
10783: }
1.335 albertel 10784: return $restored{$qualifierrest};
10785: }
1.48 www 10786: # ----------------------------------------------------------------- user.access
10787: } elsif ($space eq 'access') {
1.218 albertel 10788: # FIXME - not supporting calls for a specific user
1.48 www 10789: return &allowed($qualifier,$rest);
10790: # ------------------------------------------ user.preferences, user.environment
10791: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 10792: if (($uname eq $env{'user.name'}) &&
10793: ($udom eq $env{'user.domain'})) {
10794: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 10795: } else {
1.359 albertel 10796: my %returnhash;
10797: if (!$publicuser) {
10798: %returnhash=&userenvironment($udom,$uname,
10799: $qualifierrest);
10800: }
1.218 albertel 10801: return $returnhash{$qualifierrest};
10802: }
1.48 www 10803: # ----------------------------------------------------------------- user.course
10804: } elsif ($space eq 'course') {
1.218 albertel 10805: # FIXME - not supporting calls for a specific user
1.620 albertel 10806: return $env{join('.',('request.course',$qualifier))};
1.48 www 10807: # ------------------------------------------------------------------- user.role
10808: } elsif ($space eq 'role') {
1.218 albertel 10809: # FIXME - not supporting calls for a specific user
1.620 albertel 10810: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 10811: if ($qualifier eq 'value') {
10812: return $role;
10813: } elsif ($qualifier eq 'extent') {
10814: return $where;
10815: }
10816: # ----------------------------------------------------------------- user.domain
10817: } elsif ($space eq 'domain') {
1.218 albertel 10818: return $udom;
1.48 www 10819: # ------------------------------------------------------------------- user.name
10820: } elsif ($space eq 'name') {
1.218 albertel 10821: return $uname;
1.48 www 10822: # ---------------------------------------------------- Any other user namespace
1.29 www 10823: } else {
1.359 albertel 10824: my %reply;
10825: if (!$publicuser) {
10826: %reply=&get($space,[$qualifierrest],$udom,$uname);
10827: }
10828: return $reply{$qualifierrest};
1.48 www 10829: }
1.236 www 10830: } elsif ($realm eq 'query') {
10831: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 10832: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
10833: [$spacequalifierrest]);
1.620 albertel 10834: return $env{'form.'.$spacequalifierrest};
1.236 www 10835: } elsif ($realm eq 'request') {
1.48 www 10836: # ------------------------------------------------------------- request.browser
10837: if ($space eq 'browser') {
1.1145 bisitz 10838: return $env{'browser.'.$qualifier};
1.57 www 10839: # ------------------------------------------------------------ request.filename
10840: } else {
1.620 albertel 10841: return $env{'request.'.$spacequalifierrest};
1.29 www 10842: }
1.28 www 10843: } elsif ($realm eq 'course') {
1.48 www 10844: # ---------------------------------------------------------- course.description
1.620 albertel 10845: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 10846: } elsif ($realm eq 'resource') {
1.165 www 10847:
1.620 albertel 10848: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 10849: if (!$symbparm) { $symbparm=&symbread(); }
10850: }
1.693 albertel 10851:
1.1232 raeburn 10852: if ($qualifier eq '') {
10853: if ($space eq 'title') {
10854: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
10855: return &gettitle($symbparm);
10856: }
1.693 albertel 10857:
1.1232 raeburn 10858: if ($space eq 'map') {
10859: my ($map) = &decode_symb($symbparm);
10860: return &symbread($map);
10861: }
10862: if ($space eq 'maptitle') {
10863: my ($map) = &decode_symb($symbparm);
10864: return &gettitle($map);
10865: }
10866: if ($space eq 'filename') {
10867: if ($symbparm) {
10868: return &clutter((&decode_symb($symbparm))[2]);
10869: }
10870: return &hreflocation('',$env{'request.filename'});
1.905 albertel 10871: }
1.1232 raeburn 10872:
1.1233 raeburn 10873: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
10874: if ($space eq 'visibleparts') {
10875: my $navmap = Apache::lonnavmaps::navmap->new();
10876: my $item;
10877: if (ref($navmap)) {
10878: my $res = $navmap->getBySymb($symbparm);
10879: my $parts = $res->parts();
10880: if (ref($parts) eq 'ARRAY') {
10881: $item = join(',',@{$parts});
10882: }
10883: undef($navmap);
1.1232 raeburn 10884: }
1.1233 raeburn 10885: return $item;
1.1232 raeburn 10886: }
10887: }
10888: }
1.693 albertel 10889:
1.1301 raeburn 10890: my ($section, $group, @groups, @recurseup, $recursed);
10891: my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228 raeburn 10892: if (($courseid eq '') && ($cid)) {
10893: $courseid = $cid;
10894: }
10895: if (($symbparm && $courseid) &&
10896: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 10897:
1.218 albertel 10898: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 10899:
1.60 www 10900: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 10901: my $symbp=$symbparm;
1.1301 raeburn 10902: $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 10903: my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301 raeburn 10904: my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218 albertel 10905: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620 albertel 10906: if (($env{'user.name'} eq $uname) &&
10907: ($env{'user.domain'} eq $udom)) {
10908: $section=$env{'request.course.sec'};
1.733 raeburn 10909: @groups = split(/:/,$env{'request.course.groups'});
10910: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 10911: } else {
1.539 albertel 10912: if (! defined($usection)) {
1.551 albertel 10913: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 10914: } else {
10915: $section = $usection;
10916: }
1.733 raeburn 10917: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 10918: }
10919:
10920: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
10921: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301 raeburn 10922: my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218 albertel 10923: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
10924:
1.593 albertel 10925: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 10926: my $courselevelr=$courseid.'.'.$symbparm;
1.1301 raeburn 10927: $courseleveli=$courseid.'.'.$recurseparm;
1.593 albertel 10928: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 10929:
1.60 www 10930: # ----------------------------------------------------------- first, check user
1.624 albertel 10931:
1.1301 raeburn 10932: my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
10933: \@recurseup,$courseid,'.',$spacequalifierrest,
1.927 albertel 10934: ([$courselevelr,'resource'],
10935: [$courselevelm,'map' ],
1.1301 raeburn 10936: [$courseleveli,'map' ],
1.927 albertel 10937: [$courselevel, 'course' ]));
1.931 albertel 10938: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 10939:
1.594 albertel 10940: # ------------------------------------------------ second, check some of course
1.684 raeburn 10941: my $coursereply;
1.691 raeburn 10942: if (@groups > 0) {
10943: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301 raeburn 10944: $recurseparm,$mapparm,$spacequalifierrest,
10945: $mapp,\$recursed,\@recurseup);
10946: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 10947: }
1.96 www 10948:
1.684 raeburn 10949: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 10950: $env{'course.'.$courseid.'.domain'},
1.1301 raeburn 10951: 'course',$mapp,\$recursed,\@recurseup,
10952: $courseid,'.['.$section.'].',$spacequalifierrest,
1.927 albertel 10953: ([$seclevelr, 'resource'],
10954: [$seclevelm, 'map' ],
1.1301 raeburn 10955: [$secleveli, 'map' ],
1.927 albertel 10956: [$seclevel, 'course' ],
10957: [$courselevelr,'resource']));
10958: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 10959:
1.60 www 10960: # ------------------------------------------------------ third, check map parms
1.218 albertel 10961: my %parmhash=();
10962: my $thisparm='';
10963: if (tie(%parmhash,'GDBM_File',
1.620 albertel 10964: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 10965: &GDBM_READER(),0640)) {
1.218 albertel 10966: $thisparm=$parmhash{$symbparm};
10967: untie(%parmhash);
10968: }
1.927 albertel 10969: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 10970: }
1.594 albertel 10971: # ------------------------------------------ fourth, look in resource metadata
1.1301 raeburn 10972:
10973: my $what = $spacequalifierrest;
10974: $what=~s/\./\_/;
1.282 albertel 10975: my $filename;
10976: if (!$symbparm) { $symbparm=&symbread(); }
10977: if ($symbparm) {
1.409 www 10978: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 10979: } else {
1.620 albertel 10980: $filename=$env{'request.filename'};
1.282 albertel 10981: }
1.1301 raeburn 10982: my $metadata=&metadata($filename,$what);
1.927 albertel 10983: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1301 raeburn 10984: $metadata=&metadata($filename,'parameter_'.$what);
1.927 albertel 10985: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 10986:
1.1301 raeburn 10987: # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 10988: if ($symbparm && defined($courseid) &&
1.620 albertel 10989: $courseid eq $env{'request.course.id'}) {
1.624 albertel 10990: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
10991: $env{'course.'.$courseid.'.domain'},
1.1301 raeburn 10992: 'course',$mapp,\$recursed,\@recurseup,
10993: $courseid,'.',$spacequalifierrest,
1.927 albertel 10994: ([$courselevelm,'map' ],
1.1301 raeburn 10995: [$courseleveli,'map' ],
1.927 albertel 10996: [$courselevel, 'course']));
10997: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 10998: }
1.145 www 10999: # ------------------------------------------------------------------ Cascade up
1.218 albertel 11000: unless ($space eq '0') {
1.336 albertel 11001: my @parts=split(/_/,$space);
11002: my $id=pop(@parts);
11003: my $part=join('_',@parts);
11004: if ($part eq '') { $part='0'; }
1.927 albertel 11005: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 11006: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 11007: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 11008: }
1.395 albertel 11009: if ($recurse) { return undef; }
11010: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 11011: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 11012: # ---------------------------------------------------- Any other user namespace
11013: } elsif ($realm eq 'environment') {
11014: # ----------------------------------------------------------------- environment
1.620 albertel 11015: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
11016: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 11017: } else {
1.770 albertel 11018: if ($uname eq 'anonymous' && $udom eq '') {
11019: return '';
11020: }
1.219 albertel 11021: my %returnhash=&userenvironment($udom,$uname,
11022: $spacequalifierrest);
11023: return $returnhash{$spacequalifierrest};
11024: }
1.28 www 11025: } elsif ($realm eq 'system') {
1.48 www 11026: # ----------------------------------------------------------------- system.time
11027: if ($space eq 'time') {
11028: return time;
11029: }
1.696 albertel 11030: } elsif ($realm eq 'server') {
11031: # ----------------------------------------------------------------- system.time
11032: if ($space eq 'name') {
11033: return $ENV{'SERVER_NAME'};
11034: }
1.28 www 11035: }
1.48 www 11036: return '';
1.61 www 11037: }
11038:
1.927 albertel 11039: sub get_reply {
11040: my ($reply_value) = @_;
1.940 raeburn 11041: if (ref($reply_value) eq 'ARRAY') {
11042: if (wantarray) {
11043: return @$reply_value;
11044: }
11045: return $reply_value->[0];
11046: } else {
11047: return $reply_value;
1.927 albertel 11048: }
11049: }
11050:
1.691 raeburn 11051: sub check_group_parms {
1.1301 raeburn 11052: my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
11053: $recursed,$recurseupref) = @_;
11054: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
11055: [$what,'course']);
11056: my $coursereply;
1.691 raeburn 11057: foreach my $group (@{$groups}) {
1.1301 raeburn 11058: my @groupitems = ();
1.691 raeburn 11059: foreach my $level (@levels) {
1.927 albertel 11060: my $item = $courseid.'.['.$group.'].'.$level->[0];
11061: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 11062: }
1.1301 raeburn 11063: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
11064: $env{'course.'.$courseid.'.domain'},
11065: 'course',$mapp,$recursed,$recurseupref,
11066: $courseid,'.['.$group.'].',$what,
11067: @groupitems);
11068: last if (defined($coursereply));
1.691 raeburn 11069: }
11070: return $coursereply;
11071: }
11072:
1.1301 raeburn 11073: sub get_map_hierarchy {
1.1302 raeburn 11074: my ($mapname,$courseid) = @_;
11075: my @recurseup = ();
1.1301 raeburn 11076: if ($mapname) {
1.1302 raeburn 11077: if (($cachedmapkey eq $courseid) &&
11078: (abs($cachedmaptime-time)<5)) {
11079: if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
11080: return @{$cachedmaps{$mapname}};
11081: }
11082: }
1.1301 raeburn 11083: my $navmap = Apache::lonnavmaps::navmap->new();
11084: if (ref($navmap)) {
11085: @recurseup = $navmap->recurseup_maps($mapname);
11086: undef($navmap);
1.1302 raeburn 11087: $cachedmaps{$mapname} = \@recurseup;
11088: $cachedmaptime=time;
11089: $cachedmapkey=$courseid;
1.1301 raeburn 11090: }
11091: }
11092: return @recurseup;
11093: }
11094:
1.1302 raeburn 11095: }
11096:
1.691 raeburn 11097: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 11098: my ($courseid,@groups) = @_;
11099: @groups = sort(@groups);
1.691 raeburn 11100: return @groups;
11101: }
11102:
1.395 albertel 11103: sub packages_tab_default {
11104: my ($uri,$varname)=@_;
11105: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 11106:
11107: my (@extension,@specifics,$do_default);
11108: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 11109: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 11110: if ($pack_type eq 'default') {
11111: $do_default=1;
11112: } elsif ($pack_type eq 'extension') {
11113: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 11114: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 11115: # only look at packages defaults for packages that this id is
1.738 albertel 11116: push(@specifics,[$package,$pack_type,$pack_part]);
11117: }
11118: }
11119: # first look for a package that matches the requested part id
11120: foreach my $package (@specifics) {
11121: my (undef,$pack_type,$pack_part)=@{$package};
11122: next if ($pack_part ne $part);
11123: if (defined($packagetab{"$pack_type&$name&default"})) {
11124: return $packagetab{"$pack_type&$name&default"};
11125: }
11126: }
11127: # look for any possible matching non extension_ package
11128: foreach my $package (@specifics) {
11129: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 11130: if (defined($packagetab{"$pack_type&$name&default"})) {
11131: return $packagetab{"$pack_type&$name&default"};
11132: }
1.585 albertel 11133: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 11134: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
11135: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 11136: }
11137: }
1.738 albertel 11138: # look for any posible extension_ match
11139: foreach my $package (@extension) {
11140: my ($package,$pack_type)=@{$package};
11141: if (defined($packagetab{"$pack_type&$name&default"})) {
11142: return $packagetab{"$pack_type&$name&default"};
11143: }
11144: if (defined($packagetab{$package."&$name&default"})) {
11145: return $packagetab{$package."&$name&default"};
11146: }
11147: }
11148: # look for a global default setting
11149: if ($do_default && defined($packagetab{"default&$name&default"})) {
11150: return $packagetab{"default&$name&default"};
11151: }
1.395 albertel 11152: return undef;
11153: }
11154:
1.334 albertel 11155: sub add_prefix_and_part {
11156: my ($prefix,$part)=@_;
11157: my $keyroot;
11158: if (defined($prefix) && $prefix !~ /^__/) {
11159: # prefix that has a part already
11160: $keyroot=$prefix;
11161: } elsif (defined($prefix)) {
11162: # prefix that is missing a part
11163: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
11164: } else {
11165: # no prefix at all
11166: if (defined($part)) { $keyroot='_'.$part; }
11167: }
11168: return $keyroot;
11169: }
11170:
1.71 www 11171: # ---------------------------------------------------------------- Get metadata
11172:
1.599 albertel 11173: my %metaentry;
1.1070 www 11174: my %importedpartids;
1.71 www 11175: sub metadata {
1.176 www 11176: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 11177: $uri=&declutter($uri);
1.288 albertel 11178: # if it is a non metadata possible uri return quickly
1.529 albertel 11179: if (($uri eq '') ||
11180: (($uri =~ m|^/*adm/|) &&
1.1298 raeburn 11181: ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|exttools?)$})) ||
1.1108 raeburn 11182: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 11183: return undef;
11184: }
1.1261 raeburn 11185: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 11186: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 11187: return undef;
1.288 albertel 11188: }
1.73 www 11189: my $filename=$uri;
11190: $uri=~s/\.meta$//;
1.172 www 11191: #
11192: # Is the metadata already cached?
1.177 www 11193: # Look at timestamp of caching
1.172 www 11194: # Everything is cached by the main uri, libraries are never directly cached
11195: #
1.428 albertel 11196: if (!defined($liburi)) {
1.599 albertel 11197: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 11198: if (defined($cached)) { return $result->{':'.$what}; }
11199: }
11200: {
1.1069 www 11201: # Imported parts would go here
1.1070 www 11202: my %importedids=();
11203: my @origfileimportpartids=();
1.1069 www 11204: my $importedparts=0;
1.172 www 11205: #
11206: # Is this a recursive call for a library?
11207: #
1.599 albertel 11208: # if (! exists($metacache{$uri})) {
11209: # $metacache{$uri}={};
11210: # }
1.924 albertel 11211: my $cachetime = 60*60;
1.171 www 11212: if ($liburi) {
11213: $liburi=&declutter($liburi);
11214: $filename=$liburi;
1.401 bowersj2 11215: } else {
1.599 albertel 11216: &devalidate_cache_new('meta',$uri);
11217: undef(%metaentry);
1.401 bowersj2 11218: }
1.140 www 11219: my %metathesekeys=();
1.73 www 11220: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 11221: my $metastring;
1.1140 www 11222: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 11223: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 11224: $metastring =
1.929 albertel 11225: &Apache::lonnet::ssi_body($which,
1.924 albertel 11226: ('grade_target' => 'meta'));
11227: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 11228: } elsif (($uri !~ m -^(editupload)/-) &&
11229: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 11230: my $file=&filelocation('',&clutter($filename));
1.599 albertel 11231: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 11232: $metastring=&getfile($file);
1.489 albertel 11233: }
1.208 albertel 11234: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 11235: my $token;
1.140 www 11236: undef %metathesekeys;
1.71 www 11237: while ($token=$parser->get_token) {
1.339 albertel 11238: if ($token->[0] eq 'S') {
11239: if (defined($token->[2]->{'package'})) {
1.172 www 11240: #
11241: # This is a package - get package info
11242: #
1.339 albertel 11243: my $package=$token->[2]->{'package'};
11244: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11245: if (defined($token->[2]->{'id'})) {
11246: $keyroot.='_'.$token->[2]->{'id'};
11247: }
1.599 albertel 11248: if ($metaentry{':packages'}) {
11249: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 11250: } else {
1.599 albertel 11251: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 11252: }
1.736 albertel 11253: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 11254: my $part=$keyroot;
11255: $part=~s/^\_//;
1.736 albertel 11256: if ($pack_entry=~/^\Q$package\E\&/ ||
11257: $pack_entry=~/^\Q$package\E_0\&/) {
11258: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 11259: # ignore package.tab specified default values
11260: # here &package_tab_default() will fetch those
11261: if ($subp eq 'default') { next; }
1.736 albertel 11262: my $value=$packagetab{$pack_entry};
1.432 albertel 11263: my $unikey;
11264: if ($pack =~ /_0$/) {
11265: $unikey='parameter_0_'.$name;
11266: $part=0;
11267: } else {
11268: $unikey='parameter'.$keyroot.'_'.$name;
11269: }
1.339 albertel 11270: if ($subp eq 'display') {
11271: $value.=' [Part: '.$part.']';
11272: }
1.599 albertel 11273: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 11274: $metathesekeys{$unikey}=1;
1.599 albertel 11275: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11276: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 11277: }
1.599 albertel 11278: if (defined($metaentry{':'.$unikey.'.default'})) {
11279: $metaentry{':'.$unikey}=
11280: $metaentry{':'.$unikey.'.default'};
1.356 albertel 11281: }
1.339 albertel 11282: }
11283: }
11284: } else {
1.172 www 11285: #
11286: # This is not a package - some other kind of start tag
1.339 albertel 11287: #
11288: my $entry=$token->[1];
1.1068 www 11289: my $unikey='';
1.175 www 11290:
1.339 albertel 11291: if ($entry eq 'import') {
1.175 www 11292: #
11293: # Importing a library here
1.339 albertel 11294: #
1.1067 www 11295: my $location=$parser->get_text('/import');
11296: my $dir=$filename;
11297: $dir=~s|[^/]*$||;
11298: $location=&filelocation($dir,$location);
1.1069 www 11299:
1.1068 www 11300: my $importmode=$token->[2]->{'importmode'};
11301: if ($importmode eq 'problem') {
1.1069 www 11302: # Import as problem/response
1.1068 www 11303: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11304: } elsif ($importmode eq 'part') {
11305: # Import as part(s)
1.1069 www 11306: $importedparts=1;
11307: # We need to get the original file and the imported file to get the part order correct
11308: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
11309: # Load and inspect original file
1.1070 www 11310: if ($#origfileimportpartids<0) {
11311: undef(%importedpartids);
11312: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
11313: my $origfile=&getfile($origfilelocation);
11314: @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11315: }
11316:
1.1069 www 11317: # Load and inspect imported file
11318: my $impfile=&getfile($location);
11319: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11320: if ($#impfilepartids>=0) {
11321: # This problem had parts
1.1070 www 11322: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 11323: } else {
11324: # Importing by turning a single problem into a problem part
11325: # It gets the import-tags ID as part-ID
11326: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 11327: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 11328: }
1.1068 www 11329: } else {
11330: # Normal import
11331: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11332: if (defined($token->[2]->{'id'})) {
11333: $unikey.='_'.$token->[2]->{'id'};
11334: }
1.1067 www 11335: }
11336:
1.339 albertel 11337: if ($depthcount<20) {
1.736 albertel 11338: my $metadata =
11339: &metadata($uri,'keys', $location,$unikey,
11340: $depthcount+1);
11341: foreach my $meta (split(',',$metadata)) {
11342: $metaentry{':'.$meta}=$metaentry{':'.$meta};
11343: $metathesekeys{$meta}=1;
1.339 albertel 11344: }
1.1068 www 11345:
11346: }
1.1067 www 11347: } else {
11348: #
11349: # Not importing, some other kind of non-package, non-library start tag
11350: #
11351: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
11352: if (defined($token->[2]->{'id'})) {
11353: $unikey.='_'.$token->[2]->{'id'};
11354: }
1.339 albertel 11355: if (defined($token->[2]->{'name'})) {
11356: $unikey.='_'.$token->[2]->{'name'};
11357: }
11358: $metathesekeys{$unikey}=1;
1.736 albertel 11359: foreach my $param (@{$token->[3]}) {
11360: $metaentry{':'.$unikey.'.'.$param} =
11361: $token->[2]->{$param};
1.339 albertel 11362: }
11363: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 11364: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 11365: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
11366: # only ws inside the tag, and not in default, so use default
11367: # as value
1.599 albertel 11368: $metaentry{':'.$unikey}=$default;
1.908 albertel 11369: } elsif ( $internaltext =~ /\S/ ) {
11370: # something interesting inside the tag
11371: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 11372: } else {
1.908 albertel 11373: # no interesting values, don't set a default
1.339 albertel 11374: }
1.172 www 11375: # end of not-a-package not-a-library import
1.339 albertel 11376: }
1.172 www 11377: # end of not-a-package start tag
1.339 albertel 11378: }
1.172 www 11379: # the next is the end of "start tag"
1.339 albertel 11380: }
11381: }
1.483 albertel 11382: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 11383: $extension = lc($extension);
11384: if ($extension eq 'htm') { $extension='html'; }
11385:
1.737 albertel 11386: foreach my $key (keys(%packagetab)) {
1.483 albertel 11387: #no specific packages #how's our extension
11388: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 11389: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 11390: \%metathesekeys);
11391: }
1.883 albertel 11392:
11393: if (!exists($metaentry{':packages'})
11394: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 11395: foreach my $key (keys(%packagetab)) {
1.483 albertel 11396: #no specific packages well let's get default then
11397: if ($key!~/^default&/) { next; }
1.488 albertel 11398: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 11399: \%metathesekeys);
11400: }
11401: }
1.338 www 11402: # are there custom rights to evaluate
1.599 albertel 11403: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 11404:
1.338 www 11405: #
11406: # Importing a rights file here
1.339 albertel 11407: #
11408: unless ($depthcount) {
1.599 albertel 11409: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 11410: my $dir=$filename;
11411: $dir=~s|[^/]*$||;
11412: $location=&filelocation($dir,$location);
1.736 albertel 11413: my $rights_metadata =
11414: &metadata($uri,'keys',$location,'_rights',
11415: $depthcount+1);
11416: foreach my $rights (split(',',$rights_metadata)) {
11417: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
11418: $metathesekeys{$rights}=1;
1.339 albertel 11419: }
11420: }
11421: }
1.737 albertel 11422: # uniqifiy package listing
11423: my %seen;
11424: my @uniq_packages =
11425: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
11426: $metaentry{':packages'} = join(',',@uniq_packages);
11427:
1.1070 www 11428: if ($importedparts) {
11429: # We had imported parts and need to rebuild partorder
11430: $metaentry{':partorder'}='';
11431: $metathesekeys{'partorder'}=1;
11432: for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
11433: if ($origfileimportpartids[$index] eq 'part') {
11434: # original part, part of the problem
11435: $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
11436: } else {
11437: # we have imported parts at this position
11438: $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
11439: }
11440: }
11441: $metaentry{':partorder'}=~s/^\,//;
11442: }
11443:
1.737 albertel 11444: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 11445: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274 raeburn 11446: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 11447: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 11448: # this is the end of "was not already recently cached
1.71 www 11449: }
1.599 albertel 11450: return $metaentry{':'.$what};
1.261 albertel 11451: }
11452:
1.488 albertel 11453: sub metadata_create_package_def {
1.483 albertel 11454: my ($uri,$key,$package,$metathesekeys)=@_;
11455: my ($pack,$name,$subp)=split(/\&/,$key);
11456: if ($subp eq 'default') { next; }
11457:
1.599 albertel 11458: if (defined($metaentry{':packages'})) {
11459: $metaentry{':packages'}.=','.$package;
1.483 albertel 11460: } else {
1.599 albertel 11461: $metaentry{':packages'}=$package;
1.483 albertel 11462: }
11463: my $value=$packagetab{$key};
11464: my $unikey;
11465: $unikey='parameter_0_'.$name;
1.599 albertel 11466: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 11467: $$metathesekeys{$unikey}=1;
1.599 albertel 11468: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11469: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 11470: }
1.599 albertel 11471: if (defined($metaentry{':'.$unikey.'.default'})) {
11472: $metaentry{':'.$unikey}=
11473: $metaentry{':'.$unikey.'.default'};
1.483 albertel 11474: }
11475: }
11476:
1.261 albertel 11477: sub metadata_generate_part0 {
11478: my ($metadata,$metacache,$uri) = @_;
11479: my %allnames;
1.737 albertel 11480: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 11481: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 11482: my $part=$$metacache{':'.$metakey.'.part'};
11483: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 11484: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 11485: $allnames{$name}=$part;
11486: }
11487: }
11488: }
11489: foreach my $name (keys(%allnames)) {
11490: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 11491: my $key=":parameter_0_$name";
1.261 albertel 11492: $$metacache{"$key.part"}='0';
11493: $$metacache{"$key.name"}=$name;
1.428 albertel 11494: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 11495: $allnames{$name}.'_'.$name.
11496: '.type'};
1.428 albertel 11497: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 11498: '.display'};
1.644 www 11499: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 11500: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 11501: $$metacache{"$key.display"}=$olddis;
11502: }
1.71 www 11503: }
11504:
1.764 albertel 11505: # ------------------------------------------------------ Devalidate title cache
11506:
11507: sub devalidate_title_cache {
11508: my ($url)=@_;
11509: if (!$env{'request.course.id'}) { return; }
11510: my $symb=&symbread($url);
11511: if (!$symb) { return; }
11512: my $key=$env{'request.course.id'}."\0".$symb;
11513: &devalidate_cache_new('title',$key);
11514: }
11515:
1.1014 droeschl 11516: # ------------------------------------------------- Get the title of a course
11517:
11518: sub current_course_title {
11519: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
11520: }
1.301 www 11521: # ------------------------------------------------- Get the title of a resource
11522:
11523: sub gettitle {
11524: my $urlsymb=shift;
11525: my $symb=&symbread($urlsymb);
1.534 albertel 11526: if ($symb) {
1.620 albertel 11527: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 11528: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 11529: if (defined($cached)) {
11530: return $result;
11531: }
1.534 albertel 11532: my ($map,$resid,$url)=&decode_symb($symb);
11533: my $title='';
1.907 albertel 11534: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
11535: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
11536: } else {
11537: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11538: &GDBM_READER(),0640)) {
11539: my $mapid=$bighash{'map_pc_'.&clutter($map)};
11540: $title=$bighash{'title_'.$mapid.'.'.$resid};
11541: untie(%bighash);
11542: }
1.534 albertel 11543: }
11544: $title=~s/\&colon\;/\:/gs;
11545: if ($title) {
1.1159 www 11546: # Remember both $symb and $title for dynamic metadata
11547: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 11548: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 11549: # Cache this title and then return it
1.599 albertel 11550: return &do_cache_new('title',$key,$title,600);
1.534 albertel 11551: }
11552: $urlsymb=$url;
11553: }
11554: my $title=&metadata($urlsymb,'title');
11555: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
11556: return $title;
1.301 www 11557: }
1.613 albertel 11558:
1.614 albertel 11559: sub get_slot {
11560: my ($which,$cnum,$cdom)=@_;
11561: if (!$cnum || !$cdom) {
1.790 albertel 11562: (undef,my $courseid)=&whichuser();
1.620 albertel 11563: $cdom=$env{'course.'.$courseid.'.domain'};
11564: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 11565: }
1.703 albertel 11566: my $key=join("\0",'slots',$cdom,$cnum,$which);
11567: my %slotinfo;
11568: if (exists($remembered{$key})) {
11569: $slotinfo{$which} = $remembered{$key};
11570: } else {
11571: %slotinfo=&get('slots',[$which],$cdom,$cnum);
11572: &Apache::lonhomework::showhash(%slotinfo);
11573: my ($tmp)=keys(%slotinfo);
11574: if ($tmp=~/^error:/) { return (); }
11575: $remembered{$key} = $slotinfo{$which};
11576: }
1.616 albertel 11577: if (ref($slotinfo{$which}) eq 'HASH') {
11578: return %{$slotinfo{$which}};
11579: }
11580: return $slotinfo{$which};
1.614 albertel 11581: }
1.1150 raeburn 11582:
11583: sub get_reservable_slots {
11584: my ($cnum,$cdom,$uname,$udom) = @_;
11585: my $now = time;
11586: my $reservable_info;
11587: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
11588: if (exists($remembered{$key})) {
11589: $reservable_info = $remembered{$key};
11590: } else {
11591: my %resv;
11592: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
11593: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
11594: $reservable_info = \%resv;
11595: $remembered{$key} = $reservable_info;
11596: }
11597: return $reservable_info;
11598: }
11599:
11600: sub get_course_slots {
11601: my ($cnum,$cdom) = @_;
11602: my $hashid=$cnum.':'.$cdom;
11603: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
11604: if (defined($cached)) {
11605: if (ref($result) eq 'HASH') {
11606: return %{$result};
11607: }
11608: } else {
11609: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
11610: my ($tmp) = keys(%slots);
11611: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219 raeburn 11612: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 11613: return %slots;
11614: }
11615: }
11616: return;
11617: }
11618:
11619: sub devalidate_slots_cache {
11620: my ($cnum,$cdom)=@_;
11621: my $hashid=$cnum.':'.$cdom;
11622: &devalidate_cache_new('allslots',$hashid);
11623: }
11624:
1.1181 raeburn 11625: sub get_coursechange {
11626: my ($cdom,$cnum) = @_;
11627: if ($cdom eq '' || $cnum eq '') {
11628: return unless ($env{'request.course.id'});
11629: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
11630: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
11631: }
11632: my $hashid=$cdom.'_'.$cnum;
11633: my ($change,$cached)=&is_cached_new('crschange',$hashid);
11634: if ((defined($cached)) && ($change ne '')) {
11635: return $change;
11636: } else {
11637: my %crshash;
11638: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
11639: if ($crshash{'internal.contentchange'} eq '') {
11640: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
11641: if ($change eq '') {
11642: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
11643: $change = $crshash{'internal.created'};
11644: }
11645: } else {
11646: $change = $crshash{'internal.contentchange'};
11647: }
11648: my $cachetime = 600;
11649: &do_cache_new('crschange',$hashid,$change,$cachetime);
11650: }
11651: return $change;
11652: }
11653:
11654: sub devalidate_coursechange_cache {
11655: my ($cnum,$cdom)=@_;
11656: my $hashid=$cnum.':'.$cdom;
11657: &devalidate_cache_new('crschange',$hashid);
11658: }
11659:
1.31 www 11660: # ------------------------------------------------- Update symbolic store links
11661:
11662: sub symblist {
11663: my ($mapname,%newhash)=@_;
1.438 www 11664: $mapname=&deversion(&declutter($mapname));
1.31 www 11665: my %hash;
1.620 albertel 11666: if (($env{'request.course.fn'}) && (%newhash)) {
11667: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 11668: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 11669: foreach my $url (keys(%newhash)) {
1.711 albertel 11670: next if ($url eq 'last_known'
11671: && $env{'form.no_update_last_known'});
11672: $hash{declutter($url)}=&encode_symb($mapname,
11673: $newhash{$url}->[1],
11674: $newhash{$url}->[0]);
1.191 harris41 11675: }
1.31 www 11676: if (untie(%hash)) {
11677: return 'ok';
11678: }
11679: }
11680: }
11681: return 'error';
1.212 www 11682: }
11683:
11684: # --------------------------------------------------------------- Verify a symb
11685:
11686: sub symbverify {
1.1190 raeburn 11687: my ($symb,$thisurl,$encstate)=@_;
1.510 www 11688: my $thisfn=$thisurl;
1.439 www 11689: $thisfn=&declutter($thisfn);
1.215 www 11690: # direct jump to resource in page or to a sequence - will construct own symbs
11691: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
11692: # check URL part
1.409 www 11693: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 11694:
1.431 www 11695: unless ($url eq $thisfn) { return 0; }
1.213 www 11696:
1.216 www 11697: $symb=&symbclean($symb);
1.510 www 11698: $thisurl=&deversion($thisurl);
1.439 www 11699: $thisfn=&deversion($thisfn);
1.213 www 11700:
11701: my %bighash;
11702: my $okay=0;
1.431 www 11703:
1.620 albertel 11704: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 11705: &GDBM_READER(),0640)) {
1.1204 raeburn 11706: my $noclutter;
1.1032 raeburn 11707: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
11708: $thisurl =~ s/\?.+$//;
1.1204 raeburn 11709: if ($map =~ m{^uploaded/.+\.page$}) {
11710: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
11711: $thisurl =~ s{^\Qhttp://https://\E}{https://};
11712: $noclutter = 1;
11713: }
11714: }
11715: my $ids;
11716: if ($noclutter) {
11717: $ids=$bighash{'ids_'.$thisurl};
11718: } else {
11719: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 11720: }
1.1102 raeburn 11721: unless ($ids) {
11722: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
11723: $ids=$bighash{$idkey};
1.216 www 11724: }
11725: if ($ids) {
11726: # ------------------------------------------------------------------- Has ID(s)
1.1202 raeburn 11727: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203 raeburn 11728: $symb =~ s/\?.+$//;
1.1202 raeburn 11729: }
1.800 albertel 11730: foreach my $id (split(/\,/,$ids)) {
11731: my ($mapid,$resid)=split(/\./,$id);
1.216 www 11732: if (
11733: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190 raeburn 11734: eq $symb) {
11735: if (ref($encstate)) {
11736: $$encstate = $bighash{'encrypted_'.$id};
11737: }
1.620 albertel 11738: if (($env{'request.role.adv'}) ||
1.1101 raeburn 11739: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
11740: ($thisurl eq '/adm/navmaps')) {
1.1190 raeburn 11741: $okay=1;
1.1202 raeburn 11742: last;
1.582 albertel 11743: }
11744: }
1.216 www 11745: }
11746: }
1.213 www 11747: untie(%bighash);
11748: }
11749: return $okay;
1.31 www 11750: }
11751:
1.210 www 11752: # --------------------------------------------------------------- Clean-up symb
11753:
11754: sub symbclean {
11755: my $symb=shift;
1.568 albertel 11756: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 11757: # remove version from map
11758: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 11759:
1.210 www 11760: # remove version from URL
11761: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 11762:
1.507 www 11763: # remove wrapper
11764:
1.510 www 11765: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 11766: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 11767: return $symb;
1.409 www 11768: }
11769:
11770: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 11771:
11772: sub encode_symb {
11773: my ($map,$resid,$url)=@_;
11774: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
11775: }
1.409 www 11776:
11777: sub decode_symb {
1.568 albertel 11778: my $symb=shift;
11779: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
11780: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 11781: return (&fixversion($map),$resid,&fixversion($url));
11782: }
11783:
11784: sub fixversion {
11785: my $fn=shift;
1.609 banghart 11786: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 11787: my %bighash;
11788: my $uri=&clutter($fn);
1.620 albertel 11789: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 11790: # is this cached?
1.599 albertel 11791: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 11792: if (defined($cached)) { return $result; }
11793: # unfortunately not cached, or expired
1.620 albertel 11794: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 11795: &GDBM_READER(),0640)) {
11796: if ($bighash{'version_'.$uri}) {
11797: my $version=$bighash{'version_'.$uri};
1.444 www 11798: unless (($version eq 'mostrecent') ||
11799: ($version==&getversion($uri))) {
1.440 www 11800: $uri=~s/\.(\w+)$/\.$version\.$1/;
11801: }
11802: }
11803: untie %bighash;
1.413 www 11804: }
1.599 albertel 11805: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 11806: }
11807:
11808: sub deversion {
11809: my $url=shift;
11810: $url=~s/\.\d+\.(\w+)$/\.$1/;
11811: return $url;
1.210 www 11812: }
11813:
1.31 www 11814: # ------------------------------------------------------ Return symb list entry
11815:
11816: sub symbread {
1.1282 raeburn 11817: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
1.1265 raeburn 11818: my $cache_str='request.symbread.cached.'.$thisfn;
1.1282 raeburn 11819: if (defined($env{$cache_str})) {
11820: if ($ignorecachednull) {
11821: return $env{$cache_str} unless ($env{$cache_str} eq '');
11822: } else {
11823: return $env{$cache_str};
11824: }
11825: }
1.242 www 11826: # no filename provided? try from environment
1.1265 raeburn 11827: unless ($thisfn) {
1.620 albertel 11828: if ($env{'request.symb'}) {
11829: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 11830: }
1.620 albertel 11831: $thisfn=$env{'request.filename'};
1.44 www 11832: }
1.569 albertel 11833: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 11834: # is that filename actually a symb? Verify, clean, and return
11835: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 11836: if (&symbverify($thisfn,$1)) {
1.620 albertel 11837: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 11838: }
1.242 www 11839: }
1.44 www 11840: $thisfn=declutter($thisfn);
1.31 www 11841: my %hash;
1.37 www 11842: my %bighash;
11843: my $syval='';
1.620 albertel 11844: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 11845: my $targetfn = $thisfn;
1.609 banghart 11846: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 11847: $targetfn = 'adm/wrapper/'.$thisfn;
11848: }
1.687 albertel 11849: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
11850: $targetfn=$1;
11851: }
1.620 albertel 11852: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 11853: &GDBM_READER(),0640)) {
1.481 raeburn 11854: $syval=$hash{$targetfn};
1.37 www 11855: untie(%hash);
11856: }
11857: # ---------------------------------------------------------- There was an entry
11858: if ($syval) {
1.601 albertel 11859: #unless ($syval=~/\_\d+$/) {
1.620 albertel 11860: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 11861: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 11862: #return $env{$cache_str}='';
1.601 albertel 11863: #}
11864: #$syval.=$1;
11865: #}
1.37 www 11866: } else {
11867: # ------------------------------------------------------- Was not in symb table
1.620 albertel 11868: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 11869: &GDBM_READER(),0640)) {
1.37 www 11870: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 11871: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 11872: unless ($ids) {
11873: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 11874: }
11875: unless ($ids) {
11876: # alias?
11877: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 11878: }
1.37 www 11879: if ($ids) {
11880: # ------------------------------------------------------------------- Has ID(s)
11881: my @possibilities=split(/\,/,$ids);
1.39 www 11882: if ($#possibilities==0) {
11883: # ----------------------------------------------- There is only one possibility
1.37 www 11884: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 11885: $syval=&encode_symb($bighash{'map_id_'.$mapid},
11886: $resid,$thisfn);
1.1282 raeburn 11887: if (ref($possibles) eq 'HASH') {
11888: $possibles->{$syval} = 1;
11889: }
11890: if ($checkforblock) {
11891: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
11892: if (@blockers) {
11893: $syval = '';
11894: return;
11895: }
11896: }
11897: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 11898: # ------------------------------------------ There is more than one possibility
11899: my $realpossible=0;
1.800 albertel 11900: foreach my $id (@possibilities) {
11901: my $file=$bighash{'src_'.$id};
1.1282 raeburn 11902: my $canaccess;
11903: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
11904: $canaccess = 1;
11905: } else {
11906: $canaccess = &allowed('bre',$file);
11907: }
11908: if ($canaccess) {
11909: my ($mapid,$resid)=split(/\./,$id);
11910: if ($bighash{'map_type_'.$mapid} ne 'page') {
11911: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
11912: $resid,$thisfn);
11913: if (ref($possibles) eq 'HASH') {
11914: $possibles->{$syval} = 1;
11915: }
11916: if ($checkforblock) {
11917: my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
11918: unless (@blockers > 0) {
11919: $syval = $poss_syval;
11920: $realpossible++;
11921: }
11922: } else {
11923: $syval = $poss_syval;
11924: $realpossible++;
11925: }
11926: }
1.39 www 11927: }
1.191 harris41 11928: }
1.39 www 11929: if ($realpossible!=1) { $syval=''; }
1.249 www 11930: } else {
11931: $syval='';
1.37 www 11932: }
11933: }
1.1282 raeburn 11934: untie(%bighash);
1.481 raeburn 11935: }
1.31 www 11936: }
1.62 www 11937: if ($syval) {
1.620 albertel 11938: return $env{$cache_str}=$syval;
1.62 www 11939: }
1.31 www 11940: }
1.949 raeburn 11941: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 11942: return $env{$cache_str}='';
1.31 www 11943: }
11944:
11945: # ---------------------------------------------------------- Return random seed
11946:
1.32 www 11947: sub numval {
11948: my $txt=shift;
11949: $txt=~tr/A-J/0-9/;
11950: $txt=~tr/a-j/0-9/;
11951: $txt=~tr/K-T/0-9/;
11952: $txt=~tr/k-t/0-9/;
11953: $txt=~tr/U-Z/0-5/;
11954: $txt=~tr/u-z/0-5/;
11955: $txt=~s/\D//g;
1.564 albertel 11956: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 11957: return int($txt);
1.368 albertel 11958: }
11959:
1.484 albertel 11960: sub numval2 {
11961: my $txt=shift;
11962: $txt=~tr/A-J/0-9/;
11963: $txt=~tr/a-j/0-9/;
11964: $txt=~tr/K-T/0-9/;
11965: $txt=~tr/k-t/0-9/;
11966: $txt=~tr/U-Z/0-5/;
11967: $txt=~tr/u-z/0-5/;
11968: $txt=~s/\D//g;
11969: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
11970: my $total;
11971: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 11972: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 11973: return int($total);
11974: }
11975:
1.575 albertel 11976: sub numval3 {
11977: use integer;
11978: my $txt=shift;
11979: $txt=~tr/A-J/0-9/;
11980: $txt=~tr/a-j/0-9/;
11981: $txt=~tr/K-T/0-9/;
11982: $txt=~tr/k-t/0-9/;
11983: $txt=~tr/U-Z/0-5/;
11984: $txt=~tr/u-z/0-5/;
11985: $txt=~s/\D//g;
11986: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
11987: my $total;
11988: foreach my $val (@txts) { $total+=$val; }
11989: if ($_64bit) { $total=(($total<<32)>>32); }
11990: return $total;
11991: }
11992:
1.675 albertel 11993: sub digest {
11994: my ($data)=@_;
11995: my $digest=&Digest::MD5::md5($data);
11996: my ($a,$b,$c,$d)=unpack("iiii",$digest);
11997: my ($e,$f);
11998: {
11999: use integer;
12000: $e=($a+$b);
12001: $f=($c+$d);
12002: if ($_64bit) {
12003: $e=(($e<<32)>>32);
12004: $f=(($f<<32)>>32);
12005: }
12006: }
12007: if (wantarray) {
12008: return ($e,$f);
12009: } else {
12010: my $g;
12011: {
12012: use integer;
12013: $g=($e+$f);
12014: if ($_64bit) {
12015: $g=(($g<<32)>>32);
12016: }
12017: }
12018: return $g;
12019: }
12020: }
12021:
1.368 albertel 12022: sub latest_rnd_algorithm_id {
1.675 albertel 12023: return '64bit5';
1.366 albertel 12024: }
1.32 www 12025:
1.503 albertel 12026: sub get_rand_alg {
12027: my ($courseid)=@_;
1.790 albertel 12028: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 12029: if ($courseid) {
1.620 albertel 12030: return $env{"course.$courseid.rndseed"};
1.503 albertel 12031: }
12032: return &latest_rnd_algorithm_id();
12033: }
12034:
1.562 albertel 12035: sub validCODE {
12036: my ($CODE)=@_;
12037: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
12038: return 0;
12039: }
12040:
1.491 albertel 12041: sub getCODE {
1.620 albertel 12042: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 12043: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
12044: defined($Apache::lonhomework::parsing_a_task) ) &&
12045: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 12046: return $Apache::lonhomework::history{'resource.CODE'};
12047: }
12048: return undef;
12049: }
1.1133 foxr 12050: #
12051: # Determines the random seed for a specific context:
12052: #
12053: # parameters:
12054: # symb - in course context the symb for the seed.
12055: # course_id - The course id of the form domain_coursenum.
12056: # domain - Domain for the user.
12057: # course - Course for the user.
12058: # cenv - environment of the course.
12059: #
12060: # NOTE:
12061: # All parameters are picked out of the environment if missing
12062: # or not defined.
12063: # If a symb cannot be determined the current time is used instead.
12064: #
12065: # For a given well defined symb, courside, domain, username,
12066: # and course environment, the seed is reproducible.
12067: #
1.31 www 12068: sub rndseed {
1.1133 foxr 12069: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 12070: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 12071: if (!defined($symb)) {
1.366 albertel 12072: unless ($symb=$wsymb) { return time; }
12073: }
1.1146 foxr 12074: if (!defined $courseid) {
12075: $courseid=$wcourseid;
12076: }
12077: if (!defined $domain) { $domain=$wdomain; }
12078: if (!defined $username) { $username=$wusername }
1.1133 foxr 12079:
12080: my $which;
12081: if (defined($cenv->{'rndseed'})) {
12082: $which = $cenv->{'rndseed'};
12083: } else {
12084: $which =&get_rand_alg($courseid);
12085: }
1.491 albertel 12086: if (defined(&getCODE())) {
1.1133 foxr 12087:
1.675 albertel 12088: if ($which eq '64bit5') {
12089: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
12090: } elsif ($which eq '64bit4') {
1.575 albertel 12091: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
12092: } else {
12093: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
12094: }
1.675 albertel 12095: } elsif ($which eq '64bit5') {
12096: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 12097: } elsif ($which eq '64bit4') {
12098: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 12099: } elsif ($which eq '64bit3') {
12100: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 12101: } elsif ($which eq '64bit2') {
12102: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 12103: } elsif ($which eq '64bit') {
12104: return &rndseed_64bit($symb,$courseid,$domain,$username);
12105: }
12106: return &rndseed_32bit($symb,$courseid,$domain,$username);
12107: }
12108:
12109: sub rndseed_32bit {
12110: my ($symb,$courseid,$domain,$username)=@_;
12111: {
12112: use integer;
12113: my $symbchck=unpack("%32C*",$symb) << 27;
12114: my $symbseed=numval($symb) << 22;
12115: my $namechck=unpack("%32C*",$username) << 17;
12116: my $nameseed=numval($username) << 12;
12117: my $domainseed=unpack("%32C*",$domain) << 7;
12118: my $courseseed=unpack("%32C*",$courseid);
12119: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 12120: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12121: #&logthis("rndseed :$num:$symb");
1.564 albertel 12122: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 12123: return $num;
12124: }
12125: }
12126:
12127: sub rndseed_64bit {
12128: my ($symb,$courseid,$domain,$username)=@_;
12129: {
12130: use integer;
12131: my $symbchck=unpack("%32S*",$symb) << 21;
12132: my $symbseed=numval($symb) << 10;
12133: my $namechck=unpack("%32S*",$username);
12134:
12135: my $nameseed=numval($username) << 21;
12136: my $domainseed=unpack("%32S*",$domain) << 10;
12137: my $courseseed=unpack("%32S*",$courseid);
12138:
12139: my $num1=$symbchck+$symbseed+$namechck;
12140: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12141: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12142: #&logthis("rndseed :$num:$symb");
1.564 albertel 12143: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 12144: return "$num1,$num2";
1.155 albertel 12145: }
1.366 albertel 12146: }
12147:
1.443 albertel 12148: sub rndseed_64bit2 {
12149: my ($symb,$courseid,$domain,$username)=@_;
12150: {
12151: use integer;
12152: # strings need to be an even # of cahracters long, it it is odd the
12153: # last characters gets thrown away
12154: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12155: my $symbseed=numval($symb) << 10;
12156: my $namechck=unpack("%32S*",$username.' ');
12157:
12158: my $nameseed=numval($username) << 21;
1.501 albertel 12159: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12160: my $courseseed=unpack("%32S*",$courseid.' ');
12161:
12162: my $num1=$symbchck+$symbseed+$namechck;
12163: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12164: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12165: #&logthis("rndseed :$num:$symb");
1.803 albertel 12166: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 12167: return "$num1,$num2";
12168: }
12169: }
12170:
12171: sub rndseed_64bit3 {
12172: my ($symb,$courseid,$domain,$username)=@_;
12173: {
12174: use integer;
12175: # strings need to be an even # of cahracters long, it it is odd the
12176: # last characters gets thrown away
12177: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12178: my $symbseed=numval2($symb) << 10;
12179: my $namechck=unpack("%32S*",$username.' ');
12180:
12181: my $nameseed=numval2($username) << 21;
1.443 albertel 12182: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12183: my $courseseed=unpack("%32S*",$courseid.' ');
12184:
12185: my $num1=$symbchck+$symbseed+$namechck;
12186: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12187: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12188: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 12189: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 12190:
1.503 albertel 12191: return "$num1:$num2";
1.443 albertel 12192: }
12193: }
12194:
1.575 albertel 12195: sub rndseed_64bit4 {
12196: my ($symb,$courseid,$domain,$username)=@_;
12197: {
12198: use integer;
12199: # strings need to be an even # of cahracters long, it it is odd the
12200: # last characters gets thrown away
12201: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12202: my $symbseed=numval3($symb) << 10;
12203: my $namechck=unpack("%32S*",$username.' ');
12204:
12205: my $nameseed=numval3($username) << 21;
12206: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12207: my $courseseed=unpack("%32S*",$courseid.' ');
12208:
12209: my $num1=$symbchck+$symbseed+$namechck;
12210: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12211: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12212: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 12213: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 12214:
1.575 albertel 12215: return "$num1:$num2";
12216: }
12217: }
12218:
1.675 albertel 12219: sub rndseed_64bit5 {
12220: my ($symb,$courseid,$domain,$username)=@_;
12221: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
12222: return "$num1:$num2";
12223: }
12224:
1.366 albertel 12225: sub rndseed_CODE_64bit {
12226: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 12227: {
1.366 albertel 12228: use integer;
1.443 albertel 12229: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 12230: my $symbseed=numval2($symb);
1.491 albertel 12231: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12232: my $CODEseed=numval(&getCODE());
1.443 albertel 12233: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 12234: my $num1=$symbseed+$CODEchck;
12235: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 12236: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12237: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 12238: if ($_64bit) { $num1=(($num1<<32)>>32); }
12239: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 12240: return "$num1:$num2";
1.366 albertel 12241: }
12242: }
12243:
1.575 albertel 12244: sub rndseed_CODE_64bit4 {
12245: my ($symb,$courseid,$domain,$username)=@_;
12246: {
12247: use integer;
12248: my $symbchck=unpack("%32S*",$symb.' ') << 16;
12249: my $symbseed=numval3($symb);
12250: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12251: my $CODEseed=numval3(&getCODE());
12252: my $courseseed=unpack("%32S*",$courseid.' ');
12253: my $num1=$symbseed+$CODEchck;
12254: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 12255: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12256: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 12257: if ($_64bit) { $num1=(($num1<<32)>>32); }
12258: if ($_64bit) { $num2=(($num2<<32)>>32); }
12259: return "$num1:$num2";
12260: }
12261: }
12262:
1.675 albertel 12263: sub rndseed_CODE_64bit5 {
12264: my ($symb,$courseid,$domain,$username)=@_;
12265: my $code = &getCODE();
12266: my ($num1,$num2)=&digest("$symb,$courseid,$code");
12267: return "$num1:$num2";
12268: }
12269:
1.366 albertel 12270: sub setup_random_from_rndseed {
12271: my ($rndseed)=@_;
1.503 albertel 12272: if ($rndseed =~/([,:])/) {
1.1262 raeburn 12273: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
12274: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
12275: &Math::Random::random_set_seed_from_phrase($rndseed);
12276: } else {
12277: &Math::Random::random_set_seed($num1,$num2);
12278: }
1.366 albertel 12279: } else {
12280: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 12281: }
1.36 albertel 12282: }
12283:
1.474 albertel 12284: sub latest_receipt_algorithm_id {
1.835 albertel 12285: return 'receipt3';
1.474 albertel 12286: }
12287:
1.480 www 12288: sub recunique {
12289: my $fucourseid=shift;
12290: my $unique;
1.835 albertel 12291: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
12292: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 12293: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 12294: } else {
12295: $unique=$perlvar{'lonReceipt'};
12296: }
12297: return unpack("%32C*",$unique);
12298: }
12299:
12300: sub recprefix {
12301: my $fucourseid=shift;
12302: my $prefix;
1.835 albertel 12303: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
12304: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 12305: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 12306: } else {
12307: $prefix=$perlvar{'lonHostID'};
12308: }
12309: return unpack("%32C*",$prefix);
12310: }
12311:
1.76 www 12312: sub ireceipt {
1.474 albertel 12313: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 12314:
12315: my $return =&recprefix($fucourseid).'-';
12316:
12317: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
12318: $env{'request.state'} eq 'construct') {
12319: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
12320: return $return;
12321: }
12322:
1.76 www 12323: my $cuname=unpack("%32C*",$funame);
12324: my $cudom=unpack("%32C*",$fudom);
12325: my $cucourseid=unpack("%32C*",$fucourseid);
12326: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 12327: my $cunique=&recunique($fucourseid);
1.474 albertel 12328: my $cpart=unpack("%32S*",$part);
1.835 albertel 12329: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
12330:
1.790 albertel 12331: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 12332:
12333: $return.= ($cunique%$cuname+
12334: $cunique%$cudom+
12335: $cusymb%$cuname+
12336: $cusymb%$cudom+
12337: $cucourseid%$cuname+
12338: $cucourseid%$cudom+
12339: $cpart%$cuname+
12340: $cpart%$cudom);
12341: } else {
12342: $return.= ($cunique%$cuname+
12343: $cunique%$cudom+
12344: $cusymb%$cuname+
12345: $cusymb%$cudom+
12346: $cucourseid%$cuname+
12347: $cucourseid%$cudom);
12348: }
12349: return $return;
1.76 www 12350: }
12351:
12352: sub receipt {
1.474 albertel 12353: my ($part)=@_;
1.790 albertel 12354: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 12355: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 12356: }
1.260 ng 12357:
1.790 albertel 12358: sub whichuser {
12359: my ($passedsymb)=@_;
12360: my ($symb,$courseid,$domain,$name,$publicuser);
12361: if (defined($env{'form.grade_symb'})) {
12362: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
12363: my $allowed=&allowed('vgr',$tmp_courseid);
12364: if (!$allowed &&
12365: exists($env{'request.course.sec'}) &&
12366: $env{'request.course.sec'} !~ /^\s*$/) {
12367: $allowed=&allowed('vgr',$tmp_courseid.
12368: '/'.$env{'request.course.sec'});
12369: }
12370: if ($allowed) {
12371: ($symb)=&get_env_multiple('form.grade_symb');
12372: $courseid=$tmp_courseid;
12373: ($domain)=&get_env_multiple('form.grade_domain');
12374: ($name)=&get_env_multiple('form.grade_username');
12375: return ($symb,$courseid,$domain,$name,$publicuser);
12376: }
12377: }
12378: if (!$passedsymb) {
12379: $symb=&symbread();
12380: } else {
12381: $symb=$passedsymb;
12382: }
12383: $courseid=$env{'request.course.id'};
12384: $domain=$env{'user.domain'};
12385: $name=$env{'user.name'};
12386: if ($name eq 'public' && $domain eq 'public') {
12387: if (!defined($env{'form.username'})) {
12388: $env{'form.username'}.=time.rand(10000000);
12389: }
12390: $name.=$env{'form.username'};
12391: }
12392: return ($symb,$courseid,$domain,$name,$publicuser);
12393:
12394: }
12395:
1.36 albertel 12396: # ------------------------------------------------------------ Serves up a file
1.472 albertel 12397: # returns either the contents of the file or
12398: # -1 if the file doesn't exist
1.481 raeburn 12399: #
12400: # if the target is a file that was uploaded via DOCS,
12401: # a check will be made to see if a current copy exists on the local server,
12402: # if it does this will be served, otherwise a copy will be retrieved from
12403: # the home server for the course and stored in /home/httpd/html/userfiles on
12404: # the local server.
1.472 albertel 12405:
1.36 albertel 12406: sub getfile {
1.538 albertel 12407: my ($file) = @_;
1.609 banghart 12408: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 12409: &repcopy($file);
12410: return &readfile($file);
12411: }
12412:
12413: sub repcopy_userfile {
12414: my ($file)=@_;
1.1142 raeburn 12415: my $londocroot = $perlvar{'lonDocRoot'};
12416: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 12417: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 12418: my ($cdom,$cnum,$filename) =
1.811 albertel 12419: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 12420: my $uri="/uploaded/$cdom/$cnum/$filename";
12421: if (-e "$file") {
1.828 www 12422: # we already have a local copy, check it out
1.538 albertel 12423: my @fileinfo = stat($file);
1.828 www 12424: my $rtncode;
12425: my $info;
1.538 albertel 12426: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 12427: if ($lwpresp ne 'ok') {
1.828 www 12428: # there is no such file anymore, even though we had a local copy
1.482 albertel 12429: if ($rtncode eq '404') {
1.538 albertel 12430: unlink($file);
1.482 albertel 12431: }
12432: return -1;
12433: }
12434: if ($info < $fileinfo[9]) {
1.828 www 12435: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 12436: return 'ok';
1.828 www 12437: } else {
12438: # the file is outdated, get rid of it
12439: unlink($file);
1.482 albertel 12440: }
1.828 www 12441: }
12442: # one way or the other, at this point, we don't have the file
12443: # construct the correct path for the file
12444: my @parts = ($cdom,$cnum);
12445: if ($filename =~ m|^(.+)/[^/]+$|) {
12446: push @parts, split(/\//,$1);
12447: }
12448: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
12449: foreach my $part (@parts) {
12450: $path .= '/'.$part;
12451: if (!-e $path) {
12452: mkdir($path,0770);
1.482 albertel 12453: }
12454: }
1.828 www 12455: # now the path exists for sure
12456: # get a user agent
12457: my $ua=new LWP::UserAgent;
12458: my $transferfile=$file.'.in.transfer';
12459: # FIXME: this should flock
12460: if (-e $transferfile) { return 'ok'; }
12461: my $request;
12462: $uri=~s/^\///;
1.980 raeburn 12463: my $homeserver = &homeserver($cnum,$cdom);
12464: my $protocol = $protocol{$homeserver};
12465: $protocol = 'http' if ($protocol ne 'https');
12466: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 12467: my $response=$ua->request($request,$transferfile);
12468: # did it work?
12469: if ($response->is_error()) {
12470: unlink($transferfile);
12471: &logthis("Userfile repcopy failed for $uri");
12472: return -1;
12473: }
12474: # worked, rename the transfer file
12475: rename($transferfile,$file);
1.607 raeburn 12476: return 'ok';
1.481 raeburn 12477: }
12478:
1.517 albertel 12479: sub tokenwrapper {
12480: my $uri=shift;
1.980 raeburn 12481: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 12482: $uri=~s|^/||;
1.620 albertel 12483: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 12484: my $token=$1;
1.552 albertel 12485: my (undef,$udom,$uname,$file)=split('/',$uri,4);
12486: if ($udom && $uname && $file) {
12487: $file=~s|(\?\.*)*$||;
1.949 raeburn 12488: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 12489: my $homeserver = &homeserver($uname,$udom);
12490: my $protocol = $protocol{$homeserver};
12491: $protocol = 'http' if ($protocol ne 'https');
12492: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 12493: (($uri=~/\?/)?'&':'?').'token='.$token.
12494: '&tokenissued='.$perlvar{'lonHostID'};
12495: } else {
12496: return '/adm/notfound.html';
12497: }
12498: }
12499:
1.828 www 12500: # call with reqtype HEAD: get last modification time
12501: # call with reqtype GET: get the file contents
12502: # Do not call this with reqtype GET for large files! It loads everything into memory
12503: #
1.481 raeburn 12504: sub getuploaded {
12505: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
12506: $uri=~s/^\///;
1.980 raeburn 12507: my $homeserver = &homeserver($cnum,$cdom);
12508: my $protocol = $protocol{$homeserver};
12509: $protocol = 'http' if ($protocol ne 'https');
12510: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 12511: my $ua=new LWP::UserAgent;
12512: my $request=new HTTP::Request($reqtype,$uri);
12513: my $response=$ua->request($request);
12514: $$rtncode = $response->code;
1.482 albertel 12515: if (! $response->is_success()) {
12516: return 'failed';
12517: }
12518: if ($reqtype eq 'HEAD') {
1.486 www 12519: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 12520: } elsif ($reqtype eq 'GET') {
12521: $$info = $response->content;
1.472 albertel 12522: }
1.482 albertel 12523: return 'ok';
1.36 albertel 12524: }
12525:
1.481 raeburn 12526: sub readfile {
12527: my $file = shift;
12528: if ( (! -e $file ) || ($file eq '') ) { return -1; };
12529: my $fh;
12530: open($fh,"<$file");
12531: my $a='';
1.800 albertel 12532: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 12533: return $a;
12534: }
12535:
1.36 albertel 12536: sub filelocation {
1.590 banghart 12537: my ($dir,$file) = @_;
12538: my $location;
12539: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 12540:
12541: if ($file =~ m-^/adm/-) {
12542: $file=~s-^/adm/wrapper/-/-;
12543: $file=~s-^/adm/coursedocs/showdoc/-/-;
12544: }
1.882 albertel 12545:
1.1139 www 12546: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 12547: $location = $file;
1.609 banghart 12548: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 12549: my ($udom,$uname,$filename)=
1.811 albertel 12550: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 12551: my $home=&homeserver($uname,$udom);
12552: my $is_me=0;
12553: my @ids=¤t_machine_ids();
12554: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
12555: if ($is_me) {
1.1117 foxr 12556: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 12557: } else {
12558: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
12559: $udom.'/'.$uname.'/'.$filename;
12560: }
1.882 albertel 12561: } elsif ($file =~ m-^/adm/-) {
12562: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 12563: } else {
12564: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 12565: $file=~s:^/(res|priv)/:/:;
12566: my $space=$1;
1.590 banghart 12567: if ( !( $file =~ m:^/:) ) {
12568: $location = $dir. '/'.$file;
12569: } else {
1.1142 raeburn 12570: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 12571: }
1.59 albertel 12572: }
1.590 banghart 12573: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 12574: while ($location=~m{/\.\./}) {
12575: if ($location =~ m{/[^/]+/\.\./}) {
12576: $location=~ s{/[^/]+/\.\./}{/}g;
12577: } else {
12578: $location=~ s{/\.\./}{/}g;
12579: }
12580: } #remove dir/..
1.590 banghart 12581: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
12582: return $location;
1.46 www 12583: }
1.36 albertel 12584:
1.46 www 12585: sub hreflocation {
12586: my ($dir,$file)=@_;
1.980 raeburn 12587: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 12588: $file=filelocation($dir,$file);
1.700 albertel 12589: } elsif ($file=~m-^/adm/-) {
12590: $file=~s-^/adm/wrapper/-/-;
12591: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 12592: }
12593: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
12594: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
12595: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 12596: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
12597: {/uploaded/$1/$2/}x;
1.46 www 12598: }
1.913 albertel 12599: if ($file=~ m{^/userfiles/}) {
12600: $file =~ s{^/userfiles/}{/uploaded/};
12601: }
1.462 albertel 12602: return $file;
1.465 albertel 12603: }
12604:
1.1139 www 12605:
12606:
12607:
12608:
1.465 albertel 12609: sub current_machine_domains {
1.853 albertel 12610: return &machine_domains(&hostname($perlvar{'lonHostID'}));
12611: }
12612:
12613: sub machine_domains {
12614: my ($hostname) = @_;
1.465 albertel 12615: my @domains;
1.838 albertel 12616: my %hostname = &all_hostnames();
1.465 albertel 12617: while( my($id, $name) = each(%hostname)) {
1.467 matthew 12618: # &logthis("-$id-$name-$hostname-");
1.465 albertel 12619: if ($hostname eq $name) {
1.844 albertel 12620: push(@domains,&host_domain($id));
1.465 albertel 12621: }
12622: }
12623: return @domains;
12624: }
12625:
12626: sub current_machine_ids {
1.853 albertel 12627: return &machine_ids(&hostname($perlvar{'lonHostID'}));
12628: }
12629:
12630: sub machine_ids {
12631: my ($hostname) = @_;
12632: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 12633: my @ids;
1.888 albertel 12634: my %name_to_host = &all_names();
1.889 albertel 12635: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
12636: return @{ $name_to_host{$hostname} };
12637: }
12638: return;
1.31 www 12639: }
12640:
1.824 raeburn 12641: sub additional_machine_domains {
12642: my @domains;
12643: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
12644: while( my $line = <$fh>) {
12645: $line =~ s/\s//g;
12646: push(@domains,$line);
12647: }
12648: return @domains;
12649: }
12650:
12651: sub default_login_domain {
12652: my $domain = $perlvar{'lonDefDomain'};
12653: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
12654: foreach my $posdom (¤t_machine_domains(),
12655: &additional_machine_domains()) {
12656: if (lc($posdom) eq lc($testdomain)) {
12657: $domain=$posdom;
12658: last;
12659: }
12660: }
12661: return $domain;
12662: }
12663:
1.31 www 12664: # ------------------------------------------------------------- Declutters URLs
12665:
12666: sub declutter {
12667: my $thisfn=shift;
1.569 albertel 12668: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261 raeburn 12669: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
12670: $thisfn=~s{^/home/httpd/html}{};
12671: }
1.31 www 12672: $thisfn=~s/^\///;
1.697 albertel 12673: $thisfn=~s|^adm/wrapper/||;
12674: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 12675: $thisfn=~s/^res\///;
1.1172 bisitz 12676: $thisfn=~s/^priv\///;
1.1032 raeburn 12677: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
12678: $thisfn=~s/\?.+$//;
12679: }
1.268 www 12680: return $thisfn;
12681: }
12682:
12683: # ------------------------------------------------------------- Clutter up URLs
12684:
12685: sub clutter {
12686: my $thisfn='/'.&declutter(shift);
1.887 albertel 12687: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 12688: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 12689: $thisfn='/res'.$thisfn;
12690: }
1.1031 raeburn 12691: if ($thisfn !~m|^/adm|) {
12692: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 12693: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 12694: } else {
12695: my ($ext) = ($thisfn =~ /\.(\w+)$/);
12696: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 12697: if ($embstyle eq 'ssi'
12698: || ($embstyle eq 'hdn')
12699: || ($embstyle eq 'rat')
12700: || ($embstyle eq 'prv')
12701: || ($embstyle eq 'ign')) {
12702: #do nothing with these
12703: } elsif (($embstyle eq 'img')
1.695 albertel 12704: || ($embstyle eq 'emb')
12705: || ($embstyle eq 'wrp')) {
12706: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 12707: } elsif ($embstyle eq 'unk'
12708: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 12709: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 12710: } else {
1.718 www 12711: # &logthis("Got a blank emb style");
1.695 albertel 12712: }
1.694 albertel 12713: }
1.1298 raeburn 12714: } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/exttools?$}) {
12715: $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 12716: }
1.31 www 12717: return $thisfn;
1.12 www 12718: }
12719:
1.787 albertel 12720: sub clutter_with_no_wrapper {
12721: my $uri = &clutter(shift);
12722: if ($uri =~ m-^/adm/-) {
12723: $uri =~ s-^/adm/wrapper/-/-;
12724: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
12725: }
12726: return $uri;
12727: }
12728:
1.557 albertel 12729: sub freeze_escape {
12730: my ($value)=@_;
12731: if (ref($value)) {
12732: $value=&nfreeze($value);
12733: return '__FROZEN__'.&escape($value);
12734: }
12735: return &escape($value);
12736: }
12737:
1.11 www 12738:
1.557 albertel 12739: sub thaw_unescape {
12740: my ($value)=@_;
12741: if ($value =~ /^__FROZEN__/) {
12742: substr($value,0,10,undef);
12743: $value=&unescape($value);
12744: return &thaw($value);
12745: }
12746: return &unescape($value);
12747: }
12748:
1.436 albertel 12749: sub correct_line_ends {
12750: my ($result)=@_;
12751: $$result =~s/\r\n/\n/mg;
12752: $$result =~s/\r/\n/mg;
1.415 albertel 12753: }
1.1 albertel 12754: # ================================================================ Main Program
12755:
1.184 www 12756: sub goodbye {
1.204 albertel 12757: &logthis("Starting Shut down");
1.443 albertel 12758: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 12759: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 12760: #converted
1.599 albertel 12761: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 12762: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
12763: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
12764: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 12765: #1.1 only
1.870 albertel 12766: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
12767: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
12768: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
12769: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
12770: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 12771: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
12772: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 12773: &flushcourselogs();
12774: &logthis("Shutting down");
12775: }
12776:
1.852 albertel 12777: sub get_dns {
1.1210 raeburn 12778: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 12779: if (!$ignore_cache) {
12780: my ($content,$cached)=
12781: &Apache::lonnet::is_cached_new('dns',$url);
12782: if ($cached) {
1.1210 raeburn 12783: &$func($content,$hashref);
1.869 albertel 12784: return;
12785: }
12786: }
12787:
12788: my %alldns;
1.852 albertel 12789: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
12790: foreach my $dns (<$config>) {
12791: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 12792: my $line = $1;
12793: my ($host,$protocol) = split(/:/,$line);
12794: if ($protocol ne 'https') {
12795: $protocol = 'http';
12796: }
12797: $alldns{$host} = $protocol;
1.869 albertel 12798: }
12799: while (%alldns) {
1.1263 raeburn 12800: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.852 albertel 12801: my $ua=new LWP::UserAgent;
1.1134 raeburn 12802: $ua->timeout(30);
1.979 raeburn 12803: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 12804: my $response=$ua->request($request);
1.979 raeburn 12805: delete($alldns{$dns});
1.852 albertel 12806: next if ($response->is_error());
12807: my @content = split("\n",$response->content);
1.1210 raeburn 12808: unless ($nocache) {
1.1219 raeburn 12809: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1210 raeburn 12810: }
12811: &$func(\@content,$hashref);
1.869 albertel 12812: return;
1.852 albertel 12813: }
12814: close($config);
1.871 albertel 12815: my $which = (split('/',$url))[3];
12816: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
12817: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 12818: my @content = <$config>;
1.1210 raeburn 12819: &$func(\@content,$hashref);
12820: return;
12821: }
12822:
12823: # ------------------------------------------------------Get DNS checksums file
1.1211 raeburn 12824: sub parse_dns_checksums_tab {
1.1210 raeburn 12825: my ($lines,$hashref) = @_;
1.1264 raeburn 12826: my $lonhost = $perlvar{'lonHostID'};
12827: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1210 raeburn 12828: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264 raeburn 12829: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
12830: my $webconfdir = '/etc/httpd/conf';
12831: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
12832: $webconfdir = '/etc/apache2';
12833: } elsif ($distro =~ /^sles(\d+)$/) {
12834: if ($1 >= 10) {
12835: $webconfdir = '/etc/apache2';
12836: }
12837: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
12838: if ($1 >= 10.0) {
12839: $webconfdir = '/etc/apache2';
12840: }
12841: }
1.1210 raeburn 12842: my ($release,$timestamp) = split(/\-/,$loncaparev);
12843: my (%chksum,%revnum);
12844: if (ref($lines) eq 'ARRAY') {
12845: chomp(@{$lines});
1.1238 raeburn 12846: my $version = shift(@{$lines});
12847: if ($version eq $release) {
1.1210 raeburn 12848: foreach my $line (@{$lines}) {
1.1238 raeburn 12849: my ($file,$version,$shasum) = split(/,/,$line);
1.1264 raeburn 12850: if ($file =~ m{^/etc/httpd/conf}) {
12851: if ($webconfdir eq '/etc/apache2') {
12852: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
12853: }
12854: }
1.1238 raeburn 12855: $chksum{$file} = $shasum;
12856: $revnum{$file} = $version;
1.1210 raeburn 12857: }
12858: if (ref($hashref) eq 'HASH') {
12859: %{$hashref} = (
12860: sums => \%chksum,
12861: versions => \%revnum,
12862: );
12863: }
12864: }
12865: }
1.869 albertel 12866: return;
1.852 albertel 12867: }
1.1210 raeburn 12868:
12869: sub fetch_dns_checksums {
1.1238 raeburn 12870: my %checksums;
12871: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1260 raeburn 12872: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238 raeburn 12873: my ($release,$timestamp) = split(/\-/,$loncaparev);
12874: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211 raeburn 12875: \%checksums);
1.1210 raeburn 12876: return \%checksums;
12877: }
12878:
1.327 albertel 12879: # ------------------------------------------------------------ Read domain file
12880: {
1.852 albertel 12881: my $loaded;
1.846 albertel 12882: my %domain;
12883:
1.852 albertel 12884: sub parse_domain_tab {
12885: my ($lines) = @_;
12886: foreach my $line (@$lines) {
12887: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 12888:
1.846 albertel 12889: chomp($line);
1.852 albertel 12890: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 12891: my %this_domain;
12892: foreach my $field ('description', 'auth_def', 'auth_arg_def',
12893: 'lang_def', 'city', 'longi', 'lati',
12894: 'primary') {
12895: $this_domain{$field} = shift(@elements);
12896: }
12897: $domain{$name} = \%this_domain;
1.852 albertel 12898: }
12899: }
1.864 albertel 12900:
12901: sub reset_domain_info {
12902: undef($loaded);
12903: undef(%domain);
12904: }
12905:
1.852 albertel 12906: sub load_domain_tab {
1.1293 raeburn 12907: my ($ignore_cache,$nocache) = @_;
12908: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 12909: my $fh;
12910: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
12911: my @lines = <$fh>;
12912: &parse_domain_tab(\@lines);
1.448 albertel 12913: }
1.852 albertel 12914: close($fh);
12915: $loaded = 1;
1.327 albertel 12916: }
1.846 albertel 12917:
12918: sub domain {
1.852 albertel 12919: &load_domain_tab() if (!$loaded);
12920:
1.846 albertel 12921: my ($name,$what) = @_;
12922: return if ( !exists($domain{$name}) );
12923:
12924: if (!$what) {
12925: return $domain{$name}{'description'};
12926: }
12927: return $domain{$name}{$what};
12928: }
1.974 raeburn 12929:
12930: sub domain_info {
12931: &load_domain_tab() if (!$loaded);
12932: return %domain;
12933: }
12934:
1.327 albertel 12935: }
12936:
12937:
1.1 albertel 12938: # ------------------------------------------------------------- Read hosts file
12939: {
1.838 albertel 12940: my %hostname;
1.844 albertel 12941: my %hostdom;
1.845 albertel 12942: my %libserv;
1.852 albertel 12943: my $loaded;
1.888 albertel 12944: my %name_to_host;
1.1074 raeburn 12945: my %internetdom;
1.1107 raeburn 12946: my %LC_dns_serv;
1.852 albertel 12947:
12948: sub parse_hosts_tab {
12949: my ($file) = @_;
12950: foreach my $configline (@$file) {
12951: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 12952: chomp($configline);
12953: if ($configline =~ /^\^/) {
12954: if ($configline =~ /^\^([\w.\-]+)/) {
12955: $LC_dns_serv{$1} = 1;
12956: }
12957: next;
12958: }
1.1074 raeburn 12959: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 12960: $name=~s/\s//g;
12961: if ($id && $domain && $role && $name) {
12962: $hostname{$id}=$name;
1.888 albertel 12963: push(@{$name_to_host{$name}}, $id);
1.852 albertel 12964: $hostdom{$id}=$domain;
12965: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 12966: if (defined($protocol)) {
12967: if ($protocol eq 'https') {
12968: $protocol{$id} = $protocol;
12969: } else {
12970: $protocol{$id} = 'http';
12971: }
1.968 raeburn 12972: } else {
1.969 raeburn 12973: $protocol{$id} = 'http';
1.968 raeburn 12974: }
1.1074 raeburn 12975: if (defined($intdom)) {
12976: $internetdom{$id} = $intdom;
12977: }
1.852 albertel 12978: }
12979: }
12980: }
1.864 albertel 12981:
12982: sub reset_hosts_info {
1.897 albertel 12983: &purge_remembered();
1.864 albertel 12984: &reset_domain_info();
12985: &reset_hosts_ip_info();
1.892 albertel 12986: undef(%name_to_host);
1.864 albertel 12987: undef(%hostname);
12988: undef(%hostdom);
12989: undef(%libserv);
12990: undef($loaded);
12991: }
1.1 albertel 12992:
1.852 albertel 12993: sub load_hosts_tab {
1.1293 raeburn 12994: my ($ignore_cache,$nocache) = @_;
12995: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.852 albertel 12996: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
12997: my @config = <$config>;
12998: &parse_hosts_tab(\@config);
12999: close($config);
13000: $loaded=1;
1.1 albertel 13001: }
1.852 albertel 13002:
1.838 albertel 13003: sub hostname {
1.852 albertel 13004: &load_hosts_tab() if (!$loaded);
13005:
1.838 albertel 13006: my ($lonid) = @_;
13007: return $hostname{$lonid};
13008: }
1.845 albertel 13009:
1.838 albertel 13010: sub all_hostnames {
1.852 albertel 13011: &load_hosts_tab() if (!$loaded);
13012:
1.838 albertel 13013: return %hostname;
13014: }
1.845 albertel 13015:
1.888 albertel 13016: sub all_names {
1.1293 raeburn 13017: my ($ignore_cache,$nocache) = @_;
13018: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 13019:
13020: return %name_to_host;
13021: }
13022:
1.974 raeburn 13023: sub all_host_domain {
13024: &load_hosts_tab() if (!$loaded);
13025: return %hostdom;
13026: }
13027:
1.845 albertel 13028: sub is_library {
1.852 albertel 13029: &load_hosts_tab() if (!$loaded);
13030:
1.845 albertel 13031: return exists($libserv{$_[0]});
13032: }
13033:
13034: sub all_library {
1.852 albertel 13035: &load_hosts_tab() if (!$loaded);
13036:
1.845 albertel 13037: return %libserv;
13038: }
13039:
1.1062 droeschl 13040: sub unique_library {
13041: #2x reverse removes all hostnames that appear more than once
13042: my %unique = reverse &all_library();
13043: return reverse %unique;
13044: }
13045:
1.841 albertel 13046: sub get_servers {
1.852 albertel 13047: &load_hosts_tab() if (!$loaded);
13048:
1.841 albertel 13049: my ($domain,$type) = @_;
13050: my %possible_hosts = ($type eq 'library') ? %libserv
13051: : %hostname;
13052: my %result;
1.842 albertel 13053: if (ref($domain) eq 'ARRAY') {
13054: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 13055: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 13056: $result{$host} = $hostname;
13057: }
13058: }
13059: } else {
13060: while ( my ($host,$hostname) = each(%possible_hosts)) {
13061: if ($hostdom{$host} eq $domain) {
13062: $result{$host} = $hostname;
13063: }
1.841 albertel 13064: }
13065: }
13066: return %result;
13067: }
1.845 albertel 13068:
1.1062 droeschl 13069: sub get_unique_servers {
13070: my %unique = reverse &get_servers(@_);
13071: return reverse %unique;
13072: }
13073:
1.844 albertel 13074: sub host_domain {
1.852 albertel 13075: &load_hosts_tab() if (!$loaded);
13076:
1.844 albertel 13077: my ($lonid) = @_;
13078: return $hostdom{$lonid};
13079: }
13080:
1.841 albertel 13081: sub all_domains {
1.852 albertel 13082: &load_hosts_tab() if (!$loaded);
13083:
1.841 albertel 13084: my %seen;
13085: my @uniq = grep(!$seen{$_}++, values(%hostdom));
13086: return @uniq;
13087: }
1.1074 raeburn 13088:
13089: sub internet_dom {
13090: &load_hosts_tab() if (!$loaded);
13091:
13092: my ($lonid) = @_;
13093: return $internetdom{$lonid};
13094: }
1.1107 raeburn 13095:
13096: sub is_LC_dns {
13097: &load_hosts_tab() if (!$loaded);
13098:
13099: my ($hostname) = @_;
13100: return exists($LC_dns_serv{$hostname});
13101: }
13102:
1.1 albertel 13103: }
13104:
1.847 albertel 13105: {
13106: my %iphost;
1.856 albertel 13107: my %name_to_ip;
13108: my %lonid_to_ip;
1.869 albertel 13109:
1.847 albertel 13110: sub get_hosts_from_ip {
13111: my ($ip) = @_;
13112: my %iphosts = &get_iphost();
13113: if (ref($iphosts{$ip})) {
13114: return @{$iphosts{$ip}};
13115: }
13116: return;
1.839 albertel 13117: }
1.864 albertel 13118:
13119: sub reset_hosts_ip_info {
13120: undef(%iphost);
13121: undef(%name_to_ip);
13122: undef(%lonid_to_ip);
13123: }
1.856 albertel 13124:
13125: sub get_host_ip {
13126: my ($lonid) = @_;
13127: if (exists($lonid_to_ip{$lonid})) {
13128: return $lonid_to_ip{$lonid};
13129: }
13130: my $name=&hostname($lonid);
13131: my $ip = gethostbyname($name);
13132: return if (!$ip || length($ip) ne 4);
13133: $ip=inet_ntoa($ip);
13134: $name_to_ip{$name} = $ip;
13135: $lonid_to_ip{$lonid} = $ip;
13136: return $ip;
13137: }
1.847 albertel 13138:
13139: sub get_iphost {
1.1293 raeburn 13140: my ($ignore_cache,$nocache) = @_;
1.894 albertel 13141:
1.869 albertel 13142: if (!$ignore_cache) {
13143: if (%iphost) {
13144: return %iphost;
13145: }
13146: my ($ip_info,$cached)=
13147: &Apache::lonnet::is_cached_new('iphost','iphost');
13148: if ($cached) {
13149: %iphost = %{$ip_info->[0]};
13150: %name_to_ip = %{$ip_info->[1]};
13151: %lonid_to_ip = %{$ip_info->[2]};
13152: return %iphost;
13153: }
13154: }
1.894 albertel 13155:
13156: # get yesterday's info for fallback
13157: my %old_name_to_ip;
13158: my ($ip_info,$cached)=
13159: &Apache::lonnet::is_cached_new('iphost','iphost');
13160: if ($cached) {
13161: %old_name_to_ip = %{$ip_info->[1]};
13162: }
13163:
1.1293 raeburn 13164: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 13165: foreach my $name (keys(%name_to_host)) {
1.847 albertel 13166: my $ip;
13167: if (!exists($name_to_ip{$name})) {
13168: $ip = gethostbyname($name);
13169: if (!$ip || length($ip) ne 4) {
1.894 albertel 13170: if (defined($old_name_to_ip{$name})) {
13171: $ip = $old_name_to_ip{$name};
13172: &logthis("Can't find $name defaulting to old $ip");
13173: } else {
13174: &logthis("Name $name no IP found");
13175: next;
13176: }
13177: } else {
13178: $ip=inet_ntoa($ip);
1.847 albertel 13179: }
13180: $name_to_ip{$name} = $ip;
13181: } else {
13182: $ip = $name_to_ip{$name};
1.653 albertel 13183: }
1.888 albertel 13184: foreach my $id (@{ $name_to_host{$name} }) {
13185: $lonid_to_ip{$id} = $ip;
13186: }
13187: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 13188: }
1.1293 raeburn 13189: unless ($nocache) {
13190: &do_cache_new('iphost','iphost',
13191: [\%iphost,\%name_to_ip,\%lonid_to_ip],
13192: 48*60*60);
13193: }
1.869 albertel 13194:
1.847 albertel 13195: return %iphost;
1.598 albertel 13196: }
13197:
1.992 raeburn 13198: #
13199: # Given a DNS returns the loncapa host name for that DNS
13200: #
13201: sub host_from_dns {
13202: my ($dns) = @_;
13203: my @hosts;
13204: my $ip;
13205:
1.993 raeburn 13206: if (exists($name_to_ip{$dns})) {
1.992 raeburn 13207: $ip = $name_to_ip{$dns};
13208: }
13209: if (!$ip) {
13210: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
13211: if (length($ip) == 4) {
13212: $ip = &IO::Socket::inet_ntoa($ip);
13213: }
13214: }
13215: if ($ip) {
13216: @hosts = get_hosts_from_ip($ip);
13217: return $hosts[0];
13218: }
13219: return undef;
1.986 foxr 13220: }
1.992 raeburn 13221:
1.1074 raeburn 13222: sub get_internet_names {
13223: my ($lonid) = @_;
13224: return if ($lonid eq '');
13225: my ($idnref,$cached)=
13226: &Apache::lonnet::is_cached_new('internetnames',$lonid);
13227: if ($cached) {
13228: return $idnref;
13229: }
13230: my $ip = &get_host_ip($lonid);
13231: my @hosts = &get_hosts_from_ip($ip);
13232: my %iphost = &get_iphost();
13233: my (@idns,%seen);
13234: foreach my $id (@hosts) {
13235: my $dom = &host_domain($id);
13236: my $prim_id = &domain($dom,'primary');
13237: my $prim_ip = &get_host_ip($prim_id);
13238: next if ($seen{$prim_ip});
13239: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
13240: foreach my $id (@{$iphost{$prim_ip}}) {
13241: my $intdom = &internet_dom($id);
13242: unless (grep(/^\Q$intdom\E$/,@idns)) {
13243: push(@idns,$intdom);
13244: }
13245: }
13246: }
13247: $seen{$prim_ip} = 1;
13248: }
1.1219 raeburn 13249: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 13250: }
13251:
1.986 foxr 13252: }
13253:
1.1079 raeburn 13254: sub all_loncaparevs {
1.1249 raeburn 13255: 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 13256: }
13257:
1.1227 raeburn 13258: # ---------------------------------------------------------- Read loncaparev table
13259: {
13260: sub load_loncaparevs {
13261: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
13262: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
13263: while (my $configline=<$config>) {
13264: chomp($configline);
13265: my ($hostid,$loncaparev)=split(/:/,$configline);
13266: $loncaparevs{$hostid}=$loncaparev;
13267: }
13268: close($config);
13269: }
13270: }
13271: }
13272: }
13273:
13274: # ---------------------------------------------------------- Read serverhostID table
13275: {
13276: sub load_serverhomeIDs {
13277: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
13278: if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
13279: while (my $configline=<$config>) {
13280: chomp($configline);
13281: my ($name,$id)=split(/:/,$configline);
13282: $serverhomeIDs{$name}=$id;
13283: }
13284: close($config);
13285: }
13286: }
13287: }
13288: }
13289:
13290:
1.862 albertel 13291: BEGIN {
13292:
13293: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
13294: unless ($readit) {
13295: {
13296: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
13297: %perlvar = (%perlvar,%{$configvars});
13298: }
13299:
13300:
1.1 albertel 13301: # ------------------------------------------------------ Read spare server file
13302: {
1.448 albertel 13303: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 13304:
13305: while (my $configline=<$config>) {
13306: chomp($configline);
1.284 matthew 13307: if ($configline) {
1.784 albertel 13308: my ($host,$type) = split(':',$configline,2);
1.785 albertel 13309: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 13310: push(@{ $spareid{$type} }, $host);
1.1 albertel 13311: }
13312: }
1.448 albertel 13313: close($config);
1.1 albertel 13314: }
1.11 www 13315: # ------------------------------------------------------------ Read permissions
13316: {
1.448 albertel 13317: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 13318:
13319: while (my $configline=<$config>) {
1.448 albertel 13320: chomp($configline);
13321: if ($configline) {
13322: my ($role,$perm)=split(/ /,$configline);
13323: if ($perm ne '') { $pr{$role}=$perm; }
13324: }
1.11 www 13325: }
1.448 albertel 13326: close($config);
1.11 www 13327: }
13328:
13329: # -------------------------------------------- Read plain texts for permissions
13330: {
1.448 albertel 13331: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 13332:
13333: while (my $configline=<$config>) {
1.448 albertel 13334: chomp($configline);
13335: if ($configline) {
1.742 raeburn 13336: my ($short,@plain)=split(/:/,$configline);
13337: %{$prp{$short}} = ();
13338: if (@plain > 0) {
13339: $prp{$short}{'std'} = $plain[0];
13340: for (my $i=1; $i<@plain; $i++) {
13341: $prp{$short}{'alt'.$i} = $plain[$i];
13342: }
13343: }
1.448 albertel 13344: }
1.135 www 13345: }
1.448 albertel 13346: close($config);
1.135 www 13347: }
13348:
13349: # ---------------------------------------------------------- Read package table
13350: {
1.448 albertel 13351: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 13352:
13353: while (my $configline=<$config>) {
1.483 albertel 13354: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 13355: chomp($configline);
13356: my ($short,$plain)=split(/:/,$configline);
13357: my ($pack,$name)=split(/\&/,$short);
13358: if ($plain ne '') {
13359: $packagetab{$pack.'&'.$name.'&name'}=$name;
13360: $packagetab{$short}=$plain;
13361: }
1.11 www 13362: }
1.448 albertel 13363: close($config);
1.329 matthew 13364: }
13365:
1.1073 raeburn 13366: # ---------------------------------------------------------- Read loncaparev table
1.1227 raeburn 13367:
13368: &load_loncaparevs();
1.1073 raeburn 13369:
1.1074 raeburn 13370: # ---------------------------------------------------------- Read serverhostID table
13371:
1.1227 raeburn 13372: &load_serverhomeIDs();
13373:
13374: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 13375: {
13376: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
13377: if (-e $file) {
13378: my $parser = HTML::LCParser->new($file);
13379: while (my $token = $parser->get_token()) {
13380: if ($token->[0] eq 'S') {
13381: my $item = $token->[1];
13382: my $name = $token->[2]{'name'};
13383: my $value = $token->[2]{'value'};
1.1285 raeburn 13384: my $valuematch = $token->[2]{'valuematch'};
1.1303 raeburn 13385: my $namematch = $token->[2]{'namematch'};
13386: if ($item eq 'parameter') {
13387: if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
13388: my $release = $parser->get_text();
13389: $release =~ s/(^\s*|\s*$ )//gx;
13390: $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
13391: }
13392: } elsif ($item ne '' && $name ne '') {
1.1079 raeburn 13393: my $release = $parser->get_text();
13394: $release =~ s/(^\s*|\s*$ )//gx;
1.1303 raeburn 13395: $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079 raeburn 13396: }
13397: }
13398: }
13399: }
1.1073 raeburn 13400: }
13401:
1.1138 raeburn 13402: # ---------------------------------------------------------- Read managers table
13403: {
13404: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
13405: if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
13406: while (my $configline=<$config>) {
13407: chomp($configline);
13408: next if ($configline =~ /^\#/);
13409: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
13410: $managerstab{$configline} = 1;
13411: }
13412: }
13413: close($config);
13414: }
13415: }
13416: }
13417:
1.329 matthew 13418: # ------------- set up temporary directory
13419: {
1.1117 foxr 13420: $tmpdir = LONCAPA::tempdir();
1.329 matthew 13421:
1.11 www 13422: }
13423:
1.794 albertel 13424: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
13425: 'compress_threshold'=> 20_000,
13426: });
1.185 www 13427:
1.281 www 13428: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 13429: $dumpcount=0;
1.958 www 13430: $locknum=0;
1.22 www 13431:
1.163 harris41 13432: &logtouch();
1.672 albertel 13433: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 13434: $readit=1;
1.564 albertel 13435: {
13436: use integer;
13437: my $test=(2**32)+1;
1.568 albertel 13438: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 13439: &logthis(" Detected 64bit platform ($_64bit)");
13440: }
1.195 www 13441: }
1.1 albertel 13442: }
1.179 www 13443:
1.1 albertel 13444: 1;
1.191 harris41 13445: __END__
13446:
1.243 albertel 13447: =pod
13448:
1.191 harris41 13449: =head1 NAME
13450:
1.243 albertel 13451: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 13452:
13453: =head1 SYNOPSIS
13454:
1.243 albertel 13455: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 13456:
13457: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
13458:
1.243 albertel 13459: Common parameters:
13460:
13461: =over 4
13462:
13463: =item *
13464:
13465: $uname : an internal username (if $cname expecting a course Id specifically)
13466:
13467: =item *
13468:
13469: $udom : a domain (if $cdom expecting a course's domain specifically)
13470:
13471: =item *
13472:
13473: $symb : a resource instance identifier
13474:
13475: =item *
13476:
13477: $namespace : the name of a .db file that contains the data needed or
13478: being set.
13479:
13480: =back
13481:
1.394 bowersj2 13482: =head1 OVERVIEW
1.191 harris41 13483:
1.394 bowersj2 13484: lonnet provides subroutines which interact with the
13485: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
13486: about classes, users, and resources.
1.243 albertel 13487:
13488: For many of these objects you can also use this to store data about
13489: them or modify them in various ways.
1.191 harris41 13490:
1.394 bowersj2 13491: =head2 Symbs
1.191 harris41 13492:
1.394 bowersj2 13493: To identify a specific instance of a resource, LON-CAPA uses symbols
13494: or "symbs"X<symb>. These identifiers are built from the URL of the
13495: map, the resource number of the resource in the map, and the URL of
13496: the resource itself. The latter is somewhat redundant, but might help
13497: if maps change.
13498:
13499: An example is
13500:
13501: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
13502:
13503: The respective map entry is
13504:
13505: <resource id="19" src="/res/msu/korte/tests/part12.problem"
13506: title="Problem 2">
13507: </resource>
13508:
13509: Symbs are used by the random number generator, as well as to store and
13510: restore data specific to a certain instance of for example a problem.
13511:
13512: =head2 Storing And Retrieving Data
13513:
13514: X<store()>X<cstore()>X<restore()>Three of the most important functions
13515: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
13516: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
13517: is is the non-critical message twin of cstore. These functions are for
13518: handlers to store a perl hash to a user's permanent data space in an
13519: easy manner, and to retrieve it again on another call. It is expected
13520: that a handler would use this once at the beginning to retrieve data,
13521: and then again once at the end to send only the new data back.
13522:
13523: The data is stored in the user's data directory on the user's
13524: homeserver under the ID of the course.
13525:
13526: The hash that is returned by restore will have all of the previous
13527: value for all of the elements of the hash.
13528:
13529: Example:
13530:
13531: #creating a hash
13532: my %hash;
13533: $hash{'foo'}='bar';
13534:
13535: #storing it
13536: &Apache::lonnet::cstore(\%hash);
13537:
13538: #changing a value
13539: $hash{'foo'}='notbar';
13540:
13541: #adding a new value
13542: $hash{'bar'}='foo';
13543: &Apache::lonnet::cstore(\%hash);
13544:
13545: #retrieving the hash
13546: my %history=&Apache::lonnet::restore();
13547:
13548: #print the hash
13549: foreach my $key (sort(keys(%history))) {
13550: print("\%history{$key} = $history{$key}");
13551: }
13552:
13553: Will print out:
1.191 harris41 13554:
1.394 bowersj2 13555: %history{1:foo} = bar
13556: %history{1:keys} = foo:timestamp
13557: %history{1:timestamp} = 990455579
13558: %history{2:bar} = foo
13559: %history{2:foo} = notbar
13560: %history{2:keys} = foo:bar:timestamp
13561: %history{2:timestamp} = 990455580
13562: %history{bar} = foo
13563: %history{foo} = notbar
13564: %history{timestamp} = 990455580
13565: %history{version} = 2
13566:
13567: Note that the special hash entries C<keys>, C<version> and
13568: C<timestamp> were added to the hash. C<version> will be equal to the
13569: total number of versions of the data that have been stored. The
13570: C<timestamp> attribute will be the UNIX time the hash was
13571: stored. C<keys> is available in every historical section to list which
13572: keys were added or changed at a specific historical revision of a
13573: hash.
13574:
13575: B<Warning>: do not store the hash that restore returns directly. This
13576: will cause a mess since it will restore the historical keys as if the
13577: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 13578:
1.394 bowersj2 13579: Calling convention:
1.191 harris41 13580:
1.1225 raeburn 13581: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269 raeburn 13582: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 13583:
1.394 bowersj2 13584: For more detailed information, see lonnet specific documentation.
1.191 harris41 13585:
1.394 bowersj2 13586: =head1 RETURN MESSAGES
1.191 harris41 13587:
1.394 bowersj2 13588: =over 4
1.191 harris41 13589:
1.394 bowersj2 13590: =item * B<con_lost>: unable to contact remote host
1.191 harris41 13591:
1.394 bowersj2 13592: =item * B<con_delayed>: unable to contact remote host, message will be delivered
13593: when the connection is brought back up
1.191 harris41 13594:
1.394 bowersj2 13595: =item * B<con_failed>: unable to contact remote host and unable to save message
13596: for later delivery
1.191 harris41 13597:
1.967 bisitz 13598: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 13599:
1.394 bowersj2 13600: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 13601: that was requested
1.191 harris41 13602:
1.243 albertel 13603: =back
1.191 harris41 13604:
1.243 albertel 13605: =head1 PUBLIC SUBROUTINES
1.191 harris41 13606:
1.243 albertel 13607: =head2 Session Environment Functions
1.191 harris41 13608:
1.243 albertel 13609: =over 4
1.191 harris41 13610:
1.394 bowersj2 13611: =item *
13612: X<appenv()>
1.949 raeburn 13613: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 13614: the user envirnoment file, and will be restored for each access this
1.620 albertel 13615: user makes during this session, also modifies the %env for the current
1.949 raeburn 13616: process. Optional rolesarrayref - if defined contains a reference to an array
13617: of roles which are exempt from the restriction on modifying user.role entries
13618: in the user's environment.db and in %env.
1.191 harris41 13619:
13620: =item *
1.394 bowersj2 13621: X<delenv()>
1.987 raeburn 13622: B<delenv($delthis,$regexp)>: removes all items from the session
13623: environment file that begin with $delthis. If the
13624: optional second arg - $regexp - is true, $delthis is treated as a
13625: regular expression, otherwise \Q$delthis\E is used.
13626: The values are also deleted from the current processes %env.
1.191 harris41 13627:
1.795 albertel 13628: =item * get_env_multiple($name)
13629:
13630: gets $name from the %env hash, it seemlessly handles the cases where multiple
13631: values may be defined and end up as an array ref.
13632:
13633: returns an array of values
13634:
1.243 albertel 13635: =back
13636:
13637: =head2 User Information
1.191 harris41 13638:
1.243 albertel 13639: =over 4
1.191 harris41 13640:
13641: =item *
1.394 bowersj2 13642: X<queryauthenticate()>
13643: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 13644: authentication scheme
13645:
13646: =item *
1.394 bowersj2 13647: X<authenticate()>
1.1073 raeburn 13648: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 13649: authenticate user from domain's lib servers (first use the current
13650: one). C<$upass> should be the users password.
1.1073 raeburn 13651: $checkdefauth is optional (value is 1 if a check should be made to
13652: authenticate user using default authentication method, and allow
13653: account creation if username does not have account in the domain).
13654: $clientcancheckhost is optional (value is 1 if checking whether the
13655: server can host will occur on the client side in lonauth.pm).
1.191 harris41 13656:
13657: =item *
1.394 bowersj2 13658: X<homeserver()>
13659: B<homeserver($uname,$udom)>: find the server which has
13660: the user's directory and files (there must be only one), this caches
13661: the answer, and also caches if there is a borken connection.
1.191 harris41 13662:
13663: =item *
1.394 bowersj2 13664: X<idget()>
1.1300 raeburn 13665: B<idget($udom,$idsref,$namespace)>: find the usernames behind either
13666: a list of student/employee IDs or clicker IDs
13667: (student/employee IDs are a unique resource in a domain, there must be
13668: only 1 ID per username, and only 1 username per ID in a specific domain).
13669: clickerIDs are not necessarily unique, as students might share clickers.
13670: (returns hash: id=>name,id=>name)
1.191 harris41 13671:
13672: =item *
1.394 bowersj2 13673: X<idrget()>
13674: B<idrget($udom,@unames)>: find the IDs behind a list of
13675: usernames (returns hash: name=>id,name=>id)
1.191 harris41 13676:
13677: =item *
1.394 bowersj2 13678: X<idput()>
1.1300 raeburn 13679: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of
13680: names and associated student/employee IDs or clicker IDs.
13681:
13682: =item *
13683: X<iddel()>
13684: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted
13685: student/employee ID or clicker ID username look-ups from domain.
13686: The homeserver ($uhome) and namespace ($namespace) are optional.
13687: If no $uhome is provided, it will be determined usig &homeserver()
13688: for each user. If no $namespace is provided, the default is ids.
13689:
13690: =item *
13691: X<updateclickers()>
13692: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update
13693: clicker ID-to-username look-ups in clickers.db on library server.
13694: Permitted actions are add or del (i.e., add or delete). The
13695: clickers.db contains clickerID as keys (escaped), and each corresponding
13696: value is an escaped comma-separated list of usernames (for whom the
13697: library server is the homeserver), who registered that particular ID.
13698: If $critical is true, the update will be sent via &critical, otherwise
13699: &reply() will be used.
1.191 harris41 13700:
13701: =item *
1.394 bowersj2 13702: X<rolesinit()>
1.1169 droeschl 13703: B<rolesinit($udom,$username)>: get user privileges.
13704: returns user role, first access and timer interval hashes
1.243 albertel 13705:
13706: =item *
1.1171 droeschl 13707: X<privileged()>
13708: B<privileged($username,$domain)>: returns a true if user has a
13709: privileged and active role (i.e. su or dc), false otherwise.
13710:
13711: =item *
1.551 albertel 13712: X<getsection()>
13713: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 13714: course $cname, return section name/number or '' for "not in course"
13715: and '-1' for "no section"
13716:
13717: =item *
1.394 bowersj2 13718: X<userenvironment()>
13719: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 13720: passed in @what from the requested user's environment, returns a hash
13721:
1.858 raeburn 13722: =item *
13723: X<userlog_query()>
1.859 albertel 13724: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
13725: activity.log file. %filters defines filters applied when parsing the
13726: log file. These can be start or end timestamps, or the type of action
13727: - log to look for Login or Logout events, check for Checkin or
13728: Checkout, role for role selection. The response is in the form
13729: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
13730: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 13731:
1.243 albertel 13732: =back
13733:
13734: =head2 User Roles
13735:
13736: =over 4
13737:
13738: =item *
13739:
1.1284 raeburn 13740: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
13741: returns codes for allowed actions.
13742:
13743: The first argument is required, all others are optional.
13744:
13745: $priv is the privilege being checked.
13746: $uri contains additional information about what is being checked for access (e.g.,
13747: URL, course ID etc.).
13748: $symb is the unique resource instance identifier in a course; if needed,
13749: but not provided, it will be retrieved via a call to &symbread().
13750: $role is the role for which a priv is being checked (only used if priv is evb).
13751: $clientip is the user's IP address (only used when checking for access to portfolio
13752: files).
13753: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
13754: prevents recursive calls to &allowed.
13755:
1.243 albertel 13756: F: full access
13757: U,I,K: authentication modes (cxx only)
13758: '': forbidden
13759: 1: user needs to choose course
13760: 2: browse allowed
1.766 albertel 13761: A: passphrase authentication needed
1.1284 raeburn 13762: B: access temporarily blocked because of a blocking event in a course.
1.243 albertel 13763:
13764: =item *
13765:
1.1192 raeburn 13766: constructaccess($url,$setpriv) : check for access to construction space URL
13767:
13768: See if the owner domain and name in the URL match those in the
13769: expected environment. If so, return three element list
13770: ($ownername,$ownerdomain,$ownerhome).
13771:
13772: Otherwise return the null string.
13773:
13774: If second argument 'setpriv' is true, it assigns the privileges,
13775: and returns the same three element list, unless the owner has
13776: blocked "ad hoc" Domain Coordinator access to the Author Space,
13777: in which case the null string is returned.
13778:
13779: =item *
13780:
1.1326 raeburn 13781: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
13782: define a custom role rolename set privileges in format of lonTabs/roles.tab
13783: for system, domain, and course level. $uname and $udom are optional (current
13784: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 13785:
13786: =item *
13787:
1.988 raeburn 13788: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
13789: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 13790: $type is Course (default) or Community.
1.988 raeburn 13791: If $forcedefault evaluates to true, text returned will be default
13792: text for $type. Otherwise, if this is a course, the text returned
13793: will be a custom name for the role (if defined in the course's
13794: environment). If no custom name is defined the default is returned.
13795:
1.832 raeburn 13796: =item *
13797:
1.1219 raeburn 13798: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 13799: All arguments are optional. Returns a hash of a roles, either for
13800: co-author/assistant author roles for a user's Construction Space
1.906 albertel 13801: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 13802: In the hash, keys are set to colon-separated $uname,$udom,$role, and
13803: (optionally) if $withsec is true, a fourth colon-separated item - $section.
13804: For each key, value is set to colon-separated start and end times for
13805: the role. If no username and domain are specified, will default to
1.934 raeburn 13806: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 13807: of role statuses (active, future or previous), roles
13808: (e.g., cc,in, st etc.) and domains of the roles which can be used
13809: to restrict the list of roles reported. If no array ref is
13810: provided for types, will default to return only active roles.
1.834 albertel 13811:
1.1195 raeburn 13812: =item *
13813:
13814: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196 raeburn 13815: user: $uname:$udom has a role in the course: $cdom_$cnum.
13816:
13817: Additional optional arguments are: $type (if role checking is to be restricted
13818: to certain user status types -- previous (expired roles), active (currently
1.1195 raeburn 13819: available roles) or future (roles available in the future), and
13820: $hideprivileged -- if true will not report course roles for users who
1.1219 raeburn 13821: have active Domain Coordinator role in course's domain or in additional
13822: domains (specified in 'Domains to check for privileged users' in course
13823: environment -- set via: Course Settings -> Classlists and staff listing).
13824:
13825: =item *
13826:
13827: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
13828: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
13829: $possdomains and $possroles are optional array refs -- to domains to check and
13830: roles to check. If $possdomains is not specified, a dump will be done of the
13831: users' roles.db to check for a dc or su role in any domain. This can be
13832: time consuming if &privileged is called repeatedly (e.g., when displaying a
13833: classlist), so in such cases, supplying a $possdomains array is preferred, as
13834: this then allows &privileged_by_domain() to be used, which caches the identity
13835: of privileged users, eliminating the need for repeated calls to &dump().
13836:
13837: =item *
13838:
13839: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
13840: where the outer hash keys are domains specified in the $possdomains array ref,
13841: next inner hash keys are privileged roles specified in the $roles array ref,
13842: and the innermost hash contains key = value pairs for username:domain = end:start
13843: for active or future "privileged" users with that role in that domain. To avoid
13844: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
13845: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195 raeburn 13846:
1.243 albertel 13847: =back
13848:
13849: =head2 User Modification
13850:
13851: =over 4
13852:
13853: =item *
13854:
1.957 raeburn 13855: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 13856: user for the level given by URL. Optional start and end dates (leave empty
13857: string or zero for "no date")
1.191 harris41 13858:
13859: =item *
13860:
1.243 albertel 13861: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
13862: change a users, password, possible return values are: ok,
13863: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
13864: refused
1.191 harris41 13865:
13866: =item *
13867:
1.243 albertel 13868: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 13869:
13870: =item *
13871:
1.1058 raeburn 13872: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
13873: $forceid,$desiredhome,$email,$inststatus,$candelete) :
13874:
13875: will update user information (firstname,middlename,lastname,generation,
13876: permanentemail), and if forceid is true, student/employee ID also.
13877: A user's institutional affiliation(s) can also be updated.
13878: User information fields will not be overwritten with empty entries
13879: unless the field is included in the $candelete array reference.
13880: This array is included when a single user is modified via "Manage Users",
13881: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 13882:
13883: =item *
13884:
1.286 matthew 13885: modifystudent
13886:
1.957 raeburn 13887: modify a student's enrollment and identification information.
1.1235 raeburn 13888: The course id is resolved based on the current user's environment.
13889: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 13890: associated with a course.
13891:
1.297 matthew 13892: This call is essentially a wrapper for lonnet::modifyuser and
13893: lonnet::modify_student_enrollment
1.286 matthew 13894:
13895: Inputs:
13896:
13897: =over 4
13898:
1.957 raeburn 13899: =item B<$udom> Student's loncapa domain
1.286 matthew 13900:
1.957 raeburn 13901: =item B<$uname> Student's loncapa login name
1.286 matthew 13902:
1.964 bisitz 13903: =item B<$uid> Student/Employee ID
1.286 matthew 13904:
1.957 raeburn 13905: =item B<$umode> Student's authentication mode
1.286 matthew 13906:
1.957 raeburn 13907: =item B<$upass> Student's password
1.286 matthew 13908:
1.957 raeburn 13909: =item B<$first> Student's first name
1.286 matthew 13910:
1.957 raeburn 13911: =item B<$middle> Student's middle name
1.286 matthew 13912:
1.957 raeburn 13913: =item B<$last> Student's last name
1.286 matthew 13914:
1.957 raeburn 13915: =item B<$gene> Student's generation
1.286 matthew 13916:
1.957 raeburn 13917: =item B<$usec> Student's section in course
1.286 matthew 13918:
13919: =item B<$end> Unix time of the roles expiration
13920:
13921: =item B<$start> Unix time of the roles start date
13922:
13923: =item B<$forceid> If defined, allow $uid to be changed
13924:
13925: =item B<$desiredhome> server to use as home server for student
13926:
1.957 raeburn 13927: =item B<$email> Student's permanent e-mail address
13928:
13929: =item B<$type> Type of enrollment (auto or manual)
13930:
1.963 raeburn 13931: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
13932:
13933: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 13934:
1.963 raeburn 13935: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 13936:
1.963 raeburn 13937: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 13938:
1.1216 raeburn 13939: =item B<$inststatus> institutional status of user - : separated string of escaped status types
13940:
13941: =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 13942:
1.286 matthew 13943: =back
1.297 matthew 13944:
13945: =item *
13946:
13947: modify_student_enrollment
13948:
1.1235 raeburn 13949: Change a student's enrollment status in a class. The environment variable
1.297 matthew 13950: 'role.request.course' must be defined for this function to proceed.
13951:
13952: Inputs:
13953:
13954: =over 4
13955:
1.1235 raeburn 13956: =item $udom, student's domain
1.297 matthew 13957:
1.1235 raeburn 13958: =item $uname, student's name
1.297 matthew 13959:
1.1235 raeburn 13960: =item $uid, student's user id
1.297 matthew 13961:
1.1235 raeburn 13962: =item $first, student's first name
1.297 matthew 13963:
13964: =item $middle
13965:
13966: =item $last
13967:
13968: =item $gene
13969:
13970: =item $usec
13971:
13972: =item $end
13973:
13974: =item $start
13975:
1.957 raeburn 13976: =item $type
13977:
13978: =item $locktype
13979:
13980: =item $cid
13981:
13982: =item $selfenroll
13983:
13984: =item $context
13985:
1.1216 raeburn 13986: =item $credits, number of credits student will earn from this class
13987:
1.1314 raeburn 13988: =item $instsec, institutional course section code for student
13989:
1.297 matthew 13990: =back
13991:
1.191 harris41 13992:
13993: =item *
13994:
1.243 albertel 13995: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
13996: custom role; give a custom role to a user for the level given by URL. Specify
13997: name and domain of role author, and role name
1.191 harris41 13998:
13999: =item *
14000:
1.243 albertel 14001: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 14002:
14003: =item *
14004:
1.243 albertel 14005: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
14006:
14007: =back
14008:
14009: =head2 Course Infomation
14010:
14011: =over 4
1.191 harris41 14012:
14013: =item *
14014:
1.1118 foxr 14015: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 14016: specified course id, including all environment settings for the
14017: course, the description of the course will be in the hash under the
14018: key 'description'
1.191 harris41 14019:
1.1118 foxr 14020: $options is an optional parameter that if supplied is a hash reference that controls
14021: what how this function works. It has the following key/values:
14022:
14023: =over 4
14024:
14025: =item freshen_cache
14026:
14027: If defined, and the environment cache for the course is valid, it is
14028: returned in the returned hash.
14029:
14030: =item one_time
14031:
14032: If defined, the last cache time is set to _now_
14033:
14034: =item user
14035:
14036: If defined, the supplied username is used instead of the current user.
14037:
14038:
14039: =back
14040:
1.191 harris41 14041: =item *
14042:
1.624 albertel 14043: resdata($name,$domain,$type,@which) : request for current parameter
14044: setting for a specific $type, where $type is either 'course' or 'user',
14045: @what should be a list of parameters to ask about. This routine caches
1.1235 raeburn 14046: answers for 10 minutes.
1.243 albertel 14047:
1.877 foxr 14048: =item *
14049:
14050: get_courseresdata($courseid, $domain) : dump the entire course resource
14051: data base, returning a hash that is keyed by the resource name and has
14052: values that are the resource value. I believe that the timestamps and
14053: versions are also returned.
14054:
1.1236 raeburn 14055: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
14056: supplemental content area. This routine caches the number of files for
14057: 10 minutes.
14058:
1.243 albertel 14059: =back
14060:
14061: =head2 Course Modification
14062:
14063: =over 4
1.191 harris41 14064:
14065: =item *
14066:
1.243 albertel 14067: writecoursepref($courseid,%prefs) : write preferences (environment
14068: database) for a course
1.191 harris41 14069:
14070: =item *
14071:
1.1011 raeburn 14072: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
14073:
14074: =item *
14075:
1.1038 raeburn 14076: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 14077:
1.1167 droeschl 14078: =item *
14079:
14080: is_course($courseid), is_course($cdom, $cnum)
14081:
14082: Accepts either a combined $courseid (in the form of domain_courseid) or the
14083: two component version $cdom, $cnum. It checks if the specified course exists.
14084:
14085: Returns:
14086: undef if the course doesn't exist, otherwise
14087: in scalar context the combined courseid.
14088: in list context the two components of the course identifier, domain and
14089: courseid.
14090:
1.243 albertel 14091: =back
14092:
14093: =head2 Resource Subroutines
14094:
14095: =over 4
1.191 harris41 14096:
14097: =item *
14098:
1.243 albertel 14099: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 14100:
14101: =item *
14102:
1.243 albertel 14103: repcopy($filename) : subscribes to the requested file, and attempts to
14104: replicate from the owning library server, Might return
1.607 raeburn 14105: 'unavailable', 'not_found', 'forbidden', 'ok', or
14106: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 14107: resource. Expects the local filesystem pathname
14108: (/home/httpd/html/res/....)
14109:
14110: =back
14111:
14112: =head2 Resource Information
14113:
14114: =over 4
1.191 harris41 14115:
14116: =item *
14117:
1.1228 raeburn 14118: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
14119: and returns the value of a variety of different possible values,
14120: $varname should be a request string, and the other parameters can be
14121: used to specify who and what one is asking about. Ordinarily, $cid
14122: does not need to be specified, as it is retrived from
14123: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
14124: within lonuserstate::loadmap() when initializing a course, before
14125: $env{'request.course.id'} has been set, so it needs to be provided
14126: in that one case.
1.243 albertel 14127:
14128: Possible values for $varname are environment.lastname (or other item
14129: from the envirnment hash), user.name (or someother aspect about the
14130: user), resource.0.maxtries (or some other part and parameter of a
14131: resource)
1.204 albertel 14132:
14133: =item *
14134:
1.243 albertel 14135: directcondval($number) : get current value of a condition; reads from a state
14136: string
1.204 albertel 14137:
14138: =item *
14139:
1.243 albertel 14140: condval($condidx) : value of condition index based on state
1.204 albertel 14141:
14142: =item *
14143:
1.243 albertel 14144: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
14145: resource's metadata, $what should be either a specific key, or either
14146: 'keys' (to get a list of possible keys) or 'packages' to get a list of
14147: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
14148:
14149: this function automatically caches all requests
1.191 harris41 14150:
14151: =item *
14152:
1.243 albertel 14153: metadata_query($query,$custom,$customshow) : make a metadata query against the
14154: network of library servers; returns file handle of where SQL and regex results
14155: will be stored for query
1.191 harris41 14156:
14157: =item *
14158:
1.1282 raeburn 14159: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
14160: return symbolic list entry (all arguments optional).
14161:
14162: Args: filename is the filename (including path) for the file for which a symb
14163: is required; donotrecurse, if true will prevent calls to allowed() being made
14164: to check access status if more than one resource was found in the bighash
14165: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
14166: a randompick); ignorecachednull, if true will prevent a symb of '' being
14167: returned if $env{$cache_str} is defined as ''; checkforblock if true will
14168: cause possible symbs to be checked to determine if they are subject to content
14169: blocking, if so they will not be included as possible symbs; possibles is a
14170: ref to a hash, which, as a side effect, will be populated with all possible
14171: symbs (content blocking not tested).
14172:
1.243 albertel 14173: returns the data handle
1.191 harris41 14174:
14175: =item *
14176:
1.1190 raeburn 14177: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
14178: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 14179: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190 raeburn 14180: on failure, user must be in a course, as it assumes the existence of
14181: the course initial hash, and uses $env('request.course.id'}. The third
14182: arg is an optional reference to a scalar. If this arg is passed in the
14183: call to symbverify, it will be set to 1 if the symb has been set to be
14184: encrypted; otherwise it will be null.
1.191 harris41 14185:
14186: =item *
14187:
1.243 albertel 14188: symbclean($symb) : removes versions numbers from a symb, returns the
14189: cleaned symb
1.191 harris41 14190:
14191: =item *
14192:
1.243 albertel 14193: is_on_map($uri) : checks if the $uri is somewhere on the current
14194: course map, user must be in a course for it to work.
1.191 harris41 14195:
14196: =item *
14197:
1.243 albertel 14198: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 14199:
14200: =item *
14201:
1.243 albertel 14202: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
14203: a random seed, all arguments are optional, if they aren't sent it uses the
14204: environment to derive them. Note: if symb isn't sent and it can't get one
14205: from &symbread it will use the current time as its return value
1.191 harris41 14206:
14207: =item *
14208:
1.243 albertel 14209: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
14210: unfakeable, receipt
1.191 harris41 14211:
14212: =item *
14213:
1.620 albertel 14214: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 14215:
14216: =item *
14217:
1.243 albertel 14218: countacc($url) : count the number of accesses to a given URL
1.191 harris41 14219:
14220: =item *
14221:
1.243 albertel 14222: 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 14223:
14224: =item *
14225:
1.243 albertel 14226: 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 14227:
14228: =item *
14229:
1.243 albertel 14230: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 14231:
14232: =item *
14233:
1.243 albertel 14234: devalidate($symb) : devalidate temporary spreadsheet calculations,
14235: forcing spreadsheet to reevaluate the resource scores next time.
14236:
1.1195 raeburn 14237: =item *
14238:
1.1196 raeburn 14239: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
14240: when viewing in course context.
1.1195 raeburn 14241:
1.1196 raeburn 14242: input: six args -- filename (decluttered), course number, course domain,
14243: url, symb (if registered) and group (if this is a
14244: group item -- e.g., bulletin board, group page etc.).
1.1195 raeburn 14245:
1.1196 raeburn 14246: output: array of five scalars --
1.1195 raeburn 14247: $cfile -- url for file editing if editable on current server
14248: $home -- homeserver of resource (i.e., for author if published,
14249: or course if uploaded.).
14250: $switchserver -- 1 if server switch will be needed.
1.1196 raeburn 14251: $forceedit -- 1 if icon/link should be to go to edit mode
14252: $forceview -- 1 if icon/link should be to go to view mode
1.1195 raeburn 14253:
14254: =item *
14255:
14256: is_course_upload($file,$cnum,$cdom)
14257:
14258: Used in course context to determine if current file was uploaded to
14259: the course (i.e., would be found in /userfiles/docs on the course's
14260: homeserver.
14261:
14262: input: 3 args -- filename (decluttered), course number and course domain.
14263: output: boolean -- 1 if file was uploaded.
14264:
1.243 albertel 14265: =back
14266:
14267: =head2 Storing/Retreiving Data
14268:
14269: =over 4
1.191 harris41 14270:
14271: =item *
14272:
1.1269 raeburn 14273: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
14274: permanently for this url; hashref needs to be given and should be a \%hashname;
14275: the remaining args aren't required and if they aren't passed or are '' they will
14276: be derived from the env (with the exception of $laststore, which is an
14277: optional arg used when a user's submission is stored in grading).
14278: $laststore is $version=$timestamp, where $version is the most recent version
14279: number retrieved for the corresponding $symb in the $namespace db file, and
14280: $timestamp is the timestamp for that transaction (UNIX time).
14281: $laststore is currently only passed when cstore() is called by
14282: structuretags::finalize_storage().
1.191 harris41 14283:
14284: =item *
14285:
1.1269 raeburn 14286: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
14287: but uses critical subroutine
1.191 harris41 14288:
14289: =item *
14290:
1.243 albertel 14291: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
14292: all args are optional
1.191 harris41 14293:
14294: =item *
14295:
1.717 albertel 14296: dumpstore($namespace,$udom,$uname,$regexp,$range) :
14297: dumps the complete (or key matching regexp) namespace into a hash
14298: ($udom, $uname, $regexp, $range are optional) for a namespace that is
14299: normally &store()ed into
14300:
14301: $range should be either an integer '100' (give me the first 100
14302: matching records)
14303: or be two integers sperated by a - with no spaces
14304: '30-50' (give me the 30th through the 50th matching
14305: records)
14306:
14307:
14308: =item *
14309:
1.1269 raeburn 14310: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 14311: replaces a &store() version of data with a replacement set of data
14312: for a particular resource in a namespace passed in the $storehash hash
1.1269 raeburn 14313: reference. If $tolog is true, the transaction is logged in the courselog
14314: with an action=PUTSTORE.
1.717 albertel 14315:
14316: =item *
14317:
1.243 albertel 14318: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
14319: works very similar to store/cstore, but all data is stored in a
14320: temporary location and can be reset using tmpreset, $storehash should
14321: be a hash reference, returns nothing on success
1.191 harris41 14322:
14323: =item *
14324:
1.243 albertel 14325: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
14326: similar to restore, but all data is stored in a temporary location and
14327: can be reset using tmpreset. Returns a hash of values on success,
14328: error string otherwise.
1.191 harris41 14329:
14330: =item *
14331:
1.243 albertel 14332: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
14333: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 14334:
14335: =item *
14336:
1.243 albertel 14337: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14338: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 14339:
14340: =item *
14341:
1.243 albertel 14342: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
14343: namesp ($udom and $uname are optional)
1.191 harris41 14344:
14345: =item *
14346:
1.702 albertel 14347: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 14348: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 14349: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 14350:
1.702 albertel 14351: $range should be either an integer '100' (give me the first 100
14352: matching records)
14353: or be two integers sperated by a - with no spaces
14354: '30-50' (give me the 30th through the 50th matching
14355: records)
1.449 matthew 14356: =item *
14357:
14358: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
14359: $store can be a scalar, an array reference, or if the amount to be
14360: incremented is > 1, a hash reference.
14361:
14362: ($udom and $uname are optional)
1.191 harris41 14363:
14364: =item *
14365:
1.243 albertel 14366: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
14367: ($udom and $uname are optional)
1.191 harris41 14368:
14369: =item *
14370:
1.243 albertel 14371: cput($namespace,$storehash,$udom,$uname) : critical put
14372: ($udom and $uname are optional)
1.191 harris41 14373:
14374: =item *
14375:
1.748 albertel 14376: newput($namespace,$storehash,$udom,$uname) :
14377:
14378: Attempts to store the items in the $storehash, but only if they don't
14379: currently exist, if this succeeds you can be certain that you have
14380: successfully created a new key value pair in the $namespace db.
14381:
14382:
14383: Args:
14384: $namespace: name of database to store values to
14385: $storehash: hashref to store to the db
14386: $udom: (optional) domain of user containing the db
14387: $uname: (optional) name of user caontaining the db
14388:
14389: Returns:
14390: 'ok' -> succeeded in storing all keys of $storehash
14391: 'key_exists: <key>' -> failed to anything out of $storehash, as at
14392: least <key> already existed in the db (other
14393: requested keys may also already exist)
1.967 bisitz 14394: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 14395: 'con_lost' -> unable to contact request server
14396: 'refused' -> action was not allowed by remote machine
14397:
14398:
14399: =item *
14400:
1.243 albertel 14401: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14402: reference filled in from namesp (encrypts the return communication)
14403: ($udom and $uname are optional)
1.191 harris41 14404:
14405: =item *
14406:
1.243 albertel 14407: log($udom,$name,$home,$message) : write to permanent log for user; use
14408: critical subroutine
14409:
1.806 raeburn 14410: =item *
14411:
1.860 raeburn 14412: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
14413: array reference filled in from namespace found in domain level on either
14414: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 14415:
14416: =item *
14417:
1.860 raeburn 14418: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
14419: domain level either on specified domain server ($uhome) or primary domain
14420: server ($udom and $uhome are optional)
1.806 raeburn 14421:
1.943 raeburn 14422: =item *
14423:
1.1240 raeburn 14424: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
14425: for: authentication, language, quotas, timezone, date locale, and portal URL in
14426: the target domain.
14427:
14428: May also include additional key => value pairs for the following groups:
14429:
14430: =over
14431:
14432: =item
14433: disk quotas (MB allocated by default to portfolios and authoring spaces).
14434:
14435: =over
14436:
14437: =item defaultquota, authorquota
14438:
14439: =back
14440:
14441: =item
14442: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
14443: portfolio for users).
14444:
14445: =over
14446:
14447: =item
14448: aboutme, blog, webdav, portfolio
14449:
14450: =back
14451:
14452: =item
14453: requestcourses: ability to request courses, and how requests are processed.
14454:
14455: =over
14456:
14457: =item
1.1305 raeburn 14458: official, unofficial, community, textbook, placement
1.1240 raeburn 14459:
14460: =back
14461:
14462: =item
14463: inststatus: types of institutional affiliation, and order in which they are displayed.
14464:
14465: =over
14466:
14467: =item
1.1256 raeburn 14468: inststatustypes, inststatusorder, inststatusguest
1.1240 raeburn 14469:
14470: =back
14471:
14472: =item
14473: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
14474: for course's uploaded content.
14475:
14476: =over
14477:
14478: =item
1.1246 raeburn 14479: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
1.1305 raeburn 14480: communityquota, textbookquota, placementquota
1.1240 raeburn 14481:
14482: =back
14483:
14484: =item
14485: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
14486: on your servers.
14487:
14488: =over
14489:
14490: =item
14491: remotesessions, hostedsessions
14492:
14493: =back
14494:
14495: =back
14496:
14497: In cases where a domain coordinator has never used the "Set Domain Configuration"
14498: utility to create a configuration.db file on a domain's primary library server
14499: only the following domain defaults: auth_def, auth_arg_def, lang_def
14500: -- corresponding values are authentication type (internal, krb4, krb5,
14501: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
14502: will be available. Values are retrieved from cache (if current), unless the
14503: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
14504: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
14505:
14506: Typical usage:
1.943 raeburn 14507:
1.1240 raeburn 14508: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 14509:
1.243 albertel 14510: =back
14511:
14512: =head2 Network Status Functions
14513:
14514: =over 4
1.191 harris41 14515:
14516: =item *
14517:
1.1137 raeburn 14518: dirlist() : return directory list based on URI (first arg).
14519:
14520: Inputs: 1 required, 5 optional.
14521:
14522: =over
14523:
14524: =item
14525: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
14526:
14527: =item
14528: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
14529:
14530: =item
14531: $username - username of user/course to be listed. Extracted from $uri if absent.
14532:
14533: =item
14534: $getpropath - boolean: 1 if prepend path using &propath().
14535:
14536: =item
14537: $getuserdir - boolean: 1 if prepend path for "userfiles".
14538:
14539: =item
14540: $alternateRoot - path to prepend in place of path from $uri.
14541:
14542: =back
14543:
14544: Returns: Array of up to two items.
14545:
14546: =over
14547:
14548: a reference to an array of files/subdirectories
14549:
14550: =over
14551:
14552: Each element in the array of files/subdirectories is a & separated list of
14553: item name and the result of running stat on the item. If dirlist was requested
14554: for a file instead of a directory, the item name will be ''. For a directory
14555: listing, if the item is a metadata file, the element will end &N&M
14556: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
14557: default copyright set (1).
14558:
14559: =back
14560:
14561: a scalar containing error condition (if encountered).
14562:
14563: =over
14564:
14565: =item
14566: no_host (no homeserver identified for $username:$domain).
14567:
14568: =item
14569: no_such_host (server contacted for listing not identified as valid host).
14570:
14571: =item
14572: con_lost (connection to remote server failed).
14573:
14574: =item
14575: refused (invalid $username:$domain received on lond side).
14576:
14577: =item
14578: no_such_dir (directory at specified path on lond side does not exist).
14579:
14580: =item
14581: empty (directory at specified path on lond side is empty).
14582:
14583: =over
14584:
14585: This is currently not encountered because the &ls3, &ls2,
14586: &ls (_handler) routines on the lond side do not filter out
14587: . and .. from a directory listing.
14588:
14589: =back
14590:
14591: =back
14592:
14593: =back
1.191 harris41 14594:
14595: =item *
14596:
1.243 albertel 14597: spareserver() : find server with least workload from spare.tab
14598:
1.986 foxr 14599:
14600: =item *
14601:
14602: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
14603: if there is no corresponding loncapa host.
14604:
1.243 albertel 14605: =back
14606:
1.986 foxr 14607:
1.243 albertel 14608: =head2 Apache Request
14609:
14610: =over 4
1.191 harris41 14611:
14612: =item *
14613:
1.243 albertel 14614: ssi($url,%hash) : server side include, does a complete request cycle on url to
14615: localhost, posts hash
14616:
14617: =back
14618:
14619: =head2 Data to String to Data
14620:
14621: =over 4
1.191 harris41 14622:
14623: =item *
14624:
1.243 albertel 14625: hash2str(%hash) : convert a hash into a string complete with escaping and '='
14626: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 14627:
14628: =item *
14629:
1.243 albertel 14630: hashref2str($hashref) : convert a hashref into a string complete with
14631: escaping and '=' and '&' separators, supports elements that are
14632: arrayrefs and hashrefs
1.191 harris41 14633:
14634: =item *
14635:
1.243 albertel 14636: arrayref2str($arrayref) : convert an arrayref into a string complete
14637: with escaping and '&' separators, supports elements that are arrayrefs
14638: and hashrefs
1.191 harris41 14639:
14640: =item *
14641:
1.243 albertel 14642: str2hash($string) : convert string to hash using unescaping and
14643: splitting on '=' and '&', supports elements that are arrayrefs and
14644: hashrefs
1.191 harris41 14645:
14646: =item *
14647:
1.243 albertel 14648: str2array($string) : convert string to hash using unescaping and
14649: splitting on '&', supports elements that are arrayrefs and hashrefs
14650:
14651: =back
14652:
14653: =head2 Logging Routines
14654:
14655:
14656: These routines allow one to make log messages in the lonnet.log and
14657: lonnet.perm logfiles.
1.191 harris41 14658:
1.1119 foxr 14659: =over 4
14660:
1.191 harris41 14661: =item *
14662:
1.243 albertel 14663: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 14664:
14665: =item *
14666:
1.243 albertel 14667: logthis() : append message to the normal lonnet.log file, it gets
14668: preiodically rolled over and deleted.
1.191 harris41 14669:
14670: =item *
14671:
1.243 albertel 14672: logperm() : append a permanent message to lonnet.perm.log, this log
14673: file never gets deleted by any automated portion of the system, only
14674: messages of critical importance should go in here.
14675:
1.1119 foxr 14676:
1.243 albertel 14677: =back
14678:
14679: =head2 General File Helper Routines
14680:
14681: =over 4
1.191 harris41 14682:
14683: =item *
14684:
1.481 raeburn 14685: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
14686: (a) files in /uploaded
14687: (i) If a local copy of the file exists -
14688: compares modification date of local copy with last-modified date for
14689: definitive version stored on home server for course. If local copy is
14690: stale, requests a new version from the home server and stores it.
14691: If the original has been removed from the home server, then local copy
14692: is unlinked.
14693: (ii) If local copy does not exist -
14694: requests the file from the home server and stores it.
14695:
14696: If $caller is 'uploadrep':
14697: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
14698: for request for files originally uploaded via DOCS.
14699: - returns 'ok' if fresh local copy now available, -1 otherwise.
14700:
14701: Otherwise:
14702: This indicates a call from the content generation phase of the request.
14703: - returns the entire contents of the file or -1.
14704:
14705: (b) files in /res
14706: - returns the entire contents of a file or -1;
14707: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 14708:
1.712 albertel 14709:
14710: =item *
14711:
14712: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
14713: reference
14714:
14715: returns either a stat() list of data about the file or an empty list
14716: if the file doesn't exist or couldn't find out about it (connection
14717: problems or user unknown)
14718:
1.191 harris41 14719: =item *
14720:
1.243 albertel 14721: filelocation($dir,$file) : returns file system location of a file
14722: based on URI; meant to be "fairly clean" absolute reference, $dir is a
14723: directory that relative $file lookups are to looked in ($dir of /a/dir
14724: and a file of ../bob will become /a/bob)
1.191 harris41 14725:
14726: =item *
14727:
14728: hreflocation($dir,$file) : returns file system location or a URL; same as
14729: filelocation except for hrefs
14730:
14731: =item *
14732:
1.1261 raeburn 14733: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
14734: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 14735:
1.243 albertel 14736: =back
14737:
1.608 albertel 14738: =head2 Usererfile file routines (/uploaded*)
14739:
14740: =over 4
14741:
14742: =item *
14743:
14744: userfileupload(): main rotine for putting a file in a user or course's
14745: filespace, arguments are,
14746:
1.620 albertel 14747: formname - required - this is the name of the element in $env where the
1.608 albertel 14748: filename, and the contents of the file to create/modifed exist
1.620 albertel 14749: the filename is in $env{'form.'.$formname.'.filename'} and the
14750: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 14751: context - if coursedoc, store the file in the course of the active role
14752: of the current user;
14753: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
14754: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 14755: subdir - required - subdirectory to put the file in under ../userfiles/
14756: if undefined, it will be placed in "unknown"
14757:
14758: (This routine calls clean_filename() to remove any dangerous
14759: characters from the filename, and then calls finuserfileupload() to
14760: complete the transaction)
14761:
14762: returns either the url of the uploaded file (/uploaded/....) if successful
14763: and /adm/notfound.html if unsuccessful
14764:
14765: =item *
14766:
14767: clean_filename(): routine for cleaing a filename up for storage in
14768: userfile space, argument is:
14769:
14770: filename - proposed filename
14771:
14772: returns: the new clean filename
14773:
14774: =item *
14775:
1.1090 raeburn 14776: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 14777: userspace, probably shouldn't be called directly
14778:
14779: docuname: username or courseid of destination for the file
14780: docudom: domain of user/course of destination for the file
14781: formname: same as for userfileupload()
1.1090 raeburn 14782: fname: filename (including subdirectories) for the file
14783: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
14784: allfiles: reference to hash used to store objects found by parser
14785: codebase: reference to hash used for codebases of java objects found by parser
14786: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
14787: thumbheight: height (pixels) of thumbnail to be created for uploaded image
14788: resizewidth: width to be used to resize image using resizeImage from ImageMagick
14789: resizeheight: height to be used to resize image using resizeImage from ImageMagick
14790: context: if 'overwrite', will move the uploaded file from its temporary location to
14791: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 14792: mimetype: reference to scalar to accommodate mime type determined
14793: from File::MMagic if $parser = parse.
1.608 albertel 14794:
14795: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 14796: and /adm/notfound.html if unsuccessful (or an error message if context
14797: was 'overwrite').
14798:
1.608 albertel 14799:
14800: =item *
14801:
14802: renameuserfile(): renames an existing userfile to a new name
14803:
14804: Args:
14805: docuname: username or courseid of destination for the file
14806: docudom: domain of user/course of destination for the file
14807: old: current file name (including any subdirs under userfiles)
14808: new: desired file name (including any subdirs under userfiles)
14809:
14810: =item *
14811:
14812: mkdiruserfile(): creates a directory is a userfiles dir
14813:
14814: Args:
14815: docuname: username or courseid of destination for the file
14816: docudom: domain of user/course of destination for the file
14817: dir: dir to create (including any subdirs under userfiles)
14818:
14819: =item *
14820:
14821: removeuserfile(): removes a file that exists in userfiles
14822:
14823: Args:
14824: docuname: username or courseid of destination for the file
14825: docudom: domain of user/course of destination for the file
14826: fname: filname to delete (including any subdirs under userfiles)
14827:
14828: =item *
14829:
14830: removeuploadedurl(): convience function for removeuserfile()
14831:
14832: Args:
14833: url: a full /uploaded/... url to delete
14834:
1.747 albertel 14835: =item *
14836:
14837: get_portfile_permissions():
14838: Args:
14839: domain: domain of user or course contain the portfolio files
14840: user: name of user or num of course contain the portfolio files
14841: Returns:
14842: hashref of a dump of the proper file_permissions.db
14843:
14844:
14845: =item *
14846:
14847: get_access_controls():
14848:
14849: Args:
14850: current_permissions: the hash ref returned from get_portfile_permissions()
14851: group: (optional) the group you want the files associated with
14852: file: (optional) the file you want access info on
14853:
14854: Returns:
1.749 raeburn 14855: a hash (keys are file names) of hashes containing
14856: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
14857: values are XML containing access control settings (see below)
1.747 albertel 14858:
14859: Internal notes:
14860:
1.749 raeburn 14861: access controls are stored in file_permissions.db as key=value pairs.
14862: key -> path to file/file_name\0uniqueID:scope_end_start
14863: where scope -> public,guest,course,group,domains or users.
14864: end -> UNIX time for end of access (0 -> no end date)
14865: start -> UNIX time for start of access
14866:
14867: value -> XML description of access control
14868: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
14869: <start></start>
14870: <end></end>
14871:
14872: <password></password> for scope type = guest
14873:
14874: <domain></domain> for scope type = course or group
14875: <number></number>
14876: <roles id="">
14877: <role></role>
14878: <access></access>
14879: <section></section>
14880: <group></group>
14881: </roles>
14882:
14883: <dom></dom> for scope type = domains
14884:
14885: <users> for scope type = users
14886: <user>
14887: <uname></uname>
14888: <udom></udom>
14889: </user>
14890: </users>
14891: </scope>
14892:
14893: Access data is also aggregated for each file in an additional key=value pair:
14894: key -> path to file/file_name\0accesscontrol
14895: value -> reference to hash
14896: hash contains key = value pairs
14897: where key = uniqueID:scope_end_start
14898: value = UNIX time record was last updated
14899:
14900: Used to improve speed of look-ups of access controls for each file.
14901:
14902: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
14903:
1.1198 raeburn 14904: =item *
14905:
1.749 raeburn 14906: modify_access_controls():
14907:
14908: Modifies access controls for a portfolio file
14909: Args
14910: 1. file name
14911: 2. reference to hash of required changes,
14912: 3. domain
14913: 4. username
14914: where domain,username are the domain of the portfolio owner
14915: (either a user or a course)
14916:
14917: Returns:
14918: 1. result of additions or updates ('ok' or 'error', with error message).
14919: 2. result of deletions ('ok' or 'error', with error message).
14920: 3. reference to hash of any new or updated access controls.
14921: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
14922: key = integer (inbound ID)
1.1198 raeburn 14923: value = uniqueID
14924:
14925: =item *
14926:
14927: get_timebased_id():
14928:
14929: Attempts to get a unique timestamp-based suffix for use with items added to a
14930: course via the Course Editor (e.g., folders, composite pages,
14931: group bulletin boards).
14932:
14933: Args: (first three required; six others optional)
14934:
14935: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
14936: docssequence, or name of group
14937:
14938: 2. keyid (alphanumeric): name of temporary locking key in hash,
14939: e.g., num, boardids
14940:
14941: 3. namespace: name of gdbm file used to store suffixes already assigned;
14942: file will be named nohist_namespace.db
14943:
14944: 4. cdom: domain of course; default is current course domain from %env
14945:
14946: 5. cnum: course number; default is current course number from %env
14947:
14948: 6. idtype: set to concat if an additional digit is to be appended to the
14949: unix timestamp to form the suffix, if the plain timestamp is already
14950: in use. Default is to not do this, but simply increment the unix
14951: timestamp by 1 until a unique key is obtained.
14952:
14953: 7. who: holder of locking key; defaults to user:domain for user.
14954:
14955: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
14956: retrying); default is 3.
14957:
14958: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
14959:
14960: Returns:
14961:
14962: 1. suffix obtained (numeric)
14963:
14964: 2. result of deleting locking key (ok if deleted, or lock never obtained)
14965:
14966: 3. error: contains (localized) error message if an error occurred.
14967:
1.747 albertel 14968:
1.608 albertel 14969: =back
14970:
1.243 albertel 14971: =head2 HTTP Helper Routines
14972:
14973: =over 4
14974:
1.191 harris41 14975: =item *
14976:
14977: escape() : unpack non-word characters into CGI-compatible hex codes
14978:
14979: =item *
14980:
14981: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
14982:
1.243 albertel 14983: =back
14984:
14985: =head1 PRIVATE SUBROUTINES
14986:
14987: =head2 Underlying communication routines (Shouldn't call)
14988:
14989: =over 4
14990:
14991: =item *
14992:
14993: subreply() : tries to pass a message to lonc, returns con_lost if incapable
14994:
14995: =item *
14996:
14997: reply() : uses subreply to send a message to remote machine, logs all failures
14998:
14999: =item *
15000:
15001: critical() : passes a critical message to another server; if cannot
15002: get through then place message in connection buffer directory and
15003: returns con_delayed, if incapable of saving message, returns
15004: con_failed
15005:
15006: =item *
15007:
15008: reconlonc() : tries to reconnect lonc client processes.
15009:
15010: =back
15011:
15012: =head2 Resource Access Logging
15013:
15014: =over 4
15015:
15016: =item *
15017:
15018: flushcourselogs() : flush (save) buffer logs and access logs
15019:
15020: =item *
15021:
15022: courselog($what) : save message for course in hash
15023:
15024: =item *
15025:
15026: courseacclog($what) : save message for course using &courselog(). Perform
15027: special processing for specific resource types (problems, exams, quizzes, etc).
15028:
1.191 harris41 15029: =item *
15030:
15031: goodbye() : flush course logs and log shutting down; it is called in srm.conf
15032: as a PerlChildExitHandler
1.243 albertel 15033:
15034: =back
15035:
15036: =head2 Other
15037:
15038: =over 4
15039:
15040: =item *
15041:
15042: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 15043:
15044: =back
15045:
15046: =cut
1.877 foxr 15047:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>