Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1343
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1343 ! raeburn 4: # $Id: lonnet.pm,v 1.1342 2017/03/26 22:24:03 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.1337 raeburn 653: my ($linkname,$pubname);
1.1155 raeburn 654: if ($name eq '') {
655: $name = 'lonID';
1.1337 raeburn 656: $linkname = 'lonLinkID';
657: $pubname = 'lonPubID';
1.1155 raeburn 658: }
659: my $lonid=$cookies{$name};
1.1337 raeburn 660: if (!$lonid) {
661: if (($name eq 'lonID') && ($ENV{'SERVER_PORT'} != 443) && ($linkname)) {
662: $lonid=$cookies{$linkname};
663: }
664: if (!$lonid) {
665: if (($name eq 'lonID') && ($pubname)) {
666: $lonid=$cookies{$pubname};
667: }
668: }
669: }
1.916 albertel 670: return undef if (!$lonid);
671:
672: my $handle=&LONCAPA::clean_handle($lonid->value);
1.1155 raeburn 673: my $lonidsdir;
674: if ($name eq 'lonDAV') {
675: $lonidsdir=$r->dir_config('lonDAVsessDir');
676: } else {
677: $lonidsdir=$r->dir_config('lonIDsDir');
678: }
1.916 albertel 679: return undef if (!-e "$lonidsdir/$handle.id");
680:
1.917 albertel 681: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
682: return undef if (!$opened);
1.916 albertel 683:
684: flock($idf,LOCK_SH);
685: my %disk_env;
686: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
687: &GDBM_READER(),0640)) {
688: return undef;
689: }
690:
691: if (!defined($disk_env{'user.name'})
692: || !defined($disk_env{'user.domain'})) {
693: return undef;
694: }
1.1245 raeburn 695:
696: if (ref($userhashref) eq 'HASH') {
697: $userhashref->{'name'} = $disk_env{'user.name'};
698: $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1212 raeburn 699: }
1.1245 raeburn 700:
1.916 albertel 701: return $handle;
702: }
703:
1.830 albertel 704: sub timed_flock {
705: my ($file,$lock_type) = @_;
706: my $failed=0;
707: eval {
708: local $SIG{__DIE__}='DEFAULT';
709: local $SIG{ALRM}=sub {
710: $failed=1;
711: die("failed lock");
712: };
713: alarm(13);
714: flock($file,$lock_type);
715: alarm(0);
716: };
717: if ($failed) {
718: return undef;
719: } else {
720: return 1;
721: }
722: }
723:
1.5 www 724: # ---------------------------------------------------------- Append Environment
725:
726: sub appenv {
1.949 raeburn 727: my ($newenv,$roles) = @_;
728: if (ref($newenv) eq 'HASH') {
729: foreach my $key (keys(%{$newenv})) {
730: my $refused = 0;
731: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
732: $refused = 1;
733: if (ref($roles) eq 'ARRAY') {
1.1250 raeburn 734: my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949 raeburn 735: if (grep(/^\Q$role\E$/,@{$roles})) {
736: $refused = 0;
737: }
738: }
739: }
740: if ($refused) {
741: &logthis("<font color=\"blue\">WARNING: ".
742: "Attempt to modify environment ".$key." to ".$newenv->{$key}
743: .'</font>');
744: delete($newenv->{$key});
745: } else {
746: $env{$key}=$newenv->{$key};
747: }
748: }
749: my $opened = open(my $env_file,'+<',$env{'user.environment'});
750: if ($opened
751: && &timed_flock($env_file,LOCK_EX)
752: &&
753: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
754: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
755: while (my ($key,$value) = each(%{$newenv})) {
756: $disk_env{$key} = $value;
757: }
758: untie(%disk_env);
1.35 www 759: }
1.191 harris41 760: }
1.56 www 761: return 'ok';
762: }
763: # ----------------------------------------------------- Delete from Environment
764:
765: sub delenv {
1.1104 raeburn 766: my ($delthis,$regexp,$roles) = @_;
767: if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
768: my $refused = 1;
769: if (ref($roles) eq 'ARRAY') {
770: my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
771: if (grep(/^\Q$role\E$/,@{$roles})) {
772: $refused = 0;
773: }
774: }
775: if ($refused) {
776: &logthis("<font color=\"blue\">WARNING: ".
777: "Attempt to delete from environment ".$delthis);
778: return 'error';
779: }
1.56 www 780: }
1.917 albertel 781: my $opened = open(my $env_file,'+<',$env{'user.environment'});
782: if ($opened
1.915 albertel 783: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 784: &&
785: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
786: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 787: foreach my $key (keys(%disk_env)) {
1.987 raeburn 788: if ($regexp) {
789: if ($key=~/^$delthis/) {
790: delete($env{$key});
791: delete($disk_env{$key});
792: }
793: } else {
794: if ($key=~/^\Q$delthis\E/) {
795: delete($env{$key});
796: delete($disk_env{$key});
797: }
798: }
1.448 albertel 799: }
1.783 albertel 800: untie(%disk_env);
1.5 www 801: }
802: return 'ok';
1.369 albertel 803: }
804:
1.790 albertel 805: sub get_env_multiple {
806: my ($name) = @_;
807: my @values;
808: if (defined($env{$name})) {
809: # exists is it an array
810: if (ref($env{$name})) {
811: @values=@{ $env{$name} };
812: } else {
813: $values[0]=$env{$name};
814: }
815: }
816: return(@values);
817: }
818:
1.958 www 819: # ------------------------------------------------------------------- Locking
820:
821: sub set_lock {
822: my ($text)=@_;
823: $locknum++;
824: my $id=$$.'-'.$locknum;
825: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
826: 'session.lock.'.$id => $text});
827: return $id;
828: }
829:
830: sub get_locks {
831: my $num=0;
832: my %texts=();
833: foreach my $lock (split(/\,/,$env{'session.locks'})) {
834: if ($lock=~/\w/) {
835: $num++;
836: $texts{$lock}=$env{'session.lock.'.$lock};
837: }
838: }
839: return ($num,%texts);
840: }
841:
842: sub remove_lock {
843: my ($id)=@_;
844: my $newlocks='';
845: foreach my $lock (split(/\,/,$env{'session.locks'})) {
846: if (($lock=~/\w/) && ($lock ne $id)) {
847: $newlocks.=','.$lock;
848: }
849: }
850: &appenv({'session.locks' => $newlocks});
851: &delenv('session.lock.'.$id);
852: }
853:
854: sub remove_all_locks {
855: my $activelocks=$env{'session.locks'};
856: foreach my $lock (split(/\,/,$env{'session.locks'})) {
857: if ($lock=~/\w/) {
858: &remove_lock($lock);
859: }
860: }
861: }
862:
863:
1.369 albertel 864: # ------------------------------------------ Find out current server userload
865: sub userload {
866: my $numusers=0;
867: {
868: opendir(LONIDS,$perlvar{'lonIDsDir'});
869: my $filename;
870: my $curtime=time;
871: while ($filename=readdir(LONIDS)) {
1.925 albertel 872: next if ($filename eq '.' || $filename eq '..');
873: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 874: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 875: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 876: }
877: closedir(LONIDS);
878: }
879: my $userloadpercent=0;
880: my $maxuserload=$perlvar{'lonUserLoadLim'};
881: if ($maxuserload) {
1.371 albertel 882: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 883: }
1.372 albertel 884: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 885: return $userloadpercent;
1.283 www 886: }
887:
1.1 albertel 888: # ------------------------------ Find server with least workload from spare.tab
1.11 www 889:
1.1 albertel 890: sub spareserver {
1.1083 raeburn 891: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 892: my $spare_server;
1.370 albertel 893: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 894: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
895: : $userloadpercent;
1.1083 raeburn 896: my ($uint_dom,$remotesessions);
897: if (($udom ne '') && (&domain($udom) ne '')) {
898: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
899: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
900: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
901: $remotesessions = $udomdefaults{'remotesessions'};
902: }
1.1123 raeburn 903: my $spareshash = &this_host_spares($udom);
904: if (ref($spareshash) eq 'HASH') {
905: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
906: foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279 raeburn 907: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
908: $try_server));
1.1123 raeburn 909: ($spare_server, $lowest_load) =
910: &compare_server_load($try_server, $spare_server, $lowest_load);
911: }
1.1083 raeburn 912: }
1.784 albertel 913:
1.1123 raeburn 914: my $found_server = ($spare_server ne '' && $lowest_load < 100);
915:
916: if (!$found_server) {
917: if (ref($spareshash->{'default'}) eq 'ARRAY') {
918: foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279 raeburn 919: next unless (&spare_can_host($udom,$uint_dom,
920: $remotesessions,$try_server));
1.1123 raeburn 921: ($spare_server, $lowest_load) =
922: &compare_server_load($try_server, $spare_server, $lowest_load);
923: }
924: }
925: }
1.784 albertel 926: }
927:
928: if (!$want_server_name) {
1.968 raeburn 929: my $protocol = 'http';
930: if ($protocol{$spare_server} eq 'https') {
931: $protocol = $protocol{$spare_server};
932: }
1.1001 raeburn 933: if (defined($spare_server)) {
934: my $hostname = &hostname($spare_server);
1.1083 raeburn 935: if (defined($hostname)) {
1.1001 raeburn 936: $spare_server = $protocol.'://'.$hostname;
937: }
938: }
1.784 albertel 939: }
940: return $spare_server;
941: }
942:
943: sub compare_server_load {
1.1253 raeburn 944: my ($try_server, $spare_server, $lowest_load, $required) = @_;
945:
946: if ($required) {
947: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
948: my $remoterev = &get_server_loncaparev(undef,$try_server);
949: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
950: if (($major eq '' && $minor eq '') ||
951: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
952: return ($spare_server,$lowest_load);
953: }
954: }
1.784 albertel 955:
956: my $loadans = &reply('load', $try_server);
957: my $userloadans = &reply('userload',$try_server);
958:
959: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114 raeburn 960: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 961: }
962:
963: my $load;
964: if ($loadans =~ /\d/) {
965: if ($userloadans =~ /\d/) {
966: #both are numbers, pick the bigger one
967: $load = ($loadans > $userloadans) ? $loadans
968: : $userloadans;
1.411 albertel 969: } else {
1.784 albertel 970: $load = $loadans;
1.411 albertel 971: }
1.784 albertel 972: } else {
973: $load = $userloadans;
974: }
975:
976: if (($load =~ /\d/) && ($load < $lowest_load)) {
977: $spare_server = $try_server;
978: $lowest_load = $load;
1.370 albertel 979: }
1.784 albertel 980: return ($spare_server,$lowest_load);
1.202 matthew 981: }
1.914 albertel 982:
983: # --------------------------- ask offload servers if user already has a session
984: sub find_existing_session {
985: my ($udom,$uname) = @_;
1.1123 raeburn 986: my $spareshash = &this_host_spares($udom);
987: if (ref($spareshash) eq 'HASH') {
988: if (ref($spareshash->{'primary'}) eq 'ARRAY') {
989: foreach my $try_server (@{ $spareshash->{'primary'} }) {
990: return $try_server if (&has_user_session($try_server, $udom, $uname));
991: }
992: }
993: if (ref($spareshash->{'default'}) eq 'ARRAY') {
994: foreach my $try_server (@{ $spareshash->{'default'} }) {
995: return $try_server if (&has_user_session($try_server, $udom, $uname));
996: }
997: }
1.914 albertel 998: }
999: return;
1000: }
1001:
1002: # -------------------------------- ask if server already has a session for user
1003: sub has_user_session {
1004: my ($lonid,$udom,$uname) = @_;
1005: my $result = &reply(join(':','userhassession',
1006: map {&escape($_)} ($udom,$uname)),$lonid);
1007: return 1 if ($result eq 'ok');
1008:
1009: return 0;
1010: }
1011:
1.1076 raeburn 1012: # --------- determine least loaded server in a user's domain which allows login
1013:
1014: sub choose_server {
1.1259 raeburn 1015: my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076 raeburn 1016: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077 raeburn 1017: my %servers = &get_servers($udom);
1.1076 raeburn 1018: my $lowest_load = 30000;
1.1259 raeburn 1019: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
1020: if ($skiploadbal) {
1021: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
1022: unless (defined($cached)) {
1023: my $cachetime = 60*60*24;
1024: my %domconfig =
1025: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1026: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1027: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
1028: $cachetime);
1029: }
1030: }
1031: }
1.1076 raeburn 1032: foreach my $lonhost (keys(%servers)) {
1.1259 raeburn 1033: if ($skiploadbal) {
1034: if (ref($balancers) eq 'HASH') {
1035: next if (exists($balancers->{$lonhost}));
1036: }
1037: }
1.1115 raeburn 1038: my $loginvia;
1039: if ($checkloginvia) {
1040: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116 raeburn 1041: if ($loginvia) {
1042: my ($server,$path) = split(/:/,$loginvia);
1043: ($login_host, $lowest_load) =
1.1253 raeburn 1044: &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116 raeburn 1045: if ($login_host eq $server) {
1046: $portal_path = $path;
1.1151 raeburn 1047: $isredirect = 1;
1.1116 raeburn 1048: }
1049: } else {
1050: ($login_host, $lowest_load) =
1.1253 raeburn 1051: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116 raeburn 1052: if ($login_host eq $lonhost) {
1053: $portal_path = '';
1.1151 raeburn 1054: $isredirect = '';
1.1116 raeburn 1055: }
1056: }
1057: } else {
1.1076 raeburn 1058: ($login_host, $lowest_load) =
1.1253 raeburn 1059: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076 raeburn 1060: }
1061: }
1062: if ($login_host ne '') {
1.1116 raeburn 1063: $hostname = &hostname($login_host);
1.1076 raeburn 1064: }
1.1331 raeburn 1065: return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076 raeburn 1066: }
1067:
1.202 matthew 1068: # --------------------------------------------- Try to change a user's password
1069:
1070: sub changepass {
1.799 raeburn 1071: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 1072: $currentpass = &escape($currentpass);
1073: $newpass = &escape($newpass);
1.1030 raeburn 1074: my $lonhost = $perlvar{'lonHostID'};
1075: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 1076: $server);
1077: if (! $answer) {
1078: &logthis("No reply on password change request to $server ".
1079: "by $uname in domain $udom.");
1080: } elsif ($answer =~ "^ok") {
1081: &logthis("$uname in $udom successfully changed their password ".
1082: "on $server.");
1083: } elsif ($answer =~ "^pwchange_failure") {
1084: &logthis("$uname in $udom was unable to change their password ".
1085: "on $server. The action was blocked by either lcpasswd ".
1086: "or pwchange");
1087: } elsif ($answer =~ "^non_authorized") {
1088: &logthis("$uname in $udom did not get their password correct when ".
1089: "attempting to change it on $server.");
1090: } elsif ($answer =~ "^auth_mode_error") {
1091: &logthis("$uname in $udom attempted to change their password despite ".
1092: "not being locally or internally authenticated on $server.");
1093: } elsif ($answer =~ "^unknown_user") {
1094: &logthis("$uname in $udom attempted to change their password ".
1095: "on $server but were unable to because $server is not ".
1096: "their home server.");
1097: } elsif ($answer =~ "^refused") {
1098: &logthis("$server refused to change $uname in $udom password because ".
1099: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 1100: } elsif ($answer =~ "invalid_client") {
1101: &logthis("$server refused to change $uname in $udom password because ".
1102: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 1103: }
1104: return $answer;
1.1 albertel 1105: }
1106:
1.169 harris41 1107: # ----------------------- Try to determine user's current authentication scheme
1108:
1109: sub queryauthenticate {
1110: my ($uname,$udom)=@_;
1.456 albertel 1111: my $uhome=&homeserver($uname,$udom);
1112: if (!$uhome) {
1113: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
1114: return 'no_host';
1115: }
1116: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
1117: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
1118: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 1119: }
1.456 albertel 1120: return $answer;
1.169 harris41 1121: }
1122:
1.1 albertel 1123: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1124:
1.1 albertel 1125: sub authenticate {
1.1073 raeburn 1126: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1127: $upass=&escape($upass);
1128: $uname= &LONCAPA::clean_username($uname);
1.836 www 1129: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1130: my $newhome;
1.836 www 1131: if ((!$uhome) || ($uhome eq 'no_host')) {
1132: # Maybe the machine was offline and only re-appeared again recently?
1133: &reconlonc();
1134: # One more
1.952 raeburn 1135: $uhome=&homeserver($uname,$udom,1);
1136: if (($uhome eq 'no_host') && $checkdefauth) {
1137: if (defined(&domain($udom,'primary'))) {
1138: $newhome=&domain($udom,'primary');
1139: }
1140: if ($newhome ne '') {
1141: $uhome = $newhome;
1142: }
1143: }
1.836 www 1144: if ((!$uhome) || ($uhome eq 'no_host')) {
1145: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1146: return 'no_host';
1147: }
1.1 albertel 1148: }
1.1073 raeburn 1149: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1150: if ($answer eq 'authorized') {
1.952 raeburn 1151: if ($newhome) {
1152: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1153: return 'no_account_on_host';
1154: } else {
1155: &logthis("User $uname at $udom authorized by $uhome");
1156: return $uhome;
1157: }
1.471 albertel 1158: }
1159: if ($answer eq 'non_authorized') {
1160: &logthis("User $uname at $udom rejected by $uhome");
1161: return 'no_host';
1.9 www 1162: }
1.471 albertel 1163: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1164: return 'no_host';
1165: }
1166:
1.1073 raeburn 1167: sub can_host_session {
1.1074 raeburn 1168: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073 raeburn 1169: my $canhost = 1;
1.1074 raeburn 1170: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073 raeburn 1171: if (ref($remotesessions) eq 'HASH') {
1172: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074 raeburn 1173: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073 raeburn 1174: $canhost = 0;
1175: } else {
1176: $canhost = 1;
1177: }
1178: }
1179: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074 raeburn 1180: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073 raeburn 1181: $canhost = 1;
1182: } else {
1183: $canhost = 0;
1184: }
1185: }
1186: if ($canhost) {
1187: if ($remotesessions->{'version'} ne '') {
1188: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1189: if ($reqmajor ne '' && $reqminor ne '') {
1190: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1191: my $major = $1;
1192: my $minor = $2;
1193: if (($major < $reqmajor ) ||
1194: (($major == $reqmajor) && ($minor < $reqminor))) {
1195: $canhost = 0;
1196: }
1197: } else {
1198: $canhost = 0;
1199: }
1200: }
1201: }
1202: }
1203: }
1204: if ($canhost) {
1205: if (ref($hostedsessions) eq 'HASH') {
1.1120 raeburn 1206: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1207: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073 raeburn 1208: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120 raeburn 1209: if (($uint_dom ne '') &&
1210: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073 raeburn 1211: $canhost = 0;
1212: } else {
1213: $canhost = 1;
1214: }
1215: }
1216: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120 raeburn 1217: if (($uint_dom ne '') &&
1218: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073 raeburn 1219: $canhost = 1;
1220: } else {
1221: $canhost = 0;
1222: }
1223: }
1224: }
1225: }
1226: return $canhost;
1227: }
1228:
1.1083 raeburn 1229: sub spare_can_host {
1230: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1231: my $canhost=1;
1.1279 raeburn 1232: my $try_server_hostname = &hostname($try_server);
1233: my $serverhomeID = &get_server_homeID($try_server_hostname);
1234: my $serverhomedom = &host_domain($serverhomeID);
1235: my %defdomdefaults = &get_domain_defaults($serverhomedom);
1236: if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
1237: if ($defdomdefaults{'offloadnow'}{$try_server}) {
1238: $canhost = 0;
1239: }
1240: }
1241: if (($canhost) && ($uint_dom)) {
1242: my @intdoms;
1243: my $internet_names = &get_internet_names($try_server);
1244: if (ref($internet_names) eq 'ARRAY') {
1245: @intdoms = @{$internet_names};
1246: }
1247: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1248: my $remoterev = &get_server_loncaparev(undef,$try_server);
1249: $canhost = &can_host_session($udom,$try_server,$remoterev,
1250: $remotesessions,
1251: $defdomdefaults{'hostedsessions'});
1252: }
1.1083 raeburn 1253: }
1254: return $canhost;
1255: }
1256:
1.1123 raeburn 1257: sub this_host_spares {
1258: my ($dom) = @_;
1.1126 raeburn 1259: my ($dom_in_use,$lonhost_in_use,$result);
1.1123 raeburn 1260: my @hosts = ¤t_machine_ids();
1261: foreach my $lonhost (@hosts) {
1262: if (&host_domain($lonhost) eq $dom) {
1.1126 raeburn 1263: $dom_in_use = $dom;
1264: $lonhost_in_use = $lonhost;
1.1123 raeburn 1265: last;
1266: }
1267: }
1.1126 raeburn 1268: if ($dom_in_use ne '') {
1269: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1270: }
1271: if (ref($result) ne 'HASH') {
1272: $lonhost_in_use = $perlvar{'lonHostID'};
1273: $dom_in_use = &host_domain($lonhost_in_use);
1274: $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
1275: if (ref($result) ne 'HASH') {
1276: $result = \%spareid;
1277: }
1278: }
1279: return $result;
1280: }
1281:
1282: sub spares_for_offload {
1283: my ($dom_in_use,$lonhost_in_use) = @_;
1284: my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123 raeburn 1285: if (defined($cached)) {
1286: return $result;
1287: } else {
1.1126 raeburn 1288: my $cachetime = 60*60*24;
1289: my %domconfig =
1290: &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
1291: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1292: if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
1293: if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
1294: return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123 raeburn 1295: }
1296: }
1297: }
1298: }
1.1126 raeburn 1299: return;
1.1123 raeburn 1300: }
1301:
1.1129 raeburn 1302: sub get_lonbalancer_config {
1303: my ($servers) = @_;
1304: my ($currbalancer,$currtargets);
1305: if (ref($servers) eq 'HASH') {
1306: foreach my $server (keys(%{$servers})) {
1307: my %what = (
1308: spareid => 1,
1309: perlvar => 1,
1310: );
1311: my ($result,$returnhash) = &get_remote_globals($server,\%what);
1312: if ($result eq 'ok') {
1313: if (ref($returnhash) eq 'HASH') {
1314: if (ref($returnhash->{'perlvar'}) eq 'HASH') {
1315: if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
1316: $currbalancer = $server;
1317: $currtargets = {};
1318: if (ref($returnhash->{'spareid'}) eq 'HASH') {
1319: if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
1320: $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
1321: }
1322: if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
1323: $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
1324: }
1325: }
1326: last;
1327: }
1328: }
1329: }
1330: }
1331: }
1332: }
1333: return ($currbalancer,$currtargets);
1334: }
1335:
1336: sub check_loadbalancing {
1.1331 raeburn 1337: my ($uname,$udom,$caller) = @_;
1.1191 raeburn 1338: my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
1339: $rule_in_effect,$offloadto,$otherserver);
1.1129 raeburn 1340: my $lonhost = $perlvar{'lonHostID'};
1.1175 raeburn 1341: my @hosts = ¤t_machine_ids();
1.1129 raeburn 1342: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1343: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
1344: my $intdom = &Apache::lonnet::internet_dom($lonhost);
1345: my $serverhomedom = &host_domain($lonhost);
1.1307 raeburn 1346: my $domneedscache;
1.1129 raeburn 1347: my $cachetime = 60*60*24;
1348:
1349: if (($uintdom ne '') && ($uintdom eq $intdom)) {
1350: $dom_in_use = $udom;
1351: $homeintdom = 1;
1352: } else {
1353: $dom_in_use = $serverhomedom;
1354: }
1355: my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
1356: unless (defined($cached)) {
1357: my %domconfig =
1358: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1359: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130 raeburn 1360: $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307 raeburn 1361: } else {
1362: $domneedscache = $dom_in_use;
1.1129 raeburn 1363: }
1364: }
1365: if (ref($result) eq 'HASH') {
1.1191 raeburn 1366: ($is_balancer,$currtargets,$currrules) =
1367: &check_balancer_result($result,@hosts);
1.1129 raeburn 1368: if ($is_balancer) {
1369: if (ref($currrules) eq 'HASH') {
1370: if ($homeintdom) {
1371: if ($uname ne '') {
1372: if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
1373: my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
1374: if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
1375: $rule_in_effect = $currrules->{'_LC_author'};
1376: } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
1377: $rule_in_effect = $currrules->{'_LC_adv'}
1378: }
1379: }
1380: if ($rule_in_effect eq '') {
1381: my %userenv = &userenvironment($udom,$uname,'inststatus');
1382: if ($userenv{'inststatus'} ne '') {
1383: my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
1384: my ($othertitle,$usertypes,$types) =
1385: &Apache::loncommon::sorted_inst_types($udom);
1386: if (ref($types) eq 'ARRAY') {
1387: foreach my $type (@{$types}) {
1388: if (grep(/^\Q$type\E$/,@statuses)) {
1389: if (exists($currrules->{$type})) {
1390: $rule_in_effect = $currrules->{$type};
1391: }
1392: }
1393: }
1394: }
1395: } else {
1396: if (exists($currrules->{'default'})) {
1397: $rule_in_effect = $currrules->{'default'};
1398: }
1399: }
1400: }
1401: } else {
1402: if (exists($currrules->{'default'})) {
1403: $rule_in_effect = $currrules->{'default'};
1404: }
1405: }
1406: } else {
1407: if ($currrules->{'_LC_external'} ne '') {
1408: $rule_in_effect = $currrules->{'_LC_external'};
1409: }
1410: }
1411: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1412: $uname,$udom);
1413: }
1414: }
1415: } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239 raeburn 1416: ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129 raeburn 1417: unless (defined($cached)) {
1418: my %domconfig =
1419: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
1420: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307 raeburn 1421: $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
1422: } else {
1423: $domneedscache = $serverhomedom;
1.1129 raeburn 1424: }
1425: }
1426: if (ref($result) eq 'HASH') {
1.1191 raeburn 1427: ($is_balancer,$currtargets,$currrules) =
1428: &check_balancer_result($result,@hosts);
1429: if ($is_balancer) {
1.1129 raeburn 1430: if (ref($currrules) eq 'HASH') {
1431: if ($currrules->{'_LC_internetdom'} ne '') {
1432: $rule_in_effect = $currrules->{'_LC_internetdom'};
1433: }
1434: }
1435: $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
1436: $uname,$udom);
1437: }
1438: } else {
1439: if ($perlvar{'lonBalancer'} eq 'yes') {
1440: $is_balancer = 1;
1441: $offloadto = &this_host_spares($dom_in_use);
1442: }
1.1307 raeburn 1443: unless (defined($cached)) {
1444: $domneedscache = $serverhomedom;
1445: }
1.1129 raeburn 1446: }
1447: } else {
1448: if ($perlvar{'lonBalancer'} eq 'yes') {
1449: $is_balancer = 1;
1450: $offloadto = &this_host_spares($dom_in_use);
1451: }
1.1307 raeburn 1452: unless (defined($cached)) {
1453: $domneedscache = $serverhomedom;
1454: }
1455: }
1456: if ($domneedscache) {
1457: &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129 raeburn 1458: }
1.1176 raeburn 1459: if ($is_balancer) {
1460: my $lowest_load = 30000;
1461: if (ref($offloadto) eq 'HASH') {
1462: if (ref($offloadto->{'primary'}) eq 'ARRAY') {
1463: foreach my $try_server (@{$offloadto->{'primary'}}) {
1464: ($otherserver,$lowest_load) =
1465: &compare_server_load($try_server,$otherserver,$lowest_load);
1466: }
1.1129 raeburn 1467: }
1.1176 raeburn 1468: my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129 raeburn 1469:
1.1176 raeburn 1470: if (!$found_server) {
1471: if (ref($offloadto->{'default'}) eq 'ARRAY') {
1472: foreach my $try_server (@{$offloadto->{'default'}}) {
1473: ($otherserver,$lowest_load) =
1474: &compare_server_load($try_server,$otherserver,$lowest_load);
1475: }
1476: }
1477: }
1478: } elsif (ref($offloadto) eq 'ARRAY') {
1479: if (@{$offloadto} == 1) {
1480: $otherserver = $offloadto->[0];
1481: } elsif (@{$offloadto} > 1) {
1482: foreach my $try_server (@{$offloadto}) {
1.1129 raeburn 1483: ($otherserver,$lowest_load) =
1484: &compare_server_load($try_server,$otherserver,$lowest_load);
1485: }
1486: }
1487: }
1.1331 raeburn 1488: unless ($caller eq 'login') {
1489: if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
1490: $is_balancer = 0;
1491: if ($uname ne '' && $udom ne '') {
1492: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1176 raeburn 1493:
1.1331 raeburn 1494: &appenv({'user.loadbalexempt' => $lonhost,
1495: 'user.loadbalcheck.time' => time});
1496: }
1.1176 raeburn 1497: }
1.1129 raeburn 1498: }
1499: }
1500: }
1501: return ($is_balancer,$otherserver);
1502: }
1503:
1.1191 raeburn 1504: sub check_balancer_result {
1505: my ($result,@hosts) = @_;
1506: my ($is_balancer,$currtargets,$currrules);
1507: if (ref($result) eq 'HASH') {
1508: if ($result->{'lonhost'} ne '') {
1509: my $currbalancer = $result->{'lonhost'};
1510: if (grep(/^\Q$currbalancer\E$/,@hosts)) {
1511: $is_balancer = 1;
1512: $currtargets = $result->{'targets'};
1513: $currrules = $result->{'rules'};
1514: }
1515: } else {
1516: foreach my $key (keys(%{$result})) {
1517: if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
1518: (ref($result->{$key}) eq 'HASH')) {
1519: $is_balancer = 1;
1520: $currrules = $result->{$key}{'rules'};
1521: $currtargets = $result->{$key}{'targets'};
1522: last;
1523: }
1524: }
1525: }
1526: }
1527: return ($is_balancer,$currtargets,$currrules);
1528: }
1529:
1.1129 raeburn 1530: sub get_loadbalancer_targets {
1531: my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
1532: my $offloadto;
1.1175 raeburn 1533: if ($rule_in_effect eq 'none') {
1534: return [$perlvar{'lonHostID'}];
1535: } elsif ($rule_in_effect eq '') {
1.1129 raeburn 1536: $offloadto = $currtargets;
1537: } else {
1538: if ($rule_in_effect eq 'homeserver') {
1539: my $homeserver = &homeserver($uname,$udom);
1540: if ($homeserver ne 'no_host') {
1541: $offloadto = [$homeserver];
1542: }
1543: } elsif ($rule_in_effect eq 'externalbalancer') {
1544: my %domconfig =
1545: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
1546: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1547: if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
1548: if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
1549: $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
1550: }
1551: }
1552: } else {
1.1178 raeburn 1553: my %servers = &internet_dom_servers($udom);
1.1129 raeburn 1554: my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
1555: if (&hostname($remotebalancer) ne '') {
1556: $offloadto = [$remotebalancer];
1557: }
1558: }
1559: } elsif (&hostname($rule_in_effect) ne '') {
1560: $offloadto = [$rule_in_effect];
1561: }
1562: }
1563: return $offloadto;
1564: }
1565:
1.1127 raeburn 1566: sub internet_dom_servers {
1567: my ($dom) = @_;
1568: my (%uniqservers,%servers);
1569: my $primaryserver = &hostname(&domain($dom,'primary'));
1570: my @machinedoms = &machine_domains($primaryserver);
1571: foreach my $mdom (@machinedoms) {
1572: my %currservers = %servers;
1573: my %server = &get_servers($mdom);
1574: %servers = (%currservers,%server);
1575: }
1576: my %by_hostname;
1577: foreach my $id (keys(%servers)) {
1578: push(@{$by_hostname{$servers{$id}}},$id);
1579: }
1580: foreach my $hostname (sort(keys(%by_hostname))) {
1581: if (@{$by_hostname{$hostname}} > 1) {
1582: my $match = 0;
1583: foreach my $id (@{$by_hostname{$hostname}}) {
1584: if (&host_domain($id) eq $dom) {
1585: $uniqservers{$id} = $hostname;
1586: $match = 1;
1587: }
1588: }
1589: unless ($match) {
1590: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1591: }
1592: } else {
1593: $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
1594: }
1595: }
1596: return %uniqservers;
1597: }
1598:
1.1 albertel 1599: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1600:
1.599 albertel 1601: my %homecache;
1.1 albertel 1602: sub homeserver {
1.230 stredwic 1603: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1604: my $index="$uname:$udom";
1.426 albertel 1605:
1.599 albertel 1606: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1607:
1608: my %servers = &get_servers($udom,'library');
1609: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1610: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1611: exists($badServerCache{$tryserver}));
1.841 albertel 1612:
1613: my $answer=reply("home:$udom:$uname",$tryserver);
1614: if ($answer eq 'found') {
1615: delete($badServerCache{$tryserver});
1616: return $homecache{$index}=$tryserver;
1617: } elsif ($answer eq 'no_host') {
1618: $badServerCache{$tryserver}=1;
1619: }
1.1 albertel 1620: }
1621: return 'no_host';
1.70 www 1622: }
1623:
1.1300 raeburn 1624: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70 www 1625:
1626: sub idget {
1.1300 raeburn 1627: my ($udom,$idsref,$namespace)=@_;
1.70 www 1628: my %returnhash=();
1.1300 raeburn 1629: my @ids=();
1630: if (ref($idsref) eq 'ARRAY') {
1631: @ids = @{$idsref};
1632: } else {
1633: return %returnhash;
1634: }
1635: if ($namespace eq '') {
1636: $namespace = 'ids';
1637: }
1.70 www 1638:
1.841 albertel 1639: my %servers = &get_servers($udom,'library');
1640: foreach my $tryserver (keys(%servers)) {
1.1299 raeburn 1641: my $idlist=join('&', map { &escape($_); } @ids);
1.1300 raeburn 1642: if ($namespace eq 'ids') {
1643: $idlist=~tr/A-Z/a-z/;
1644: }
1645: my $reply;
1646: if ($namespace eq 'ids') {
1647: $reply=&reply("idget:$udom:".$idlist,$tryserver);
1648: } else {
1649: $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
1650: }
1.841 albertel 1651: my @answer=();
1652: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1653: @answer=split(/\&/,$reply);
1654: } ;
1655: my $i;
1656: for ($i=0;$i<=$#ids;$i++) {
1657: if ($answer[$i]) {
1.1299 raeburn 1658: $returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300 raeburn 1659: }
1.841 albertel 1660: }
1.1300 raeburn 1661: }
1.70 www 1662: return %returnhash;
1663: }
1664:
1665: # ------------------------------------- Find the IDs behind a list of usernames
1666:
1667: sub idrget {
1668: my ($udom,@unames)=@_;
1669: my %returnhash=();
1.800 albertel 1670: foreach my $uname (@unames) {
1671: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1672: }
1.70 www 1673: return %returnhash;
1674: }
1675:
1.1300 raeburn 1676: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70 www 1677:
1678: sub idput {
1.1300 raeburn 1679: my ($udom,$idsref,$uhom,$namespace)=@_;
1.70 www 1680: my %servers=();
1.1300 raeburn 1681: my %ids=();
1682: my %byid = ();
1683: if (ref($idsref) eq 'HASH') {
1684: %ids=%{$idsref};
1685: }
1686: if ($namespace eq '') {
1687: $namespace = 'ids';
1688: }
1.800 albertel 1689: foreach my $uname (keys(%ids)) {
1690: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300 raeburn 1691: if ($uhom eq '') {
1692: $uhom=&homeserver($uname,$udom);
1693: }
1.70 www 1694: if ($uhom ne 'no_host') {
1.800 albertel 1695: my $esc_unam=&escape($uname);
1.1300 raeburn 1696: if ($namespace eq 'ids') {
1697: my $id=&escape($ids{$uname});
1698: $id=~tr/A-Z/a-z/;
1699: my $esc_unam=&escape($uname);
1700: $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70 www 1701: } else {
1.1300 raeburn 1702: my @currids = split(/,/,$ids{$uname});
1703: foreach my $id (@currids) {
1704: $byid{$uhom}{$id} .= $uname.',';
1705: }
1706: }
1707: }
1708: }
1709: if ($namespace eq 'clickers') {
1710: foreach my $server (keys(%byid)) {
1711: if (ref($byid{$server}) eq 'HASH') {
1712: foreach my $id (keys(%{$byid{$server}})) {
1713: $byid{$server} =~ s/,$//;
1714: $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&';
1715: }
1.70 www 1716: }
1717: }
1.191 harris41 1718: }
1.800 albertel 1719: foreach my $server (keys(%servers)) {
1.1300 raeburn 1720: $servers{$server} =~ s/\&$//;
1721: if ($namespace eq 'ids') {
1722: &critical('idput:'.$udom.':'.$servers{$server},$server);
1723: } else {
1724: &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
1725: }
1.191 harris41 1726: }
1.344 www 1727: }
1728:
1.1300 raeburn 1729: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231 raeburn 1730:
1731: sub iddel {
1.1300 raeburn 1732: my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231 raeburn 1733: my %result=();
1.1300 raeburn 1734: my %ids=();
1735: my %byid = ();
1736: if (ref($idshashref) eq 'HASH') {
1737: %ids=%{$idshashref};
1738: } else {
1.1231 raeburn 1739: return %result;
1740: }
1.1300 raeburn 1741: if ($namespace eq '') {
1742: $namespace = 'ids';
1743: }
1.1231 raeburn 1744: my %servers=();
1.1300 raeburn 1745: while (my ($id,$unamestr) = each(%ids)) {
1746: if ($namespace eq 'ids') {
1747: my $uhom = $uhome;
1748: if ($uhom eq '') {
1749: $uhom=&homeserver($unamestr,$udom);
1750: }
1751: if ($uhom ne 'no_host') {
1752: $servers{$uhom}.='&'.&escape($id);
1753: }
1754: } else {
1755: my @curritems = split(/,/,$ids{$id});
1756: foreach my $uname (@curritems) {
1757: my $uhom = $uhome;
1758: if ($uhom eq '') {
1759: $uhom=&homeserver($uname,$udom);
1760: }
1761: if ($uhom ne 'no_host') {
1762: $byid{$uhom}{$id} .= $uname.',';
1763: }
1764: }
1.1231 raeburn 1765: }
1.1300 raeburn 1766: }
1767: if ($namespace eq 'clickers') {
1768: foreach my $server (keys(%byid)) {
1769: if (ref($byid{$server}) eq 'HASH') {
1770: foreach my $id (keys(%{$byid{$server}})) {
1771: $byid{$server}{$id} =~ s/,$//;
1772: $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
1773: }
1.1231 raeburn 1774: }
1775: }
1776: }
1777: foreach my $server (keys(%servers)) {
1.1300 raeburn 1778: $servers{$server} =~ s/\&$//;
1779: if ($namespace eq 'ids') {
1780: $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
1781: } elsif ($namespace eq 'clickers') {
1782: $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
1783: }
1.1231 raeburn 1784: }
1785: return %result;
1786: }
1787:
1.1300 raeburn 1788: # ----- Update clicker ID-to-username look-ups in clickers.db on library server
1789:
1790: sub updateclickers {
1791: my ($udom,$action,$idshashref,$uhome,$critical) = @_;
1792: my %clickers;
1793: if (ref($idshashref) eq 'HASH') {
1794: %clickers=%{$idshashref};
1795: } else {
1796: return;
1797: }
1798: my $items='';
1799: foreach my $item (keys(%clickers)) {
1800: $items.=&escape($item).'='.&escape($clickers{$item}).'&';
1801: }
1802: $items=~s/\&$//;
1803: my $request = "updateclickers:$udom:$action:$items";
1804: if ($critical) {
1805: return &critical($request,$uhome);
1806: } else {
1807: return &reply($request,$uhome);
1808: }
1809: }
1810:
1.1023 raeburn 1811: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1812: sub dump_dom {
1.1165 droeschl 1813: my ($namespace, $udom, $regexp) = @_;
1814:
1815: $udom ||= $env{'user.domain'};
1816:
1817: return () unless $udom;
1818:
1819: return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012 raeburn 1820: }
1821:
1.1023 raeburn 1822: # ------------------------------------------ get items from domain db files
1.806 raeburn 1823:
1824: sub get_dom {
1.860 raeburn 1825: my ($namespace,$storearr,$udom,$uhome)=@_;
1.1267 raeburn 1826: return if ($udom eq 'public');
1.806 raeburn 1827: my $items='';
1828: foreach my $item (@$storearr) {
1829: $items.=&escape($item).'&';
1830: }
1831: $items=~s/\&$//;
1.860 raeburn 1832: if (!$udom) {
1833: $udom=$env{'user.domain'};
1.1267 raeburn 1834: return if ($udom eq 'public');
1.860 raeburn 1835: if (defined(&domain($udom,'primary'))) {
1836: $uhome=&domain($udom,'primary');
1837: } else {
1.874 albertel 1838: undef($uhome);
1.860 raeburn 1839: }
1840: } else {
1841: if (!$uhome) {
1842: if (defined(&domain($udom,'primary'))) {
1843: $uhome=&domain($udom,'primary');
1844: }
1845: }
1846: }
1847: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1848: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1849: my %returnhash;
1.875 albertel 1850: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1851: return %returnhash;
1852: }
1.806 raeburn 1853: my @pairs=split(/\&/,$rep);
1854: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1855: return @pairs;
1856: }
1857: my $i=0;
1858: foreach my $item (@$storearr) {
1859: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1860: $i++;
1861: }
1862: return %returnhash;
1863: } else {
1.880 banghart 1864: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1865: }
1866: }
1867:
1868: # -------------------------------------------- put items in domain db files
1869:
1870: sub put_dom {
1.860 raeburn 1871: my ($namespace,$storehash,$udom,$uhome)=@_;
1872: if (!$udom) {
1873: $udom=$env{'user.domain'};
1874: if (defined(&domain($udom,'primary'))) {
1875: $uhome=&domain($udom,'primary');
1876: } else {
1.874 albertel 1877: undef($uhome);
1.860 raeburn 1878: }
1879: } else {
1880: if (!$uhome) {
1881: if (defined(&domain($udom,'primary'))) {
1882: $uhome=&domain($udom,'primary');
1883: }
1884: }
1885: }
1886: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1887: my $items='';
1888: foreach my $item (keys(%$storehash)) {
1889: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1890: }
1891: $items=~s/\&$//;
1892: return &reply("putdom:$udom:$namespace:$items",$uhome);
1893: } else {
1.860 raeburn 1894: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1895: }
1896: }
1897:
1.1023 raeburn 1898: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1899: sub newput_dom {
1.1023 raeburn 1900: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1901: my $result;
1902: if (!$udom) {
1903: $udom=$env{'user.domain'};
1904: }
1.1023 raeburn 1905: if ($udom) {
1906: my $uname = &get_domainconfiguser($udom);
1907: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1908: }
1909: return $result;
1910: }
1911:
1.1023 raeburn 1912: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1913: sub del_dom {
1.1023 raeburn 1914: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1915: if (ref($storearr) eq 'ARRAY') {
1916: if (!$udom) {
1917: $udom=$env{'user.domain'};
1918: }
1.1023 raeburn 1919: if ($udom) {
1920: my $uname = &get_domainconfiguser($udom);
1921: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1922: }
1923: }
1924: }
1925:
1.1023 raeburn 1926: # ----------------------------------construct domainconfig user for a domain
1927: sub get_domainconfiguser {
1928: my ($udom) = @_;
1929: return $udom.'-domainconfig';
1930: }
1931:
1.837 raeburn 1932: sub retrieve_inst_usertypes {
1933: my ($udom) = @_;
1934: my (%returnhash,@order);
1.989 raeburn 1935: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1936: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1937: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256 raeburn 1938: return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989 raeburn 1939: } else {
1940: if (defined(&domain($udom,'primary'))) {
1941: my $uhome=&domain($udom,'primary');
1942: my $rep=&reply("inst_usertypes:$udom",$uhome);
1943: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256 raeburn 1944: &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989 raeburn 1945: return (\%returnhash,\@order);
1946: }
1947: my ($hashitems,$orderitems) = split(/:/,$rep);
1948: my @pairs=split(/\&/,$hashitems);
1949: foreach my $item (@pairs) {
1950: my ($key,$value)=split(/=/,$item,2);
1951: $key = &unescape($key);
1952: next if ($key =~ /^error: 2 /);
1953: $returnhash{$key}=&thaw_unescape($value);
1954: }
1955: my @esc_order = split(/\&/,$orderitems);
1956: foreach my $item (@esc_order) {
1957: push(@order,&unescape($item));
1958: }
1959: } else {
1.1256 raeburn 1960: &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837 raeburn 1961: }
1.1256 raeburn 1962: return (\%returnhash,\@order);
1.837 raeburn 1963: }
1964: }
1965:
1.868 raeburn 1966: sub is_domainimage {
1967: my ($url) = @_;
1.1306 raeburn 1968: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
1.868 raeburn 1969: if (&domain($1) ne '') {
1970: return '1';
1971: }
1972: }
1973: return;
1974: }
1975:
1.899 raeburn 1976: sub inst_directory_query {
1977: my ($srch) = @_;
1978: my $udom = $srch->{'srchdomain'};
1979: my %results;
1980: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1981: my $outcome;
1.899 raeburn 1982: if ($homeserver ne '') {
1.904 albertel 1983: my $queryid=&reply("querysend:instdirsearch:".
1984: &escape($srch->{'srchby'}).':'.
1985: &escape($srch->{'srchterm'}).':'.
1986: &escape($srch->{'srchtype'}),$homeserver);
1987: my $host=&hostname($homeserver);
1988: if ($queryid !~/^\Q$host\E\_/) {
1.1343 ! raeburn 1989: &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904 albertel 1990: return;
1991: }
1992: my $response = &get_query_reply($queryid);
1993: my $maxtries = 5;
1994: my $tries = 1;
1995: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1996: $response = &get_query_reply($queryid);
1997: $tries ++;
1998: }
1999:
2000: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 2001: if ($response eq 'unavailable') {
2002: $outcome = $response;
2003: } else {
2004: $outcome = 'ok';
2005: my @matches = split(/\n/,$response);
2006: foreach my $match (@matches) {
2007: my ($key,$value) = split(/=/,$match);
2008: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
2009: }
1.899 raeburn 2010: }
2011: }
2012: }
1.909 raeburn 2013: return ($outcome,%results);
1.899 raeburn 2014: }
2015:
2016: sub usersearch {
2017: my ($srch) = @_;
2018: my $dom = $srch->{'srchdomain'};
2019: my %results;
2020: my %libserv = &all_library();
2021: my $query = 'usersearch';
2022: foreach my $tryserver (keys(%libserv)) {
2023: if (&host_domain($tryserver) eq $dom) {
2024: my $host=&hostname($tryserver);
2025: my $queryid=
1.911 raeburn 2026: &reply("querysend:".&escape($query).':'.
2027: &escape($srch->{'srchby'}).':'.
1.899 raeburn 2028: &escape($srch->{'srchtype'}).':'.
2029: &escape($srch->{'srchterm'}),$tryserver);
2030: if ($queryid !~/^\Q$host\E\_/) {
2031: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 2032: next;
1.899 raeburn 2033: }
2034: my $reply = &get_query_reply($queryid);
2035: my $maxtries = 1;
2036: my $tries = 1;
2037: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
2038: $reply = &get_query_reply($queryid);
2039: $tries ++;
2040: }
2041: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
2042: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
2043: } else {
1.911 raeburn 2044: my @matches;
2045: if ($reply =~ /\n/) {
2046: @matches = split(/\n/,$reply);
2047: } else {
2048: @matches = split(/\&/,$reply);
2049: }
1.899 raeburn 2050: foreach my $match (@matches) {
2051: my ($uname,$udom,%userhash);
1.911 raeburn 2052: foreach my $entry (split(/:/,$match)) {
2053: my ($key,$value) =
2054: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 2055: $userhash{$key} = $value;
2056: if ($key eq 'username') {
2057: $uname = $value;
2058: } elsif ($key eq 'domain') {
2059: $udom = $value;
1.911 raeburn 2060: }
1.899 raeburn 2061: }
2062: $results{$uname.':'.$udom} = \%userhash;
2063: }
2064: }
2065: }
2066: }
2067: return %results;
2068: }
2069:
1.912 raeburn 2070: sub get_instuser {
2071: my ($udom,$uname,$id) = @_;
2072: my $homeserver = &domain($udom,'primary');
2073: my ($outcome,%results);
2074: if ($homeserver ne '') {
2075: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
2076: &escape($id).':'.&escape($udom),$homeserver);
2077: my $host=&hostname($homeserver);
2078: if ($queryid !~/^\Q$host\E\_/) {
2079: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
2080: return;
2081: }
2082: my $response = &get_query_reply($queryid);
2083: my $maxtries = 5;
2084: my $tries = 1;
2085: while (($response=~/^timeout/) && ($tries < $maxtries)) {
2086: $response = &get_query_reply($queryid);
2087: $tries ++;
2088: }
2089: if (!&error($response) && $response ne 'refused') {
2090: if ($response eq 'unavailable') {
2091: $outcome = $response;
2092: } else {
2093: $outcome = 'ok';
2094: my @matches = split(/\n/,$response);
2095: foreach my $match (@matches) {
2096: my ($key,$value) = split(/=/,$match);
2097: $results{&unescape($key)} = &thaw_unescape($value);
2098: }
2099: }
2100: }
2101: }
2102: my %userinfo;
2103: if (ref($results{$uname}) eq 'HASH') {
2104: %userinfo = %{$results{$uname}};
2105: }
2106: return ($outcome,%userinfo);
2107: }
2108:
1.1290 raeburn 2109: sub get_multiple_instusers {
2110: my ($udom,$users,$caller) = @_;
2111: my ($outcome,$results);
2112: if (ref($users) eq 'HASH') {
2113: my $count = keys(%{$users});
2114: my $requested = &freeze_escape($users);
2115: my $homeserver = &domain($udom,'primary');
2116: if ($homeserver ne '') {
2117: my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
2118: my $host=&hostname($homeserver);
2119: if ($queryid !~/^\Q$host\E\_/) {
2120: &logthis('get_multiple_instusers invalid queryid: '.$queryid.
2121: ' for host: '.$homeserver.'in domain '.$udom);
2122: return ($outcome,$results);
2123: }
2124: my $response = &get_query_reply($queryid);
2125: my $maxtries = 5;
2126: if ($count > 100) {
2127: $maxtries = 1+int($count/20);
2128: }
2129: my $tries = 1;
2130: while (($response=~/^timeout/) && ($tries <= $maxtries)) {
2131: $response = &get_query_reply($queryid);
2132: $tries ++;
2133: }
2134: if ($response eq '') {
2135: $results = {};
2136: foreach my $key (keys(%{$users})) {
2137: my ($uname,$id);
2138: if ($caller eq 'id') {
2139: $id = $key;
2140: } else {
2141: $uname = $key;
2142: }
2143: my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291 raeburn 2144: $outcome = $resp;
1.1290 raeburn 2145: if ($resp eq 'ok') {
2146: %{$results} = (%{$results}, %info);
2147: } else {
2148: last;
2149: }
2150: }
2151: } elsif(!&error($response) && ($response ne 'refused')) {
2152: if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
2153: $outcome = $response;
2154: } else {
1.1291 raeburn 2155: ($outcome,my $userdata) = split(/=/,$response,2);
1.1290 raeburn 2156: if ($outcome eq 'ok') {
2157: $results = &thaw_unescape($userdata);
2158: }
2159: }
2160: }
2161: }
2162: }
2163: return ($outcome,$results);
2164: }
2165:
1.912 raeburn 2166: sub inst_rulecheck {
1.923 raeburn 2167: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 2168: my %returnhash;
2169: if ($udom ne '') {
2170: if (ref($rules) eq 'ARRAY') {
2171: @{$rules} = map {&escape($_);} (@{$rules});
2172: my $rulestr = join(':',@{$rules});
2173: my $homeserver=&domain($udom,'primary');
2174: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2175: my $response;
2176: if ($item eq 'username') {
2177: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
2178: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 2179: $homeserver));
1.923 raeburn 2180: } elsif ($item eq 'id') {
2181: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
2182: ':'.&escape($id).':'.$rulestr,
2183: $homeserver));
1.945 raeburn 2184: } elsif ($item eq 'selfcreate') {
2185: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 2186: &escape($udom).':'.&escape($uname).
2187: ':'.$rulestr,$homeserver));
1.923 raeburn 2188: }
1.912 raeburn 2189: if ($response ne 'refused') {
2190: my @pairs=split(/\&/,$response);
2191: foreach my $item (@pairs) {
2192: my ($key,$value)=split(/=/,$item,2);
2193: $key = &unescape($key);
2194: next if ($key =~ /^error: 2 /);
2195: $returnhash{$key}=&thaw_unescape($value);
2196: }
2197: }
2198: }
2199: }
2200: }
2201: return %returnhash;
2202: }
2203:
2204: sub inst_userrules {
1.923 raeburn 2205: my ($udom,$check) = @_;
1.912 raeburn 2206: my (%ruleshash,@ruleorder);
2207: if ($udom ne '') {
2208: my $homeserver=&domain($udom,'primary');
2209: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 2210: my $response;
2211: if ($check eq 'id') {
2212: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 2213: $homeserver);
1.943 raeburn 2214: } elsif ($check eq 'email') {
2215: $response=&reply('instemailrules:'.&escape($udom),
2216: $homeserver);
1.923 raeburn 2217: } else {
2218: $response=&reply('instuserrules:'.&escape($udom),
2219: $homeserver);
2220: }
1.912 raeburn 2221: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 2222: ($response ne 'unknown_cmd') &&
1.912 raeburn 2223: ($response ne 'no_such_host')) {
2224: my ($hashitems,$orderitems) = split(/:/,$response);
2225: my @pairs=split(/\&/,$hashitems);
2226: foreach my $item (@pairs) {
2227: my ($key,$value)=split(/=/,$item,2);
2228: $key = &unescape($key);
2229: next if ($key =~ /^error: 2 /);
2230: $ruleshash{$key}=&thaw_unescape($value);
2231: }
2232: my @esc_order = split(/\&/,$orderitems);
2233: foreach my $item (@esc_order) {
2234: push(@ruleorder,&unescape($item));
2235: }
2236: }
2237: }
2238: }
2239: return (\%ruleshash,\@ruleorder);
2240: }
2241:
1.976 raeburn 2242: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 2243:
2244: sub get_domain_defaults {
1.1240 raeburn 2245: my ($domain,$ignore_cache) = @_;
1.1242 raeburn 2246: return if (($domain eq '') || ($domain eq 'public'));
1.943 raeburn 2247: my $cachetime = 60*60*24;
1.1240 raeburn 2248: unless ($ignore_cache) {
2249: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
2250: if (defined($cached)) {
2251: if (ref($result) eq 'HASH') {
2252: return %{$result};
2253: }
1.943 raeburn 2254: }
2255: }
2256: my %domdefaults;
2257: my %domconfig =
1.989 raeburn 2258: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 2259: 'requestcourses','inststatus',
1.1183 raeburn 2260: 'coursedefaults','usersessions',
1.1258 raeburn 2261: 'requestauthor','selfenrollment',
1.1320 raeburn 2262: 'coursecategories','ssl','autoenroll',
1.1332 raeburn 2263: 'trust','helpsettings'],$domain);
1.1305 raeburn 2264: my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943 raeburn 2265: if (ref($domconfig{'defaults'}) eq 'HASH') {
2266: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
2267: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
2268: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 2269: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 2270: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147 raeburn 2271: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1340 raeburn 2272: $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
2273: $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
2274: $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.943 raeburn 2275: } else {
2276: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
2277: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
2278: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
2279: }
1.976 raeburn 2280: if (ref($domconfig{'quotas'}) eq 'HASH') {
2281: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
2282: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
2283: } else {
2284: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229 raeburn 2285: }
1.1177 raeburn 2286: my @usertools = ('aboutme','blog','webdav','portfolio');
1.976 raeburn 2287: foreach my $item (@usertools) {
2288: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
2289: $domdefaults{$item} = $domconfig{'quotas'}{$item};
2290: }
2291: }
1.1229 raeburn 2292: if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
2293: $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
2294: }
1.976 raeburn 2295: }
1.985 raeburn 2296: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305 raeburn 2297: foreach my $item ('official','unofficial','community','textbook','placement') {
1.985 raeburn 2298: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
2299: }
2300: }
1.1183 raeburn 2301: if (ref($domconfig{'requestauthor'}) eq 'HASH') {
2302: $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
2303: }
1.989 raeburn 2304: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256 raeburn 2305: foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989 raeburn 2306: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
2307: }
2308: }
1.1047 raeburn 2309: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230 raeburn 2310: $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277 raeburn 2311: $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
2312: $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
2313: if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
2314: $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
2315: }
1.1254 raeburn 2316: foreach my $type (@coursetypes) {
2317: if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
2318: unless ($type eq 'community') {
2319: $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
2320: }
2321: }
2322: if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
2323: $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
2324: }
1.1277 raeburn 2325: if ($domdefaults{'postsubmit'} eq 'on') {
2326: if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
2327: $domdefaults{$type.'postsubtimeout'} =
2328: $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
2329: }
2330: }
1.1230 raeburn 2331: }
1.1286 raeburn 2332: if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
2333: if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
2334: my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
2335: if (@clonecodes) {
2336: $domdefaults{'canclone'} = join('+',@clonecodes);
2337: }
2338: }
2339: } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
2340: $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
2341: }
1.1047 raeburn 2342: }
1.1073 raeburn 2343: if (ref($domconfig{'usersessions'}) eq 'HASH') {
2344: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
2345: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
2346: }
2347: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
2348: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
2349: }
1.1279 raeburn 2350: if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
2351: $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
2352: }
1.1073 raeburn 2353: }
1.1254 raeburn 2354: if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
2355: if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
2356: my @settings = ('types','registered','enroll_dates','access_dates','section',
2357: 'approval','limit');
2358: foreach my $type (@coursetypes) {
2359: if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
2360: my @mgrdc = ();
2361: foreach my $item (@settings) {
2362: if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
2363: push(@mgrdc,$item);
2364: }
2365: }
2366: if (@mgrdc) {
2367: $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
2368: }
2369: }
2370: }
2371: }
2372: if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
2373: foreach my $type (@coursetypes) {
2374: if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
2375: foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
2376: $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
2377: }
2378: }
2379: }
2380: }
2381: }
1.1258 raeburn 2382: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
2383: $domdefaults{'catauth'} = 'std';
2384: $domdefaults{'catunauth'} = 'std';
2385: if ($domconfig{'coursecategories'}{'auth'}) {
2386: $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
2387: }
2388: if ($domconfig{'coursecategories'}{'unauth'}) {
2389: $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
2390: }
2391: }
1.1315 raeburn 2392: if (ref($domconfig{'ssl'}) eq 'HASH') {
2393: if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
2394: $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
2395: }
1.1338 raeburn 2396: if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
2397: $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
2398: }
2399: if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
2400: $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315 raeburn 2401: }
2402: }
1.1320 raeburn 2403: if (ref($domconfig{'trust'}) eq 'HASH') {
2404: my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
2405: foreach my $prefix (@prefixes) {
2406: if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
2407: $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
2408: }
2409: }
2410: }
1.1314 raeburn 2411: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
2412: $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
2413: }
1.1332 raeburn 2414: if (ref($domconfig{'helpsettings'}) eq 'HASH') {
2415: $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
2416: if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
2417: $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
2418: }
2419: }
1.1219 raeburn 2420: &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943 raeburn 2421: return %domdefaults;
2422: }
2423:
1.1311 raeburn 2424: sub course_portal_url {
2425: my ($cnum,$cdom) = @_;
2426: my $chome = &homeserver($cnum,$cdom);
2427: my $hostname = &hostname($chome);
2428: my $protocol = $protocol{$chome};
2429: $protocol = 'http' if ($protocol ne 'https');
2430: my %domdefaults = &get_domain_defaults($cdom);
2431: my $firsturl;
2432: if ($domdefaults{'portal_def'}) {
2433: $firsturl = $domdefaults{'portal_def'};
2434: } else {
2435: $firsturl = $protocol.'://'.$hostname;
2436: }
2437: return $firsturl;
2438: }
2439:
1.344 www 2440: # --------------------------------------------------- Assign a key to a student
2441:
2442: sub assign_access_key {
1.364 www 2443: #
2444: # a valid key looks like uname:udom#comments
2445: # comments are being appended
2446: #
1.498 www 2447: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
2448: $kdom=
1.620 albertel 2449: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 2450: $knum=
1.620 albertel 2451: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 2452: $cdom=
1.620 albertel 2453: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2454: $cnum=
1.620 albertel 2455: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2456: $udom=$env{'user.name'} unless (defined($udom));
2457: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 2458: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 2459: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 2460: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 2461: # assigned to this person
2462: # - this should not happen,
1.345 www 2463: # unless something went wrong
2464: # the first time around
2465: # ready to assign
1.364 www 2466: $logentry=$1.'; '.$logentry;
1.496 www 2467: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 2468: $kdom,$knum) eq 'ok') {
1.345 www 2469: # key now belongs to user
1.346 www 2470: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 2471: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 2472: &appenv({'environment.'.$envkey => $ckey});
1.345 www 2473: return 'ok';
2474: } else {
2475: return
2476: 'error: Count not permanently assign key, will need to be re-entered later.';
2477: }
2478: } else {
2479: return 'error: Could not assign key, try again later.';
2480: }
1.364 www 2481: } elsif (!$existing{$ckey}) {
1.345 www 2482: # the key does not exist
2483: return 'error: The key does not exist';
2484: } else {
2485: # the key is somebody else's
2486: return 'error: The key is already in use';
2487: }
1.344 www 2488: }
2489:
1.364 www 2490: # ------------------------------------------ put an additional comment on a key
2491:
2492: sub comment_access_key {
2493: #
2494: # a valid key looks like uname:udom#comments
2495: # comments are being appended
2496: #
2497: my ($ckey,$cdom,$cnum,$logentry)=@_;
2498: $cdom=
1.620 albertel 2499: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 2500: $cnum=
1.620 albertel 2501: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 2502: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
2503: if ($existing{$ckey}) {
2504: $existing{$ckey}.='; '.$logentry;
2505: # ready to assign
1.367 www 2506: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 2507: $cdom,$cnum) eq 'ok') {
2508: return 'ok';
2509: } else {
2510: return 'error: Count not store comment.';
2511: }
2512: } else {
2513: # the key does not exist
2514: return 'error: The key does not exist';
2515: }
2516: }
2517:
1.344 www 2518: # ------------------------------------------------------ Generate a set of keys
2519:
2520: sub generate_access_keys {
1.364 www 2521: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 2522: $cdom=
1.620 albertel 2523: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2524: $cnum=
1.620 albertel 2525: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 2526: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 2527: unless (($cdom) && ($cnum)) { return 0; }
2528: if ($number>10000) { return 0; }
2529: sleep(2); # make sure don't get same seed twice
2530: srand(time()^($$+($$<<15))); # from "Programming Perl"
2531: my $total=0;
2532: for (my $i=1;$i<=$number;$i++) {
2533: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
2534: sprintf("%lx",int(100000*rand)).'-'.
2535: sprintf("%lx",int(100000*rand));
2536: $newkey=~s/1/g/g; # folks mix up 1 and l
2537: $newkey=~s/0/h/g; # and also 0 and O
2538: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
2539: if ($existing{$newkey}) {
2540: $i--;
2541: } else {
1.364 www 2542: if (&put('accesskeys',
2543: { $newkey => '# generated '.localtime().
1.620 albertel 2544: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 2545: '; '.$logentry },
2546: $cdom,$cnum) eq 'ok') {
1.344 www 2547: $total++;
2548: }
2549: }
2550: }
1.620 albertel 2551: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 2552: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
2553: return $total;
2554: }
2555:
2556: # ------------------------------------------------------- Validate an accesskey
2557:
2558: sub validate_access_key {
2559: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
2560: $cdom=
1.620 albertel 2561: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 2562: $cnum=
1.620 albertel 2563: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
2564: $udom=$env{'user.domain'} unless (defined($udom));
2565: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 2566: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 2567: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 2568: }
2569:
2570: # ------------------------------------- Find the section of student in a course
1.652 albertel 2571: sub devalidate_getsection_cache {
2572: my ($udom,$unam,$courseid)=@_;
2573: my $hashid="$udom:$unam:$courseid";
2574: &devalidate_cache_new('getsection',$hashid);
2575: }
1.298 matthew 2576:
1.815 albertel 2577: sub courseid_to_courseurl {
2578: my ($courseid) = @_;
2579: #already url style courseid
2580: return $courseid if ($courseid =~ m{^/});
2581:
2582: if (exists($env{'course.'.$courseid.'.num'})) {
2583: my $cnum = $env{'course.'.$courseid.'.num'};
2584: my $cdom = $env{'course.'.$courseid.'.domain'};
2585: return "/$cdom/$cnum";
2586: }
2587:
2588: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
2589: if (exists($courseinfo{'num'})) {
2590: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
2591: }
2592:
2593: return undef;
2594: }
2595:
1.298 matthew 2596: sub getsection {
2597: my ($udom,$unam,$courseid)=@_;
1.599 albertel 2598: my $cachetime=1800;
1.551 albertel 2599:
2600: my $hashid="$udom:$unam:$courseid";
1.599 albertel 2601: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 2602: if (defined($cached)) { return $result; }
2603:
1.298 matthew 2604: my %Pending;
2605: my %Expired;
2606: #
2607: # Each role can either have not started yet (pending), be active,
2608: # or have expired.
2609: #
2610: # If there is an active role, we are done.
2611: #
2612: # If there is more than one role which has not started yet,
2613: # choose the one which will start sooner
2614: # If there is one role which has not started yet, return it.
2615: #
2616: # If there is more than one expired role, choose the one which ended last.
2617: # If there is a role which has expired, return it.
2618: #
1.815 albertel 2619: $courseid = &courseid_to_courseurl($courseid);
1.1166 raeburn 2620: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817 raeburn 2621: foreach my $key (keys(%roleshash)) {
1.479 albertel 2622: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 2623: my $section=$1;
2624: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 2625: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 2626: my $now=time;
1.548 albertel 2627: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 2628: $Expired{$end}=$section;
2629: next;
2630: }
1.548 albertel 2631: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 2632: $Pending{$start}=$section;
2633: next;
2634: }
1.599 albertel 2635: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 2636: }
2637: #
2638: # Presumedly there will be few matching roles from the above
2639: # loop and the sorting time will be negligible.
2640: if (scalar(keys(%Pending))) {
2641: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 2642: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 2643: }
2644: if (scalar(keys(%Expired))) {
2645: my @sorted = sort {$a <=> $b} keys(%Expired);
2646: my $time = pop(@sorted);
1.599 albertel 2647: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 2648: }
1.599 albertel 2649: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 2650: }
1.70 www 2651:
1.599 albertel 2652: sub save_cache {
2653: &purge_remembered();
1.722 albertel 2654: #&Apache::loncommon::validate_page();
1.620 albertel 2655: undef(%env);
1.780 albertel 2656: undef($env_loaded);
1.599 albertel 2657: }
1.452 albertel 2658:
1.599 albertel 2659: my $to_remember=-1;
2660: my %remembered;
2661: my %accessed;
2662: my $kicks=0;
2663: my $hits=0;
1.849 albertel 2664: sub make_key {
2665: my ($name,$id) = @_;
1.872 albertel 2666: if (length($id) > 65
2667: && length(&escape($id)) > 200) {
2668: $id=length($id).':'.&Digest::MD5::md5_hex($id);
2669: }
1.849 albertel 2670: return &escape($name.':'.$id);
2671: }
2672:
1.599 albertel 2673: sub devalidate_cache_new {
2674: my ($name,$id,$debug) = @_;
2675: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319 damieng 2676: my $remembered_id=$name.':'.$id;
1.849 albertel 2677: $id=&make_key($name,$id);
1.599 albertel 2678: $memcache->delete($id);
1.1319 damieng 2679: delete($remembered{$remembered_id});
2680: delete($accessed{$remembered_id});
1.599 albertel 2681: }
2682:
2683: sub is_cached_new {
2684: my ($name,$id,$debug) = @_;
1.1319 damieng 2685: my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
2686: if (exists($remembered{$remembered_id})) {
2687: if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
2688: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 2689: $hits++;
1.1319 damieng 2690: return ($remembered{$remembered_id},1);
1.599 albertel 2691: }
1.1319 damieng 2692: $id=&make_key($name,$id);
1.599 albertel 2693: my $value = $memcache->get($id);
2694: if (!(defined($value))) {
2695: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 2696: return (undef,undef);
1.416 albertel 2697: }
1.599 albertel 2698: if ($value eq '__undef__') {
2699: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
2700: $value=undef;
2701: }
1.1319 damieng 2702: &make_room($remembered_id,$value,$debug);
1.599 albertel 2703: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
2704: return ($value,1);
2705: }
2706:
2707: sub do_cache_new {
2708: my ($name,$id,$value,$time,$debug) = @_;
1.1319 damieng 2709: my $remembered_id=$name.':'.$id;
1.849 albertel 2710: $id=&make_key($name,$id);
1.599 albertel 2711: my $setvalue=$value;
2712: if (!defined($setvalue)) {
2713: $setvalue='__undef__';
2714: }
1.623 albertel 2715: if (!defined($time) ) {
2716: $time=600;
2717: }
1.599 albertel 2718: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 2719: my $result = $memcache->set($id,$setvalue,$time);
2720: if (! $result) {
1.872 albertel 2721: &logthis("caching of id -> $id failed");
1.910 albertel 2722: $memcache->disconnect_all();
1.872 albertel 2723: }
1.600 albertel 2724: # need to make a copy of $value
1.1319 damieng 2725: &make_room($remembered_id,$value,$debug);
1.599 albertel 2726: return $value;
2727: }
2728:
2729: sub make_room {
1.1319 damieng 2730: my ($remembered_id,$value,$debug)=@_;
1.919 albertel 2731:
1.1319 damieng 2732: $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919 albertel 2733: : $value;
1.599 albertel 2734: if ($to_remember<0) { return; }
1.1319 damieng 2735: $accessed{$remembered_id}=[&gettimeofday()];
1.599 albertel 2736: if (scalar(keys(%remembered)) <= $to_remember) { return; }
2737: my $to_kick;
2738: my $max_time=0;
2739: foreach my $other (keys(%accessed)) {
2740: if (&tv_interval($accessed{$other}) > $max_time) {
2741: $to_kick=$other;
2742: $max_time=&tv_interval($accessed{$other});
2743: }
2744: }
2745: delete($remembered{$to_kick});
2746: delete($accessed{$to_kick});
2747: $kicks++;
2748: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 2749: return;
2750: }
2751:
1.599 albertel 2752: sub purge_remembered {
1.604 albertel 2753: #&logthis("Tossing ".scalar(keys(%remembered)));
2754: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 2755: undef(%remembered);
2756: undef(%accessed);
1.428 albertel 2757: }
1.70 www 2758: # ------------------------------------- Read an entry from a user's environment
2759:
2760: sub userenvironment {
2761: my ($udom,$unam,@what)=@_;
1.976 raeburn 2762: my $items;
2763: foreach my $item (@what) {
2764: $items.=&escape($item).'&';
2765: }
2766: $items=~s/\&$//;
1.70 www 2767: my %returnhash=();
1.1009 raeburn 2768: my $uhome = &homeserver($unam,$udom);
2769: unless ($uhome eq 'no_host') {
2770: my @answer=split(/\&/,
2771: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 2772: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
2773: return %returnhash;
2774: }
1.1009 raeburn 2775: my $i;
2776: for ($i=0;$i<=$#what;$i++) {
2777: $returnhash{$what[$i]}=&unescape($answer[$i]);
2778: }
1.70 www 2779: }
2780: return %returnhash;
1.1 albertel 2781: }
2782:
1.617 albertel 2783: # ---------------------------------------------------------- Get a studentphoto
2784: sub studentphoto {
2785: my ($udom,$unam,$ext) = @_;
2786: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 2787: if (defined($env{'request.course.id'})) {
1.708 raeburn 2788: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 2789: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
2790: return(&retrievestudentphoto($udom,$unam,$ext));
2791: } else {
2792: my ($result,$perm_reqd)=
1.707 albertel 2793: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2794: if ($result eq 'ok') {
2795: if (!($perm_reqd eq 'yes')) {
2796: return(&retrievestudentphoto($udom,$unam,$ext));
2797: }
2798: }
2799: }
2800: }
2801: } else {
2802: my ($result,$perm_reqd) =
1.707 albertel 2803: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2804: if ($result eq 'ok') {
2805: if (!($perm_reqd eq 'yes')) {
2806: return(&retrievestudentphoto($udom,$unam,$ext));
2807: }
2808: }
2809: }
2810: return '/adm/lonKaputt/lonlogo_broken.gif';
2811: }
2812:
2813: sub retrievestudentphoto {
2814: my ($udom,$unam,$ext,$type) = @_;
2815: my $home=&Apache::lonnet::homeserver($unam,$udom);
2816: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2817: if ($ret eq 'ok') {
2818: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2819: if ($type eq 'thumbnail') {
2820: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2821: }
2822: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2823: return $tokenurl;
2824: } else {
2825: if ($type eq 'thumbnail') {
2826: return '/adm/lonKaputt/genericstudent_tn.gif';
2827: } else {
2828: return '/adm/lonKaputt/lonlogo_broken.gif';
2829: }
1.617 albertel 2830: }
2831: }
2832:
1.263 www 2833: # -------------------------------------------------------------------- New chat
2834:
2835: sub chatsend {
1.724 raeburn 2836: my ($newentry,$anon,$group)=@_;
1.620 albertel 2837: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2838: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2839: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 2840: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 2841: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 2842: &escape($newentry)).':'.$group,$chome);
1.292 www 2843: }
2844:
2845: # ------------------------------------------ Find current version of a resource
2846:
2847: sub getversion {
2848: my $fname=&clutter(shift);
1.1189 raeburn 2849: unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292 www 2850: return ¤tversion(&filelocation('',$fname));
2851: }
2852:
2853: sub currentversion {
2854: my $fname=shift;
2855: my $author=$fname;
2856: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2857: my ($udom,$uname)=split(/\//,$author);
1.1112 www 2858: my $home=&homeserver($uname,$udom);
1.292 www 2859: if ($home eq 'no_host') {
2860: return -1;
2861: }
1.1112 www 2862: my $answer=&reply("currentversion:$fname",$home);
1.292 www 2863: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2864: return -1;
2865: }
1.1112 www 2866: return $answer;
1.263 www 2867: }
2868:
1.1111 www 2869: #
2870: # Return special version number of resource if set by override, empty otherwise
2871: #
2872: sub usedversion {
2873: my $fname=shift;
2874: unless ($fname) { $fname=$env{'request.uri'}; }
2875: my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
2876: if ($urlversion) { return $urlversion; }
2877: return '';
2878: }
2879:
1.1 albertel 2880: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 2881:
1.1 albertel 2882: sub subscribe {
2883: my $fname=shift;
1.761 raeburn 2884: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 2885: $fname=~s/[\n\r]//g;
1.1 albertel 2886: my $author=$fname;
2887: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2888: my ($udom,$uname)=split(/\//,$author);
2889: my $home=homeserver($uname,$udom);
1.335 albertel 2890: if ($home eq 'no_host') {
2891: return 'not_found';
1.1 albertel 2892: }
2893: my $answer=reply("sub:$fname",$home);
1.64 www 2894: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2895: $answer.=' by '.$home;
2896: }
1.1 albertel 2897: return $answer;
2898: }
2899:
1.8 www 2900: # -------------------------------------------------------------- Replicate file
2901:
2902: sub repcopy {
2903: my $filename=shift;
1.23 www 2904: $filename=~s/\/+/\//g;
1.1142 raeburn 2905: my $londocroot = $perlvar{'lonDocRoot'};
2906: if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164 raeburn 2907: if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142 raeburn 2908: if ($filename=~m{^\Q$londocroot/userfiles/\E} or
2909: $filename=~m{^/*(uploaded|editupload)/}) {
1.538 albertel 2910: return &repcopy_userfile($filename);
2911: }
1.532 albertel 2912: $filename=~s/[\n\r]//g;
1.8 www 2913: my $transname="$filename.in.transfer";
1.828 www 2914: # FIXME: this should flock
1.607 raeburn 2915: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 2916: my $remoteurl=subscribe($filename);
1.64 www 2917: if ($remoteurl =~ /^con_lost by/) {
2918: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2919: return 'unavailable';
1.8 www 2920: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 2921: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 2922: return 'not_found';
1.64 www 2923: } elsif ($remoteurl =~ /^rejected by/) {
2924: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2925: return 'forbidden';
1.20 www 2926: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 2927: return 'ok';
1.8 www 2928: } else {
1.290 www 2929: my $author=$filename;
2930: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2931: my ($udom,$uname)=split(/\//,$author);
2932: my $home=homeserver($uname,$udom);
2933: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 2934: my @parts=split(/\//,$filename);
2935: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142 raeburn 2936: if ($path ne "$londocroot/res") {
1.8 www 2937: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 2938: return 'bad_request';
1.8 www 2939: }
2940: my $count;
2941: for ($count=5;$count<$#parts;$count++) {
2942: $path.="/$parts[$count]";
2943: if ((-e $path)!=1) {
2944: mkdir($path,0777);
2945: }
2946: }
2947: my $ua=new LWP::UserAgent;
2948: my $request=new HTTP::Request('GET',"$remoteurl");
2949: my $response=$ua->request($request,$transname);
2950: if ($response->is_error()) {
2951: unlink($transname);
2952: my $message=$response->status_line;
1.672 albertel 2953: &logthis("<font color=\"blue\">WARNING:"
1.12 www 2954: ." LWP get: $message: $filename</font>");
1.607 raeburn 2955: return 'unavailable';
1.8 www 2956: } else {
1.16 www 2957: if ($remoteurl!~/\.meta$/) {
2958: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2959: my $mresponse=$ua->request($mrequest,$filename.'.meta');
2960: if ($mresponse->is_error()) {
2961: unlink($filename.'.meta');
2962: &logthis(
1.672 albertel 2963: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 2964: }
2965: }
1.8 www 2966: rename($transname,$filename);
1.607 raeburn 2967: return 'ok';
1.8 www 2968: }
1.290 www 2969: }
1.8 www 2970: }
1.330 www 2971: }
2972:
2973: # ------------------------------------------------ Get server side include body
2974: sub ssi_body {
1.381 albertel 2975: my ($filelink,%form)=@_;
1.606 matthew 2976: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
2977: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
2978: }
1.953 www 2979: my $output='';
2980: my $response;
1.980 raeburn 2981: if ($filelink=~/^https?\:/) {
1.954 raeburn 2982: ($output,$response)=&externalssi($filelink);
1.953 www 2983: } else {
1.1004 droeschl 2984: $filelink .= $filelink=~/\?/ ? '&' : '?';
2985: $filelink .= 'inhibitmenu=yes';
1.953 www 2986: ($output,$response)=&ssi($filelink,%form);
2987: }
1.778 albertel 2988: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2989: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2990: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2991: if (wantarray) {
2992: return ($output, $response);
2993: } else {
2994: return $output;
2995: }
1.8 www 2996: }
2997:
1.15 www 2998: # --------------------------------------------------------- Server Side Include
2999:
1.782 albertel 3000: sub absolute_url {
3001: my ($host_name) = @_;
3002: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
3003: if ($host_name eq '') {
3004: $host_name = $ENV{'SERVER_NAME'};
3005: }
3006: return $protocol.$host_name;
3007: }
3008:
1.942 foxr 3009: #
3010: # Server side include.
3011: # Parameters:
3012: # fn Possibly encrypted resource name/id.
3013: # form Hash that describes how the rendering should be done
3014: # and other things.
1.944 foxr 3015: # Returns:
1.950 raeburn 3016: # Scalar context: The content of the response.
3017: # Array context: 2 element list of the content and the full response object.
1.942 foxr 3018: #
1.15 www 3019: sub ssi {
3020:
1.944 foxr 3021: my ($fn,%form)=@_;
1.15 www 3022: my $ua=new LWP::UserAgent;
1.23 www 3023: my $request;
1.711 albertel 3024:
3025: $form{'no_update_last_known'}=1;
1.895 albertel 3026: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 3027: if (%form) {
1.782 albertel 3028: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1272 droeschl 3029: $request->content(join('&',map {
3030: my $name = escape($_);
3031: "$name=" . ( ref($form{$_}) eq 'ARRAY'
3032: ? join("&$name=", map {escape($_) } @{$form{$_}})
3033: : &escape($form{$_}) );
3034: } keys(%form)));
1.23 www 3035: } else {
1.782 albertel 3036: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 3037: }
3038:
1.15 www 3039: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1173 foxr 3040: my $response= $ua->request($request);
1.1182 foxr 3041: my $content = $response->content;
3042:
3043:
1.944 foxr 3044: if (wantarray) {
1.1173 foxr 3045: return ($content, $response);
1.944 foxr 3046: } else {
1.1173 foxr 3047: return $content;
1.942 foxr 3048: }
1.324 www 3049: }
3050:
3051: sub externalssi {
3052: my ($url)=@_;
3053: my $ua=new LWP::UserAgent;
3054: my $request=new HTTP::Request('GET',$url);
3055: my $response=$ua->request($request);
1.954 raeburn 3056: if (wantarray) {
3057: return ($response->content, $response);
3058: } else {
3059: return $response->content;
3060: }
1.15 www 3061: }
1.254 www 3062:
1.492 albertel 3063: # -------------------------------- Allow a /uploaded/ URI to be vouched for
3064:
3065: sub allowuploaded {
3066: my ($srcurl,$url)=@_;
3067: $url=&clutter(&declutter($url));
3068: my $dir=$url;
3069: $dir=~s/\/[^\/]+$//;
3070: my %httpref=();
3071: my $httpurl=&hreflocation('',$url);
3072: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 3073: &Apache::lonnet::appenv(\%httpref);
1.254 www 3074: }
1.477 raeburn 3075:
1.1193 raeburn 3076: #
3077: # Determine if the current user should be able to edit a particular resource,
3078: # when viewing in course context.
3079: # (a) When viewing resource used to determine if "Edit" item is included in
3080: # Functions.
3081: # (b) When displaying folder contents in course editor, used to determine if
3082: # "Edit" link will be displayed alongside resource.
3083: #
1.1196 raeburn 3084: # input: six args -- filename (decluttered), course number, course domain,
3085: # url, symb (if registered) and group (if this is a group
3086: # item -- e.g., bulletin board, group page etc.).
3087: # output: array of five scalars --
1.1193 raeburn 3088: # $cfile -- url for file editing if editable on current server
3089: # $home -- homeserver of resource (i.e., for author if published,
3090: # or course if uploaded.).
3091: # $switchserver -- 1 if server switch will be needed.
1.1196 raeburn 3092: # $forceedit -- 1 if icon/link should be to go to edit mode
3093: # $forceview -- 1 if icon/link should be to go to view mode
1.1193 raeburn 3094: #
3095:
3096: sub can_edit_resource {
1.1194 raeburn 3097: my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
3098: my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
3099: #
3100: # For aboutme pages user can only edit his/her own.
3101: #
1.1199 raeburn 3102: if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194 raeburn 3103: my ($sdom,$sname) = ($1,$2);
3104: if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
3105: $home = $env{'user.home'};
3106: $cfile = $resurl;
3107: if ($env{'form.forceedit'}) {
3108: $forceview = 1;
3109: } else {
3110: $forceedit = 1;
3111: }
3112: return ($cfile,$home,$switchserver,$forceedit,$forceview);
3113: } else {
3114: return;
3115: }
3116: }
3117:
3118: if ($env{'request.course.id'}) {
3119: my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
3120: if ($group ne '') {
3121: # if this is a group homepage or group bulletin board, check group privs
3122: my $allowed = 0;
1.1197 raeburn 3123: if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
3124: if ((&allowed('mdg',$env{'request.course.id'}.
1.1198 raeburn 3125: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194 raeburn 3126: (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
3127: $allowed = 1;
3128: }
1.1197 raeburn 3129: } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
3130: if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
3131: (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194 raeburn 3132: $allowed = 1;
3133: }
3134: }
3135: if ($allowed) {
3136: $home=&homeserver($cnum,$cdom);
3137: if ($env{'form.forceedit'}) {
3138: $forceview = 1;
3139: } else {
3140: $forceedit = 1;
3141: }
3142: $cfile = $resurl;
3143: } else {
3144: return;
3145: }
3146: } else {
1.1208 raeburn 3147: if ($resurl =~ m{^/?adm/viewclasslist$}) {
3148: unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
3149: return;
3150: }
3151: } elsif (!$crsedit) {
1.1194 raeburn 3152: #
3153: # No edit allowed where CC has switched to student role.
3154: #
3155: return;
3156: }
3157: }
3158: }
3159:
1.1193 raeburn 3160: if ($file ne '') {
3161: if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194 raeburn 3162: if (&is_course_upload($file,$cnum,$cdom)) {
3163: $uploaded = 1;
3164: $incourse = 1;
1.1193 raeburn 3165: if ($file =~/\.(htm|html|css|js|txt)$/) {
3166: $cfile = &hreflocation('',$file);
1.1201 raeburn 3167: if ($env{'form.forceedit'}) {
3168: $forceview = 1;
3169: } else {
3170: $forceedit = 1;
3171: }
1.1194 raeburn 3172: }
3173: } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
3174: $incourse = 1;
3175: if ($env{'form.forceedit'}) {
3176: $forceview = 1;
3177: } else {
3178: $forceedit = 1;
3179: }
3180: $cfile = $resurl;
3181: } elsif (($resurl ne '') && (&is_on_map($resurl))) {
3182: if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
3183: $incourse = 1;
3184: if ($env{'form.forceedit'}) {
3185: $forceview = 1;
3186: } else {
3187: $forceedit = 1;
3188: }
3189: $cfile = $resurl;
1.1199 raeburn 3190: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194 raeburn 3191: $incourse = 1;
3192: $cfile = $resurl.'/smpedit';
1.1199 raeburn 3193: } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194 raeburn 3194: $incourse = 1;
1.1199 raeburn 3195: if ($env{'form.forceedit'}) {
3196: $forceview = 1;
3197: } else {
3198: $forceedit = 1;
3199: }
3200: $cfile = $resurl;
1.1343 ! raeburn 3201: } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298 raeburn 3202: $incourse = 1;
3203: if ($env{'form.forceedit'}) {
3204: $forceview = 1;
3205: } else {
3206: $forceedit = 1;
3207: }
3208: $cfile = $resurl;
1.1208 raeburn 3209: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3210: $incourse = 1;
3211: if ($env{'form.forceedit'}) {
3212: $forceview = 1;
3213: } else {
3214: $forceedit = 1;
3215: }
3216: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194 raeburn 3217: }
3218: } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
3219: my $template = '/res/lib/templates/simpleproblem.problem';
3220: if (&is_on_map($template)) {
3221: $incourse = 1;
3222: $forceview = 1;
3223: $cfile = $template;
1.1193 raeburn 3224: }
1.1199 raeburn 3225: } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
3226: $incourse = 1;
3227: if ($env{'form.forceedit'}) {
3228: $forceview = 1;
3229: } else {
3230: $forceedit = 1;
3231: }
3232: $cfile = $resurl;
1.1343 ! raeburn 3233: } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298 raeburn 3234: $incourse = 1;
3235: if ($env{'form.forceedit'}) {
3236: $forceview = 1;
3237: } else {
3238: $forceedit = 1;
3239: }
3240: $cfile = $resurl;
1.1199 raeburn 3241: } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
3242: $incourse = 1;
3243: $forceview = 1;
3244: if ($symb) {
3245: my ($map,$id,$res)=&decode_symb($symb);
3246: $env{'request.symb'} = $symb;
3247: $cfile = &clutter($res);
3248: } else {
3249: $cfile = $env{'form.suppurl'};
1.1298 raeburn 3250: my $escfile = &unescape($cfile);
1.1343 ! raeburn 3251: if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298 raeburn 3252: $cfile = '/adm/wrapper'.$escfile;
3253: } else {
3254: $escfile =~ s{^http://}{};
3255: $cfile = &escape("/adm/wrapper/ext/$escfile");
3256: }
1.1199 raeburn 3257: }
1.1234 raeburn 3258: } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
3259: if ($env{'form.forceedit'}) {
3260: $forceview = 1;
3261: } else {
3262: $forceedit = 1;
3263: }
3264: $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193 raeburn 3265: }
3266: }
1.1194 raeburn 3267: if ($uploaded || $incourse) {
3268: $home=&homeserver($cnum,$cdom);
1.1207 raeburn 3269: } elsif ($file !~ m{/$}) {
1.1193 raeburn 3270: $file=~s{^(priv/$match_domain/$match_username)}{/$1};
3271: $file=~s{^($match_domain/$match_username)}{/priv/$1};
3272: # Check that the user has permission to edit this resource
3273: my $setpriv = 1;
3274: my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
3275: if (defined($cfudom)) {
3276: $home=&homeserver($cfuname,$cfudom);
3277: $cfile=$file;
3278: }
3279: }
1.1194 raeburn 3280: if (($cfile ne '') && (!$incourse || $uploaded) &&
3281: (($home ne '') && ($home ne 'no_host'))) {
1.1193 raeburn 3282: my @ids=¤t_machine_ids();
3283: unless (grep(/^\Q$home\E$/,@ids)) {
3284: $switchserver=1;
3285: }
3286: }
3287: }
1.1194 raeburn 3288: return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193 raeburn 3289: }
3290:
3291: sub is_course_upload {
3292: my ($file,$cnum,$cdom) = @_;
3293: my $uploadpath = &LONCAPA::propath($cdom,$cnum);
3294: $uploadpath =~ s{^\/}{};
1.1201 raeburn 3295: if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
3296: ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193 raeburn 3297: return 1;
3298: }
3299: return;
3300: }
3301:
1.1194 raeburn 3302: sub in_course {
1.1195 raeburn 3303: my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
3304: if ($hideprivileged) {
3305: my $skipuser;
1.1219 raeburn 3306: my %coursehash = &coursedescription($cdom.'_'.$cnum);
3307: my @possdoms = ($cdom);
3308: if ($coursehash{'checkforpriv'}) {
3309: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
3310: }
3311: if (&privileged($uname,$udom,\@possdoms)) {
1.1195 raeburn 3312: $skipuser = 1;
3313: if ($coursehash{'nothideprivileged'}) {
3314: foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3315: my $user;
3316: if ($item =~ /:/) {
3317: $user = $item;
3318: } else {
3319: $user = join(':',split(/[\@]/,$item));
3320: }
3321: if ($user eq $uname.':'.$udom) {
3322: undef($skipuser);
3323: last;
3324: }
3325: }
3326: }
3327: if ($skipuser) {
3328: return 0;
3329: }
3330: }
3331: }
1.1194 raeburn 3332: $type ||= 'any';
3333: if (!defined($cdom) || !defined($cnum)) {
3334: my $cid = $env{'request.course.id'};
3335: $cdom = $env{'course.'.$cid.'.domain'};
3336: $cnum = $env{'course.'.$cid.'.num'};
3337: }
3338: my $typesref;
1.1195 raeburn 3339: if (($type eq 'any') || ($type eq 'all')) {
1.1194 raeburn 3340: $typesref = ['active','previous','future'];
3341: } elsif ($type eq 'previous' || $type eq 'future') {
3342: $typesref = [$type];
3343: }
3344: my %roles = &get_my_roles($uname,$udom,'userroles',
3345: $typesref,undef,[$cdom]);
3346: my ($tmp) = keys(%roles);
3347: return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
3348: my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
3349: if (@course_roles > 0) {
3350: return 1;
3351: }
3352: return 0;
3353: }
3354:
1.478 albertel 3355: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 3356: # input: action, courseID, current domain, intended
1.637 raeburn 3357: # path to file, source of file, instruction to parse file for objects,
3358: # ref to hash for embedded objects,
3359: # ref to hash for codebase of java objects.
1.1095 raeburn 3360: # reference to scalar to accommodate mime type determined
3361: # from File::MMagic if $parser = parse.
1.637 raeburn 3362: #
1.485 raeburn 3363: # output: url to file (if action was uploaddoc),
3364: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 3365: #
1.478 albertel 3366: # Allows directory structure to be used within lonUsers/../userfiles/ for a
3367: # course.
1.477 raeburn 3368: #
1.478 albertel 3369: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3370: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
3371: # course's home server.
1.477 raeburn 3372: #
1.478 albertel 3373: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
3374: # be copied from $source (current location) to
3375: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3376: # and will then be copied to
3377: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
3378: # course's home server.
1.485 raeburn 3379: #
1.481 raeburn 3380: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 3381: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 3382: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
3383: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
3384: # in course's home server.
1.637 raeburn 3385: #
1.477 raeburn 3386:
3387: sub process_coursefile {
1.1095 raeburn 3388: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
3389: $mimetype)=@_;
1.477 raeburn 3390: my $fetchresult;
1.638 albertel 3391: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 3392: if ($action eq 'propagate') {
1.638 albertel 3393: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
3394: $home);
1.481 raeburn 3395: } else {
1.477 raeburn 3396: my $fpath = '';
3397: my $fname = $file;
1.478 albertel 3398: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 3399: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 3400: my $filepath = &build_filepath($fpath);
1.481 raeburn 3401: if ($action eq 'copy') {
3402: if ($source eq '') {
3403: $fetchresult = 'no source file';
3404: return $fetchresult;
3405: } else {
3406: my $destination = $filepath.'/'.$fname;
3407: rename($source,$destination);
3408: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3409: $home);
1.481 raeburn 3410: }
3411: } elsif ($action eq 'uploaddoc') {
3412: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 3413: print $fh $env{'form.'.$source};
1.481 raeburn 3414: close($fh);
1.637 raeburn 3415: if ($parser eq 'parse') {
1.1024 raeburn 3416: my $mm = new File::MMagic;
1.1095 raeburn 3417: my $type = $mm->checktype_filename($filepath.'/'.$fname);
3418: if ($type eq 'text/html') {
1.1024 raeburn 3419: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
3420: unless ($parse_result eq 'ok') {
3421: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
3422: }
1.637 raeburn 3423: }
1.1095 raeburn 3424: if (ref($mimetype)) {
3425: $$mimetype = $type;
3426: }
1.637 raeburn 3427: }
1.477 raeburn 3428: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3429: $home);
1.481 raeburn 3430: if ($fetchresult eq 'ok') {
3431: return '/uploaded/'.$fpath.'/'.$fname;
3432: } else {
3433: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 3434: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 3435: return '/adm/notfound.html';
3436: }
1.477 raeburn 3437: }
3438: }
1.485 raeburn 3439: unless ( $fetchresult eq 'ok') {
1.477 raeburn 3440: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 3441: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 3442: }
3443: return $fetchresult;
3444: }
3445:
1.637 raeburn 3446: sub build_filepath {
3447: my ($fpath) = @_;
3448: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
3449: unless ($fpath eq '') {
3450: my @parts=split('/',$fpath);
3451: foreach my $part (@parts) {
3452: $filepath.= '/'.$part;
3453: if ((-e $filepath)!=1) {
3454: mkdir($filepath,0777);
3455: }
3456: }
3457: }
3458: return $filepath;
3459: }
3460:
3461: sub store_edited_file {
1.638 albertel 3462: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 3463: my $file = $primary_url;
3464: $file =~ s#^/uploaded/$docudom/$docuname/##;
3465: my $fpath = '';
3466: my $fname = $file;
3467: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
3468: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
3469: my $filepath = &build_filepath($fpath);
3470: open(my $fh,'>'.$filepath.'/'.$fname);
3471: print $fh $content;
3472: close($fh);
1.638 albertel 3473: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 3474: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 3475: $home);
1.637 raeburn 3476: if ($$fetchresult eq 'ok') {
3477: return '/uploaded/'.$fpath.'/'.$fname;
3478: } else {
1.638 albertel 3479: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
3480: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 3481: return '/adm/notfound.html';
3482: }
3483: }
3484:
1.531 albertel 3485: sub clean_filename {
1.831 albertel 3486: my ($fname,$args)=@_;
1.315 www 3487: # Replace Windows backslashes by forward slashes
1.257 www 3488: $fname=~s/\\/\//g;
1.831 albertel 3489: if (!$args->{'keep_path'}) {
3490: # Get rid of everything but the actual filename
3491: $fname=~s/^.*\/([^\/]+)$/$1/;
3492: }
1.315 www 3493: # Replace spaces by underscores
3494: $fname=~s/\s+/\_/g;
3495: # Replace all other weird characters by nothing
1.831 albertel 3496: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 3497: # Replace all .\d. sequences with _\d. so they no longer look like version
3498: # numbers
3499: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 3500: return $fname;
3501: }
1.1051 raeburn 3502: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
3503: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
3504: # image with the same aspect ratio as the original, but with dimensions which do
3505: # not exceed $resizewidth and $resizeheight.
3506:
1.984 neumanie 3507: sub resizeImage {
1.1051 raeburn 3508: my ($img_path,$resizewidth,$resizeheight) = @_;
3509: my $ima = Image::Magick->new;
3510: my $resized;
3511: if (-e $img_path) {
3512: $ima->Read($img_path);
3513: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
3514: my $width = $ima->Get('width');
3515: my $height = $ima->Get('height');
3516: if ($width > $resizewidth) {
3517: my $factor = $width/$resizewidth;
3518: my $newheight = $height/$factor;
3519: $ima->Scale(width=>$resizewidth,height=>$newheight);
3520: $resized = 1;
3521: }
3522: }
3523: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
3524: my $width = $ima->Get('width');
3525: my $height = $ima->Get('height');
3526: if ($height > $resizeheight) {
3527: my $factor = $height/$resizeheight;
3528: my $newwidth = $width/$factor;
3529: $ima->Scale(width=>$newwidth,height=>$resizeheight);
3530: $resized = 1;
3531: }
3532: }
3533: if ($resized) {
3534: $ima->Write($img_path);
3535: }
3536: }
3537: return;
1.977 amueller 3538: }
3539:
1.608 albertel 3540: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 3541: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093 raeburn 3542: # the desired filename is in $env{"form.$formname.filename"}
1.1090 raeburn 3543: # $context - possible values: coursedoc, existingfile, overwrite,
3544: # canceloverwrite, or ''.
3545: # if 'coursedoc': upload to the current course
3546: # if 'existingfile': write file to tmp/overwrites directory
3547: # if 'canceloverwrite': delete file written to tmp/overwrites directory
3548: # $context is passed as argument to &finishuserfileupload
1.686 albertel 3549: # $subdir - directory in userfile to store the file into
1.858 raeburn 3550: # $parser - instruction to parse file for objects ($parser = parse)
3551: # $allfiles - reference to hash for embedded objects
3552: # $codebase - reference to hash for codebase of java objects
3553: # $desuname - username for permanent storage of uploaded file
3554: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 3555: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
3556: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 3557: # $resizewidth - width (pixels) to which to resize uploaded image
3558: # $resizeheight - height (pixels) to which to resize uploaded image
1.1095 raeburn 3559: # $mimetype - reference to scalar to accommodate mime type determined
1.1152 raeburn 3560: # from File::MMagic.
1.858 raeburn 3561: #
1.686 albertel 3562: # output: url of file in userspace, or error: <message>
3563: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 3564:
1.531 albertel 3565: sub userfileupload {
1.1090 raeburn 3566: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095 raeburn 3567: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 3568: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 3569: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 3570: $fname=&clean_filename($fname);
1.1090 raeburn 3571: # See if there is anything left
1.257 www 3572: unless ($fname) { return 'error: no uploaded file'; }
1.1090 raeburn 3573: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
3574: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
3575: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
3576: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 3577: my $now = time;
1.1090 raeburn 3578: my $filepath;
1.1095 raeburn 3579: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090 raeburn 3580: $filepath = 'tmp/helprequests/'.$now;
3581: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
3582: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
3583: '_'.$env{'user.domain'}.'/pending';
3584: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
3585: my ($docuname,$docudom);
3586: if ($destudom) {
3587: $docudom = $destudom;
3588: } else {
3589: $docudom = $env{'user.domain'};
3590: }
3591: if ($destuname) {
3592: $docuname = $destuname;
3593: } else {
3594: $docuname = $env{'user.name'};
3595: }
3596: if (exists($env{'form.group'})) {
3597: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3598: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3599: }
3600: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
3601: if ($context eq 'canceloverwrite') {
3602: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
3603: if (-e $tempfile) {
3604: my @info = stat($tempfile);
3605: if ($info[9] eq $env{'form.timestamp'}) {
3606: unlink($tempfile);
3607: }
3608: }
3609: return;
1.523 raeburn 3610: }
3611: }
1.1090 raeburn 3612: # Create the directory if not present
1.741 raeburn 3613: my @parts=split(/\//,$filepath);
3614: my $fullpath = $perlvar{'lonDaemons'};
3615: for (my $i=0;$i<@parts;$i++) {
3616: $fullpath .= '/'.$parts[$i];
3617: if ((-e $fullpath)!=1) {
3618: mkdir($fullpath,0777);
3619: }
3620: }
3621: open(my $fh,'>'.$fullpath.'/'.$fname);
3622: print $fh $env{'form.'.$formname};
3623: close($fh);
1.1090 raeburn 3624: if ($context eq 'existingfile') {
3625: my @info = stat($fullpath.'/'.$fname);
3626: return ($fullpath.'/'.$fname,$info[9]);
3627: } else {
3628: return $fullpath.'/'.$fname;
3629: }
1.523 raeburn 3630: }
1.995 raeburn 3631: if ($subdir eq 'scantron') {
3632: $fname = 'scantron_orig_'.$fname;
1.1093 raeburn 3633: } else {
1.995 raeburn 3634: $fname="$subdir/$fname";
3635: }
1.1090 raeburn 3636: if ($context eq 'coursedoc') {
1.638 albertel 3637: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3638: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 3639: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 3640: return &finishuserfileupload($docuname,$docudom,
3641: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 3642: $codebase,$thumbwidth,$thumbheight,
1.1095 raeburn 3643: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 3644: } else {
1.1218 raeburn 3645: if ($env{'form.folder'}) {
3646: $fname=$env{'form.folder'}.'/'.$fname;
3647: }
1.638 albertel 3648: return &process_coursefile('uploaddoc',$docuname,$docudom,
3649: $fname,$formname,$parser,
1.1095 raeburn 3650: $allfiles,$codebase,$mimetype);
1.481 raeburn 3651: }
1.719 banghart 3652: } elsif (defined($destuname)) {
3653: my $docuname=$destuname;
3654: my $docudom=$destudom;
1.860 raeburn 3655: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3656: $parser,$allfiles,$codebase,
1.1051 raeburn 3657: $thumbwidth,$thumbheight,
1.1095 raeburn 3658: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 3659: } else {
1.638 albertel 3660: my $docuname=$env{'user.name'};
3661: my $docudom=$env{'user.domain'};
1.1220 raeburn 3662: if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714 raeburn 3663: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
3664: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
3665: }
1.860 raeburn 3666: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
3667: $parser,$allfiles,$codebase,
1.1051 raeburn 3668: $thumbwidth,$thumbheight,
1.1095 raeburn 3669: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 3670: }
1.271 www 3671: }
3672:
3673: sub finishuserfileupload {
1.860 raeburn 3674: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095 raeburn 3675: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 3676: my $path=$docudom.'/'.$docuname.'/';
1.258 www 3677: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 3678:
1.860 raeburn 3679: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 3680: $file=$fname;
3681: if ($fname=~m|/|) {
3682: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
3683: $path.=$fnamepath.'/';
3684: }
1.259 www 3685: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 3686: my $count;
3687: for ($count=4;$count<=$#parts;$count++) {
3688: $filepath.="/$parts[$count]";
3689: if ((-e $filepath)!=1) {
3690: mkdir($filepath,0777);
3691: }
3692: }
1.984 neumanie 3693:
1.258 www 3694: # Save the file
3695: {
1.701 albertel 3696: if (!open(FH,'>'.$filepath.'/'.$file)) {
3697: &logthis('Failed to create '.$filepath.'/'.$file);
3698: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
3699: return '/adm/notfound.html';
3700: }
1.1090 raeburn 3701: if ($context eq 'overwrite') {
1.1117 foxr 3702: my $source = LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090 raeburn 3703: my $target = $filepath.'/'.$file;
3704: if (-e $source) {
3705: my @info = stat($source);
3706: if ($info[9] eq $env{'form.timestamp'}) {
3707: unless (&File::Copy::move($source,$target)) {
3708: &logthis('Failed to overwrite '.$filepath.'/'.$file);
3709: return "Moving from $source failed";
3710: }
3711: } else {
3712: return "Temporary file: $source had unexpected date/time for last modification";
3713: }
3714: } else {
3715: return "Temporary file: $source missing";
3716: }
3717: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 3718: &logthis('Failed to write to '.$filepath.'/'.$file);
3719: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
3720: return '/adm/notfound.html';
3721: }
1.570 albertel 3722: close(FH);
1.1051 raeburn 3723: if ($resizewidth && $resizeheight) {
3724: my $mm = new File::MMagic;
3725: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
3726: if ($mime_type =~ m{^image/}) {
3727: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
3728: }
1.977 amueller 3729: }
1.258 www 3730: }
1.1152 raeburn 3731: if (($context eq 'coursedoc') || ($parser eq 'parse')) {
3732: if (ref($mimetype)) {
3733: if ($$mimetype eq '') {
3734: my $mm = new File::MMagic;
3735: my $type = $mm->checktype_filename($filepath.'/'.$file);
3736: $$mimetype = $type;
3737: }
3738: }
3739: }
1.637 raeburn 3740: if ($parser eq 'parse') {
1.1152 raeburn 3741: if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024 raeburn 3742: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
3743: $allfiles,$codebase);
3744: unless ($parse_result eq 'ok') {
3745: &logthis('Failed to parse '.$filepath.$file.
3746: ' for embedded media: '.$parse_result);
3747: }
1.637 raeburn 3748: }
3749: }
1.860 raeburn 3750: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
3751: my $input = $filepath.'/'.$file;
3752: my $output = $filepath.'/'.'tn-'.$file;
3753: my $thumbsize = $thumbwidth.'x'.$thumbheight;
3754: system("convert -sample $thumbsize $input $output");
3755: if (-e $filepath.'/'.'tn-'.$file) {
3756: $fetchthumb = 1;
3757: }
3758: }
1.858 raeburn 3759:
1.259 www 3760: # Notify homeserver to grep it
3761: #
1.984 neumanie 3762: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 3763: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 3764: if ($fetchresult eq 'ok') {
1.860 raeburn 3765: if ($fetchthumb) {
3766: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
3767: if ($thumbresult ne 'ok') {
3768: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
3769: $docuhome.': '.$thumbresult);
3770: }
3771: }
1.259 www 3772: #
1.258 www 3773: # Return the URL to it
1.494 albertel 3774: return '/uploaded/'.$path.$file;
1.263 www 3775: } else {
1.494 albertel 3776: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
3777: ': '.$fetchresult);
1.263 www 3778: return '/adm/notfound.html';
1.858 raeburn 3779: }
1.493 albertel 3780: }
3781:
1.637 raeburn 3782: sub extract_embedded_items {
1.961 raeburn 3783: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 3784: my @state = ();
1.1164 raeburn 3785: my (%lastids,%related,%shockwave,%flashvars);
1.637 raeburn 3786: my %javafiles = (
3787: codebase => '',
3788: code => '',
3789: archive => ''
3790: );
3791: my %mediafiles = (
3792: src => '',
3793: movie => '',
3794: );
1.648 raeburn 3795: my $p;
3796: if ($content) {
3797: $p = HTML::LCParser->new($content);
3798: } else {
1.961 raeburn 3799: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 3800: }
1.641 albertel 3801: while (my $t=$p->get_token()) {
1.640 albertel 3802: if ($t->[0] eq 'S') {
3803: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 3804: push(@state, $tagname);
1.648 raeburn 3805: if (lc($tagname) eq 'allow') {
3806: &add_filetype($allfiles,$attr->{'src'},'src');
3807: }
1.640 albertel 3808: if (lc($tagname) eq 'img') {
3809: &add_filetype($allfiles,$attr->{'src'},'src');
3810: }
1.886 albertel 3811: if (lc($tagname) eq 'a') {
1.1222 raeburn 3812: unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221 raeburn 3813: &add_filetype($allfiles,$attr->{'href'},'href');
3814: }
1.886 albertel 3815: }
1.645 raeburn 3816: if (lc($tagname) eq 'script') {
1.1164 raeburn 3817: my $src;
1.645 raeburn 3818: if ($attr->{'archive'} =~ /\.jar$/i) {
3819: &add_filetype($allfiles,$attr->{'archive'},'archive');
3820: } else {
1.1164 raeburn 3821: if ($attr->{'src'} ne '') {
3822: $src = $attr->{'src'};
3823: &add_filetype($allfiles,$src,'src');
3824: }
3825: }
3826: my $text = $p->get_trimmed_text();
3827: if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
3828: my @swfargs = split(/,/,$1);
3829: foreach my $item (@swfargs) {
3830: $item =~ s/["']//g;
3831: $item =~ s/^\s+//;
3832: $item =~ s/\s+$//;
3833: }
3834: if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
3835: if (ref($related{$swfargs[0]}) eq 'ARRAY') {
3836: push(@{$related{$swfargs[0]}},$swfargs[2]);
3837: } else {
3838: $related{$swfargs[0]} = [$swfargs[2]];
3839: }
3840: }
1.645 raeburn 3841: }
3842: }
3843: if (lc($tagname) eq 'link') {
3844: if (lc($attr->{'rel'}) eq 'stylesheet') {
3845: &add_filetype($allfiles,$attr->{'href'},'href');
3846: }
3847: }
1.640 albertel 3848: if (lc($tagname) eq 'object' ||
3849: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
3850: foreach my $item (keys(%javafiles)) {
3851: $javafiles{$item} = '';
3852: }
1.1164 raeburn 3853: if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
3854: $lastids{lc($tagname)} = $attr->{'id'};
3855: }
1.640 albertel 3856: }
3857: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
3858: my $name = lc($attr->{'name'});
3859: foreach my $item (keys(%javafiles)) {
3860: if ($name eq $item) {
3861: $javafiles{$item} = $attr->{'value'};
3862: last;
3863: }
3864: }
1.1164 raeburn 3865: my $pathfrom;
1.640 albertel 3866: foreach my $item (keys(%mediafiles)) {
3867: if ($name eq $item) {
1.1164 raeburn 3868: $pathfrom = $attr->{'value'};
3869: $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
3870: &add_filetype($allfiles,$pathfrom,$name);
1.640 albertel 3871: last;
3872: }
3873: }
1.1164 raeburn 3874: if ($name eq 'flashvars') {
3875: $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
3876: }
3877: if ($pathfrom ne '') {
3878: &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
3879: $pathfrom);
3880: }
1.640 albertel 3881: }
3882: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
3883: foreach my $item (keys(%javafiles)) {
3884: if ($attr->{$item}) {
3885: $javafiles{$item} = $attr->{$item};
3886: last;
3887: }
3888: }
3889: foreach my $item (keys(%mediafiles)) {
3890: if ($attr->{$item}) {
3891: &add_filetype($allfiles,$attr->{$item},$item);
3892: last;
3893: }
3894: }
1.1164 raeburn 3895: if (lc($tagname) eq 'embed') {
3896: if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
3897: &embedded_dependency($allfiles,\%related,$attr->{'name'},
3898: $attr->{'src'});
3899: }
3900: }
1.640 albertel 3901: }
1.1243 raeburn 3902: if (lc($tagname) eq 'iframe') {
3903: my $src = $attr->{'src'} ;
3904: if (($src ne '') && ($src !~ m{^(/|https?://)})) {
3905: &add_filetype($allfiles,$src,'src');
3906: } elsif ($src =~ m{^/}) {
3907: if ($env{'request.course.id'}) {
3908: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
3909: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
3910: my $url = &hreflocation('',$fullpath);
3911: if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
3912: my $relpath = $1;
3913: if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
3914: &add_filetype($allfiles,$1,'src');
3915: }
3916: }
3917: }
3918: }
3919: }
1.1164 raeburn 3920: if ($t->[4] =~ m{/>$}) {
1.1243 raeburn 3921: pop(@state);
1.1164 raeburn 3922: }
1.640 albertel 3923: } elsif ($t->[0] eq 'E') {
3924: my ($tagname) = ($t->[1]);
3925: if ($javafiles{'codebase'} ne '') {
3926: $javafiles{'codebase'} .= '/';
3927: }
3928: if (lc($tagname) eq 'applet' ||
3929: lc($tagname) eq 'object' ||
3930: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
3931: ) {
3932: foreach my $item (keys(%javafiles)) {
3933: if ($item ne 'codebase' && $javafiles{$item} ne '') {
3934: my $file=$javafiles{'codebase'}.$javafiles{$item};
3935: &add_filetype($allfiles,$file,$item);
3936: }
3937: }
3938: }
3939: pop @state;
3940: }
3941: }
1.1164 raeburn 3942: foreach my $id (sort(keys(%flashvars))) {
3943: if ($shockwave{$id} ne '') {
3944: my @pairs = split(/\&/,$flashvars{$id});
3945: foreach my $pair (@pairs) {
3946: my ($key,$value) = split(/\=/,$pair);
3947: if ($key eq 'thumb') {
3948: &add_filetype($allfiles,$value,$key);
3949: } elsif ($key eq 'content') {
3950: my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
3951: my ($ext) = ($value =~ /\.([^.]+)$/);
3952: if ($ext ne '') {
3953: &add_filetype($allfiles,$path.$value,$ext);
3954: }
3955: }
3956: }
3957: }
3958: }
1.637 raeburn 3959: return 'ok';
3960: }
3961:
1.639 albertel 3962: sub add_filetype {
3963: my ($allfiles,$file,$type)=@_;
3964: if (exists($allfiles->{$file})) {
3965: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
3966: push(@{$allfiles->{$file}}, &escape($type));
3967: }
3968: } else {
3969: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 3970: }
3971: }
3972:
1.1164 raeburn 3973: sub embedded_dependency {
3974: my ($allfiles,$related,$identifier,$pathfrom) = @_;
3975: if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
3976: if (($identifier ne '') &&
3977: (ref($related->{$identifier}) eq 'ARRAY') &&
3978: ($pathfrom ne '')) {
3979: my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
3980: foreach my $dep (@{$related->{$identifier}}) {
3981: &add_filetype($allfiles,$path.$dep,'object');
3982: }
3983: }
3984: }
3985: return;
3986: }
3987:
1.493 albertel 3988: sub removeuploadedurl {
1.984 neumanie 3989: my ($url)=@_;
3990: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 3991: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 3992: }
3993:
3994: sub removeuserfile {
3995: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 3996: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 3997: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 3998: if ($result eq 'ok') {
1.798 raeburn 3999: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
4000: my $metafile = $fname.'.meta';
4001: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 4002: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 4003: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4004: my $sqlresult =
1.823 albertel 4005: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4006: 'portfolio_metadata',$group,
4007: 'delete');
1.798 raeburn 4008: }
4009: }
4010: return $result;
1.257 www 4011: }
1.15 www 4012:
1.530 albertel 4013: sub mkdiruserfile {
4014: my ($docuname,$docudom,$dir)=@_;
4015: my $home=&homeserver($docuname,$docudom);
4016: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
4017: }
4018:
1.531 albertel 4019: sub renameuserfile {
4020: my ($docuname,$docudom,$old,$new)=@_;
4021: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 4022: my $result = &reply("renameuserfile:$docudom:$docuname:".
4023: &escape("$old").':'.&escape("$new"),$home);
4024: if ($result eq 'ok') {
4025: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
4026: my $oldmeta = $old.'.meta';
4027: my $newmeta = $new.'.meta';
4028: my $metaresult =
4029: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 4030: my $url = "/uploaded/$docudom/$docuname/$old";
4031: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 4032: my $sqlresult =
1.823 albertel 4033: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 4034: 'portfolio_metadata',$group,
4035: 'delete');
1.798 raeburn 4036: }
4037: }
4038: return $result;
1.531 albertel 4039: }
4040:
1.14 www 4041: # ------------------------------------------------------------------------- Log
4042:
4043: sub log {
4044: my ($dom,$nam,$hom,$what)=@_;
1.47 www 4045: return critical("log:$dom:$nam:$what",$hom);
1.157 www 4046: }
4047:
4048: # ------------------------------------------------------------------ Course Log
1.352 www 4049: #
4050: # This routine flushes several buffers of non-mission-critical nature
4051: #
1.157 www 4052:
4053: sub flushcourselogs {
1.352 www 4054: &logthis('Flushing log buffers');
4055: #
4056: # course logs
4057: # This is a log of all transactions in a course, which can be used
4058: # for data mining purposes
4059: #
4060: # It also collects the courseid database, which lists last transaction
4061: # times and course titles for all courseids
4062: #
4063: my %courseidbuffer=();
1.921 raeburn 4064: foreach my $crsid (keys(%courselogs)) {
1.352 www 4065: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 4066: &escape($courselogs{$crsid}),
4067: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 4068: delete $courselogs{$crsid};
4069: } else {
4070: &logthis('Failed to flush log buffer for '.$crsid);
4071: if (length($courselogs{$crsid})>40000) {
1.672 albertel 4072: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 4073: " exceeded maximum size, deleting.</font>");
4074: delete $courselogs{$crsid};
4075: }
1.352 www 4076: }
1.920 raeburn 4077: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 4078: 'description' => $coursedescrbuf{$crsid},
4079: 'inst_code' => $courseinstcodebuf{$crsid},
4080: 'type' => $coursetypebuf{$crsid},
4081: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 4082: };
1.191 harris41 4083: }
1.352 www 4084: #
4085: # Write course id database (reverse lookup) to homeserver of courses
4086: # Is used in pickcourse
4087: #
1.840 albertel 4088: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 4089: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 4090: $courseidbuffer{$crs_home},
4091: $crs_home,'timeonly');
1.352 www 4092: }
4093: #
4094: # File accesses
4095: # Writes to the dynamic metadata of resources to get hit counts, etc.
4096: #
1.449 matthew 4097: foreach my $entry (keys(%accesshash)) {
1.458 matthew 4098: if ($entry =~ /___count$/) {
4099: my ($dom,$name);
1.807 albertel 4100: ($dom,$name,undef)=
1.811 albertel 4101: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 4102: if (! defined($dom) || $dom eq '' ||
4103: ! defined($name) || $name eq '') {
1.620 albertel 4104: my $cid = $env{'request.course.id'};
4105: $dom = $env{'request.'.$cid.'.domain'};
4106: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 4107: }
1.450 matthew 4108: my $value = $accesshash{$entry};
4109: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
4110: my %temphash=($url => $value);
1.449 matthew 4111: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
4112: if ($result eq 'ok') {
4113: delete $accesshash{$entry};
4114: }
4115: } else {
1.811 albertel 4116: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159 www 4117: if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450 matthew 4118: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 4119: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
4120: delete $accesshash{$entry};
4121: }
1.185 www 4122: }
1.191 harris41 4123: }
1.352 www 4124: #
4125: # Roles
4126: # Reverse lookup of user roles for course faculty/staff and co-authorship
4127: #
1.800 albertel 4128: foreach my $entry (keys(%userrolehash)) {
1.351 www 4129: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 4130: split(/\:/,$entry);
4131: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 4132: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 4133: $rudom,$runame) eq 'ok') {
4134: delete $userrolehash{$entry};
4135: }
4136: }
1.662 raeburn 4137: #
1.1334 raeburn 4138: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662 raeburn 4139: #
4140: my %domrolebuffer = ();
1.1000 raeburn 4141: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 4142: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 4143: if ($domrolebuffer{$rudom}) {
4144: $domrolebuffer{$rudom}.='&'.&escape($entry).
4145: '='.&escape($domainrolehash{$entry});
4146: } else {
4147: $domrolebuffer{$rudom}.=&escape($entry).
4148: '='.&escape($domainrolehash{$entry});
4149: }
4150: delete $domainrolehash{$entry};
4151: }
4152: foreach my $dom (keys(%domrolebuffer)) {
1.1324 raeburn 4153: my %servers;
4154: if (defined(&domain($dom,'primary'))) {
4155: my $primary=&domain($dom,'primary');
4156: my $hostname=&hostname($primary);
4157: $servers{$primary} = $hostname;
4158: } else {
4159: %servers = &get_servers($dom,'library');
4160: }
1.841 albertel 4161: foreach my $tryserver (keys(%servers)) {
1.1324 raeburn 4162: if (&reply('domroleput:'.$dom.':'.
4163: $domrolebuffer{$dom},$tryserver) eq 'ok') {
4164: last;
4165: } else {
1.841 albertel 4166: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
4167: }
1.662 raeburn 4168: }
4169: }
1.186 www 4170: $dumpcount++;
1.157 www 4171: }
4172:
4173: sub courselog {
4174: my $what=shift;
1.158 www 4175: $what=time.':'.$what;
1.620 albertel 4176: unless ($env{'request.course.id'}) { return ''; }
4177: $coursedombuf{$env{'request.course.id'}}=
4178: $env{'course.'.$env{'request.course.id'}.'.domain'};
4179: $coursenumbuf{$env{'request.course.id'}}=
4180: $env{'course.'.$env{'request.course.id'}.'.num'};
4181: $coursehombuf{$env{'request.course.id'}}=
4182: $env{'course.'.$env{'request.course.id'}.'.home'};
4183: $coursedescrbuf{$env{'request.course.id'}}=
4184: $env{'course.'.$env{'request.course.id'}.'.description'};
4185: $courseinstcodebuf{$env{'request.course.id'}}=
4186: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
4187: $courseownerbuf{$env{'request.course.id'}}=
4188: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 4189: $coursetypebuf{$env{'request.course.id'}}=
4190: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 4191: if (defined $courselogs{$env{'request.course.id'}}) {
4192: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 4193: } else {
1.620 albertel 4194: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 4195: }
1.620 albertel 4196: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 4197: &flushcourselogs();
4198: }
1.158 www 4199: }
4200:
4201: sub courseacclog {
4202: my $fnsymb=shift;
1.620 albertel 4203: unless ($env{'request.course.id'}) { return ''; }
4204: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144 www 4205: if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187 www 4206: $what.=':POST';
1.583 matthew 4207: # FIXME: Probably ought to escape things....
1.800 albertel 4208: foreach my $key (keys(%env)) {
4209: if ($key=~/^form\.(.*)/) {
1.975 raeburn 4210: my $formitem = $1;
4211: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
4212: $what.=':'.$formitem.'='.$env{$key};
4213: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
4214: $what.=':'.$formitem.'='.$env{$key};
4215: }
1.158 www 4216: }
1.191 harris41 4217: }
1.583 matthew 4218: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
4219: # FIXME: We should not be depending on a form parameter that someone
4220: # editing lonsearchcat.pm might change in the future.
1.620 albertel 4221: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 4222: $what.= ':POST';
4223: # FIXME: Probably ought to escape things....
4224: foreach my $element ('courseexp','crsfulltext','crsrelated',
4225: 'crsdiscuss') {
1.620 albertel 4226: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 4227: }
4228: }
1.158 www 4229: }
4230: &courselog($what);
1.149 www 4231: }
4232:
1.185 www 4233: sub countacc {
4234: my $url=&declutter(shift);
1.458 matthew 4235: return if (! defined($url) || $url eq '');
1.620 albertel 4236: unless ($env{'request.course.id'}) { return ''; }
1.1158 www 4237: #
4238: # Mark that this url was used in this course
4239: #
1.620 albertel 4240: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158 www 4241: #
4242: # Increase the access count for this resource in this child process
4243: #
1.281 www 4244: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 4245: $accesshash{$key}++;
1.185 www 4246: }
1.349 www 4247:
1.361 www 4248: sub linklog {
4249: my ($from,$to)=@_;
4250: $from=&declutter($from);
4251: $to=&declutter($to);
4252: $accesshash{$from.'___'.$to.'___comefrom'}=1;
4253: $accesshash{$to.'___'.$from.'___goto'}=1;
4254: }
1.1160 www 4255:
4256: sub statslog {
4257: my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
4258: if ($users<2) { return; }
4259: my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
4260: 'course' => $env{'request.course.id'},
4261: 'sections' => '"all"',
4262: 'num_students' => $users,
4263: 'part' => $part,
4264: 'symb' => $symb,
4265: 'mean_tries' => $av_attempts,
4266: 'deg_of_diff' => $degdiff});
4267: foreach my $key (keys(%dynstore)) {
4268: $accesshash{$key}=$dynstore{$key};
4269: }
4270: }
1.361 www 4271:
1.349 www 4272: sub userrolelog {
4273: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169 droeschl 4274: if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350 www 4275: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4276: $userrolehash
4277: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 4278: =$tend.':'.$tstart;
1.662 raeburn 4279: }
1.1169 droeschl 4280: if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898 albertel 4281: $userrolehash
4282: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
4283: =$tend.':'.$tstart;
4284: }
1.1334 raeburn 4285: if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662 raeburn 4286: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
4287: $domainrolehash
4288: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
4289: = $tend.':'.$tstart;
4290: }
1.351 www 4291: }
4292:
1.957 raeburn 4293: sub courserolelog {
4294: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184 raeburn 4295: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
4296: my $cdom = $1;
4297: my $cnum = $2;
4298: my $sec = $3;
4299: my $namespace = 'rolelog';
4300: my %storehash = (
4301: role => $trole,
4302: start => $tstart,
4303: end => $tend,
4304: selfenroll => $selfenroll,
4305: context => $context,
4306: );
4307: if ($trole eq 'gr') {
4308: $namespace = 'groupslog';
4309: $storehash{'group'} = $sec;
4310: } else {
4311: $storehash{'section'} = $sec;
4312: }
1.1188 raeburn 4313: &write_log('course',$namespace,\%storehash,$delflag,$username,
4314: $domain,$cnum,$cdom);
1.1184 raeburn 4315: if (($trole ne 'st') || ($sec ne '')) {
4316: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957 raeburn 4317: }
4318: }
4319: return;
4320: }
4321:
1.1184 raeburn 4322: sub domainrolelog {
4323: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4324: if ($area =~ m{^/($match_domain)/$}) {
4325: my $cdom = $1;
4326: my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
4327: my $namespace = 'rolelog';
4328: my %storehash = (
4329: role => $trole,
4330: start => $tstart,
4331: end => $tend,
4332: context => $context,
4333: );
1.1188 raeburn 4334: &write_log('domain',$namespace,\%storehash,$delflag,$username,
4335: $domain,$domconfiguser,$cdom);
1.1184 raeburn 4336: }
4337: return;
4338:
4339: }
4340:
4341: sub coauthorrolelog {
4342: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
4343: if ($area =~ m{^/($match_domain)/($match_username)$}) {
4344: my $audom = $1;
4345: my $auname = $2;
4346: my $namespace = 'rolelog';
4347: my %storehash = (
4348: role => $trole,
4349: start => $tstart,
4350: end => $tend,
4351: context => $context,
4352: );
1.1188 raeburn 4353: &write_log('author',$namespace,\%storehash,$delflag,$username,
4354: $domain,$auname,$audom);
1.1184 raeburn 4355: }
4356: return;
4357: }
4358:
1.351 www 4359: sub get_course_adv_roles {
1.948 raeburn 4360: my ($cid,$codes) = @_;
1.620 albertel 4361: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 4362: my %coursehash=&coursedescription($cid);
1.988 raeburn 4363: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 4364: my %nothide=();
1.800 albertel 4365: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 4366: if ($user !~ /:/) {
4367: $nothide{join(':',split(/[\@]/,$user))}=1;
4368: } else {
4369: $nothide{$user}=1;
4370: }
1.470 www 4371: }
1.1219 raeburn 4372: my @possdoms = ($coursehash{'domain'});
4373: if ($coursehash{'checkforpriv'}) {
4374: push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
4375: }
1.351 www 4376: my %returnhash=();
4377: my %dumphash=
4378: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
4379: my $now=time;
1.997 raeburn 4380: my %privileged;
1.1000 raeburn 4381: foreach my $entry (keys(%dumphash)) {
1.800 albertel 4382: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 4383: if (($tstart) && ($tstart<0)) { next; }
4384: if (($tend) && ($tend<$now)) { next; }
4385: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 4386: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 4387: if ($username eq '' || $domain eq '') { next; }
1.1219 raeburn 4388: if ((&privileged($username,$domain,\@possdoms)) &&
1.997 raeburn 4389: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 4390: if ($role eq 'cr') { next; }
1.948 raeburn 4391: if ($codes) {
4392: if ($section) { $role .= ':'.$section; }
4393: if ($returnhash{$role}) {
4394: $returnhash{$role}.=','.$username.':'.$domain;
4395: } else {
4396: $returnhash{$role}=$username.':'.$domain;
4397: }
1.351 www 4398: } else {
1.988 raeburn 4399: my $key=&plaintext($role,$crstype);
1.973 bisitz 4400: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 4401: if ($returnhash{$key}) {
4402: $returnhash{$key}.=','.$username.':'.$domain;
4403: } else {
4404: $returnhash{$key}=$username.':'.$domain;
4405: }
1.351 www 4406: }
1.948 raeburn 4407: }
1.400 www 4408: return %returnhash;
4409: }
4410:
4411: sub get_my_roles {
1.937 raeburn 4412: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 4413: unless (defined($uname)) { $uname=$env{'user.name'}; }
4414: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 4415: my (%dumphash,%nothide);
1.1086 raeburn 4416: if ($context eq 'userroles') {
1.1166 raeburn 4417: %dumphash = &dump('roles',$udom,$uname);
1.858 raeburn 4418: } else {
1.1219 raeburn 4419: %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 4420: if ($hidepriv) {
4421: my %coursehash=&coursedescription($udom.'_'.$uname);
4422: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
4423: if ($user !~ /:/) {
4424: $nothide{join(':',split(/[\@]/,$user))} = 1;
4425: } else {
4426: $nothide{$user} = 1;
4427: }
4428: }
4429: }
1.858 raeburn 4430: }
1.400 www 4431: my %returnhash=();
4432: my $now=time;
1.999 raeburn 4433: my %privileged;
1.800 albertel 4434: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 4435: my ($role,$tend,$tstart);
4436: if ($context eq 'userroles') {
1.1149 raeburn 4437: next if ($entry =~ /^rolesdef/);
1.867 raeburn 4438: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
4439: } else {
4440: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
4441: }
1.400 www 4442: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 4443: my $status = 'active';
1.939 raeburn 4444: if (($tend) && ($tend<=$now)) {
1.832 raeburn 4445: $status = 'previous';
4446: }
4447: if (($tstart) && ($now<$tstart)) {
4448: $status = 'future';
4449: }
4450: if (ref($types) eq 'ARRAY') {
4451: if (!grep(/^\Q$status\E$/,@{$types})) {
4452: next;
4453: }
4454: } else {
4455: if ($status ne 'active') {
4456: next;
4457: }
4458: }
1.867 raeburn 4459: my ($rolecode,$username,$domain,$section,$area);
4460: if ($context eq 'userroles') {
1.1186 raeburn 4461: ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867 raeburn 4462: (undef,$domain,$username,$section) = split(/\//,$area);
4463: } else {
4464: ($role,$username,$domain,$section) = split(/\:/,$entry);
4465: }
1.832 raeburn 4466: if (ref($roledoms) eq 'ARRAY') {
4467: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
4468: next;
4469: }
4470: }
4471: if (ref($roles) eq 'ARRAY') {
4472: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 4473: if ($role =~ /^cr\//) {
4474: if (!grep(/^cr$/,@{$roles})) {
4475: next;
4476: }
1.1104 raeburn 4477: } elsif ($role =~ /^gr\//) {
4478: if (!grep(/^gr$/,@{$roles})) {
4479: next;
4480: }
1.922 raeburn 4481: } else {
4482: next;
4483: }
1.832 raeburn 4484: }
1.867 raeburn 4485: }
1.937 raeburn 4486: if ($hidepriv) {
1.1219 raeburn 4487: my @privroles = ('dc','su');
1.999 raeburn 4488: if ($context eq 'userroles') {
1.1219 raeburn 4489: next if (grep(/^\Q$role\E$/,@privroles));
1.999 raeburn 4490: } else {
1.1219 raeburn 4491: my $possdoms = [$domain];
4492: if (ref($roledoms) eq 'ARRAY') {
4493: push(@{$possdoms},@{$roledoms});
1.999 raeburn 4494: }
1.1219 raeburn 4495: if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999 raeburn 4496: if (!$nothide{$username.':'.$domain}) {
4497: next;
4498: }
4499: }
1.937 raeburn 4500: }
4501: }
1.933 raeburn 4502: if ($withsec) {
4503: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
4504: $tstart.':'.$tend;
4505: } else {
4506: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
4507: }
1.832 raeburn 4508: }
1.373 www 4509: return %returnhash;
1.399 www 4510: }
4511:
1.1333 raeburn 4512: sub get_all_adhocroles {
4513: my ($dom) = @_;
4514: my @roles_by_num = ();
4515: my %domdefaults = &get_domain_defaults($dom);
4516: my (%description,%access_in_dom,%access_info);
4517: if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
4518: my $count = 0;
4519: my %domcurrent = %{$domdefaults{'adhocroles'}};
4520: my %ordered;
4521: foreach my $role (sort(keys(%domcurrent))) {
4522: my ($order,$desc,$access_in_dom);
4523: if (ref($domcurrent{$role}) eq 'HASH') {
4524: $order = $domcurrent{$role}{'order'};
4525: $desc = $domcurrent{$role}{'desc'};
4526: $access_in_dom{$role} = $domcurrent{$role}{'access'};
4527: $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
4528: }
4529: if ($order eq '') {
4530: $order = $count;
4531: }
4532: $ordered{$order} = $role;
4533: if ($desc ne '') {
4534: $description{$role} = $desc;
4535: } else {
4536: $description{$role}= $role;
4537: }
4538: $count++;
4539: }
4540: foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
4541: push(@roles_by_num,$ordered{$item});
4542: }
4543: }
4544: return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
4545: }
4546:
1.1332 raeburn 4547: sub get_my_adhocroles {
1.1333 raeburn 4548: my ($cid,$checkreg) = @_;
4549: my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
4550: if ($env{'request.course.id'} eq $cid) {
4551: $cdom = $env{'course.'.$cid.'.domain'};
4552: $cnum = $env{'course.'.$cid.'.num'};
4553: $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
4554: } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
4555: $cdom = $1;
4556: $cnum = $2;
4557: %info = &Apache::lonnet::get('environment',['internal.coursecode'],
4558: $cdom,$cnum);
4559: }
4560: if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
4561: my $user = $env{'user.name'}.':'.$env{'user.domain'};
4562: my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
4563: if ($rosterhash{$user} ne '') {
4564: my $type = (split(/:/,$rosterhash{$user}))[5];
4565: return ([],{}) if ($type eq 'auto');
4566: }
4567: }
4568: if (($cdom ne '') && ($cnum ne '')) {
1.1334 raeburn 4569: if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332 raeburn 4570: my $then=$env{'user.login.time'};
4571: my $update=$env{'user.update.time'};
1.1335 raeburn 4572: if (!$update) {
4573: $update = $then;
4574: }
4575: my @liveroles;
1.1334 raeburn 4576: foreach my $role ('dh','da') {
4577: if ($env{"user.role.$role./$cdom/"}) {
1.1335 raeburn 4578: my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334 raeburn 4579: my $limit = $update;
4580: if ($env{'request.role'} eq "$role./$cdom/") {
4581: $limit = $then;
4582: }
1.1335 raeburn 4583: my $activerole = 1;
4584: if ($tstart && $tstart>$limit) { $activerole = 0; }
4585: if ($tend && $tend <$limit) { $activerole = 0; }
4586: if ($activerole) {
4587: push(@liveroles,$role);
4588: }
1.1334 raeburn 4589: }
1.1332 raeburn 4590: }
1.1335 raeburn 4591: if (@liveroles) {
1.1332 raeburn 4592: if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333 raeburn 4593: my ($accessref,$accessinfo,%access_in_dom);
4594: ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
4595: if (ref($roles_by_num) eq 'ARRAY') {
4596: if (@{$roles_by_num}) {
1.1332 raeburn 4597: my %settings;
4598: if ($env{'request.course.id'} eq $cid) {
4599: foreach my $envkey (keys(%env)) {
4600: if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
4601: $settings{$1} = $env{$envkey};
4602: }
4603: }
4604: } else {
4605: %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
4606: }
4607: my %setincrs;
4608: if ($settings{'internal.adhocaccess'}) {
4609: map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
4610: }
4611: my @statuses;
4612: if ($env{'environment.inststatus'}) {
4613: @statuses = split(/,/,$env{'environment.inststatus'});
4614: }
4615: my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333 raeburn 4616: if (ref($accessref) eq 'HASH') {
4617: %access_in_dom = %{$accessref};
4618: }
4619: foreach my $role (@{$roles_by_num}) {
1.1332 raeburn 4620: my ($curraccess,@okstatus,@personnel);
4621: if ($setincrs{$role}) {
4622: ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
4623: if ($curraccess eq 'status') {
4624: @okstatus = split(/\&/,$rest);
4625: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4626: @personnel = split(/\&/,$rest);
4627: }
4628: } else {
4629: $curraccess = $access_in_dom{$role};
1.1333 raeburn 4630: if (ref($accessinfo) eq 'HASH') {
4631: if ($curraccess eq 'status') {
4632: if (ref($accessinfo->{$role}) eq 'ARRAY') {
4633: @okstatus = @{$accessinfo->{$role}};
4634: }
4635: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4636: if (ref($accessinfo->{$role}) eq 'ARRAY') {
4637: @personnel = @{$accessinfo->{$role}};
4638: }
1.1332 raeburn 4639: }
4640: }
4641: }
4642: if ($curraccess eq 'none') {
4643: next;
4644: } elsif ($curraccess eq 'all') {
4645: push(@possroles,$role);
1.1336 raeburn 4646: } elsif ($curraccess eq 'dh') {
1.1335 raeburn 4647: if (grep(/^dh$/,@liveroles)) {
4648: push(@possroles,$role);
4649: } else {
4650: next;
4651: }
1.1336 raeburn 4652: } elsif ($curraccess eq 'da') {
1.1335 raeburn 4653: if (grep(/^da$/,@liveroles)) {
4654: push(@possroles,$role);
4655: } else {
4656: next;
4657: }
1.1332 raeburn 4658: } elsif ($curraccess eq 'status') {
4659: if (@okstatus) {
4660: if (!@statuses) {
4661: if (grep(/^default$/,@okstatus)) {
4662: push(@possroles,$role);
4663: }
4664: } else {
4665: foreach my $status (@okstatus) {
4666: if (grep(/^\Q$status\E$/,@statuses)) {
4667: push(@possroles,$role);
4668: last;
4669: }
4670: }
4671: }
4672: }
4673: } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
4674: if (grep(/^\Q$user\E$/,@personnel)) {
4675: if ($curraccess eq 'exc') {
4676: push(@possroles,$role);
4677: }
4678: } elsif ($curraccess eq 'inc') {
4679: push(@possroles,$role);
4680: }
4681: }
4682: }
4683: }
4684: }
4685: }
4686: }
4687: }
4688: }
1.1333 raeburn 4689: unless (ref($description) eq 'HASH') {
4690: if (ref($roles_by_num) eq 'ARRAY') {
4691: my %desc;
4692: map { $desc{$_} = $_; } (@{$roles_by_num});
4693: $description = \%desc;
4694: } else {
4695: $description = {};
4696: }
4697: }
4698: return (\@possroles,$description);
1.1332 raeburn 4699: }
4700:
1.399 www 4701: # ----------------------------------------------------- Frontpage Announcements
4702: #
4703: #
4704:
4705: sub postannounce {
4706: my ($server,$text)=@_;
1.844 albertel 4707: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 4708: unless ($text=~/\w/) { $text=''; }
4709: return &reply('setannounce:'.&escape($text),$server);
4710: }
4711:
4712: sub getannounce {
1.448 albertel 4713:
4714: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 4715: my $announcement='';
1.800 albertel 4716: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 4717: close($fh);
1.399 www 4718: if ($announcement=~/\w/) {
4719: return
4720: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 4721: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 4722: } else {
4723: return '';
4724: }
4725: } else {
4726: return '';
4727: }
1.351 www 4728: }
1.353 www 4729:
4730: # ---------------------------------------------------------- Course ID routines
4731: # Deal with domain's nohist_courseid.db files
4732: #
4733:
4734: sub courseidput {
1.921 raeburn 4735: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 4736: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 4737: my $outcome;
4738: if ($caller eq 'timeonly') {
4739: my $cids = '';
4740: foreach my $item (keys(%$storehash)) {
4741: $cids.=&escape($item).'&';
4742: }
4743: $cids=~s/\&$//;
4744: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
4745: $coursehome);
4746: } else {
4747: my $items = '';
4748: foreach my $item (keys(%$storehash)) {
4749: $items.= &escape($item).'='.
4750: &freeze_escape($$storehash{$item}).'&';
4751: }
4752: $items=~s/\&$//;
4753: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
4754: $coursehome);
1.918 raeburn 4755: }
4756: if ($outcome eq 'unknown_cmd') {
4757: my $what;
4758: foreach my $cid (keys(%$storehash)) {
4759: $what .= &escape($cid).'=';
1.921 raeburn 4760: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 4761: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 4762: }
4763: $what =~ s/\:$/&/;
4764: }
4765: $what =~ s/\&$//;
4766: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
4767: } else {
4768: return $outcome;
4769: }
1.353 www 4770: }
4771:
4772: sub courseiddump {
1.921 raeburn 4773: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 4774: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 4775: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247 raeburn 4776: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287 raeburn 4777: $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918 raeburn 4778: my $as_hash = 1;
4779: my %returnhash;
4780: if (!$domfilter) { $domfilter=''; }
1.845 albertel 4781: my %libserv = &all_library();
4782: foreach my $tryserver (keys(%libserv)) {
4783: if ( ( $hostidflag == 1
4784: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
4785: || (!defined($hostidflag)) ) {
4786:
1.918 raeburn 4787: if (($domfilter eq '') ||
4788: (&host_domain($tryserver) eq $domfilter)) {
1.1180 droeschl 4789: my $rep;
4790: if (grep { $_ eq $tryserver } current_machine_ids()) {
4791: $rep = LONCAPA::Lond::dump_course_id_handler(
4792: join(":", (&host_domain($tryserver), $sincefilter,
4793: &escape($descfilter), &escape($instcodefilter),
4794: &escape($ownerfilter), &escape($coursefilter),
4795: &escape($typefilter), &escape($regexp_ok),
4796: $as_hash, &escape($selfenrollonly),
4797: &escape($catfilter), $showhidden, $caller,
4798: &escape($cloner), &escape($cc_clone), $cloneonly,
4799: &escape($createdbefore), &escape($createdafter),
1.1287 raeburn 4800: &escape($creationcontext),$domcloner,$hasuniquecode,
4801: $reqcrsdom,&escape($reqinstcode))));
1.1180 droeschl 4802: } else {
4803: $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
4804: $sincefilter.':'.&escape($descfilter).':'.
4805: &escape($instcodefilter).':'.&escape($ownerfilter).
4806: ':'.&escape($coursefilter).':'.&escape($typefilter).
4807: ':'.&escape($regexp_ok).':'.$as_hash.':'.
4808: &escape($selfenrollonly).':'.&escape($catfilter).':'.
4809: $showhidden.':'.$caller.':'.&escape($cloner).':'.
4810: &escape($cc_clone).':'.$cloneonly.':'.
4811: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287 raeburn 4812: &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
4813: ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180 droeschl 4814: }
4815:
1.918 raeburn 4816: my @pairs=split(/\&/,$rep);
4817: foreach my $item (@pairs) {
4818: my ($key,$value)=split(/\=/,$item,2);
4819: $key = &unescape($key);
4820: next if ($key =~ /^error: 2 /);
4821: my $result = &thaw_unescape($value);
4822: if (ref($result) eq 'HASH') {
4823: $returnhash{$key}=$result;
4824: } else {
1.921 raeburn 4825: my @responses = split(/:/,$value);
4826: my @items = ('description','inst_code','owner','type');
1.918 raeburn 4827: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 4828: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 4829: }
1.1008 raeburn 4830: }
1.353 www 4831: }
4832: }
4833: }
4834: }
4835: return %returnhash;
4836: }
4837:
1.1055 raeburn 4838: sub courselastaccess {
4839: my ($cdom,$cnum,$hostidref) = @_;
4840: my %returnhash;
4841: if ($cdom && $cnum) {
4842: my $chome = &homeserver($cnum,$cdom);
4843: if ($chome ne 'no_host') {
4844: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
4845: &extract_lastaccess(\%returnhash,$rep);
4846: }
4847: } else {
4848: if (!$cdom) { $cdom=''; }
4849: my %libserv = &all_library();
4850: foreach my $tryserver (keys(%libserv)) {
4851: if (ref($hostidref) eq 'ARRAY') {
4852: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
4853: }
4854: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
4855: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
4856: &extract_lastaccess(\%returnhash,$rep);
4857: }
4858: }
4859: }
4860: return %returnhash;
4861: }
4862:
4863: sub extract_lastaccess {
4864: my ($returnhash,$rep) = @_;
4865: if (ref($returnhash) eq 'HASH') {
4866: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
4867: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
4868: $rep eq '') {
4869: my @pairs=split(/\&/,$rep);
4870: foreach my $item (@pairs) {
4871: my ($key,$value)=split(/\=/,$item,2);
4872: $key = &unescape($key);
4873: next if ($key =~ /^error: 2 /);
4874: $returnhash->{$key} = &thaw_unescape($value);
4875: }
4876: }
4877: }
4878: return;
4879: }
4880:
1.658 raeburn 4881: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 4882:
4883: sub dcmailput {
1.685 raeburn 4884: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 4885: my $status = &Apache::lonnet::critical(
1.740 www 4886: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
4887: &escape($message),$server);
1.662 raeburn 4888: return $status;
4889: }
4890:
1.658 raeburn 4891: sub dcmaildump {
4892: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 4893: my %returnhash=();
1.846 albertel 4894:
4895: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 4896: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
4897: &escape($enddate).':';
4898: my @esc_senders=map { &escape($_)} @$senders;
4899: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 4900: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 4901: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 4902: if (($key) && ($value)) {
4903: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 4904: }
4905: }
4906: }
4907: return %returnhash;
4908: }
1.662 raeburn 4909: # ---------------------------------------------------------- Domain roles
4910:
4911: sub get_domain_roles {
4912: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 4913: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 4914: $startdate = '.';
4915: }
1.1018 raeburn 4916: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 4917: $enddate = '.';
4918: }
1.922 raeburn 4919: my $rolelist;
4920: if (ref($roles) eq 'ARRAY') {
1.1219 raeburn 4921: $rolelist = join('&',@{$roles});
1.922 raeburn 4922: }
1.662 raeburn 4923: my %personnel = ();
1.841 albertel 4924:
4925: my %servers = &get_servers($dom,'library');
4926: foreach my $tryserver (keys(%servers)) {
4927: %{$personnel{$tryserver}}=();
4928: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
4929: &escape($startdate).':'.
4930: &escape($enddate).':'.
4931: &escape($rolelist), $tryserver))) {
4932: my ($key,$value) = split(/\=/,$line,2);
4933: if (($key) && ($value)) {
4934: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
4935: }
4936: }
1.662 raeburn 4937: }
4938: return %personnel;
4939: }
1.658 raeburn 4940:
1.1332 raeburn 4941: sub get_active_domroles {
4942: my ($dom,$roles) = @_;
4943: return () unless (ref($roles) eq 'ARRAY');
4944: my $now = time;
4945: my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
4946: my %domroles;
4947: foreach my $server (keys(%dompersonnel)) {
4948: foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
4949: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
4950: $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
4951: }
4952: }
4953: return %domroles;
4954: }
4955:
1.1057 www 4956: # ----------------------------------------------------------- Interval timing
1.149 www 4957:
1.1153 www 4958: {
4959: # Caches needed for speedup of navmaps
4960: # We don't want to cache this for very long at all (5 seconds at most)
4961: #
4962: # The user for whom we cache
4963: my $cachedkey='';
4964: # The cached times for this user
4965: my %cachedtimes=();
4966: # When this was last done
1.1282 raeburn 4967: my $cachedtime='';
1.1153 www 4968:
4969: sub load_all_first_access {
1.1308 raeburn 4970: my ($uname,$udom,$ignorecache)=@_;
1.1156 www 4971: if (($cachedkey eq $uname.':'.$udom) &&
1.1308 raeburn 4972: (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
4973: (!$ignorecache)) {
1.1154 raeburn 4974: return;
4975: }
4976: $cachedtime=time;
4977: $cachedkey=$uname.':'.$udom;
4978: %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153 www 4979: }
4980:
1.504 albertel 4981: sub get_first_access {
1.1308 raeburn 4982: my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790 albertel 4983: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 4984: if ($argsymb) { $symb=$argsymb; }
4985: my ($map,$id,$res)=&decode_symb($symb);
1.1162 raeburn 4986: if ($argmap) { $map = $argmap; }
1.926 albertel 4987: if ($type eq 'course') {
4988: $res='course';
4989: } elsif ($type eq 'map') {
1.588 albertel 4990: $res=&symbread($map);
4991: } else {
4992: $res=$symb;
4993: }
1.1308 raeburn 4994: &load_all_first_access($uname,$udom,$ignorecache);
1.1153 www 4995: return $cachedtimes{"$courseid\0$res"};
1.504 albertel 4996: }
4997:
4998: sub set_first_access {
1.1162 raeburn 4999: my ($type,$interval)=@_;
1.790 albertel 5000: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 5001: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 5002: if ($type eq 'course') {
5003: $res='course';
5004: } elsif ($type eq 'map') {
1.588 albertel 5005: $res=&symbread($map);
5006: } else {
5007: $res=$symb;
5008: }
1.1153 www 5009: $cachedkey='';
1.1162 raeburn 5010: my $firstaccess=&get_first_access($type,$symb,$map);
1.505 albertel 5011: if (!$firstaccess) {
1.1162 raeburn 5012: my $start = time;
5013: my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
5014: $udom,$uname);
5015: if ($putres eq 'ok') {
5016: &put('timerinterval',{"$courseid\0$res"=>$interval},
5017: $udom,$uname);
5018: &appenv(
5019: {
5020: 'course.'.$courseid.'.firstaccess.'.$res => $start,
5021: 'course.'.$courseid.'.timerinterval.'.$res => $interval,
5022: }
5023: );
5024: }
5025: return $putres;
1.505 albertel 5026: }
5027: return 'already_set';
1.504 albertel 5028: }
1.1153 www 5029: }
1.1282 raeburn 5030:
1.110 www 5031: # --------------------------------------------- Set Expire Date for Spreadsheet
5032:
5033: sub expirespread {
5034: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 5035: my $cid=$env{'request.course.id'};
1.110 www 5036: if ($cid) {
5037: my $now=time;
5038: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 5039: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
5040: $env{'course.'.$cid.'.num'}.
1.110 www 5041: ':nohist_expirationdates:'.
5042: &escape($key).'='.$now,
1.620 albertel 5043: $env{'course.'.$cid.'.home'})
1.110 www 5044: }
5045: return 'ok';
1.14 www 5046: }
5047:
1.109 www 5048: # ----------------------------------------------------- Devalidate Spreadsheets
5049:
5050: sub devalidate {
1.325 www 5051: my ($symb,$uname,$udom)=@_;
1.620 albertel 5052: my $cid=$env{'request.course.id'};
1.109 www 5053: if ($cid) {
1.391 matthew 5054: # delete the stored spreadsheets for
5055: # - the student level sheet of this user in course's homespace
5056: # - the assessment level sheet for this resource
5057: # for this user in user's homespace
1.553 albertel 5058: # - current conditional state info
1.325 www 5059: my $key=$uname.':'.$udom.':';
1.109 www 5060: my $status=
1.299 matthew 5061: &del('nohist_calculatedsheets',
1.391 matthew 5062: [$key.'studentcalc:'],
1.620 albertel 5063: $env{'course.'.$cid.'.domain'},
5064: $env{'course.'.$cid.'.num'})
1.133 albertel 5065: .' '.
5066: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 5067: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 5068: unless ($status eq 'ok ok') {
5069: &logthis('Could not devalidate spreadsheet '.
1.325 www 5070: $uname.' at '.$udom.' for '.
1.109 www 5071: $symb.': '.$status);
1.133 albertel 5072: }
1.553 albertel 5073: &delenv('user.state.'.$cid);
1.109 www 5074: }
5075: }
5076:
1.265 albertel 5077: sub get_scalar {
5078: my ($string,$end) = @_;
5079: my $value;
5080: if ($$string =~ s/^([^&]*?)($end)/$2/) {
5081: $value = $1;
5082: } elsif ($$string =~ s/^([^&]*?)&//) {
5083: $value = $1;
5084: }
5085: return &unescape($value);
5086: }
5087:
5088: sub array2str {
5089: my (@array) = @_;
5090: my $result=&arrayref2str(\@array);
5091: $result=~s/^__ARRAY_REF__//;
5092: $result=~s/__END_ARRAY_REF__$//;
5093: return $result;
5094: }
5095:
1.204 albertel 5096: sub arrayref2str {
5097: my ($arrayref) = @_;
1.265 albertel 5098: my $result='__ARRAY_REF__';
1.204 albertel 5099: foreach my $elem (@$arrayref) {
1.265 albertel 5100: if(ref($elem) eq 'ARRAY') {
5101: $result.=&arrayref2str($elem).'&';
5102: } elsif(ref($elem) eq 'HASH') {
5103: $result.=&hashref2str($elem).'&';
5104: } elsif(ref($elem)) {
5105: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 5106: } else {
5107: $result.=&escape($elem).'&';
5108: }
5109: }
5110: $result=~s/\&$//;
1.265 albertel 5111: $result .= '__END_ARRAY_REF__';
1.204 albertel 5112: return $result;
5113: }
5114:
1.168 albertel 5115: sub hash2str {
1.204 albertel 5116: my (%hash) = @_;
5117: my $result=&hashref2str(\%hash);
1.265 albertel 5118: $result=~s/^__HASH_REF__//;
5119: $result=~s/__END_HASH_REF__$//;
1.204 albertel 5120: return $result;
5121: }
5122:
5123: sub hashref2str {
5124: my ($hashref)=@_;
1.265 albertel 5125: my $result='__HASH_REF__';
1.800 albertel 5126: foreach my $key (sort(keys(%$hashref))) {
5127: if (ref($key) eq 'ARRAY') {
5128: $result.=&arrayref2str($key).'=';
5129: } elsif (ref($key) eq 'HASH') {
5130: $result.=&hashref2str($key).'=';
5131: } elsif (ref($key)) {
1.265 albertel 5132: $result.='=';
1.800 albertel 5133: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 5134: } else {
1.1132 raeburn 5135: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 5136: }
5137:
1.800 albertel 5138: if(ref($hashref->{$key}) eq 'ARRAY') {
5139: $result.=&arrayref2str($hashref->{$key}).'&';
5140: } elsif(ref($hashref->{$key}) eq 'HASH') {
5141: $result.=&hashref2str($hashref->{$key}).'&';
5142: } elsif(ref($hashref->{$key})) {
1.265 albertel 5143: $result.='&';
1.800 albertel 5144: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 5145: } else {
1.800 albertel 5146: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 5147: }
5148: }
1.168 albertel 5149: $result=~s/\&$//;
1.265 albertel 5150: $result .= '__END_HASH_REF__';
1.168 albertel 5151: return $result;
5152: }
5153:
5154: sub str2hash {
1.265 albertel 5155: my ($string)=@_;
5156: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
5157: return %$hash;
5158: }
5159:
5160: sub str2hashref {
1.168 albertel 5161: my ($string) = @_;
1.265 albertel 5162:
5163: my %hash;
5164:
5165: if($string !~ /^__HASH_REF__/) {
5166: if (! ($string eq '' || !defined($string))) {
5167: $hash{'error'}='Not hash reference';
5168: }
5169: return (\%hash, $string);
5170: }
5171:
5172: $string =~ s/^__HASH_REF__//;
5173:
5174: while($string !~ /^__END_HASH_REF__/) {
5175: #key
5176: my $key='';
5177: if($string =~ /^__HASH_REF__/) {
5178: ($key, $string)=&str2hashref($string);
5179: if(defined($key->{'error'})) {
5180: $hash{'error'}='Bad data';
5181: return (\%hash, $string);
5182: }
5183: } elsif($string =~ /^__ARRAY_REF__/) {
5184: ($key, $string)=&str2arrayref($string);
5185: if($key->[0] eq 'Array reference error') {
5186: $hash{'error'}='Bad data';
5187: return (\%hash, $string);
5188: }
5189: } else {
5190: $string =~ s/^(.*?)=//;
1.267 albertel 5191: $key=&unescape($1);
1.265 albertel 5192: }
5193: $string =~ s/^=//;
5194:
5195: #value
5196: my $value='';
5197: if($string =~ /^__HASH_REF__/) {
5198: ($value, $string)=&str2hashref($string);
5199: if(defined($value->{'error'})) {
5200: $hash{'error'}='Bad data';
5201: return (\%hash, $string);
5202: }
5203: } elsif($string =~ /^__ARRAY_REF__/) {
5204: ($value, $string)=&str2arrayref($string);
5205: if($value->[0] eq 'Array reference error') {
5206: $hash{'error'}='Bad data';
5207: return (\%hash, $string);
5208: }
5209: } else {
5210: $value=&get_scalar(\$string,'__END_HASH_REF__');
5211: }
5212: $string =~ s/^&//;
5213:
5214: $hash{$key}=$value;
1.204 albertel 5215: }
1.265 albertel 5216:
5217: $string =~ s/^__END_HASH_REF__//;
5218:
5219: return (\%hash, $string);
1.204 albertel 5220: }
5221:
5222: sub str2array {
1.265 albertel 5223: my ($string)=@_;
5224: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
5225: return @$array;
5226: }
5227:
5228: sub str2arrayref {
1.204 albertel 5229: my ($string) = @_;
1.265 albertel 5230: my @array;
5231:
5232: if($string !~ /^__ARRAY_REF__/) {
5233: if (! ($string eq '' || !defined($string))) {
5234: $array[0]='Array reference error';
5235: }
5236: return (\@array, $string);
5237: }
5238:
5239: $string =~ s/^__ARRAY_REF__//;
5240:
5241: while($string !~ /^__END_ARRAY_REF__/) {
5242: my $value='';
5243: if($string =~ /^__HASH_REF__/) {
5244: ($value, $string)=&str2hashref($string);
5245: if(defined($value->{'error'})) {
5246: $array[0] ='Array reference error';
5247: return (\@array, $string);
5248: }
5249: } elsif($string =~ /^__ARRAY_REF__/) {
5250: ($value, $string)=&str2arrayref($string);
5251: if($value->[0] eq 'Array reference error') {
5252: $array[0] ='Array reference error';
5253: return (\@array, $string);
5254: }
5255: } else {
5256: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
5257: }
5258: $string =~ s/^&//;
5259:
5260: push(@array, $value);
1.191 harris41 5261: }
1.265 albertel 5262:
5263: $string =~ s/^__END_ARRAY_REF__//;
5264:
5265: return (\@array, $string);
1.168 albertel 5266: }
5267:
1.167 albertel 5268: # -------------------------------------------------------------------Temp Store
5269:
1.168 albertel 5270: sub tmpreset {
5271: my ($symb,$namespace,$domain,$stuname) = @_;
5272: if (!$symb) {
5273: $symb=&symbread();
1.620 albertel 5274: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5275: }
5276: $symb=escape($symb);
5277:
1.620 albertel 5278: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 5279: $namespace=~s/\//\_/g;
5280: $namespace=~s/\W//g;
5281:
1.620 albertel 5282: if (!$domain) { $domain=$env{'user.domain'}; }
5283: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5284: if ($domain eq 'public' && $stuname eq 'public') {
5285: $stuname=$ENV{'REMOTE_ADDR'};
5286: }
1.1117 foxr 5287: my $path=LONCAPA::tempdir();
1.168 albertel 5288: my %hash;
5289: if (tie(%hash,'GDBM_File',
5290: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5291: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 5292: foreach my $key (keys(%hash)) {
1.180 albertel 5293: if ($key=~ /:$symb/) {
1.168 albertel 5294: delete($hash{$key});
5295: }
5296: }
5297: }
5298: }
5299:
1.167 albertel 5300: sub tmpstore {
1.168 albertel 5301: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
5302:
5303: if (!$symb) {
5304: $symb=&symbread();
1.620 albertel 5305: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5306: }
5307: $symb=escape($symb);
5308:
5309: if (!$namespace) {
5310: # I don't think we would ever want to store this for a course.
5311: # it seems this will only be used if we don't have a course.
1.620 albertel 5312: #$namespace=$env{'request.course.id'};
1.168 albertel 5313: #if (!$namespace) {
1.620 albertel 5314: $namespace=$env{'request.state'};
1.168 albertel 5315: #}
5316: }
5317: $namespace=~s/\//\_/g;
5318: $namespace=~s/\W//g;
1.620 albertel 5319: if (!$domain) { $domain=$env{'user.domain'}; }
5320: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5321: if ($domain eq 'public' && $stuname eq 'public') {
5322: $stuname=$ENV{'REMOTE_ADDR'};
5323: }
1.168 albertel 5324: my $now=time;
5325: my %hash;
1.1117 foxr 5326: my $path=LONCAPA::tempdir();
1.168 albertel 5327: if (tie(%hash,'GDBM_File',
5328: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5329: &GDBM_WRCREAT(),0640)) {
1.168 albertel 5330: $hash{"version:$symb"}++;
5331: my $version=$hash{"version:$symb"};
5332: my $allkeys='';
5333: foreach my $key (keys(%$storehash)) {
5334: $allkeys.=$key.':';
1.591 albertel 5335: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 5336: }
5337: $hash{"$version:$symb:timestamp"}=$now;
5338: $allkeys.='timestamp';
5339: $hash{"$version:keys:$symb"}=$allkeys;
5340: if (untie(%hash)) {
5341: return 'ok';
5342: } else {
5343: return "error:$!";
5344: }
5345: } else {
5346: return "error:$!";
5347: }
5348: }
1.167 albertel 5349:
1.168 albertel 5350: # -----------------------------------------------------------------Temp Restore
1.167 albertel 5351:
1.168 albertel 5352: sub tmprestore {
5353: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 5354:
1.168 albertel 5355: if (!$symb) {
5356: $symb=&symbread();
1.620 albertel 5357: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 5358: }
5359: $symb=escape($symb);
5360:
1.620 albertel 5361: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 5362:
1.620 albertel 5363: if (!$domain) { $domain=$env{'user.domain'}; }
5364: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 5365: if ($domain eq 'public' && $stuname eq 'public') {
5366: $stuname=$ENV{'REMOTE_ADDR'};
5367: }
1.168 albertel 5368: my %returnhash;
5369: $namespace=~s/\//\_/g;
5370: $namespace=~s/\W//g;
5371: my %hash;
1.1117 foxr 5372: my $path=LONCAPA::tempdir();
1.168 albertel 5373: if (tie(%hash,'GDBM_File',
5374: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 5375: &GDBM_READER(),0640)) {
1.168 albertel 5376: my $version=$hash{"version:$symb"};
5377: $returnhash{'version'}=$version;
5378: my $scope;
5379: for ($scope=1;$scope<=$version;$scope++) {
5380: my $vkeys=$hash{"$scope:keys:$symb"};
5381: my @keys=split(/:/,$vkeys);
5382: my $key;
5383: $returnhash{"$scope:keys"}=$vkeys;
5384: foreach $key (@keys) {
1.591 albertel 5385: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
5386: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 5387: }
5388: }
1.168 albertel 5389: if (!(untie(%hash))) {
5390: return "error:$!";
5391: }
5392: } else {
5393: return "error:$!";
5394: }
5395: return %returnhash;
1.167 albertel 5396: }
5397:
1.9 www 5398: # ----------------------------------------------------------------------- Store
5399:
5400: sub store {
1.1269 raeburn 5401: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 5402: my $home='';
5403:
1.168 albertel 5404: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5405:
1.213 www 5406: $symb=&symbclean($symb);
1.122 albertel 5407: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 5408:
1.620 albertel 5409: if (!$domain) { $domain=$env{'user.domain'}; }
5410: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 5411:
5412: &devalidate($symb,$stuname,$domain);
1.109 www 5413:
5414: $symb=escape($symb);
1.187 www 5415: if (!$namespace) {
1.620 albertel 5416: unless ($namespace=$env{'request.course.id'}) {
1.187 www 5417: return '';
5418: }
5419: }
1.620 albertel 5420: if (!$home) { $home=$env{'user.home'}; }
1.447 www 5421:
5422: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5423: $$storehash{'host'}=$perlvar{'lonHostID'};
5424:
1.12 www 5425: my $namevalue='';
1.800 albertel 5426: foreach my $key (keys(%$storehash)) {
5427: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 5428: }
1.12 www 5429: $namevalue=~s/\&$//;
1.187 www 5430: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269 raeburn 5431: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9 www 5432: }
5433:
1.47 www 5434: # -------------------------------------------------------------- Critical Store
5435:
5436: sub cstore {
1.1269 raeburn 5437: my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124 www 5438: my $home='';
5439:
1.168 albertel 5440: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5441:
1.213 www 5442: $symb=&symbclean($symb);
1.122 albertel 5443: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 5444:
1.620 albertel 5445: if (!$domain) { $domain=$env{'user.domain'}; }
5446: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 5447:
5448: &devalidate($symb,$stuname,$domain);
1.109 www 5449:
5450: $symb=escape($symb);
1.187 www 5451: if (!$namespace) {
1.620 albertel 5452: unless ($namespace=$env{'request.course.id'}) {
1.187 www 5453: return '';
5454: }
5455: }
1.620 albertel 5456: if (!$home) { $home=$env{'user.home'}; }
1.447 www 5457:
5458: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
5459: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 5460:
1.47 www 5461: my $namevalue='';
1.800 albertel 5462: foreach my $key (keys(%$storehash)) {
5463: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 5464: }
1.47 www 5465: $namevalue=~s/\&$//;
1.187 www 5466: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 5467: return critical
1.1269 raeburn 5468: ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47 www 5469: }
5470:
1.9 www 5471: # --------------------------------------------------------------------- Restore
5472:
5473: sub restore {
1.124 www 5474: my ($symb,$namespace,$domain,$stuname) = @_;
5475: my $home='';
5476:
1.168 albertel 5477: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 5478:
1.122 albertel 5479: if (!$symb) {
1.1224 raeburn 5480: return if ($namespace eq 'courserequests');
5481: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 5482: } else {
1.1224 raeburn 5483: unless ($namespace eq 'courserequests') {
5484: $symb=&escape(&symbclean($symb));
5485: }
1.122 albertel 5486: }
1.188 www 5487: if (!$namespace) {
1.620 albertel 5488: unless ($namespace=$env{'request.course.id'}) {
1.188 www 5489: return '';
5490: }
5491: }
1.620 albertel 5492: if (!$domain) { $domain=$env{'user.domain'}; }
5493: if (!$stuname) { $stuname=$env{'user.name'}; }
5494: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 5495: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
5496:
1.12 www 5497: my %returnhash=();
1.800 albertel 5498: foreach my $line (split(/\&/,$answer)) {
5499: my ($name,$value)=split(/\=/,$line);
1.591 albertel 5500: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 5501: }
1.75 www 5502: my $version;
5503: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 5504: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
5505: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 5506: }
1.75 www 5507: }
1.13 www 5508: return %returnhash;
1.34 www 5509: }
5510:
5511: # ---------------------------------------------------------- Course Description
1.1118 foxr 5512: #
5513: #
1.34 www 5514:
5515: sub coursedescription {
1.731 albertel 5516: my ($courseid,$args)=@_;
1.34 www 5517: $courseid=~s/^\///;
1.49 www 5518: $courseid=~s/\_/\//g;
1.34 www 5519: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 5520: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 5521: my $normalid=$cdomain.'_'.$cnum;
5522: # need to always cache even if we get errors otherwise we keep
5523: # trying and trying and trying to get the course description.
5524: my %envhash=();
5525: my %returnhash=();
1.731 albertel 5526:
5527: my $expiretime=600;
5528: if ($env{'request.course.id'} eq $normalid) {
5529: $expiretime=120;
5530: }
5531:
5532: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
5533: if (!$args->{'freshen_cache'}
5534: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
5535: foreach my $key (keys(%env)) {
5536: next if ($key !~ /^\Q$prefix\E(.*)/);
5537: my ($setting) = $1;
5538: $returnhash{$setting} = $env{$key};
5539: }
5540: return %returnhash;
5541: }
5542:
1.1118 foxr 5543: # get the data again
5544:
1.731 albertel 5545: if (!$args->{'one_time'}) {
5546: $envhash{'course.'.$normalid.'.last_cache'}=time;
5547: }
1.811 albertel 5548:
1.34 www 5549: if ($chome ne 'no_host') {
1.302 albertel 5550: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 5551: if (!exists($returnhash{'con_lost'})) {
1.1118 foxr 5552: my $username = $env{'user.name'}; # Defult username
5553: if(defined $args->{'user'}) {
5554: $username = $args->{'user'};
5555: }
1.129 albertel 5556: $returnhash{'home'}= $chome;
5557: $returnhash{'domain'} = $cdomain;
5558: $returnhash{'num'} = $cnum;
1.741 raeburn 5559: if (!defined($returnhash{'type'})) {
5560: $returnhash{'type'} = 'Course';
5561: }
1.130 albertel 5562: while (my ($name,$value) = each %returnhash) {
1.53 www 5563: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 5564: }
1.270 www 5565: $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117 foxr 5566: $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118 foxr 5567: $username.'_'.$cdomain.'_'.$cnum;
1.60 www 5568: $envhash{'course.'.$normalid.'.home'}=$chome;
5569: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
5570: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 5571: }
5572: }
1.731 albertel 5573: if (!$args->{'one_time'}) {
1.949 raeburn 5574: &appenv(\%envhash);
1.731 albertel 5575: }
1.302 albertel 5576: return %returnhash;
1.461 www 5577: }
5578:
1.1080 raeburn 5579: sub update_released_required {
5580: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
5581: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
5582: $cid = $env{'request.course.id'};
5583: $cdom = $env{'course.'.$cid.'.domain'};
5584: $cnum = $env{'course.'.$cid.'.num'};
5585: $chome = $env{'course.'.$cid.'.home'};
5586: }
5587: if ($needsrelease) {
5588: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
5589: my $needsupdate;
5590: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
5591: $needsupdate = 1;
5592: } else {
5593: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
5594: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
5595: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
5596: $needsupdate = 1;
5597: }
5598: }
5599: if ($needsupdate) {
5600: my %needshash = (
5601: 'internal.releaserequired' => $needsrelease,
5602: );
5603: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
5604: if ($putresult eq 'ok') {
5605: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
5606: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
5607: if (ref($crsinfo{$cid}) eq 'HASH') {
5608: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
5609: &courseidput($cdom,\%crsinfo,$chome,'notime');
5610: }
5611: }
5612: }
5613: }
5614: return;
5615: }
5616:
1.461 www 5617: # -------------------------------------------------See if a user is privileged
5618:
5619: sub privileged {
1.1219 raeburn 5620: my ($username,$domain,$possdomains,$possroles)=@_;
1.1170 droeschl 5621: my $now = time;
1.1219 raeburn 5622: my $roles;
5623: if (ref($possroles) eq 'ARRAY') {
5624: $roles = $possroles;
5625: } else {
5626: $roles = ['dc','su'];
5627: }
5628: if (ref($possdomains) eq 'ARRAY') {
5629: my %privileged = &privileged_by_domain($possdomains,$roles);
5630: foreach my $dom (@{$possdomains}) {
5631: if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
5632: (ref($privileged{$dom}) eq 'HASH')) {
5633: foreach my $role (@{$roles}) {
5634: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5635: if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
5636: my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
5637: return 1 unless (($end && $end < $now) ||
5638: ($start && $start > $now));
5639: }
5640: }
5641: }
5642: }
5643: }
5644: } else {
5645: my %rolesdump = &dump("roles", $domain, $username) or return 0;
5646: my $now = time;
1.1170 droeschl 5647:
1.1275 musolffc 5648: for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170 droeschl 5649: my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219 raeburn 5650: if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170 droeschl 5651: return 1 unless ($tend && $tend < $now)
1.1219 raeburn 5652: or ($tstart && $tstart > $now);
1.1170 droeschl 5653: }
1.1219 raeburn 5654: }
5655: }
5656: return 0;
5657: }
1.1170 droeschl 5658:
1.1219 raeburn 5659: sub privileged_by_domain {
5660: my ($domains,$roles) = @_;
5661: my %privileged = ();
5662: my $cachetime = 60*60*24;
5663: my $now = time;
5664: unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
5665: return %privileged;
5666: }
5667: foreach my $dom (@{$domains}) {
5668: next if (ref($privileged{$dom}) eq 'HASH');
5669: my $needroles;
5670: foreach my $role (@{$roles}) {
5671: my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
5672: if (defined($cached)) {
5673: if (ref($result) eq 'HASH') {
5674: $privileged{$dom}{$role} = $result;
5675: }
5676: } else {
5677: $needroles = 1;
5678: }
5679: }
5680: if ($needroles) {
5681: my %dompersonnel = &get_domain_roles($dom,$roles);
5682: $privileged{$dom} = {};
5683: foreach my $server (keys(%dompersonnel)) {
5684: if (ref($dompersonnel{$server}) eq 'HASH') {
5685: foreach my $item (keys(%{$dompersonnel{$server}})) {
5686: my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
5687: my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
5688: next if ($end && $end < $now);
5689: $privileged{$dom}{$trole}{$uname.':'.$udom} =
5690: $dompersonnel{$server}{$item};
5691: }
5692: }
5693: }
5694: if (ref($privileged{$dom}) eq 'HASH') {
5695: foreach my $role (@{$roles}) {
5696: if (ref($privileged{$dom}{$role}) eq 'HASH') {
5697: &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
5698: } else {
5699: my %hash = ();
5700: &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
5701: }
5702: }
5703: }
5704: }
5705: }
5706: return %privileged;
1.9 www 5707: }
1.1 albertel 5708:
1.103 harris41 5709: # -------------------------------------------------------- Get user privileges
1.11 www 5710:
5711: sub rolesinit {
1.1169 droeschl 5712: my ($domain, $username) = @_;
5713: my %userroles = ('user.login.time' => time);
5714: my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
5715:
5716: # firstaccess and timerinterval are related to timed maps/resources.
5717: # also, blocking can be triggered by an activating timer
5718: # it's saved in the user's %env.
5719: my %firstaccess = &dump('firstaccesstimes', $domain, $username);
5720: my %timerinterval = &dump('timerinterval', $domain, $username);
5721: my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
5722: %timerintchk, %timerintenv);
5723:
1.1162 raeburn 5724: foreach my $key (keys(%firstaccess)) {
1.1169 droeschl 5725: my ($cid, $rest) = split(/\0/, $key);
1.1162 raeburn 5726: $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
5727: }
1.1169 droeschl 5728:
1.1162 raeburn 5729: foreach my $key (keys(%timerinterval)) {
5730: my ($cid,$rest) = split(/\0/,$key);
5731: $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
5732: }
1.1169 droeschl 5733:
1.11 www 5734: my %allroles=();
1.1162 raeburn 5735: my %allgroups=();
1.11 www 5736:
1.1274 raeburn 5737: for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169 droeschl 5738: my $role = $rolesdump{$area};
5739: $area =~ s/\_\w\w$//;
5740:
5741: my ($trole, $tend, $tstart, $group_privs);
5742:
5743: if ($role =~ /^cr/) {
5744: # Custom role, defined by a user
5745: # e.g., user.role.cr/msu/smith/mynewrole
5746: if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
5747: $trole = $1;
5748: ($tend, $tstart) = split('_', $2);
5749: } else {
5750: $trole = $role;
5751: }
5752: } elsif ($role =~ m|^gr/|) {
5753: # Role of member in a group, defined within a course/community
5754: # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
5755: ($trole, $tend, $tstart) = split(/_/, $role);
5756: next if $tstart eq '-1';
5757: ($trole, $group_privs) = split(/\//, $trole);
5758: $group_privs = &unescape($group_privs);
5759: } else {
5760: # Just a normal role, defined in roles.tab
5761: ($trole, $tend, $tstart) = split(/_/,$role);
5762: }
5763:
5764: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
5765: $username);
5766: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
5767:
5768: # role expired or not available yet?
5769: $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or
5770: ($tstart != 0 && $tstart > $userroles{'user.login.time'});
5771:
5772: next if $area eq '' or $trole eq '';
5773:
5774: my $spec = "$trole.$area";
5775: my ($tdummy, $tdomain, $trest) = split(/\//, $area);
5776:
5777: if ($trole =~ /^cr\//) {
5778: # Custom role, defined by a user
5779: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5780: } elsif ($trole eq 'gr') {
5781: # Role of a member in a group, defined within a course/community
5782: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
5783: next;
5784: } else {
5785: # Normal role, defined in roles.tab
5786: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5787: }
5788:
5789: my $cid = $tdomain.'_'.$trest;
5790: unless ($firstaccchk{$cid}) {
5791: if (ref($coursetimerstarts{$cid}) eq 'HASH') {
5792: foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
5793: $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} =
5794: $coursetimerstarts{$cid}{$item};
5795: }
5796: }
5797: $firstaccchk{$cid} = 1;
5798: }
5799: unless ($timerintchk{$cid}) {
5800: if (ref($coursetimerintervals{$cid}) eq 'HASH') {
5801: foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
5802: $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
5803: $coursetimerintervals{$cid}{$item};
1.1162 raeburn 5804: }
1.12 www 5805: }
1.1169 droeschl 5806: $timerintchk{$cid} = 1;
1.191 harris41 5807: }
1.11 www 5808: }
1.1169 droeschl 5809:
1.1341 raeburn 5810: @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
5811: \%allroles, \%allgroups);
1.1169 droeschl 5812: $env{'user.adv'} = $userroles{'user.adv'};
1.1341 raeburn 5813: $env{'user.rar'} = $userroles{'user.rar'};
1.1169 droeschl 5814:
1.1162 raeburn 5815: return (\%userroles,\%firstaccenv,\%timerintenv);
1.11 www 5816: }
5817:
1.567 raeburn 5818: sub set_arearole {
1.1215 raeburn 5819: my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
5820: unless ($nolog) {
1.567 raeburn 5821: # log the associated role with the area
1.1215 raeburn 5822: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
5823: }
1.743 albertel 5824: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 5825: }
5826:
5827: sub custom_roleprivs {
5828: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
5829: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250 raeburn 5830: my $homsvr = &homeserver($rauthor,$rdomain);
1.838 albertel 5831: if (&hostname($homsvr) ne '') {
1.567 raeburn 5832: my ($rdummy,$roledef)=
5833: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
5834: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
5835: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
5836: if (defined($syspriv)) {
1.1043 raeburn 5837: if ($trest =~ /^$match_community$/) {
5838: $syspriv =~ s/bre\&S//;
5839: }
1.567 raeburn 5840: $$allroles{'cm./'}.=':'.$syspriv;
5841: $$allroles{$spec.'./'}.=':'.$syspriv;
5842: }
5843: if ($tdomain ne '') {
5844: if (defined($dompriv)) {
5845: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
5846: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
5847: }
5848: if (($trest ne '') && (defined($coursepriv))) {
1.1332 raeburn 5849: if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
5850: my $rolename = $1;
5851: $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
5852: }
1.567 raeburn 5853: $$allroles{'cm.'.$area}.=':'.$coursepriv;
5854: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
5855: }
5856: }
5857: }
5858: }
5859: }
5860:
1.1332 raeburn 5861: sub course_adhocrole_privs {
5862: my ($rolename,$cdom,$cnum,$coursepriv) = @_;
5863: my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
5864: if ($overrides{"internal.adhocpriv.$rolename"}) {
5865: my (%currprivs,%storeprivs);
5866: foreach my $item (split(/:/,$coursepriv)) {
5867: my ($priv,$restrict) = split(/\&/,$item);
5868: $currprivs{$priv} = $restrict;
5869: }
5870: my (%possadd,%possremove,%full);
5871: foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
5872: my ($priv,$restrict)=split(/\&/,$item);
5873: $full{$priv} = $restrict;
5874: }
5875: foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
5876: next if ($item eq '');
5877: my ($rule,$rest) = split(/=/,$item);
5878: next unless (($rule eq 'off') || ($rule eq 'on'));
5879: foreach my $priv (split(/:/,$rest)) {
5880: if ($priv ne '') {
5881: if ($rule eq 'off') {
5882: $possremove{$priv} = 1;
5883: } else {
5884: $possadd{$priv} = 1;
5885: }
5886: }
5887: }
5888: }
5889: foreach my $priv (sort(keys(%full))) {
5890: if (exists($currprivs{$priv})) {
5891: unless (exists($possremove{$priv})) {
5892: $storeprivs{$priv} = $currprivs{$priv};
5893: }
5894: } elsif (exists($possadd{$priv})) {
5895: $storeprivs{$priv} = $full{$priv};
5896: }
5897: }
5898: $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
5899: }
5900: return $coursepriv;
5901: }
5902:
1.678 raeburn 5903: sub group_roleprivs {
5904: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
5905: my $access = 1;
5906: my $now = time;
5907: if (($tend!=0) && ($tend<$now)) { $access = 0; }
5908: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
5909: if ($access) {
1.811 albertel 5910: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 5911: $$allgroups{$course}{$group} .=':'.$group_privs;
5912: }
5913: }
1.567 raeburn 5914:
5915: sub standard_roleprivs {
5916: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
5917: if (defined($pr{$trole.':s'})) {
5918: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
5919: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
5920: }
5921: if ($tdomain ne '') {
5922: if (defined($pr{$trole.':d'})) {
5923: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
5924: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
5925: }
5926: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
5927: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
5928: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
5929: }
5930: }
5931: }
5932:
5933: sub set_userprivs {
1.1064 raeburn 5934: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 5935: my $author=0;
5936: my $adv=0;
1.1341 raeburn 5937: my $rar=0;
1.678 raeburn 5938: my %grouproles = ();
5939: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 5940: my @groupkeys;
1.1000 raeburn 5941: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 5942: push(@groupkeys,$role);
5943: }
5944: if (ref($groups_roles) eq 'HASH') {
5945: foreach my $key (keys(%{$groups_roles})) {
5946: unless (grep(/^\Q$key\E$/,@groupkeys)) {
5947: push(@groupkeys,$key);
5948: }
5949: }
5950: }
5951: if (@groupkeys > 0) {
5952: foreach my $role (@groupkeys) {
5953: my ($trole,$area,$sec,$extendedarea);
5954: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
5955: $trole = $1;
5956: $area = $2;
5957: $sec = $3;
5958: $extendedarea = $area.$sec;
5959: if (exists($$allgroups{$area})) {
5960: foreach my $group (keys(%{$$allgroups{$area}})) {
5961: my $spec = $trole.'.'.$extendedarea;
5962: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 5963: $$allgroups{$area}{$group};
1.1064 raeburn 5964: }
1.678 raeburn 5965: }
5966: }
5967: }
5968: }
5969: }
1.800 albertel 5970: foreach my $group (keys(%grouproles)) {
5971: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 5972: }
1.800 albertel 5973: foreach my $role (keys(%{$allroles})) {
5974: my %thesepriv;
1.941 raeburn 5975: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 5976: foreach my $item (split(/:/,$$allroles{$role})) {
5977: if ($item ne '') {
5978: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 5979: if ($restrictions eq '') {
5980: $thesepriv{$privilege}='F';
5981: } elsif ($thesepriv{$privilege} ne 'F') {
5982: $thesepriv{$privilege}.=$restrictions;
5983: }
5984: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341 raeburn 5985: if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567 raeburn 5986: }
5987: }
5988: my $thesestr='';
1.1104 raeburn 5989: foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 5990: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
5991: }
5992: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 5993: }
1.1341 raeburn 5994: return ($author,$adv,$rar);
1.567 raeburn 5995: }
5996:
1.994 raeburn 5997: sub role_status {
1.1104 raeburn 5998: my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 5999: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250 raeburn 6000: my ($one,$two) = split(m{\./},$rolekey,2);
6001: (undef,undef,$$role) = split(/\./,$one,3);
1.994 raeburn 6002: unless (!defined($$role) || $$role eq '') {
1.1251 raeburn 6003: $$where = '/'.$two;
1.994 raeburn 6004: $$trolecode=$$role.'.'.$$where;
6005: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
6006: $$tstatus='is';
1.1104 raeburn 6007: if ($$tstart && $$tstart>$update) {
1.994 raeburn 6008: $$tstatus='future';
1.1034 raeburn 6009: if ($$tstart<$now) {
6010: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 6011: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 6012: my (%allroles,%allgroups,$group_privs,
6013: %groups_roles,@rolecodes);
1.1002 raeburn 6014: my %userroles = (
6015: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
6016: );
1.1064 raeburn 6017: @rolecodes = ('cm');
1.1002 raeburn 6018: my $spec=$$role.'.'.$$where;
6019: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
6020: if ($$role =~ /^cr\//) {
6021: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 6022: push(@rolecodes,'cr');
1.1002 raeburn 6023: } elsif ($$role eq 'gr') {
1.1064 raeburn 6024: push(@rolecodes,$$role);
1.1002 raeburn 6025: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
6026: $env{'user.name'});
1.1064 raeburn 6027: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 6028: (undef,my $group_privs) = split(/\//,$trole);
6029: $group_privs = &unescape($group_privs);
6030: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 6031: 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 6032: &get_groups_roles($tdomain,$trest,
6033: \%course_roles,\@rolecodes,
6034: \%groups_roles);
1.1002 raeburn 6035: } else {
1.1064 raeburn 6036: push(@rolecodes,$$role);
1.1002 raeburn 6037: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
6038: }
1.1341 raeburn 6039: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
6040: \%groups_roles);
1.1064 raeburn 6041: &appenv(\%userroles,\@rolecodes);
1.1342 raeburn 6042: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002 raeburn 6043: }
6044: }
1.1034 raeburn 6045: $$tstatus = 'is';
1.1002 raeburn 6046: }
1.994 raeburn 6047: }
6048: if ($$tend) {
1.1104 raeburn 6049: if ($$tend<$update) {
1.994 raeburn 6050: $$tstatus='expired';
6051: } elsif ($$tend<$now) {
6052: $$tstatus='will_not';
6053: }
6054: }
6055: }
6056: }
6057: }
6058:
1.1104 raeburn 6059: sub get_groups_roles {
6060: my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
6061: return unless((ref($cdom_courseroles) eq 'HASH') &&
6062: (ref($rolecodes) eq 'ARRAY') &&
6063: (ref($groups_roles) eq 'HASH'));
6064: if (keys(%{$cdom_courseroles}) > 0) {
6065: my ($cnum) = ($rest =~ /^($match_courseid)/);
6066: if ($cdom ne '' && $cnum ne '') {
6067: foreach my $key (keys(%{$cdom_courseroles})) {
6068: if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
6069: my $crsrole = $1;
6070: my $crssec = $2;
6071: if ($crsrole =~ /^cr/) {
6072: unless (grep(/^cr$/,@{$rolecodes})) {
6073: push(@{$rolecodes},'cr');
6074: }
6075: } else {
6076: unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
6077: push(@{$rolecodes},$crsrole);
6078: }
6079: }
6080: my $rolekey = "$crsrole./$cdom/$cnum";
6081: if ($crssec ne '') {
6082: $rolekey .= "/$crssec";
6083: }
6084: $rolekey .= './';
6085: $groups_roles->{$rolekey} = $rolecodes;
6086: }
6087: }
6088: }
6089: }
6090: return;
6091: }
6092:
6093: sub delete_env_groupprivs {
6094: my ($where,$courseroles,$possroles) = @_;
6095: return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
6096: my ($dummy,$udom,$uname,$group) = split(/\//,$where);
6097: unless (ref($courseroles->{$udom}) eq 'HASH') {
6098: %{$courseroles->{$udom}} =
6099: &get_my_roles('','','userroles',['active'],
6100: $possroles,[$udom],1);
6101: }
6102: if (ref($courseroles->{$udom}) eq 'HASH') {
6103: foreach my $item (keys(%{$courseroles->{$udom}})) {
6104: my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
6105: my $area = '/'.$cdom.'/'.$cnum;
6106: my $privkey = "user.priv.$crsrole.$area";
6107: if ($crssec ne '') {
6108: $privkey .= '/'.$crssec;
6109: }
6110: $privkey .= ".$area/$group";
6111: &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
6112: }
6113: }
6114: return;
6115: }
6116:
1.994 raeburn 6117: sub check_adhoc_privs {
1.1329 raeburn 6118: my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994 raeburn 6119: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329 raeburn 6120: if ($sec) {
6121: $cckey .= '/'.$sec;
6122: }
1.1185 raeburn 6123: my $setprivs;
1.994 raeburn 6124: if ($env{$cckey}) {
6125: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104 raeburn 6126: &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 6127: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329 raeburn 6128: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185 raeburn 6129: $setprivs = 1;
1.994 raeburn 6130: }
6131: } else {
1.1330 raeburn 6132: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185 raeburn 6133: $setprivs = 1;
1.994 raeburn 6134: }
1.1185 raeburn 6135: return $setprivs;
1.994 raeburn 6136: }
6137:
6138: sub set_adhoc_privileges {
1.1326 raeburn 6139: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329 raeburn 6140: my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994 raeburn 6141: my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329 raeburn 6142: if ($sec ne '') {
6143: $area .= '/'.$sec;
6144: }
1.994 raeburn 6145: my $spec = $role.'.'.$area;
6146: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215 raeburn 6147: $env{'user.name'},1);
1.1326 raeburn 6148: my %rolehash = ();
1.1332 raeburn 6149: if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
6150: my $rolename = $1;
1.1326 raeburn 6151: &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332 raeburn 6152: my %domdef = &get_domain_defaults($dcdom);
6153: if (ref($domdef{'adhocroles'}) eq 'HASH') {
6154: if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
6155: &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
6156: }
6157: }
1.1326 raeburn 6158: } else {
6159: &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
6160: }
1.1341 raeburn 6161: my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994 raeburn 6162: &appenv(\%userroles,[$role,'cm']);
1.1342 raeburn 6163: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1088 raeburn 6164: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
6165: &appenv( {'request.role' => $spec,
6166: 'request.role.domain' => $dcdom,
1.1332 raeburn 6167: 'request.course.sec' => $sec,
1.1088 raeburn 6168: }
6169: );
6170: my $tadv=0;
6171: if (&allowed('adv') eq 'F') { $tadv=1; }
6172: &appenv({'request.role.adv' => $tadv});
6173: }
1.994 raeburn 6174: }
6175:
1.12 www 6176: # --------------------------------------------------------------- get interface
6177:
6178: sub get {
1.131 albertel 6179: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 6180: my $items='';
1.800 albertel 6181: foreach my $item (@$storearr) {
6182: $items.=&escape($item).'&';
1.191 harris41 6183: }
1.12 www 6184: $items=~s/\&$//;
1.620 albertel 6185: if (!$udomain) { $udomain=$env{'user.domain'}; }
6186: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 6187: my $uhome=&homeserver($uname,$udomain);
6188:
1.133 albertel 6189: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 6190: my @pairs=split(/\&/,$rep);
1.273 albertel 6191: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
6192: return @pairs;
6193: }
1.15 www 6194: my %returnhash=();
1.42 www 6195: my $i=0;
1.800 albertel 6196: foreach my $item (@$storearr) {
6197: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 6198: $i++;
1.191 harris41 6199: }
1.15 www 6200: return %returnhash;
1.27 www 6201: }
6202:
6203: # --------------------------------------------------------------- del interface
6204:
6205: sub del {
1.133 albertel 6206: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 6207: my $items='';
1.800 albertel 6208: foreach my $item (@$storearr) {
6209: $items.=&escape($item).'&';
1.191 harris41 6210: }
1.984 neumanie 6211:
1.27 www 6212: $items=~s/\&$//;
1.620 albertel 6213: if (!$udomain) { $udomain=$env{'user.domain'}; }
6214: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 6215: my $uhome=&homeserver($uname,$udomain);
6216: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 6217: }
6218:
6219: # -------------------------------------------------------------- dump interface
6220:
1.1180 droeschl 6221: sub unserialize {
6222: my ($rep, $escapedkeys) = @_;
6223:
6224: return {} if $rep =~ /^error/;
6225:
6226: my %returnhash=();
1.1252 raeburn 6227: foreach my $item (split(/\&/,$rep)) {
1.1180 droeschl 6228: my ($key, $value) = split(/=/, $item, 2);
6229: $key = unescape($key) unless $escapedkeys;
6230: next if $key =~ /^error: 2 /;
1.1252 raeburn 6231: $returnhash{$key} = &thaw_unescape($value);
1.1180 droeschl 6232: }
6233: #return %returnhash;
6234: return \%returnhash;
6235: }
6236:
6237: # see Lond::dump_with_regexp
6238: # if $escapedkeys hash keys won't get unescaped.
1.15 www 6239: sub dump {
1.1180 droeschl 6240: my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755 albertel 6241: if (!$udomain) { $udomain=$env{'user.domain'}; }
6242: if (!$uname) { $uname=$env{'user.name'}; }
6243: my $uhome=&homeserver($uname,$udomain);
1.1167 droeschl 6244:
1.1266 raeburn 6245: if ($regexp) {
6246: $regexp=&escape($regexp);
6247: } else {
6248: $regexp='.';
6249: }
1.1180 droeschl 6250: if (grep { $_ eq $uhome } current_machine_ids()) {
6251: # user is hosted on this machine
1.1266 raeburn 6252: my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226 raeburn 6253: $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180 droeschl 6254: return %{unserialize($reply, $escapedkeys)};
6255: }
1.1166 raeburn 6256: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755 albertel 6257: my @pairs=split(/\&/,$rep);
6258: my %returnhash=();
1.1098 foxr 6259: if (!($rep =~ /^error/ )) {
6260: foreach my $item (@pairs) {
6261: my ($key,$value)=split(/=/,$item,2);
1.1180 droeschl 6262: $key = unescape($key) unless $escapedkeys;
6263: #$key = &unescape($key);
1.1098 foxr 6264: next if ($key =~ /^error: 2 /);
6265: $returnhash{$key}=&thaw_unescape($value);
6266: }
1.755 albertel 6267: }
6268: return %returnhash;
1.407 www 6269: }
6270:
1.1098 foxr 6271:
1.717 albertel 6272: # --------------------------------------------------------- dumpstore interface
6273:
6274: sub dumpstore {
6275: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180 droeschl 6276: # same as dump but keys must be escaped. They may contain colon separated
6277: # lists of values that may themself contain colons (e.g. symbs).
6278: return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717 albertel 6279: }
6280:
1.407 www 6281: # -------------------------------------------------------------- keys interface
6282:
6283: sub getkeys {
6284: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 6285: if (!$udomain) { $udomain=$env{'user.domain'}; }
6286: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 6287: my $uhome=&homeserver($uname,$udomain);
6288: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
6289: my @keyarray=();
1.800 albertel 6290: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 6291: next if ($key =~ /^error: 2 /);
1.800 albertel 6292: push(@keyarray,&unescape($key));
1.407 www 6293: }
6294: return @keyarray;
1.318 matthew 6295: }
6296:
1.319 matthew 6297: # --------------------------------------------------------------- currentdump
6298: sub currentdump {
1.328 matthew 6299: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 6300: $courseid = $env{'request.course.id'} if (! defined($courseid));
6301: $sdom = $env{'user.domain'} if (! defined($sdom));
6302: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 6303: my $uhome = &homeserver($sname,$sdom);
1.1180 droeschl 6304: my $rep;
6305:
6306: if (grep { $_ eq $uhome } current_machine_ids()) {
6307: $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname,
6308: $courseid)));
6309: } else {
6310: $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
6311: }
6312:
1.318 matthew 6313: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 6314: #
1.318 matthew 6315: my %returnhash=();
1.319 matthew 6316: #
1.1343 ! raeburn 6317: if ($rep eq 'unknown_cmd') {
1.319 matthew 6318: # an old lond will not know currentdump
6319: # Do a dump and make it look like a currentdump
1.822 albertel 6320: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 6321: return if ($tmp[0] =~ /^(error:|no_such_host)/);
6322: my %hash = @tmp;
6323: @tmp=();
1.424 matthew 6324: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 6325: } else {
6326: my @pairs=split(/\&/,$rep);
1.800 albertel 6327: foreach my $pair (@pairs) {
6328: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 6329: my ($symb,$param) = split(/:/,$key);
6330: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 6331: &thaw_unescape($value);
1.319 matthew 6332: }
1.191 harris41 6333: }
1.12 www 6334: return %returnhash;
1.424 matthew 6335: }
6336:
6337: sub convert_dump_to_currentdump{
6338: my %hash = %{shift()};
6339: my %returnhash;
6340: # Code ripped from lond, essentially. The only difference
6341: # here is the unescaping done by lonnet::dump(). Conceivably
6342: # we might run in to problems with parameter names =~ /^v\./
6343: while (my ($key,$value) = each(%hash)) {
6344: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 6345: $symb = &unescape($symb);
6346: $param = &unescape($param);
1.424 matthew 6347: next if ($v eq 'version' || $symb eq 'keys');
6348: next if (exists($returnhash{$symb}) &&
6349: exists($returnhash{$symb}->{$param}) &&
6350: $returnhash{$symb}->{'v.'.$param} > $v);
6351: $returnhash{$symb}->{$param}=$value;
6352: $returnhash{$symb}->{'v.'.$param}=$v;
6353: }
6354: #
6355: # Remove all of the keys in the hashes which keep track of
6356: # the version of the parameter.
6357: while (my ($symb,$param_hash) = each(%returnhash)) {
6358: # use a foreach because we are going to delete from the hash.
6359: foreach my $key (keys(%$param_hash)) {
6360: delete($param_hash->{$key}) if ($key =~ /^v\./);
6361: }
6362: }
6363: return \%returnhash;
1.12 www 6364: }
6365:
1.627 albertel 6366: # ------------------------------------------------------ critical inc interface
6367:
6368: sub cinc {
6369: return &inc(@_,'critical');
6370: }
6371:
1.449 matthew 6372: # --------------------------------------------------------------- inc interface
6373:
6374: sub inc {
1.627 albertel 6375: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 6376: if (!$udomain) { $udomain=$env{'user.domain'}; }
6377: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 6378: my $uhome=&homeserver($uname,$udomain);
6379: my $items='';
6380: if (! ref($store)) {
6381: # got a single value, so use that instead
6382: $items = &escape($store).'=&';
6383: } elsif (ref($store) eq 'SCALAR') {
6384: $items = &escape($$store).'=&';
6385: } elsif (ref($store) eq 'ARRAY') {
6386: $items = join('=&',map {&escape($_);} @{$store});
6387: } elsif (ref($store) eq 'HASH') {
6388: while (my($key,$value) = each(%{$store})) {
6389: $items.= &escape($key).'='.&escape($value).'&';
6390: }
6391: }
6392: $items=~s/\&$//;
1.627 albertel 6393: if ($critical) {
6394: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
6395: } else {
6396: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
6397: }
1.449 matthew 6398: }
6399:
1.12 www 6400: # --------------------------------------------------------------- put interface
6401:
6402: sub put {
1.134 albertel 6403: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 6404: if (!$udomain) { $udomain=$env{'user.domain'}; }
6405: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 6406: my $uhome=&homeserver($uname,$udomain);
1.12 www 6407: my $items='';
1.800 albertel 6408: foreach my $item (keys(%$storehash)) {
6409: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 6410: }
1.12 www 6411: $items=~s/\&$//;
1.134 albertel 6412: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 6413: }
6414:
1.631 albertel 6415: # ------------------------------------------------------------ newput interface
6416:
6417: sub newput {
6418: my ($namespace,$storehash,$udomain,$uname)=@_;
6419: if (!$udomain) { $udomain=$env{'user.domain'}; }
6420: if (!$uname) { $uname=$env{'user.name'}; }
6421: my $uhome=&homeserver($uname,$udomain);
6422: my $items='';
6423: foreach my $key (keys(%$storehash)) {
6424: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
6425: }
6426: $items=~s/\&$//;
6427: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
6428: }
6429:
6430: # --------------------------------------------------------- putstore interface
6431:
1.524 raeburn 6432: sub putstore {
1.1269 raeburn 6433: my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620 albertel 6434: if (!$udomain) { $udomain=$env{'user.domain'}; }
6435: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 6436: my $uhome=&homeserver($uname,$udomain);
6437: my $items='';
1.715 albertel 6438: foreach my $key (keys(%$storehash)) {
6439: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 6440: }
1.715 albertel 6441: $items=~s/\&$//;
1.716 albertel 6442: my $esc_symb=&escape($symb);
6443: my $esc_v=&escape($version);
1.715 albertel 6444: my $reply =
1.716 albertel 6445: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 6446: $uhome);
1.1269 raeburn 6447: if (($tolog) && ($reply eq 'ok')) {
6448: my $namevalue='';
6449: foreach my $key (keys(%{$storehash})) {
6450: $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
6451: }
6452: $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
6453: '&host='.&escape($perlvar{'lonHostID'}).
6454: '&version='.$esc_v.
6455: '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
6456: &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
6457: }
1.715 albertel 6458: if ($reply eq 'unknown_cmd') {
1.716 albertel 6459: # gfall back to way things use to be done
1.715 albertel 6460: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
6461: $uname);
1.524 raeburn 6462: }
1.715 albertel 6463: return $reply;
6464: }
6465:
6466: sub old_putstore {
1.716 albertel 6467: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
6468: if (!$udomain) { $udomain=$env{'user.domain'}; }
6469: if (!$uname) { $uname=$env{'user.name'}; }
6470: my $uhome=&homeserver($uname,$udomain);
6471: my %newstorehash;
1.800 albertel 6472: foreach my $item (keys(%$storehash)) {
6473: my $key = $version.':'.&escape($symb).':'.$item;
6474: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 6475: }
6476: my $items='';
6477: my %allitems = ();
1.800 albertel 6478: foreach my $item (keys(%newstorehash)) {
6479: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 6480: my $key = $1.':keys:'.$2;
6481: $allitems{$key} .= $3.':';
6482: }
1.800 albertel 6483: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 6484: }
1.800 albertel 6485: foreach my $item (keys(%allitems)) {
6486: $allitems{$item} =~ s/\:$//;
6487: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 6488: }
6489: $items=~s/\&$//;
6490: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 6491: }
6492:
1.47 www 6493: # ------------------------------------------------------ critical put interface
6494:
6495: sub cput {
1.134 albertel 6496: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 6497: if (!$udomain) { $udomain=$env{'user.domain'}; }
6498: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 6499: my $uhome=&homeserver($uname,$udomain);
1.47 www 6500: my $items='';
1.800 albertel 6501: foreach my $item (keys(%$storehash)) {
6502: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 6503: }
1.47 www 6504: $items=~s/\&$//;
1.134 albertel 6505: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 6506: }
6507:
6508: # -------------------------------------------------------------- eget interface
6509:
6510: sub eget {
1.133 albertel 6511: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 6512: my $items='';
1.800 albertel 6513: foreach my $item (@$storearr) {
6514: $items.=&escape($item).'&';
1.191 harris41 6515: }
1.12 www 6516: $items=~s/\&$//;
1.620 albertel 6517: if (!$udomain) { $udomain=$env{'user.domain'}; }
6518: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 6519: my $uhome=&homeserver($uname,$udomain);
6520: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 6521: my @pairs=split(/\&/,$rep);
6522: my %returnhash=();
1.42 www 6523: my $i=0;
1.800 albertel 6524: foreach my $item (@$storearr) {
6525: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 6526: $i++;
1.191 harris41 6527: }
1.12 www 6528: return %returnhash;
6529: }
6530:
1.667 albertel 6531: # ------------------------------------------------------------ tmpput interface
6532: sub tmpput {
1.802 raeburn 6533: my ($storehash,$server,$context)=@_;
1.667 albertel 6534: my $items='';
1.800 albertel 6535: foreach my $item (keys(%$storehash)) {
6536: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 6537: }
6538: $items=~s/\&$//;
1.802 raeburn 6539: if (defined($context)) {
6540: $items .= ':'.&escape($context);
6541: }
1.667 albertel 6542: return &reply("tmpput:$items",$server);
6543: }
6544:
6545: # ------------------------------------------------------------ tmpget interface
6546: sub tmpget {
1.688 albertel 6547: my ($token,$server)=@_;
6548: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6549: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 6550: my %returnhash;
1.1328 raeburn 6551: if ($rep =~ /^(con_lost|error|no_such_host)/i) {
6552: return %returnhash;
6553: }
1.667 albertel 6554: foreach my $item (split(/\&/,$rep)) {
6555: my ($key,$value)=split(/=/,$item);
6556: $returnhash{&unescape($key)}=&thaw_unescape($value);
6557: }
6558: return %returnhash;
6559: }
6560:
1.1113 raeburn 6561: # ------------------------------------------------------------ tmpdel interface
1.688 albertel 6562: sub tmpdel {
6563: my ($token,$server)=@_;
6564: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
6565: return &reply("tmpdel:$token",$server);
6566: }
6567:
1.1198 raeburn 6568: # ------------------------------------------------------------ get_timebased_id
6569:
6570: sub get_timebased_id {
6571: my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
6572: $maxtries) = @_;
6573: my ($newid,$error,$dellock);
6574: unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {
6575: return ('','ok','invalid call to get suffix');
6576: }
6577:
6578: # set defaults for any optional args for which values were not supplied
6579: if ($who eq '') {
6580: $who = $env{'user.name'}.':'.$env{'user.domain'};
6581: }
6582: if (!$locktries) {
6583: $locktries = 3;
6584: }
6585: if (!$maxtries) {
6586: $maxtries = 10;
6587: }
6588:
6589: if (($cdom eq '') || ($cnum eq '')) {
6590: if ($env{'request.course.id'}) {
6591: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
6592: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
6593: }
6594: if (($cdom eq '') || ($cnum eq '')) {
6595: return ('','ok','call to get suffix not in course context');
6596: }
6597: }
6598:
6599: # construct locking item
6600: my $lockhash = {
6601: $prefix."\0".'locked_'.$keyid => $who,
6602: };
6603: my $tries = 0;
6604:
6605: # attempt to get lock on nohist_$namespace file
6606: my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6607: while (($gotlock ne 'ok') && $tries <$locktries) {
6608: $tries ++;
6609: sleep 1;
6610: $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
6611: }
6612:
6613: # attempt to get unique identifier, based on current timestamp
6614: if ($gotlock eq 'ok') {
6615: my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
6616: my $id = time;
6617: $newid = $id;
1.1268 raeburn 6618: if ($idtype eq 'addcode') {
6619: $newid .= &sixnum_code();
6620: }
1.1198 raeburn 6621: my $idtries = 0;
6622: while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
6623: if ($idtype eq 'concat') {
6624: $newid = $id.$idtries;
1.1268 raeburn 6625: } elsif ($idtype eq 'addcode') {
6626: $newid = $newid.&sixnum_code();
1.1198 raeburn 6627: } else {
6628: $newid ++;
6629: }
6630: $idtries ++;
6631: }
6632: if (!exists($inuse{$prefix."\0".$newid})) {
6633: my %new_item = (
6634: $prefix."\0".$newid => $who,
6635: );
6636: my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
6637: $cdom,$cnum);
6638: if ($putresult ne 'ok') {
6639: undef($newid);
6640: $error = 'error saving new item: '.$putresult;
6641: }
6642: } else {
1.1268 raeburn 6643: undef($newid);
1.1198 raeburn 6644: $error = ('error: no unique suffix available for the new item ');
6645: }
6646: # remove lock
6647: my @del_lock = ($prefix."\0".'locked_'.$keyid);
6648: $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
6649: } else {
6650: $error = "error: could not obtain lockfile\n";
6651: $dellock = 'ok';
1.1276 raeburn 6652: if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
6653: $dellock = 'nolock';
6654: }
1.1198 raeburn 6655: }
6656: return ($newid,$dellock,$error);
6657: }
6658:
1.1268 raeburn 6659: sub sixnum_code {
6660: my $code;
6661: for (0..6) {
6662: $code .= int( rand(9) );
6663: }
6664: return $code;
6665: }
6666:
1.765 albertel 6667: # -------------------------------------------------- portfolio access checking
6668:
6669: sub portfolio_access {
1.1270 raeburn 6670: my ($requrl,$clientip) = @_;
1.765 albertel 6671: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270 raeburn 6672: my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814 raeburn 6673: if ($result) {
6674: my %setters;
6675: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6676: my ($startblock,$endblock) =
6677: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
6678: if ($startblock && $endblock) {
6679: return 'B';
6680: }
6681: } else {
6682: my ($startblock,$endblock) =
6683: &Apache::loncommon::blockcheck(\%setters,'port');
6684: if ($startblock && $endblock) {
6685: return 'B';
6686: }
6687: }
6688: }
1.765 albertel 6689: if ($result eq 'ok') {
1.766 albertel 6690: return 'F';
1.765 albertel 6691: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 6692: return 'A';
1.765 albertel 6693: }
1.766 albertel 6694: return '';
1.765 albertel 6695: }
6696:
6697: sub get_portfolio_access {
1.1270 raeburn 6698: my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767 albertel 6699:
6700: if (!ref($access_hash)) {
6701: my $current_perms = &get_portfile_permissions($udom,$unum);
6702: my %access_controls = &get_access_controls($current_perms,$group,
6703: $file_name);
6704: $access_hash = $access_controls{$file_name};
6705: }
6706:
1.1270 raeburn 6707: my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765 albertel 6708: my $now = time;
6709: if (ref($access_hash) eq 'HASH') {
6710: foreach my $key (keys(%{$access_hash})) {
6711: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
6712: if ($start > $now) {
6713: next;
6714: }
6715: if ($end && $end<$now) {
6716: next;
6717: }
6718: if ($scope eq 'public') {
6719: $public = $key;
6720: last;
6721: } elsif ($scope eq 'guest') {
6722: $guest = $key;
6723: } elsif ($scope eq 'domains') {
6724: push(@domains,$key);
6725: } elsif ($scope eq 'users') {
6726: push(@users,$key);
6727: } elsif ($scope eq 'course') {
6728: push(@courses,$key);
6729: } elsif ($scope eq 'group') {
6730: push(@groups,$key);
1.1270 raeburn 6731: } elsif ($scope eq 'ip') {
6732: push(@ips,$key);
1.765 albertel 6733: }
6734: }
6735: if ($public) {
6736: return 'ok';
1.1270 raeburn 6737: } elsif (@ips > 0) {
6738: my $allowed;
6739: foreach my $ipkey (@ips) {
6740: if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
6741: if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
6742: $allowed = 1;
6743: last;
6744: }
6745: }
6746: }
6747: if ($allowed) {
6748: return 'ok';
6749: }
1.765 albertel 6750: }
6751: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
6752: if ($guest) {
6753: return $guest;
6754: }
6755: } else {
6756: if (@domains > 0) {
6757: foreach my $domkey (@domains) {
6758: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
6759: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
6760: return 'ok';
6761: }
6762: }
6763: }
6764: }
6765: if (@users > 0) {
6766: foreach my $userkey (@users) {
1.865 raeburn 6767: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
6768: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
6769: if (ref($item) eq 'HASH') {
6770: if (($item->{'uname'} eq $env{'user.name'}) &&
6771: ($item->{'udom'} eq $env{'user.domain'})) {
6772: return 'ok';
6773: }
6774: }
6775: }
6776: }
1.765 albertel 6777: }
6778: }
6779: my %roleshash;
6780: my @courses_and_groups = @courses;
6781: push(@courses_and_groups,@groups);
6782: if (@courses_and_groups > 0) {
6783: my (%allgroups,%allroles);
6784: my ($start,$end,$role,$sec,$group);
6785: foreach my $envkey (%env) {
1.1060 raeburn 6786: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 6787: my $cid = $2.'_'.$3;
6788: if ($1 eq 'gr') {
6789: $group = $4;
6790: $allgroups{$cid}{$group} = $env{$envkey};
6791: } else {
6792: if ($4 eq '') {
6793: $sec = 'none';
6794: } else {
6795: $sec = $4;
6796: }
6797: $allroles{$cid}{$1}{$sec} = $env{$envkey};
6798: }
1.811 albertel 6799: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 6800: my $cid = $2.'_'.$3;
6801: if ($4 eq '') {
6802: $sec = 'none';
6803: } else {
6804: $sec = $4;
6805: }
6806: $allroles{$cid}{$1}{$sec} = $env{$envkey};
6807: }
6808: }
6809: if (keys(%allroles) == 0) {
6810: return;
6811: }
6812: foreach my $key (@courses_and_groups) {
6813: my %content = %{$$access_hash{$key}};
6814: my $cnum = $content{'number'};
6815: my $cdom = $content{'domain'};
6816: my $cid = $cdom.'_'.$cnum;
6817: if (!exists($allroles{$cid})) {
6818: next;
6819: }
6820: foreach my $role_id (keys(%{$content{'roles'}})) {
6821: my @sections = @{$content{'roles'}{$role_id}{'section'}};
6822: my @groups = @{$content{'roles'}{$role_id}{'group'}};
6823: my @status = @{$content{'roles'}{$role_id}{'access'}};
6824: my @roles = @{$content{'roles'}{$role_id}{'role'}};
6825: foreach my $role (keys(%{$allroles{$cid}})) {
6826: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
6827: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
6828: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
6829: if (grep/^all$/,@sections) {
6830: return 'ok';
6831: } else {
6832: if (grep/^$sec$/,@sections) {
6833: return 'ok';
6834: }
6835: }
6836: }
6837: }
6838: if (keys(%{$allgroups{$cid}}) == 0) {
6839: if (grep/^none$/,@groups) {
6840: return 'ok';
6841: }
6842: } else {
6843: if (grep/^all$/,@groups) {
6844: return 'ok';
6845: }
6846: foreach my $group (keys(%{$allgroups{$cid}})) {
6847: if (grep/^$group$/,@groups) {
6848: return 'ok';
6849: }
6850: }
6851: }
6852: }
6853: }
6854: }
6855: }
6856: }
6857: if ($guest) {
6858: return $guest;
6859: }
6860: }
6861: }
6862: return;
6863: }
6864:
6865: sub course_group_datechecker {
6866: my ($dates,$now,$status) = @_;
6867: my ($start,$end) = split(/\./,$dates);
6868: if (!$start && !$end) {
6869: return 'ok';
6870: }
6871: if (grep/^active$/,@{$status}) {
6872: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
6873: return 'ok';
6874: }
6875: }
6876: if (grep/^previous$/,@{$status}) {
6877: if ($end > $now ) {
6878: return 'ok';
6879: }
6880: }
6881: if (grep/^future$/,@{$status}) {
6882: if ($start > $now) {
6883: return 'ok';
6884: }
6885: }
6886: return;
6887: }
6888:
6889: sub parse_portfolio_url {
6890: my ($url) = @_;
6891:
6892: my ($type,$udom,$unum,$group,$file_name);
6893:
1.823 albertel 6894: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 6895: $type = 1;
6896: $udom = $1;
6897: $unum = $2;
6898: $file_name = $3;
1.823 albertel 6899: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 6900: $type = 2;
6901: $udom = $1;
6902: $unum = $2;
6903: $group = $3;
6904: $file_name = $3.'/'.$4;
6905: }
6906: if (wantarray) {
6907: return ($type,$udom,$unum,$file_name,$group);
6908: }
6909: return $type;
6910: }
6911:
6912: sub is_portfolio_url {
6913: my ($url) = @_;
6914: return scalar(&parse_portfolio_url($url));
6915: }
6916:
1.798 raeburn 6917: sub is_portfolio_file {
6918: my ($file) = @_;
1.820 raeburn 6919: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 6920: return 1;
6921: }
6922: return;
6923: }
6924:
1.976 raeburn 6925: sub usertools_access {
1.1084 raeburn 6926: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985 raeburn 6927: my ($access,%tools);
6928: if ($context eq '') {
6929: $context = 'tools';
6930: }
6931: if ($context eq 'requestcourses') {
6932: %tools = (
6933: official => 1,
6934: unofficial => 1,
1.1006 raeburn 6935: community => 1,
1.1246 raeburn 6936: textbook => 1,
1.1305 raeburn 6937: placement => 1,
1.985 raeburn 6938: );
1.1183 raeburn 6939: } elsif ($context eq 'requestauthor') {
6940: %tools = (
6941: requestauthor => 1,
6942: );
1.985 raeburn 6943: } else {
6944: %tools = (
6945: aboutme => 1,
6946: blog => 1,
1.1177 raeburn 6947: webdav => 1,
1.985 raeburn 6948: portfolio => 1,
6949: );
6950: }
1.976 raeburn 6951: return if (!defined($tools{$tool}));
6952:
1.1242 raeburn 6953: if (($udom eq '') || ($uname eq '')) {
1.976 raeburn 6954: $udom = $env{'user.domain'};
6955: $uname = $env{'user.name'};
6956: }
6957:
1.978 raeburn 6958: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
6959: if ($action ne 'reload') {
1.985 raeburn 6960: if ($context eq 'requestcourses') {
6961: return $env{'environment.canrequest.'.$tool};
1.1183 raeburn 6962: } elsif ($context eq 'requestauthor') {
6963: return $env{'environment.canrequest.author'};
1.985 raeburn 6964: } else {
6965: return $env{'environment.availabletools.'.$tool};
6966: }
6967: }
1.976 raeburn 6968: }
6969:
1.1183 raeburn 6970: my ($toolstatus,$inststatus,$envkey);
6971: if ($context eq 'requestauthor') {
6972: $envkey = $context;
6973: } else {
6974: $envkey = $context.'.'.$tool;
6975: }
1.976 raeburn 6976:
1.985 raeburn 6977: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
6978: ($action ne 'reload')) {
1.1183 raeburn 6979: $toolstatus = $env{'environment.'.$envkey};
1.976 raeburn 6980: $inststatus = $env{'environment.inststatus'};
6981: } else {
1.1084 raeburn 6982: if (ref($userenvref) eq 'HASH') {
1.1183 raeburn 6983: $toolstatus = $userenvref->{$envkey};
1.1084 raeburn 6984: $inststatus = $userenvref->{'inststatus'};
6985: } else {
1.1183 raeburn 6986: my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
6987: $toolstatus = $userenv{$envkey};
1.1084 raeburn 6988: $inststatus = $userenv{'inststatus'};
6989: }
1.976 raeburn 6990: }
6991:
6992: if ($toolstatus ne '') {
6993: if ($toolstatus) {
6994: $access = 1;
6995: } else {
6996: $access = 0;
6997: }
6998: return $access;
6999: }
7000:
1.1084 raeburn 7001: my ($is_adv,%domdef);
7002: if (ref($is_advref) eq 'HASH') {
7003: $is_adv = $is_advref->{'is_adv'};
7004: } else {
7005: $is_adv = &is_advanced_user($udom,$uname);
7006: }
7007: if (ref($domdefref) eq 'HASH') {
7008: %domdef = %{$domdefref};
7009: } else {
7010: %domdef = &get_domain_defaults($udom);
7011: }
1.976 raeburn 7012: if (ref($domdef{$tool}) eq 'HASH') {
7013: if ($is_adv) {
7014: if ($domdef{$tool}{'_LC_adv'} ne '') {
7015: if ($domdef{$tool}{'_LC_adv'}) {
7016: $access = 1;
7017: } else {
7018: $access = 0;
7019: }
7020: return $access;
7021: }
7022: }
7023: if ($inststatus ne '') {
7024: my ($hasaccess,$hasnoaccess);
7025: foreach my $affiliation (split(/:/,$inststatus)) {
7026: if ($domdef{$tool}{$affiliation} ne '') {
7027: if ($domdef{$tool}{$affiliation}) {
7028: $hasaccess = 1;
7029: } else {
7030: $hasnoaccess = 1;
7031: }
7032: }
7033: }
7034: if ($hasaccess || $hasnoaccess) {
7035: if ($hasaccess) {
7036: $access = 1;
7037: } elsif ($hasnoaccess) {
7038: $access = 0;
7039: }
7040: return $access;
7041: }
7042: } else {
7043: if ($domdef{$tool}{'default'} ne '') {
7044: if ($domdef{$tool}{'default'}) {
7045: $access = 1;
7046: } elsif ($domdef{$tool}{'default'} == 0) {
7047: $access = 0;
7048: }
7049: return $access;
7050: }
7051: }
7052: } else {
1.1177 raeburn 7053: if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985 raeburn 7054: $access = 1;
7055: } else {
7056: $access = 0;
7057: }
1.976 raeburn 7058: return $access;
7059: }
7060: }
7061:
1.1050 raeburn 7062: sub is_course_owner {
7063: my ($cdom,$cnum,$udom,$uname) = @_;
7064: if (($udom eq '') || ($uname eq '')) {
7065: $udom = $env{'user.domain'};
7066: $uname = $env{'user.name'};
7067: }
7068: unless (($udom eq '') || ($uname eq '')) {
7069: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
7070: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
7071: return 1;
7072: } else {
7073: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
7074: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
7075: return 1;
7076: }
7077: }
7078: }
7079: }
7080: return;
7081: }
7082:
1.976 raeburn 7083: sub is_advanced_user {
7084: my ($udom,$uname) = @_;
1.1085 raeburn 7085: if ($udom ne '' && $uname ne '') {
7086: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128 raeburn 7087: if (wantarray) {
7088: return ($env{'user.adv'},$env{'user.author'});
7089: } else {
7090: return $env{'user.adv'};
7091: }
1.1085 raeburn 7092: }
7093: }
1.976 raeburn 7094: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
7095: my %allroles;
1.1128 raeburn 7096: my ($is_adv,$is_author);
1.976 raeburn 7097: foreach my $role (keys(%roleshash)) {
7098: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
7099: my $area = '/'.$tdomain.'/'.$trest;
7100: if ($sec ne '') {
7101: $area .= '/'.$sec;
7102: }
7103: if (($area ne '') && ($trole ne '')) {
7104: my $spec=$trole.'.'.$area;
7105: if ($trole =~ /^cr\//) {
7106: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
7107: } elsif ($trole ne 'gr') {
7108: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
7109: }
1.1128 raeburn 7110: if ($trole eq 'au') {
7111: $is_author = 1;
7112: }
1.976 raeburn 7113: }
7114: }
7115: foreach my $role (keys(%allroles)) {
7116: last if ($is_adv);
7117: foreach my $item (split(/:/,$allroles{$role})) {
7118: if ($item ne '') {
7119: my ($privilege,$restrictions)=split(/&/,$item);
7120: if ($privilege eq 'adv') {
7121: $is_adv = 1;
7122: last;
7123: }
7124: }
7125: }
7126: }
1.1128 raeburn 7127: if (wantarray) {
7128: return ($is_adv,$is_author);
7129: }
1.976 raeburn 7130: return $is_adv;
7131: }
1.798 raeburn 7132:
1.1035 raeburn 7133: sub check_can_request {
1.1036 raeburn 7134: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 7135: my $canreq = 0;
7136: my ($types,$typename) = &Apache::loncommon::course_types();
7137: my @options = ('approval','validate','autolimit');
7138: my $optregex = join('|',@options);
7139: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
7140: foreach my $type (@{$types}) {
7141: if (&usertools_access($env{'user.name'},
7142: $env{'user.domain'},
7143: $type,undef,'requestcourses')) {
7144: $canreq ++;
1.1036 raeburn 7145: if (ref($request_domains) eq 'HASH') {
7146: push(@{$request_domains->{$type}},$env{'user.domain'});
7147: }
1.1035 raeburn 7148: if ($dom eq $env{'user.domain'}) {
7149: $can_request->{$type} = 1;
7150: }
7151: }
7152: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
7153: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
7154: if (@curr > 0) {
1.1036 raeburn 7155: foreach my $item (@curr) {
7156: if (ref($request_domains) eq 'HASH') {
7157: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
7158: if ($otherdom ne '') {
7159: if (ref($request_domains->{$type}) eq 'ARRAY') {
7160: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
7161: push(@{$request_domains->{$type}},$otherdom);
7162: }
7163: } else {
7164: push(@{$request_domains->{$type}},$otherdom);
7165: }
7166: }
7167: }
7168: }
7169: unless($dom eq $env{'user.domain'}) {
7170: $canreq ++;
1.1035 raeburn 7171: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
7172: $can_request->{$type} = 1;
7173: }
7174: }
7175: }
7176: }
7177: }
7178: }
7179: return $canreq;
7180: }
7181:
1.341 www 7182: # ---------------------------------------------- Custom access rule evaluation
7183:
7184: sub customaccess {
7185: my ($priv,$uri)=@_;
1.807 albertel 7186: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 7187: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 7188: $udom = &LONCAPA::clean_domain($udom);
7189: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 7190: my $access=0;
1.800 albertel 7191: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 7192: my ($effect,$realm,$role,$type)=split(/\:/,$right);
7193: if ($type eq 'user') {
7194: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 7195: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 7196: if ($tdom) {
7197: if ($tdom ne $env{'user.domain'}) { next; }
7198: }
1.896 albertel 7199: if ($tuname) {
7200: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 7201: }
7202: $access=($effect eq 'allow');
7203: last;
7204: }
7205: } else {
7206: if ($role) {
7207: if ($role ne $urole) { next; }
7208: }
7209: foreach my $scope (split(/\s*\,\s*/,$realm)) {
7210: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
7211: if ($tdom) {
7212: if ($tdom ne $udom) { next; }
7213: }
7214: if ($tcrs) {
7215: if ($tcrs ne $ucrs) { next; }
7216: }
7217: if ($tsec) {
7218: if ($tsec ne $usec) { next; }
7219: }
7220: $access=($effect eq 'allow');
7221: last;
7222: }
7223: if ($realm eq '' && $role eq '') {
7224: $access=($effect eq 'allow');
7225: }
1.402 bowersj2 7226: }
1.341 www 7227: }
7228: return $access;
7229: }
7230:
1.103 harris41 7231: # ------------------------------------------------- Check for a user privilege
1.12 www 7232:
7233: sub allowed {
1.1281 raeburn 7234: my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
1.705 albertel 7235: my $ver_orguri=$uri;
1.439 www 7236: $uri=&deversion($uri);
1.152 www 7237: my $orguri=$uri;
1.52 www 7238: $uri=&declutter($uri);
1.809 raeburn 7239:
1.810 raeburn 7240: if ($priv eq 'evb') {
7241: # Evade communication block restrictions for specified role in a course
7242: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
7243: return $1;
7244: } else {
7245: return;
7246: }
7247: }
7248:
1.620 albertel 7249: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 7250: # Free bre access to adm and meta resources
1.1343 ! raeburn 7251: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$}))
1.769 albertel 7252: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
7253: && ($priv eq 'bre')) {
1.14 www 7254: return 'F';
1.159 www 7255: }
7256:
1.545 banghart 7257: # Free bre access to user's own portfolio contents
1.714 raeburn 7258: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 7259: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 7260: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 7261: my %setters;
7262: my ($startblock,$endblock) =
7263: &Apache::loncommon::blockcheck(\%setters,'port');
7264: if ($startblock && $endblock) {
7265: return 'B';
7266: } else {
7267: return 'F';
7268: }
1.545 banghart 7269: }
7270:
1.762 raeburn 7271: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 7272: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
7273: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
7274: if (exists($env{'request.course.id'})) {
7275: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7276: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7277: if (($domain eq $cdom) && ($name eq $cnum)) {
7278: my $courseprivid=$env{'request.course.id'};
7279: $courseprivid=~s/\_/\//;
7280: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
7281: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
7282: return $1;
1.762 raeburn 7283: } else {
7284: if ($env{'request.course.sec'}) {
7285: $courseprivid.='/'.$env{'request.course.sec'};
7286: }
7287: if ($env{'user.priv.'.$env{'request.role'}.'./'.
7288: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
7289: return $2;
7290: }
1.714 raeburn 7291: }
7292: }
7293: }
7294: }
7295:
1.159 www 7296: # Free bre to public access
7297:
7298: if ($priv eq 'bre') {
1.238 www 7299: my $copyright=&metadata($uri,'copyright');
1.620 albertel 7300: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 7301: return 'F';
7302: }
1.238 www 7303: if ($copyright eq 'priv') {
7304: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 7305: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 7306: return '';
7307: }
7308: }
7309: if ($copyright eq 'domain') {
7310: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 7311: unless (($env{'user.domain'} eq $1) ||
7312: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 7313: return '';
7314: }
1.262 matthew 7315: }
1.620 albertel 7316: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 7317: # Library role, so allow browsing of resources in this domain.
7318: return 'F';
1.238 www 7319: }
1.341 www 7320: if ($copyright eq 'custom') {
7321: unless (&customaccess($priv,$uri)) { return ''; }
7322: }
1.14 www 7323: }
1.264 matthew 7324: # Domain coordinator is trying to create a course
1.620 albertel 7325: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 7326: # uri is the requested domain in this case.
7327: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 7328: # a role of dc for the domain in question.
1.620 albertel 7329: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 7330: }
1.29 www 7331:
1.52 www 7332: my $thisallowed='';
7333: my $statecond=0;
7334: my $courseprivid='';
7335:
1.1039 raeburn 7336: my $ownaccess;
1.1043 raeburn 7337: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 7338: if (($priv eq 'bro') && ($env{'user.author'})) {
7339: if ($uri eq '') {
7340: $ownaccess = 1;
7341: } else {
7342: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
7343: my $udom = $env{'user.domain'};
7344: my $uname = $env{'user.name'};
7345: if ($uri =~ m{^\Q$udom\E/?$}) {
7346: $ownaccess = 1;
1.1040 raeburn 7347: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 7348: unless ($uri =~ m{\.\./}) {
7349: $ownaccess = 1;
7350: }
7351: } elsif (($udom ne 'public') && ($uname ne 'public')) {
7352: my $now = time;
7353: if ($uri =~ m{^([^/]+)/?$}) {
7354: my $adom = $1;
7355: foreach my $key (keys(%env)) {
1.1042 raeburn 7356: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 7357: my ($start,$end) = split('.',$env{$key});
7358: if (($now >= $start) && (!$end || $end < $now)) {
7359: $ownaccess = 1;
7360: last;
7361: }
7362: }
7363: }
7364: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
7365: my $adom = $1;
7366: my $aname = $2;
1.1042 raeburn 7367: foreach my $role ('ca','aa') {
7368: if ($env{"user.role.$role./$adom/$aname"}) {
7369: my ($start,$end) =
7370: split('.',$env{"user.role.$role./$adom/$aname"});
7371: if (($now >= $start) && (!$end || $end < $now)) {
7372: $ownaccess = 1;
7373: last;
7374: }
1.1039 raeburn 7375: }
7376: }
7377: }
7378: }
7379: }
7380: }
7381: }
7382:
1.52 www 7383: # Course
7384:
1.620 albertel 7385: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7386: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7387: $thisallowed.=$1;
7388: }
1.52 www 7389: }
1.29 www 7390:
1.52 www 7391: # Domain
7392:
1.620 albertel 7393: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 7394: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7395: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7396: $thisallowed.=$1;
7397: }
1.12 www 7398: }
1.52 www 7399:
1.1141 raeburn 7400: # User who is not author or co-author might still be able to edit
7401: # resource of an author in the domain (e.g., if Domain Coordinator).
7402: if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
7403: (&allowed('mdc',$env{'request.course.id'}))) {
7404: if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
7405: $thisallowed.=$1;
7406: }
7407: }
7408:
1.52 www 7409: # Course: uri itself is a course
1.66 www 7410: my $courseuri=$uri;
7411: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 7412: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 7413:
1.620 albertel 7414: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 7415: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 7416: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 7417: $thisallowed.=$1;
7418: }
1.12 www 7419: }
1.29 www 7420:
1.665 albertel 7421: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 7422: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 7423: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 7424: $thisallowed='';
1.671 raeburn 7425: my ($match)=&is_on_map($uri);
7426: if ($match) {
7427: if ($env{'user.priv.'.$env{'request.role'}.'./'}
7428: =~/\Q$priv\E\&([^\:]*)/) {
1.1281 raeburn 7429: my $value = $1;
7430: if ($noblockcheck) {
7431: $thisallowed.=$value;
1.1162 raeburn 7432: } else {
1.1281 raeburn 7433: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7434: if (@blockers > 0) {
7435: $thisallowed = 'B';
7436: } else {
7437: $thisallowed.=$value;
7438: }
1.1162 raeburn 7439: }
1.671 raeburn 7440: }
7441: } else {
1.705 albertel 7442: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 7443: if ($refuri) {
7444: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 7445: $thisallowed='F';
1.671 raeburn 7446: } else {
7447: $refuri=&declutter($refuri);
7448: my ($match) = &is_on_map($refuri);
7449: if ($match) {
1.1281 raeburn 7450: if ($noblockcheck) {
7451: $thisallowed='F';
1.1162 raeburn 7452: } else {
1.1281 raeburn 7453: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7454: if (@blockers > 0) {
7455: $thisallowed = 'B';
7456: } else {
7457: $thisallowed='F';
7458: }
1.1162 raeburn 7459: }
1.671 raeburn 7460: }
1.669 raeburn 7461: }
1.671 raeburn 7462: }
7463: }
1.314 www 7464: }
1.492 albertel 7465:
1.766 albertel 7466: if ($priv eq 'bre'
7467: && $thisallowed ne 'F'
7468: && $thisallowed ne '2'
7469: && &is_portfolio_url($uri)) {
1.1270 raeburn 7470: $thisallowed = &portfolio_access($uri,$clientip);
1.766 albertel 7471: }
1.1250 raeburn 7472:
1.52 www 7473: # Full access at system, domain or course-wide level? Exit.
1.29 www 7474: if ($thisallowed=~/F/) {
7475: return 'F';
7476: }
7477:
1.52 www 7478: # If this is generating or modifying users, exit with special codes
1.29 www 7479:
1.643 www 7480: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
7481: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 7482: my ($audom,$auname)=split('/',$uri);
1.643 www 7483: # no author name given, so this just checks on the general right to make a co-author in this domain
7484: unless ($auname) { return $thisallowed; }
7485: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 7486: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
7487: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
7488: ($audom ne $env{'request.role.domain'}))) { return ''; }
7489: }
1.52 www 7490: return $thisallowed;
7491: }
7492: #
1.103 harris41 7493: # Gathered so far: system, domain and course wide privileges
1.52 www 7494: #
7495: # Course: See if uri or referer is an individual resource that is part of
7496: # the course
7497:
1.620 albertel 7498: if ($env{'request.course.id'}) {
1.232 www 7499:
1.620 albertel 7500: $courseprivid=$env{'request.course.id'};
7501: if ($env{'request.course.sec'}) {
7502: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 7503: }
7504: $courseprivid=~s/\_/\//;
7505: my $checkreferer=1;
1.232 www 7506: my ($match,$cond)=&is_on_map($uri);
7507: if ($match) {
7508: $statecond=$cond;
1.620 albertel 7509: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 7510: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 7511: my $value = $1;
7512: if ($priv eq 'bre') {
1.1281 raeburn 7513: if ($noblockcheck) {
7514: $thisallowed.=$value;
1.1162 raeburn 7515: } else {
1.1281 raeburn 7516: my @blockers = &has_comm_blocking($priv,$symb,$uri);
7517: if (@blockers > 0) {
7518: $thisallowed = 'B';
7519: } else {
7520: $thisallowed.=$value;
7521: }
1.1162 raeburn 7522: }
7523: } else {
7524: $thisallowed.=$value;
7525: }
1.52 www 7526: $checkreferer=0;
7527: }
1.29 www 7528: }
1.83 www 7529:
1.148 www 7530: if ($checkreferer) {
1.620 albertel 7531: my $refuri=$env{'httpref.'.$orguri};
1.148 www 7532: unless ($refuri) {
1.800 albertel 7533: foreach my $key (keys(%env)) {
7534: if ($key=~/^httpref\..*\*/) {
7535: my $pattern=$key;
1.156 www 7536: $pattern=~s/^httpref\.\/res\///;
1.148 www 7537: $pattern=~s/\*/\[\^\/\]\+/g;
7538: $pattern=~s/\//\\\//g;
1.152 www 7539: if ($orguri=~/$pattern/) {
1.800 albertel 7540: $refuri=$env{$key};
1.148 www 7541: }
7542: }
1.191 harris41 7543: }
1.148 www 7544: }
1.232 www 7545:
1.148 www 7546: if ($refuri) {
1.152 www 7547: $refuri=&declutter($refuri);
1.232 www 7548: my ($match,$cond)=&is_on_map($refuri);
7549: if ($match) {
7550: my $refstatecond=$cond;
1.620 albertel 7551: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 7552: =~/\Q$priv\E\&([^\:]*)/) {
1.1162 raeburn 7553: my $value = $1;
7554: if ($priv eq 'bre') {
1.1281 raeburn 7555: if ($noblockcheck) {
7556: $thisallowed.=$value;
1.1162 raeburn 7557: } else {
1.1281 raeburn 7558: my @blockers = &has_comm_blocking($priv,$symb,$refuri);
7559: if (@blockers > 0) {
7560: $thisallowed = 'B';
7561: } else {
7562: $thisallowed.=$value;
7563: }
1.1162 raeburn 7564: }
7565: } else {
7566: $thisallowed.=$value;
7567: }
1.53 www 7568: $uri=$refuri;
7569: $statecond=$refstatecond;
1.52 www 7570: }
7571: }
1.148 www 7572: }
1.29 www 7573: }
1.52 www 7574: }
1.29 www 7575:
1.52 www 7576: #
1.103 harris41 7577: # Gathered now: all privileges that could apply, and condition number
1.52 www 7578: #
7579: #
7580: # Full or no access?
7581: #
1.29 www 7582:
1.52 www 7583: if ($thisallowed=~/F/) {
7584: return 'F';
7585: }
1.29 www 7586:
1.52 www 7587: unless ($thisallowed) {
7588: return '';
7589: }
1.29 www 7590:
1.52 www 7591: # Restrictions exist, deal with them
7592: #
7593: # C:according to course preferences
7594: # R:according to resource settings
7595: # L:unless locked
7596: # X:according to user session state
7597: #
7598:
7599: # Possibly locked functionality, check all courses
1.54 www 7600: # Locks might take effect only after 10 minutes cache expiration for other
7601: # courses, and 2 minutes for current course
1.52 www 7602:
7603: my $envkey;
7604: if ($thisallowed=~/L/) {
1.1000 raeburn 7605: foreach $envkey (keys(%env)) {
1.54 www 7606: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
7607: my $courseid=$2;
7608: my $roleid=$1.'.'.$2;
1.92 www 7609: $courseid=~s/^\///;
1.54 www 7610: my $expiretime=600;
1.620 albertel 7611: if ($env{'request.role'} eq $roleid) {
1.54 www 7612: $expiretime=120;
7613: }
7614: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
7615: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 7616: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 7617: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 7618: }
1.620 albertel 7619: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
7620: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
7621: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
7622: &log($env{'user.domain'},$env{'user.name'},
7623: $env{'user.home'},
1.57 www 7624: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 7625: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 7626: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 7627: return '';
7628: }
7629: }
1.620 albertel 7630: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
7631: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
7632: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
7633: &log($env{'user.domain'},$env{'user.name'},
7634: $env{'user.home'},
1.57 www 7635: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 7636: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 7637: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 7638: return '';
7639: }
7640: }
7641: }
1.29 www 7642: }
1.52 www 7643: }
7644:
7645: #
7646: # Rest of the restrictions depend on selected course
7647: #
7648:
1.620 albertel 7649: unless ($env{'request.course.id'}) {
1.766 albertel 7650: if ($thisallowed eq 'A') {
7651: return 'A';
1.814 raeburn 7652: } elsif ($thisallowed eq 'B') {
7653: return 'B';
1.766 albertel 7654: } else {
7655: return '1';
7656: }
1.52 www 7657: }
1.29 www 7658:
1.52 www 7659: #
7660: # Now user is definitely in a course
7661: #
1.53 www 7662:
7663:
7664: # Course preferences
7665:
7666: if ($thisallowed=~/C/) {
1.620 albertel 7667: my $rolecode=(split(/\./,$env{'request.role'}))[0];
7668: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
7669: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 7670: =~/\Q$rolecode\E/) {
1.1304 raeburn 7671: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689 albertel 7672: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7673: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
7674: $env{'request.course.id'});
7675: }
1.237 www 7676: return '';
7677: }
7678:
1.620 albertel 7679: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 7680: =~/\Q$unamedom\E/) {
1.1304 raeburn 7681: if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689 albertel 7682: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
7683: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
7684: $env{'request.course.id'});
7685: }
1.54 www 7686: return '';
7687: }
1.53 www 7688: }
7689:
7690: # Resource preferences
7691:
7692: if ($thisallowed=~/R/) {
1.620 albertel 7693: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 7694: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103 raeburn 7695: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 7696: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
7697: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
7698: }
7699: return '';
1.54 www 7700: }
1.53 www 7701: }
1.30 www 7702:
1.246 www 7703: # Restricted by state or randomout?
1.30 www 7704:
1.52 www 7705: if ($thisallowed=~/X/) {
1.620 albertel 7706: if ($env{'acc.randomout'}) {
1.579 albertel 7707: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 7708: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 7709: return '';
7710: }
1.247 www 7711: }
7712: if (&condval($statecond)) {
1.52 www 7713: return '2';
7714: } else {
7715: return '';
7716: }
7717: }
1.30 www 7718:
1.766 albertel 7719: if ($thisallowed eq 'A') {
7720: return 'A';
1.814 raeburn 7721: } elsif ($thisallowed eq 'B') {
7722: return 'B';
1.766 albertel 7723: }
1.52 www 7724: return 'F';
1.232 www 7725: }
1.1162 raeburn 7726:
1.1192 raeburn 7727: # ------------------------------------------- Check construction space access
7728:
7729: sub constructaccess {
7730: my ($url,$setpriv)=@_;
7731:
7732: # We do not allow editing of previous versions of files
7733: if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
7734:
7735: # Get username and domain from URL
7736: my ($ownername,$ownerdomain,$ownerhome);
7737:
7738: ($ownerdomain,$ownername) =
1.1325 raeburn 7739: ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192 raeburn 7740:
7741: # The URL does not really point to any authorspace, forget it
7742: unless (($ownername) && ($ownerdomain)) { return ''; }
7743:
7744: # Now we need to see if the user has access to the authorspace of
7745: # $ownername at $ownerdomain
7746:
7747: if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
7748: # Real author for this?
7749: $ownerhome = $env{'user.home'};
7750: if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
7751: return ($ownername,$ownerdomain,$ownerhome);
7752: }
7753: } else {
7754: # Co-author for this?
7755: if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
7756: exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
7757: $ownerhome = &homeserver($ownername,$ownerdomain);
7758: return ($ownername,$ownerdomain,$ownerhome);
7759: }
1.1312 raeburn 7760: if ($env{'request.course.id'}) {
7761: if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
7762: ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
7763: if (&allowed('mdc',$env{'request.course.id'})) {
7764: $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
7765: return ($ownername,$ownerdomain,$ownerhome);
7766: }
7767: }
7768: }
1.1192 raeburn 7769: }
7770:
7771: # We don't have any access right now. If we are not possibly going to do anything about this,
7772: # we might as well leave
7773: unless ($setpriv) { return ''; }
7774:
7775: # Backdoor access?
7776: my $allowed=&allowed('eco',$ownerdomain);
7777: # Nope
7778: unless ($allowed) { return ''; }
7779: # Looks like we may have access, but could be locked by the owner of the construction space
7780: if ($allowed eq 'U') {
7781: my %blocked=&get('environment',['domcoord.author'],
7782: $ownerdomain,$ownername);
7783: # Is blocked by owner
7784: if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
7785: }
7786: if (($allowed eq 'F') || ($allowed eq 'U')) {
7787: # Grant temporary access
7788: my $then=$env{'user.login.time'};
1.1209 raeburn 7789: my $update=$env{'user.update.time'};
1.1192 raeburn 7790: if (!$update) { $update = $then; }
7791: my $refresh=$env{'user.refresh.time'};
7792: if (!$refresh) { $refresh = $update; }
7793: my $now = time;
7794: &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
7795: $now,'ca','constructaccess');
7796: $ownerhome = &homeserver($ownername,$ownerdomain);
7797: return($ownername,$ownerdomain,$ownerhome);
7798: }
7799: # No business here
7800: return '';
7801: }
7802:
1.1282 raeburn 7803: # ----------------------------------------------------------- Content Blocking
7804:
7805: {
7806: # Caches for faster Course Contents display where content blocking
7807: # is in operation (i.e., interval param set) for timed quiz.
7808: #
7809: # User for whom data are being temporarily cached.
7810: my $cacheduser='';
7811: # Cached blockers for this user (a hash of blocking items).
7812: my %cachedblockers=();
7813: # When the data were last cached.
7814: my $cachedlast='';
7815:
7816: sub load_all_blockers {
7817: my ($uname,$udom,$blocks)=@_;
7818: if (($uname ne '') && ($udom ne '')) {
7819: if (($cacheduser eq $uname.':'.$udom) &&
7820: (abs($cachedlast-time)<5)) {
7821: return;
7822: }
7823: }
7824: $cachedlast=time;
7825: $cacheduser=$uname.':'.$udom;
7826: %cachedblockers = &get_commblock_resources($blocks);
7827: }
7828:
1.1162 raeburn 7829: sub get_comm_blocks {
7830: my ($cdom,$cnum) = @_;
7831: if ($cdom eq '' || $cnum eq '') {
7832: return unless ($env{'request.course.id'});
7833: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
7834: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
7835: }
7836: my %commblocks;
7837: my $hashid=$cdom.'_'.$cnum;
7838: my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
7839: if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
7840: %commblocks = %{$blocksref};
7841: } else {
7842: %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
7843: my $cachetime = 600;
7844: &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
7845: }
7846: return %commblocks;
7847: }
7848:
1.1282 raeburn 7849: sub get_commblock_resources {
7850: my ($blocks) = @_;
7851: my %blockers = ();
7852: return %blockers unless ($env{'request.course.id'});
7853: return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
1.1162 raeburn 7854: my %commblocks;
7855: if (ref($blocks) eq 'HASH') {
7856: %commblocks = %{$blocks};
7857: } else {
7858: %commblocks = &get_comm_blocks();
7859: }
1.1282 raeburn 7860: return %blockers unless (keys(%commblocks) > 0);
7861: my $navmap = Apache::lonnavmaps::navmap->new();
7862: return %blockers unless (ref($navmap));
1.1162 raeburn 7863: my $now = time;
7864: foreach my $block (keys(%commblocks)) {
7865: if ($block =~ /^(\d+)____(\d+)$/) {
7866: my ($start,$end) = ($1,$2);
7867: if ($start <= $now && $end >= $now) {
7868: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
7869: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
7870: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282 raeburn 7871: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
7872: $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
1.1162 raeburn 7873: }
7874: }
7875: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282 raeburn 7876: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
7877: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162 raeburn 7878: }
7879: }
7880: }
7881: }
7882: }
7883: } elsif ($block =~ /^firstaccess____(.+)$/) {
7884: my $item = $1;
1.1163 raeburn 7885: my @to_test;
1.1162 raeburn 7886: if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
7887: if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1282 raeburn 7888: my @interval;
7889: my $type = 'map';
7890: if ($item eq 'course') {
7891: $type = 'course';
7892: @interval=&EXT("resource.0.interval");
7893: } else {
7894: if ($item =~ /___\d+___/) {
7895: $type = 'resource';
7896: @interval=&EXT("resource.0.interval",$item);
7897: if (ref($navmap)) {
7898: my $res = $navmap->getBySymb($item);
7899: push(@to_test,$res);
7900: }
1.1162 raeburn 7901: } else {
1.1282 raeburn 7902: my $mapsymb = &symbread($item,1);
7903: if ($mapsymb) {
7904: if (ref($navmap)) {
7905: my $mapres = $navmap->getBySymb($mapsymb);
7906: @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
7907: foreach my $res (@to_test) {
7908: my $symb = $res->symb();
7909: next if ($symb eq $mapsymb);
7910: if ($symb ne '') {
7911: @interval=&EXT("resource.0.interval",$symb);
7912: if ($interval[1] eq 'map') {
7913: last;
1.1162 raeburn 7914: }
7915: }
7916: }
7917: }
7918: }
7919: }
1.1282 raeburn 7920: }
1.1310 raeburn 7921: if ($interval[0] =~ /^(\d+)/) {
1.1308 raeburn 7922: my $timelimit = $1;
1.1282 raeburn 7923: my $first_access;
7924: if ($type eq 'resource') {
7925: $first_access=&get_first_access($interval[1],$item);
7926: } elsif ($type eq 'map') {
7927: $first_access=&get_first_access($interval[1],undef,$item);
7928: } else {
7929: $first_access=&get_first_access($interval[1]);
7930: }
7931: if ($first_access) {
1.1292 raeburn 7932: my $timesup = $first_access+$timelimit;
1.1282 raeburn 7933: if ($timesup > $now) {
7934: my $activeblock;
7935: foreach my $res (@to_test) {
1.1283 raeburn 7936: if ($res->answerable()) {
1.1282 raeburn 7937: $activeblock = 1;
7938: last;
7939: }
7940: }
7941: if ($activeblock) {
7942: if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
7943: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
7944: $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
7945: }
7946: }
7947: if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
7948: if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
7949: $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163 raeburn 7950: }
1.1162 raeburn 7951: }
7952: }
7953: }
7954: }
7955: }
7956: }
7957: }
7958: }
7959: }
1.1282 raeburn 7960: return %blockers;
1.1162 raeburn 7961: }
7962:
1.1282 raeburn 7963: sub has_comm_blocking {
7964: my ($priv,$symb,$uri,$blocks) = @_;
7965: my @blockers;
7966: return unless ($env{'request.course.id'});
7967: return unless ($priv eq 'bre');
7968: return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
7969: return if ($env{'request.state'} eq 'construct');
7970: &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
7971: return unless (keys(%cachedblockers) > 0);
7972: my (%possibles,@symbs);
7973: if (!$symb) {
7974: $symb = &symbread($uri,1,1,1,\%possibles);
1.1162 raeburn 7975: }
1.1282 raeburn 7976: if ($symb) {
7977: @symbs = ($symb);
7978: } elsif (keys(%possibles)) {
7979: @symbs = keys(%possibles);
7980: }
7981: my $noblock;
7982: foreach my $symb (@symbs) {
7983: last if ($noblock);
7984: my ($map,$resid,$resurl)=&decode_symb($symb);
7985: foreach my $block (keys(%cachedblockers)) {
7986: if ($block =~ /^firstaccess____(.+)$/) {
7987: my $item = $1;
7988: if (($item eq $map) || ($item eq $symb)) {
7989: $noblock = 1;
7990: last;
7991: }
7992: }
7993: if (ref($cachedblockers{$block}) eq 'HASH') {
7994: if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
7995: if ($cachedblockers{$block}{'resources'}{$symb}) {
7996: unless (grep(/^\Q$block\E$/,@blockers)) {
7997: push(@blockers,$block);
7998: }
7999: }
8000: }
1.1162 raeburn 8001: }
1.1282 raeburn 8002: if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
8003: if ($cachedblockers{$block}{'maps'}{$map}) {
8004: unless (grep(/^\Q$block\E$/,@blockers)) {
8005: push(@blockers,$block);
8006: }
8007: }
1.1162 raeburn 8008: }
8009: }
8010: }
1.1282 raeburn 8011: return if ($noblock);
8012: return @blockers;
8013: }
1.1162 raeburn 8014: }
8015:
1.1282 raeburn 8016: # -------------------------------- Deversion and split uri into path an filename
8017:
1.1133 foxr 8018: #
1.1282 raeburn 8019: # Removes the version from a URI and
1.1133 foxr 8020: # splits it in to its filename and path to the filename.
8021: # Seems like File::Basename could have done this more clearly.
8022: # Parameters:
8023: # $uri - input URI
8024: # Returns:
8025: # Two element list consisting of
8026: # $pathname - the URI up to and excluding the trailing /
8027: # $filename - The part of the URI following the last /
8028: # NOTE:
8029: # Another realization of this is simply:
8030: # use File::Basename;
8031: # ...
8032: # $uri = shift;
8033: # $filename = basename($uri);
8034: # $path = dirname($uri);
8035: # return ($filename, $path);
8036: #
8037: # The implementation below is probably faster however.
8038: #
1.710 albertel 8039: sub split_uri_for_cond {
8040: my $uri=&deversion(&declutter(shift));
8041: my @uriparts=split(/\//,$uri);
8042: my $filename=pop(@uriparts);
8043: my $pathname=join('/',@uriparts);
8044: return ($pathname,$filename);
8045: }
1.232 www 8046: # --------------------------------------------------- Is a resource on the map?
8047:
8048: sub is_on_map {
1.710 albertel 8049: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 8050: #Trying to find the conditional for the file
1.620 albertel 8051: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 8052: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 8053: if ($match) {
1.289 bowersj2 8054: return (1,$1);
8055: } else {
1.434 www 8056: return (0,0);
1.289 bowersj2 8057: }
1.12 www 8058: }
8059:
1.427 www 8060: # --------------------------------------------------------- Get symb from alias
8061:
8062: sub get_symb_from_alias {
8063: my $symb=shift;
8064: my ($map,$resid,$url)=&decode_symb($symb);
8065: # Already is a symb
8066: if ($url) { return $symb; }
8067: # Must be an alias
8068: my $aliassymb='';
8069: my %bighash;
1.620 albertel 8070: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 8071: &GDBM_READER(),0640)) {
8072: my $rid=$bighash{'mapalias_'.$symb};
8073: if ($rid) {
8074: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 8075: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
8076: $resid,$bighash{'src_'.$rid});
1.427 www 8077: }
8078: untie %bighash;
8079: }
8080: return $aliassymb;
8081: }
8082:
1.12 www 8083: # ----------------------------------------------------------------- Define Role
8084:
8085: sub definerole {
8086: if (allowed('mcr','/')) {
1.1326 raeburn 8087: my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800 albertel 8088: foreach my $role (split(':',$sysrole)) {
8089: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 8090: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
8091: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
8092: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 8093: return "refused:s:$crole&$cqual";
8094: }
8095: }
1.191 harris41 8096: }
1.800 albertel 8097: foreach my $role (split(':',$domrole)) {
8098: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 8099: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
8100: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
8101: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 8102: return "refused:d:$crole&$cqual";
8103: }
8104: }
1.191 harris41 8105: }
1.800 albertel 8106: foreach my $role (split(':',$courole)) {
8107: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 8108: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
8109: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
8110: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 8111: return "refused:c:$crole&$cqual";
8112: }
8113: }
1.191 harris41 8114: }
1.1326 raeburn 8115: my $uhome;
8116: if (($uname ne '') && ($udom ne '')) {
8117: $uhome = &homeserver($uname,$udom);
8118: return $uhome if ($uhome eq 'no_host');
8119: } else {
8120: $uname = $env{'user.name'};
8121: $udom = $env{'user.domain'};
8122: $uhome = $env{'user.home'};
8123: }
1.620 albertel 8124: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326 raeburn 8125: "$udom:$uname:rolesdef_$rolename=".
1.21 www 8126: escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326 raeburn 8127: return reply($command,$uhome);
1.12 www 8128: } else {
8129: return 'refused';
8130: }
1.105 harris41 8131: }
8132:
8133: # ---------------- Make a metadata query against the network of library servers
8134:
8135: sub metadata_query {
1.1237 raeburn 8136: my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120 harris41 8137: my %rhash;
1.845 albertel 8138: my %libserv = &all_library();
1.244 matthew 8139: my @server_list = (defined($server_array) ? @$server_array
8140: : keys(%libserv) );
8141: for my $server (@server_list) {
1.1237 raeburn 8142: my $domains = '';
8143: if (ref($domains_hash) eq 'HASH') {
8144: $domains = $domains_hash->{$server};
8145: }
1.118 harris41 8146: unless ($custom or $customshow) {
1.1237 raeburn 8147: my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118 harris41 8148: $rhash{$server}=$reply;
8149: }
8150: else {
8151: my $reply=&reply("querysend:".&escape($query).':'.
1.1237 raeburn 8152: &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118 harris41 8153: $server);
8154: $rhash{$server}=$reply;
8155: }
1.112 harris41 8156: }
1.118 harris41 8157: return \%rhash;
1.240 www 8158: }
8159:
8160: # ----------------------------------------- Send log queries and wait for reply
8161:
8162: sub log_query {
8163: my ($uname,$udom,$query,%filters)=@_;
8164: my $uhome=&homeserver($uname,$udom);
8165: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 8166: my $uhost=&hostname($uhome);
1.800 albertel 8167: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 8168: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
8169: $uhome);
1.479 albertel 8170: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 8171: return get_query_reply($queryid);
8172: }
8173:
1.818 raeburn 8174: # -------------------------- Update MySQL table for portfolio file
8175:
8176: sub update_portfolio_table {
1.821 raeburn 8177: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 8178: if ($group ne '') {
8179: $file_name =~s /^\Q$group\E//;
8180: }
1.818 raeburn 8181: my $homeserver = &homeserver($uname,$udom);
8182: my $queryid=
1.821 raeburn 8183: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
8184: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 8185: my $reply = &get_query_reply($queryid);
8186: return $reply;
8187: }
8188:
1.899 raeburn 8189: # -------------------------- Update MySQL allusers table
8190:
8191: sub update_allusers_table {
8192: my ($uname,$udom,$names) = @_;
8193: my $homeserver = &homeserver($uname,$udom);
8194: my $queryid=
8195: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
8196: 'lastname='.&escape($names->{'lastname'}).'%%'.
8197: 'firstname='.&escape($names->{'firstname'}).'%%'.
8198: 'middlename='.&escape($names->{'middlename'}).'%%'.
8199: 'generation='.&escape($names->{'generation'}).'%%'.
8200: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
8201: 'id='.&escape($names->{'id'}),$homeserver);
1.1075 raeburn 8202: return;
1.899 raeburn 8203: }
8204:
1.508 raeburn 8205: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 8206:
8207: sub fetch_enrollment_query {
1.511 raeburn 8208: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317 raeburn 8209: my ($homeserver,$sleep,$loopmax);
1.547 raeburn 8210: my $maxtries = 1;
1.508 raeburn 8211: if ($context eq 'automated') {
8212: $homeserver = $perlvar{'lonHostID'};
1.1317 raeburn 8213: $sleep = 2;
8214: $loopmax = 100;
1.547 raeburn 8215: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 8216: } else {
8217: $homeserver = &homeserver($cnum,$dom);
8218: }
1.838 albertel 8219: my $host=&hostname($homeserver);
1.506 raeburn 8220: my $cmd = '';
1.1000 raeburn 8221: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 8222: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 8223: }
8224: $cmd =~ s/%%$//;
8225: $cmd = &escape($cmd);
8226: my $query = 'fetchenrollment';
1.620 albertel 8227: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 8228: unless ($queryid=~/^\Q$host\E\_/) {
8229: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
8230: return 'error: '.$queryid;
8231: }
1.1317 raeburn 8232: my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 8233: my $tries = 1;
8234: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317 raeburn 8235: $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547 raeburn 8236: $tries ++;
8237: }
1.526 raeburn 8238: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 8239: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 8240: } else {
1.901 albertel 8241: my @responses = split(/:/,$reply);
1.1322 raeburn 8242: if (grep { $_ eq $homeserver } ¤t_machine_ids()) {
1.800 albertel 8243: foreach my $line (@responses) {
8244: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 8245: $$replyref{$key} = $value;
8246: }
8247: } else {
1.1117 foxr 8248: my $pathname = LONCAPA::tempdir();
1.800 albertel 8249: foreach my $line (@responses) {
8250: my ($key,$value) = split(/=/,$line);
1.506 raeburn 8251: $$replyref{$key} = $value;
8252: if ($value > 0) {
1.800 albertel 8253: foreach my $item (@{$$affiliatesref{$key}}) {
8254: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 8255: my $destname = $pathname.'/'.$filename;
8256: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 8257: if ($xml_classlist =~ /^error/) {
8258: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
8259: } else {
1.506 raeburn 8260: if ( open(FILE,">$destname") ) {
8261: print FILE &unescape($xml_classlist);
8262: close(FILE);
1.526 raeburn 8263: } else {
8264: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 8265: }
8266: }
8267: }
8268: }
8269: }
8270: }
8271: return 'ok';
8272: }
8273: return 'error';
8274: }
8275:
1.242 www 8276: sub get_query_reply {
1.1317 raeburn 8277: my ($queryid,$sleep,$loopmax) = @_;;
8278: if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
8279: $sleep = 0.2;
8280: }
8281: if (($loopmax eq '') || ($loopmax =~ /\D/)) {
8282: $loopmax = 100;
8283: }
1.1117 foxr 8284: my $replyfile=LONCAPA::tempdir().$queryid;
1.240 www 8285: my $reply='';
1.1317 raeburn 8286: for (1..$loopmax) {
8287: sleep($sleep);
1.240 www 8288: if (-e $replyfile.'.end') {
1.448 albertel 8289: if (open(my $fh,$replyfile)) {
1.904 albertel 8290: $reply = join('',<$fh>);
8291: close($fh);
1.240 www 8292: } else { return 'error: reply_file_error'; }
1.242 www 8293: return &unescape($reply);
8294: }
1.240 www 8295: }
1.242 www 8296: return 'timeout:'.$queryid;
1.240 www 8297: }
8298:
8299: sub courselog_query {
1.241 www 8300: #
8301: # possible filters:
8302: # url: url or symb
8303: # username
8304: # domain
8305: # action: view, submit, grade
8306: # start: timestamp
8307: # end: timestamp
8308: #
1.240 www 8309: my (%filters)=@_;
1.620 albertel 8310: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 8311: if ($filters{'url'}) {
8312: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
8313: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
8314: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
8315: }
1.620 albertel 8316: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
8317: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 8318: return &log_query($cname,$cdom,'courselog',%filters);
8319: }
8320:
8321: sub userlog_query {
1.858 raeburn 8322: #
8323: # possible filters:
8324: # action: log check role
8325: # start: timestamp
8326: # end: timestamp
8327: #
1.240 www 8328: my ($uname,$udom,%filters)=@_;
8329: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 8330: }
8331:
1.506 raeburn 8332: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
8333:
8334: sub auto_run {
1.508 raeburn 8335: my ($cnum,$cdom) = @_;
1.876 raeburn 8336: my $response = 0;
8337: my $settings;
8338: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
8339: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
8340: $settings = $domconfig{'autoenroll'};
8341: if ($settings->{'run'} eq '1') {
8342: $response = 1;
8343: }
8344: } else {
1.934 raeburn 8345: my $homeserver;
8346: if (&is_course($cdom,$cnum)) {
8347: $homeserver = &homeserver($cnum,$cdom);
8348: } else {
8349: $homeserver = &domain($cdom,'primary');
8350: }
8351: if ($homeserver ne 'no_host') {
8352: $response = &reply('autorun:'.$cdom,$homeserver);
8353: }
1.876 raeburn 8354: }
1.506 raeburn 8355: return $response;
8356: }
1.776 albertel 8357:
1.506 raeburn 8358: sub auto_get_sections {
1.508 raeburn 8359: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 8360: my $homeserver;
8361: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8362: $homeserver = &homeserver($cnum,$cdom);
8363: }
8364: if (!defined($homeserver)) {
8365: if ($cdom =~ /^$match_domain$/) {
8366: $homeserver = &domain($cdom,'primary');
8367: }
8368: }
8369: my @secs;
8370: if (defined($homeserver)) {
8371: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
8372: unless ($response eq 'refused') {
8373: @secs = split(/:/,$response);
8374: }
1.506 raeburn 8375: }
8376: return @secs;
8377: }
1.776 albertel 8378:
1.506 raeburn 8379: sub auto_new_course {
1.1099 raeburn 8380: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 8381: my $homeserver = &homeserver($cnum,$cdom);
1.1099 raeburn 8382: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 8383: return $response;
8384: }
1.776 albertel 8385:
1.506 raeburn 8386: sub auto_validate_courseID {
1.508 raeburn 8387: my ($cnum,$cdom,$inst_course_id) = @_;
8388: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 8389: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 8390: return $response;
8391: }
1.776 albertel 8392:
1.1007 raeburn 8393: sub auto_validate_instcode {
1.1020 raeburn 8394: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 8395: my ($homeserver,$response);
8396: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8397: $homeserver = &homeserver($cnum,$cdom);
8398: }
8399: if (!defined($homeserver)) {
8400: if ($cdom =~ /^$match_domain$/) {
8401: $homeserver = &domain($cdom,'primary');
8402: }
8403: }
1.1065 raeburn 8404: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
8405: &escape($instcode).':'.&escape($owner),$homeserver));
1.1216 raeburn 8406: my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
8407: return ($outcome,$description,$defaultcredits);
1.1007 raeburn 8408: }
8409:
1.506 raeburn 8410: sub auto_create_password {
1.873 raeburn 8411: my ($cnum,$cdom,$authparam,$udom) = @_;
8412: my ($homeserver,$response);
1.506 raeburn 8413: my $create_passwd = 0;
8414: my $authchk = '';
1.873 raeburn 8415: if ($udom =~ /^$match_domain$/) {
8416: $homeserver = &domain($udom,'primary');
8417: }
8418: if ($homeserver eq '') {
8419: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
8420: $homeserver = &homeserver($cnum,$cdom);
8421: }
8422: }
8423: if ($homeserver eq '') {
8424: $authchk = 'nodomain';
1.506 raeburn 8425: } else {
1.873 raeburn 8426: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
8427: if ($response eq 'refused') {
8428: $authchk = 'refused';
8429: } else {
1.901 albertel 8430: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 8431: }
1.506 raeburn 8432: }
8433: return ($authparam,$create_passwd,$authchk);
8434: }
8435:
1.706 raeburn 8436: sub auto_photo_permission {
8437: my ($cnum,$cdom,$students) = @_;
8438: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 8439: my ($outcome,$perm_reqd,$conditions) =
8440: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 8441: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8442: return (undef,undef);
8443: }
1.706 raeburn 8444: return ($outcome,$perm_reqd,$conditions);
8445: }
8446:
8447: sub auto_checkphotos {
8448: my ($uname,$udom,$pid) = @_;
8449: my $homeserver = &homeserver($uname,$udom);
8450: my ($result,$resulttype);
8451: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 8452: &escape($uname).':'.&escape($pid),
8453: $homeserver));
1.709 albertel 8454: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8455: return (undef,undef);
8456: }
1.706 raeburn 8457: if ($outcome) {
8458: ($result,$resulttype) = split(/:/,$outcome);
8459: }
8460: return ($result,$resulttype);
8461: }
8462:
8463: sub auto_photochoice {
8464: my ($cnum,$cdom) = @_;
8465: my $homeserver = &homeserver($cnum,$cdom);
8466: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 8467: &escape($cdom),
8468: $homeserver)));
1.709 albertel 8469: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
8470: return (undef,undef);
8471: }
1.706 raeburn 8472: return ($update,$comment);
8473: }
8474:
8475: sub auto_photoupdate {
8476: my ($affiliatesref,$dom,$cnum,$photo) = @_;
8477: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 8478: my $host=&hostname($homeserver);
1.706 raeburn 8479: my $cmd = '';
8480: my $maxtries = 1;
1.800 albertel 8481: foreach my $affiliate (keys(%{$affiliatesref})) {
8482: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 8483: }
8484: $cmd =~ s/%%$//;
8485: $cmd = &escape($cmd);
8486: my $query = 'institutionalphotos';
8487: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
8488: unless ($queryid=~/^\Q$host\E\_/) {
8489: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
8490: return 'error: '.$queryid;
8491: }
8492: my $reply = &get_query_reply($queryid);
8493: my $tries = 1;
8494: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
8495: $reply = &get_query_reply($queryid);
8496: $tries ++;
8497: }
8498: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
8499: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
8500: } else {
8501: my @responses = split(/:/,$reply);
8502: my $outcome = shift(@responses);
8503: foreach my $item (@responses) {
8504: my ($key,$value) = split(/=/,$item);
8505: $$photo{$key} = $value;
8506: }
8507: return $outcome;
8508: }
8509: return 'error';
8510: }
8511:
1.521 raeburn 8512: sub auto_instcode_format {
1.793 albertel 8513: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
8514: $cat_order) = @_;
1.521 raeburn 8515: my $courses = '';
1.772 raeburn 8516: my @homeservers;
1.521 raeburn 8517: if ($caller eq 'global') {
1.841 albertel 8518: my %servers = &get_servers($codedom,'library');
8519: foreach my $tryserver (keys(%servers)) {
8520: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8521: push(@homeservers,$tryserver);
8522: }
1.584 raeburn 8523: }
1.1022 raeburn 8524: } elsif ($caller eq 'requests') {
8525: if ($codedom =~ /^$match_domain$/) {
8526: my $chome = &domain($codedom,'primary');
8527: unless ($chome eq 'no_host') {
8528: push(@homeservers,$chome);
8529: }
8530: }
1.521 raeburn 8531: } else {
1.772 raeburn 8532: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 8533: }
1.793 albertel 8534: foreach my $code (keys(%{$instcodes})) {
8535: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 8536: }
8537: chop($courses);
1.772 raeburn 8538: my $ok_response = 0;
8539: my $response;
8540: while (@homeservers > 0 && $ok_response == 0) {
8541: my $server = shift(@homeservers);
8542: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
8543: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
8544: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 8545: split(/:/,$response);
1.772 raeburn 8546: %{$codes} = (%{$codes},&str2hash($codes_str));
8547: push(@{$codetitles},&str2array($codetitles_str));
8548: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
8549: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
8550: $ok_response = 1;
8551: }
8552: }
8553: if ($ok_response) {
1.521 raeburn 8554: return 'ok';
1.772 raeburn 8555: } else {
8556: return $response;
1.521 raeburn 8557: }
8558: }
8559:
1.792 raeburn 8560: sub auto_instcode_defaults {
8561: my ($domain,$returnhash,$code_order) = @_;
8562: my @homeservers;
1.841 albertel 8563:
8564: my %servers = &get_servers($domain,'library');
8565: foreach my $tryserver (keys(%servers)) {
8566: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8567: push(@homeservers,$tryserver);
8568: }
1.792 raeburn 8569: }
1.841 albertel 8570:
1.792 raeburn 8571: my $response;
1.841 albertel 8572: foreach my $server (@homeservers) {
1.792 raeburn 8573: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 8574: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
8575:
8576: foreach my $pair (split(/\&/,$response)) {
8577: my ($name,$value)=split(/\=/,$pair);
8578: if ($name eq 'code_order') {
8579: @{$code_order} = split(/\&/,&unescape($value));
8580: } else {
8581: $returnhash->{&unescape($name)}=&unescape($value);
8582: }
8583: }
8584: return 'ok';
1.792 raeburn 8585: }
1.841 albertel 8586:
8587: return $response;
1.1003 raeburn 8588: }
8589:
8590: sub auto_possible_instcodes {
1.1007 raeburn 8591: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
8592: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
8593: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8594: return;
8595: }
1.1003 raeburn 8596: my (@homeservers,$uhome);
8597: if (defined(&domain($domain,'primary'))) {
8598: $uhome=&domain($domain,'primary');
8599: push(@homeservers,&domain($domain,'primary'));
8600: } else {
8601: my %servers = &get_servers($domain,'library');
8602: foreach my $tryserver (keys(%servers)) {
8603: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
8604: push(@homeservers,$tryserver);
8605: }
8606: }
8607: }
8608: my $response;
8609: foreach my $server (@homeservers) {
8610: $response=&reply('autopossibleinstcodes:'.$domain,$server);
8611: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 8612: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
8613: split(':',$response);
8614: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
8615: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 8616: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 8617: my ($name,$value)=split('=',$item);
8618: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 8619: }
8620: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 8621: my ($name,$value)=split('=',$item);
8622: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 8623: }
8624: return 'ok';
8625: }
8626: return $response;
8627: }
1.792 raeburn 8628:
1.1010 raeburn 8629: sub auto_courserequest_checks {
8630: my ($dom) = @_;
1.1020 raeburn 8631: my ($homeserver,%validations);
8632: if ($dom =~ /^$match_domain$/) {
8633: $homeserver = &domain($dom,'primary');
8634: }
8635: unless ($homeserver eq 'no_host') {
8636: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
8637: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8638: my @items = split(/&/,$response);
8639: foreach my $item (@items) {
8640: my ($key,$value) = split('=',$item);
8641: $validations{&unescape($key)} = &thaw_unescape($value);
8642: }
8643: }
8644: }
1.1010 raeburn 8645: return %validations;
8646: }
8647:
1.1020 raeburn 8648: sub auto_courserequest_validation {
1.1255 raeburn 8649: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020 raeburn 8650: my ($homeserver,$response);
8651: if ($dom =~ /^$match_domain$/) {
8652: $homeserver = &domain($dom,'primary');
8653: }
1.1255 raeburn 8654: unless ($homeserver eq 'no_host') {
8655: my $customdata;
8656: if (ref($custominfo) eq 'HASH') {
8657: $customdata = &freeze_escape($custominfo);
8658: }
1.1020 raeburn 8659: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 8660: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255 raeburn 8661: ':'.&escape($instcode).':'.&escape($instseclist).':'.
8662: $customdata,$homeserver));
1.1020 raeburn 8663: }
8664: return $response;
8665: }
8666:
1.777 albertel 8667: sub auto_validate_class_sec {
1.918 raeburn 8668: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 8669: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 8670: my $ownerlist;
8671: if (ref($owners) eq 'ARRAY') {
8672: $ownerlist = join(',',@{$owners});
8673: } else {
8674: $ownerlist = $owners;
8675: }
1.773 raeburn 8676: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 8677: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 8678: return $response;
8679: }
8680:
1.1248 raeburn 8681: sub auto_crsreq_update {
8682: my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257 raeburn 8683: $code,$accessstart,$accessend,$inbound) = @_;
1.1248 raeburn 8684: my ($homeserver,%crsreqresponse);
8685: if ($cdom =~ /^$match_domain$/) {
8686: $homeserver = &domain($cdom,'primary');
8687: }
8688: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8689: my $info;
8690: if (ref($inbound) eq 'HASH') {
8691: $info = &freeze_escape($inbound);
8692: }
8693: my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
8694: ':'.&escape($action).':'.&escape($ownername).':'.
8695: &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257 raeburn 8696: &escape($title).':'.&escape($code).':'.
8697: &escape($accessstart).':'.&escape($accessend).':'.$info,
8698: $homeserver);
1.1248 raeburn 8699: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
8700: my @items = split(/&/,$response);
8701: foreach my $item (@items) {
8702: my ($key,$value) = split('=',$item);
8703: $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
8704: }
8705: }
8706: }
8707: return \%crsreqresponse;
8708: }
8709:
1.1305 raeburn 8710: sub auto_export_grades {
1.1309 raeburn 8711: my ($cdom,$cnum,$inforef,$gradesref) = @_;
8712: my ($homeserver,%exportresponse);
8713: if ($cdom =~ /^$match_domain$/) {
8714: $homeserver = &domain($cdom,'primary');
8715: }
8716: unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
8717: my $info;
8718: if (ref($inforef) eq 'HASH') {
8719: $info = &freeze_escape($inforef);
8720: }
8721: if (ref($gradesref) eq 'HASH') {
8722: my $grades = &freeze_escape($gradesref);
8723: my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
8724: $info.':'.$grades,$homeserver);
8725: unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
8726: my @items = split(/&/,$response);
8727: foreach my $item (@items) {
8728: my ($key,$value) = split('=',$item);
8729: $exportresponse{&unescape($key)} = &thaw_unescape($value);
8730: }
8731: }
8732: }
8733: }
8734: return \%exportresponse;
1.1305 raeburn 8735: }
8736:
1.1287 raeburn 8737: sub check_instcode_cloning {
8738: my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
8739: unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
8740: return;
8741: }
8742: my $canclone;
8743: if (@{$code_order} > 0) {
8744: my $instcoderegexp ='^';
8745: my @clonecodes = split(/\&/,$cloner);
8746: foreach my $item (@{$code_order}) {
8747: if (grep(/^\Q$item\E=/,@clonecodes)) {
8748: foreach my $pair (@clonecodes) {
8749: my ($key,$val) = split(/\=/,$pair,2);
8750: $val = &unescape($val);
8751: if ($key eq $item) {
8752: $instcoderegexp .= '('.$val.')';
8753: last;
8754: }
8755: }
8756: } else {
8757: $instcoderegexp .= $codedefaults->{$item};
8758: }
8759: }
8760: $instcoderegexp .= '$';
8761: my (@from,@to);
8762: eval {
8763: (@from) = ($clonefromcode =~ /$instcoderegexp/);
8764: (@to) = ($clonetocode =~ /$instcoderegexp/);
8765: };
8766: if ((@from > 0) && (@to > 0)) {
8767: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
8768: if (!@diffs) {
8769: $canclone = 1;
8770: }
8771: }
8772: }
8773: return $canclone;
8774: }
8775:
8776: sub default_instcode_cloning {
8777: my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
8778: my (%codedefaults,@code_order,$canclone);
8779: if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
8780: %codedefaults = %{$codedefaultsref};
8781: @code_order = @{$codeorderref};
8782: } elsif ($clonedom) {
8783: &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
8784: }
8785: if (($domdefclone) && (@code_order)) {
8786: my @clonecodes = split(/\+/,$domdefclone);
8787: my $instcoderegexp ='^';
8788: foreach my $item (@code_order) {
8789: if (grep(/^\Q$item\E$/,@clonecodes)) {
8790: $instcoderegexp .= '('.$codedefaults{$item}.')';
8791: } else {
8792: $instcoderegexp .= $codedefaults{$item};
8793: }
8794: }
8795: $instcoderegexp .= '$';
8796: my (@from,@to);
8797: eval {
8798: (@from) = ($clonefromcode =~ /$instcoderegexp/);
8799: (@to) = ($clonetocode =~ /$instcoderegexp/);
8800: };
8801: if ((@from > 0) && (@to > 0)) {
8802: my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
8803: if (!@diffs) {
8804: $canclone = 1;
8805: }
8806: }
8807: }
8808: return $canclone;
8809: }
8810:
1.679 raeburn 8811: # ------------------------------------------------------- Course Group routines
8812:
8813: sub get_coursegroups {
1.809 raeburn 8814: my ($cdom,$cnum,$group,$namespace) = @_;
8815: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 8816: }
8817:
1.679 raeburn 8818: sub modify_coursegroup {
8819: my ($cdom,$cnum,$groupsettings) = @_;
8820: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
8821: }
8822:
1.809 raeburn 8823: sub toggle_coursegroup_status {
8824: my ($cdom,$cnum,$group,$action) = @_;
8825: my ($from_namespace,$to_namespace);
8826: if ($action eq 'delete') {
8827: $from_namespace = 'coursegroups';
8828: $to_namespace = 'deleted_groups';
8829: } else {
8830: $from_namespace = 'deleted_groups';
8831: $to_namespace = 'coursegroups';
8832: }
8833: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 8834: if (my $tmp = &error(%curr_group)) {
8835: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
8836: return ('read error',$tmp);
8837: } else {
8838: my %savedsettings = %curr_group;
1.809 raeburn 8839: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 8840: my $deloutcome;
8841: if ($result eq 'ok') {
1.809 raeburn 8842: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 8843: } else {
8844: return ('write error',$result);
8845: }
8846: if ($deloutcome eq 'ok') {
8847: return 'ok';
8848: } else {
8849: return ('delete error',$deloutcome);
8850: }
8851: }
8852: }
8853:
1.679 raeburn 8854: sub modify_group_roles {
1.957 raeburn 8855: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 8856: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
8857: my $role = 'gr/'.&escape($userprivs);
8858: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 8859: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 8860: if ($result eq 'ok') {
8861: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
8862: }
1.679 raeburn 8863: return $result;
8864: }
8865:
8866: sub modify_coursegroup_membership {
8867: my ($cdom,$cnum,$membership) = @_;
8868: my $result = &put('groupmembership',$membership,$cdom,$cnum);
8869: return $result;
8870: }
8871:
1.682 raeburn 8872: sub get_active_groups {
8873: my ($udom,$uname,$cdom,$cnum) = @_;
8874: my $now = time;
8875: my %groups = ();
8876: foreach my $key (keys(%env)) {
1.811 albertel 8877: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 8878: my ($start,$end) = split(/\./,$env{$key});
8879: if (($end!=0) && ($end<$now)) { next; }
8880: if (($start!=0) && ($start>$now)) { next; }
8881: if ($1 eq $cdom && $2 eq $cnum) {
8882: $groups{$3} = $env{$key} ;
8883: }
8884: }
8885: }
8886: return %groups;
8887: }
8888:
1.683 raeburn 8889: sub get_group_membership {
8890: my ($cdom,$cnum,$group) = @_;
8891: return(&dump('groupmembership',$cdom,$cnum,$group));
8892: }
8893:
8894: sub get_users_groups {
8895: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 8896: my @usersgroups;
1.683 raeburn 8897: my $cachetime=1800;
8898:
8899: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 8900: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
8901: if (defined($cached)) {
1.734 albertel 8902: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 8903: } else {
8904: $grouplist = '';
1.816 raeburn 8905: my $courseurl = &courseid_to_courseurl($courseid);
1.1166 raeburn 8906: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 8907: my $access_end = $env{'course.'.$courseid.
8908: '.default_enrollment_end_date'};
8909: my $now = time;
8910: foreach my $key (keys(%roleshash)) {
8911: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
8912: my $group = $1;
8913: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
8914: my $start = $2;
8915: my $end = $1;
8916: if ($start == -1) { next; } # deleted from group
8917: if (($start!=0) && ($start>$now)) { next; }
8918: if (($end!=0) && ($end<$now)) {
8919: if ($access_end && $access_end < $now) {
8920: if ($access_end - $end < 86400) {
8921: push(@usersgroups,$group);
1.733 raeburn 8922: }
8923: }
1.817 raeburn 8924: next;
1.733 raeburn 8925: }
1.817 raeburn 8926: push(@usersgroups,$group);
1.683 raeburn 8927: }
8928: }
8929: }
1.817 raeburn 8930: @usersgroups = &sort_course_groups($courseid,@usersgroups);
8931: $grouplist = join(':',@usersgroups);
8932: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 8933: }
1.733 raeburn 8934: return @usersgroups;
1.683 raeburn 8935: }
8936:
8937: sub devalidate_getgroups_cache {
8938: my ($udom,$uname,$cdom,$cnum)=@_;
8939: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 8940:
1.683 raeburn 8941: my $hashid="$udom:$uname:$courseid";
8942: &devalidate_cache_new('getgroups',$hashid);
8943: }
8944:
1.12 www 8945: # ------------------------------------------------------------------ Plain Text
8946:
8947: sub plaintext {
1.988 raeburn 8948: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 8949: if ($short =~ m{^cr/}) {
1.758 albertel 8950: return (split('/',$short))[-1];
8951: }
1.742 raeburn 8952: if (!defined($cid)) {
8953: $cid = $env{'request.course.id'};
8954: }
8955: my %rolenames = (
1.1008 raeburn 8956: Course => 'std',
8957: Community => 'alt1',
1.1305 raeburn 8958: Placement => 'std',
1.742 raeburn 8959: );
1.1037 raeburn 8960: if ($cid ne '') {
8961: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
8962: unless ($forcedefault) {
8963: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
8964: &Apache::lonlocal::mt_escape(\$roletext);
8965: return &Apache::lonlocal::mt($roletext);
8966: }
8967: }
8968: }
8969: if ((defined($type)) && (defined($rolenames{$type})) &&
8970: (defined($rolenames{$type})) &&
8971: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 8972: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 8973: } elsif ($cid ne '') {
8974: my $crstype = $env{'course.'.$cid.'.type'};
8975: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
8976: (defined($prp{$short}{$rolenames{$crstype}}))) {
8977: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
8978: }
1.742 raeburn 8979: }
1.1037 raeburn 8980: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 8981: }
8982:
8983: # ----------------------------------------------------------------- Assign Role
8984:
8985: sub assignrole {
1.957 raeburn 8986: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
8987: $context)=@_;
1.21 www 8988: my $mrole;
8989: if ($role =~ /^cr\//) {
1.393 www 8990: my $cwosec=$url;
1.811 albertel 8991: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 8992: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 8993: my $refused = 1;
8994: if ($context eq 'requestcourses') {
8995: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
8996: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
8997: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
8998: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
8999: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9000: if ($crsenv{'internal.courseowner'} eq
9001: $env{'user.name'}.':'.$env{'user.domain'}) {
9002: $refused = '';
9003: }
9004: }
9005: }
9006: }
9007: }
9008: if ($refused) {
9009: &logthis('Refused custom assignrole: '.
9010: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
9011: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
9012: return 'refused';
9013: }
1.104 www 9014: }
1.21 www 9015: $mrole='cr';
1.678 raeburn 9016: } elsif ($role =~ /^gr\//) {
9017: my $cwogrp=$url;
1.811 albertel 9018: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 9019: unless (&allowed('mdg',$cwogrp)) {
9020: &logthis('Refused group assignrole: '.
9021: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
9022: $env{'user.name'}.' at '.$env{'user.domain'});
9023: return 'refused';
9024: }
9025: $mrole='gr';
1.21 www 9026: } else {
1.82 www 9027: my $cwosec=$url;
1.811 albertel 9028: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 9029: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
9030: my $refused;
9031: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
9032: if (!(&allowed('c'.$role,$url))) {
9033: $refused = 1;
9034: }
9035: } else {
9036: $refused = 1;
9037: }
1.947 raeburn 9038: if ($refused) {
1.1045 raeburn 9039: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
9040: if (!$selfenroll && $context eq 'course') {
9041: my %crsenv;
9042: if ($role eq 'cc' || $role eq 'co') {
9043: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9044: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
9045: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
9046: if ($crsenv{'internal.courseowner'} eq
9047: $env{'user.name'}.':'.$env{'user.domain'}) {
9048: $refused = '';
9049: }
9050: }
9051: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
9052: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
9053: if ($crsenv{'internal.courseowner'} eq
9054: $env{'user.name'}.':'.$env{'user.domain'}) {
9055: $refused = '';
9056: }
9057: }
9058: }
9059: }
9060: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 9061: $refused = '';
1.1017 raeburn 9062: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 9063: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 9064: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 9065: my $wrongcc;
9066: if ($cnum =~ /^$match_community$/) {
9067: $wrongcc = 1 if ($role eq 'cc');
9068: } else {
9069: $wrongcc = 1 if ($role eq 'co');
9070: }
9071: unless ($wrongcc) {
9072: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
9073: if ($crsenv{'internal.courseowner'} eq
9074: $env{'user.name'}.':'.$env{'user.domain'}) {
9075: $refused = '';
9076: }
1.1017 raeburn 9077: }
9078: }
1.1183 raeburn 9079: } elsif ($context eq 'requestauthor') {
9080: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
9081: ($url eq '/'.$udom.'/') && ($role eq 'au')) {
9082: if ($env{'environment.requestauthor'} eq 'automatic') {
9083: $refused = '';
9084: } else {
9085: my %domdefaults = &get_domain_defaults($udom);
9086: if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
9087: my $checkbystatus;
9088: if ($env{'user.adv'}) {
9089: my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
9090: if ($disposition eq 'automatic') {
9091: $refused = '';
9092: } elsif ($disposition eq '') {
9093: $checkbystatus = 1;
9094: }
9095: } else {
9096: $checkbystatus = 1;
9097: }
9098: if ($checkbystatus) {
9099: if ($env{'environment.inststatus'}) {
9100: my @inststatuses = split(/,/,$env{'environment.inststatus'});
9101: foreach my $type (@inststatuses) {
9102: if (($type ne '') &&
9103: ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
9104: $refused = '';
9105: }
9106: }
9107: } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
9108: $refused = '';
9109: }
9110: }
9111: }
9112: }
9113: }
1.1017 raeburn 9114: }
9115: if ($refused) {
1.947 raeburn 9116: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
9117: ' '.$role.' '.$end.' '.$start.' by '.
9118: $env{'user.name'}.' at '.$env{'user.domain'});
9119: return 'refused';
9120: }
1.932 raeburn 9121: }
1.1131 raeburn 9122: } elsif ($role eq 'au') {
9123: if ($url ne '/'.$udom.'/') {
9124: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
9125: ' to assign author role for '.$uname.':'.$udom.
9126: ' in domain: '.$url.' refused (wrong domain).');
9127: return 'refused';
9128: }
1.104 www 9129: }
1.21 www 9130: $mrole=$role;
9131: }
1.620 albertel 9132: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 9133: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 9134: if ($end) { $command.='_'.$end; }
1.21 www 9135: if ($start) {
9136: if ($end) {
1.81 www 9137: $command.='_'.$start;
1.21 www 9138: } else {
1.81 www 9139: $command.='_0_'.$start;
1.21 www 9140: }
9141: }
1.739 raeburn 9142: my $origstart = $start;
9143: my $origend = $end;
1.957 raeburn 9144: my $delflag;
1.357 www 9145: # actually delete
9146: if ($deleteflag) {
1.373 www 9147: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 9148: # modify command to delete the role
1.620 albertel 9149: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 9150: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 9151: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 9152: # set start and finish to negative values for userrolelog
9153: $start=-1;
9154: $end=-1;
1.957 raeburn 9155: $delflag = 1;
1.357 www 9156: }
9157: }
9158: # send command
1.349 www 9159: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 9160: # log new user role if status is ok
1.349 www 9161: if ($answer eq 'ok') {
1.663 raeburn 9162: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184 raeburn 9163: if (($role eq 'cc') || ($role eq 'in') ||
9164: ($role eq 'ep') || ($role eq 'ad') ||
9165: ($role eq 'ta') || ($role eq 'st') ||
9166: ($role=~/^cr/) || ($role eq 'gr') ||
9167: ($role eq 'co')) {
1.1200 raeburn 9168: # for course roles, perform group memberships changes triggered by role change.
9169: unless ($role =~ /^gr/) {
9170: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
9171: $origstart,$selfenroll,$context);
9172: }
1.1184 raeburn 9173: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9174: $selfenroll,$context);
9175: } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334 raeburn 9176: ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
9177: ($role eq 'da')) {
1.1184 raeburn 9178: &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9179: $context);
9180: } elsif (($role eq 'ca') || ($role eq 'aa')) {
9181: &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
9182: $context);
9183: }
1.1053 raeburn 9184: if ($role eq 'cc') {
9185: &autoupdate_coowners($url,$end,$start,$uname,$udom);
9186: }
1.349 www 9187: }
9188: return $answer;
1.169 harris41 9189: }
9190:
1.1053 raeburn 9191: sub autoupdate_coowners {
9192: my ($url,$end,$start,$uname,$udom) = @_;
9193: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
9194: if (($cdom ne '') && ($cnum ne '')) {
9195: my $now = time;
9196: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
9197: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
9198: my %coursehash = &coursedescription($cdom.'_'.$cnum);
9199: my $instcode = $coursehash{'internal.coursecode'};
9200: if ($instcode ne '') {
1.1056 raeburn 9201: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
9202: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 9203: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 9204: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
9205: if ($result eq 'valid') {
9206: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 9207: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
9208: push(@newcoowners,$coowner);
9209: }
9210: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
9211: push(@newcoowners,$uname.':'.$udom);
9212: }
9213: @newcoowners = sort(@newcoowners);
9214: } else {
9215: push(@newcoowners,$uname.':'.$udom);
9216: }
9217: } else {
9218: if ($coursehash{'internal.co-owners'}) {
9219: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
9220: unless ($coowner eq $uname.':'.$udom) {
9221: push(@newcoowners,$coowner);
9222: }
9223: }
9224: unless (@newcoowners > 0) {
9225: $delcoowners = 1;
9226: $coowners = '';
9227: }
9228: }
9229: }
9230: if (@newcoowners || $delcoowners) {
9231: &store_coowners($cdom,$cnum,$coursehash{'home'},
9232: $delcoowners,@newcoowners);
9233: }
9234: }
9235: }
9236: }
9237: }
9238: }
9239: }
9240:
9241: sub store_coowners {
9242: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
9243: my $cid = $cdom.'_'.$cnum;
9244: my ($coowners,$delresult,$putresult);
9245: if (@newcoowners) {
9246: $coowners = join(',',@newcoowners);
9247: my %coownershash = (
9248: 'internal.co-owners' => $coowners,
9249: );
9250: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
9251: if ($putresult eq 'ok') {
9252: if ($env{'course.'.$cid.'.num'} eq $cnum) {
9253: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
9254: }
9255: }
9256: }
9257: if ($delcoowners) {
9258: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
9259: if ($delresult eq 'ok') {
9260: if ($env{'course.'.$cid.'.internal.co-owners'}) {
9261: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
9262: }
9263: }
9264: }
9265: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
9266: my %crsinfo =
9267: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
9268: if (ref($crsinfo{$cid}) eq 'HASH') {
9269: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
9270: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
9271: }
9272: }
9273: }
9274:
1.169 harris41 9275: # -------------------------------------------------- Modify user authentication
1.197 www 9276: # Overrides without validation
9277:
1.169 harris41 9278: sub modifyuserauth {
9279: my ($udom,$uname,$umode,$upass)=@_;
9280: my $uhome=&homeserver($uname,$udom);
1.197 www 9281: unless (&allowed('mau',$udom)) { return 'refused'; }
9282: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 9283: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
9284: ' in domain '.$env{'request.role.domain'});
1.169 harris41 9285: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
9286: &escape($upass),$uhome);
1.620 albertel 9287: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 9288: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
9289: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
9290: &log($udom,,$uname,$uhome,
1.620 albertel 9291: 'Authentication changed by '.$env{'user.domain'}.', '.
9292: $env{'user.name'}.', '.$umode.
1.197 www 9293: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 9294: unless ($reply eq 'ok') {
1.197 www 9295: &logthis('Authentication mode error: '.$reply);
1.169 harris41 9296: return 'error: '.$reply;
9297: }
1.170 harris41 9298: return 'ok';
1.80 www 9299: }
9300:
1.81 www 9301: # --------------------------------------------------------------- Modify a user
1.80 www 9302:
1.81 www 9303: sub modifyuser {
1.206 matthew 9304: my ($udom, $uname, $uid,
9305: $umode, $upass, $first,
9306: $middle, $last, $gene,
1.1058 raeburn 9307: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 9308: $udom= &LONCAPA::clean_domain($udom);
9309: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 9310: my $showcandelete = 'none';
9311: if (ref($candelete) eq 'ARRAY') {
9312: if (@{$candelete} > 0) {
9313: $showcandelete = join(', ',@{$candelete});
9314: }
9315: }
1.81 www 9316: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 9317: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 9318: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 9319: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
9320: ' desiredhome not specified').
1.620 albertel 9321: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
9322: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 9323: my $uhome=&homeserver($uname,$udom,'true');
1.1075 raeburn 9324: my $newuser;
9325: if ($uhome eq 'no_host') {
9326: $newuser = 1;
9327: }
1.80 www 9328: # ----------------------------------------------------------------- Create User
1.406 albertel 9329: if (($uhome eq 'no_host') &&
9330: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 9331: my $unhome='';
1.844 albertel 9332: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 9333: $unhome = $desiredhome;
1.620 albertel 9334: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
9335: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 9336: } else { # load balancing routine for determining $unhome
1.81 www 9337: my $loadm=10000000;
1.841 albertel 9338: my %servers = &get_servers($udom,'library');
9339: foreach my $tryserver (keys(%servers)) {
9340: my $answer=reply('load',$tryserver);
9341: if (($answer=~/\d+/) && ($answer<$loadm)) {
9342: $loadm=$answer;
9343: $unhome=$tryserver;
9344: }
1.80 www 9345: }
9346: }
9347: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 9348: return 'error: unable to find a home server for '.$uname.
9349: ' in domain '.$udom;
1.80 www 9350: }
9351: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
9352: &escape($upass),$unhome);
9353: unless ($reply eq 'ok') {
9354: return 'error: '.$reply;
9355: }
1.230 stredwic 9356: $uhome=&homeserver($uname,$udom,'true');
1.80 www 9357: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 9358: return 'error: unable verify users home machine.';
1.80 www 9359: }
1.209 matthew 9360: } # End of creation of new user
1.80 www 9361: # ---------------------------------------------------------------------- Add ID
9362: if ($uid) {
9363: $uid=~tr/A-Z/a-z/;
9364: my %uidhash=&idrget($udom,$uname);
1.196 www 9365: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
9366: && (!$forceid)) {
1.80 www 9367: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 9368: return 'error: user id "'.$uid.'" does not match '.
9369: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 9370: }
9371: } else {
1.1300 raeburn 9372: &idput($udom,{$uname => $uid},$uhome,'ids');
1.80 www 9373: }
9374: }
9375: # -------------------------------------------------------------- Add names, etc
1.313 matthew 9376: my @tmp=&get('environment',
1.899 raeburn 9377: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 9378: 'permanentemail','inststatus'],
1.134 albertel 9379: $udom,$uname);
1.1075 raeburn 9380: my (%names,%oldnames);
1.313 matthew 9381: if ($tmp[0] =~ m/^error:.*/) {
9382: %names=();
9383: } else {
9384: %names = @tmp;
1.1075 raeburn 9385: %oldnames = %names;
1.313 matthew 9386: }
1.388 www 9387: #
1.1058 raeburn 9388: # If name, email and/or uid are blank (e.g., because an uploaded file
9389: # of users did not contain them), do not overwrite existing values
9390: # unless field is in $candelete array ref.
9391: #
9392:
9393: my @fields = ('firstname','middlename','lastname','generation',
9394: 'permanentemail','id');
9395: my %newvalues;
9396: if (ref($candelete) eq 'ARRAY') {
9397: foreach my $field (@fields) {
9398: if (grep(/^\Q$field\E$/,@{$candelete})) {
9399: if ($field eq 'firstname') {
9400: $names{$field} = $first;
9401: } elsif ($field eq 'middlename') {
9402: $names{$field} = $middle;
9403: } elsif ($field eq 'lastname') {
9404: $names{$field} = $last;
9405: } elsif ($field eq 'generation') {
9406: $names{$field} = $gene;
9407: } elsif ($field eq 'permanentemail') {
9408: $names{$field} = $email;
9409: } elsif ($field eq 'id') {
9410: $names{$field} = $uid;
9411: }
9412: }
9413: }
9414: }
1.388 www 9415: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 9416: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 9417: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 9418: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 9419: if ($email) {
9420: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 9421: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 9422: }
1.899 raeburn 9423: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 9424: if (defined($inststatus)) {
9425: $names{'inststatus'} = '';
9426: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
9427: if (ref($usertypes) eq 'HASH') {
9428: my @okstatuses;
9429: foreach my $item (split(/:/,$inststatus)) {
9430: if (defined($usertypes->{$item})) {
9431: push(@okstatuses,$item);
9432: }
9433: }
9434: if (@okstatuses) {
9435: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
9436: }
9437: }
9438: }
1.1075 raeburn 9439: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 9440: $umode.', '.$first.', '.$middle.', '.
1.1075 raeburn 9441: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 9442: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
9443: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
9444: } else {
9445: $logmsg .= ' during self creation';
9446: }
1.1075 raeburn 9447: my $changed;
9448: if ($newuser) {
9449: $changed = 1;
9450: } else {
9451: foreach my $field (@fields) {
9452: if ($names{$field} ne $oldnames{$field}) {
9453: $changed = 1;
9454: last;
9455: }
9456: }
9457: }
9458: unless ($changed) {
9459: $logmsg = 'No changes in user information needed for: '.$logmsg;
9460: &logthis($logmsg);
9461: return 'ok';
9462: }
9463: my $reply = &put('environment', \%names, $udom,$uname);
9464: if ($reply ne 'ok') {
9465: return 'error: '.$reply;
9466: }
1.1087 raeburn 9467: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
9468: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
9469: }
1.1075 raeburn 9470: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
9471: &devalidate_cache_new('namescache',$uname.':'.$udom);
9472: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 9473: &logthis($logmsg);
1.134 albertel 9474: return 'ok';
1.80 www 9475: }
9476:
1.81 www 9477: # -------------------------------------------------------------- Modify student
1.80 www 9478:
1.81 www 9479: sub modifystudent {
9480: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 9481: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314 raeburn 9482: $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455 albertel 9483: if (!$cid) {
1.620 albertel 9484: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 9485: return 'not_in_class';
9486: }
1.80 www 9487: }
9488: # --------------------------------------------------------------- Make the user
1.81 www 9489: my $reply=&modifyuser
1.209 matthew 9490: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 9491: $desiredhome,$email,$inststatus);
1.80 www 9492: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 9493: # This will cause &modify_student_enrollment to get the uid from the
1.1235 raeburn 9494: # student's environment
1.297 matthew 9495: $uid = undef if (!$forceid);
1.455 albertel 9496: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216 raeburn 9497: $gene,$usec,$end,$start,$type,$locktype,
1.1314 raeburn 9498: $cid,$selfenroll,$context,$credits,$instsec);
1.297 matthew 9499: return $reply;
9500: }
9501:
9502: sub modify_student_enrollment {
1.1216 raeburn 9503: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314 raeburn 9504: $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455 albertel 9505: my ($cdom,$cnum,$chome);
9506: if (!$cid) {
1.620 albertel 9507: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 9508: return 'not_in_class';
9509: }
1.620 albertel 9510: $cdom=$env{'course.'.$cid.'.domain'};
9511: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 9512: } else {
9513: ($cdom,$cnum)=split(/_/,$cid);
9514: }
1.620 albertel 9515: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 9516: if (!$chome) {
1.457 raeburn 9517: $chome=&homeserver($cnum,$cdom);
1.297 matthew 9518: }
1.455 albertel 9519: if (!$chome) { return 'unknown_course'; }
1.297 matthew 9520: # Make sure the user exists
1.81 www 9521: my $uhome=&homeserver($uname,$udom);
9522: if (($uhome eq '') || ($uhome eq 'no_host')) {
9523: return 'error: no such user';
9524: }
1.297 matthew 9525: # Get student data if we were not given enough information
9526: if (!defined($first) || $first eq '' ||
9527: !defined($last) || $last eq '' ||
9528: !defined($uid) || $uid eq '' ||
9529: !defined($middle) || $middle eq '' ||
9530: !defined($gene) || $gene eq '') {
1.294 matthew 9531: # They did not supply us with enough data to enroll the student, so
9532: # we need to pick up more information.
1.297 matthew 9533: my %tmp = &get('environment',
1.294 matthew 9534: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 9535: ,$udom,$uname);
9536:
1.800 albertel 9537: #foreach my $key (keys(%tmp)) {
9538: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 9539: #}
1.294 matthew 9540: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
9541: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
9542: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 9543: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 9544: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
9545: }
1.556 albertel 9546: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148 raeburn 9547: my $user = "$uname:$udom";
9548: my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487 albertel 9549: my $reply=cput('classlist',
1.1148 raeburn 9550: {$user =>
1.1314 raeburn 9551: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487 albertel 9552: $cdom,$cnum);
1.1148 raeburn 9553: if (($reply eq 'ok') || ($reply eq 'delayed')) {
9554: &devalidate_getsection_cache($udom,$uname,$cid);
9555: } else {
1.81 www 9556: return 'error: '.$reply;
9557: }
1.297 matthew 9558: # Add student role to user
1.83 www 9559: my $uurl='/'.$cid;
1.81 www 9560: $uurl=~s/\_/\//g;
9561: if ($usec) {
9562: $uurl.='/'.$usec;
9563: }
1.1148 raeburn 9564: my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
9565: $selfenroll,$context);
9566: if ($result ne 'ok') {
9567: if ($old_entry{$user} ne '') {
9568: $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
9569: } else {
9570: $reply = &del('classlist',[$user],$cdom,$cnum);
9571: }
9572: }
9573: return $result;
1.21 www 9574: }
9575:
1.556 albertel 9576: sub format_name {
9577: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
9578: my $name;
9579: if ($first ne 'lastname') {
9580: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
9581: } else {
9582: if ($lastname=~/\S/) {
9583: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
9584: $name=~s/\s+,/,/;
9585: } else {
9586: $name.= $firstname.' '.$middlename.' '.$generation;
9587: }
9588: }
9589: $name=~s/^\s+//;
9590: $name=~s/\s+$//;
9591: $name=~s/\s+/ /g;
9592: return $name;
9593: }
9594:
1.84 www 9595: # ------------------------------------------------- Write to course preferences
9596:
9597: sub writecoursepref {
9598: my ($courseid,%prefs)=@_;
9599: $courseid=~s/^\///;
9600: $courseid=~s/\_/\//g;
9601: my ($cdomain,$cnum)=split(/\//,$courseid);
9602: my $chome=homeserver($cnum,$cdomain);
9603: if (($chome eq '') || ($chome eq 'no_host')) {
9604: return 'error: no such course';
9605: }
9606: my $cstring='';
1.800 albertel 9607: foreach my $pref (keys(%prefs)) {
9608: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 9609: }
1.84 www 9610: $cstring=~s/\&$//;
9611: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
9612: }
9613:
9614: # ---------------------------------------------------------- Make/modify course
9615:
9616: sub createcourse {
1.741 raeburn 9617: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 9618: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 9619: $url=&declutter($url);
9620: my $cid='';
1.1028 raeburn 9621: if ($context eq 'requestcourses') {
9622: my $can_create = 0;
9623: my ($ownername,$ownerdom) = split(':',$course_owner);
9624: if ($udom eq $ownerdom) {
9625: if (&usertools_access($ownername,$ownerdom,$category,undef,
9626: $context)) {
9627: $can_create = 1;
9628: }
9629: } else {
9630: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
9631: $category);
9632: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
9633: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
9634: if (@curr > 0) {
9635: my @options = qw(approval validate autolimit);
9636: my $optregex = join('|',@options);
9637: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
9638: $can_create = 1;
9639: }
9640: }
9641: }
9642: }
9643: if ($can_create) {
9644: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
9645: unless (&allowed('ccc',$udom)) {
9646: return 'refused';
9647: }
1.1017 raeburn 9648: }
9649: } else {
9650: return 'refused';
9651: }
1.1028 raeburn 9652: } elsif (!&allowed('ccc',$udom)) {
9653: return 'refused';
1.84 www 9654: }
1.1011 raeburn 9655: # --------------------------------------------------------------- Get Unique ID
9656: my $uname;
9657: if ($cnum =~ /^$match_courseid$/) {
9658: my $chome=&homeserver($cnum,$udom,'true');
9659: if (($chome eq '') || ($chome eq 'no_host')) {
9660: $uname = $cnum;
9661: } else {
1.1038 raeburn 9662: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 9663: }
9664: } else {
1.1038 raeburn 9665: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 9666: }
9667: return $uname if ($uname =~ /^error/);
9668: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 9669: if (!defined($course_server)) {
9670: if (defined(&domain($udom,'primary'))) {
9671: $course_server = &domain($udom,'primary');
9672: } else {
9673: $course_server = $env{'user.home'};
9674: }
9675: }
9676: my %host_servers =
9677: &Apache::lonnet::get_servers($udom,'library');
9678: unless ($host_servers{$course_server}) {
9679: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 9680: }
1.84 www 9681: # ------------------------------------------------------------- Make the course
9682: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 9683: $course_server);
1.84 www 9684: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 9685: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 9686: if (($uhome eq '') || ($uhome eq 'no_host')) {
9687: return 'error: no such course';
9688: }
1.271 www 9689: # ----------------------------------------------------------------- Course made
1.516 raeburn 9690: # log existence
1.1029 raeburn 9691: my $now = time;
1.918 raeburn 9692: my $newcourse = {
9693: $udom.'_'.$uname => {
1.921 raeburn 9694: description => $description,
9695: inst_code => $inst_code,
9696: owner => $course_owner,
9697: type => $crstype,
1.1029 raeburn 9698: creator => $env{'user.name'}.':'.
9699: $env{'user.domain'},
9700: created => $now,
9701: context => $context,
1.918 raeburn 9702: },
9703: };
1.921 raeburn 9704: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 9705: # set toplevel url
1.271 www 9706: my $topurl=$url;
9707: unless ($nonstandard) {
9708: # ------------------------------------------ For standard courses, make top url
9709: my $mapurl=&clutter($url);
1.278 www 9710: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 9711: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 9712: <map>
9713: <resource id="1" type="start"></resource>
9714: <resource id="2" src="$mapurl"></resource>
9715: <resource id="3" type="finish"></resource>
9716: <link index="1" from="1" to="2"></link>
9717: <link index="2" from="2" to="3"></link>
9718: </map>
9719: ENDINITMAP
9720: $topurl=&declutter(
1.638 albertel 9721: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 9722: );
9723: }
9724: # ----------------------------------------------------------- Write preferences
1.84 www 9725: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 9726: ('description' => $description,
9727: 'url' => $topurl,
9728: 'internal.creator' => $env{'user.name'}.':'.
9729: $env{'user.domain'},
9730: 'internal.created' => $now,
9731: 'internal.creationcontext' => $context)
9732: );
1.84 www 9733: return '/'.$udom.'/'.$uname;
9734: }
9735:
1.1011 raeburn 9736: # ------------------------------------------------------------------- Create ID
9737: sub generate_coursenum {
1.1038 raeburn 9738: my ($udom,$crstype) = @_;
1.1011 raeburn 9739: my $domdesc = &domain($udom);
9740: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 9741: my $first;
9742: if ($crstype eq 'Community') {
9743: $first = '0';
9744: } else {
9745: $first = int(1+rand(9));
9746: }
9747: my $uname=$first.
1.1011 raeburn 9748: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
9749: substr($$.time,0,5).unpack("H8",pack("I32",time)).
9750: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
9751: # ----------------------------------------------- Make sure that does not exist
9752: my $uhome=&homeserver($uname,$udom,'true');
9753: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 9754: if ($crstype eq 'Community') {
9755: $first = '0';
9756: } else {
9757: $first = int(1+rand(9));
9758: }
9759: $uname=$first.
1.1011 raeburn 9760: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
9761: substr($$.time,0,5).unpack("H8",pack("I32",time)).
9762: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
9763: $uhome=&homeserver($uname,$udom,'true');
9764: unless (($uhome eq '') || ($uhome eq 'no_host')) {
9765: return 'error: unable to generate unique course-ID';
9766: }
9767: }
9768: return $uname;
9769: }
9770:
1.813 albertel 9771: sub is_course {
1.1167 droeschl 9772: my ($cdom, $cnum) = scalar(@_) == 1 ?
9773: ($_[0] =~ /^($match_domain)_($match_courseid)$/) : @_;
9774:
9775: return unless $cdom and $cnum;
9776:
9777: my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
9778: '.');
9779:
1.1219 raeburn 9780: return unless(exists($courses{$cdom.'_'.$cnum}));
1.1167 droeschl 9781: return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813 albertel 9782: }
9783:
1.1015 raeburn 9784: sub store_userdata {
9785: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 9786: my $result;
1.1016 raeburn 9787: if ($datakey ne '') {
1.1013 raeburn 9788: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 9789: if ($udom eq '' || $uname eq '') {
9790: $udom = $env{'user.domain'};
9791: $uname = $env{'user.name'};
9792: }
9793: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 9794: if (($uhome eq '') || ($uhome eq 'no_host')) {
9795: $result = 'error: no_host';
9796: } else {
9797: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
9798: $storehash->{'host'} = $perlvar{'lonHostID'};
9799:
9800: my $namevalue='';
9801: foreach my $key (keys(%{$storehash})) {
9802: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
9803: }
9804: $namevalue=~s/\&$//;
1.1224 raeburn 9805: unless ($namespace eq 'courserequests') {
9806: $datakey = &escape($datakey);
9807: }
1.1105 raeburn 9808: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
9809: $namevalue,$uhome);
1.1013 raeburn 9810: }
9811: } else {
9812: $result = 'error: data to store was not a hash reference';
9813: }
9814: } else {
9815: $result= 'error: invalid requestkey';
9816: }
9817: return $result;
9818: }
9819:
1.21 www 9820: # ---------------------------------------------------------- Assign Custom Role
9821:
9822: sub assigncustomrole {
1.957 raeburn 9823: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9824: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 9825: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 9826: }
9827:
9828: # ----------------------------------------------------------------- Revoke Role
9829:
9830: sub revokerole {
1.957 raeburn 9831: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9832: my $now=time;
1.965 raeburn 9833: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 9834: }
9835:
9836: # ---------------------------------------------------------- Revoke Custom Role
9837:
9838: sub revokecustomrole {
1.957 raeburn 9839: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 9840: my $now=time;
1.357 www 9841: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 9842: $deleteflag,$selfenroll,$context);
1.17 www 9843: }
9844:
1.533 banghart 9845: # ------------------------------------------------------------ Disk usage
1.535 albertel 9846: sub diskusage {
1.955 raeburn 9847: my ($udom,$uname,$directorypath,$getpropath)=@_;
9848: $directorypath =~ s/\/$//;
9849: my $listing=&reply('du2:'.&escape($directorypath).':'
9850: .&escape($getpropath).':'.&escape($uname).':'
9851: .&escape($udom),homeserver($uname,$udom));
9852: if ($listing eq 'unknown_cmd') {
9853: if ($getpropath) {
9854: $directorypath = &propath($udom,$uname).'/'.$directorypath;
9855: }
9856: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
9857: }
1.514 albertel 9858: return $listing;
1.512 banghart 9859: }
9860:
1.566 banghart 9861: sub is_locked {
1.1096 raeburn 9862: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 9863: my @check;
9864: my $is_locked;
1.1093 raeburn 9865: push (@check,$file_name);
1.613 albertel 9866: my %locked = &get('file_permissions',\@check,
1.620 albertel 9867: $env{'user.domain'},$env{'user.name'});
1.615 albertel 9868: my ($tmp)=keys(%locked);
9869: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 9870:
1.566 banghart 9871: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 9872: $is_locked = 'false';
9873: foreach my $entry (@{$locked{$file_name}}) {
1.1096 raeburn 9874: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 9875: $is_locked = 'true';
1.1096 raeburn 9876: if (ref($which) eq 'ARRAY') {
9877: push(@{$which},$entry);
9878: } else {
9879: last;
9880: }
1.745 raeburn 9881: }
9882: }
1.566 banghart 9883: } else {
9884: $is_locked = 'false';
9885: }
1.1093 raeburn 9886: return $is_locked;
1.566 banghart 9887: }
9888:
1.759 albertel 9889: sub declutter_portfile {
9890: my ($file) = @_;
1.833 albertel 9891: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 9892: return $file;
9893: }
9894:
1.559 banghart 9895: # ------------------------------------------------------------- Mark as Read Only
9896:
9897: sub mark_as_readonly {
9898: my ($domain,$user,$files,$what) = @_;
1.613 albertel 9899: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 9900: my ($tmp)=keys(%current_permissions);
9901: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 9902: foreach my $file (@{$files}) {
1.759 albertel 9903: $file = &declutter_portfile($file);
1.561 banghart 9904: push(@{$current_permissions{$file}},$what);
1.559 banghart 9905: }
1.613 albertel 9906: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 9907: return;
9908: }
9909:
1.572 banghart 9910: # ------------------------------------------------------------Save Selected Files
9911:
9912: sub save_selected_files {
9913: my ($user, $path, @files) = @_;
9914: my $filename = $user."savedfiles";
1.573 banghart 9915: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 9916: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 9917: foreach my $file (@files) {
1.620 albertel 9918: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 9919: }
9920: foreach my $file (@other_files) {
1.574 banghart 9921: print (OUT $file."\n");
1.572 banghart 9922: }
1.574 banghart 9923: close (OUT);
1.572 banghart 9924: return 'ok';
9925: }
9926:
1.574 banghart 9927: sub clear_selected_files {
9928: my ($user) = @_;
9929: my $filename = $user."savedfiles";
1.1117 foxr 9930: open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574 banghart 9931: print (OUT undef);
9932: close (OUT);
9933: return ("ok");
9934: }
9935:
1.572 banghart 9936: sub files_in_path {
9937: my ($user, $path) = @_;
9938: my $filename = $user."savedfiles";
9939: my %return_files;
1.1117 foxr 9940: open (IN, '<'.LONCAPA::tempdir().$filename);
1.573 banghart 9941: while (my $line_in = <IN>) {
1.574 banghart 9942: chomp ($line_in);
9943: my @paths_and_file = split (m!/!, $line_in);
9944: my $file_part = pop (@paths_and_file);
9945: my $path_part = join ('/', @paths_and_file);
1.573 banghart 9946: $path_part.='/';
9947: my $path_and_file = $path_part.$file_part;
9948: if ($path_part eq $path) {
9949: $return_files{$file_part}= 'selected';
9950: }
9951: }
1.574 banghart 9952: close (IN);
9953: return (\%return_files);
1.572 banghart 9954: }
9955:
9956: # called in portfolio select mode, to show files selected NOT in current directory
9957: sub files_not_in_path {
9958: my ($user, $path) = @_;
9959: my $filename = $user."savedfiles";
9960: my @return_files;
9961: my $path_part;
1.1117 foxr 9962: open(IN, '<'.LONCAPA::.$filename);
1.800 albertel 9963: while (my $line = <IN>) {
1.572 banghart 9964: #ok, I know it's clunky, but I want it to work
1.800 albertel 9965: my @paths_and_file = split(m|/|, $line);
9966: my $file_part = pop(@paths_and_file);
9967: chomp($file_part);
9968: my $path_part = join('/', @paths_and_file);
1.572 banghart 9969: $path_part .= '/';
9970: my $path_and_file = $path_part.$file_part;
9971: if ($path_part ne $path) {
1.800 albertel 9972: push(@return_files, ($path_and_file));
1.572 banghart 9973: }
9974: }
1.800 albertel 9975: close(OUT);
1.574 banghart 9976: return (@return_files);
1.572 banghart 9977: }
9978:
1.1273 raeburn 9979: #------------------------------Submitted/Handedback Portfolio Files Versioning
9980:
9981: sub portfiles_versioning {
9982: my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
9983: my $portfolio_root = '/userfiles/portfolio';
9984: return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
9985: foreach my $file (@{$portfiles}) {
9986: &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
9987: my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
9988: my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
9989: my $getpropath = 1;
9990: my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
9991: $stu_name,$getpropath);
9992: my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
9993: my $new_answer =
9994: &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
9995: if ($new_answer ne 'problem getting file') {
9996: push(@{$versioned_portfiles}, $directory.$new_answer);
9997: &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
9998: [$symb,$env{'request.course.id'},'graded']);
9999: }
10000: }
10001: }
10002:
10003: sub get_next_version {
10004: my ($answer_name, $answer_ext, $dir_list) = @_;
10005: my $version;
10006: if (ref($dir_list) eq 'ARRAY') {
10007: foreach my $row (@{$dir_list}) {
10008: my ($file) = split(/\&/,$row,2);
10009: my ($file_name,$file_version,$file_ext) =
10010: &file_name_version_ext($file);
10011: if (($file_name eq $answer_name) &&
10012: ($file_ext eq $answer_ext)) {
10013: # gets here if filename and extension match,
10014: # regardless of version
10015: if ($file_version ne '') {
10016: # a versioned file is found so save it for later
10017: if ($file_version > $version) {
10018: $version = $file_version;
10019: }
10020: }
10021: }
10022: }
10023: }
10024: $version ++;
10025: return($version);
10026: }
10027:
10028: sub version_selected_portfile {
10029: my ($domain,$stu_name,$directory,$file_name,$version) = @_;
10030: my ($answer_name,$answer_ver,$answer_ext) =
10031: &file_name_version_ext($file_name);
10032: my $new_answer;
10033: $env{'form.copy'} =
10034: &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
10035: if($env{'form.copy'} eq '-1') {
10036: $new_answer = 'problem getting file';
10037: } else {
10038: $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
10039: my $copy_result =
10040: &finishuserfileupload($stu_name,$domain,'copy',
10041: '/portfolio'.$directory.$new_answer);
10042: }
10043: undef($env{'form.copy'});
10044: return ($new_answer);
10045: }
10046:
10047: sub file_name_version_ext {
10048: my ($file)=@_;
10049: my @file_parts = split(/\./, $file);
10050: my ($name,$version,$ext);
10051: if (@file_parts > 1) {
10052: $ext=pop(@file_parts);
10053: if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
10054: $version=pop(@file_parts);
10055: }
10056: $name=join('.',@file_parts);
10057: } else {
10058: $name=join('.',@file_parts);
10059: }
10060: return($name,$version,$ext);
10061: }
10062:
1.745 raeburn 10063: #----------------------------------------------Get portfolio file permissions
1.629 banghart 10064:
1.745 raeburn 10065: sub get_portfile_permissions {
10066: my ($domain,$user) = @_;
1.613 albertel 10067: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 10068: my ($tmp)=keys(%current_permissions);
10069: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 10070: return \%current_permissions;
10071: }
10072:
10073: #---------------------------------------------Get portfolio file access controls
10074:
1.749 raeburn 10075: sub get_access_controls {
1.745 raeburn 10076: my ($current_permissions,$group,$file) = @_;
1.769 albertel 10077: my %access;
10078: my $real_file = $file;
10079: $file =~ s/\.meta$//;
1.745 raeburn 10080: if (defined($file)) {
1.749 raeburn 10081: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
10082: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 10083: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 10084: }
10085: }
1.745 raeburn 10086: } else {
1.749 raeburn 10087: foreach my $key (keys(%{$current_permissions})) {
10088: if ($key =~ /\0accesscontrol$/) {
10089: if (defined($group)) {
10090: if ($key !~ m-^\Q$group\E/-) {
10091: next;
10092: }
10093: }
10094: my ($fullpath) = split(/\0/,$key);
10095: if (ref($$current_permissions{$key}) eq 'HASH') {
10096: foreach my $control (keys(%{$$current_permissions{$key}})) {
10097: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
10098: }
10099: }
10100: }
10101: }
10102: }
10103: return %access;
10104: }
10105:
10106: sub modify_access_controls {
10107: my ($file_name,$changes,$domain,$user)=@_;
10108: my ($outcome,$deloutcome);
10109: my %store_permissions;
10110: my %new_values;
10111: my %new_control;
10112: my %translation;
10113: my @deletions = ();
10114: my $now = time;
10115: if (exists($$changes{'activate'})) {
10116: if (ref($$changes{'activate'}) eq 'HASH') {
10117: my @newitems = sort(keys(%{$$changes{'activate'}}));
10118: my $numnew = scalar(@newitems);
10119: for (my $i=0; $i<$numnew; $i++) {
10120: my $newkey = $newitems[$i];
10121: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 10122: if ($newkey =~ /^\d+:/) {
10123: $newkey =~ s/^(\d+)/$newid/;
10124: $translation{$1} = $newid;
10125: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
10126: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
10127: $translation{$1} = $newid;
10128: }
1.749 raeburn 10129: $new_values{$file_name."\0".$newkey} =
10130: $$changes{'activate'}{$newitems[$i]};
10131: $new_control{$newkey} = $now;
10132: }
10133: }
10134: }
10135: my %todelete;
10136: my %changed_items;
10137: foreach my $action ('delete','update') {
10138: if (exists($$changes{$action})) {
10139: if (ref($$changes{$action}) eq 'HASH') {
10140: foreach my $key (keys(%{$$changes{$action}})) {
10141: my ($itemnum) = ($key =~ /^([^:]+):/);
10142: if ($action eq 'delete') {
10143: $todelete{$itemnum} = 1;
10144: } else {
10145: $changed_items{$itemnum} = $key;
10146: }
10147: }
1.745 raeburn 10148: }
10149: }
1.749 raeburn 10150: }
10151: # get lock on access controls for file.
10152: my $lockhash = {
10153: $file_name."\0".'locked_access_records' => $env{'user.name'}.
10154: ':'.$env{'user.domain'},
10155: };
10156: my $tries = 0;
10157: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10158:
1.1288 damieng 10159: while (($gotlock ne 'ok') && $tries < 10) {
1.749 raeburn 10160: $tries ++;
1.1289 damieng 10161: sleep(0.1);
1.749 raeburn 10162: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10163: }
10164: if ($gotlock eq 'ok') {
10165: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
10166: my ($tmp)=keys(%curr_permissions);
10167: if ($tmp=~/^error:/) { undef(%curr_permissions); }
10168: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
10169: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
10170: if (ref($curr_controls) eq 'HASH') {
10171: foreach my $control_item (keys(%{$curr_controls})) {
10172: my ($itemnum) = ($control_item =~ /^([^:]+):/);
10173: if (defined($todelete{$itemnum})) {
10174: push(@deletions,$file_name."\0".$control_item);
10175: } else {
10176: if (defined($changed_items{$itemnum})) {
10177: $new_control{$changed_items{$itemnum}} = $now;
10178: push(@deletions,$file_name."\0".$control_item);
10179: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
10180: } else {
10181: $new_control{$control_item} = $$curr_controls{$control_item};
10182: }
10183: }
1.745 raeburn 10184: }
10185: }
10186: }
1.970 raeburn 10187: my ($group);
10188: if (&is_course($domain,$user)) {
10189: ($group,my $file) = split(/\//,$file_name,2);
10190: }
1.749 raeburn 10191: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
10192: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
10193: $outcome = &put('file_permissions',\%new_values,$domain,$user);
10194: # remove lock
10195: my @del_lock = ($file_name."\0".'locked_access_records');
10196: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 10197: my $sqlresult =
1.970 raeburn 10198: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 10199: $group);
1.749 raeburn 10200: } else {
10201: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 10202: }
1.749 raeburn 10203: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 10204: }
10205:
1.827 raeburn 10206: sub make_public_indefinitely {
1.1271 raeburn 10207: my (@requrl) = @_;
10208: return &automated_portfile_access('public',\@requrl);
10209: }
10210:
10211: sub automated_portfile_access {
10212: my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273 raeburn 10213: unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
10214: return 'invalid';
10215: }
1.1271 raeburn 10216: my %urls;
10217: if (ref($addsref) eq 'ARRAY') {
10218: foreach my $requrl (@{$addsref}) {
10219: if (&is_portfolio_url($requrl)) {
10220: unless (exists($urls{$requrl})) {
10221: $urls{$requrl} = 'add';
10222: }
10223: }
10224: }
10225: }
10226: if (ref($delsref) eq 'ARRAY') {
10227: foreach my $requrl (@{$delsref}) {
10228: if (&is_portfolio_url($requrl)) {
10229: unless (exists($urls{$requrl})) {
10230: $urls{$requrl} = 'delete';
10231: }
10232: }
10233: }
10234: }
10235: unless (keys(%urls)) {
10236: return 'invalid';
10237: }
10238: my $ip;
10239: if ($accesstype eq 'ip') {
10240: if (ref($info) eq 'HASH') {
10241: if ($info->{'ip'} ne '') {
10242: $ip = $info->{'ip'};
10243: }
10244: }
10245: if ($ip eq '') {
10246: return 'invalid';
10247: }
10248: }
10249: my $errors;
1.827 raeburn 10250: my $now = time;
1.1271 raeburn 10251: my %current_perms;
10252: foreach my $requrl (sort(keys(%urls))) {
10253: my $action;
10254: if ($urls{$requrl} eq 'add') {
10255: $action = 'activate';
10256: } else {
10257: $action = 'none';
10258: }
10259: my $aclnum = 0;
1.827 raeburn 10260: my (undef,$udom,$unum,$file_name,$group) =
10261: &parse_portfolio_url($requrl);
1.1271 raeburn 10262: unless (exists($current_perms{$unum.':'.$udom})) {
10263: $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
10264: }
10265: my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827 raeburn 10266: $group,$file_name);
10267: foreach my $key (keys(%{$access_controls{$file_name}})) {
10268: my ($num,$scope,$end,$start) =
10269: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271 raeburn 10270: if ($scope eq $accesstype) {
10271: if (($start <= $now) && ($end == 0)) {
10272: if ($accesstype eq 'ip') {
10273: if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
10274: if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
10275: if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
10276: if ($urls{$requrl} eq 'add') {
10277: $action = 'none';
10278: last;
10279: } else {
10280: $action = 'delete';
10281: $aclnum = $num;
10282: last;
10283: }
10284: }
10285: }
10286: }
10287: } elsif ($accesstype eq 'public') {
10288: if ($urls{$requrl} eq 'add') {
10289: $action = 'none';
10290: last;
10291: } else {
10292: $action = 'delete';
10293: $aclnum = $num;
10294: last;
10295: }
10296: }
10297: } elsif ($accesstype eq 'public') {
1.827 raeburn 10298: $action = 'update';
10299: $aclnum = $num;
1.1271 raeburn 10300: last;
1.827 raeburn 10301: }
10302: }
10303: }
10304: if ($action eq 'none') {
1.1271 raeburn 10305: next;
1.827 raeburn 10306: } else {
10307: my %changes;
10308: my $newend = 0;
10309: my $newstart = $now;
1.1271 raeburn 10310: my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827 raeburn 10311: $changes{$action}{$newkey} = {
1.1271 raeburn 10312: type => $accesstype,
1.827 raeburn 10313: time => {
10314: start => $newstart,
10315: end => $newend,
10316: },
10317: };
1.1271 raeburn 10318: if ($accesstype eq 'ip') {
10319: $changes{$action}{$newkey}{'ip'} = [$ip];
10320: }
1.827 raeburn 10321: my ($outcome,$deloutcome,$new_values,$translation) =
10322: &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271 raeburn 10323: unless ($outcome eq 'ok') {
10324: $errors .= $outcome.' ';
10325: }
1.827 raeburn 10326: }
1.1271 raeburn 10327: }
10328: if ($errors) {
10329: $errors =~ s/\s$//;
10330: return $errors;
1.827 raeburn 10331: } else {
1.1271 raeburn 10332: return 'ok';
1.827 raeburn 10333: }
10334: }
10335:
1.745 raeburn 10336: #------------------------------------------------------Get Marked as Read Only
10337:
10338: sub get_marked_as_readonly {
10339: my ($domain,$user,$what,$group) = @_;
10340: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 10341: my @readonly_files;
1.629 banghart 10342: my $cmp1=$what;
10343: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 10344: while (my ($file_name,$value) = each(%{$current_permissions})) {
10345: if (defined($group)) {
10346: if ($file_name !~ m-^\Q$group\E/-) {
10347: next;
10348: }
10349: }
1.561 banghart 10350: if (ref($value) eq "ARRAY"){
10351: foreach my $stored_what (@{$value}) {
1.629 banghart 10352: my $cmp2=$stored_what;
1.759 albertel 10353: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 10354: $cmp2=join('',@{$stored_what});
1.745 raeburn 10355: }
1.629 banghart 10356: if ($cmp1 eq $cmp2) {
1.561 banghart 10357: push(@readonly_files, $file_name);
1.745 raeburn 10358: last;
1.563 banghart 10359: } elsif (!defined($what)) {
10360: push(@readonly_files, $file_name);
1.745 raeburn 10361: last;
1.561 banghart 10362: }
10363: }
1.745 raeburn 10364: }
1.561 banghart 10365: }
10366: return @readonly_files;
10367: }
1.577 banghart 10368: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 10369:
1.577 banghart 10370: sub get_marked_as_readonly_hash {
1.745 raeburn 10371: my ($current_permissions,$group,$what) = @_;
1.577 banghart 10372: my %readonly_files;
1.745 raeburn 10373: while (my ($file_name,$value) = each(%{$current_permissions})) {
10374: if (defined($group)) {
10375: if ($file_name !~ m-^\Q$group\E/-) {
10376: next;
10377: }
10378: }
1.577 banghart 10379: if (ref($value) eq "ARRAY"){
10380: foreach my $stored_what (@{$value}) {
1.745 raeburn 10381: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 10382: foreach my $lock_descriptor(@{$stored_what}) {
10383: if ($lock_descriptor eq 'graded') {
10384: $readonly_files{$file_name} = 'graded';
10385: } elsif ($lock_descriptor eq 'handback') {
10386: $readonly_files{$file_name} = 'handback';
10387: } else {
10388: if (!exists($readonly_files{$file_name})) {
10389: $readonly_files{$file_name} = 'locked';
10390: }
10391: }
1.745 raeburn 10392: }
1.750 banghart 10393: }
1.577 banghart 10394: }
10395: }
10396: }
10397: return %readonly_files;
10398: }
1.559 banghart 10399: # ------------------------------------------------------------ Unmark as Read Only
10400:
10401: sub unmark_as_readonly {
1.629 banghart 10402: # unmarks $file_name (if $file_name is defined), or all files locked by $what
10403: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 10404: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 10405: $file_name = &declutter_portfile($file_name);
1.634 albertel 10406: my $symb_crs = $what;
10407: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 10408: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 10409: my ($tmp)=keys(%current_permissions);
10410: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 10411: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 10412: foreach my $file (@readonly_files) {
1.759 albertel 10413: my $clean_file = &declutter_portfile($file);
10414: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 10415: my $current_locks = $current_permissions{$file};
1.563 banghart 10416: my @new_locks;
10417: my @del_keys;
10418: if (ref($current_locks) eq "ARRAY"){
10419: foreach my $locker (@{$current_locks}) {
1.632 albertel 10420: my $compare=$locker;
1.749 raeburn 10421: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 10422: $compare=join('',@{$locker});
1.746 raeburn 10423: if ($compare ne $symb_crs) {
10424: push(@new_locks, $locker);
10425: }
1.563 banghart 10426: }
10427: }
1.650 albertel 10428: if (scalar(@new_locks) > 0) {
1.563 banghart 10429: $current_permissions{$file} = \@new_locks;
10430: } else {
10431: push(@del_keys, $file);
1.613 albertel 10432: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 10433: delete($current_permissions{$file});
1.563 banghart 10434: }
10435: }
1.561 banghart 10436: }
1.613 albertel 10437: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 10438: return;
10439: }
1.512 banghart 10440:
1.17 www 10441: # ------------------------------------------------------------ Directory lister
10442:
10443: sub dirlist {
1.955 raeburn 10444: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 10445: $uri=~s/^\///;
10446: $uri=~s/\/$//;
1.253 stredwic 10447: my ($udom, $uname);
1.955 raeburn 10448: if ($getuserdir) {
1.253 stredwic 10449: $udom = $userdomain;
10450: $uname = $username;
1.955 raeburn 10451: } else {
10452: (undef,$udom,$uname)=split(/\//,$uri);
10453: if(defined($userdomain)) {
10454: $udom = $userdomain;
10455: }
10456: if(defined($username)) {
10457: $uname = $username;
10458: }
1.253 stredwic 10459: }
1.955 raeburn 10460: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 10461:
1.955 raeburn 10462: $dirRoot = $perlvar{'lonDocRoot'};
10463: if (defined($getpropath)) {
10464: $dirRoot = &propath($udom,$uname);
1.253 stredwic 10465: $dirRoot =~ s/\/$//;
1.955 raeburn 10466: } elsif (defined($getuserdir)) {
10467: my $subdir=$uname.'__';
10468: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
10469: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
10470: ."/$udom/$subdir/$uname";
10471: } elsif (defined($alternateRoot)) {
10472: $dirRoot = $alternateRoot;
1.751 banghart 10473: }
1.253 stredwic 10474:
10475: if($udom) {
10476: if($uname) {
1.1135 raeburn 10477: my $uhome = &homeserver($uname,$udom);
1.1136 raeburn 10478: if ($uhome eq 'no_host') {
10479: return ([],'no_host');
10480: }
1.955 raeburn 10481: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 10482: .$getuserdir.':'.&escape($dirRoot)
1.1135 raeburn 10483: .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955 raeburn 10484: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 10485: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955 raeburn 10486: } else {
10487: @listing_results = map { &unescape($_); } split(/:/,$listing);
10488: }
1.605 matthew 10489: if ($listing eq 'unknown_cmd') {
1.1135 raeburn 10490: $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605 matthew 10491: @listing_results = split(/:/,$listing);
10492: } else {
10493: @listing_results = map { &unescape($_); } split(/:/,$listing);
10494: }
1.1135 raeburn 10495: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
1.1136 raeburn 10496: ($listing eq 'rejected') || ($listing eq 'refused') ||
10497: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10498: return ([],$listing);
10499: } else {
10500: return (\@listing_results);
1.1135 raeburn 10501: }
1.955 raeburn 10502: } elsif(!$alternateRoot) {
1.1136 raeburn 10503: my (%allusers,%listerror);
1.841 albertel 10504: my %servers = &get_servers($udom,'library');
1.955 raeburn 10505: foreach my $tryserver (keys(%servers)) {
10506: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
10507: &escape($udom),$tryserver);
10508: if ($listing eq 'unknown_cmd') {
10509: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
10510: $udom, $tryserver);
10511: } else {
10512: @listing_results = map { &unescape($_); } split(/:/,$listing);
10513: }
1.841 albertel 10514: if ($listing eq 'unknown_cmd') {
10515: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
10516: $udom, $tryserver);
10517: @listing_results = split(/:/,$listing);
10518: } else {
10519: @listing_results =
10520: map { &unescape($_); } split(/:/,$listing);
10521: }
1.1136 raeburn 10522: if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
10523: ($listing eq 'rejected') || ($listing eq 'refused') ||
10524: ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
10525: $listerror{$tryserver} = $listing;
10526: } else {
1.841 albertel 10527: foreach my $line (@listing_results) {
10528: my ($entry) = split(/&/,$line,2);
10529: $allusers{$entry} = 1;
10530: }
10531: }
1.253 stredwic 10532: }
1.1136 raeburn 10533: my @alluserslist=();
1.800 albertel 10534: foreach my $user (sort(keys(%allusers))) {
1.1136 raeburn 10535: push(@alluserslist,$user.'&user');
1.253 stredwic 10536: }
1.1318 droeschl 10537:
10538: if (!%listerror) {
10539: # no errors
10540: return (\@alluserslist);
10541: } elsif (scalar(keys(%servers)) == 1) {
10542: # one library server, one error
10543: my ($key) = keys(%listerror);
10544: return (\@alluserslist, $listerror{$key});
10545: } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
10546: # con_lost indicates that we might miss data from at least one
10547: # library server
10548: return (\@alluserslist, 'con_lost');
10549: } else {
10550: # multiple library servers and no con_lost -> data should be
10551: # complete.
10552: return (\@alluserslist);
10553: }
10554:
1.253 stredwic 10555: } else {
1.1136 raeburn 10556: return ([],'missing username');
1.253 stredwic 10557: }
1.955 raeburn 10558: } elsif(!defined($getpropath)) {
1.1136 raeburn 10559: my $path = $perlvar{'lonDocRoot'}.'/res/';
10560: my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
10561: return (\@all_domains);
1.955 raeburn 10562: } else {
1.1136 raeburn 10563: return ([],'missing domain');
1.275 stredwic 10564: }
10565: }
10566:
10567: # --------------------------------------------- GetFileTimestamp
10568: # This function utilizes dirlist and returns the date stamp for
10569: # when it was last modified. It will also return an error of -1
10570: # if an error occurs
10571:
10572: sub GetFileTimestamp {
1.955 raeburn 10573: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 10574: $studentDomain = &LONCAPA::clean_domain($studentDomain);
10575: $studentName = &LONCAPA::clean_username($studentName);
1.1136 raeburn 10576: my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
10577: undef,$getuserdir);
10578: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10579: return -1;
10580: }
10581: if (ref($fileref) eq 'ARRAY') {
10582: my @stats = split('&',$fileref->[0]);
1.375 matthew 10583: # @stats contains first the filename, then the stat output
10584: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 10585: } else {
10586: return -1;
1.253 stredwic 10587: }
1.26 www 10588: }
10589:
1.712 albertel 10590: sub stat_file {
10591: my ($uri) = @_;
1.787 albertel 10592: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 10593:
1.955 raeburn 10594: my ($udom,$uname,$file);
1.712 albertel 10595: if ($uri =~ m-^/(uploaded|editupload)/-) {
10596: ($udom,$uname,$file) =
1.811 albertel 10597: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 10598: $file = 'userfiles/'.$file;
10599: }
10600: if ($uri =~ m-^/res/-) {
10601: ($udom,$uname) =
1.807 albertel 10602: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 10603: $file = $uri;
10604: }
10605:
10606: if (!$udom || !$uname || !$file) {
10607: # unable to handle the uri
10608: return ();
10609: }
1.956 raeburn 10610: my $getpropath;
10611: if ($file =~ /^userfiles\//) {
10612: $getpropath = 1;
10613: }
1.1136 raeburn 10614: my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
10615: if (($error eq 'empty') || ($error eq 'no_such_dir')) {
10616: return ();
10617: } else {
10618: if (ref($listref) eq 'ARRAY') {
10619: my @stats = split('&',$listref->[0]);
10620: shift(@stats); #filename is first
10621: return @stats;
10622: }
1.712 albertel 10623: }
10624: return ();
10625: }
10626:
1.1313 raeburn 10627: # --------------------------------------------------------- recursedirs
10628: # Recursive function to traverse either a specific user's Authoring Space
10629: # or corresponding Published Resource Space, and populate the hash ref:
10630: # $dirhashref with URLs of all directories, and if $filehashref hash
10631: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
10632: # or .rights files in resource space, and .meta, .save, .log, and .bak
10633: # files in Authoring Space.
10634: #
10635: # Inputs:
10636: #
10637: # $is_home - true if current server is home server for user's space
10638: # $context - either: priv, or res respectively for Authoring or Resource Space.
10639: # $docroot - Document root (i.e., /home/httpd/html
10640: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
10641: # $relpath - Current path (relative to top level).
10642: # $dirhashref - reference to hash to populate with URLs of directories (Required)
10643: # $filehashref - reference to hash to populate with URLs of files (Optional)
10644: #
10645: # Returns: nothing
10646: #
10647: # Side Effects: populates $dirhashref, and $filehashref (if provided).
10648: #
10649: # Currently used by interface/londocs.pm to create linked select boxes for
10650: # directory and filename to import a Course "Author" resource into a course, and
10651: # also to create linked select boxes for Authoring Space and Directory to choose
10652: # save location for creation of a new "standard" problem from the Course Editor.
10653: #
10654:
10655: sub recursedirs {
10656: my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
10657: return unless (ref($dirhashref) eq 'HASH');
10658: my $currpath = $docroot.$toppath;
10659: if ($relpath) {
10660: $currpath .= "/$relpath";
10661: }
10662: my $savefile;
10663: if (ref($filehashref)) {
10664: $savefile = 1;
10665: }
10666: if ($is_home) {
10667: if (opendir(my $dirh,$currpath)) {
10668: foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
10669: next if ($item eq '');
10670: if (-d "$currpath/$item") {
10671: my $newpath;
10672: if ($relpath) {
10673: $newpath = "$relpath/$item";
10674: } else {
10675: $newpath = $item;
10676: }
10677: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10678: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10679: } elsif ($savefile) {
10680: if ($context eq 'priv') {
10681: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10682: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10683: }
10684: } else {
10685: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
10686: $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
10687: }
10688: }
10689: }
10690: }
10691: closedir($dirh);
10692: }
10693: } else {
10694: my ($dirlistref,$listerror) =
10695: &dirlist($toppath.$relpath);
10696: my @dir_lines;
10697: my $dirptr=16384;
10698: if (ref($dirlistref) eq 'ARRAY') {
10699: foreach my $dir_line (sort
10700: {
10701: my ($afile)=split('&',$a,2);
10702: my ($bfile)=split('&',$b,2);
10703: return (lc($afile) cmp lc($bfile));
10704: } (@{$dirlistref})) {
10705: my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
10706: split(/\&/,$dir_line,16);
10707: $item =~ s/\s+$//;
10708: next if (($item =~ /^\.\.?$/) || ($obs));
10709: if ($dirptr&$testdir) {
10710: my $newpath;
10711: if ($relpath) {
10712: $newpath = "$relpath/$item";
10713: } else {
10714: $relpath = '/';
10715: $newpath = $item;
10716: }
10717: $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
10718: &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
10719: } elsif ($savefile) {
10720: if ($context eq 'priv') {
10721: unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
10722: $filehashref->{$relpath}{$item} = 1;
10723: }
10724: } else {
10725: unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
10726: $filehashref->{$relpath}{$item} = 1;
10727: }
10728: }
10729: }
10730: }
10731: }
10732: }
10733: return;
10734: }
10735:
1.26 www 10736: # -------------------------------------------------------- Value of a Condition
10737:
1.713 albertel 10738: # gets the value of a specific preevaluated condition
10739: # stored in the string $env{user.state.<cid>}
10740: # or looks up a condition reference in the bighash and if if hasn't
10741: # already been evaluated recurses into docondval to get the value of
10742: # the condition, then memoizing it to
10743: # $env{user.state.<cid>.<condition>}
1.40 www 10744: sub directcondval {
10745: my $number=shift;
1.620 albertel 10746: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 10747: &Apache::lonuserstate::evalstate();
10748: }
1.713 albertel 10749: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
10750: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
10751: } elsif ($number =~ /^_/) {
10752: my $sub_condition;
10753: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
10754: &GDBM_READER(),0640)) {
10755: $sub_condition=$bighash{'conditions'.$number};
10756: untie(%bighash);
10757: }
10758: my $value = &docondval($sub_condition);
1.949 raeburn 10759: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 10760: return $value;
10761: }
1.620 albertel 10762: if ($env{'user.state.'.$env{'request.course.id'}}) {
10763: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 10764: } else {
10765: return 2;
10766: }
10767: }
10768:
1.713 albertel 10769: # get the collection of conditions for this resource
1.26 www 10770: sub condval {
10771: my $condidx=shift;
1.54 www 10772: my $allpathcond='';
1.713 albertel 10773: foreach my $cond (split(/\|/,$condidx)) {
10774: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
10775: $allpathcond.=
10776: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
10777: }
1.191 harris41 10778: }
1.54 www 10779: $allpathcond=~s/\|$//;
1.713 albertel 10780: return &docondval($allpathcond);
10781: }
10782:
10783: #evaluates an expression of conditions
10784: sub docondval {
10785: my ($allpathcond) = @_;
10786: my $result=0;
10787: if ($env{'request.course.id'}
10788: && defined($allpathcond)) {
10789: my $operand='|';
10790: my @stack;
10791: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
10792: if ($chunk eq '(') {
10793: push @stack,($operand,$result);
10794: } elsif ($chunk eq ')') {
10795: my $before=pop @stack;
10796: if (pop @stack eq '&') {
10797: $result=$result>$before?$before:$result;
10798: } else {
10799: $result=$result>$before?$result:$before;
10800: }
10801: } elsif (($chunk eq '&') || ($chunk eq '|')) {
10802: $operand=$chunk;
10803: } else {
10804: my $new=directcondval($chunk);
10805: if ($operand eq '&') {
10806: $result=$result>$new?$new:$result;
10807: } else {
10808: $result=$result>$new?$result:$new;
10809: }
10810: }
10811: }
1.26 www 10812: }
10813: return $result;
1.421 albertel 10814: }
10815:
10816: # ---------------------------------------------------- Devalidate courseresdata
10817:
10818: sub devalidatecourseresdata {
10819: my ($coursenum,$coursedomain)=@_;
10820: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 10821: &devalidate_cache_new('courseres',$hashid);
1.28 www 10822: }
10823:
1.763 www 10824:
1.200 www 10825: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 10826: #
10827: # Parameters:
10828: # $coursenum - Number of the course.
10829: # $coursedomain - Domain at which the course was created.
10830: # Returns:
10831: # A hash of the course parameters along (I think) with timestamps
10832: # and version info.
1.877 foxr 10833:
1.624 albertel 10834: sub get_courseresdata {
10835: my ($coursenum,$coursedomain)=@_;
1.200 www 10836: my $coursehom=&homeserver($coursenum,$coursedomain);
10837: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 10838: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 10839: my %dumpreply;
1.417 albertel 10840: unless (defined($cached)) {
1.624 albertel 10841: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 10842: $result=\%dumpreply;
1.251 albertel 10843: my ($tmp) = keys(%dumpreply);
10844: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 10845: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 10846: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
10847: return $tmp;
1.416 albertel 10848: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 10849: $result=undef;
1.599 albertel 10850: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 10851: }
10852: }
1.624 albertel 10853: return $result;
10854: }
10855:
1.633 albertel 10856: sub devalidateuserresdata {
10857: my ($uname,$udom)=@_;
10858: my $hashid="$udom:$uname";
10859: &devalidate_cache_new('userres',$hashid);
10860: }
10861:
1.624 albertel 10862: sub get_userresdata {
10863: my ($uname,$udom)=@_;
10864: #most student don\'t have any data set, check if there is some data
10865: if (&EXT_cache_status($udom,$uname)) { return undef; }
10866:
10867: my $hashid="$udom:$uname";
10868: my ($result,$cached)=&is_cached_new('userres',$hashid);
10869: if (!defined($cached)) {
10870: my %resourcedata=&dump('resourcedata',$udom,$uname);
10871: $result=\%resourcedata;
10872: &do_cache_new('userres',$hashid,$result,600);
10873: }
10874: my ($tmp)=keys(%$result);
10875: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
10876: return $result;
10877: }
10878: #error 2 occurs when the .db doesn't exist
10879: if ($tmp!~/error: 2 /) {
1.1294 raeburn 10880: if ((!defined($cached)) || ($tmp ne 'con_lost')) {
10881: &logthis("<font color=\"blue\">WARNING:".
10882: " Trying to get resource data for ".
10883: $uname." at ".$udom.": ".
10884: $tmp."</font>");
10885: }
1.624 albertel 10886: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 10887: #&EXT_cache_set($udom,$uname);
10888: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 10889: undef($tmp); # not really an error so don't send it back
1.624 albertel 10890: }
10891: return $tmp;
10892: }
1.879 foxr 10893: #----------------------------------------------- resdata - return resource data
10894: # Purpose:
10895: # Return resource data for either users or for a course.
10896: # Parameters:
10897: # $name - Course/user name.
10898: # $domain - Name of the domain the user/course is registered on.
1.1311 raeburn 10899: # $type - Type of thing $name is (must be 'course' or 'user')
1.1301 raeburn 10900: # $mapp - decluttered URL of enclosing map
10901: # $recursed - Ref to scalar -- set to 1, if nested maps have been recursed.
10902: # $recurseup - Ref to array of map URLs, starting with map containing
10903: # $mapp up through hierarchy of nested maps to top level map.
10904: # $courseid - CourseID (first part of param identifier).
10905: # $modifier - Middle part of param identifier.
10906: # $what - Last part of param identifier.
1.879 foxr 10907: # @which - Array of names of resources desired.
10908: # Returns:
10909: # The value of the first reasource in @which that is found in the
10910: # resource hash.
10911: # Exceptional Conditions:
10912: # If the $type passed in is not valid (not the string 'course' or
10913: # 'user', an undefined reference is returned.
10914: # If none of the resources are found, an undef is returned
1.624 albertel 10915: sub resdata {
1.1301 raeburn 10916: my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
10917: $modifier,$what,@which)=@_;
1.624 albertel 10918: my $result;
10919: if ($type eq 'course') {
10920: $result=&get_courseresdata($name,$domain);
10921: } elsif ($type eq 'user') {
10922: $result=&get_userresdata($name,$domain);
10923: }
10924: if (!ref($result)) { return $result; }
1.251 albertel 10925: foreach my $item (@which) {
1.1301 raeburn 10926: if ($item->[1] eq 'course') {
10927: if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
10928: unless ($$recursed) {
1.1302 raeburn 10929: @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301 raeburn 10930: $$recursed = 1;
10931: }
10932: foreach my $item (@${recurseup}) {
10933: my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
10934: last if (defined($result->{$norecursechk}));
10935: my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
10936: if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
10937: }
10938: }
10939: }
10940: if (defined($result->{$item->[0]})) {
1.927 albertel 10941: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 10942: }
1.250 albertel 10943: }
1.291 albertel 10944: return undef;
1.200 www 10945: }
10946:
1.1298 raeburn 10947: sub get_domain_ltitools {
10948: my ($cdom) = @_;
10949: my %ltitools;
10950: my ($result,$cached)=&is_cached_new('ltitools',$cdom);
10951: if (defined($cached)) {
10952: if (ref($result) eq 'HASH') {
10953: %ltitools = %{$result};
10954: }
10955: } else {
10956: my %domconfig = &get_dom('configuration',['ltitools'],$cdom);
10957: if (ref($domconfig{'ltitools'}) eq 'HASH') {
10958: %ltitools = %{$domconfig{'ltitools'}};
10959: }
10960: my $cachetime = 24*60*60;
10961: &do_cache_new('ltitools',$cdom,\%ltitools,$cachetime);
10962: }
10963: return %ltitools;
10964: }
10965:
1.1236 raeburn 10966: sub get_numsuppfiles {
10967: my ($cnum,$cdom,$ignorecache)=@_;
10968: my $hashid=$cnum.':'.$cdom;
10969: my ($suppcount,$cached);
10970: unless ($ignorecache) {
10971: ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
10972: }
10973: unless (defined($cached)) {
10974: my $chome=&homeserver($cnum,$cdom);
10975: unless ($chome eq 'no_host') {
10976: ($suppcount,my $errors) = (0,0);
10977: my $suppmap = 'supplemental.sequence';
10978: ($suppcount,$errors) =
10979: &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,$errors);
10980: }
10981: &do_cache_new('suppcount',$hashid,$suppcount,600);
10982: }
10983: return $suppcount;
10984: }
10985:
1.379 matthew 10986: #
10987: # EXT resource caching routines
10988: #
10989:
1.1302 raeburn 10990: {
10991: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
10992: #
10993: # The course for which we cache
10994: my $cachedmapkey='';
10995: # The cached recursive maps for this course
10996: my %cachedmaps=();
10997: # When this was last done
10998: my $cachedmaptime='';
10999:
1.379 matthew 11000: sub clear_EXT_cache_status {
1.383 albertel 11001: &delenv('cache.EXT.');
1.379 matthew 11002: }
11003:
11004: sub EXT_cache_status {
11005: my ($target_domain,$target_user) = @_;
1.383 albertel 11006: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 11007: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 11008: # We know already the user has no data
11009: return 1;
11010: } else {
11011: return 0;
11012: }
11013: }
11014:
11015: sub EXT_cache_set {
11016: my ($target_domain,$target_user) = @_;
1.383 albertel 11017: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 11018: #&appenv({$cachename => time});
1.379 matthew 11019: }
11020:
1.28 www 11021: # --------------------------------------------------------- Value of a Variable
1.58 www 11022: sub EXT {
1.715 albertel 11023:
1.1228 raeburn 11024: my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68 www 11025: unless ($varname) { return ''; }
1.218 albertel 11026: #get real user name/domain, courseid and symb
11027: my $courseid;
1.359 albertel 11028: my $publicuser;
1.427 www 11029: if ($symbparm) {
11030: $symbparm=&get_symb_from_alias($symbparm);
11031: }
1.218 albertel 11032: if (!($uname && $udom)) {
1.790 albertel 11033: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 11034: if (!$symbparm) { $symbparm=$cursymb; }
11035: } else {
1.620 albertel 11036: $courseid=$env{'request.course.id'};
1.218 albertel 11037: }
1.48 www 11038: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
11039: my $rest;
1.320 albertel 11040: if (defined($therest[0])) {
1.48 www 11041: $rest=join('.',@therest);
11042: } else {
11043: $rest='';
11044: }
1.320 albertel 11045:
1.57 www 11046: my $qualifierrest=$qualifier;
11047: if ($rest) { $qualifierrest.='.'.$rest; }
11048: my $spacequalifierrest=$space;
11049: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 11050: if ($realm eq 'user') {
1.48 www 11051: # --------------------------------------------------------------- user.resource
11052: if ($space eq 'resource') {
1.651 albertel 11053: if ( (defined($Apache::lonhomework::parsing_a_problem)
11054: || defined($Apache::lonhomework::parsing_a_task))
11055: &&
1.744 albertel 11056: ($symbparm eq &symbread()) ) {
11057: # if we are in the middle of processing the resource the
11058: # get the value we are planning on committing
11059: if (defined($Apache::lonhomework::results{$qualifierrest})) {
11060: return $Apache::lonhomework::results{$qualifierrest};
11061: } else {
11062: return $Apache::lonhomework::history{$qualifierrest};
11063: }
1.335 albertel 11064: } else {
1.359 albertel 11065: my %restored;
1.620 albertel 11066: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 11067: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
11068: } else {
11069: %restored=&restore($symbparm,$courseid,$udom,$uname);
11070: }
1.335 albertel 11071: return $restored{$qualifierrest};
11072: }
1.48 www 11073: # ----------------------------------------------------------------- user.access
11074: } elsif ($space eq 'access') {
1.218 albertel 11075: # FIXME - not supporting calls for a specific user
1.48 www 11076: return &allowed($qualifier,$rest);
11077: # ------------------------------------------ user.preferences, user.environment
11078: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 11079: if (($uname eq $env{'user.name'}) &&
11080: ($udom eq $env{'user.domain'})) {
11081: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 11082: } else {
1.359 albertel 11083: my %returnhash;
11084: if (!$publicuser) {
11085: %returnhash=&userenvironment($udom,$uname,
11086: $qualifierrest);
11087: }
1.218 albertel 11088: return $returnhash{$qualifierrest};
11089: }
1.48 www 11090: # ----------------------------------------------------------------- user.course
11091: } elsif ($space eq 'course') {
1.218 albertel 11092: # FIXME - not supporting calls for a specific user
1.620 albertel 11093: return $env{join('.',('request.course',$qualifier))};
1.48 www 11094: # ------------------------------------------------------------------- user.role
11095: } elsif ($space eq 'role') {
1.218 albertel 11096: # FIXME - not supporting calls for a specific user
1.620 albertel 11097: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 11098: if ($qualifier eq 'value') {
11099: return $role;
11100: } elsif ($qualifier eq 'extent') {
11101: return $where;
11102: }
11103: # ----------------------------------------------------------------- user.domain
11104: } elsif ($space eq 'domain') {
1.218 albertel 11105: return $udom;
1.48 www 11106: # ------------------------------------------------------------------- user.name
11107: } elsif ($space eq 'name') {
1.218 albertel 11108: return $uname;
1.48 www 11109: # ---------------------------------------------------- Any other user namespace
1.29 www 11110: } else {
1.359 albertel 11111: my %reply;
11112: if (!$publicuser) {
11113: %reply=&get($space,[$qualifierrest],$udom,$uname);
11114: }
11115: return $reply{$qualifierrest};
1.48 www 11116: }
1.236 www 11117: } elsif ($realm eq 'query') {
11118: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 11119: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
11120: [$spacequalifierrest]);
1.620 albertel 11121: return $env{'form.'.$spacequalifierrest};
1.236 www 11122: } elsif ($realm eq 'request') {
1.48 www 11123: # ------------------------------------------------------------- request.browser
11124: if ($space eq 'browser') {
1.1145 bisitz 11125: return $env{'browser.'.$qualifier};
1.57 www 11126: # ------------------------------------------------------------ request.filename
11127: } else {
1.620 albertel 11128: return $env{'request.'.$spacequalifierrest};
1.29 www 11129: }
1.28 www 11130: } elsif ($realm eq 'course') {
1.48 www 11131: # ---------------------------------------------------------- course.description
1.620 albertel 11132: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 11133: } elsif ($realm eq 'resource') {
1.165 www 11134:
1.620 albertel 11135: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 11136: if (!$symbparm) { $symbparm=&symbread(); }
11137: }
1.693 albertel 11138:
1.1232 raeburn 11139: if ($qualifier eq '') {
11140: if ($space eq 'title') {
11141: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
11142: return &gettitle($symbparm);
11143: }
1.693 albertel 11144:
1.1232 raeburn 11145: if ($space eq 'map') {
11146: my ($map) = &decode_symb($symbparm);
11147: return &symbread($map);
11148: }
11149: if ($space eq 'maptitle') {
11150: my ($map) = &decode_symb($symbparm);
11151: return &gettitle($map);
11152: }
11153: if ($space eq 'filename') {
11154: if ($symbparm) {
11155: return &clutter((&decode_symb($symbparm))[2]);
11156: }
11157: return &hreflocation('',$env{'request.filename'});
1.905 albertel 11158: }
1.1232 raeburn 11159:
1.1233 raeburn 11160: if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
11161: if ($space eq 'visibleparts') {
11162: my $navmap = Apache::lonnavmaps::navmap->new();
11163: my $item;
11164: if (ref($navmap)) {
11165: my $res = $navmap->getBySymb($symbparm);
11166: my $parts = $res->parts();
11167: if (ref($parts) eq 'ARRAY') {
11168: $item = join(',',@{$parts});
11169: }
11170: undef($navmap);
1.1232 raeburn 11171: }
1.1233 raeburn 11172: return $item;
1.1232 raeburn 11173: }
11174: }
11175: }
1.693 albertel 11176:
1.1301 raeburn 11177: my ($section, $group, @groups, @recurseup, $recursed);
11178: my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228 raeburn 11179: if (($courseid eq '') && ($cid)) {
11180: $courseid = $cid;
11181: }
11182: if (($symbparm && $courseid) &&
11183: (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid))) {
1.165 www 11184:
1.218 albertel 11185: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 11186:
1.60 www 11187: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 11188: my $symbp=$symbparm;
1.1301 raeburn 11189: $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 11190: my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301 raeburn 11191: my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218 albertel 11192: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620 albertel 11193: if (($env{'user.name'} eq $uname) &&
11194: ($env{'user.domain'} eq $udom)) {
11195: $section=$env{'request.course.sec'};
1.733 raeburn 11196: @groups = split(/:/,$env{'request.course.groups'});
11197: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 11198: } else {
1.539 albertel 11199: if (! defined($usection)) {
1.551 albertel 11200: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 11201: } else {
11202: $section = $usection;
11203: }
1.733 raeburn 11204: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 11205: }
11206:
11207: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
11208: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301 raeburn 11209: my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218 albertel 11210: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
11211:
1.593 albertel 11212: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 11213: my $courselevelr=$courseid.'.'.$symbparm;
1.1301 raeburn 11214: $courseleveli=$courseid.'.'.$recurseparm;
1.593 albertel 11215: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 11216:
1.60 www 11217: # ----------------------------------------------------------- first, check user
1.624 albertel 11218:
1.1301 raeburn 11219: my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
11220: \@recurseup,$courseid,'.',$spacequalifierrest,
1.927 albertel 11221: ([$courselevelr,'resource'],
11222: [$courselevelm,'map' ],
1.1301 raeburn 11223: [$courseleveli,'map' ],
1.927 albertel 11224: [$courselevel, 'course' ]));
1.931 albertel 11225: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 11226:
1.594 albertel 11227: # ------------------------------------------------ second, check some of course
1.684 raeburn 11228: my $coursereply;
1.691 raeburn 11229: if (@groups > 0) {
11230: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301 raeburn 11231: $recurseparm,$mapparm,$spacequalifierrest,
11232: $mapp,\$recursed,\@recurseup);
11233: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 11234: }
1.96 www 11235:
1.684 raeburn 11236: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 11237: $env{'course.'.$courseid.'.domain'},
1.1301 raeburn 11238: 'course',$mapp,\$recursed,\@recurseup,
11239: $courseid,'.['.$section.'].',$spacequalifierrest,
1.927 albertel 11240: ([$seclevelr, 'resource'],
11241: [$seclevelm, 'map' ],
1.1301 raeburn 11242: [$secleveli, 'map' ],
1.927 albertel 11243: [$seclevel, 'course' ],
11244: [$courselevelr,'resource']));
11245: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 11246:
1.60 www 11247: # ------------------------------------------------------ third, check map parms
1.218 albertel 11248: my %parmhash=();
11249: my $thisparm='';
11250: if (tie(%parmhash,'GDBM_File',
1.620 albertel 11251: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 11252: &GDBM_READER(),0640)) {
1.218 albertel 11253: $thisparm=$parmhash{$symbparm};
11254: untie(%parmhash);
11255: }
1.927 albertel 11256: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 11257: }
1.594 albertel 11258: # ------------------------------------------ fourth, look in resource metadata
1.1301 raeburn 11259:
11260: my $what = $spacequalifierrest;
11261: $what=~s/\./\_/;
1.282 albertel 11262: my $filename;
11263: if (!$symbparm) { $symbparm=&symbread(); }
11264: if ($symbparm) {
1.409 www 11265: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 11266: } else {
1.620 albertel 11267: $filename=$env{'request.filename'};
1.282 albertel 11268: }
1.1301 raeburn 11269: my $metadata=&metadata($filename,$what);
1.927 albertel 11270: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1301 raeburn 11271: $metadata=&metadata($filename,'parameter_'.$what);
1.927 albertel 11272: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 11273:
1.1301 raeburn 11274: # ----------------------------------------------- fifth, look in rest of course
1.593 albertel 11275: if ($symbparm && defined($courseid) &&
1.620 albertel 11276: $courseid eq $env{'request.course.id'}) {
1.624 albertel 11277: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
11278: $env{'course.'.$courseid.'.domain'},
1.1301 raeburn 11279: 'course',$mapp,\$recursed,\@recurseup,
11280: $courseid,'.',$spacequalifierrest,
1.927 albertel 11281: ([$courselevelm,'map' ],
1.1301 raeburn 11282: [$courseleveli,'map' ],
1.927 albertel 11283: [$courselevel, 'course']));
11284: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 11285: }
1.145 www 11286: # ------------------------------------------------------------------ Cascade up
1.218 albertel 11287: unless ($space eq '0') {
1.336 albertel 11288: my @parts=split(/_/,$space);
11289: my $id=pop(@parts);
11290: my $part=join('_',@parts);
11291: if ($part eq '') { $part='0'; }
1.927 albertel 11292: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 11293: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 11294: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 11295: }
1.395 albertel 11296: if ($recurse) { return undef; }
11297: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 11298: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 11299: # ---------------------------------------------------- Any other user namespace
11300: } elsif ($realm eq 'environment') {
11301: # ----------------------------------------------------------------- environment
1.620 albertel 11302: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
11303: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 11304: } else {
1.770 albertel 11305: if ($uname eq 'anonymous' && $udom eq '') {
11306: return '';
11307: }
1.219 albertel 11308: my %returnhash=&userenvironment($udom,$uname,
11309: $spacequalifierrest);
11310: return $returnhash{$spacequalifierrest};
11311: }
1.28 www 11312: } elsif ($realm eq 'system') {
1.48 www 11313: # ----------------------------------------------------------------- system.time
11314: if ($space eq 'time') {
11315: return time;
11316: }
1.696 albertel 11317: } elsif ($realm eq 'server') {
11318: # ----------------------------------------------------------------- system.time
11319: if ($space eq 'name') {
11320: return $ENV{'SERVER_NAME'};
11321: }
1.28 www 11322: }
1.48 www 11323: return '';
1.61 www 11324: }
11325:
1.927 albertel 11326: sub get_reply {
11327: my ($reply_value) = @_;
1.940 raeburn 11328: if (ref($reply_value) eq 'ARRAY') {
11329: if (wantarray) {
11330: return @$reply_value;
11331: }
11332: return $reply_value->[0];
11333: } else {
11334: return $reply_value;
1.927 albertel 11335: }
11336: }
11337:
1.691 raeburn 11338: sub check_group_parms {
1.1301 raeburn 11339: my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
11340: $recursed,$recurseupref) = @_;
11341: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
11342: [$what,'course']);
11343: my $coursereply;
1.691 raeburn 11344: foreach my $group (@{$groups}) {
1.1301 raeburn 11345: my @groupitems = ();
1.691 raeburn 11346: foreach my $level (@levels) {
1.927 albertel 11347: my $item = $courseid.'.['.$group.'].'.$level->[0];
11348: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 11349: }
1.1301 raeburn 11350: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
11351: $env{'course.'.$courseid.'.domain'},
11352: 'course',$mapp,$recursed,$recurseupref,
11353: $courseid,'.['.$group.'].',$what,
11354: @groupitems);
11355: last if (defined($coursereply));
1.691 raeburn 11356: }
11357: return $coursereply;
11358: }
11359:
1.1301 raeburn 11360: sub get_map_hierarchy {
1.1302 raeburn 11361: my ($mapname,$courseid) = @_;
11362: my @recurseup = ();
1.1301 raeburn 11363: if ($mapname) {
1.1302 raeburn 11364: if (($cachedmapkey eq $courseid) &&
11365: (abs($cachedmaptime-time)<5)) {
11366: if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
11367: return @{$cachedmaps{$mapname}};
11368: }
11369: }
1.1301 raeburn 11370: my $navmap = Apache::lonnavmaps::navmap->new();
11371: if (ref($navmap)) {
11372: @recurseup = $navmap->recurseup_maps($mapname);
11373: undef($navmap);
1.1302 raeburn 11374: $cachedmaps{$mapname} = \@recurseup;
11375: $cachedmaptime=time;
11376: $cachedmapkey=$courseid;
1.1301 raeburn 11377: }
11378: }
11379: return @recurseup;
11380: }
11381:
1.1302 raeburn 11382: }
11383:
1.691 raeburn 11384: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 11385: my ($courseid,@groups) = @_;
11386: @groups = sort(@groups);
1.691 raeburn 11387: return @groups;
11388: }
11389:
1.395 albertel 11390: sub packages_tab_default {
11391: my ($uri,$varname)=@_;
11392: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 11393:
11394: my (@extension,@specifics,$do_default);
11395: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 11396: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 11397: if ($pack_type eq 'default') {
11398: $do_default=1;
11399: } elsif ($pack_type eq 'extension') {
11400: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 11401: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 11402: # only look at packages defaults for packages that this id is
1.738 albertel 11403: push(@specifics,[$package,$pack_type,$pack_part]);
11404: }
11405: }
11406: # first look for a package that matches the requested part id
11407: foreach my $package (@specifics) {
11408: my (undef,$pack_type,$pack_part)=@{$package};
11409: next if ($pack_part ne $part);
11410: if (defined($packagetab{"$pack_type&$name&default"})) {
11411: return $packagetab{"$pack_type&$name&default"};
11412: }
11413: }
11414: # look for any possible matching non extension_ package
11415: foreach my $package (@specifics) {
11416: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 11417: if (defined($packagetab{"$pack_type&$name&default"})) {
11418: return $packagetab{"$pack_type&$name&default"};
11419: }
1.585 albertel 11420: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 11421: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
11422: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 11423: }
11424: }
1.738 albertel 11425: # look for any posible extension_ match
11426: foreach my $package (@extension) {
11427: my ($package,$pack_type)=@{$package};
11428: if (defined($packagetab{"$pack_type&$name&default"})) {
11429: return $packagetab{"$pack_type&$name&default"};
11430: }
11431: if (defined($packagetab{$package."&$name&default"})) {
11432: return $packagetab{$package."&$name&default"};
11433: }
11434: }
11435: # look for a global default setting
11436: if ($do_default && defined($packagetab{"default&$name&default"})) {
11437: return $packagetab{"default&$name&default"};
11438: }
1.395 albertel 11439: return undef;
11440: }
11441:
1.334 albertel 11442: sub add_prefix_and_part {
11443: my ($prefix,$part)=@_;
11444: my $keyroot;
11445: if (defined($prefix) && $prefix !~ /^__/) {
11446: # prefix that has a part already
11447: $keyroot=$prefix;
11448: } elsif (defined($prefix)) {
11449: # prefix that is missing a part
11450: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
11451: } else {
11452: # no prefix at all
11453: if (defined($part)) { $keyroot='_'.$part; }
11454: }
11455: return $keyroot;
11456: }
11457:
1.71 www 11458: # ---------------------------------------------------------------- Get metadata
11459:
1.599 albertel 11460: my %metaentry;
1.1070 www 11461: my %importedpartids;
1.71 www 11462: sub metadata {
1.176 www 11463: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 11464: $uri=&declutter($uri);
1.288 albertel 11465: # if it is a non metadata possible uri return quickly
1.529 albertel 11466: if (($uri eq '') ||
11467: (($uri =~ m|^/*adm/|) &&
1.1343 ! raeburn 11468: ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108 raeburn 11469: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 11470: return undef;
11471: }
1.1261 raeburn 11472: if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv})
1.924 albertel 11473: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 11474: return undef;
1.288 albertel 11475: }
1.73 www 11476: my $filename=$uri;
11477: $uri=~s/\.meta$//;
1.172 www 11478: #
11479: # Is the metadata already cached?
1.177 www 11480: # Look at timestamp of caching
1.172 www 11481: # Everything is cached by the main uri, libraries are never directly cached
11482: #
1.428 albertel 11483: if (!defined($liburi)) {
1.599 albertel 11484: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 11485: if (defined($cached)) { return $result->{':'.$what}; }
11486: }
11487: {
1.1069 www 11488: # Imported parts would go here
1.1070 www 11489: my %importedids=();
11490: my @origfileimportpartids=();
1.1069 www 11491: my $importedparts=0;
1.172 www 11492: #
11493: # Is this a recursive call for a library?
11494: #
1.599 albertel 11495: # if (! exists($metacache{$uri})) {
11496: # $metacache{$uri}={};
11497: # }
1.924 albertel 11498: my $cachetime = 60*60;
1.171 www 11499: if ($liburi) {
11500: $liburi=&declutter($liburi);
11501: $filename=$liburi;
1.401 bowersj2 11502: } else {
1.599 albertel 11503: &devalidate_cache_new('meta',$uri);
11504: undef(%metaentry);
1.401 bowersj2 11505: }
1.140 www 11506: my %metathesekeys=();
1.73 www 11507: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 11508: my $metastring;
1.1140 www 11509: if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929 albertel 11510: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 11511: $metastring =
1.929 albertel 11512: &Apache::lonnet::ssi_body($which,
1.924 albertel 11513: ('grade_target' => 'meta'));
11514: $cachetime = 1; # only want this cached in the child not long term
1.1108 raeburn 11515: } elsif (($uri !~ m -^(editupload)/-) &&
11516: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 11517: my $file=&filelocation('',&clutter($filename));
1.599 albertel 11518: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 11519: $metastring=&getfile($file);
1.489 albertel 11520: }
1.208 albertel 11521: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 11522: my $token;
1.140 www 11523: undef %metathesekeys;
1.71 www 11524: while ($token=$parser->get_token) {
1.339 albertel 11525: if ($token->[0] eq 'S') {
11526: if (defined($token->[2]->{'package'})) {
1.172 www 11527: #
11528: # This is a package - get package info
11529: #
1.339 albertel 11530: my $package=$token->[2]->{'package'};
11531: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11532: if (defined($token->[2]->{'id'})) {
11533: $keyroot.='_'.$token->[2]->{'id'};
11534: }
1.599 albertel 11535: if ($metaentry{':packages'}) {
11536: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 11537: } else {
1.599 albertel 11538: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 11539: }
1.736 albertel 11540: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 11541: my $part=$keyroot;
11542: $part=~s/^\_//;
1.736 albertel 11543: if ($pack_entry=~/^\Q$package\E\&/ ||
11544: $pack_entry=~/^\Q$package\E_0\&/) {
11545: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 11546: # ignore package.tab specified default values
11547: # here &package_tab_default() will fetch those
11548: if ($subp eq 'default') { next; }
1.736 albertel 11549: my $value=$packagetab{$pack_entry};
1.432 albertel 11550: my $unikey;
11551: if ($pack =~ /_0$/) {
11552: $unikey='parameter_0_'.$name;
11553: $part=0;
11554: } else {
11555: $unikey='parameter'.$keyroot.'_'.$name;
11556: }
1.339 albertel 11557: if ($subp eq 'display') {
11558: $value.=' [Part: '.$part.']';
11559: }
1.599 albertel 11560: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 11561: $metathesekeys{$unikey}=1;
1.599 albertel 11562: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11563: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 11564: }
1.599 albertel 11565: if (defined($metaentry{':'.$unikey.'.default'})) {
11566: $metaentry{':'.$unikey}=
11567: $metaentry{':'.$unikey.'.default'};
1.356 albertel 11568: }
1.339 albertel 11569: }
11570: }
11571: } else {
1.172 www 11572: #
11573: # This is not a package - some other kind of start tag
1.339 albertel 11574: #
11575: my $entry=$token->[1];
1.1068 www 11576: my $unikey='';
1.175 www 11577:
1.339 albertel 11578: if ($entry eq 'import') {
1.175 www 11579: #
11580: # Importing a library here
1.339 albertel 11581: #
1.1067 www 11582: my $location=$parser->get_text('/import');
11583: my $dir=$filename;
11584: $dir=~s|[^/]*$||;
11585: $location=&filelocation($dir,$location);
1.1069 www 11586:
1.1068 www 11587: my $importmode=$token->[2]->{'importmode'};
11588: if ($importmode eq 'problem') {
1.1069 www 11589: # Import as problem/response
1.1068 www 11590: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11591: } elsif ($importmode eq 'part') {
11592: # Import as part(s)
1.1069 www 11593: $importedparts=1;
11594: # We need to get the original file and the imported file to get the part order correct
11595: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
11596: # Load and inspect original file
1.1070 www 11597: if ($#origfileimportpartids<0) {
11598: undef(%importedpartids);
11599: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
11600: my $origfile=&getfile($origfilelocation);
11601: @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11602: }
11603:
1.1069 www 11604: # Load and inspect imported file
11605: my $impfile=&getfile($location);
11606: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
11607: if ($#impfilepartids>=0) {
11608: # This problem had parts
1.1070 www 11609: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 11610: } else {
11611: # Importing by turning a single problem into a problem part
11612: # It gets the import-tags ID as part-ID
11613: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 11614: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 11615: }
1.1068 www 11616: } else {
11617: # Normal import
11618: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
11619: if (defined($token->[2]->{'id'})) {
11620: $unikey.='_'.$token->[2]->{'id'};
11621: }
1.1067 www 11622: }
11623:
1.339 albertel 11624: if ($depthcount<20) {
1.736 albertel 11625: my $metadata =
11626: &metadata($uri,'keys', $location,$unikey,
11627: $depthcount+1);
11628: foreach my $meta (split(',',$metadata)) {
11629: $metaentry{':'.$meta}=$metaentry{':'.$meta};
11630: $metathesekeys{$meta}=1;
1.339 albertel 11631: }
1.1068 www 11632:
11633: }
1.1067 www 11634: } else {
11635: #
11636: # Not importing, some other kind of non-package, non-library start tag
11637: #
11638: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
11639: if (defined($token->[2]->{'id'})) {
11640: $unikey.='_'.$token->[2]->{'id'};
11641: }
1.339 albertel 11642: if (defined($token->[2]->{'name'})) {
11643: $unikey.='_'.$token->[2]->{'name'};
11644: }
11645: $metathesekeys{$unikey}=1;
1.736 albertel 11646: foreach my $param (@{$token->[3]}) {
11647: $metaentry{':'.$unikey.'.'.$param} =
11648: $token->[2]->{$param};
1.339 albertel 11649: }
11650: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 11651: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 11652: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
11653: # only ws inside the tag, and not in default, so use default
11654: # as value
1.599 albertel 11655: $metaentry{':'.$unikey}=$default;
1.908 albertel 11656: } elsif ( $internaltext =~ /\S/ ) {
11657: # something interesting inside the tag
11658: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 11659: } else {
1.908 albertel 11660: # no interesting values, don't set a default
1.339 albertel 11661: }
1.172 www 11662: # end of not-a-package not-a-library import
1.339 albertel 11663: }
1.172 www 11664: # end of not-a-package start tag
1.339 albertel 11665: }
1.172 www 11666: # the next is the end of "start tag"
1.339 albertel 11667: }
11668: }
1.483 albertel 11669: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 11670: $extension = lc($extension);
11671: if ($extension eq 'htm') { $extension='html'; }
11672:
1.737 albertel 11673: foreach my $key (keys(%packagetab)) {
1.483 albertel 11674: #no specific packages #how's our extension
11675: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 11676: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 11677: \%metathesekeys);
11678: }
1.883 albertel 11679:
11680: if (!exists($metaentry{':packages'})
11681: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 11682: foreach my $key (keys(%packagetab)) {
1.483 albertel 11683: #no specific packages well let's get default then
11684: if ($key!~/^default&/) { next; }
1.488 albertel 11685: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 11686: \%metathesekeys);
11687: }
11688: }
1.338 www 11689: # are there custom rights to evaluate
1.599 albertel 11690: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 11691:
1.338 www 11692: #
11693: # Importing a rights file here
1.339 albertel 11694: #
11695: unless ($depthcount) {
1.599 albertel 11696: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 11697: my $dir=$filename;
11698: $dir=~s|[^/]*$||;
11699: $location=&filelocation($dir,$location);
1.736 albertel 11700: my $rights_metadata =
11701: &metadata($uri,'keys',$location,'_rights',
11702: $depthcount+1);
11703: foreach my $rights (split(',',$rights_metadata)) {
11704: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
11705: $metathesekeys{$rights}=1;
1.339 albertel 11706: }
11707: }
11708: }
1.737 albertel 11709: # uniqifiy package listing
11710: my %seen;
11711: my @uniq_packages =
11712: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
11713: $metaentry{':packages'} = join(',',@uniq_packages);
11714:
1.1070 www 11715: if ($importedparts) {
11716: # We had imported parts and need to rebuild partorder
11717: $metaentry{':partorder'}='';
11718: $metathesekeys{'partorder'}=1;
11719: for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
11720: if ($origfileimportpartids[$index] eq 'part') {
11721: # original part, part of the problem
11722: $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
11723: } else {
11724: # we have imported parts at this position
11725: $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
11726: }
11727: }
11728: $metaentry{':partorder'}=~s/^\,//;
11729: }
11730:
1.737 albertel 11731: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 11732: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274 raeburn 11733: $metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924 albertel 11734: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 11735: # this is the end of "was not already recently cached
1.71 www 11736: }
1.599 albertel 11737: return $metaentry{':'.$what};
1.261 albertel 11738: }
11739:
1.488 albertel 11740: sub metadata_create_package_def {
1.483 albertel 11741: my ($uri,$key,$package,$metathesekeys)=@_;
11742: my ($pack,$name,$subp)=split(/\&/,$key);
11743: if ($subp eq 'default') { next; }
11744:
1.599 albertel 11745: if (defined($metaentry{':packages'})) {
11746: $metaentry{':packages'}.=','.$package;
1.483 albertel 11747: } else {
1.599 albertel 11748: $metaentry{':packages'}=$package;
1.483 albertel 11749: }
11750: my $value=$packagetab{$key};
11751: my $unikey;
11752: $unikey='parameter_0_'.$name;
1.599 albertel 11753: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 11754: $$metathesekeys{$unikey}=1;
1.599 albertel 11755: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
11756: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 11757: }
1.599 albertel 11758: if (defined($metaentry{':'.$unikey.'.default'})) {
11759: $metaentry{':'.$unikey}=
11760: $metaentry{':'.$unikey.'.default'};
1.483 albertel 11761: }
11762: }
11763:
1.261 albertel 11764: sub metadata_generate_part0 {
11765: my ($metadata,$metacache,$uri) = @_;
11766: my %allnames;
1.737 albertel 11767: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 11768: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 11769: my $part=$$metacache{':'.$metakey.'.part'};
11770: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 11771: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 11772: $allnames{$name}=$part;
11773: }
11774: }
11775: }
11776: foreach my $name (keys(%allnames)) {
11777: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 11778: my $key=":parameter_0_$name";
1.261 albertel 11779: $$metacache{"$key.part"}='0';
11780: $$metacache{"$key.name"}=$name;
1.428 albertel 11781: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 11782: $allnames{$name}.'_'.$name.
11783: '.type'};
1.428 albertel 11784: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 11785: '.display'};
1.644 www 11786: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 11787: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 11788: $$metacache{"$key.display"}=$olddis;
11789: }
1.71 www 11790: }
11791:
1.764 albertel 11792: # ------------------------------------------------------ Devalidate title cache
11793:
11794: sub devalidate_title_cache {
11795: my ($url)=@_;
11796: if (!$env{'request.course.id'}) { return; }
11797: my $symb=&symbread($url);
11798: if (!$symb) { return; }
11799: my $key=$env{'request.course.id'}."\0".$symb;
11800: &devalidate_cache_new('title',$key);
11801: }
11802:
1.1014 droeschl 11803: # ------------------------------------------------- Get the title of a course
11804:
11805: sub current_course_title {
11806: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
11807: }
1.301 www 11808: # ------------------------------------------------- Get the title of a resource
11809:
11810: sub gettitle {
11811: my $urlsymb=shift;
11812: my $symb=&symbread($urlsymb);
1.534 albertel 11813: if ($symb) {
1.620 albertel 11814: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 11815: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 11816: if (defined($cached)) {
11817: return $result;
11818: }
1.534 albertel 11819: my ($map,$resid,$url)=&decode_symb($symb);
11820: my $title='';
1.907 albertel 11821: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
11822: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
11823: } else {
11824: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11825: &GDBM_READER(),0640)) {
11826: my $mapid=$bighash{'map_pc_'.&clutter($map)};
11827: $title=$bighash{'title_'.$mapid.'.'.$resid};
11828: untie(%bighash);
11829: }
1.534 albertel 11830: }
11831: $title=~s/\&colon\;/\:/gs;
11832: if ($title) {
1.1159 www 11833: # Remember both $symb and $title for dynamic metadata
11834: $accesshash{$symb.'___crstitle'}=$title;
1.1161 www 11835: $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159 www 11836: # Cache this title and then return it
1.599 albertel 11837: return &do_cache_new('title',$key,$title,600);
1.534 albertel 11838: }
11839: $urlsymb=$url;
11840: }
11841: my $title=&metadata($urlsymb,'title');
11842: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
11843: return $title;
1.301 www 11844: }
1.613 albertel 11845:
1.614 albertel 11846: sub get_slot {
11847: my ($which,$cnum,$cdom)=@_;
11848: if (!$cnum || !$cdom) {
1.790 albertel 11849: (undef,my $courseid)=&whichuser();
1.620 albertel 11850: $cdom=$env{'course.'.$courseid.'.domain'};
11851: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 11852: }
1.703 albertel 11853: my $key=join("\0",'slots',$cdom,$cnum,$which);
11854: my %slotinfo;
11855: if (exists($remembered{$key})) {
11856: $slotinfo{$which} = $remembered{$key};
11857: } else {
11858: %slotinfo=&get('slots',[$which],$cdom,$cnum);
11859: &Apache::lonhomework::showhash(%slotinfo);
11860: my ($tmp)=keys(%slotinfo);
11861: if ($tmp=~/^error:/) { return (); }
11862: $remembered{$key} = $slotinfo{$which};
11863: }
1.616 albertel 11864: if (ref($slotinfo{$which}) eq 'HASH') {
11865: return %{$slotinfo{$which}};
11866: }
11867: return $slotinfo{$which};
1.614 albertel 11868: }
1.1150 raeburn 11869:
11870: sub get_reservable_slots {
11871: my ($cnum,$cdom,$uname,$udom) = @_;
11872: my $now = time;
11873: my $reservable_info;
11874: my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
11875: if (exists($remembered{$key})) {
11876: $reservable_info = $remembered{$key};
11877: } else {
11878: my %resv;
11879: ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
11880: &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
11881: $reservable_info = \%resv;
11882: $remembered{$key} = $reservable_info;
11883: }
11884: return $reservable_info;
11885: }
11886:
11887: sub get_course_slots {
11888: my ($cnum,$cdom) = @_;
11889: my $hashid=$cnum.':'.$cdom;
11890: my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
11891: if (defined($cached)) {
11892: if (ref($result) eq 'HASH') {
11893: return %{$result};
11894: }
11895: } else {
11896: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
11897: my ($tmp) = keys(%slots);
11898: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219 raeburn 11899: &do_cache_new('allslots',$hashid,\%slots,600);
1.1150 raeburn 11900: return %slots;
11901: }
11902: }
11903: return;
11904: }
11905:
11906: sub devalidate_slots_cache {
11907: my ($cnum,$cdom)=@_;
11908: my $hashid=$cnum.':'.$cdom;
11909: &devalidate_cache_new('allslots',$hashid);
11910: }
11911:
1.1181 raeburn 11912: sub get_coursechange {
11913: my ($cdom,$cnum) = @_;
11914: if ($cdom eq '' || $cnum eq '') {
11915: return unless ($env{'request.course.id'});
11916: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
11917: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
11918: }
11919: my $hashid=$cdom.'_'.$cnum;
11920: my ($change,$cached)=&is_cached_new('crschange',$hashid);
11921: if ((defined($cached)) && ($change ne '')) {
11922: return $change;
11923: } else {
11924: my %crshash;
11925: %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
11926: if ($crshash{'internal.contentchange'} eq '') {
11927: $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
11928: if ($change eq '') {
11929: %crshash = &get('environment',['internal.created'],$cdom,$cnum);
11930: $change = $crshash{'internal.created'};
11931: }
11932: } else {
11933: $change = $crshash{'internal.contentchange'};
11934: }
11935: my $cachetime = 600;
11936: &do_cache_new('crschange',$hashid,$change,$cachetime);
11937: }
11938: return $change;
11939: }
11940:
11941: sub devalidate_coursechange_cache {
11942: my ($cnum,$cdom)=@_;
11943: my $hashid=$cnum.':'.$cdom;
11944: &devalidate_cache_new('crschange',$hashid);
11945: }
11946:
1.31 www 11947: # ------------------------------------------------- Update symbolic store links
11948:
11949: sub symblist {
11950: my ($mapname,%newhash)=@_;
1.438 www 11951: $mapname=&deversion(&declutter($mapname));
1.31 www 11952: my %hash;
1.620 albertel 11953: if (($env{'request.course.fn'}) && (%newhash)) {
11954: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 11955: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 11956: foreach my $url (keys(%newhash)) {
1.711 albertel 11957: next if ($url eq 'last_known'
11958: && $env{'form.no_update_last_known'});
11959: $hash{declutter($url)}=&encode_symb($mapname,
11960: $newhash{$url}->[1],
11961: $newhash{$url}->[0]);
1.191 harris41 11962: }
1.31 www 11963: if (untie(%hash)) {
11964: return 'ok';
11965: }
11966: }
11967: }
11968: return 'error';
1.212 www 11969: }
11970:
11971: # --------------------------------------------------------------- Verify a symb
11972:
11973: sub symbverify {
1.1190 raeburn 11974: my ($symb,$thisurl,$encstate)=@_;
1.510 www 11975: my $thisfn=$thisurl;
1.439 www 11976: $thisfn=&declutter($thisfn);
1.215 www 11977: # direct jump to resource in page or to a sequence - will construct own symbs
11978: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
11979: # check URL part
1.409 www 11980: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 11981:
1.431 www 11982: unless ($url eq $thisfn) { return 0; }
1.213 www 11983:
1.216 www 11984: $symb=&symbclean($symb);
1.510 www 11985: $thisurl=&deversion($thisurl);
1.439 www 11986: $thisfn=&deversion($thisfn);
1.213 www 11987:
11988: my %bighash;
11989: my $okay=0;
1.431 www 11990:
1.620 albertel 11991: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 11992: &GDBM_READER(),0640)) {
1.1204 raeburn 11993: my $noclutter;
1.1032 raeburn 11994: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
11995: $thisurl =~ s/\?.+$//;
1.1204 raeburn 11996: if ($map =~ m{^uploaded/.+\.page$}) {
11997: $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
11998: $thisurl =~ s{^\Qhttp://https://\E}{https://};
11999: $noclutter = 1;
12000: }
12001: }
12002: my $ids;
12003: if ($noclutter) {
12004: $ids=$bighash{'ids_'.$thisurl};
12005: } else {
12006: $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032 raeburn 12007: }
1.1102 raeburn 12008: unless ($ids) {
12009: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
12010: $ids=$bighash{$idkey};
1.216 www 12011: }
12012: if ($ids) {
12013: # ------------------------------------------------------------------- Has ID(s)
1.1202 raeburn 12014: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203 raeburn 12015: $symb =~ s/\?.+$//;
1.1202 raeburn 12016: }
1.800 albertel 12017: foreach my $id (split(/\,/,$ids)) {
12018: my ($mapid,$resid)=split(/\./,$id);
1.216 www 12019: if (
12020: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190 raeburn 12021: eq $symb) {
12022: if (ref($encstate)) {
12023: $$encstate = $bighash{'encrypted_'.$id};
12024: }
1.620 albertel 12025: if (($env{'request.role.adv'}) ||
1.1101 raeburn 12026: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
12027: ($thisurl eq '/adm/navmaps')) {
1.1190 raeburn 12028: $okay=1;
1.1202 raeburn 12029: last;
1.582 albertel 12030: }
12031: }
1.216 www 12032: }
12033: }
1.213 www 12034: untie(%bighash);
12035: }
12036: return $okay;
1.31 www 12037: }
12038:
1.210 www 12039: # --------------------------------------------------------------- Clean-up symb
12040:
12041: sub symbclean {
12042: my $symb=shift;
1.568 albertel 12043: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 12044: # remove version from map
12045: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 12046:
1.210 www 12047: # remove version from URL
12048: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 12049:
1.507 www 12050: # remove wrapper
12051:
1.510 www 12052: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 12053: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 12054: return $symb;
1.409 www 12055: }
12056:
12057: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 12058:
12059: sub encode_symb {
12060: my ($map,$resid,$url)=@_;
12061: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
12062: }
1.409 www 12063:
12064: sub decode_symb {
1.568 albertel 12065: my $symb=shift;
12066: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
12067: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 12068: return (&fixversion($map),$resid,&fixversion($url));
12069: }
12070:
12071: sub fixversion {
12072: my $fn=shift;
1.609 banghart 12073: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 12074: my %bighash;
12075: my $uri=&clutter($fn);
1.620 albertel 12076: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 12077: # is this cached?
1.599 albertel 12078: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 12079: if (defined($cached)) { return $result; }
12080: # unfortunately not cached, or expired
1.620 albertel 12081: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 12082: &GDBM_READER(),0640)) {
12083: if ($bighash{'version_'.$uri}) {
12084: my $version=$bighash{'version_'.$uri};
1.444 www 12085: unless (($version eq 'mostrecent') ||
12086: ($version==&getversion($uri))) {
1.440 www 12087: $uri=~s/\.(\w+)$/\.$version\.$1/;
12088: }
12089: }
12090: untie %bighash;
1.413 www 12091: }
1.599 albertel 12092: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 12093: }
12094:
12095: sub deversion {
12096: my $url=shift;
12097: $url=~s/\.\d+\.(\w+)$/\.$1/;
12098: return $url;
1.210 www 12099: }
12100:
1.31 www 12101: # ------------------------------------------------------ Return symb list entry
12102:
12103: sub symbread {
1.1282 raeburn 12104: my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
1.1265 raeburn 12105: my $cache_str='request.symbread.cached.'.$thisfn;
1.1282 raeburn 12106: if (defined($env{$cache_str})) {
12107: if ($ignorecachednull) {
12108: return $env{$cache_str} unless ($env{$cache_str} eq '');
12109: } else {
12110: return $env{$cache_str};
12111: }
12112: }
1.242 www 12113: # no filename provided? try from environment
1.1265 raeburn 12114: unless ($thisfn) {
1.620 albertel 12115: if ($env{'request.symb'}) {
12116: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 12117: }
1.620 albertel 12118: $thisfn=$env{'request.filename'};
1.44 www 12119: }
1.569 albertel 12120: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 12121: # is that filename actually a symb? Verify, clean, and return
12122: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 12123: if (&symbverify($thisfn,$1)) {
1.620 albertel 12124: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 12125: }
1.242 www 12126: }
1.44 www 12127: $thisfn=declutter($thisfn);
1.31 www 12128: my %hash;
1.37 www 12129: my %bighash;
12130: my $syval='';
1.620 albertel 12131: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 12132: my $targetfn = $thisfn;
1.609 banghart 12133: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 12134: $targetfn = 'adm/wrapper/'.$thisfn;
12135: }
1.687 albertel 12136: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
12137: $targetfn=$1;
12138: }
1.620 albertel 12139: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 12140: &GDBM_READER(),0640)) {
1.481 raeburn 12141: $syval=$hash{$targetfn};
1.37 www 12142: untie(%hash);
12143: }
12144: # ---------------------------------------------------------- There was an entry
12145: if ($syval) {
1.601 albertel 12146: #unless ($syval=~/\_\d+$/) {
1.620 albertel 12147: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 12148: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 12149: #return $env{$cache_str}='';
1.601 albertel 12150: #}
12151: #$syval.=$1;
12152: #}
1.37 www 12153: } else {
12154: # ------------------------------------------------------- Was not in symb table
1.620 albertel 12155: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 12156: &GDBM_READER(),0640)) {
1.37 www 12157: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 12158: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 12159: unless ($ids) {
12160: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 12161: }
12162: unless ($ids) {
12163: # alias?
12164: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 12165: }
1.37 www 12166: if ($ids) {
12167: # ------------------------------------------------------------------- Has ID(s)
12168: my @possibilities=split(/\,/,$ids);
1.39 www 12169: if ($#possibilities==0) {
12170: # ----------------------------------------------- There is only one possibility
1.37 www 12171: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 12172: $syval=&encode_symb($bighash{'map_id_'.$mapid},
12173: $resid,$thisfn);
1.1282 raeburn 12174: if (ref($possibles) eq 'HASH') {
12175: $possibles->{$syval} = 1;
12176: }
12177: if ($checkforblock) {
12178: my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
12179: if (@blockers) {
12180: $syval = '';
12181: return;
12182: }
12183: }
12184: } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
1.39 www 12185: # ------------------------------------------ There is more than one possibility
12186: my $realpossible=0;
1.800 albertel 12187: foreach my $id (@possibilities) {
12188: my $file=$bighash{'src_'.$id};
1.1282 raeburn 12189: my $canaccess;
12190: if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
12191: $canaccess = 1;
12192: } else {
12193: $canaccess = &allowed('bre',$file);
12194: }
12195: if ($canaccess) {
12196: my ($mapid,$resid)=split(/\./,$id);
12197: if ($bighash{'map_type_'.$mapid} ne 'page') {
12198: my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
12199: $resid,$thisfn);
12200: if (ref($possibles) eq 'HASH') {
12201: $possibles->{$syval} = 1;
12202: }
12203: if ($checkforblock) {
12204: my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
12205: unless (@blockers > 0) {
12206: $syval = $poss_syval;
12207: $realpossible++;
12208: }
12209: } else {
12210: $syval = $poss_syval;
12211: $realpossible++;
12212: }
12213: }
1.39 www 12214: }
1.191 harris41 12215: }
1.39 www 12216: if ($realpossible!=1) { $syval=''; }
1.249 www 12217: } else {
12218: $syval='';
1.37 www 12219: }
12220: }
1.1282 raeburn 12221: untie(%bighash);
1.481 raeburn 12222: }
1.31 www 12223: }
1.62 www 12224: if ($syval) {
1.620 albertel 12225: return $env{$cache_str}=$syval;
1.62 www 12226: }
1.31 www 12227: }
1.949 raeburn 12228: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 12229: return $env{$cache_str}='';
1.31 www 12230: }
12231:
12232: # ---------------------------------------------------------- Return random seed
12233:
1.32 www 12234: sub numval {
12235: my $txt=shift;
12236: $txt=~tr/A-J/0-9/;
12237: $txt=~tr/a-j/0-9/;
12238: $txt=~tr/K-T/0-9/;
12239: $txt=~tr/k-t/0-9/;
12240: $txt=~tr/U-Z/0-5/;
12241: $txt=~tr/u-z/0-5/;
12242: $txt=~s/\D//g;
1.564 albertel 12243: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 12244: return int($txt);
1.368 albertel 12245: }
12246:
1.484 albertel 12247: sub numval2 {
12248: my $txt=shift;
12249: $txt=~tr/A-J/0-9/;
12250: $txt=~tr/a-j/0-9/;
12251: $txt=~tr/K-T/0-9/;
12252: $txt=~tr/k-t/0-9/;
12253: $txt=~tr/U-Z/0-5/;
12254: $txt=~tr/u-z/0-5/;
12255: $txt=~s/\D//g;
12256: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12257: my $total;
12258: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 12259: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 12260: return int($total);
12261: }
12262:
1.575 albertel 12263: sub numval3 {
12264: use integer;
12265: my $txt=shift;
12266: $txt=~tr/A-J/0-9/;
12267: $txt=~tr/a-j/0-9/;
12268: $txt=~tr/K-T/0-9/;
12269: $txt=~tr/k-t/0-9/;
12270: $txt=~tr/U-Z/0-5/;
12271: $txt=~tr/u-z/0-5/;
12272: $txt=~s/\D//g;
12273: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
12274: my $total;
12275: foreach my $val (@txts) { $total+=$val; }
12276: if ($_64bit) { $total=(($total<<32)>>32); }
12277: return $total;
12278: }
12279:
1.675 albertel 12280: sub digest {
12281: my ($data)=@_;
12282: my $digest=&Digest::MD5::md5($data);
12283: my ($a,$b,$c,$d)=unpack("iiii",$digest);
12284: my ($e,$f);
12285: {
12286: use integer;
12287: $e=($a+$b);
12288: $f=($c+$d);
12289: if ($_64bit) {
12290: $e=(($e<<32)>>32);
12291: $f=(($f<<32)>>32);
12292: }
12293: }
12294: if (wantarray) {
12295: return ($e,$f);
12296: } else {
12297: my $g;
12298: {
12299: use integer;
12300: $g=($e+$f);
12301: if ($_64bit) {
12302: $g=(($g<<32)>>32);
12303: }
12304: }
12305: return $g;
12306: }
12307: }
12308:
1.368 albertel 12309: sub latest_rnd_algorithm_id {
1.675 albertel 12310: return '64bit5';
1.366 albertel 12311: }
1.32 www 12312:
1.503 albertel 12313: sub get_rand_alg {
12314: my ($courseid)=@_;
1.790 albertel 12315: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 12316: if ($courseid) {
1.620 albertel 12317: return $env{"course.$courseid.rndseed"};
1.503 albertel 12318: }
12319: return &latest_rnd_algorithm_id();
12320: }
12321:
1.562 albertel 12322: sub validCODE {
12323: my ($CODE)=@_;
12324: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
12325: return 0;
12326: }
12327:
1.491 albertel 12328: sub getCODE {
1.620 albertel 12329: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 12330: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
12331: defined($Apache::lonhomework::parsing_a_task) ) &&
12332: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 12333: return $Apache::lonhomework::history{'resource.CODE'};
12334: }
12335: return undef;
12336: }
1.1133 foxr 12337: #
12338: # Determines the random seed for a specific context:
12339: #
12340: # parameters:
12341: # symb - in course context the symb for the seed.
12342: # course_id - The course id of the form domain_coursenum.
12343: # domain - Domain for the user.
12344: # course - Course for the user.
12345: # cenv - environment of the course.
12346: #
12347: # NOTE:
12348: # All parameters are picked out of the environment if missing
12349: # or not defined.
12350: # If a symb cannot be determined the current time is used instead.
12351: #
12352: # For a given well defined symb, courside, domain, username,
12353: # and course environment, the seed is reproducible.
12354: #
1.31 www 12355: sub rndseed {
1.1133 foxr 12356: my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790 albertel 12357: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 12358: if (!defined($symb)) {
1.366 albertel 12359: unless ($symb=$wsymb) { return time; }
12360: }
1.1146 foxr 12361: if (!defined $courseid) {
12362: $courseid=$wcourseid;
12363: }
12364: if (!defined $domain) { $domain=$wdomain; }
12365: if (!defined $username) { $username=$wusername }
1.1133 foxr 12366:
12367: my $which;
12368: if (defined($cenv->{'rndseed'})) {
12369: $which = $cenv->{'rndseed'};
12370: } else {
12371: $which =&get_rand_alg($courseid);
12372: }
1.491 albertel 12373: if (defined(&getCODE())) {
1.1133 foxr 12374:
1.675 albertel 12375: if ($which eq '64bit5') {
12376: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
12377: } elsif ($which eq '64bit4') {
1.575 albertel 12378: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
12379: } else {
12380: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
12381: }
1.675 albertel 12382: } elsif ($which eq '64bit5') {
12383: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 12384: } elsif ($which eq '64bit4') {
12385: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 12386: } elsif ($which eq '64bit3') {
12387: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 12388: } elsif ($which eq '64bit2') {
12389: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 12390: } elsif ($which eq '64bit') {
12391: return &rndseed_64bit($symb,$courseid,$domain,$username);
12392: }
12393: return &rndseed_32bit($symb,$courseid,$domain,$username);
12394: }
12395:
12396: sub rndseed_32bit {
12397: my ($symb,$courseid,$domain,$username)=@_;
12398: {
12399: use integer;
12400: my $symbchck=unpack("%32C*",$symb) << 27;
12401: my $symbseed=numval($symb) << 22;
12402: my $namechck=unpack("%32C*",$username) << 17;
12403: my $nameseed=numval($username) << 12;
12404: my $domainseed=unpack("%32C*",$domain) << 7;
12405: my $courseseed=unpack("%32C*",$courseid);
12406: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 12407: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12408: #&logthis("rndseed :$num:$symb");
1.564 albertel 12409: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 12410: return $num;
12411: }
12412: }
12413:
12414: sub rndseed_64bit {
12415: my ($symb,$courseid,$domain,$username)=@_;
12416: {
12417: use integer;
12418: my $symbchck=unpack("%32S*",$symb) << 21;
12419: my $symbseed=numval($symb) << 10;
12420: my $namechck=unpack("%32S*",$username);
12421:
12422: my $nameseed=numval($username) << 21;
12423: my $domainseed=unpack("%32S*",$domain) << 10;
12424: my $courseseed=unpack("%32S*",$courseid);
12425:
12426: my $num1=$symbchck+$symbseed+$namechck;
12427: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12428: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12429: #&logthis("rndseed :$num:$symb");
1.564 albertel 12430: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 12431: return "$num1,$num2";
1.155 albertel 12432: }
1.366 albertel 12433: }
12434:
1.443 albertel 12435: sub rndseed_64bit2 {
12436: my ($symb,$courseid,$domain,$username)=@_;
12437: {
12438: use integer;
12439: # strings need to be an even # of cahracters long, it it is odd the
12440: # last characters gets thrown away
12441: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12442: my $symbseed=numval($symb) << 10;
12443: my $namechck=unpack("%32S*",$username.' ');
12444:
12445: my $nameseed=numval($username) << 21;
1.501 albertel 12446: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12447: my $courseseed=unpack("%32S*",$courseid.' ');
12448:
12449: my $num1=$symbchck+$symbseed+$namechck;
12450: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12451: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12452: #&logthis("rndseed :$num:$symb");
1.803 albertel 12453: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 12454: return "$num1,$num2";
12455: }
12456: }
12457:
12458: sub rndseed_64bit3 {
12459: my ($symb,$courseid,$domain,$username)=@_;
12460: {
12461: use integer;
12462: # strings need to be an even # of cahracters long, it it is odd the
12463: # last characters gets thrown away
12464: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12465: my $symbseed=numval2($symb) << 10;
12466: my $namechck=unpack("%32S*",$username.' ');
12467:
12468: my $nameseed=numval2($username) << 21;
1.443 albertel 12469: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12470: my $courseseed=unpack("%32S*",$courseid.' ');
12471:
12472: my $num1=$symbchck+$symbseed+$namechck;
12473: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12474: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12475: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 12476: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 12477:
1.503 albertel 12478: return "$num1:$num2";
1.443 albertel 12479: }
12480: }
12481:
1.575 albertel 12482: sub rndseed_64bit4 {
12483: my ($symb,$courseid,$domain,$username)=@_;
12484: {
12485: use integer;
12486: # strings need to be an even # of cahracters long, it it is odd the
12487: # last characters gets thrown away
12488: my $symbchck=unpack("%32S*",$symb.' ') << 21;
12489: my $symbseed=numval3($symb) << 10;
12490: my $namechck=unpack("%32S*",$username.' ');
12491:
12492: my $nameseed=numval3($username) << 21;
12493: my $domainseed=unpack("%32S*",$domain.' ') << 10;
12494: my $courseseed=unpack("%32S*",$courseid.' ');
12495:
12496: my $num1=$symbchck+$symbseed+$namechck;
12497: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 12498: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
12499: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 12500: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110 www 12501:
1.575 albertel 12502: return "$num1:$num2";
12503: }
12504: }
12505:
1.675 albertel 12506: sub rndseed_64bit5 {
12507: my ($symb,$courseid,$domain,$username)=@_;
12508: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
12509: return "$num1:$num2";
12510: }
12511:
1.366 albertel 12512: sub rndseed_CODE_64bit {
12513: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 12514: {
1.366 albertel 12515: use integer;
1.443 albertel 12516: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 12517: my $symbseed=numval2($symb);
1.491 albertel 12518: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12519: my $CODEseed=numval(&getCODE());
1.443 albertel 12520: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 12521: my $num1=$symbseed+$CODEchck;
12522: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 12523: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12524: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 12525: if ($_64bit) { $num1=(($num1<<32)>>32); }
12526: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 12527: return "$num1:$num2";
1.366 albertel 12528: }
12529: }
12530:
1.575 albertel 12531: sub rndseed_CODE_64bit4 {
12532: my ($symb,$courseid,$domain,$username)=@_;
12533: {
12534: use integer;
12535: my $symbchck=unpack("%32S*",$symb.' ') << 16;
12536: my $symbseed=numval3($symb);
12537: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
12538: my $CODEseed=numval3(&getCODE());
12539: my $courseseed=unpack("%32S*",$courseid.' ');
12540: my $num1=$symbseed+$CODEchck;
12541: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 12542: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
12543: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 12544: if ($_64bit) { $num1=(($num1<<32)>>32); }
12545: if ($_64bit) { $num2=(($num2<<32)>>32); }
12546: return "$num1:$num2";
12547: }
12548: }
12549:
1.675 albertel 12550: sub rndseed_CODE_64bit5 {
12551: my ($symb,$courseid,$domain,$username)=@_;
12552: my $code = &getCODE();
12553: my ($num1,$num2)=&digest("$symb,$courseid,$code");
12554: return "$num1:$num2";
12555: }
12556:
1.366 albertel 12557: sub setup_random_from_rndseed {
12558: my ($rndseed)=@_;
1.503 albertel 12559: if ($rndseed =~/([,:])/) {
1.1262 raeburn 12560: my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
12561: if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
12562: &Math::Random::random_set_seed_from_phrase($rndseed);
12563: } else {
12564: &Math::Random::random_set_seed($num1,$num2);
12565: }
1.366 albertel 12566: } else {
12567: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 12568: }
1.36 albertel 12569: }
12570:
1.474 albertel 12571: sub latest_receipt_algorithm_id {
1.835 albertel 12572: return 'receipt3';
1.474 albertel 12573: }
12574:
1.480 www 12575: sub recunique {
12576: my $fucourseid=shift;
12577: my $unique;
1.835 albertel 12578: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
12579: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 12580: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 12581: } else {
12582: $unique=$perlvar{'lonReceipt'};
12583: }
12584: return unpack("%32C*",$unique);
12585: }
12586:
12587: sub recprefix {
12588: my $fucourseid=shift;
12589: my $prefix;
1.835 albertel 12590: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
12591: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 12592: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 12593: } else {
12594: $prefix=$perlvar{'lonHostID'};
12595: }
12596: return unpack("%32C*",$prefix);
12597: }
12598:
1.76 www 12599: sub ireceipt {
1.474 albertel 12600: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 12601:
12602: my $return =&recprefix($fucourseid).'-';
12603:
12604: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
12605: $env{'request.state'} eq 'construct') {
12606: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
12607: return $return;
12608: }
12609:
1.76 www 12610: my $cuname=unpack("%32C*",$funame);
12611: my $cudom=unpack("%32C*",$fudom);
12612: my $cucourseid=unpack("%32C*",$fucourseid);
12613: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 12614: my $cunique=&recunique($fucourseid);
1.474 albertel 12615: my $cpart=unpack("%32S*",$part);
1.835 albertel 12616: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
12617:
1.790 albertel 12618: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 12619:
12620: $return.= ($cunique%$cuname+
12621: $cunique%$cudom+
12622: $cusymb%$cuname+
12623: $cusymb%$cudom+
12624: $cucourseid%$cuname+
12625: $cucourseid%$cudom+
12626: $cpart%$cuname+
12627: $cpart%$cudom);
12628: } else {
12629: $return.= ($cunique%$cuname+
12630: $cunique%$cudom+
12631: $cusymb%$cuname+
12632: $cusymb%$cudom+
12633: $cucourseid%$cuname+
12634: $cucourseid%$cudom);
12635: }
12636: return $return;
1.76 www 12637: }
12638:
12639: sub receipt {
1.474 albertel 12640: my ($part)=@_;
1.790 albertel 12641: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 12642: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 12643: }
1.260 ng 12644:
1.790 albertel 12645: sub whichuser {
12646: my ($passedsymb)=@_;
12647: my ($symb,$courseid,$domain,$name,$publicuser);
12648: if (defined($env{'form.grade_symb'})) {
12649: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
12650: my $allowed=&allowed('vgr',$tmp_courseid);
12651: if (!$allowed &&
12652: exists($env{'request.course.sec'}) &&
12653: $env{'request.course.sec'} !~ /^\s*$/) {
12654: $allowed=&allowed('vgr',$tmp_courseid.
12655: '/'.$env{'request.course.sec'});
12656: }
12657: if ($allowed) {
12658: ($symb)=&get_env_multiple('form.grade_symb');
12659: $courseid=$tmp_courseid;
12660: ($domain)=&get_env_multiple('form.grade_domain');
12661: ($name)=&get_env_multiple('form.grade_username');
12662: return ($symb,$courseid,$domain,$name,$publicuser);
12663: }
12664: }
12665: if (!$passedsymb) {
12666: $symb=&symbread();
12667: } else {
12668: $symb=$passedsymb;
12669: }
12670: $courseid=$env{'request.course.id'};
12671: $domain=$env{'user.domain'};
12672: $name=$env{'user.name'};
12673: if ($name eq 'public' && $domain eq 'public') {
12674: if (!defined($env{'form.username'})) {
12675: $env{'form.username'}.=time.rand(10000000);
12676: }
12677: $name.=$env{'form.username'};
12678: }
12679: return ($symb,$courseid,$domain,$name,$publicuser);
12680:
12681: }
12682:
1.36 albertel 12683: # ------------------------------------------------------------ Serves up a file
1.472 albertel 12684: # returns either the contents of the file or
12685: # -1 if the file doesn't exist
1.481 raeburn 12686: #
12687: # if the target is a file that was uploaded via DOCS,
12688: # a check will be made to see if a current copy exists on the local server,
12689: # if it does this will be served, otherwise a copy will be retrieved from
12690: # the home server for the course and stored in /home/httpd/html/userfiles on
12691: # the local server.
1.472 albertel 12692:
1.36 albertel 12693: sub getfile {
1.538 albertel 12694: my ($file) = @_;
1.609 banghart 12695: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 12696: &repcopy($file);
12697: return &readfile($file);
12698: }
12699:
12700: sub repcopy_userfile {
12701: my ($file)=@_;
1.1142 raeburn 12702: my $londocroot = $perlvar{'lonDocRoot'};
12703: if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164 raeburn 12704: if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538 albertel 12705: my ($cdom,$cnum,$filename) =
1.811 albertel 12706: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 12707: my $uri="/uploaded/$cdom/$cnum/$filename";
12708: if (-e "$file") {
1.828 www 12709: # we already have a local copy, check it out
1.538 albertel 12710: my @fileinfo = stat($file);
1.828 www 12711: my $rtncode;
12712: my $info;
1.538 albertel 12713: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 12714: if ($lwpresp ne 'ok') {
1.828 www 12715: # there is no such file anymore, even though we had a local copy
1.482 albertel 12716: if ($rtncode eq '404') {
1.538 albertel 12717: unlink($file);
1.482 albertel 12718: }
12719: return -1;
12720: }
12721: if ($info < $fileinfo[9]) {
1.828 www 12722: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 12723: return 'ok';
1.828 www 12724: } else {
12725: # the file is outdated, get rid of it
12726: unlink($file);
1.482 albertel 12727: }
1.828 www 12728: }
12729: # one way or the other, at this point, we don't have the file
12730: # construct the correct path for the file
12731: my @parts = ($cdom,$cnum);
12732: if ($filename =~ m|^(.+)/[^/]+$|) {
12733: push @parts, split(/\//,$1);
12734: }
12735: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
12736: foreach my $part (@parts) {
12737: $path .= '/'.$part;
12738: if (!-e $path) {
12739: mkdir($path,0770);
1.482 albertel 12740: }
12741: }
1.828 www 12742: # now the path exists for sure
12743: # get a user agent
12744: my $ua=new LWP::UserAgent;
12745: my $transferfile=$file.'.in.transfer';
12746: # FIXME: this should flock
12747: if (-e $transferfile) { return 'ok'; }
12748: my $request;
12749: $uri=~s/^\///;
1.980 raeburn 12750: my $homeserver = &homeserver($cnum,$cdom);
12751: my $protocol = $protocol{$homeserver};
12752: $protocol = 'http' if ($protocol ne 'https');
12753: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 12754: my $response=$ua->request($request,$transferfile);
12755: # did it work?
12756: if ($response->is_error()) {
12757: unlink($transferfile);
12758: &logthis("Userfile repcopy failed for $uri");
12759: return -1;
12760: }
12761: # worked, rename the transfer file
12762: rename($transferfile,$file);
1.607 raeburn 12763: return 'ok';
1.481 raeburn 12764: }
12765:
1.517 albertel 12766: sub tokenwrapper {
12767: my $uri=shift;
1.980 raeburn 12768: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 12769: $uri=~s|^/||;
1.620 albertel 12770: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 12771: my $token=$1;
1.552 albertel 12772: my (undef,$udom,$uname,$file)=split('/',$uri,4);
12773: if ($udom && $uname && $file) {
12774: $file=~s|(\?\.*)*$||;
1.949 raeburn 12775: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 12776: my $homeserver = &homeserver($uname,$udom);
12777: my $protocol = $protocol{$homeserver};
12778: $protocol = 'http' if ($protocol ne 'https');
12779: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 12780: (($uri=~/\?/)?'&':'?').'token='.$token.
12781: '&tokenissued='.$perlvar{'lonHostID'};
12782: } else {
12783: return '/adm/notfound.html';
12784: }
12785: }
12786:
1.828 www 12787: # call with reqtype HEAD: get last modification time
12788: # call with reqtype GET: get the file contents
12789: # Do not call this with reqtype GET for large files! It loads everything into memory
12790: #
1.481 raeburn 12791: sub getuploaded {
12792: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
12793: $uri=~s/^\///;
1.980 raeburn 12794: my $homeserver = &homeserver($cnum,$cdom);
12795: my $protocol = $protocol{$homeserver};
12796: $protocol = 'http' if ($protocol ne 'https');
12797: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 12798: my $ua=new LWP::UserAgent;
12799: my $request=new HTTP::Request($reqtype,$uri);
12800: my $response=$ua->request($request);
12801: $$rtncode = $response->code;
1.482 albertel 12802: if (! $response->is_success()) {
12803: return 'failed';
12804: }
12805: if ($reqtype eq 'HEAD') {
1.486 www 12806: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 12807: } elsif ($reqtype eq 'GET') {
12808: $$info = $response->content;
1.472 albertel 12809: }
1.482 albertel 12810: return 'ok';
1.36 albertel 12811: }
12812:
1.481 raeburn 12813: sub readfile {
12814: my $file = shift;
12815: if ( (! -e $file ) || ($file eq '') ) { return -1; };
12816: my $fh;
12817: open($fh,"<$file");
12818: my $a='';
1.800 albertel 12819: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 12820: return $a;
12821: }
12822:
1.36 albertel 12823: sub filelocation {
1.590 banghart 12824: my ($dir,$file) = @_;
12825: my $location;
12826: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 12827:
12828: if ($file =~ m-^/adm/-) {
12829: $file=~s-^/adm/wrapper/-/-;
12830: $file=~s-^/adm/coursedocs/showdoc/-/-;
12831: }
1.882 albertel 12832:
1.1139 www 12833: if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956 raeburn 12834: $location = $file;
1.609 banghart 12835: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 12836: my ($udom,$uname,$filename)=
1.811 albertel 12837: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 12838: my $home=&homeserver($uname,$udom);
12839: my $is_me=0;
12840: my @ids=¤t_machine_ids();
12841: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
12842: if ($is_me) {
1.1117 foxr 12843: $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 12844: } else {
12845: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
12846: $udom.'/'.$uname.'/'.$filename;
12847: }
1.882 albertel 12848: } elsif ($file =~ m-^/adm/-) {
12849: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 12850: } else {
12851: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139 www 12852: $file=~s:^/(res|priv)/:/:;
12853: my $space=$1;
1.590 banghart 12854: if ( !( $file =~ m:^/:) ) {
12855: $location = $dir. '/'.$file;
12856: } else {
1.1142 raeburn 12857: $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590 banghart 12858: }
1.59 albertel 12859: }
1.590 banghart 12860: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 12861: while ($location=~m{/\.\./}) {
12862: if ($location =~ m{/[^/]+/\.\./}) {
12863: $location=~ s{/[^/]+/\.\./}{/}g;
12864: } else {
12865: $location=~ s{/\.\./}{/}g;
12866: }
12867: } #remove dir/..
1.590 banghart 12868: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
12869: return $location;
1.46 www 12870: }
1.36 albertel 12871:
1.46 www 12872: sub hreflocation {
12873: my ($dir,$file)=@_;
1.980 raeburn 12874: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 12875: $file=filelocation($dir,$file);
1.700 albertel 12876: } elsif ($file=~m-^/adm/-) {
12877: $file=~s-^/adm/wrapper/-/-;
12878: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 12879: }
12880: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
12881: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
12882: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143 raeburn 12883: $file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
12884: {/uploaded/$1/$2/}x;
1.46 www 12885: }
1.913 albertel 12886: if ($file=~ m{^/userfiles/}) {
12887: $file =~ s{^/userfiles/}{/uploaded/};
12888: }
1.462 albertel 12889: return $file;
1.465 albertel 12890: }
12891:
1.1139 www 12892:
12893:
12894:
12895:
1.465 albertel 12896: sub current_machine_domains {
1.853 albertel 12897: return &machine_domains(&hostname($perlvar{'lonHostID'}));
12898: }
12899:
12900: sub machine_domains {
12901: my ($hostname) = @_;
1.465 albertel 12902: my @domains;
1.838 albertel 12903: my %hostname = &all_hostnames();
1.465 albertel 12904: while( my($id, $name) = each(%hostname)) {
1.467 matthew 12905: # &logthis("-$id-$name-$hostname-");
1.465 albertel 12906: if ($hostname eq $name) {
1.844 albertel 12907: push(@domains,&host_domain($id));
1.465 albertel 12908: }
12909: }
12910: return @domains;
12911: }
12912:
12913: sub current_machine_ids {
1.853 albertel 12914: return &machine_ids(&hostname($perlvar{'lonHostID'}));
12915: }
12916:
12917: sub machine_ids {
12918: my ($hostname) = @_;
12919: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 12920: my @ids;
1.888 albertel 12921: my %name_to_host = &all_names();
1.889 albertel 12922: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
12923: return @{ $name_to_host{$hostname} };
12924: }
12925: return;
1.31 www 12926: }
12927:
1.824 raeburn 12928: sub additional_machine_domains {
12929: my @domains;
12930: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
12931: while( my $line = <$fh>) {
12932: $line =~ s/\s//g;
12933: push(@domains,$line);
12934: }
12935: return @domains;
12936: }
12937:
12938: sub default_login_domain {
12939: my $domain = $perlvar{'lonDefDomain'};
12940: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
12941: foreach my $posdom (¤t_machine_domains(),
12942: &additional_machine_domains()) {
12943: if (lc($posdom) eq lc($testdomain)) {
12944: $domain=$posdom;
12945: last;
12946: }
12947: }
12948: return $domain;
12949: }
12950:
1.31 www 12951: # ------------------------------------------------------------- Declutters URLs
12952:
12953: sub declutter {
12954: my $thisfn=shift;
1.569 albertel 12955: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261 raeburn 12956: unless ($thisfn=~m{^/home/httpd/html/priv/}) {
12957: $thisfn=~s{^/home/httpd/html}{};
12958: }
1.31 www 12959: $thisfn=~s/^\///;
1.697 albertel 12960: $thisfn=~s|^adm/wrapper/||;
12961: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 12962: $thisfn=~s/^res\///;
1.1172 bisitz 12963: $thisfn=~s/^priv\///;
1.1032 raeburn 12964: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
12965: $thisfn=~s/\?.+$//;
12966: }
1.268 www 12967: return $thisfn;
12968: }
12969:
12970: # ------------------------------------------------------------- Clutter up URLs
12971:
12972: sub clutter {
12973: my $thisfn='/'.&declutter(shift);
1.887 albertel 12974: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 12975: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 12976: $thisfn='/res'.$thisfn;
12977: }
1.1031 raeburn 12978: if ($thisfn !~m|^/adm|) {
12979: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 12980: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 12981: } else {
12982: my ($ext) = ($thisfn =~ /\.(\w+)$/);
12983: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 12984: if ($embstyle eq 'ssi'
12985: || ($embstyle eq 'hdn')
12986: || ($embstyle eq 'rat')
12987: || ($embstyle eq 'prv')
12988: || ($embstyle eq 'ign')) {
12989: #do nothing with these
12990: } elsif (($embstyle eq 'img')
1.695 albertel 12991: || ($embstyle eq 'emb')
12992: || ($embstyle eq 'wrp')) {
12993: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 12994: } elsif ($embstyle eq 'unk'
12995: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 12996: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 12997: } else {
1.718 www 12998: # &logthis("Got a blank emb style");
1.695 albertel 12999: }
1.694 albertel 13000: }
1.1343 ! raeburn 13001: } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298 raeburn 13002: $thisfn='/adm/wrapper'.$thisfn;
1.694 albertel 13003: }
1.31 www 13004: return $thisfn;
1.12 www 13005: }
13006:
1.787 albertel 13007: sub clutter_with_no_wrapper {
13008: my $uri = &clutter(shift);
13009: if ($uri =~ m-^/adm/-) {
13010: $uri =~ s-^/adm/wrapper/-/-;
13011: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
13012: }
13013: return $uri;
13014: }
13015:
1.557 albertel 13016: sub freeze_escape {
13017: my ($value)=@_;
13018: if (ref($value)) {
13019: $value=&nfreeze($value);
13020: return '__FROZEN__'.&escape($value);
13021: }
13022: return &escape($value);
13023: }
13024:
1.11 www 13025:
1.557 albertel 13026: sub thaw_unescape {
13027: my ($value)=@_;
13028: if ($value =~ /^__FROZEN__/) {
13029: substr($value,0,10,undef);
13030: $value=&unescape($value);
13031: return &thaw($value);
13032: }
13033: return &unescape($value);
13034: }
13035:
1.436 albertel 13036: sub correct_line_ends {
13037: my ($result)=@_;
13038: $$result =~s/\r\n/\n/mg;
13039: $$result =~s/\r/\n/mg;
1.415 albertel 13040: }
1.1 albertel 13041: # ================================================================ Main Program
13042:
1.184 www 13043: sub goodbye {
1.204 albertel 13044: &logthis("Starting Shut down");
1.443 albertel 13045: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 13046: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 13047: #converted
1.599 albertel 13048: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 13049: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
13050: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
13051: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 13052: #1.1 only
1.870 albertel 13053: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
13054: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
13055: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
13056: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
13057: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 13058: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
13059: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 13060: &flushcourselogs();
13061: &logthis("Shutting down");
13062: }
13063:
1.852 albertel 13064: sub get_dns {
1.1210 raeburn 13065: my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869 albertel 13066: if (!$ignore_cache) {
13067: my ($content,$cached)=
13068: &Apache::lonnet::is_cached_new('dns',$url);
13069: if ($cached) {
1.1210 raeburn 13070: &$func($content,$hashref);
1.869 albertel 13071: return;
13072: }
13073: }
13074:
13075: my %alldns;
1.852 albertel 13076: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
13077: foreach my $dns (<$config>) {
13078: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 13079: my $line = $1;
13080: my ($host,$protocol) = split(/:/,$line);
13081: if ($protocol ne 'https') {
13082: $protocol = 'http';
13083: }
13084: $alldns{$host} = $protocol;
1.869 albertel 13085: }
13086: while (%alldns) {
1.1263 raeburn 13087: my ($dns) = sort { $b cmp $a } keys(%alldns);
1.852 albertel 13088: my $ua=new LWP::UserAgent;
1.1134 raeburn 13089: $ua->timeout(30);
1.979 raeburn 13090: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 13091: my $response=$ua->request($request);
1.979 raeburn 13092: delete($alldns{$dns});
1.852 albertel 13093: next if ($response->is_error());
13094: my @content = split("\n",$response->content);
1.1210 raeburn 13095: unless ($nocache) {
1.1219 raeburn 13096: &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1210 raeburn 13097: }
13098: &$func(\@content,$hashref);
1.869 albertel 13099: return;
1.852 albertel 13100: }
13101: close($config);
1.871 albertel 13102: my $which = (split('/',$url))[3];
13103: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
13104: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 13105: my @content = <$config>;
1.1210 raeburn 13106: &$func(\@content,$hashref);
13107: return;
13108: }
13109:
13110: # ------------------------------------------------------Get DNS checksums file
1.1211 raeburn 13111: sub parse_dns_checksums_tab {
1.1210 raeburn 13112: my ($lines,$hashref) = @_;
1.1264 raeburn 13113: my $lonhost = $perlvar{'lonHostID'};
13114: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1210 raeburn 13115: my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264 raeburn 13116: my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
13117: my $webconfdir = '/etc/httpd/conf';
13118: if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
13119: $webconfdir = '/etc/apache2';
13120: } elsif ($distro =~ /^sles(\d+)$/) {
13121: if ($1 >= 10) {
13122: $webconfdir = '/etc/apache2';
13123: }
13124: } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
13125: if ($1 >= 10.0) {
13126: $webconfdir = '/etc/apache2';
13127: }
13128: }
1.1210 raeburn 13129: my ($release,$timestamp) = split(/\-/,$loncaparev);
13130: my (%chksum,%revnum);
13131: if (ref($lines) eq 'ARRAY') {
13132: chomp(@{$lines});
1.1238 raeburn 13133: my $version = shift(@{$lines});
13134: if ($version eq $release) {
1.1210 raeburn 13135: foreach my $line (@{$lines}) {
1.1238 raeburn 13136: my ($file,$version,$shasum) = split(/,/,$line);
1.1264 raeburn 13137: if ($file =~ m{^/etc/httpd/conf}) {
13138: if ($webconfdir eq '/etc/apache2') {
13139: $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
13140: }
13141: }
1.1238 raeburn 13142: $chksum{$file} = $shasum;
13143: $revnum{$file} = $version;
1.1210 raeburn 13144: }
13145: if (ref($hashref) eq 'HASH') {
13146: %{$hashref} = (
13147: sums => \%chksum,
13148: versions => \%revnum,
13149: );
13150: }
13151: }
13152: }
1.869 albertel 13153: return;
1.852 albertel 13154: }
1.1210 raeburn 13155:
13156: sub fetch_dns_checksums {
1.1238 raeburn 13157: my %checksums;
13158: my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1260 raeburn 13159: my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238 raeburn 13160: my ($release,$timestamp) = split(/\-/,$loncaparev);
13161: &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211 raeburn 13162: \%checksums);
1.1210 raeburn 13163: return \%checksums;
13164: }
13165:
1.327 albertel 13166: # ------------------------------------------------------------ Read domain file
13167: {
1.852 albertel 13168: my $loaded;
1.846 albertel 13169: my %domain;
13170:
1.852 albertel 13171: sub parse_domain_tab {
13172: my ($lines) = @_;
13173: foreach my $line (@$lines) {
13174: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 13175:
1.846 albertel 13176: chomp($line);
1.852 albertel 13177: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 13178: my %this_domain;
13179: foreach my $field ('description', 'auth_def', 'auth_arg_def',
13180: 'lang_def', 'city', 'longi', 'lati',
13181: 'primary') {
13182: $this_domain{$field} = shift(@elements);
13183: }
13184: $domain{$name} = \%this_domain;
1.852 albertel 13185: }
13186: }
1.864 albertel 13187:
13188: sub reset_domain_info {
13189: undef($loaded);
13190: undef(%domain);
13191: }
13192:
1.852 albertel 13193: sub load_domain_tab {
1.1293 raeburn 13194: my ($ignore_cache,$nocache) = @_;
13195: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852 albertel 13196: my $fh;
13197: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
13198: my @lines = <$fh>;
13199: &parse_domain_tab(\@lines);
1.448 albertel 13200: }
1.852 albertel 13201: close($fh);
13202: $loaded = 1;
1.327 albertel 13203: }
1.846 albertel 13204:
13205: sub domain {
1.852 albertel 13206: &load_domain_tab() if (!$loaded);
13207:
1.846 albertel 13208: my ($name,$what) = @_;
13209: return if ( !exists($domain{$name}) );
13210:
13211: if (!$what) {
13212: return $domain{$name}{'description'};
13213: }
13214: return $domain{$name}{$what};
13215: }
1.974 raeburn 13216:
13217: sub domain_info {
13218: &load_domain_tab() if (!$loaded);
13219: return %domain;
13220: }
13221:
1.327 albertel 13222: }
13223:
13224:
1.1 albertel 13225: # ------------------------------------------------------------- Read hosts file
13226: {
1.838 albertel 13227: my %hostname;
1.844 albertel 13228: my %hostdom;
1.845 albertel 13229: my %libserv;
1.852 albertel 13230: my $loaded;
1.888 albertel 13231: my %name_to_host;
1.1074 raeburn 13232: my %internetdom;
1.1107 raeburn 13233: my %LC_dns_serv;
1.852 albertel 13234:
13235: sub parse_hosts_tab {
13236: my ($file) = @_;
13237: foreach my $configline (@$file) {
13238: next if ($configline =~ /^(\#|\s*$ )/x);
1.1107 raeburn 13239: chomp($configline);
13240: if ($configline =~ /^\^/) {
13241: if ($configline =~ /^\^([\w.\-]+)/) {
13242: $LC_dns_serv{$1} = 1;
13243: }
13244: next;
13245: }
1.1074 raeburn 13246: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 13247: $name=~s/\s//g;
13248: if ($id && $domain && $role && $name) {
13249: $hostname{$id}=$name;
1.888 albertel 13250: push(@{$name_to_host{$name}}, $id);
1.852 albertel 13251: $hostdom{$id}=$domain;
13252: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 13253: if (defined($protocol)) {
13254: if ($protocol eq 'https') {
13255: $protocol{$id} = $protocol;
13256: } else {
13257: $protocol{$id} = 'http';
13258: }
1.968 raeburn 13259: } else {
1.969 raeburn 13260: $protocol{$id} = 'http';
1.968 raeburn 13261: }
1.1074 raeburn 13262: if (defined($intdom)) {
13263: $internetdom{$id} = $intdom;
13264: }
1.852 albertel 13265: }
13266: }
13267: }
1.864 albertel 13268:
13269: sub reset_hosts_info {
1.897 albertel 13270: &purge_remembered();
1.864 albertel 13271: &reset_domain_info();
13272: &reset_hosts_ip_info();
1.1339 raeburn 13273: undef(%internetdom);
1.892 albertel 13274: undef(%name_to_host);
1.864 albertel 13275: undef(%hostname);
13276: undef(%hostdom);
13277: undef(%libserv);
13278: undef($loaded);
13279: }
1.1 albertel 13280:
1.852 albertel 13281: sub load_hosts_tab {
1.1293 raeburn 13282: my ($ignore_cache,$nocache) = @_;
13283: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.852 albertel 13284: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
13285: my @config = <$config>;
13286: &parse_hosts_tab(\@config);
13287: close($config);
13288: $loaded=1;
1.1 albertel 13289: }
1.852 albertel 13290:
1.838 albertel 13291: sub hostname {
1.852 albertel 13292: &load_hosts_tab() if (!$loaded);
13293:
1.838 albertel 13294: my ($lonid) = @_;
13295: return $hostname{$lonid};
13296: }
1.845 albertel 13297:
1.838 albertel 13298: sub all_hostnames {
1.852 albertel 13299: &load_hosts_tab() if (!$loaded);
13300:
1.838 albertel 13301: return %hostname;
13302: }
1.845 albertel 13303:
1.888 albertel 13304: sub all_names {
1.1293 raeburn 13305: my ($ignore_cache,$nocache) = @_;
13306: &load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888 albertel 13307:
13308: return %name_to_host;
13309: }
13310:
1.974 raeburn 13311: sub all_host_domain {
13312: &load_hosts_tab() if (!$loaded);
13313: return %hostdom;
13314: }
13315:
1.1339 raeburn 13316: sub all_host_intdom {
13317: &load_hosts_tab() if (!$loaded);
13318: return %internetdom;
13319: }
13320:
1.845 albertel 13321: sub is_library {
1.852 albertel 13322: &load_hosts_tab() if (!$loaded);
13323:
1.845 albertel 13324: return exists($libserv{$_[0]});
13325: }
13326:
13327: sub all_library {
1.852 albertel 13328: &load_hosts_tab() if (!$loaded);
13329:
1.845 albertel 13330: return %libserv;
13331: }
13332:
1.1062 droeschl 13333: sub unique_library {
13334: #2x reverse removes all hostnames that appear more than once
13335: my %unique = reverse &all_library();
13336: return reverse %unique;
13337: }
13338:
1.841 albertel 13339: sub get_servers {
1.852 albertel 13340: &load_hosts_tab() if (!$loaded);
13341:
1.841 albertel 13342: my ($domain,$type) = @_;
13343: my %possible_hosts = ($type eq 'library') ? %libserv
13344: : %hostname;
13345: my %result;
1.842 albertel 13346: if (ref($domain) eq 'ARRAY') {
13347: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 13348: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 13349: $result{$host} = $hostname;
13350: }
13351: }
13352: } else {
13353: while ( my ($host,$hostname) = each(%possible_hosts)) {
13354: if ($hostdom{$host} eq $domain) {
13355: $result{$host} = $hostname;
13356: }
1.841 albertel 13357: }
13358: }
13359: return %result;
13360: }
1.845 albertel 13361:
1.1062 droeschl 13362: sub get_unique_servers {
13363: my %unique = reverse &get_servers(@_);
13364: return reverse %unique;
13365: }
13366:
1.844 albertel 13367: sub host_domain {
1.852 albertel 13368: &load_hosts_tab() if (!$loaded);
13369:
1.844 albertel 13370: my ($lonid) = @_;
13371: return $hostdom{$lonid};
13372: }
13373:
1.841 albertel 13374: sub all_domains {
1.852 albertel 13375: &load_hosts_tab() if (!$loaded);
13376:
1.841 albertel 13377: my %seen;
13378: my @uniq = grep(!$seen{$_}++, values(%hostdom));
13379: return @uniq;
13380: }
1.1074 raeburn 13381:
13382: sub internet_dom {
13383: &load_hosts_tab() if (!$loaded);
13384:
13385: my ($lonid) = @_;
13386: return $internetdom{$lonid};
13387: }
1.1107 raeburn 13388:
13389: sub is_LC_dns {
13390: &load_hosts_tab() if (!$loaded);
13391:
13392: my ($hostname) = @_;
13393: return exists($LC_dns_serv{$hostname});
13394: }
13395:
1.1 albertel 13396: }
13397:
1.847 albertel 13398: {
13399: my %iphost;
1.856 albertel 13400: my %name_to_ip;
13401: my %lonid_to_ip;
1.869 albertel 13402:
1.847 albertel 13403: sub get_hosts_from_ip {
13404: my ($ip) = @_;
13405: my %iphosts = &get_iphost();
13406: if (ref($iphosts{$ip})) {
13407: return @{$iphosts{$ip}};
13408: }
13409: return;
1.839 albertel 13410: }
1.864 albertel 13411:
13412: sub reset_hosts_ip_info {
13413: undef(%iphost);
13414: undef(%name_to_ip);
13415: undef(%lonid_to_ip);
13416: }
1.856 albertel 13417:
13418: sub get_host_ip {
13419: my ($lonid) = @_;
13420: if (exists($lonid_to_ip{$lonid})) {
13421: return $lonid_to_ip{$lonid};
13422: }
13423: my $name=&hostname($lonid);
13424: my $ip = gethostbyname($name);
13425: return if (!$ip || length($ip) ne 4);
13426: $ip=inet_ntoa($ip);
13427: $name_to_ip{$name} = $ip;
13428: $lonid_to_ip{$lonid} = $ip;
13429: return $ip;
13430: }
1.847 albertel 13431:
13432: sub get_iphost {
1.1293 raeburn 13433: my ($ignore_cache,$nocache) = @_;
1.894 albertel 13434:
1.869 albertel 13435: if (!$ignore_cache) {
13436: if (%iphost) {
13437: return %iphost;
13438: }
13439: my ($ip_info,$cached)=
13440: &Apache::lonnet::is_cached_new('iphost','iphost');
13441: if ($cached) {
13442: %iphost = %{$ip_info->[0]};
13443: %name_to_ip = %{$ip_info->[1]};
13444: %lonid_to_ip = %{$ip_info->[2]};
13445: return %iphost;
13446: }
13447: }
1.894 albertel 13448:
13449: # get yesterday's info for fallback
13450: my %old_name_to_ip;
13451: my ($ip_info,$cached)=
13452: &Apache::lonnet::is_cached_new('iphost','iphost');
13453: if ($cached) {
13454: %old_name_to_ip = %{$ip_info->[1]};
13455: }
13456:
1.1293 raeburn 13457: my %name_to_host = &all_names($ignore_cache,$nocache);
1.888 albertel 13458: foreach my $name (keys(%name_to_host)) {
1.847 albertel 13459: my $ip;
13460: if (!exists($name_to_ip{$name})) {
13461: $ip = gethostbyname($name);
13462: if (!$ip || length($ip) ne 4) {
1.894 albertel 13463: if (defined($old_name_to_ip{$name})) {
13464: $ip = $old_name_to_ip{$name};
13465: &logthis("Can't find $name defaulting to old $ip");
13466: } else {
13467: &logthis("Name $name no IP found");
13468: next;
13469: }
13470: } else {
13471: $ip=inet_ntoa($ip);
1.847 albertel 13472: }
13473: $name_to_ip{$name} = $ip;
13474: } else {
13475: $ip = $name_to_ip{$name};
1.653 albertel 13476: }
1.888 albertel 13477: foreach my $id (@{ $name_to_host{$name} }) {
13478: $lonid_to_ip{$id} = $ip;
13479: }
13480: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 13481: }
1.1293 raeburn 13482: unless ($nocache) {
13483: &do_cache_new('iphost','iphost',
13484: [\%iphost,\%name_to_ip,\%lonid_to_ip],
13485: 48*60*60);
13486: }
1.869 albertel 13487:
1.847 albertel 13488: return %iphost;
1.598 albertel 13489: }
13490:
1.992 raeburn 13491: #
13492: # Given a DNS returns the loncapa host name for that DNS
13493: #
13494: sub host_from_dns {
13495: my ($dns) = @_;
13496: my @hosts;
13497: my $ip;
13498:
1.993 raeburn 13499: if (exists($name_to_ip{$dns})) {
1.992 raeburn 13500: $ip = $name_to_ip{$dns};
13501: }
13502: if (!$ip) {
13503: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
13504: if (length($ip) == 4) {
13505: $ip = &IO::Socket::inet_ntoa($ip);
13506: }
13507: }
13508: if ($ip) {
13509: @hosts = get_hosts_from_ip($ip);
13510: return $hosts[0];
13511: }
13512: return undef;
1.986 foxr 13513: }
1.992 raeburn 13514:
1.1074 raeburn 13515: sub get_internet_names {
13516: my ($lonid) = @_;
13517: return if ($lonid eq '');
13518: my ($idnref,$cached)=
13519: &Apache::lonnet::is_cached_new('internetnames',$lonid);
13520: if ($cached) {
13521: return $idnref;
13522: }
13523: my $ip = &get_host_ip($lonid);
13524: my @hosts = &get_hosts_from_ip($ip);
13525: my %iphost = &get_iphost();
13526: my (@idns,%seen);
13527: foreach my $id (@hosts) {
13528: my $dom = &host_domain($id);
13529: my $prim_id = &domain($dom,'primary');
13530: my $prim_ip = &get_host_ip($prim_id);
13531: next if ($seen{$prim_ip});
13532: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
13533: foreach my $id (@{$iphost{$prim_ip}}) {
13534: my $intdom = &internet_dom($id);
13535: unless (grep(/^\Q$intdom\E$/,@idns)) {
13536: push(@idns,$intdom);
13537: }
13538: }
13539: }
13540: $seen{$prim_ip} = 1;
13541: }
1.1219 raeburn 13542: return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074 raeburn 13543: }
13544:
1.986 foxr 13545: }
13546:
1.1079 raeburn 13547: sub all_loncaparevs {
1.1249 raeburn 13548: 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 13549: }
13550:
1.1227 raeburn 13551: # ---------------------------------------------------------- Read loncaparev table
13552: {
13553: sub load_loncaparevs {
13554: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
13555: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
13556: while (my $configline=<$config>) {
13557: chomp($configline);
13558: my ($hostid,$loncaparev)=split(/:/,$configline);
13559: $loncaparevs{$hostid}=$loncaparev;
13560: }
13561: close($config);
13562: }
13563: }
13564: }
13565: }
13566:
13567: # ---------------------------------------------------------- Read serverhostID table
13568: {
13569: sub load_serverhomeIDs {
13570: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
13571: if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
13572: while (my $configline=<$config>) {
13573: chomp($configline);
13574: my ($name,$id)=split(/:/,$configline);
13575: $serverhomeIDs{$name}=$id;
13576: }
13577: close($config);
13578: }
13579: }
13580: }
13581: }
13582:
13583:
1.862 albertel 13584: BEGIN {
13585:
13586: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
13587: unless ($readit) {
13588: {
13589: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
13590: %perlvar = (%perlvar,%{$configvars});
13591: }
13592:
13593:
1.1 albertel 13594: # ------------------------------------------------------ Read spare server file
13595: {
1.448 albertel 13596: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 13597:
13598: while (my $configline=<$config>) {
13599: chomp($configline);
1.284 matthew 13600: if ($configline) {
1.784 albertel 13601: my ($host,$type) = split(':',$configline,2);
1.785 albertel 13602: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 13603: push(@{ $spareid{$type} }, $host);
1.1 albertel 13604: }
13605: }
1.448 albertel 13606: close($config);
1.1 albertel 13607: }
1.11 www 13608: # ------------------------------------------------------------ Read permissions
13609: {
1.448 albertel 13610: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 13611:
13612: while (my $configline=<$config>) {
1.448 albertel 13613: chomp($configline);
13614: if ($configline) {
13615: my ($role,$perm)=split(/ /,$configline);
13616: if ($perm ne '') { $pr{$role}=$perm; }
13617: }
1.11 www 13618: }
1.448 albertel 13619: close($config);
1.11 www 13620: }
13621:
13622: # -------------------------------------------- Read plain texts for permissions
13623: {
1.448 albertel 13624: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 13625:
13626: while (my $configline=<$config>) {
1.448 albertel 13627: chomp($configline);
13628: if ($configline) {
1.742 raeburn 13629: my ($short,@plain)=split(/:/,$configline);
13630: %{$prp{$short}} = ();
13631: if (@plain > 0) {
13632: $prp{$short}{'std'} = $plain[0];
13633: for (my $i=1; $i<@plain; $i++) {
13634: $prp{$short}{'alt'.$i} = $plain[$i];
13635: }
13636: }
1.448 albertel 13637: }
1.135 www 13638: }
1.448 albertel 13639: close($config);
1.135 www 13640: }
13641:
13642: # ---------------------------------------------------------- Read package table
13643: {
1.448 albertel 13644: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 13645:
13646: while (my $configline=<$config>) {
1.483 albertel 13647: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 13648: chomp($configline);
13649: my ($short,$plain)=split(/:/,$configline);
13650: my ($pack,$name)=split(/\&/,$short);
13651: if ($plain ne '') {
13652: $packagetab{$pack.'&'.$name.'&name'}=$name;
13653: $packagetab{$short}=$plain;
13654: }
1.11 www 13655: }
1.448 albertel 13656: close($config);
1.329 matthew 13657: }
13658:
1.1073 raeburn 13659: # ---------------------------------------------------------- Read loncaparev table
1.1227 raeburn 13660:
13661: &load_loncaparevs();
1.1073 raeburn 13662:
1.1074 raeburn 13663: # ---------------------------------------------------------- Read serverhostID table
13664:
1.1227 raeburn 13665: &load_serverhomeIDs();
13666:
13667: # ---------------------------------------------------------- Read releaseslist XML
1.1079 raeburn 13668: {
13669: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
13670: if (-e $file) {
13671: my $parser = HTML::LCParser->new($file);
13672: while (my $token = $parser->get_token()) {
13673: if ($token->[0] eq 'S') {
13674: my $item = $token->[1];
13675: my $name = $token->[2]{'name'};
13676: my $value = $token->[2]{'value'};
1.1285 raeburn 13677: my $valuematch = $token->[2]{'valuematch'};
1.1303 raeburn 13678: my $namematch = $token->[2]{'namematch'};
13679: if ($item eq 'parameter') {
13680: if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
13681: my $release = $parser->get_text();
13682: $release =~ s/(^\s*|\s*$ )//gx;
13683: $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
13684: }
13685: } elsif ($item ne '' && $name ne '') {
1.1079 raeburn 13686: my $release = $parser->get_text();
13687: $release =~ s/(^\s*|\s*$ )//gx;
1.1303 raeburn 13688: $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079 raeburn 13689: }
13690: }
13691: }
13692: }
1.1073 raeburn 13693: }
13694:
1.1138 raeburn 13695: # ---------------------------------------------------------- Read managers table
13696: {
13697: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
13698: if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
13699: while (my $configline=<$config>) {
13700: chomp($configline);
13701: next if ($configline =~ /^\#/);
13702: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
13703: $managerstab{$configline} = 1;
13704: }
13705: }
13706: close($config);
13707: }
13708: }
13709: }
13710:
1.329 matthew 13711: # ------------- set up temporary directory
13712: {
1.1117 foxr 13713: $tmpdir = LONCAPA::tempdir();
1.329 matthew 13714:
1.11 www 13715: }
13716:
1.794 albertel 13717: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
13718: 'compress_threshold'=> 20_000,
13719: });
1.185 www 13720:
1.281 www 13721: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 13722: $dumpcount=0;
1.958 www 13723: $locknum=0;
1.22 www 13724:
1.163 harris41 13725: &logtouch();
1.672 albertel 13726: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 13727: $readit=1;
1.564 albertel 13728: {
13729: use integer;
13730: my $test=(2**32)+1;
1.568 albertel 13731: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 13732: &logthis(" Detected 64bit platform ($_64bit)");
13733: }
1.195 www 13734: }
1.1 albertel 13735: }
1.179 www 13736:
1.1 albertel 13737: 1;
1.191 harris41 13738: __END__
13739:
1.243 albertel 13740: =pod
13741:
1.191 harris41 13742: =head1 NAME
13743:
1.243 albertel 13744: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 13745:
13746: =head1 SYNOPSIS
13747:
1.243 albertel 13748: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 13749:
13750: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
13751:
1.243 albertel 13752: Common parameters:
13753:
13754: =over 4
13755:
13756: =item *
13757:
13758: $uname : an internal username (if $cname expecting a course Id specifically)
13759:
13760: =item *
13761:
13762: $udom : a domain (if $cdom expecting a course's domain specifically)
13763:
13764: =item *
13765:
13766: $symb : a resource instance identifier
13767:
13768: =item *
13769:
13770: $namespace : the name of a .db file that contains the data needed or
13771: being set.
13772:
13773: =back
13774:
1.394 bowersj2 13775: =head1 OVERVIEW
1.191 harris41 13776:
1.394 bowersj2 13777: lonnet provides subroutines which interact with the
13778: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
13779: about classes, users, and resources.
1.243 albertel 13780:
13781: For many of these objects you can also use this to store data about
13782: them or modify them in various ways.
1.191 harris41 13783:
1.394 bowersj2 13784: =head2 Symbs
1.191 harris41 13785:
1.394 bowersj2 13786: To identify a specific instance of a resource, LON-CAPA uses symbols
13787: or "symbs"X<symb>. These identifiers are built from the URL of the
13788: map, the resource number of the resource in the map, and the URL of
13789: the resource itself. The latter is somewhat redundant, but might help
13790: if maps change.
13791:
13792: An example is
13793:
13794: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
13795:
13796: The respective map entry is
13797:
13798: <resource id="19" src="/res/msu/korte/tests/part12.problem"
13799: title="Problem 2">
13800: </resource>
13801:
13802: Symbs are used by the random number generator, as well as to store and
13803: restore data specific to a certain instance of for example a problem.
13804:
13805: =head2 Storing And Retrieving Data
13806:
13807: X<store()>X<cstore()>X<restore()>Three of the most important functions
13808: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
13809: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
13810: is is the non-critical message twin of cstore. These functions are for
13811: handlers to store a perl hash to a user's permanent data space in an
13812: easy manner, and to retrieve it again on another call. It is expected
13813: that a handler would use this once at the beginning to retrieve data,
13814: and then again once at the end to send only the new data back.
13815:
13816: The data is stored in the user's data directory on the user's
13817: homeserver under the ID of the course.
13818:
13819: The hash that is returned by restore will have all of the previous
13820: value for all of the elements of the hash.
13821:
13822: Example:
13823:
13824: #creating a hash
13825: my %hash;
13826: $hash{'foo'}='bar';
13827:
13828: #storing it
13829: &Apache::lonnet::cstore(\%hash);
13830:
13831: #changing a value
13832: $hash{'foo'}='notbar';
13833:
13834: #adding a new value
13835: $hash{'bar'}='foo';
13836: &Apache::lonnet::cstore(\%hash);
13837:
13838: #retrieving the hash
13839: my %history=&Apache::lonnet::restore();
13840:
13841: #print the hash
13842: foreach my $key (sort(keys(%history))) {
13843: print("\%history{$key} = $history{$key}");
13844: }
13845:
13846: Will print out:
1.191 harris41 13847:
1.394 bowersj2 13848: %history{1:foo} = bar
13849: %history{1:keys} = foo:timestamp
13850: %history{1:timestamp} = 990455579
13851: %history{2:bar} = foo
13852: %history{2:foo} = notbar
13853: %history{2:keys} = foo:bar:timestamp
13854: %history{2:timestamp} = 990455580
13855: %history{bar} = foo
13856: %history{foo} = notbar
13857: %history{timestamp} = 990455580
13858: %history{version} = 2
13859:
13860: Note that the special hash entries C<keys>, C<version> and
13861: C<timestamp> were added to the hash. C<version> will be equal to the
13862: total number of versions of the data that have been stored. The
13863: C<timestamp> attribute will be the UNIX time the hash was
13864: stored. C<keys> is available in every historical section to list which
13865: keys were added or changed at a specific historical revision of a
13866: hash.
13867:
13868: B<Warning>: do not store the hash that restore returns directly. This
13869: will cause a mess since it will restore the historical keys as if the
13870: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 13871:
1.394 bowersj2 13872: Calling convention:
1.191 harris41 13873:
1.1225 raeburn 13874: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269 raeburn 13875: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191 harris41 13876:
1.394 bowersj2 13877: For more detailed information, see lonnet specific documentation.
1.191 harris41 13878:
1.394 bowersj2 13879: =head1 RETURN MESSAGES
1.191 harris41 13880:
1.394 bowersj2 13881: =over 4
1.191 harris41 13882:
1.394 bowersj2 13883: =item * B<con_lost>: unable to contact remote host
1.191 harris41 13884:
1.394 bowersj2 13885: =item * B<con_delayed>: unable to contact remote host, message will be delivered
13886: when the connection is brought back up
1.191 harris41 13887:
1.394 bowersj2 13888: =item * B<con_failed>: unable to contact remote host and unable to save message
13889: for later delivery
1.191 harris41 13890:
1.967 bisitz 13891: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 13892:
1.394 bowersj2 13893: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 13894: that was requested
1.191 harris41 13895:
1.243 albertel 13896: =back
1.191 harris41 13897:
1.243 albertel 13898: =head1 PUBLIC SUBROUTINES
1.191 harris41 13899:
1.243 albertel 13900: =head2 Session Environment Functions
1.191 harris41 13901:
1.243 albertel 13902: =over 4
1.191 harris41 13903:
1.394 bowersj2 13904: =item *
13905: X<appenv()>
1.949 raeburn 13906: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 13907: the user envirnoment file, and will be restored for each access this
1.620 albertel 13908: user makes during this session, also modifies the %env for the current
1.949 raeburn 13909: process. Optional rolesarrayref - if defined contains a reference to an array
13910: of roles which are exempt from the restriction on modifying user.role entries
13911: in the user's environment.db and in %env.
1.191 harris41 13912:
13913: =item *
1.394 bowersj2 13914: X<delenv()>
1.987 raeburn 13915: B<delenv($delthis,$regexp)>: removes all items from the session
13916: environment file that begin with $delthis. If the
13917: optional second arg - $regexp - is true, $delthis is treated as a
13918: regular expression, otherwise \Q$delthis\E is used.
13919: The values are also deleted from the current processes %env.
1.191 harris41 13920:
1.795 albertel 13921: =item * get_env_multiple($name)
13922:
13923: gets $name from the %env hash, it seemlessly handles the cases where multiple
13924: values may be defined and end up as an array ref.
13925:
13926: returns an array of values
13927:
1.243 albertel 13928: =back
13929:
13930: =head2 User Information
1.191 harris41 13931:
1.243 albertel 13932: =over 4
1.191 harris41 13933:
13934: =item *
1.394 bowersj2 13935: X<queryauthenticate()>
13936: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 13937: authentication scheme
13938:
13939: =item *
1.394 bowersj2 13940: X<authenticate()>
1.1073 raeburn 13941: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 13942: authenticate user from domain's lib servers (first use the current
13943: one). C<$upass> should be the users password.
1.1073 raeburn 13944: $checkdefauth is optional (value is 1 if a check should be made to
13945: authenticate user using default authentication method, and allow
13946: account creation if username does not have account in the domain).
13947: $clientcancheckhost is optional (value is 1 if checking whether the
13948: server can host will occur on the client side in lonauth.pm).
1.191 harris41 13949:
13950: =item *
1.394 bowersj2 13951: X<homeserver()>
13952: B<homeserver($uname,$udom)>: find the server which has
13953: the user's directory and files (there must be only one), this caches
13954: the answer, and also caches if there is a borken connection.
1.191 harris41 13955:
13956: =item *
1.394 bowersj2 13957: X<idget()>
1.1300 raeburn 13958: B<idget($udom,$idsref,$namespace)>: find the usernames behind either
13959: a list of student/employee IDs or clicker IDs
13960: (student/employee IDs are a unique resource in a domain, there must be
13961: only 1 ID per username, and only 1 username per ID in a specific domain).
13962: clickerIDs are not necessarily unique, as students might share clickers.
13963: (returns hash: id=>name,id=>name)
1.191 harris41 13964:
13965: =item *
1.394 bowersj2 13966: X<idrget()>
13967: B<idrget($udom,@unames)>: find the IDs behind a list of
13968: usernames (returns hash: name=>id,name=>id)
1.191 harris41 13969:
13970: =item *
1.394 bowersj2 13971: X<idput()>
1.1300 raeburn 13972: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of
13973: names and associated student/employee IDs or clicker IDs.
13974:
13975: =item *
13976: X<iddel()>
13977: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted
13978: student/employee ID or clicker ID username look-ups from domain.
13979: The homeserver ($uhome) and namespace ($namespace) are optional.
13980: If no $uhome is provided, it will be determined usig &homeserver()
13981: for each user. If no $namespace is provided, the default is ids.
13982:
13983: =item *
13984: X<updateclickers()>
13985: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update
13986: clicker ID-to-username look-ups in clickers.db on library server.
13987: Permitted actions are add or del (i.e., add or delete). The
13988: clickers.db contains clickerID as keys (escaped), and each corresponding
13989: value is an escaped comma-separated list of usernames (for whom the
13990: library server is the homeserver), who registered that particular ID.
13991: If $critical is true, the update will be sent via &critical, otherwise
13992: &reply() will be used.
1.191 harris41 13993:
13994: =item *
1.394 bowersj2 13995: X<rolesinit()>
1.1169 droeschl 13996: B<rolesinit($udom,$username)>: get user privileges.
13997: returns user role, first access and timer interval hashes
1.243 albertel 13998:
13999: =item *
1.1171 droeschl 14000: X<privileged()>
14001: B<privileged($username,$domain)>: returns a true if user has a
14002: privileged and active role (i.e. su or dc), false otherwise.
14003:
14004: =item *
1.551 albertel 14005: X<getsection()>
14006: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 14007: course $cname, return section name/number or '' for "not in course"
14008: and '-1' for "no section"
14009:
14010: =item *
1.394 bowersj2 14011: X<userenvironment()>
14012: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 14013: passed in @what from the requested user's environment, returns a hash
14014:
1.858 raeburn 14015: =item *
14016: X<userlog_query()>
1.859 albertel 14017: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
14018: activity.log file. %filters defines filters applied when parsing the
14019: log file. These can be start or end timestamps, or the type of action
14020: - log to look for Login or Logout events, check for Checkin or
14021: Checkout, role for role selection. The response is in the form
14022: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
14023: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 14024:
1.243 albertel 14025: =back
14026:
14027: =head2 User Roles
14028:
14029: =over 4
14030:
14031: =item *
14032:
1.1284 raeburn 14033: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege;
14034: returns codes for allowed actions.
14035:
14036: The first argument is required, all others are optional.
14037:
14038: $priv is the privilege being checked.
14039: $uri contains additional information about what is being checked for access (e.g.,
14040: URL, course ID etc.).
14041: $symb is the unique resource instance identifier in a course; if needed,
14042: but not provided, it will be retrieved via a call to &symbread().
14043: $role is the role for which a priv is being checked (only used if priv is evb).
14044: $clientip is the user's IP address (only used when checking for access to portfolio
14045: files).
14046: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This
14047: prevents recursive calls to &allowed.
14048:
1.243 albertel 14049: F: full access
14050: U,I,K: authentication modes (cxx only)
14051: '': forbidden
14052: 1: user needs to choose course
14053: 2: browse allowed
1.766 albertel 14054: A: passphrase authentication needed
1.1284 raeburn 14055: B: access temporarily blocked because of a blocking event in a course.
1.243 albertel 14056:
14057: =item *
14058:
1.1192 raeburn 14059: constructaccess($url,$setpriv) : check for access to construction space URL
14060:
14061: See if the owner domain and name in the URL match those in the
14062: expected environment. If so, return three element list
14063: ($ownername,$ownerdomain,$ownerhome).
14064:
14065: Otherwise return the null string.
14066:
14067: If second argument 'setpriv' is true, it assigns the privileges,
14068: and returns the same three element list, unless the owner has
14069: blocked "ad hoc" Domain Coordinator access to the Author Space,
14070: in which case the null string is returned.
14071:
14072: =item *
14073:
1.1326 raeburn 14074: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
14075: define a custom role rolename set privileges in format of lonTabs/roles.tab
14076: for system, domain, and course level. $uname and $udom are optional (current
14077: user's username and domain will be used when either of $uname or $udom are absent.
1.243 albertel 14078:
14079: =item *
14080:
1.988 raeburn 14081: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
14082: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 14083: $type is Course (default) or Community.
1.988 raeburn 14084: If $forcedefault evaluates to true, text returned will be default
14085: text for $type. Otherwise, if this is a course, the text returned
14086: will be a custom name for the role (if defined in the course's
14087: environment). If no custom name is defined the default is returned.
14088:
1.832 raeburn 14089: =item *
14090:
1.1219 raeburn 14091: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858 raeburn 14092: All arguments are optional. Returns a hash of a roles, either for
14093: co-author/assistant author roles for a user's Construction Space
1.906 albertel 14094: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 14095: In the hash, keys are set to colon-separated $uname,$udom,$role, and
14096: (optionally) if $withsec is true, a fourth colon-separated item - $section.
14097: For each key, value is set to colon-separated start and end times for
14098: the role. If no username and domain are specified, will default to
1.934 raeburn 14099: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 14100: of role statuses (active, future or previous), roles
14101: (e.g., cc,in, st etc.) and domains of the roles which can be used
14102: to restrict the list of roles reported. If no array ref is
14103: provided for types, will default to return only active roles.
1.834 albertel 14104:
1.1195 raeburn 14105: =item *
14106:
14107: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196 raeburn 14108: user: $uname:$udom has a role in the course: $cdom_$cnum.
14109:
14110: Additional optional arguments are: $type (if role checking is to be restricted
14111: to certain user status types -- previous (expired roles), active (currently
1.1195 raeburn 14112: available roles) or future (roles available in the future), and
14113: $hideprivileged -- if true will not report course roles for users who
1.1219 raeburn 14114: have active Domain Coordinator role in course's domain or in additional
14115: domains (specified in 'Domains to check for privileged users' in course
14116: environment -- set via: Course Settings -> Classlists and staff listing).
14117:
14118: =item *
14119:
14120: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
14121: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
14122: $possdomains and $possroles are optional array refs -- to domains to check and
14123: roles to check. If $possdomains is not specified, a dump will be done of the
14124: users' roles.db to check for a dc or su role in any domain. This can be
14125: time consuming if &privileged is called repeatedly (e.g., when displaying a
14126: classlist), so in such cases, supplying a $possdomains array is preferred, as
14127: this then allows &privileged_by_domain() to be used, which caches the identity
14128: of privileged users, eliminating the need for repeated calls to &dump().
14129:
14130: =item *
14131:
14132: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
14133: where the outer hash keys are domains specified in the $possdomains array ref,
14134: next inner hash keys are privileged roles specified in the $roles array ref,
14135: and the innermost hash contains key = value pairs for username:domain = end:start
14136: for active or future "privileged" users with that role in that domain. To avoid
14137: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
14138: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195 raeburn 14139:
1.243 albertel 14140: =back
14141:
14142: =head2 User Modification
14143:
14144: =over 4
14145:
14146: =item *
14147:
1.957 raeburn 14148: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 14149: user for the level given by URL. Optional start and end dates (leave empty
14150: string or zero for "no date")
1.191 harris41 14151:
14152: =item *
14153:
1.243 albertel 14154: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
14155: change a users, password, possible return values are: ok,
14156: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
14157: refused
1.191 harris41 14158:
14159: =item *
14160:
1.243 albertel 14161: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 14162:
14163: =item *
14164:
1.1058 raeburn 14165: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
14166: $forceid,$desiredhome,$email,$inststatus,$candelete) :
14167:
14168: will update user information (firstname,middlename,lastname,generation,
14169: permanentemail), and if forceid is true, student/employee ID also.
14170: A user's institutional affiliation(s) can also be updated.
14171: User information fields will not be overwritten with empty entries
14172: unless the field is included in the $candelete array reference.
14173: This array is included when a single user is modified via "Manage Users",
14174: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 14175:
14176: =item *
14177:
1.286 matthew 14178: modifystudent
14179:
1.957 raeburn 14180: modify a student's enrollment and identification information.
1.1235 raeburn 14181: The course id is resolved based on the current user's environment.
14182: This means the invoking user must be a course coordinator or otherwise
1.286 matthew 14183: associated with a course.
14184:
1.297 matthew 14185: This call is essentially a wrapper for lonnet::modifyuser and
14186: lonnet::modify_student_enrollment
1.286 matthew 14187:
14188: Inputs:
14189:
14190: =over 4
14191:
1.957 raeburn 14192: =item B<$udom> Student's loncapa domain
1.286 matthew 14193:
1.957 raeburn 14194: =item B<$uname> Student's loncapa login name
1.286 matthew 14195:
1.964 bisitz 14196: =item B<$uid> Student/Employee ID
1.286 matthew 14197:
1.957 raeburn 14198: =item B<$umode> Student's authentication mode
1.286 matthew 14199:
1.957 raeburn 14200: =item B<$upass> Student's password
1.286 matthew 14201:
1.957 raeburn 14202: =item B<$first> Student's first name
1.286 matthew 14203:
1.957 raeburn 14204: =item B<$middle> Student's middle name
1.286 matthew 14205:
1.957 raeburn 14206: =item B<$last> Student's last name
1.286 matthew 14207:
1.957 raeburn 14208: =item B<$gene> Student's generation
1.286 matthew 14209:
1.957 raeburn 14210: =item B<$usec> Student's section in course
1.286 matthew 14211:
14212: =item B<$end> Unix time of the roles expiration
14213:
14214: =item B<$start> Unix time of the roles start date
14215:
14216: =item B<$forceid> If defined, allow $uid to be changed
14217:
14218: =item B<$desiredhome> server to use as home server for student
14219:
1.957 raeburn 14220: =item B<$email> Student's permanent e-mail address
14221:
14222: =item B<$type> Type of enrollment (auto or manual)
14223:
1.963 raeburn 14224: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
14225:
14226: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 14227:
1.963 raeburn 14228: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 14229:
1.963 raeburn 14230: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 14231:
1.1216 raeburn 14232: =item B<$inststatus> institutional status of user - : separated string of escaped status types
14233:
14234: =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 14235:
1.286 matthew 14236: =back
1.297 matthew 14237:
14238: =item *
14239:
14240: modify_student_enrollment
14241:
1.1235 raeburn 14242: Change a student's enrollment status in a class. The environment variable
1.297 matthew 14243: 'role.request.course' must be defined for this function to proceed.
14244:
14245: Inputs:
14246:
14247: =over 4
14248:
1.1235 raeburn 14249: =item $udom, student's domain
1.297 matthew 14250:
1.1235 raeburn 14251: =item $uname, student's name
1.297 matthew 14252:
1.1235 raeburn 14253: =item $uid, student's user id
1.297 matthew 14254:
1.1235 raeburn 14255: =item $first, student's first name
1.297 matthew 14256:
14257: =item $middle
14258:
14259: =item $last
14260:
14261: =item $gene
14262:
14263: =item $usec
14264:
14265: =item $end
14266:
14267: =item $start
14268:
1.957 raeburn 14269: =item $type
14270:
14271: =item $locktype
14272:
14273: =item $cid
14274:
14275: =item $selfenroll
14276:
14277: =item $context
14278:
1.1216 raeburn 14279: =item $credits, number of credits student will earn from this class
14280:
1.1314 raeburn 14281: =item $instsec, institutional course section code for student
14282:
1.297 matthew 14283: =back
14284:
1.191 harris41 14285:
14286: =item *
14287:
1.243 albertel 14288: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
14289: custom role; give a custom role to a user for the level given by URL. Specify
14290: name and domain of role author, and role name
1.191 harris41 14291:
14292: =item *
14293:
1.243 albertel 14294: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 14295:
14296: =item *
14297:
1.243 albertel 14298: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
14299:
14300: =back
14301:
14302: =head2 Course Infomation
14303:
14304: =over 4
1.191 harris41 14305:
14306: =item *
14307:
1.1118 foxr 14308: coursedescription($courseid,$options) : returns a hash of information about the
1.631 albertel 14309: specified course id, including all environment settings for the
14310: course, the description of the course will be in the hash under the
14311: key 'description'
1.191 harris41 14312:
1.1118 foxr 14313: $options is an optional parameter that if supplied is a hash reference that controls
14314: what how this function works. It has the following key/values:
14315:
14316: =over 4
14317:
14318: =item freshen_cache
14319:
14320: If defined, and the environment cache for the course is valid, it is
14321: returned in the returned hash.
14322:
14323: =item one_time
14324:
14325: If defined, the last cache time is set to _now_
14326:
14327: =item user
14328:
14329: If defined, the supplied username is used instead of the current user.
14330:
14331:
14332: =back
14333:
1.191 harris41 14334: =item *
14335:
1.624 albertel 14336: resdata($name,$domain,$type,@which) : request for current parameter
14337: setting for a specific $type, where $type is either 'course' or 'user',
14338: @what should be a list of parameters to ask about. This routine caches
1.1235 raeburn 14339: answers for 10 minutes.
1.243 albertel 14340:
1.877 foxr 14341: =item *
14342:
14343: get_courseresdata($courseid, $domain) : dump the entire course resource
14344: data base, returning a hash that is keyed by the resource name and has
14345: values that are the resource value. I believe that the timestamps and
14346: versions are also returned.
14347:
1.1236 raeburn 14348: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
14349: supplemental content area. This routine caches the number of files for
14350: 10 minutes.
14351:
1.243 albertel 14352: =back
14353:
14354: =head2 Course Modification
14355:
14356: =over 4
1.191 harris41 14357:
14358: =item *
14359:
1.243 albertel 14360: writecoursepref($courseid,%prefs) : write preferences (environment
14361: database) for a course
1.191 harris41 14362:
14363: =item *
14364:
1.1011 raeburn 14365: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
14366:
14367: =item *
14368:
1.1038 raeburn 14369: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 14370:
1.1167 droeschl 14371: =item *
14372:
14373: is_course($courseid), is_course($cdom, $cnum)
14374:
14375: Accepts either a combined $courseid (in the form of domain_courseid) or the
14376: two component version $cdom, $cnum. It checks if the specified course exists.
14377:
14378: Returns:
14379: undef if the course doesn't exist, otherwise
14380: in scalar context the combined courseid.
14381: in list context the two components of the course identifier, domain and
14382: courseid.
14383:
1.243 albertel 14384: =back
14385:
14386: =head2 Resource Subroutines
14387:
14388: =over 4
1.191 harris41 14389:
14390: =item *
14391:
1.243 albertel 14392: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 14393:
14394: =item *
14395:
1.243 albertel 14396: repcopy($filename) : subscribes to the requested file, and attempts to
14397: replicate from the owning library server, Might return
1.607 raeburn 14398: 'unavailable', 'not_found', 'forbidden', 'ok', or
14399: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 14400: resource. Expects the local filesystem pathname
14401: (/home/httpd/html/res/....)
14402:
14403: =back
14404:
14405: =head2 Resource Information
14406:
14407: =over 4
1.191 harris41 14408:
14409: =item *
14410:
1.1228 raeburn 14411: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates
14412: and returns the value of a variety of different possible values,
14413: $varname should be a request string, and the other parameters can be
14414: used to specify who and what one is asking about. Ordinarily, $cid
14415: does not need to be specified, as it is retrived from
14416: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
14417: within lonuserstate::loadmap() when initializing a course, before
14418: $env{'request.course.id'} has been set, so it needs to be provided
14419: in that one case.
1.243 albertel 14420:
14421: Possible values for $varname are environment.lastname (or other item
14422: from the envirnment hash), user.name (or someother aspect about the
14423: user), resource.0.maxtries (or some other part and parameter of a
14424: resource)
1.204 albertel 14425:
14426: =item *
14427:
1.243 albertel 14428: directcondval($number) : get current value of a condition; reads from a state
14429: string
1.204 albertel 14430:
14431: =item *
14432:
1.243 albertel 14433: condval($condidx) : value of condition index based on state
1.204 albertel 14434:
14435: =item *
14436:
1.243 albertel 14437: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
14438: resource's metadata, $what should be either a specific key, or either
14439: 'keys' (to get a list of possible keys) or 'packages' to get a list of
14440: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
14441:
14442: this function automatically caches all requests
1.191 harris41 14443:
14444: =item *
14445:
1.243 albertel 14446: metadata_query($query,$custom,$customshow) : make a metadata query against the
14447: network of library servers; returns file handle of where SQL and regex results
14448: will be stored for query
1.191 harris41 14449:
14450: =item *
14451:
1.1282 raeburn 14452: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) :
14453: return symbolic list entry (all arguments optional).
14454:
14455: Args: filename is the filename (including path) for the file for which a symb
14456: is required; donotrecurse, if true will prevent calls to allowed() being made
14457: to check access status if more than one resource was found in the bighash
14458: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of
14459: a randompick); ignorecachednull, if true will prevent a symb of '' being
14460: returned if $env{$cache_str} is defined as ''; checkforblock if true will
14461: cause possible symbs to be checked to determine if they are subject to content
14462: blocking, if so they will not be included as possible symbs; possibles is a
14463: ref to a hash, which, as a side effect, will be populated with all possible
14464: symbs (content blocking not tested).
14465:
1.243 albertel 14466: returns the data handle
1.191 harris41 14467:
14468: =item *
14469:
1.1190 raeburn 14470: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
14471: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582 albertel 14472: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190 raeburn 14473: on failure, user must be in a course, as it assumes the existence of
14474: the course initial hash, and uses $env('request.course.id'}. The third
14475: arg is an optional reference to a scalar. If this arg is passed in the
14476: call to symbverify, it will be set to 1 if the symb has been set to be
14477: encrypted; otherwise it will be null.
1.191 harris41 14478:
14479: =item *
14480:
1.243 albertel 14481: symbclean($symb) : removes versions numbers from a symb, returns the
14482: cleaned symb
1.191 harris41 14483:
14484: =item *
14485:
1.243 albertel 14486: is_on_map($uri) : checks if the $uri is somewhere on the current
14487: course map, user must be in a course for it to work.
1.191 harris41 14488:
14489: =item *
14490:
1.243 albertel 14491: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 14492:
14493: =item *
14494:
1.243 albertel 14495: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
14496: a random seed, all arguments are optional, if they aren't sent it uses the
14497: environment to derive them. Note: if symb isn't sent and it can't get one
14498: from &symbread it will use the current time as its return value
1.191 harris41 14499:
14500: =item *
14501:
1.243 albertel 14502: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
14503: unfakeable, receipt
1.191 harris41 14504:
14505: =item *
14506:
1.620 albertel 14507: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 14508:
14509: =item *
14510:
1.243 albertel 14511: countacc($url) : count the number of accesses to a given URL
1.191 harris41 14512:
14513: =item *
14514:
1.243 albertel 14515: 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 14516:
14517: =item *
14518:
1.243 albertel 14519: 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 14520:
14521: =item *
14522:
1.243 albertel 14523: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 14524:
14525: =item *
14526:
1.243 albertel 14527: devalidate($symb) : devalidate temporary spreadsheet calculations,
14528: forcing spreadsheet to reevaluate the resource scores next time.
14529:
1.1195 raeburn 14530: =item *
14531:
1.1196 raeburn 14532: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
14533: when viewing in course context.
1.1195 raeburn 14534:
1.1196 raeburn 14535: input: six args -- filename (decluttered), course number, course domain,
14536: url, symb (if registered) and group (if this is a
14537: group item -- e.g., bulletin board, group page etc.).
1.1195 raeburn 14538:
1.1196 raeburn 14539: output: array of five scalars --
1.1195 raeburn 14540: $cfile -- url for file editing if editable on current server
14541: $home -- homeserver of resource (i.e., for author if published,
14542: or course if uploaded.).
14543: $switchserver -- 1 if server switch will be needed.
1.1196 raeburn 14544: $forceedit -- 1 if icon/link should be to go to edit mode
14545: $forceview -- 1 if icon/link should be to go to view mode
1.1195 raeburn 14546:
14547: =item *
14548:
14549: is_course_upload($file,$cnum,$cdom)
14550:
14551: Used in course context to determine if current file was uploaded to
14552: the course (i.e., would be found in /userfiles/docs on the course's
14553: homeserver.
14554:
14555: input: 3 args -- filename (decluttered), course number and course domain.
14556: output: boolean -- 1 if file was uploaded.
14557:
1.243 albertel 14558: =back
14559:
14560: =head2 Storing/Retreiving Data
14561:
14562: =over 4
1.191 harris41 14563:
14564: =item *
14565:
1.1269 raeburn 14566: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
14567: permanently for this url; hashref needs to be given and should be a \%hashname;
14568: the remaining args aren't required and if they aren't passed or are '' they will
14569: be derived from the env (with the exception of $laststore, which is an
14570: optional arg used when a user's submission is stored in grading).
14571: $laststore is $version=$timestamp, where $version is the most recent version
14572: number retrieved for the corresponding $symb in the $namespace db file, and
14573: $timestamp is the timestamp for that transaction (UNIX time).
14574: $laststore is currently only passed when cstore() is called by
14575: structuretags::finalize_storage().
1.191 harris41 14576:
14577: =item *
14578:
1.1269 raeburn 14579: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
14580: but uses critical subroutine
1.191 harris41 14581:
14582: =item *
14583:
1.243 albertel 14584: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
14585: all args are optional
1.191 harris41 14586:
14587: =item *
14588:
1.717 albertel 14589: dumpstore($namespace,$udom,$uname,$regexp,$range) :
14590: dumps the complete (or key matching regexp) namespace into a hash
14591: ($udom, $uname, $regexp, $range are optional) for a namespace that is
14592: normally &store()ed into
14593:
14594: $range should be either an integer '100' (give me the first 100
14595: matching records)
14596: or be two integers sperated by a - with no spaces
14597: '30-50' (give me the 30th through the 50th matching
14598: records)
14599:
14600:
14601: =item *
14602:
1.1269 raeburn 14603: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717 albertel 14604: replaces a &store() version of data with a replacement set of data
14605: for a particular resource in a namespace passed in the $storehash hash
1.1269 raeburn 14606: reference. If $tolog is true, the transaction is logged in the courselog
14607: with an action=PUTSTORE.
1.717 albertel 14608:
14609: =item *
14610:
1.243 albertel 14611: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
14612: works very similar to store/cstore, but all data is stored in a
14613: temporary location and can be reset using tmpreset, $storehash should
14614: be a hash reference, returns nothing on success
1.191 harris41 14615:
14616: =item *
14617:
1.243 albertel 14618: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
14619: similar to restore, but all data is stored in a temporary location and
14620: can be reset using tmpreset. Returns a hash of values on success,
14621: error string otherwise.
1.191 harris41 14622:
14623: =item *
14624:
1.243 albertel 14625: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
14626: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 14627:
14628: =item *
14629:
1.243 albertel 14630: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14631: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 14632:
14633: =item *
14634:
1.243 albertel 14635: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
14636: namesp ($udom and $uname are optional)
1.191 harris41 14637:
14638: =item *
14639:
1.702 albertel 14640: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 14641: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 14642: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 14643:
1.702 albertel 14644: $range should be either an integer '100' (give me the first 100
14645: matching records)
14646: or be two integers sperated by a - with no spaces
14647: '30-50' (give me the 30th through the 50th matching
14648: records)
1.449 matthew 14649: =item *
14650:
14651: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
14652: $store can be a scalar, an array reference, or if the amount to be
14653: incremented is > 1, a hash reference.
14654:
14655: ($udom and $uname are optional)
1.191 harris41 14656:
14657: =item *
14658:
1.243 albertel 14659: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
14660: ($udom and $uname are optional)
1.191 harris41 14661:
14662: =item *
14663:
1.243 albertel 14664: cput($namespace,$storehash,$udom,$uname) : critical put
14665: ($udom and $uname are optional)
1.191 harris41 14666:
14667: =item *
14668:
1.748 albertel 14669: newput($namespace,$storehash,$udom,$uname) :
14670:
14671: Attempts to store the items in the $storehash, but only if they don't
14672: currently exist, if this succeeds you can be certain that you have
14673: successfully created a new key value pair in the $namespace db.
14674:
14675:
14676: Args:
14677: $namespace: name of database to store values to
14678: $storehash: hashref to store to the db
14679: $udom: (optional) domain of user containing the db
14680: $uname: (optional) name of user caontaining the db
14681:
14682: Returns:
14683: 'ok' -> succeeded in storing all keys of $storehash
14684: 'key_exists: <key>' -> failed to anything out of $storehash, as at
14685: least <key> already existed in the db (other
14686: requested keys may also already exist)
1.967 bisitz 14687: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 14688: 'con_lost' -> unable to contact request server
14689: 'refused' -> action was not allowed by remote machine
14690:
14691:
14692: =item *
14693:
1.243 albertel 14694: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
14695: reference filled in from namesp (encrypts the return communication)
14696: ($udom and $uname are optional)
1.191 harris41 14697:
14698: =item *
14699:
1.243 albertel 14700: log($udom,$name,$home,$message) : write to permanent log for user; use
14701: critical subroutine
14702:
1.806 raeburn 14703: =item *
14704:
1.860 raeburn 14705: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
14706: array reference filled in from namespace found in domain level on either
14707: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 14708:
14709: =item *
14710:
1.860 raeburn 14711: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
14712: domain level either on specified domain server ($uhome) or primary domain
14713: server ($udom and $uhome are optional)
1.806 raeburn 14714:
1.943 raeburn 14715: =item *
14716:
1.1240 raeburn 14717: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults
14718: for: authentication, language, quotas, timezone, date locale, and portal URL in
14719: the target domain.
14720:
14721: May also include additional key => value pairs for the following groups:
14722:
14723: =over
14724:
14725: =item
14726: disk quotas (MB allocated by default to portfolios and authoring spaces).
14727:
14728: =over
14729:
14730: =item defaultquota, authorquota
14731:
14732: =back
14733:
14734: =item
14735: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
14736: portfolio for users).
14737:
14738: =over
14739:
14740: =item
14741: aboutme, blog, webdav, portfolio
14742:
14743: =back
14744:
14745: =item
14746: requestcourses: ability to request courses, and how requests are processed.
14747:
14748: =over
14749:
14750: =item
1.1305 raeburn 14751: official, unofficial, community, textbook, placement
1.1240 raeburn 14752:
14753: =back
14754:
14755: =item
14756: inststatus: types of institutional affiliation, and order in which they are displayed.
14757:
14758: =over
14759:
14760: =item
1.1256 raeburn 14761: inststatustypes, inststatusorder, inststatusguest
1.1240 raeburn 14762:
14763: =back
14764:
14765: =item
14766: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
14767: for course's uploaded content.
14768:
14769: =over
14770:
14771: =item
1.1246 raeburn 14772: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota,
1.1305 raeburn 14773: communityquota, textbookquota, placementquota
1.1240 raeburn 14774:
14775: =back
14776:
14777: =item
14778: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
14779: on your servers.
14780:
14781: =over
14782:
14783: =item
14784: remotesessions, hostedsessions
14785:
14786: =back
14787:
14788: =back
14789:
14790: In cases where a domain coordinator has never used the "Set Domain Configuration"
14791: utility to create a configuration.db file on a domain's primary library server
14792: only the following domain defaults: auth_def, auth_arg_def, lang_def
14793: -- corresponding values are authentication type (internal, krb4, krb5,
14794: or localauth), initial password or a kerberos realm, language (e.g., en-us) --
14795: will be available. Values are retrieved from cache (if current), unless the
14796: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
14797: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
14798:
14799: Typical usage:
1.943 raeburn 14800:
1.1240 raeburn 14801: %domdefaults = &get_domain_defaults($target_domain);
1.943 raeburn 14802:
1.243 albertel 14803: =back
14804:
14805: =head2 Network Status Functions
14806:
14807: =over 4
1.191 harris41 14808:
14809: =item *
14810:
1.1137 raeburn 14811: dirlist() : return directory list based on URI (first arg).
14812:
14813: Inputs: 1 required, 5 optional.
14814:
14815: =over
14816:
14817: =item
14818: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
14819:
14820: =item
14821: $userdomain - domain of user/course to be listed. Extracted from $uri if absent.
14822:
14823: =item
14824: $username - username of user/course to be listed. Extracted from $uri if absent.
14825:
14826: =item
14827: $getpropath - boolean: 1 if prepend path using &propath().
14828:
14829: =item
14830: $getuserdir - boolean: 1 if prepend path for "userfiles".
14831:
14832: =item
14833: $alternateRoot - path to prepend in place of path from $uri.
14834:
14835: =back
14836:
14837: Returns: Array of up to two items.
14838:
14839: =over
14840:
14841: a reference to an array of files/subdirectories
14842:
14843: =over
14844:
14845: Each element in the array of files/subdirectories is a & separated list of
14846: item name and the result of running stat on the item. If dirlist was requested
14847: for a file instead of a directory, the item name will be ''. For a directory
14848: listing, if the item is a metadata file, the element will end &N&M
14849: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
14850: default copyright set (1).
14851:
14852: =back
14853:
14854: a scalar containing error condition (if encountered).
14855:
14856: =over
14857:
14858: =item
14859: no_host (no homeserver identified for $username:$domain).
14860:
14861: =item
14862: no_such_host (server contacted for listing not identified as valid host).
14863:
14864: =item
14865: con_lost (connection to remote server failed).
14866:
14867: =item
14868: refused (invalid $username:$domain received on lond side).
14869:
14870: =item
14871: no_such_dir (directory at specified path on lond side does not exist).
14872:
14873: =item
14874: empty (directory at specified path on lond side is empty).
14875:
14876: =over
14877:
14878: This is currently not encountered because the &ls3, &ls2,
14879: &ls (_handler) routines on the lond side do not filter out
14880: . and .. from a directory listing.
14881:
14882: =back
14883:
14884: =back
14885:
14886: =back
1.191 harris41 14887:
14888: =item *
14889:
1.243 albertel 14890: spareserver() : find server with least workload from spare.tab
14891:
1.986 foxr 14892:
14893: =item *
14894:
14895: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
14896: if there is no corresponding loncapa host.
14897:
1.243 albertel 14898: =back
14899:
1.986 foxr 14900:
1.243 albertel 14901: =head2 Apache Request
14902:
14903: =over 4
1.191 harris41 14904:
14905: =item *
14906:
1.243 albertel 14907: ssi($url,%hash) : server side include, does a complete request cycle on url to
14908: localhost, posts hash
14909:
14910: =back
14911:
14912: =head2 Data to String to Data
14913:
14914: =over 4
1.191 harris41 14915:
14916: =item *
14917:
1.243 albertel 14918: hash2str(%hash) : convert a hash into a string complete with escaping and '='
14919: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 14920:
14921: =item *
14922:
1.243 albertel 14923: hashref2str($hashref) : convert a hashref into a string complete with
14924: escaping and '=' and '&' separators, supports elements that are
14925: arrayrefs and hashrefs
1.191 harris41 14926:
14927: =item *
14928:
1.243 albertel 14929: arrayref2str($arrayref) : convert an arrayref into a string complete
14930: with escaping and '&' separators, supports elements that are arrayrefs
14931: and hashrefs
1.191 harris41 14932:
14933: =item *
14934:
1.243 albertel 14935: str2hash($string) : convert string to hash using unescaping and
14936: splitting on '=' and '&', supports elements that are arrayrefs and
14937: hashrefs
1.191 harris41 14938:
14939: =item *
14940:
1.243 albertel 14941: str2array($string) : convert string to hash using unescaping and
14942: splitting on '&', supports elements that are arrayrefs and hashrefs
14943:
14944: =back
14945:
14946: =head2 Logging Routines
14947:
14948:
14949: These routines allow one to make log messages in the lonnet.log and
14950: lonnet.perm logfiles.
1.191 harris41 14951:
1.1119 foxr 14952: =over 4
14953:
1.191 harris41 14954: =item *
14955:
1.243 albertel 14956: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 14957:
14958: =item *
14959:
1.243 albertel 14960: logthis() : append message to the normal lonnet.log file, it gets
14961: preiodically rolled over and deleted.
1.191 harris41 14962:
14963: =item *
14964:
1.243 albertel 14965: logperm() : append a permanent message to lonnet.perm.log, this log
14966: file never gets deleted by any automated portion of the system, only
14967: messages of critical importance should go in here.
14968:
1.1119 foxr 14969:
1.243 albertel 14970: =back
14971:
14972: =head2 General File Helper Routines
14973:
14974: =over 4
1.191 harris41 14975:
14976: =item *
14977:
1.481 raeburn 14978: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
14979: (a) files in /uploaded
14980: (i) If a local copy of the file exists -
14981: compares modification date of local copy with last-modified date for
14982: definitive version stored on home server for course. If local copy is
14983: stale, requests a new version from the home server and stores it.
14984: If the original has been removed from the home server, then local copy
14985: is unlinked.
14986: (ii) If local copy does not exist -
14987: requests the file from the home server and stores it.
14988:
14989: If $caller is 'uploadrep':
14990: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
14991: for request for files originally uploaded via DOCS.
14992: - returns 'ok' if fresh local copy now available, -1 otherwise.
14993:
14994: Otherwise:
14995: This indicates a call from the content generation phase of the request.
14996: - returns the entire contents of the file or -1.
14997:
14998: (b) files in /res
14999: - returns the entire contents of a file or -1;
15000: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 15001:
1.712 albertel 15002:
15003: =item *
15004:
15005: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
15006: reference
15007:
15008: returns either a stat() list of data about the file or an empty list
15009: if the file doesn't exist or couldn't find out about it (connection
15010: problems or user unknown)
15011:
1.191 harris41 15012: =item *
15013:
1.243 albertel 15014: filelocation($dir,$file) : returns file system location of a file
15015: based on URI; meant to be "fairly clean" absolute reference, $dir is a
15016: directory that relative $file lookups are to looked in ($dir of /a/dir
15017: and a file of ../bob will become /a/bob)
1.191 harris41 15018:
15019: =item *
15020:
15021: hreflocation($dir,$file) : returns file system location or a URL; same as
15022: filelocation except for hrefs
15023:
15024: =item *
15025:
1.1261 raeburn 15026: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
15027: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191 harris41 15028:
1.243 albertel 15029: =back
15030:
1.608 albertel 15031: =head2 Usererfile file routines (/uploaded*)
15032:
15033: =over 4
15034:
15035: =item *
15036:
15037: userfileupload(): main rotine for putting a file in a user or course's
15038: filespace, arguments are,
15039:
1.620 albertel 15040: formname - required - this is the name of the element in $env where the
1.608 albertel 15041: filename, and the contents of the file to create/modifed exist
1.620 albertel 15042: the filename is in $env{'form.'.$formname.'.filename'} and the
15043: contents of the file is located in $env{'form.'.$formname}
1.1090 raeburn 15044: context - if coursedoc, store the file in the course of the active role
15045: of the current user;
15046: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
15047: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 15048: subdir - required - subdirectory to put the file in under ../userfiles/
15049: if undefined, it will be placed in "unknown"
15050:
15051: (This routine calls clean_filename() to remove any dangerous
15052: characters from the filename, and then calls finuserfileupload() to
15053: complete the transaction)
15054:
15055: returns either the url of the uploaded file (/uploaded/....) if successful
15056: and /adm/notfound.html if unsuccessful
15057:
15058: =item *
15059:
15060: clean_filename(): routine for cleaing a filename up for storage in
15061: userfile space, argument is:
15062:
15063: filename - proposed filename
15064:
15065: returns: the new clean filename
15066:
15067: =item *
15068:
1.1090 raeburn 15069: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 15070: userspace, probably shouldn't be called directly
15071:
15072: docuname: username or courseid of destination for the file
15073: docudom: domain of user/course of destination for the file
15074: formname: same as for userfileupload()
1.1090 raeburn 15075: fname: filename (including subdirectories) for the file
15076: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
15077: allfiles: reference to hash used to store objects found by parser
15078: codebase: reference to hash used for codebases of java objects found by parser
15079: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
15080: thumbheight: height (pixels) of thumbnail to be created for uploaded image
15081: resizewidth: width to be used to resize image using resizeImage from ImageMagick
15082: resizeheight: height to be used to resize image using resizeImage from ImageMagick
15083: context: if 'overwrite', will move the uploaded file from its temporary location to
15084: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095 raeburn 15085: mimetype: reference to scalar to accommodate mime type determined
15086: from File::MMagic if $parser = parse.
1.608 albertel 15087:
15088: returns either the url of the uploaded file (/uploaded/....) if successful
1.1090 raeburn 15089: and /adm/notfound.html if unsuccessful (or an error message if context
15090: was 'overwrite').
15091:
1.608 albertel 15092:
15093: =item *
15094:
15095: renameuserfile(): renames an existing userfile to a new name
15096:
15097: Args:
15098: docuname: username or courseid of destination for the file
15099: docudom: domain of user/course of destination for the file
15100: old: current file name (including any subdirs under userfiles)
15101: new: desired file name (including any subdirs under userfiles)
15102:
15103: =item *
15104:
15105: mkdiruserfile(): creates a directory is a userfiles dir
15106:
15107: Args:
15108: docuname: username or courseid of destination for the file
15109: docudom: domain of user/course of destination for the file
15110: dir: dir to create (including any subdirs under userfiles)
15111:
15112: =item *
15113:
15114: removeuserfile(): removes a file that exists in userfiles
15115:
15116: Args:
15117: docuname: username or courseid of destination for the file
15118: docudom: domain of user/course of destination for the file
15119: fname: filname to delete (including any subdirs under userfiles)
15120:
15121: =item *
15122:
15123: removeuploadedurl(): convience function for removeuserfile()
15124:
15125: Args:
15126: url: a full /uploaded/... url to delete
15127:
1.747 albertel 15128: =item *
15129:
15130: get_portfile_permissions():
15131: Args:
15132: domain: domain of user or course contain the portfolio files
15133: user: name of user or num of course contain the portfolio files
15134: Returns:
15135: hashref of a dump of the proper file_permissions.db
15136:
15137:
15138: =item *
15139:
15140: get_access_controls():
15141:
15142: Args:
15143: current_permissions: the hash ref returned from get_portfile_permissions()
15144: group: (optional) the group you want the files associated with
15145: file: (optional) the file you want access info on
15146:
15147: Returns:
1.749 raeburn 15148: a hash (keys are file names) of hashes containing
15149: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
15150: values are XML containing access control settings (see below)
1.747 albertel 15151:
15152: Internal notes:
15153:
1.749 raeburn 15154: access controls are stored in file_permissions.db as key=value pairs.
15155: key -> path to file/file_name\0uniqueID:scope_end_start
15156: where scope -> public,guest,course,group,domains or users.
15157: end -> UNIX time for end of access (0 -> no end date)
15158: start -> UNIX time for start of access
15159:
15160: value -> XML description of access control
15161: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
15162: <start></start>
15163: <end></end>
15164:
15165: <password></password> for scope type = guest
15166:
15167: <domain></domain> for scope type = course or group
15168: <number></number>
15169: <roles id="">
15170: <role></role>
15171: <access></access>
15172: <section></section>
15173: <group></group>
15174: </roles>
15175:
15176: <dom></dom> for scope type = domains
15177:
15178: <users> for scope type = users
15179: <user>
15180: <uname></uname>
15181: <udom></udom>
15182: </user>
15183: </users>
15184: </scope>
15185:
15186: Access data is also aggregated for each file in an additional key=value pair:
15187: key -> path to file/file_name\0accesscontrol
15188: value -> reference to hash
15189: hash contains key = value pairs
15190: where key = uniqueID:scope_end_start
15191: value = UNIX time record was last updated
15192:
15193: Used to improve speed of look-ups of access controls for each file.
15194:
15195: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
15196:
1.1198 raeburn 15197: =item *
15198:
1.749 raeburn 15199: modify_access_controls():
15200:
15201: Modifies access controls for a portfolio file
15202: Args
15203: 1. file name
15204: 2. reference to hash of required changes,
15205: 3. domain
15206: 4. username
15207: where domain,username are the domain of the portfolio owner
15208: (either a user or a course)
15209:
15210: Returns:
15211: 1. result of additions or updates ('ok' or 'error', with error message).
15212: 2. result of deletions ('ok' or 'error', with error message).
15213: 3. reference to hash of any new or updated access controls.
15214: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
15215: key = integer (inbound ID)
1.1198 raeburn 15216: value = uniqueID
15217:
15218: =item *
15219:
15220: get_timebased_id():
15221:
15222: Attempts to get a unique timestamp-based suffix for use with items added to a
15223: course via the Course Editor (e.g., folders, composite pages,
15224: group bulletin boards).
15225:
15226: Args: (first three required; six others optional)
15227:
15228: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
15229: docssequence, or name of group
15230:
15231: 2. keyid (alphanumeric): name of temporary locking key in hash,
15232: e.g., num, boardids
15233:
15234: 3. namespace: name of gdbm file used to store suffixes already assigned;
15235: file will be named nohist_namespace.db
15236:
15237: 4. cdom: domain of course; default is current course domain from %env
15238:
15239: 5. cnum: course number; default is current course number from %env
15240:
15241: 6. idtype: set to concat if an additional digit is to be appended to the
15242: unix timestamp to form the suffix, if the plain timestamp is already
15243: in use. Default is to not do this, but simply increment the unix
15244: timestamp by 1 until a unique key is obtained.
15245:
15246: 7. who: holder of locking key; defaults to user:domain for user.
15247:
15248: 8. locktries: number of attempts to obtain a lock (sleep of 1s before
15249: retrying); default is 3.
15250:
15251: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.
15252:
15253: Returns:
15254:
15255: 1. suffix obtained (numeric)
15256:
15257: 2. result of deleting locking key (ok if deleted, or lock never obtained)
15258:
15259: 3. error: contains (localized) error message if an error occurred.
15260:
1.747 albertel 15261:
1.608 albertel 15262: =back
15263:
1.243 albertel 15264: =head2 HTTP Helper Routines
15265:
15266: =over 4
15267:
1.191 harris41 15268: =item *
15269:
15270: escape() : unpack non-word characters into CGI-compatible hex codes
15271:
15272: =item *
15273:
15274: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
15275:
1.243 albertel 15276: =back
15277:
15278: =head1 PRIVATE SUBROUTINES
15279:
15280: =head2 Underlying communication routines (Shouldn't call)
15281:
15282: =over 4
15283:
15284: =item *
15285:
15286: subreply() : tries to pass a message to lonc, returns con_lost if incapable
15287:
15288: =item *
15289:
15290: reply() : uses subreply to send a message to remote machine, logs all failures
15291:
15292: =item *
15293:
15294: critical() : passes a critical message to another server; if cannot
15295: get through then place message in connection buffer directory and
15296: returns con_delayed, if incapable of saving message, returns
15297: con_failed
15298:
15299: =item *
15300:
15301: reconlonc() : tries to reconnect lonc client processes.
15302:
15303: =back
15304:
15305: =head2 Resource Access Logging
15306:
15307: =over 4
15308:
15309: =item *
15310:
15311: flushcourselogs() : flush (save) buffer logs and access logs
15312:
15313: =item *
15314:
15315: courselog($what) : save message for course in hash
15316:
15317: =item *
15318:
15319: courseacclog($what) : save message for course using &courselog(). Perform
15320: special processing for specific resource types (problems, exams, quizzes, etc).
15321:
1.191 harris41 15322: =item *
15323:
15324: goodbye() : flush course logs and log shutting down; it is called in srm.conf
15325: as a PerlChildExitHandler
1.243 albertel 15326:
15327: =back
15328:
15329: =head2 Other
15330:
15331: =over 4
15332:
15333: =item *
15334:
15335: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 15336:
15337: =back
15338:
15339: =cut
1.877 foxr 15340:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>