Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1056.4.33.2.2
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1056.4.33.2 2(raebur 4:2): # $Id: lonnet.pm,v 1.1056.4.33.2.1 2012/02/01 18:27:09 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.871 albertel 78: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1056.4.33 raeburn 79: $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
80: %managerstab);
1.871 albertel 81:
82: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
83: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
84: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 85: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 86:
1.1 albertel 87: use IO::Socket;
1.31 www 88: use GDBM_File;
1.208 albertel 89: use HTML::LCParser;
1.88 www 90: use Fcntl qw(:flock);
1.870 albertel 91: use Storable qw(thaw nfreeze);
1.539 albertel 92: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 93: use Cache::Memcached;
1.676 albertel 94: use Digest::MD5;
1.790 albertel 95: use Math::Random;
1.1024 raeburn 96: use File::MMagic;
1.807 albertel 97: use LONCAPA qw(:DEFAULT :match);
1.740 www 98: use LONCAPA::Configuration;
1.1056.4.13 raeburn 99: use File::Copy;
1.676 albertel 100:
1.195 www 101: my $readit;
1.550 foxr 102: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 103:
1.619 albertel 104: require Exporter;
105:
106: our @ISA = qw (Exporter);
107: our @EXPORT = qw(%env);
108:
1.449 matthew 109:
1.1 albertel 110: # --------------------------------------------------------------------- Logging
1.729 www 111: {
112: my $logid;
113: sub instructor_log {
1.957 raeburn 114: my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
115: if (($cnum eq '') || ($cdom eq '')) {
116: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
117: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
118: }
1.729 www 119: $logid++;
1.957 raeburn 120: my $now = time();
121: my $id=$now.'00000'.$$.'00000'.$logid;
1.729 www 122: return &Apache::lonnet::put('nohist_'.$hash_name,
1.730 www 123: { $id => {
124: 'exe_uname' => $env{'user.name'},
125: 'exe_udom' => $env{'user.domain'},
1.957 raeburn 126: 'exe_time' => $now,
1.730 www 127: 'exe_ip' => $ENV{'REMOTE_ADDR'},
128: 'delflag' => $delflag,
129: 'logentry' => $storehash,
130: 'uname' => $uname,
131: 'udom' => $udom,
132: }
1.957 raeburn 133: },$cdom,$cnum);
1.729 www 134: }
135: }
1.1 albertel 136:
1.163 harris41 137: sub logtouch {
138: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 139: unless (-e "$execdir/logs/lonnet.log") {
140: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 141: close $fh;
142: }
143: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
144: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
145: }
146:
1.1 albertel 147: sub logthis {
148: my $message=shift;
149: my $execdir=$perlvar{'lonDaemons'};
150: my $now=time;
151: my $local=localtime($now);
1.448 albertel 152: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986 foxr 153: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
154: print $fh $logstring;
1.448 albertel 155: close($fh);
156: }
1.1 albertel 157: return 1;
158: }
159:
160: sub logperm {
161: my $message=shift;
162: my $execdir=$perlvar{'lonDaemons'};
163: my $now=time;
164: my $local=localtime($now);
1.448 albertel 165: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
166: print $fh "$now:$message:$local\n";
167: close($fh);
168: }
1.1 albertel 169: return 1;
170: }
171:
1.850 albertel 172: sub create_connection {
1.853 albertel 173: my ($hostname,$lonid) = @_;
1.851 albertel 174: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 175: Type => SOCK_STREAM,
176: Timeout => 10);
177: return 0 if (!$client);
1.890 albertel 178: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 179: my $result = <$client>;
180: chomp($result);
181: return 1 if ($result eq 'done');
182: return 0;
183: }
184:
1.983 raeburn 185: sub get_server_timezone {
186: my ($cnum,$cdom) = @_;
187: my $home=&homeserver($cnum,$cdom);
188: if ($home ne 'no_host') {
189: my $cachetime = 24*3600;
190: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
191: if (defined($cached)) {
192: return $timezone;
193: } else {
194: my $timezone = &reply('servertimezone',$home);
195: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
196: }
197: }
198: }
1.850 albertel 199:
1.1056.4.25 raeburn 200: sub get_server_distarch {
201: my ($lonhost,$ignore_cache) = @_;
202: if (defined($lonhost)) {
203: if (!defined(&hostname($lonhost))) {
204: return;
205: }
206: my $cachetime = 12*3600;
207: if (!$ignore_cache) {
208: my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
209: if (defined($cached)) {
210: return $distarch;
211: }
212: }
213: my $rep = &reply('serverdistarch',$lonhost);
214: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
215: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
216: $rep eq '') {
217: return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
218: }
219: }
220: return;
221: }
222:
1.993 raeburn 223: sub get_server_loncaparev {
1.1056.4.3 raeburn 224: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 225: if (defined($lonhost)) {
226: if (!defined(&hostname($lonhost))) {
227: undef($lonhost);
228: }
229: }
230: if (!defined($lonhost)) {
231: if (defined(&domain($dom,'primary'))) {
232: $lonhost=&domain($dom,'primary');
233: if ($lonhost eq 'no_host') {
234: undef($lonhost);
235: }
236: }
237: }
238: if (defined($lonhost)) {
1.1056.4.3 raeburn 239: my $cachetime = 12*3600;
240: if (!$ignore_cache) {
241: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
242: if (defined($cached)) {
243: return $loncaparev;
244: }
245: }
246: my ($answer,$loncaparev);
247: my @ids=¤t_machine_ids();
248: if (grep(/^\Q$lonhost\E$/,@ids)) {
249: $answer = $perlvar{'lonVersion'};
1.1056.4.7 raeburn 250: if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1056.4.3 raeburn 251: $loncaparev = $1;
252: }
253: } else {
254: $answer = &reply('serverloncaparev',$lonhost);
255: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
256: if ($caller eq 'loncron') {
257: my $ua=new LWP::UserAgent;
1.1056.4.8 raeburn 258: $ua->timeout(4);
1.1056.4.3 raeburn 259: my $protocol = $protocol{$lonhost};
260: $protocol = 'http' if ($protocol ne 'https');
261: my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
262: my $request=new HTTP::Request('GET',$url);
263: my $response=$ua->request($request);
264: unless ($response->is_error()) {
265: my $content = $response->content;
1.1056.4.7 raeburn 266: if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1056.4.3 raeburn 267: $loncaparev = $1;
268: }
269: }
270: } else {
271: $loncaparev = $loncaparevs{$lonhost};
272: }
1.1056.4.7 raeburn 273: } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1056.4.3 raeburn 274: $loncaparev = $1;
275: }
276: }
277: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
278: }
279: }
280:
281: sub get_server_homeID {
282: my ($hostname,$ignore_cache,$caller) = @_;
283: unless ($ignore_cache) {
284: my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
1.993 raeburn 285: if (defined($cached)) {
1.1056.4.3 raeburn 286: return $serverhomeID;
287: }
288: }
289: my $cachetime = 12*3600;
290: my $serverhomeID;
291: if ($caller eq 'loncron') {
292: my @machine_ids = &machine_ids($hostname);
293: foreach my $id (@machine_ids) {
294: my $response = &reply('serverhomeID',$id);
295: unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
296: $serverhomeID = $response;
297: last;
298: }
1.993 raeburn 299: }
1.1056.4.3 raeburn 300: if ($serverhomeID eq '') {
301: $serverhomeID = $machine_ids[-1];
302: }
303: } else {
304: $serverhomeID = $serverhomeIDs{$hostname};
1.993 raeburn 305: }
1.1056.4.3 raeburn 306: return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
1.993 raeburn 307: }
308:
1.1 albertel 309: # -------------------------------------------------- Non-critical communication
310: sub subreply {
311: my ($cmd,$server)=@_;
1.838 albertel 312: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 313: #
314: # With loncnew process trimming, there's a timing hole between lonc server
315: # process exit and the master server picking up the listen on the AF_UNIX
316: # socket. In that time interval, a lock file will exist:
317:
318: my $lockfile=$peerfile.".lock";
319: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
320: sleep(1);
321: }
322: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 323: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 324: #
1.550 foxr 325: # We'll give the connection a few tries before abandoning it. If
326: # connection is not possible, we'll con_lost back to the client.
327: #
328: my $client;
329: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
330: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
331: Type => SOCK_STREAM,
332: Timeout => 10);
1.869 albertel 333: if ($client) {
1.550 foxr 334: last; # Connected!
1.850 albertel 335: } else {
1.853 albertel 336: &create_connection(&hostname($server),$server);
1.550 foxr 337: }
1.850 albertel 338: sleep(1); # Try again later if failed connection.
1.550 foxr 339: }
340: my $answer;
341: if ($client) {
1.704 albertel 342: print $client "sethost:$server:$cmd\n";
1.550 foxr 343: $answer=<$client>;
344: if (!$answer) { $answer="con_lost"; }
345: chomp($answer);
346: } else {
347: $answer = 'con_lost'; # Failed connection.
348: }
1.1 albertel 349: return $answer;
350: }
351:
352: sub reply {
353: my ($cmd,$server)=@_;
1.838 albertel 354: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 355: my $answer=subreply($cmd,$server);
1.65 www 356: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 357: &logthis("<font color=\"blue\">WARNING:".
1.12 www 358: " $cmd to $server returned $answer</font>");
359: }
1.1 albertel 360: return $answer;
361: }
362:
363: # ----------------------------------------------------------- Send USR1 to lonc
364:
365: sub reconlonc {
1.891 albertel 366: my ($lonid) = @_;
367: my $hostname = &hostname($lonid);
368: if ($lonid) {
369: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
370: if ($hostname && -e $peerfile) {
371: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
372: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
373: Type => SOCK_STREAM,
374: Timeout => 10);
375: if ($client) {
376: print $client ("reset_retries\n");
377: my $answer=<$client>;
378: #reset just this one.
379: }
380: }
381: return;
382: }
383:
1.836 www 384: &logthis("Trying to reconnect lonc");
1.1 albertel 385: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 386: if (open(my $fh,"<$loncfile")) {
1.1 albertel 387: my $loncpid=<$fh>;
388: chomp($loncpid);
389: if (kill 0 => $loncpid) {
390: &logthis("lonc at pid $loncpid responding, sending USR1");
391: kill USR1 => $loncpid;
392: sleep 1;
1.836 www 393: } else {
1.12 www 394: &logthis(
1.672 albertel 395: "<font color=\"blue\">WARNING:".
1.12 www 396: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 397: }
398: } else {
1.836 www 399: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 400: }
401: }
402:
403: # ------------------------------------------------------ Critical communication
1.12 www 404:
1.1 albertel 405: sub critical {
406: my ($cmd,$server)=@_;
1.838 albertel 407: unless (&hostname($server)) {
1.672 albertel 408: &logthis("<font color=\"blue\">WARNING:".
1.89 www 409: " Critical message to unknown server ($server)</font>");
410: return 'no_such_host';
411: }
1.1 albertel 412: my $answer=reply($cmd,$server);
413: if ($answer eq 'con_lost') {
414: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 415: my $answer=reply($cmd,$server);
1.1 albertel 416: if ($answer eq 'con_lost') {
417: my $now=time;
418: my $middlename=$cmd;
1.5 www 419: $middlename=substr($middlename,0,16);
1.1 albertel 420: $middlename=~s/\W//g;
421: my $dfilename=
1.305 www 422: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
423: $dumpcount++;
1.1 albertel 424: {
1.448 albertel 425: my $dfh;
426: if (open($dfh,">$dfilename")) {
427: print $dfh "$cmd\n";
428: close($dfh);
429: }
1.1 albertel 430: }
431: sleep 2;
432: my $wcmd='';
433: {
1.448 albertel 434: my $dfh;
435: if (open($dfh,"<$dfilename")) {
436: $wcmd=<$dfh>;
437: close($dfh);
438: }
1.1 albertel 439: }
440: chomp($wcmd);
1.7 www 441: if ($wcmd eq $cmd) {
1.672 albertel 442: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 443: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 444: &logperm("D:$server:$cmd");
445: return 'con_delayed';
446: } else {
1.672 albertel 447: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 448: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 449: &logperm("F:$server:$cmd");
450: return 'con_failed';
451: }
452: }
453: }
454: return $answer;
1.405 albertel 455: }
456:
1.755 albertel 457: # ------------------------------------------- check if return value is an error
458:
459: sub error {
460: my ($result) = @_;
1.756 albertel 461: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 462: if ($2 == 2) { return undef; }
463: return $1;
464: }
465: return undef;
466: }
467:
1.783 albertel 468: sub convert_and_load_session_env {
469: my ($lonidsdir,$handle)=@_;
470: my @profile;
471: {
1.917 albertel 472: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
473: if (!$opened) {
1.915 albertel 474: return 0;
475: }
1.783 albertel 476: flock($idf,LOCK_SH);
477: @profile=<$idf>;
478: close($idf);
479: }
480: my %temp_env;
481: foreach my $line (@profile) {
1.786 albertel 482: if ($line !~ m/=/) {
483: return 0;
484: }
1.783 albertel 485: chomp($line);
486: my ($envname,$envvalue)=split(/=/,$line,2);
487: $temp_env{&unescape($envname)} = &unescape($envvalue);
488: }
489: unlink("$lonidsdir/$handle.id");
490: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
491: 0640)) {
492: %disk_env = %temp_env;
493: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
494: untie(%disk_env);
495: }
1.786 albertel 496: return 1;
1.783 albertel 497: }
498:
1.374 www 499: # ------------------------------------------- Transfer profile into environment
1.780 albertel 500: my $env_loaded;
501: sub transfer_profile_to_env {
1.788 albertel 502: my ($lonidsdir,$handle,$force_transfer) = @_;
503: if (!$force_transfer && $env_loaded) { return; }
1.374 www 504:
1.720 albertel 505: if (!defined($lonidsdir)) {
506: $lonidsdir = $perlvar{'lonIDsDir'};
507: }
508: if (!defined($handle)) {
509: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
510: }
511:
1.786 albertel 512: my $convert;
513: {
1.917 albertel 514: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
515: if (!$opened) {
1.915 albertel 516: return;
517: }
1.786 albertel 518: flock($idf,LOCK_SH);
519: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
520: &GDBM_READER(),0640)) {
521: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
522: untie(%disk_env);
523: } else {
524: $convert = 1;
525: }
526: }
527: if ($convert) {
528: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
529: &logthis("Failed to load session, or convert session.");
530: }
1.374 www 531: }
1.783 albertel 532:
1.786 albertel 533: my %remove;
1.783 albertel 534: while ( my $envname = each(%env) ) {
1.433 matthew 535: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
536: if ($time < time-300) {
1.783 albertel 537: $remove{$key}++;
1.433 matthew 538: }
539: }
540: }
1.783 albertel 541:
1.619 albertel 542: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 543: $env_loaded=1;
1.783 albertel 544: foreach my $expired_key (keys(%remove)) {
1.433 matthew 545: &delenv($expired_key);
1.374 www 546: }
1.1 albertel 547: }
548:
1.916 albertel 549: # ---------------------------------------------------- Check for valid session
550: sub check_for_valid_session {
551: my ($r) = @_;
552: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
553: my $lonid=$cookies{'lonID'};
554: return undef if (!$lonid);
555:
556: my $handle=&LONCAPA::clean_handle($lonid->value);
557: my $lonidsdir=$r->dir_config('lonIDsDir');
558: return undef if (!-e "$lonidsdir/$handle.id");
559:
1.917 albertel 560: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
561: return undef if (!$opened);
1.916 albertel 562:
563: flock($idf,LOCK_SH);
564: my %disk_env;
565: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
566: &GDBM_READER(),0640)) {
567: return undef;
568: }
569:
570: if (!defined($disk_env{'user.name'})
571: || !defined($disk_env{'user.domain'})) {
572: return undef;
573: }
574: return $handle;
575: }
576:
1.830 albertel 577: sub timed_flock {
578: my ($file,$lock_type) = @_;
579: my $failed=0;
580: eval {
581: local $SIG{__DIE__}='DEFAULT';
582: local $SIG{ALRM}=sub {
583: $failed=1;
584: die("failed lock");
585: };
586: alarm(13);
587: flock($file,$lock_type);
588: alarm(0);
589: };
590: if ($failed) {
591: return undef;
592: } else {
593: return 1;
594: }
595: }
596:
1.5 www 597: # ---------------------------------------------------------- Append Environment
598:
599: sub appenv {
1.949 raeburn 600: my ($newenv,$roles) = @_;
601: if (ref($newenv) eq 'HASH') {
602: foreach my $key (keys(%{$newenv})) {
603: my $refused = 0;
604: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
605: $refused = 1;
606: if (ref($roles) eq 'ARRAY') {
607: my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
608: if (grep(/^\Q$role\E$/,@{$roles})) {
609: $refused = 0;
610: }
611: }
612: }
613: if ($refused) {
614: &logthis("<font color=\"blue\">WARNING: ".
615: "Attempt to modify environment ".$key." to ".$newenv->{$key}
616: .'</font>');
617: delete($newenv->{$key});
618: } else {
619: $env{$key}=$newenv->{$key};
620: }
621: }
622: my $opened = open(my $env_file,'+<',$env{'user.environment'});
623: if ($opened
624: && &timed_flock($env_file,LOCK_EX)
625: &&
626: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
627: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
628: while (my ($key,$value) = each(%{$newenv})) {
629: $disk_env{$key} = $value;
630: }
631: untie(%disk_env);
1.35 www 632: }
1.191 harris41 633: }
1.56 www 634: return 'ok';
635: }
636: # ----------------------------------------------------- Delete from Environment
637:
638: sub delenv {
1.1056.4.33.2 2(raebur 639:2): my ($delthis,$regexp,$roles) = @_;
640:2): if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
641:2): my $refused = 1;
642:2): if (ref($roles) eq 'ARRAY') {
643:2): my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
644:2): if (grep(/^\Q$role\E$/,@{$roles})) {
645:2): $refused = 0;
646:2): }
647:2): }
648:2): if ($refused) {
649:2): &logthis("<font color=\"blue\">WARNING: ".
650:2): "Attempt to delete from environment ".$delthis);
651:2): return 'error';
652:2): }
1.56 www 653: }
1.917 albertel 654: my $opened = open(my $env_file,'+<',$env{'user.environment'});
655: if ($opened
1.915 albertel 656: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 657: &&
658: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
659: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 660: foreach my $key (keys(%disk_env)) {
1.987 raeburn 661: if ($regexp) {
662: if ($key=~/^$delthis/) {
663: delete($env{$key});
664: delete($disk_env{$key});
665: }
666: } else {
667: if ($key=~/^\Q$delthis\E/) {
668: delete($env{$key});
669: delete($disk_env{$key});
670: }
671: }
1.448 albertel 672: }
1.783 albertel 673: untie(%disk_env);
1.5 www 674: }
675: return 'ok';
1.369 albertel 676: }
677:
1.790 albertel 678: sub get_env_multiple {
679: my ($name) = @_;
680: my @values;
681: if (defined($env{$name})) {
682: # exists is it an array
683: if (ref($env{$name})) {
684: @values=@{ $env{$name} };
685: } else {
686: $values[0]=$env{$name};
687: }
688: }
689: return(@values);
690: }
691:
1.958 www 692: # ------------------------------------------------------------------- Locking
693:
694: sub set_lock {
695: my ($text)=@_;
696: $locknum++;
697: my $id=$$.'-'.$locknum;
698: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
699: 'session.lock.'.$id => $text});
700: return $id;
701: }
702:
703: sub get_locks {
704: my $num=0;
705: my %texts=();
706: foreach my $lock (split(/\,/,$env{'session.locks'})) {
707: if ($lock=~/\w/) {
708: $num++;
709: $texts{$lock}=$env{'session.lock.'.$lock};
710: }
711: }
712: return ($num,%texts);
713: }
714:
715: sub remove_lock {
716: my ($id)=@_;
717: my $newlocks='';
718: foreach my $lock (split(/\,/,$env{'session.locks'})) {
719: if (($lock=~/\w/) && ($lock ne $id)) {
720: $newlocks.=','.$lock;
721: }
722: }
723: &appenv({'session.locks' => $newlocks});
724: &delenv('session.lock.'.$id);
725: }
726:
727: sub remove_all_locks {
728: my $activelocks=$env{'session.locks'};
729: foreach my $lock (split(/\,/,$env{'session.locks'})) {
730: if ($lock=~/\w/) {
731: &remove_lock($lock);
732: }
733: }
734: }
735:
736:
1.369 albertel 737: # ------------------------------------------ Find out current server userload
738: sub userload {
739: my $numusers=0;
740: {
741: opendir(LONIDS,$perlvar{'lonIDsDir'});
742: my $filename;
743: my $curtime=time;
744: while ($filename=readdir(LONIDS)) {
1.925 albertel 745: next if ($filename eq '.' || $filename eq '..');
746: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 747: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 748: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 749: }
750: closedir(LONIDS);
751: }
752: my $userloadpercent=0;
753: my $maxuserload=$perlvar{'lonUserLoadLim'};
754: if ($maxuserload) {
1.371 albertel 755: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 756: }
1.372 albertel 757: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 758: return $userloadpercent;
1.283 www 759: }
760:
761: # ------------------------------------------ Fight off request when overloaded
762:
763: sub overloaderror {
764: my ($r,$checkserver)=@_;
765: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
766: my $loadavg;
767: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 768: open(my $loadfile,'/proc/loadavg');
1.283 www 769: $loadavg=<$loadfile>;
770: $loadavg =~ s/\s.*//g;
1.285 matthew 771: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 772: close($loadfile);
1.283 www 773: } else {
774: $loadavg=&reply('load',$checkserver);
775: }
1.285 matthew 776: my $overload=$loadavg-100;
1.283 www 777: if ($overload>0) {
1.285 matthew 778: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 779: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 780: return 413;
1.283 www 781: }
782: return '';
1.5 www 783: }
1.1 albertel 784:
785: # ------------------------------ Find server with least workload from spare.tab
1.11 www 786:
1.1 albertel 787: sub spareserver {
1.1056.4.23 raeburn 788: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 789: my $spare_server;
1.370 albertel 790: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 791: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
792: : $userloadpercent;
1.1056.4.8 raeburn 793: my ($uint_dom,$remotesessions);
1.1056.4.23 raeburn 794: if (($udom ne '') && (&domain($udom) ne '')) {
795: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1.1056.4.8 raeburn 796: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.23 raeburn 797: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
1.1056.4.8 raeburn 798: $remotesessions = $udomdefaults{'remotesessions'};
799: }
1.784 albertel 800: foreach my $try_server (@{ $spareid{'primary'} }) {
1.1056.4.8 raeburn 801: if ($uint_dom) {
1.1056.4.23 raeburn 802: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
803: $try_server));
1.1056.4.8 raeburn 804: }
1.784 albertel 805: ($spare_server, $lowest_load) =
806: &compare_server_load($try_server, $spare_server, $lowest_load);
807: }
808:
809: my $found_server = ($spare_server ne '' && $lowest_load < 100);
810:
811: if (!$found_server) {
812: foreach my $try_server (@{ $spareid{'default'} }) {
1.1056.4.8 raeburn 813: if ($uint_dom) {
1.1056.4.23 raeburn 814: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
815: $try_server));
1.1056.4.8 raeburn 816: }
1.784 albertel 817: ($spare_server, $lowest_load) =
818: &compare_server_load($try_server, $spare_server, $lowest_load);
819: }
820: }
821:
822: if (!$want_server_name) {
1.968 raeburn 823: my $protocol = 'http';
824: if ($protocol{$spare_server} eq 'https') {
825: $protocol = $protocol{$spare_server};
826: }
1.1001 raeburn 827: if (defined($spare_server)) {
828: my $hostname = &hostname($spare_server);
1.1056.4.8 raeburn 829: if (defined($hostname)) {
1.1001 raeburn 830: $spare_server = $protocol.'://'.$hostname;
831: }
832: }
1.784 albertel 833: }
834: return $spare_server;
835: }
836:
837: sub compare_server_load {
838: my ($try_server, $spare_server, $lowest_load) = @_;
839:
840: my $loadans = &reply('load', $try_server);
841: my $userloadans = &reply('userload',$try_server);
842:
843: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1056.4.29 raeburn 844: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 845: }
846:
847: my $load;
848: if ($loadans =~ /\d/) {
849: if ($userloadans =~ /\d/) {
850: #both are numbers, pick the bigger one
851: $load = ($loadans > $userloadans) ? $loadans
852: : $userloadans;
1.411 albertel 853: } else {
1.784 albertel 854: $load = $loadans;
1.411 albertel 855: }
1.784 albertel 856: } else {
857: $load = $userloadans;
858: }
859:
860: if (($load =~ /\d/) && ($load < $lowest_load)) {
861: $spare_server = $try_server;
862: $lowest_load = $load;
1.370 albertel 863: }
1.784 albertel 864: return ($spare_server,$lowest_load);
1.202 matthew 865: }
1.914 albertel 866:
867: # --------------------------- ask offload servers if user already has a session
868: sub find_existing_session {
869: my ($udom,$uname) = @_;
870: foreach my $try_server (@{ $spareid{'primary'} },
871: @{ $spareid{'default'} }) {
872: return $try_server if (&has_user_session($try_server, $udom, $uname));
873: }
874: return;
875: }
876:
877: # -------------------------------- ask if server already has a session for user
878: sub has_user_session {
879: my ($lonid,$udom,$uname) = @_;
880: my $result = &reply(join(':','userhassession',
881: map {&escape($_)} ($udom,$uname)),$lonid);
882: return 1 if ($result eq 'ok');
883:
884: return 0;
885: }
886:
1.1056.4.3 raeburn 887: # --------- determine least loaded server in a user's domain which allows login
888:
889: sub choose_server {
1.1056.4.31 raeburn 890: my ($udom,$checkloginvia) = @_;
1.1056.4.3 raeburn 891: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
892: my %servers = &get_servers($udom);
893: my $lowest_load = 30000;
1.1056.4.33.2 1(raebur 894:2): my ($login_host,$hostname,$portal_path,$isredirect);
1.1056.4.3 raeburn 895: foreach my $lonhost (keys(%servers)) {
1.1056.4.31 raeburn 896: my $loginvia;
897: if ($checkloginvia) {
898: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
899: if ($loginvia) {
900: my ($server,$path) = split(/:/,$loginvia);
901: ($login_host, $lowest_load) =
902: &compare_server_load($lonhost, $login_host, $lowest_load);
903: if ($login_host eq $server) {
904: $portal_path = $path;
1.1056.4.33.2 1(raebur 905:2): $isredirect = 1;
1.1056.4.31 raeburn 906: }
907: } else {
908: ($login_host, $lowest_load) =
909: &compare_server_load($lonhost, $login_host, $lowest_load);
910: if ($login_host eq $lonhost) {
911: $portal_path = '';
1.1056.4.33.2 1(raebur 912:2): $isredirect = '';
1.1056.4.31 raeburn 913: }
914: }
915: } else {
1.1056.4.3 raeburn 916: ($login_host, $lowest_load) =
1.1056.4.31 raeburn 917: &compare_server_load($lonhost, $login_host, $lowest_load);
1.1056.4.3 raeburn 918: }
919: }
920: if ($login_host ne '') {
1.1056.4.31 raeburn 921: $hostname = &hostname($login_host);
1.1056.4.3 raeburn 922: }
1.1056.4.33.2 1(raebur 923:2): return ($login_host,$hostname,$portal_path,$isredirect);
1.1056.4.3 raeburn 924: }
925:
1.202 matthew 926: # --------------------------------------------- Try to change a user's password
927:
928: sub changepass {
1.799 raeburn 929: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 930: $currentpass = &escape($currentpass);
931: $newpass = &escape($newpass);
1.1030 raeburn 932: my $lonhost = $perlvar{'lonHostID'};
933: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 934: $server);
935: if (! $answer) {
936: &logthis("No reply on password change request to $server ".
937: "by $uname in domain $udom.");
938: } elsif ($answer =~ "^ok") {
939: &logthis("$uname in $udom successfully changed their password ".
940: "on $server.");
941: } elsif ($answer =~ "^pwchange_failure") {
942: &logthis("$uname in $udom was unable to change their password ".
943: "on $server. The action was blocked by either lcpasswd ".
944: "or pwchange");
945: } elsif ($answer =~ "^non_authorized") {
946: &logthis("$uname in $udom did not get their password correct when ".
947: "attempting to change it on $server.");
948: } elsif ($answer =~ "^auth_mode_error") {
949: &logthis("$uname in $udom attempted to change their password despite ".
950: "not being locally or internally authenticated on $server.");
951: } elsif ($answer =~ "^unknown_user") {
952: &logthis("$uname in $udom attempted to change their password ".
953: "on $server but were unable to because $server is not ".
954: "their home server.");
955: } elsif ($answer =~ "^refused") {
956: &logthis("$server refused to change $uname in $udom password because ".
957: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 958: } elsif ($answer =~ "invalid_client") {
959: &logthis("$server refused to change $uname in $udom password because ".
960: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 961: }
962: return $answer;
1.1 albertel 963: }
964:
1.169 harris41 965: # ----------------------- Try to determine user's current authentication scheme
966:
967: sub queryauthenticate {
968: my ($uname,$udom)=@_;
1.456 albertel 969: my $uhome=&homeserver($uname,$udom);
970: if (!$uhome) {
971: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
972: return 'no_host';
973: }
974: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
975: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
976: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 977: }
1.456 albertel 978: return $answer;
1.169 harris41 979: }
980:
1.1 albertel 981: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 982:
1.1 albertel 983: sub authenticate {
1.1056.4.3 raeburn 984: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 985: $upass=&escape($upass);
986: $uname= &LONCAPA::clean_username($uname);
1.836 www 987: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 988: my $newhome;
1.836 www 989: if ((!$uhome) || ($uhome eq 'no_host')) {
990: # Maybe the machine was offline and only re-appeared again recently?
991: &reconlonc();
992: # One more
1.952 raeburn 993: $uhome=&homeserver($uname,$udom,1);
994: if (($uhome eq 'no_host') && $checkdefauth) {
995: if (defined(&domain($udom,'primary'))) {
996: $newhome=&domain($udom,'primary');
997: }
998: if ($newhome ne '') {
999: $uhome = $newhome;
1000: }
1001: }
1.836 www 1002: if ((!$uhome) || ($uhome eq 'no_host')) {
1003: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1004: return 'no_host';
1005: }
1.1 albertel 1006: }
1.1056.4.3 raeburn 1007: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1008: if ($answer eq 'authorized') {
1.952 raeburn 1009: if ($newhome) {
1010: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1011: return 'no_account_on_host';
1012: } else {
1013: &logthis("User $uname at $udom authorized by $uhome");
1014: return $uhome;
1015: }
1.471 albertel 1016: }
1017: if ($answer eq 'non_authorized') {
1018: &logthis("User $uname at $udom rejected by $uhome");
1019: return 'no_host';
1.9 www 1020: }
1.471 albertel 1021: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1022: return 'no_host';
1023: }
1024:
1.1056.4.3 raeburn 1025: sub can_host_session {
1026: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1027: my $canhost = 1;
1028: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1029: if (ref($remotesessions) eq 'HASH') {
1030: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1031: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1032: $canhost = 0;
1033: } else {
1034: $canhost = 1;
1035: }
1036: }
1037: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1038: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1039: $canhost = 1;
1040: } else {
1041: $canhost = 0;
1042: }
1043: }
1044: if ($canhost) {
1045: if ($remotesessions->{'version'} ne '') {
1046: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1047: if ($reqmajor ne '' && $reqminor ne '') {
1048: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1049: my $major = $1;
1050: my $minor = $2;
1051: if (($major < $reqmajor ) ||
1052: (($major == $reqmajor) && ($minor < $reqminor))) {
1053: $canhost = 0;
1054: }
1055: } else {
1056: $canhost = 0;
1057: }
1058: }
1059: }
1060: }
1061: }
1062: if ($canhost) {
1063: if (ref($hostedsessions) eq 'HASH') {
1.1056.4.28 raeburn 1064: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1065: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.3 raeburn 1066: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1067: if (($uint_dom ne '') &&
1068: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1056.4.3 raeburn 1069: $canhost = 0;
1070: } else {
1071: $canhost = 1;
1072: }
1073: }
1074: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1075: if (($uint_dom ne '') &&
1076: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1056.4.3 raeburn 1077: $canhost = 1;
1078: } else {
1079: $canhost = 0;
1080: }
1081: }
1082: }
1083: }
1084: return $canhost;
1085: }
1086:
1.1056.4.8 raeburn 1087: sub spare_can_host {
1088: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1089: my $canhost=1;
1090: my @intdoms;
1091: my $internet_names = &Apache::lonnet::get_internet_names($try_server);
1092: if (ref($internet_names) eq 'ARRAY') {
1093: @intdoms = @{$internet_names};
1094: }
1095: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1096: my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
1097: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
1098: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
1099: my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
1100: $canhost = &can_host_session($udom,$try_server,$remoterev,
1101: $remotesessions,
1102: $defdomdefaults{'hostedsessions'});
1103: }
1104: return $canhost;
1105: }
1106:
1.1 albertel 1107: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1108:
1.599 albertel 1109: my %homecache;
1.1 albertel 1110: sub homeserver {
1.230 stredwic 1111: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1112: my $index="$uname:$udom";
1.426 albertel 1113:
1.599 albertel 1114: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1115:
1116: my %servers = &get_servers($udom,'library');
1117: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1118: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1119: exists($badServerCache{$tryserver}));
1.841 albertel 1120:
1121: my $answer=reply("home:$udom:$uname",$tryserver);
1122: if ($answer eq 'found') {
1123: delete($badServerCache{$tryserver});
1124: return $homecache{$index}=$tryserver;
1125: } elsif ($answer eq 'no_host') {
1126: $badServerCache{$tryserver}=1;
1127: }
1.1 albertel 1128: }
1129: return 'no_host';
1.70 www 1130: }
1131:
1132: # ------------------------------------- Find the usernames behind a list of IDs
1133:
1134: sub idget {
1135: my ($udom,@ids)=@_;
1136: my %returnhash=();
1137:
1.841 albertel 1138: my %servers = &get_servers($udom,'library');
1139: foreach my $tryserver (keys(%servers)) {
1140: my $idlist=join('&',@ids);
1141: $idlist=~tr/A-Z/a-z/;
1142: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1143: my @answer=();
1144: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1145: @answer=split(/\&/,$reply);
1146: } ;
1147: my $i;
1148: for ($i=0;$i<=$#ids;$i++) {
1149: if ($answer[$i]) {
1150: $returnhash{$ids[$i]}=$answer[$i];
1151: }
1152: }
1153: }
1.70 www 1154: return %returnhash;
1155: }
1156:
1157: # ------------------------------------- Find the IDs behind a list of usernames
1158:
1159: sub idrget {
1160: my ($udom,@unames)=@_;
1161: my %returnhash=();
1.800 albertel 1162: foreach my $uname (@unames) {
1163: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1164: }
1.70 www 1165: return %returnhash;
1166: }
1167:
1168: # ------------------------------- Store away a list of names and associated IDs
1169:
1170: sub idput {
1171: my ($udom,%ids)=@_;
1172: my %servers=();
1.800 albertel 1173: foreach my $uname (keys(%ids)) {
1174: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1175: my $uhom=&homeserver($uname,$udom);
1.70 www 1176: if ($uhom ne 'no_host') {
1.800 albertel 1177: my $id=&escape($ids{$uname});
1.70 www 1178: $id=~tr/A-Z/a-z/;
1.800 albertel 1179: my $esc_unam=&escape($uname);
1.70 www 1180: if ($servers{$uhom}) {
1.800 albertel 1181: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1182: } else {
1.800 albertel 1183: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1184: }
1185: }
1.191 harris41 1186: }
1.800 albertel 1187: foreach my $server (keys(%servers)) {
1188: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1189: }
1.344 www 1190: }
1191:
1.1023 raeburn 1192: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1193: sub dump_dom {
1.1023 raeburn 1194: my ($namespace,$udom,$regexp,$range)=@_;
1.1012 raeburn 1195: if (!$udom) {
1196: $udom=$env{'user.domain'};
1197: }
1198: my %returnhash;
1.1023 raeburn 1199: if ($udom) {
1200: my $uname = &get_domainconfiguser($udom);
1201: %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012 raeburn 1202: }
1203: return %returnhash;
1204: }
1205:
1.1023 raeburn 1206: # ------------------------------------------ get items from domain db files
1.806 raeburn 1207:
1208: sub get_dom {
1.860 raeburn 1209: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 1210: my $items='';
1211: foreach my $item (@$storearr) {
1212: $items.=&escape($item).'&';
1213: }
1214: $items=~s/\&$//;
1.860 raeburn 1215: if (!$udom) {
1216: $udom=$env{'user.domain'};
1217: if (defined(&domain($udom,'primary'))) {
1218: $uhome=&domain($udom,'primary');
1219: } else {
1.874 albertel 1220: undef($uhome);
1.860 raeburn 1221: }
1222: } else {
1223: if (!$uhome) {
1224: if (defined(&domain($udom,'primary'))) {
1225: $uhome=&domain($udom,'primary');
1226: }
1227: }
1228: }
1229: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1230: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1231: my %returnhash;
1.875 albertel 1232: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1233: return %returnhash;
1234: }
1.806 raeburn 1235: my @pairs=split(/\&/,$rep);
1236: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1237: return @pairs;
1238: }
1239: my $i=0;
1240: foreach my $item (@$storearr) {
1241: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1242: $i++;
1243: }
1244: return %returnhash;
1245: } else {
1.880 banghart 1246: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1247: }
1248: }
1249:
1250: # -------------------------------------------- put items in domain db files
1251:
1252: sub put_dom {
1.860 raeburn 1253: my ($namespace,$storehash,$udom,$uhome)=@_;
1254: if (!$udom) {
1255: $udom=$env{'user.domain'};
1256: if (defined(&domain($udom,'primary'))) {
1257: $uhome=&domain($udom,'primary');
1258: } else {
1.874 albertel 1259: undef($uhome);
1.860 raeburn 1260: }
1261: } else {
1262: if (!$uhome) {
1263: if (defined(&domain($udom,'primary'))) {
1264: $uhome=&domain($udom,'primary');
1265: }
1266: }
1267: }
1268: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1269: my $items='';
1270: foreach my $item (keys(%$storehash)) {
1271: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1272: }
1273: $items=~s/\&$//;
1274: return &reply("putdom:$udom:$namespace:$items",$uhome);
1275: } else {
1.860 raeburn 1276: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1277: }
1278: }
1279:
1.1023 raeburn 1280: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1281: sub newput_dom {
1.1023 raeburn 1282: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1283: my $result;
1284: if (!$udom) {
1285: $udom=$env{'user.domain'};
1286: }
1.1023 raeburn 1287: if ($udom) {
1288: my $uname = &get_domainconfiguser($udom);
1289: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1290: }
1291: return $result;
1292: }
1293:
1.1023 raeburn 1294: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1295: sub del_dom {
1.1023 raeburn 1296: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1297: if (ref($storearr) eq 'ARRAY') {
1298: if (!$udom) {
1299: $udom=$env{'user.domain'};
1300: }
1.1023 raeburn 1301: if ($udom) {
1302: my $uname = &get_domainconfiguser($udom);
1303: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1304: }
1305: }
1306: }
1307:
1.1023 raeburn 1308: # ----------------------------------construct domainconfig user for a domain
1309: sub get_domainconfiguser {
1310: my ($udom) = @_;
1311: return $udom.'-domainconfig';
1312: }
1313:
1.837 raeburn 1314: sub retrieve_inst_usertypes {
1315: my ($udom) = @_;
1316: my (%returnhash,@order);
1.989 raeburn 1317: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1318: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1319: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1320: %returnhash = %{$domdefs{'inststatustypes'}};
1321: @order = @{$domdefs{'inststatusorder'}};
1322: } else {
1323: if (defined(&domain($udom,'primary'))) {
1324: my $uhome=&domain($udom,'primary');
1325: my $rep=&reply("inst_usertypes:$udom",$uhome);
1326: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1327: &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
1328: return (\%returnhash,\@order);
1329: }
1330: my ($hashitems,$orderitems) = split(/:/,$rep);
1331: my @pairs=split(/\&/,$hashitems);
1332: foreach my $item (@pairs) {
1333: my ($key,$value)=split(/=/,$item,2);
1334: $key = &unescape($key);
1335: next if ($key =~ /^error: 2 /);
1336: $returnhash{$key}=&thaw_unescape($value);
1337: }
1338: my @esc_order = split(/\&/,$orderitems);
1339: foreach my $item (@esc_order) {
1340: push(@order,&unescape($item));
1341: }
1342: } else {
1343: &logthis("get_dom failed - no primary domain server for $udom");
1.837 raeburn 1344: }
1345: }
1346: return (\%returnhash,\@order);
1347: }
1348:
1.868 raeburn 1349: sub is_domainimage {
1350: my ($url) = @_;
1351: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
1352: if (&domain($1) ne '') {
1353: return '1';
1354: }
1355: }
1356: return;
1357: }
1358:
1.899 raeburn 1359: sub inst_directory_query {
1360: my ($srch) = @_;
1361: my $udom = $srch->{'srchdomain'};
1362: my %results;
1363: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1364: my $outcome;
1.899 raeburn 1365: if ($homeserver ne '') {
1.904 albertel 1366: my $queryid=&reply("querysend:instdirsearch:".
1367: &escape($srch->{'srchby'}).':'.
1368: &escape($srch->{'srchterm'}).':'.
1369: &escape($srch->{'srchtype'}),$homeserver);
1370: my $host=&hostname($homeserver);
1371: if ($queryid !~/^\Q$host\E\_/) {
1372: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1373: return;
1374: }
1375: my $response = &get_query_reply($queryid);
1376: my $maxtries = 5;
1377: my $tries = 1;
1378: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1379: $response = &get_query_reply($queryid);
1380: $tries ++;
1381: }
1382:
1383: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 1384: if ($response eq 'unavailable') {
1385: $outcome = $response;
1386: } else {
1387: $outcome = 'ok';
1388: my @matches = split(/\n/,$response);
1389: foreach my $match (@matches) {
1390: my ($key,$value) = split(/=/,$match);
1391: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
1392: }
1.899 raeburn 1393: }
1394: }
1395: }
1.909 raeburn 1396: return ($outcome,%results);
1.899 raeburn 1397: }
1398:
1399: sub usersearch {
1400: my ($srch) = @_;
1401: my $dom = $srch->{'srchdomain'};
1402: my %results;
1403: my %libserv = &all_library();
1404: my $query = 'usersearch';
1405: foreach my $tryserver (keys(%libserv)) {
1406: if (&host_domain($tryserver) eq $dom) {
1407: my $host=&hostname($tryserver);
1408: my $queryid=
1.911 raeburn 1409: &reply("querysend:".&escape($query).':'.
1410: &escape($srch->{'srchby'}).':'.
1.899 raeburn 1411: &escape($srch->{'srchtype'}).':'.
1412: &escape($srch->{'srchterm'}),$tryserver);
1413: if ($queryid !~/^\Q$host\E\_/) {
1414: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 1415: next;
1.899 raeburn 1416: }
1417: my $reply = &get_query_reply($queryid);
1418: my $maxtries = 1;
1419: my $tries = 1;
1420: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1421: $reply = &get_query_reply($queryid);
1422: $tries ++;
1423: }
1424: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1425: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
1426: } else {
1.911 raeburn 1427: my @matches;
1428: if ($reply =~ /\n/) {
1429: @matches = split(/\n/,$reply);
1430: } else {
1431: @matches = split(/\&/,$reply);
1432: }
1.899 raeburn 1433: foreach my $match (@matches) {
1434: my ($uname,$udom,%userhash);
1.911 raeburn 1435: foreach my $entry (split(/:/,$match)) {
1436: my ($key,$value) =
1437: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 1438: $userhash{$key} = $value;
1439: if ($key eq 'username') {
1440: $uname = $value;
1441: } elsif ($key eq 'domain') {
1442: $udom = $value;
1.911 raeburn 1443: }
1.899 raeburn 1444: }
1445: $results{$uname.':'.$udom} = \%userhash;
1446: }
1447: }
1448: }
1449: }
1450: return %results;
1451: }
1452:
1.912 raeburn 1453: sub get_instuser {
1454: my ($udom,$uname,$id) = @_;
1455: my $homeserver = &domain($udom,'primary');
1456: my ($outcome,%results);
1457: if ($homeserver ne '') {
1458: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
1459: &escape($id).':'.&escape($udom),$homeserver);
1460: my $host=&hostname($homeserver);
1461: if ($queryid !~/^\Q$host\E\_/) {
1462: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1463: return;
1464: }
1465: my $response = &get_query_reply($queryid);
1466: my $maxtries = 5;
1467: my $tries = 1;
1468: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1469: $response = &get_query_reply($queryid);
1470: $tries ++;
1471: }
1472: if (!&error($response) && $response ne 'refused') {
1473: if ($response eq 'unavailable') {
1474: $outcome = $response;
1475: } else {
1476: $outcome = 'ok';
1477: my @matches = split(/\n/,$response);
1478: foreach my $match (@matches) {
1479: my ($key,$value) = split(/=/,$match);
1480: $results{&unescape($key)} = &thaw_unescape($value);
1481: }
1482: }
1483: }
1484: }
1485: my %userinfo;
1486: if (ref($results{$uname}) eq 'HASH') {
1487: %userinfo = %{$results{$uname}};
1488: }
1489: return ($outcome,%userinfo);
1490: }
1491:
1492: sub inst_rulecheck {
1.923 raeburn 1493: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 1494: my %returnhash;
1495: if ($udom ne '') {
1496: if (ref($rules) eq 'ARRAY') {
1497: @{$rules} = map {&escape($_);} (@{$rules});
1498: my $rulestr = join(':',@{$rules});
1499: my $homeserver=&domain($udom,'primary');
1500: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1501: my $response;
1502: if ($item eq 'username') {
1503: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
1504: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 1505: $homeserver));
1.923 raeburn 1506: } elsif ($item eq 'id') {
1507: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
1508: ':'.&escape($id).':'.$rulestr,
1509: $homeserver));
1.945 raeburn 1510: } elsif ($item eq 'selfcreate') {
1511: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 1512: &escape($udom).':'.&escape($uname).
1513: ':'.$rulestr,$homeserver));
1.923 raeburn 1514: }
1.912 raeburn 1515: if ($response ne 'refused') {
1516: my @pairs=split(/\&/,$response);
1517: foreach my $item (@pairs) {
1518: my ($key,$value)=split(/=/,$item,2);
1519: $key = &unescape($key);
1520: next if ($key =~ /^error: 2 /);
1521: $returnhash{$key}=&thaw_unescape($value);
1522: }
1523: }
1524: }
1525: }
1526: }
1527: return %returnhash;
1528: }
1529:
1530: sub inst_userrules {
1.923 raeburn 1531: my ($udom,$check) = @_;
1.912 raeburn 1532: my (%ruleshash,@ruleorder);
1533: if ($udom ne '') {
1534: my $homeserver=&domain($udom,'primary');
1535: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1536: my $response;
1537: if ($check eq 'id') {
1538: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 1539: $homeserver);
1.943 raeburn 1540: } elsif ($check eq 'email') {
1541: $response=&reply('instemailrules:'.&escape($udom),
1542: $homeserver);
1.923 raeburn 1543: } else {
1544: $response=&reply('instuserrules:'.&escape($udom),
1545: $homeserver);
1546: }
1.912 raeburn 1547: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 1548: ($response ne 'unknown_cmd') &&
1.912 raeburn 1549: ($response ne 'no_such_host')) {
1550: my ($hashitems,$orderitems) = split(/:/,$response);
1551: my @pairs=split(/\&/,$hashitems);
1552: foreach my $item (@pairs) {
1553: my ($key,$value)=split(/=/,$item,2);
1554: $key = &unescape($key);
1555: next if ($key =~ /^error: 2 /);
1556: $ruleshash{$key}=&thaw_unescape($value);
1557: }
1558: my @esc_order = split(/\&/,$orderitems);
1559: foreach my $item (@esc_order) {
1560: push(@ruleorder,&unescape($item));
1561: }
1562: }
1563: }
1564: }
1565: return (\%ruleshash,\@ruleorder);
1566: }
1567:
1.976 raeburn 1568: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 1569:
1570: sub get_domain_defaults {
1571: my ($domain) = @_;
1572: my $cachetime = 60*60*24;
1573: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
1574: if (defined($cached)) {
1575: if (ref($result) eq 'HASH') {
1576: return %{$result};
1577: }
1578: }
1579: my %domdefaults;
1580: my %domconfig =
1.989 raeburn 1581: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 1582: 'requestcourses','inststatus',
1.1056.4.3 raeburn 1583: 'coursedefaults','usersessions'],$domain);
1.943 raeburn 1584: if (ref($domconfig{'defaults'}) eq 'HASH') {
1585: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
1586: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
1587: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 1588: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 1589: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1056.4.33.2 1(raebur 1590:2): $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943 raeburn 1591: } else {
1592: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
1593: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
1594: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
1595: }
1.976 raeburn 1596: if (ref($domconfig{'quotas'}) eq 'HASH') {
1597: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
1598: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
1599: } else {
1600: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1601: }
1602: my @usertools = ('aboutme','blog','portfolio');
1603: foreach my $item (@usertools) {
1604: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
1605: $domdefaults{$item} = $domconfig{'quotas'}{$item};
1606: }
1607: }
1608: }
1.985 raeburn 1609: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006 raeburn 1610: foreach my $item ('official','unofficial','community') {
1.985 raeburn 1611: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
1612: }
1613: }
1.989 raeburn 1614: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1615: foreach my $item ('inststatustypes','inststatusorder') {
1616: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
1617: }
1618: }
1.1047 raeburn 1619: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1620: foreach my $item ('canuse_pdfforms') {
1621: $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
1622: }
1623: }
1.1056.4.3 raeburn 1624: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1625: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
1626: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
1627: }
1628: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
1629: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
1630: }
1631: }
1.943 raeburn 1632: &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
1633: $cachetime);
1634: return %domdefaults;
1635: }
1636:
1.344 www 1637: # --------------------------------------------------- Assign a key to a student
1638:
1639: sub assign_access_key {
1.364 www 1640: #
1641: # a valid key looks like uname:udom#comments
1642: # comments are being appended
1643: #
1.498 www 1644: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
1645: $kdom=
1.620 albertel 1646: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 1647: $knum=
1.620 albertel 1648: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 1649: $cdom=
1.620 albertel 1650: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1651: $cnum=
1.620 albertel 1652: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1653: $udom=$env{'user.name'} unless (defined($udom));
1654: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 1655: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 1656: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 1657: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 1658: # assigned to this person
1659: # - this should not happen,
1.345 www 1660: # unless something went wrong
1661: # the first time around
1662: # ready to assign
1.364 www 1663: $logentry=$1.'; '.$logentry;
1.496 www 1664: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 1665: $kdom,$knum) eq 'ok') {
1.345 www 1666: # key now belongs to user
1.346 www 1667: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 1668: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 1669: &appenv({'environment.'.$envkey => $ckey});
1.345 www 1670: return 'ok';
1671: } else {
1672: return
1673: 'error: Count not permanently assign key, will need to be re-entered later.';
1674: }
1675: } else {
1676: return 'error: Could not assign key, try again later.';
1677: }
1.364 www 1678: } elsif (!$existing{$ckey}) {
1.345 www 1679: # the key does not exist
1680: return 'error: The key does not exist';
1681: } else {
1682: # the key is somebody else's
1683: return 'error: The key is already in use';
1684: }
1.344 www 1685: }
1686:
1.364 www 1687: # ------------------------------------------ put an additional comment on a key
1688:
1689: sub comment_access_key {
1690: #
1691: # a valid key looks like uname:udom#comments
1692: # comments are being appended
1693: #
1694: my ($ckey,$cdom,$cnum,$logentry)=@_;
1695: $cdom=
1.620 albertel 1696: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 1697: $cnum=
1.620 albertel 1698: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 1699: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1700: if ($existing{$ckey}) {
1701: $existing{$ckey}.='; '.$logentry;
1702: # ready to assign
1.367 www 1703: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 1704: $cdom,$cnum) eq 'ok') {
1705: return 'ok';
1706: } else {
1707: return 'error: Count not store comment.';
1708: }
1709: } else {
1710: # the key does not exist
1711: return 'error: The key does not exist';
1712: }
1713: }
1714:
1.344 www 1715: # ------------------------------------------------------ Generate a set of keys
1716:
1717: sub generate_access_keys {
1.364 www 1718: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 1719: $cdom=
1.620 albertel 1720: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1721: $cnum=
1.620 albertel 1722: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 1723: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 1724: unless (($cdom) && ($cnum)) { return 0; }
1725: if ($number>10000) { return 0; }
1726: sleep(2); # make sure don't get same seed twice
1727: srand(time()^($$+($$<<15))); # from "Programming Perl"
1728: my $total=0;
1729: for (my $i=1;$i<=$number;$i++) {
1730: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
1731: sprintf("%lx",int(100000*rand)).'-'.
1732: sprintf("%lx",int(100000*rand));
1733: $newkey=~s/1/g/g; # folks mix up 1 and l
1734: $newkey=~s/0/h/g; # and also 0 and O
1735: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
1736: if ($existing{$newkey}) {
1737: $i--;
1738: } else {
1.364 www 1739: if (&put('accesskeys',
1740: { $newkey => '# generated '.localtime().
1.620 albertel 1741: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 1742: '; '.$logentry },
1743: $cdom,$cnum) eq 'ok') {
1.344 www 1744: $total++;
1745: }
1746: }
1747: }
1.620 albertel 1748: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 1749: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
1750: return $total;
1751: }
1752:
1753: # ------------------------------------------------------- Validate an accesskey
1754:
1755: sub validate_access_key {
1756: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
1757: $cdom=
1.620 albertel 1758: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1759: $cnum=
1.620 albertel 1760: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1761: $udom=$env{'user.domain'} unless (defined($udom));
1762: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 1763: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 1764: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 1765: }
1766:
1767: # ------------------------------------- Find the section of student in a course
1.652 albertel 1768: sub devalidate_getsection_cache {
1769: my ($udom,$unam,$courseid)=@_;
1770: my $hashid="$udom:$unam:$courseid";
1771: &devalidate_cache_new('getsection',$hashid);
1772: }
1.298 matthew 1773:
1.815 albertel 1774: sub courseid_to_courseurl {
1775: my ($courseid) = @_;
1776: #already url style courseid
1777: return $courseid if ($courseid =~ m{^/});
1778:
1779: if (exists($env{'course.'.$courseid.'.num'})) {
1780: my $cnum = $env{'course.'.$courseid.'.num'};
1781: my $cdom = $env{'course.'.$courseid.'.domain'};
1782: return "/$cdom/$cnum";
1783: }
1784:
1785: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
1786: if (exists($courseinfo{'num'})) {
1787: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
1788: }
1789:
1790: return undef;
1791: }
1792:
1.298 matthew 1793: sub getsection {
1794: my ($udom,$unam,$courseid)=@_;
1.599 albertel 1795: my $cachetime=1800;
1.551 albertel 1796:
1797: my $hashid="$udom:$unam:$courseid";
1.599 albertel 1798: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 1799: if (defined($cached)) { return $result; }
1800:
1.298 matthew 1801: my %Pending;
1802: my %Expired;
1803: #
1804: # Each role can either have not started yet (pending), be active,
1805: # or have expired.
1806: #
1807: # If there is an active role, we are done.
1808: #
1809: # If there is more than one role which has not started yet,
1810: # choose the one which will start sooner
1811: # If there is one role which has not started yet, return it.
1812: #
1813: # If there is more than one expired role, choose the one which ended last.
1814: # If there is a role which has expired, return it.
1815: #
1.815 albertel 1816: $courseid = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 1817: my $extra = &freeze_escape({'skipcheck' => 1});
1818: my %roleshash = &dump('roles',$udom,$unam,$courseid,undef,$extra);
1.817 raeburn 1819: foreach my $key (keys(%roleshash)) {
1.479 albertel 1820: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1821: my $section=$1;
1822: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1823: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1824: my $now=time;
1.548 albertel 1825: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1826: $Expired{$end}=$section;
1827: next;
1828: }
1.548 albertel 1829: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1830: $Pending{$start}=$section;
1831: next;
1832: }
1.599 albertel 1833: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1834: }
1835: #
1836: # Presumedly there will be few matching roles from the above
1837: # loop and the sorting time will be negligible.
1838: if (scalar(keys(%Pending))) {
1839: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1840: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1841: }
1842: if (scalar(keys(%Expired))) {
1843: my @sorted = sort {$a <=> $b} keys(%Expired);
1844: my $time = pop(@sorted);
1.599 albertel 1845: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1846: }
1.599 albertel 1847: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1848: }
1.70 www 1849:
1.599 albertel 1850: sub save_cache {
1851: &purge_remembered();
1.722 albertel 1852: #&Apache::loncommon::validate_page();
1.620 albertel 1853: undef(%env);
1.780 albertel 1854: undef($env_loaded);
1.599 albertel 1855: }
1.452 albertel 1856:
1.599 albertel 1857: my $to_remember=-1;
1858: my %remembered;
1859: my %accessed;
1860: my $kicks=0;
1861: my $hits=0;
1.849 albertel 1862: sub make_key {
1863: my ($name,$id) = @_;
1.872 albertel 1864: if (length($id) > 65
1865: && length(&escape($id)) > 200) {
1866: $id=length($id).':'.&Digest::MD5::md5_hex($id);
1867: }
1.849 albertel 1868: return &escape($name.':'.$id);
1869: }
1870:
1.599 albertel 1871: sub devalidate_cache_new {
1872: my ($name,$id,$debug) = @_;
1873: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1874: $id=&make_key($name,$id);
1.599 albertel 1875: $memcache->delete($id);
1876: delete($remembered{$id});
1877: delete($accessed{$id});
1878: }
1879:
1880: sub is_cached_new {
1881: my ($name,$id,$debug) = @_;
1.849 albertel 1882: $id=&make_key($name,$id);
1.599 albertel 1883: if (exists($remembered{$id})) {
1884: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1885: $accessed{$id}=[&gettimeofday()];
1886: $hits++;
1887: return ($remembered{$id},1);
1888: }
1889: my $value = $memcache->get($id);
1890: if (!(defined($value))) {
1891: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1892: return (undef,undef);
1.416 albertel 1893: }
1.599 albertel 1894: if ($value eq '__undef__') {
1895: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1896: $value=undef;
1897: }
1898: &make_room($id,$value,$debug);
1899: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1900: return ($value,1);
1901: }
1902:
1903: sub do_cache_new {
1904: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1905: $id=&make_key($name,$id);
1.599 albertel 1906: my $setvalue=$value;
1907: if (!defined($setvalue)) {
1908: $setvalue='__undef__';
1909: }
1.623 albertel 1910: if (!defined($time) ) {
1911: $time=600;
1912: }
1.599 albertel 1913: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 1914: my $result = $memcache->set($id,$setvalue,$time);
1915: if (! $result) {
1.872 albertel 1916: &logthis("caching of id -> $id failed");
1.910 albertel 1917: $memcache->disconnect_all();
1.872 albertel 1918: }
1.600 albertel 1919: # need to make a copy of $value
1.919 albertel 1920: &make_room($id,$value,$debug);
1.599 albertel 1921: return $value;
1922: }
1923:
1924: sub make_room {
1925: my ($id,$value,$debug)=@_;
1.919 albertel 1926:
1927: $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
1928: : $value;
1.599 albertel 1929: if ($to_remember<0) { return; }
1930: $accessed{$id}=[&gettimeofday()];
1931: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1932: my $to_kick;
1933: my $max_time=0;
1934: foreach my $other (keys(%accessed)) {
1935: if (&tv_interval($accessed{$other}) > $max_time) {
1936: $to_kick=$other;
1937: $max_time=&tv_interval($accessed{$other});
1938: }
1939: }
1940: delete($remembered{$to_kick});
1941: delete($accessed{$to_kick});
1942: $kicks++;
1943: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1944: return;
1945: }
1946:
1.599 albertel 1947: sub purge_remembered {
1.604 albertel 1948: #&logthis("Tossing ".scalar(keys(%remembered)));
1949: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1950: undef(%remembered);
1951: undef(%accessed);
1.428 albertel 1952: }
1.70 www 1953: # ------------------------------------- Read an entry from a user's environment
1954:
1955: sub userenvironment {
1956: my ($udom,$unam,@what)=@_;
1.976 raeburn 1957: my $items;
1958: foreach my $item (@what) {
1959: $items.=&escape($item).'&';
1960: }
1961: $items=~s/\&$//;
1.70 www 1962: my %returnhash=();
1.1009 raeburn 1963: my $uhome = &homeserver($unam,$udom);
1964: unless ($uhome eq 'no_host') {
1965: my @answer=split(/\&/,
1966: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 1967: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
1968: return %returnhash;
1969: }
1.1009 raeburn 1970: my $i;
1971: for ($i=0;$i<=$#what;$i++) {
1972: $returnhash{$what[$i]}=&unescape($answer[$i]);
1973: }
1.70 www 1974: }
1975: return %returnhash;
1.1 albertel 1976: }
1977:
1.617 albertel 1978: # ---------------------------------------------------------- Get a studentphoto
1979: sub studentphoto {
1980: my ($udom,$unam,$ext) = @_;
1981: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 1982: if (defined($env{'request.course.id'})) {
1.708 raeburn 1983: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 1984: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1985: return(&retrievestudentphoto($udom,$unam,$ext));
1986: } else {
1987: my ($result,$perm_reqd)=
1.707 albertel 1988: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1989: if ($result eq 'ok') {
1990: if (!($perm_reqd eq 'yes')) {
1991: return(&retrievestudentphoto($udom,$unam,$ext));
1992: }
1993: }
1994: }
1995: }
1996: } else {
1997: my ($result,$perm_reqd) =
1.707 albertel 1998: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1999: if ($result eq 'ok') {
2000: if (!($perm_reqd eq 'yes')) {
2001: return(&retrievestudentphoto($udom,$unam,$ext));
2002: }
2003: }
2004: }
2005: return '/adm/lonKaputt/lonlogo_broken.gif';
2006: }
2007:
2008: sub retrievestudentphoto {
2009: my ($udom,$unam,$ext,$type) = @_;
2010: my $home=&Apache::lonnet::homeserver($unam,$udom);
2011: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2012: if ($ret eq 'ok') {
2013: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2014: if ($type eq 'thumbnail') {
2015: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2016: }
2017: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2018: return $tokenurl;
2019: } else {
2020: if ($type eq 'thumbnail') {
2021: return '/adm/lonKaputt/genericstudent_tn.gif';
2022: } else {
2023: return '/adm/lonKaputt/lonlogo_broken.gif';
2024: }
1.617 albertel 2025: }
2026: }
2027:
1.263 www 2028: # -------------------------------------------------------------------- New chat
2029:
2030: sub chatsend {
1.724 raeburn 2031: my ($newentry,$anon,$group)=@_;
1.620 albertel 2032: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2033: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2034: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 2035: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 2036: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 2037: &escape($newentry)).':'.$group,$chome);
1.292 www 2038: }
2039:
2040: # ------------------------------------------ Find current version of a resource
2041:
2042: sub getversion {
2043: my $fname=&clutter(shift);
2044: unless ($fname=~/^\/res\//) { return -1; }
2045: return ¤tversion(&filelocation('',$fname));
2046: }
2047:
2048: sub currentversion {
2049: my $fname=shift;
1.599 albertel 2050: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 2051: if (defined($cached)) { return $result; }
1.292 www 2052: my $author=$fname;
2053: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2054: my ($udom,$uname)=split(/\//,$author);
2055: my $home=homeserver($uname,$udom);
2056: if ($home eq 'no_host') {
2057: return -1;
2058: }
2059: my $answer=reply("currentversion:$fname",$home);
2060: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2061: return -1;
2062: }
1.599 albertel 2063: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 2064: }
2065:
1.1 albertel 2066: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 2067:
1.1 albertel 2068: sub subscribe {
2069: my $fname=shift;
1.761 raeburn 2070: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 2071: $fname=~s/[\n\r]//g;
1.1 albertel 2072: my $author=$fname;
2073: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2074: my ($udom,$uname)=split(/\//,$author);
2075: my $home=homeserver($uname,$udom);
1.335 albertel 2076: if ($home eq 'no_host') {
2077: return 'not_found';
1.1 albertel 2078: }
2079: my $answer=reply("sub:$fname",$home);
1.64 www 2080: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2081: $answer.=' by '.$home;
2082: }
1.1 albertel 2083: return $answer;
2084: }
2085:
1.8 www 2086: # -------------------------------------------------------------- Replicate file
2087:
2088: sub repcopy {
2089: my $filename=shift;
1.23 www 2090: $filename=~s/\/+/\//g;
1.607 raeburn 2091: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
2092: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 2093: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 2094: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 2095: return &repcopy_userfile($filename);
2096: }
1.532 albertel 2097: $filename=~s/[\n\r]//g;
1.8 www 2098: my $transname="$filename.in.transfer";
1.828 www 2099: # FIXME: this should flock
1.607 raeburn 2100: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 2101: my $remoteurl=subscribe($filename);
1.64 www 2102: if ($remoteurl =~ /^con_lost by/) {
2103: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2104: return 'unavailable';
1.8 www 2105: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 2106: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 2107: return 'not_found';
1.64 www 2108: } elsif ($remoteurl =~ /^rejected by/) {
2109: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2110: return 'forbidden';
1.20 www 2111: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 2112: return 'ok';
1.8 www 2113: } else {
1.290 www 2114: my $author=$filename;
2115: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2116: my ($udom,$uname)=split(/\//,$author);
2117: my $home=homeserver($uname,$udom);
2118: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 2119: my @parts=split(/\//,$filename);
2120: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
2121: if ($path ne "$perlvar{'lonDocRoot'}/res") {
2122: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 2123: return 'bad_request';
1.8 www 2124: }
2125: my $count;
2126: for ($count=5;$count<$#parts;$count++) {
2127: $path.="/$parts[$count]";
2128: if ((-e $path)!=1) {
2129: mkdir($path,0777);
2130: }
2131: }
2132: my $ua=new LWP::UserAgent;
2133: my $request=new HTTP::Request('GET',"$remoteurl");
2134: my $response=$ua->request($request,$transname);
2135: if ($response->is_error()) {
2136: unlink($transname);
2137: my $message=$response->status_line;
1.672 albertel 2138: &logthis("<font color=\"blue\">WARNING:"
1.12 www 2139: ." LWP get: $message: $filename</font>");
1.607 raeburn 2140: return 'unavailable';
1.8 www 2141: } else {
1.16 www 2142: if ($remoteurl!~/\.meta$/) {
2143: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2144: my $mresponse=$ua->request($mrequest,$filename.'.meta');
2145: if ($mresponse->is_error()) {
2146: unlink($filename.'.meta');
2147: &logthis(
1.672 albertel 2148: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 2149: }
2150: }
1.8 www 2151: rename($transname,$filename);
1.607 raeburn 2152: return 'ok';
1.8 www 2153: }
1.290 www 2154: }
1.8 www 2155: }
1.330 www 2156: }
2157:
2158: # ------------------------------------------------ Get server side include body
2159: sub ssi_body {
1.381 albertel 2160: my ($filelink,%form)=@_;
1.606 matthew 2161: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
2162: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
2163: }
1.953 www 2164: my $output='';
2165: my $response;
1.980 raeburn 2166: if ($filelink=~/^https?\:/) {
1.954 raeburn 2167: ($output,$response)=&externalssi($filelink);
1.953 www 2168: } else {
1.1004 droeschl 2169: $filelink .= $filelink=~/\?/ ? '&' : '?';
2170: $filelink .= 'inhibitmenu=yes';
1.953 www 2171: ($output,$response)=&ssi($filelink,%form);
2172: }
1.778 albertel 2173: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2174: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2175: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2176: if (wantarray) {
2177: return ($output, $response);
2178: } else {
2179: return $output;
2180: }
1.8 www 2181: }
2182:
1.15 www 2183: # --------------------------------------------------------- Server Side Include
2184:
1.782 albertel 2185: sub absolute_url {
2186: my ($host_name) = @_;
2187: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
2188: if ($host_name eq '') {
2189: $host_name = $ENV{'SERVER_NAME'};
2190: }
2191: return $protocol.$host_name;
2192: }
2193:
1.942 foxr 2194: #
2195: # Server side include.
2196: # Parameters:
2197: # fn Possibly encrypted resource name/id.
2198: # form Hash that describes how the rendering should be done
2199: # and other things.
1.944 foxr 2200: # Returns:
1.950 raeburn 2201: # Scalar context: The content of the response.
2202: # Array context: 2 element list of the content and the full response object.
1.942 foxr 2203: #
1.15 www 2204: sub ssi {
2205:
1.944 foxr 2206: my ($fn,%form)=@_;
1.15 www 2207: my $ua=new LWP::UserAgent;
1.23 www 2208: my $request;
1.711 albertel 2209:
2210: $form{'no_update_last_known'}=1;
1.895 albertel 2211: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 2212: if (%form) {
1.782 albertel 2213: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000 raeburn 2214: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23 www 2215: } else {
1.782 albertel 2216: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 2217: }
2218:
1.15 www 2219: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
2220: my $response=$ua->request($request);
2221:
1.944 foxr 2222: if (wantarray) {
2223: return ($response->content, $response);
2224: } else {
2225: return $response->content;
1.942 foxr 2226: }
1.324 www 2227: }
2228:
2229: sub externalssi {
2230: my ($url)=@_;
2231: my $ua=new LWP::UserAgent;
2232: my $request=new HTTP::Request('GET',$url);
2233: my $response=$ua->request($request);
1.954 raeburn 2234: if (wantarray) {
2235: return ($response->content, $response);
2236: } else {
2237: return $response->content;
2238: }
1.15 www 2239: }
1.254 www 2240:
1.492 albertel 2241: # -------------------------------- Allow a /uploaded/ URI to be vouched for
2242:
2243: sub allowuploaded {
2244: my ($srcurl,$url)=@_;
2245: $url=&clutter(&declutter($url));
2246: my $dir=$url;
2247: $dir=~s/\/[^\/]+$//;
2248: my %httpref=();
2249: my $httpurl=&hreflocation('',$url);
2250: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 2251: &Apache::lonnet::appenv(\%httpref);
1.254 www 2252: }
1.477 raeburn 2253:
1.478 albertel 2254: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 2255: # input: action, courseID, current domain, intended
1.637 raeburn 2256: # path to file, source of file, instruction to parse file for objects,
2257: # ref to hash for embedded objects,
2258: # ref to hash for codebase of java objects.
1.1056.4.16 raeburn 2259: # reference to scalar to accommodate mime type determined
2260: # from File::MMagic if $parser = parse.
1.637 raeburn 2261: #
1.485 raeburn 2262: # output: url to file (if action was uploaddoc),
2263: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 2264: #
1.478 albertel 2265: # Allows directory structure to be used within lonUsers/../userfiles/ for a
2266: # course.
1.477 raeburn 2267: #
1.478 albertel 2268: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2269: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
2270: # course's home server.
1.477 raeburn 2271: #
1.478 albertel 2272: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
2273: # be copied from $source (current location) to
2274: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2275: # and will then be copied to
2276: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
2277: # course's home server.
1.485 raeburn 2278: #
1.481 raeburn 2279: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 2280: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 2281: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2282: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
2283: # in course's home server.
1.637 raeburn 2284: #
1.477 raeburn 2285:
2286: sub process_coursefile {
1.1056.4.16 raeburn 2287: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
2288: $mimetype)=@_;
1.477 raeburn 2289: my $fetchresult;
1.638 albertel 2290: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 2291: if ($action eq 'propagate') {
1.638 albertel 2292: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
2293: $home);
1.481 raeburn 2294: } else {
1.477 raeburn 2295: my $fpath = '';
2296: my $fname = $file;
1.478 albertel 2297: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 2298: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 2299: my $filepath = &build_filepath($fpath);
1.481 raeburn 2300: if ($action eq 'copy') {
2301: if ($source eq '') {
2302: $fetchresult = 'no source file';
2303: return $fetchresult;
2304: } else {
2305: my $destination = $filepath.'/'.$fname;
2306: rename($source,$destination);
2307: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2308: $home);
1.481 raeburn 2309: }
2310: } elsif ($action eq 'uploaddoc') {
2311: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 2312: print $fh $env{'form.'.$source};
1.481 raeburn 2313: close($fh);
1.637 raeburn 2314: if ($parser eq 'parse') {
1.1024 raeburn 2315: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2316: my $type = $mm->checktype_filename($filepath.'/'.$fname);
2317: if ($type eq 'text/html') {
1.1024 raeburn 2318: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
2319: unless ($parse_result eq 'ok') {
2320: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
2321: }
1.637 raeburn 2322: }
1.1056.4.16 raeburn 2323: if (ref($mimetype)) {
2324: $$mimetype = $type;
2325: }
1.637 raeburn 2326: }
1.477 raeburn 2327: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2328: $home);
1.481 raeburn 2329: if ($fetchresult eq 'ok') {
2330: return '/uploaded/'.$fpath.'/'.$fname;
2331: } else {
2332: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2333: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 2334: return '/adm/notfound.html';
2335: }
1.477 raeburn 2336: }
2337: }
1.485 raeburn 2338: unless ( $fetchresult eq 'ok') {
1.477 raeburn 2339: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2340: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 2341: }
2342: return $fetchresult;
2343: }
2344:
1.637 raeburn 2345: sub build_filepath {
2346: my ($fpath) = @_;
2347: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
2348: unless ($fpath eq '') {
2349: my @parts=split('/',$fpath);
2350: foreach my $part (@parts) {
2351: $filepath.= '/'.$part;
2352: if ((-e $filepath)!=1) {
2353: mkdir($filepath,0777);
2354: }
2355: }
2356: }
2357: return $filepath;
2358: }
2359:
2360: sub store_edited_file {
1.638 albertel 2361: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 2362: my $file = $primary_url;
2363: $file =~ s#^/uploaded/$docudom/$docuname/##;
2364: my $fpath = '';
2365: my $fname = $file;
2366: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
2367: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
2368: my $filepath = &build_filepath($fpath);
2369: open(my $fh,'>'.$filepath.'/'.$fname);
2370: print $fh $content;
2371: close($fh);
1.638 albertel 2372: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 2373: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2374: $home);
1.637 raeburn 2375: if ($$fetchresult eq 'ok') {
2376: return '/uploaded/'.$fpath.'/'.$fname;
2377: } else {
1.638 albertel 2378: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
2379: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 2380: return '/adm/notfound.html';
2381: }
2382: }
2383:
1.531 albertel 2384: sub clean_filename {
1.831 albertel 2385: my ($fname,$args)=@_;
1.315 www 2386: # Replace Windows backslashes by forward slashes
1.257 www 2387: $fname=~s/\\/\//g;
1.831 albertel 2388: if (!$args->{'keep_path'}) {
2389: # Get rid of everything but the actual filename
2390: $fname=~s/^.*\/([^\/]+)$/$1/;
2391: }
1.315 www 2392: # Replace spaces by underscores
2393: $fname=~s/\s+/\_/g;
2394: # Replace all other weird characters by nothing
1.831 albertel 2395: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 2396: # Replace all .\d. sequences with _\d. so they no longer look like version
2397: # numbers
2398: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 2399: return $fname;
2400: }
1.1051 raeburn 2401: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
2402: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
2403: # image with the same aspect ratio as the original, but with dimensions which do
2404: # not exceed $resizewidth and $resizeheight.
2405:
1.984 neumanie 2406: sub resizeImage {
1.1051 raeburn 2407: my ($img_path,$resizewidth,$resizeheight) = @_;
2408: my $ima = Image::Magick->new;
2409: my $resized;
2410: if (-e $img_path) {
2411: $ima->Read($img_path);
2412: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
2413: my $width = $ima->Get('width');
2414: my $height = $ima->Get('height');
2415: if ($width > $resizewidth) {
2416: my $factor = $width/$resizewidth;
2417: my $newheight = $height/$factor;
2418: $ima->Scale(width=>$resizewidth,height=>$newheight);
2419: $resized = 1;
2420: }
2421: }
2422: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
2423: my $width = $ima->Get('width');
2424: my $height = $ima->Get('height');
2425: if ($height > $resizeheight) {
2426: my $factor = $height/$resizeheight;
2427: my $newwidth = $width/$factor;
2428: $ima->Scale(width=>$newwidth,height=>$resizeheight);
2429: $resized = 1;
2430: }
2431: }
2432: if ($resized) {
2433: $ima->Write($img_path);
2434: }
2435: }
2436: return;
1.977 amueller 2437: }
2438:
1.608 albertel 2439: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 2440: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1056.4.13 raeburn 2441: # the desired filename is in $env{"form.$formname.filename"}
2442: # $context - possible values: coursedoc, existingfile, overwrite,
2443: # canceloverwrite, or ''.
2444: # if 'coursedoc': upload to the current course
2445: # if 'existingfile': write file to tmp/overwrites directory
2446: # if 'canceloverwrite': delete file written to tmp/overwrites directory
2447: # $context is passed as argument to &finishuserfileupload
1.686 albertel 2448: # $subdir - directory in userfile to store the file into
1.858 raeburn 2449: # $parser - instruction to parse file for objects ($parser = parse)
2450: # $allfiles - reference to hash for embedded objects
2451: # $codebase - reference to hash for codebase of java objects
2452: # $desuname - username for permanent storage of uploaded file
2453: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 2454: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
2455: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 2456: # $resizewidth - width (pixels) to which to resize uploaded image
2457: # $resizeheight - height (pixels) to which to resize uploaded image
1.1056.4.16 raeburn 2458: # $mimetype - reference to scalar to accommodate mime type determined
2459: # from File::MMagic if $parser = parse.
1.858 raeburn 2460: #
1.686 albertel 2461: # output: url of file in userspace, or error: <message>
2462: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 2463:
1.531 albertel 2464: sub userfileupload {
1.1056.4.13 raeburn 2465: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1056.4.16 raeburn 2466: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 2467: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 2468: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 2469: $fname=&clean_filename($fname);
1.1056.4.13 raeburn 2470: # See if there is anything left
1.257 www 2471: unless ($fname) { return 'error: no uploaded file'; }
1.1056.4.13 raeburn 2472: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
2473: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
2474: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
2475: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 2476: my $now = time;
1.1056.4.13 raeburn 2477: my $filepath;
2478: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
2479: $filepath = 'tmp/helprequests/'.$now;
2480: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
2481: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
2482: '_'.$env{'user.domain'}.'/pending';
2483: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
2484: my ($docuname,$docudom);
2485: if ($destudom) {
2486: $docudom = $destudom;
2487: } else {
2488: $docudom = $env{'user.domain'};
2489: }
2490: if ($destuname) {
2491: $docuname = $destuname;
2492: } else {
2493: $docuname = $env{'user.name'};
2494: }
2495: if (exists($env{'form.group'})) {
2496: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2497: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2498: }
2499: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
2500: if ($context eq 'canceloverwrite') {
2501: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
2502: if (-e $tempfile) {
2503: my @info = stat($tempfile);
2504: if ($info[9] eq $env{'form.timestamp'}) {
2505: unlink($tempfile);
2506: }
2507: }
2508: return;
1.523 raeburn 2509: }
2510: }
1.1056.4.13 raeburn 2511: # Create the directory if not present
1.741 raeburn 2512: my @parts=split(/\//,$filepath);
2513: my $fullpath = $perlvar{'lonDaemons'};
2514: for (my $i=0;$i<@parts;$i++) {
2515: $fullpath .= '/'.$parts[$i];
2516: if ((-e $fullpath)!=1) {
2517: mkdir($fullpath,0777);
2518: }
2519: }
2520: open(my $fh,'>'.$fullpath.'/'.$fname);
2521: print $fh $env{'form.'.$formname};
2522: close($fh);
1.1056.4.13 raeburn 2523: if ($context eq 'existingfile') {
2524: my @info = stat($fullpath.'/'.$fname);
2525: return ($fullpath.'/'.$fname,$info[9]);
2526: } else {
2527: return $fullpath.'/'.$fname;
2528: }
1.523 raeburn 2529: }
1.995 raeburn 2530: if ($subdir eq 'scantron') {
2531: $fname = 'scantron_orig_'.$fname;
1.1056.4.14 raeburn 2532: } else {
1.995 raeburn 2533: $fname="$subdir/$fname";
2534: }
1.1056.4.13 raeburn 2535: if ($context eq 'coursedoc') {
1.638 albertel 2536: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2537: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 2538: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 2539: return &finishuserfileupload($docuname,$docudom,
2540: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 2541: $codebase,$thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2542: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 2543: } else {
1.620 albertel 2544: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 2545: return &process_coursefile('uploaddoc',$docuname,$docudom,
2546: $fname,$formname,$parser,
1.1056.4.16 raeburn 2547: $allfiles,$codebase,$mimetype);
1.481 raeburn 2548: }
1.719 banghart 2549: } elsif (defined($destuname)) {
2550: my $docuname=$destuname;
2551: my $docudom=$destudom;
1.860 raeburn 2552: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2553: $parser,$allfiles,$codebase,
1.1051 raeburn 2554: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2555: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2556: } else {
1.638 albertel 2557: my $docuname=$env{'user.name'};
2558: my $docudom=$env{'user.domain'};
1.714 raeburn 2559: if (exists($env{'form.group'})) {
2560: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2561: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2562: }
1.860 raeburn 2563: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2564: $parser,$allfiles,$codebase,
1.1051 raeburn 2565: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2566: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2567: }
1.271 www 2568: }
2569:
2570: sub finishuserfileupload {
1.860 raeburn 2571: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1056.4.16 raeburn 2572: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 2573: my $path=$docudom.'/'.$docuname.'/';
1.258 www 2574: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 2575:
1.860 raeburn 2576: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 2577: $file=$fname;
2578: if ($fname=~m|/|) {
2579: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
2580: $path.=$fnamepath.'/';
2581: }
1.259 www 2582: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 2583: my $count;
2584: for ($count=4;$count<=$#parts;$count++) {
2585: $filepath.="/$parts[$count]";
2586: if ((-e $filepath)!=1) {
2587: mkdir($filepath,0777);
2588: }
2589: }
1.984 neumanie 2590:
1.258 www 2591: # Save the file
2592: {
1.701 albertel 2593: if (!open(FH,'>'.$filepath.'/'.$file)) {
2594: &logthis('Failed to create '.$filepath.'/'.$file);
2595: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
2596: return '/adm/notfound.html';
2597: }
1.1056.4.13 raeburn 2598: if ($context eq 'overwrite') {
2599: my $source = $perlvar{'lonDaemons'}.'/tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
2600: my $target = $filepath.'/'.$file;
2601: if (-e $source) {
2602: my @info = stat($source);
2603: if ($info[9] eq $env{'form.timestamp'}) {
2604: unless (&File::Copy::move($source,$target)) {
2605: &logthis('Failed to overwrite '.$filepath.'/'.$file);
2606: return "Moving from $source failed";
2607: }
2608: } else {
2609: return "Temporary file: $source had unexpected date/time for last modification";
2610: }
2611: } else {
2612: return "Temporary file: $source missing";
2613: }
2614: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 2615: &logthis('Failed to write to '.$filepath.'/'.$file);
2616: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
2617: return '/adm/notfound.html';
2618: }
1.570 albertel 2619: close(FH);
1.1051 raeburn 2620: if ($resizewidth && $resizeheight) {
2621: my $mm = new File::MMagic;
2622: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
2623: if ($mime_type =~ m{^image/}) {
2624: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
2625: }
1.977 amueller 2626: }
1.258 www 2627: }
1.637 raeburn 2628: if ($parser eq 'parse') {
1.1024 raeburn 2629: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2630: my $type = $mm->checktype_filename($filepath.'/'.$file);
2631: if ($type eq 'text/html') {
1.1024 raeburn 2632: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
2633: $allfiles,$codebase);
2634: unless ($parse_result eq 'ok') {
2635: &logthis('Failed to parse '.$filepath.$file.
2636: ' for embedded media: '.$parse_result);
2637: }
1.637 raeburn 2638: }
1.1056.4.16 raeburn 2639: if (ref($mimetype)) {
2640: $$mimetype = $type;
2641: }
1.637 raeburn 2642: }
1.860 raeburn 2643: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
2644: my $input = $filepath.'/'.$file;
2645: my $output = $filepath.'/'.'tn-'.$file;
2646: my $thumbsize = $thumbwidth.'x'.$thumbheight;
2647: system("convert -sample $thumbsize $input $output");
2648: if (-e $filepath.'/'.'tn-'.$file) {
2649: $fetchthumb = 1;
2650: }
2651: }
1.858 raeburn 2652:
1.259 www 2653: # Notify homeserver to grep it
2654: #
1.984 neumanie 2655: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 2656: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 2657: if ($fetchresult eq 'ok') {
1.860 raeburn 2658: if ($fetchthumb) {
2659: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
2660: if ($thumbresult ne 'ok') {
2661: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
2662: $docuhome.': '.$thumbresult);
2663: }
2664: }
1.259 www 2665: #
1.258 www 2666: # Return the URL to it
1.494 albertel 2667: return '/uploaded/'.$path.$file;
1.263 www 2668: } else {
1.494 albertel 2669: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
2670: ': '.$fetchresult);
1.263 www 2671: return '/adm/notfound.html';
1.858 raeburn 2672: }
1.493 albertel 2673: }
2674:
1.637 raeburn 2675: sub extract_embedded_items {
1.961 raeburn 2676: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 2677: my @state = ();
2678: my %javafiles = (
2679: codebase => '',
2680: code => '',
2681: archive => ''
2682: );
2683: my %mediafiles = (
2684: src => '',
2685: movie => '',
2686: );
1.648 raeburn 2687: my $p;
2688: if ($content) {
2689: $p = HTML::LCParser->new($content);
2690: } else {
1.961 raeburn 2691: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 2692: }
1.641 albertel 2693: while (my $t=$p->get_token()) {
1.640 albertel 2694: if ($t->[0] eq 'S') {
2695: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 2696: push(@state, $tagname);
1.648 raeburn 2697: if (lc($tagname) eq 'allow') {
2698: &add_filetype($allfiles,$attr->{'src'},'src');
2699: }
1.640 albertel 2700: if (lc($tagname) eq 'img') {
2701: &add_filetype($allfiles,$attr->{'src'},'src');
2702: }
1.886 albertel 2703: if (lc($tagname) eq 'a') {
2704: &add_filetype($allfiles,$attr->{'href'},'href');
2705: }
1.645 raeburn 2706: if (lc($tagname) eq 'script') {
2707: if ($attr->{'archive'} =~ /\.jar$/i) {
2708: &add_filetype($allfiles,$attr->{'archive'},'archive');
2709: } else {
2710: &add_filetype($allfiles,$attr->{'src'},'src');
2711: }
2712: }
2713: if (lc($tagname) eq 'link') {
2714: if (lc($attr->{'rel'}) eq 'stylesheet') {
2715: &add_filetype($allfiles,$attr->{'href'},'href');
2716: }
2717: }
1.640 albertel 2718: if (lc($tagname) eq 'object' ||
2719: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
2720: foreach my $item (keys(%javafiles)) {
2721: $javafiles{$item} = '';
2722: }
2723: }
2724: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
2725: my $name = lc($attr->{'name'});
2726: foreach my $item (keys(%javafiles)) {
2727: if ($name eq $item) {
2728: $javafiles{$item} = $attr->{'value'};
2729: last;
2730: }
2731: }
2732: foreach my $item (keys(%mediafiles)) {
2733: if ($name eq $item) {
2734: &add_filetype($allfiles, $attr->{'value'}, 'value');
2735: last;
2736: }
2737: }
2738: }
2739: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
2740: foreach my $item (keys(%javafiles)) {
2741: if ($attr->{$item}) {
2742: $javafiles{$item} = $attr->{$item};
2743: last;
2744: }
2745: }
2746: foreach my $item (keys(%mediafiles)) {
2747: if ($attr->{$item}) {
2748: &add_filetype($allfiles,$attr->{$item},$item);
2749: last;
2750: }
2751: }
2752: }
2753: } elsif ($t->[0] eq 'E') {
2754: my ($tagname) = ($t->[1]);
2755: if ($javafiles{'codebase'} ne '') {
2756: $javafiles{'codebase'} .= '/';
2757: }
2758: if (lc($tagname) eq 'applet' ||
2759: lc($tagname) eq 'object' ||
2760: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
2761: ) {
2762: foreach my $item (keys(%javafiles)) {
2763: if ($item ne 'codebase' && $javafiles{$item} ne '') {
2764: my $file=$javafiles{'codebase'}.$javafiles{$item};
2765: &add_filetype($allfiles,$file,$item);
2766: }
2767: }
2768: }
2769: pop @state;
2770: }
2771: }
1.637 raeburn 2772: return 'ok';
2773: }
2774:
1.639 albertel 2775: sub add_filetype {
2776: my ($allfiles,$file,$type)=@_;
2777: if (exists($allfiles->{$file})) {
2778: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
2779: push(@{$allfiles->{$file}}, &escape($type));
2780: }
2781: } else {
2782: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 2783: }
2784: }
2785:
1.493 albertel 2786: sub removeuploadedurl {
1.984 neumanie 2787: my ($url)=@_;
2788: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 2789: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 2790: }
2791:
2792: sub removeuserfile {
2793: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 2794: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2795: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 2796: if ($result eq 'ok') {
1.798 raeburn 2797: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
2798: my $metafile = $fname.'.meta';
2799: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 2800: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 2801: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2802: my $sqlresult =
1.823 albertel 2803: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2804: 'portfolio_metadata',$group,
2805: 'delete');
1.798 raeburn 2806: }
2807: }
2808: return $result;
1.257 www 2809: }
1.15 www 2810:
1.530 albertel 2811: sub mkdiruserfile {
2812: my ($docuname,$docudom,$dir)=@_;
2813: my $home=&homeserver($docuname,$docudom);
2814: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
2815: }
2816:
1.531 albertel 2817: sub renameuserfile {
2818: my ($docuname,$docudom,$old,$new)=@_;
2819: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2820: my $result = &reply("renameuserfile:$docudom:$docuname:".
2821: &escape("$old").':'.&escape("$new"),$home);
2822: if ($result eq 'ok') {
2823: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
2824: my $oldmeta = $old.'.meta';
2825: my $newmeta = $new.'.meta';
2826: my $metaresult =
2827: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 2828: my $url = "/uploaded/$docudom/$docuname/$old";
2829: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2830: my $sqlresult =
1.823 albertel 2831: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2832: 'portfolio_metadata',$group,
2833: 'delete');
1.798 raeburn 2834: }
2835: }
2836: return $result;
1.531 albertel 2837: }
2838:
1.14 www 2839: # ------------------------------------------------------------------------- Log
2840:
2841: sub log {
2842: my ($dom,$nam,$hom,$what)=@_;
1.47 www 2843: return critical("log:$dom:$nam:$what",$hom);
1.157 www 2844: }
2845:
2846: # ------------------------------------------------------------------ Course Log
1.352 www 2847: #
2848: # This routine flushes several buffers of non-mission-critical nature
2849: #
1.157 www 2850:
2851: sub flushcourselogs {
1.352 www 2852: &logthis('Flushing log buffers');
2853: #
2854: # course logs
2855: # This is a log of all transactions in a course, which can be used
2856: # for data mining purposes
2857: #
2858: # It also collects the courseid database, which lists last transaction
2859: # times and course titles for all courseids
2860: #
2861: my %courseidbuffer=();
1.921 raeburn 2862: foreach my $crsid (keys(%courselogs)) {
1.352 www 2863: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 2864: &escape($courselogs{$crsid}),
2865: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 2866: delete $courselogs{$crsid};
2867: } else {
2868: &logthis('Failed to flush log buffer for '.$crsid);
2869: if (length($courselogs{$crsid})>40000) {
1.672 albertel 2870: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 2871: " exceeded maximum size, deleting.</font>");
2872: delete $courselogs{$crsid};
2873: }
1.352 www 2874: }
1.920 raeburn 2875: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 2876: 'description' => $coursedescrbuf{$crsid},
2877: 'inst_code' => $courseinstcodebuf{$crsid},
2878: 'type' => $coursetypebuf{$crsid},
2879: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 2880: };
1.191 harris41 2881: }
1.352 www 2882: #
2883: # Write course id database (reverse lookup) to homeserver of courses
2884: # Is used in pickcourse
2885: #
1.840 albertel 2886: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 2887: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 2888: $courseidbuffer{$crs_home},
2889: $crs_home,'timeonly');
1.352 www 2890: }
2891: #
2892: # File accesses
2893: # Writes to the dynamic metadata of resources to get hit counts, etc.
2894: #
1.449 matthew 2895: foreach my $entry (keys(%accesshash)) {
1.458 matthew 2896: if ($entry =~ /___count$/) {
2897: my ($dom,$name);
1.807 albertel 2898: ($dom,$name,undef)=
1.811 albertel 2899: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 2900: if (! defined($dom) || $dom eq '' ||
2901: ! defined($name) || $name eq '') {
1.620 albertel 2902: my $cid = $env{'request.course.id'};
2903: $dom = $env{'request.'.$cid.'.domain'};
2904: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 2905: }
1.450 matthew 2906: my $value = $accesshash{$entry};
2907: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
2908: my %temphash=($url => $value);
1.449 matthew 2909: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
2910: if ($result eq 'ok') {
2911: delete $accesshash{$entry};
2912: } elsif ($result eq 'unknown_cmd') {
2913: # Target server has old code running on it.
1.450 matthew 2914: my %temphash=($entry => $value);
1.449 matthew 2915: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2916: delete $accesshash{$entry};
2917: }
2918: }
2919: } else {
1.811 albertel 2920: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 2921: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 2922: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2923: delete $accesshash{$entry};
2924: }
1.185 www 2925: }
1.191 harris41 2926: }
1.352 www 2927: #
2928: # Roles
2929: # Reverse lookup of user roles for course faculty/staff and co-authorship
2930: #
1.800 albertel 2931: foreach my $entry (keys(%userrolehash)) {
1.351 www 2932: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 2933: split(/\:/,$entry);
2934: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 2935: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 2936: $rudom,$runame) eq 'ok') {
2937: delete $userrolehash{$entry};
2938: }
2939: }
1.662 raeburn 2940: #
2941: # Reverse lookup of domain roles (dc, ad, li, sc, au)
2942: #
2943: my %domrolebuffer = ();
1.1000 raeburn 2944: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 2945: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 2946: if ($domrolebuffer{$rudom}) {
2947: $domrolebuffer{$rudom}.='&'.&escape($entry).
2948: '='.&escape($domainrolehash{$entry});
2949: } else {
2950: $domrolebuffer{$rudom}.=&escape($entry).
2951: '='.&escape($domainrolehash{$entry});
2952: }
2953: delete $domainrolehash{$entry};
2954: }
2955: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 2956: my %servers = &get_servers($dom,'library');
2957: foreach my $tryserver (keys(%servers)) {
2958: unless (&reply('domroleput:'.$dom.':'.
2959: $domrolebuffer{$dom},$tryserver) eq 'ok') {
2960: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
2961: }
1.662 raeburn 2962: }
2963: }
1.186 www 2964: $dumpcount++;
1.157 www 2965: }
2966:
2967: sub courselog {
2968: my $what=shift;
1.158 www 2969: $what=time.':'.$what;
1.620 albertel 2970: unless ($env{'request.course.id'}) { return ''; }
2971: $coursedombuf{$env{'request.course.id'}}=
2972: $env{'course.'.$env{'request.course.id'}.'.domain'};
2973: $coursenumbuf{$env{'request.course.id'}}=
2974: $env{'course.'.$env{'request.course.id'}.'.num'};
2975: $coursehombuf{$env{'request.course.id'}}=
2976: $env{'course.'.$env{'request.course.id'}.'.home'};
2977: $coursedescrbuf{$env{'request.course.id'}}=
2978: $env{'course.'.$env{'request.course.id'}.'.description'};
2979: $courseinstcodebuf{$env{'request.course.id'}}=
2980: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2981: $courseownerbuf{$env{'request.course.id'}}=
2982: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 2983: $coursetypebuf{$env{'request.course.id'}}=
2984: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 2985: if (defined $courselogs{$env{'request.course.id'}}) {
2986: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 2987: } else {
1.620 albertel 2988: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 2989: }
1.620 albertel 2990: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 2991: &flushcourselogs();
2992: }
1.158 www 2993: }
2994:
2995: sub courseacclog {
2996: my $fnsymb=shift;
1.620 albertel 2997: unless ($env{'request.course.id'}) { return ''; }
2998: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 2999: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 3000: $what.=':POST';
1.583 matthew 3001: # FIXME: Probably ought to escape things....
1.800 albertel 3002: foreach my $key (keys(%env)) {
3003: if ($key=~/^form\.(.*)/) {
1.975 raeburn 3004: my $formitem = $1;
3005: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
3006: $what.=':'.$formitem.'='.$env{$key};
3007: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
3008: $what.=':'.$formitem.'='.$env{$key};
3009: }
1.158 www 3010: }
1.191 harris41 3011: }
1.583 matthew 3012: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
3013: # FIXME: We should not be depending on a form parameter that someone
3014: # editing lonsearchcat.pm might change in the future.
1.620 albertel 3015: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 3016: $what.= ':POST';
3017: # FIXME: Probably ought to escape things....
3018: foreach my $element ('courseexp','crsfulltext','crsrelated',
3019: 'crsdiscuss') {
1.620 albertel 3020: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 3021: }
3022: }
1.158 www 3023: }
3024: &courselog($what);
1.149 www 3025: }
3026:
1.185 www 3027: sub countacc {
3028: my $url=&declutter(shift);
1.458 matthew 3029: return if (! defined($url) || $url eq '');
1.620 albertel 3030: unless ($env{'request.course.id'}) { return ''; }
3031: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 3032: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 3033: $accesshash{$key}++;
1.185 www 3034: }
1.349 www 3035:
1.361 www 3036: sub linklog {
3037: my ($from,$to)=@_;
3038: $from=&declutter($from);
3039: $to=&declutter($to);
3040: $accesshash{$from.'___'.$to.'___comefrom'}=1;
3041: $accesshash{$to.'___'.$from.'___goto'}=1;
3042: }
3043:
1.349 www 3044: sub userrolelog {
3045: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 3046: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 3047: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 3048: ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041 raeburn 3049: ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350 www 3050: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3051: $userrolehash
3052: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 3053: =$tend.':'.$tstart;
1.662 raeburn 3054: }
1.898 albertel 3055: if (($env{'request.role'} =~ /dc\./) &&
3056: (($trole=~/^au/) || ($trole=~/^in/) ||
3057: ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041 raeburn 3058: ($trole=~/^cr/) || ($trole=~/^ta/) ||
3059: ($trole=~/^co/))) {
1.898 albertel 3060: $userrolehash
3061: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
3062: =$tend.':'.$tstart;
3063: }
1.662 raeburn 3064: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
3065: ($trole=~/^li/) || ($trole=~/^li/) ||
3066: ($trole=~/^au/) || ($trole=~/^dg/) ||
3067: ($trole=~/^sc/)) {
3068: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3069: $domainrolehash
3070: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
3071: = $tend.':'.$tstart;
3072: }
1.351 www 3073: }
3074:
1.957 raeburn 3075: sub courserolelog {
3076: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
3077: if (($trole eq 'cc') || ($trole eq 'in') ||
3078: ($trole eq 'ep') || ($trole eq 'ad') ||
3079: ($trole eq 'ta') || ($trole eq 'st') ||
1.1044 raeburn 3080: ($trole=~/^cr/) || ($trole eq 'gr') ||
3081: ($trole eq 'co')) {
1.957 raeburn 3082: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
3083: my $cdom = $1;
3084: my $cnum = $2;
3085: my $sec = $3;
3086: my $namespace = 'rolelog';
3087: my %storehash = (
3088: role => $trole,
3089: start => $tstart,
3090: end => $tend,
3091: selfenroll => $selfenroll,
3092: context => $context,
3093: );
3094: if ($trole eq 'gr') {
3095: $namespace = 'groupslog';
3096: $storehash{'group'} = $sec;
3097: } else {
3098: $storehash{'section'} = $sec;
3099: }
3100: &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996 raeburn 3101: if (($trole ne 'st') || ($sec ne '')) {
3102: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
3103: }
1.957 raeburn 3104: }
3105: }
3106: return;
3107: }
3108:
1.351 www 3109: sub get_course_adv_roles {
1.948 raeburn 3110: my ($cid,$codes) = @_;
1.620 albertel 3111: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 3112: my %coursehash=&coursedescription($cid);
1.988 raeburn 3113: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 3114: my %nothide=();
1.800 albertel 3115: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 3116: if ($user !~ /:/) {
3117: $nothide{join(':',split(/[\@]/,$user))}=1;
3118: } else {
3119: $nothide{$user}=1;
3120: }
1.470 www 3121: }
1.351 www 3122: my %returnhash=();
3123: my %dumphash=
3124: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
3125: my $now=time;
1.997 raeburn 3126: my %privileged;
1.1000 raeburn 3127: foreach my $entry (keys(%dumphash)) {
1.800 albertel 3128: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 3129: if (($tstart) && ($tstart<0)) { next; }
3130: if (($tend) && ($tend<$now)) { next; }
3131: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 3132: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 3133: if ($username eq '' || $domain eq '') { next; }
1.997 raeburn 3134: unless (ref($privileged{$domain}) eq 'HASH') {
3135: my %dompersonnel =
1.998 raeburn 3136: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997 raeburn 3137: $privileged{$domain} = {};
3138: foreach my $server (keys(%dompersonnel)) {
1.999 raeburn 3139: if (ref($dompersonnel{$server}) eq 'HASH') {
1.997 raeburn 3140: foreach my $user (keys(%{$dompersonnel{$server}})) {
3141: my ($trole,$uname,$udom) = split(/:/,$user);
3142: $privileged{$udom}{$uname} = 1;
3143: }
3144: }
3145: }
3146: }
3147: if ((exists($privileged{$domain}{$username})) &&
3148: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 3149: if ($role eq 'cr') { next; }
1.948 raeburn 3150: if ($codes) {
3151: if ($section) { $role .= ':'.$section; }
3152: if ($returnhash{$role}) {
3153: $returnhash{$role}.=','.$username.':'.$domain;
3154: } else {
3155: $returnhash{$role}=$username.':'.$domain;
3156: }
1.351 www 3157: } else {
1.988 raeburn 3158: my $key=&plaintext($role,$crstype);
1.973 bisitz 3159: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 3160: if ($returnhash{$key}) {
3161: $returnhash{$key}.=','.$username.':'.$domain;
3162: } else {
3163: $returnhash{$key}=$username.':'.$domain;
3164: }
1.351 www 3165: }
1.948 raeburn 3166: }
1.400 www 3167: return %returnhash;
3168: }
3169:
3170: sub get_my_roles {
1.937 raeburn 3171: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 3172: unless (defined($uname)) { $uname=$env{'user.name'}; }
3173: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 3174: my (%dumphash,%nothide);
1.858 raeburn 3175: if ($context eq 'userroles') {
1.1056.4.10 raeburn 3176: my $extra = &freeze_escape({'skipcheck' => 1});
3177: %dumphash = &dump('roles',$udom,$uname,'.',undef,$extra);
1.858 raeburn 3178: } else {
3179: %dumphash=
1.400 www 3180: &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 3181: if ($hidepriv) {
3182: my %coursehash=&coursedescription($udom.'_'.$uname);
3183: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3184: if ($user !~ /:/) {
3185: $nothide{join(':',split(/[\@]/,$user))} = 1;
3186: } else {
3187: $nothide{$user} = 1;
3188: }
3189: }
3190: }
1.858 raeburn 3191: }
1.400 www 3192: my %returnhash=();
3193: my $now=time;
1.999 raeburn 3194: my %privileged;
1.800 albertel 3195: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 3196: my ($role,$tend,$tstart);
3197: if ($context eq 'userroles') {
3198: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
3199: } else {
3200: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
3201: }
1.400 www 3202: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 3203: my $status = 'active';
1.939 raeburn 3204: if (($tend) && ($tend<=$now)) {
1.832 raeburn 3205: $status = 'previous';
3206: }
3207: if (($tstart) && ($now<$tstart)) {
3208: $status = 'future';
3209: }
3210: if (ref($types) eq 'ARRAY') {
3211: if (!grep(/^\Q$status\E$/,@{$types})) {
3212: next;
3213: }
3214: } else {
3215: if ($status ne 'active') {
3216: next;
3217: }
3218: }
1.867 raeburn 3219: my ($rolecode,$username,$domain,$section,$area);
3220: if ($context eq 'userroles') {
3221: ($area,$rolecode) = split(/_/,$entry);
3222: (undef,$domain,$username,$section) = split(/\//,$area);
3223: } else {
3224: ($role,$username,$domain,$section) = split(/\:/,$entry);
3225: }
1.832 raeburn 3226: if (ref($roledoms) eq 'ARRAY') {
3227: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
3228: next;
3229: }
3230: }
3231: if (ref($roles) eq 'ARRAY') {
3232: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 3233: if ($role =~ /^cr\//) {
3234: if (!grep(/^cr$/,@{$roles})) {
3235: next;
3236: }
1.1056.4.33.2 2(raebur 3237:2): } elsif ($role =~ /^gr\//) {
3238:2): if (!grep(/^gr$/,@{$roles})) {
3239:2): next;
3240:2): }
1.922 raeburn 3241: } else {
3242: next;
3243: }
1.832 raeburn 3244: }
1.867 raeburn 3245: }
1.937 raeburn 3246: if ($hidepriv) {
1.999 raeburn 3247: if ($context eq 'userroles') {
3248: if ((&privileged($username,$domain)) &&
3249: (!$nothide{$username.':'.$domain})) {
3250: next;
3251: }
3252: } else {
3253: unless (ref($privileged{$domain}) eq 'HASH') {
3254: my %dompersonnel =
3255: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
3256: $privileged{$domain} = {};
3257: if (keys(%dompersonnel)) {
3258: foreach my $server (keys(%dompersonnel)) {
3259: if (ref($dompersonnel{$server}) eq 'HASH') {
3260: foreach my $user (keys(%{$dompersonnel{$server}})) {
3261: my ($trole,$uname,$udom) = split(/:/,$user);
3262: $privileged{$udom}{$uname} = $trole;
3263: }
3264: }
3265: }
3266: }
3267: }
3268: if (exists($privileged{$domain}{$username})) {
3269: if (!$nothide{$username.':'.$domain}) {
3270: next;
3271: }
3272: }
1.937 raeburn 3273: }
3274: }
1.933 raeburn 3275: if ($withsec) {
3276: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
3277: $tstart.':'.$tend;
3278: } else {
3279: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
3280: }
1.832 raeburn 3281: }
1.373 www 3282: return %returnhash;
1.399 www 3283: }
3284:
3285: # ----------------------------------------------------- Frontpage Announcements
3286: #
3287: #
3288:
3289: sub postannounce {
3290: my ($server,$text)=@_;
1.844 albertel 3291: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 3292: unless ($text=~/\w/) { $text=''; }
3293: return &reply('setannounce:'.&escape($text),$server);
3294: }
3295:
3296: sub getannounce {
1.448 albertel 3297:
3298: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 3299: my $announcement='';
1.800 albertel 3300: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 3301: close($fh);
1.399 www 3302: if ($announcement=~/\w/) {
3303: return
3304: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 3305: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 3306: } else {
3307: return '';
3308: }
3309: } else {
3310: return '';
3311: }
1.351 www 3312: }
1.353 www 3313:
3314: # ---------------------------------------------------------- Course ID routines
3315: # Deal with domain's nohist_courseid.db files
3316: #
3317:
3318: sub courseidput {
1.921 raeburn 3319: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 3320: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 3321: my $outcome;
3322: if ($caller eq 'timeonly') {
3323: my $cids = '';
3324: foreach my $item (keys(%$storehash)) {
3325: $cids.=&escape($item).'&';
3326: }
3327: $cids=~s/\&$//;
3328: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
3329: $coursehome);
3330: } else {
3331: my $items = '';
3332: foreach my $item (keys(%$storehash)) {
3333: $items.= &escape($item).'='.
3334: &freeze_escape($$storehash{$item}).'&';
3335: }
3336: $items=~s/\&$//;
3337: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
3338: $coursehome);
1.918 raeburn 3339: }
3340: if ($outcome eq 'unknown_cmd') {
3341: my $what;
3342: foreach my $cid (keys(%$storehash)) {
3343: $what .= &escape($cid).'=';
1.921 raeburn 3344: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 3345: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 3346: }
3347: $what =~ s/\:$/&/;
3348: }
3349: $what =~ s/\&$//;
3350: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
3351: } else {
3352: return $outcome;
3353: }
1.353 www 3354: }
3355:
3356: sub courseiddump {
1.921 raeburn 3357: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 3358: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 3359: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1056.4.3 raeburn 3360: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918 raeburn 3361: my $as_hash = 1;
3362: my %returnhash;
3363: if (!$domfilter) { $domfilter=''; }
1.845 albertel 3364: my %libserv = &all_library();
3365: foreach my $tryserver (keys(%libserv)) {
3366: if ( ( $hostidflag == 1
3367: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
3368: || (!defined($hostidflag)) ) {
3369:
1.918 raeburn 3370: if (($domfilter eq '') ||
3371: (&host_domain($tryserver) eq $domfilter)) {
3372: my $rep =
3373: &reply('courseiddump:'.&host_domain($tryserver).':'.
3374: $sincefilter.':'.&escape($descfilter).':'.
3375: &escape($instcodefilter).':'.&escape($ownerfilter).
3376: ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947 raeburn 3377: ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962 raeburn 3378: &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008 raeburn 3379: $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029 raeburn 3380: &escape($cc_clone).':'.$cloneonly.':'.
3381: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1056.4.3 raeburn 3382: &escape($creationcontext).':'.$domcloner,
3383: $tryserver);
1.918 raeburn 3384: my @pairs=split(/\&/,$rep);
3385: foreach my $item (@pairs) {
3386: my ($key,$value)=split(/\=/,$item,2);
3387: $key = &unescape($key);
3388: next if ($key =~ /^error: 2 /);
3389: my $result = &thaw_unescape($value);
3390: if (ref($result) eq 'HASH') {
3391: $returnhash{$key}=$result;
3392: } else {
1.921 raeburn 3393: my @responses = split(/:/,$value);
3394: my @items = ('description','inst_code','owner','type');
1.918 raeburn 3395: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 3396: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 3397: }
1.1008 raeburn 3398: }
1.353 www 3399: }
3400: }
3401: }
3402: }
3403: return %returnhash;
3404: }
3405:
1.1055 raeburn 3406: sub courselastaccess {
3407: my ($cdom,$cnum,$hostidref) = @_;
3408: my %returnhash;
3409: if ($cdom && $cnum) {
3410: my $chome = &homeserver($cnum,$cdom);
3411: if ($chome ne 'no_host') {
3412: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
3413: &extract_lastaccess(\%returnhash,$rep);
3414: }
3415: } else {
3416: if (!$cdom) { $cdom=''; }
3417: my %libserv = &all_library();
3418: foreach my $tryserver (keys(%libserv)) {
3419: if (ref($hostidref) eq 'ARRAY') {
3420: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
3421: }
3422: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
3423: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
3424: &extract_lastaccess(\%returnhash,$rep);
3425: }
3426: }
3427: }
3428: return %returnhash;
3429: }
3430:
3431: sub extract_lastaccess {
3432: my ($returnhash,$rep) = @_;
3433: if (ref($returnhash) eq 'HASH') {
3434: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
3435: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
3436: $rep eq '') {
3437: my @pairs=split(/\&/,$rep);
3438: foreach my $item (@pairs) {
3439: my ($key,$value)=split(/\=/,$item,2);
3440: $key = &unescape($key);
3441: next if ($key =~ /^error: 2 /);
3442: $returnhash->{$key} = &thaw_unescape($value);
3443: }
3444: }
3445: }
3446: return;
3447: }
3448:
1.658 raeburn 3449: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 3450:
3451: sub dcmailput {
1.685 raeburn 3452: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 3453: my $status = &Apache::lonnet::critical(
1.740 www 3454: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
3455: &escape($message),$server);
1.662 raeburn 3456: return $status;
3457: }
3458:
1.658 raeburn 3459: sub dcmaildump {
3460: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 3461: my %returnhash=();
1.846 albertel 3462:
3463: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 3464: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
3465: &escape($enddate).':';
3466: my @esc_senders=map { &escape($_)} @$senders;
3467: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 3468: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 3469: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 3470: if (($key) && ($value)) {
3471: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 3472: }
3473: }
3474: }
3475: return %returnhash;
3476: }
1.662 raeburn 3477: # ---------------------------------------------------------- Domain roles
3478:
3479: sub get_domain_roles {
3480: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 3481: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 3482: $startdate = '.';
3483: }
1.1018 raeburn 3484: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 3485: $enddate = '.';
3486: }
1.922 raeburn 3487: my $rolelist;
3488: if (ref($roles) eq 'ARRAY') {
3489: $rolelist = join(':',@{$roles});
3490: }
1.662 raeburn 3491: my %personnel = ();
1.841 albertel 3492:
3493: my %servers = &get_servers($dom,'library');
3494: foreach my $tryserver (keys(%servers)) {
3495: %{$personnel{$tryserver}}=();
3496: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
3497: &escape($startdate).':'.
3498: &escape($enddate).':'.
3499: &escape($rolelist), $tryserver))) {
3500: my ($key,$value) = split(/\=/,$line,2);
3501: if (($key) && ($value)) {
3502: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
3503: }
3504: }
1.662 raeburn 3505: }
3506: return %personnel;
3507: }
1.658 raeburn 3508:
1.149 www 3509: # ----------------------------------------------------------- Check out an item
3510:
1.504 albertel 3511: sub get_first_access {
3512: my ($type,$argsymb)=@_;
1.790 albertel 3513: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3514: if ($argsymb) { $symb=$argsymb; }
3515: my ($map,$id,$res)=&decode_symb($symb);
1.926 albertel 3516: if ($type eq 'course') {
3517: $res='course';
3518: } elsif ($type eq 'map') {
1.588 albertel 3519: $res=&symbread($map);
3520: } else {
3521: $res=$symb;
3522: }
3523: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
3524: return $times{"$courseid\0$res"};
1.504 albertel 3525: }
3526:
3527: sub set_first_access {
3528: my ($type)=@_;
1.790 albertel 3529: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3530: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 3531: if ($type eq 'course') {
3532: $res='course';
3533: } elsif ($type eq 'map') {
1.588 albertel 3534: $res=&symbread($map);
3535: } else {
3536: $res=$symb;
3537: }
3538: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 3539: if (!$firstaccess) {
1.588 albertel 3540: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 3541: }
3542: return 'already_set';
1.504 albertel 3543: }
3544:
1.149 www 3545: sub checkout {
3546: my ($symb,$tuname,$tudom,$tcrsid)=@_;
3547: my $now=time;
3548: my $lonhost=$perlvar{'lonHostID'};
3549: my $infostr=&escape(
1.234 www 3550: 'CHECKOUTTOKEN&'.
1.149 www 3551: $tuname.'&'.
3552: $tudom.'&'.
3553: $tcrsid.'&'.
3554: $symb.'&'.
3555: $now.'&'.$ENV{'REMOTE_ADDR'});
3556: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 3557: if ($token=~/^error\:/) {
1.672 albertel 3558: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3559: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
3560: "</font>");
3561: return '';
3562: }
3563:
1.149 www 3564: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
3565: $token=~tr/a-z/A-Z/;
3566:
1.153 www 3567: my %infohash=('resource.0.outtoken' => $token,
3568: 'resource.0.checkouttime' => $now,
3569: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 3570:
3571: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3572: return '';
1.151 www 3573: } else {
1.672 albertel 3574: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3575: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
3576: "</font>");
1.149 www 3577: }
3578:
3579: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3580: &escape('Checkout '.$infostr.' - '.
3581: $token)) ne 'ok') {
3582: return '';
1.151 www 3583: } else {
1.672 albertel 3584: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3585: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
3586: "</font>");
1.149 www 3587: }
1.151 www 3588: return $token;
1.149 www 3589: }
3590:
3591: # ------------------------------------------------------------ Check in an item
3592:
3593: sub checkin {
3594: my $token=shift;
1.150 www 3595: my $now=time;
3596: my ($ta,$tb,$lonhost)=split(/\*/,$token);
3597: $lonhost=~tr/A-Z/a-z/;
1.838 albertel 3598: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150 www 3599: $dtoken=~s/\W/\_/g;
1.234 www 3600: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 3601: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
3602:
1.154 www 3603: unless (($tuname) && ($tudom)) {
3604: &logthis('Check in '.$token.' ('.$dtoken.') failed');
3605: return '';
3606: }
3607:
3608: unless (&allowed('mgr',$tcrsid)) {
3609: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 3610: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 3611: return '';
3612: }
3613:
1.153 www 3614: my %infohash=('resource.0.intoken' => $token,
3615: 'resource.0.checkintime' => $now,
3616: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 3617:
3618: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3619: return '';
3620: }
3621:
3622: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3623: &escape('Checkin - '.$token)) ne 'ok') {
3624: return '';
3625: }
3626:
3627: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 3628: }
3629:
3630: # --------------------------------------------- Set Expire Date for Spreadsheet
3631:
3632: sub expirespread {
3633: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 3634: my $cid=$env{'request.course.id'};
1.110 www 3635: if ($cid) {
3636: my $now=time;
3637: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 3638: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
3639: $env{'course.'.$cid.'.num'}.
1.110 www 3640: ':nohist_expirationdates:'.
3641: &escape($key).'='.$now,
1.620 albertel 3642: $env{'course.'.$cid.'.home'})
1.110 www 3643: }
3644: return 'ok';
1.14 www 3645: }
3646:
1.109 www 3647: # ----------------------------------------------------- Devalidate Spreadsheets
3648:
3649: sub devalidate {
1.325 www 3650: my ($symb,$uname,$udom)=@_;
1.620 albertel 3651: my $cid=$env{'request.course.id'};
1.109 www 3652: if ($cid) {
1.391 matthew 3653: # delete the stored spreadsheets for
3654: # - the student level sheet of this user in course's homespace
3655: # - the assessment level sheet for this resource
3656: # for this user in user's homespace
1.553 albertel 3657: # - current conditional state info
1.325 www 3658: my $key=$uname.':'.$udom.':';
1.109 www 3659: my $status=
1.299 matthew 3660: &del('nohist_calculatedsheets',
1.391 matthew 3661: [$key.'studentcalc:'],
1.620 albertel 3662: $env{'course.'.$cid.'.domain'},
3663: $env{'course.'.$cid.'.num'})
1.133 albertel 3664: .' '.
3665: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 3666: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 3667: unless ($status eq 'ok ok') {
3668: &logthis('Could not devalidate spreadsheet '.
1.325 www 3669: $uname.' at '.$udom.' for '.
1.109 www 3670: $symb.': '.$status);
1.133 albertel 3671: }
1.553 albertel 3672: &delenv('user.state.'.$cid);
1.109 www 3673: }
3674: }
3675:
1.265 albertel 3676: sub get_scalar {
3677: my ($string,$end) = @_;
3678: my $value;
3679: if ($$string =~ s/^([^&]*?)($end)/$2/) {
3680: $value = $1;
3681: } elsif ($$string =~ s/^([^&]*?)&//) {
3682: $value = $1;
3683: }
3684: return &unescape($value);
3685: }
3686:
3687: sub array2str {
3688: my (@array) = @_;
3689: my $result=&arrayref2str(\@array);
3690: $result=~s/^__ARRAY_REF__//;
3691: $result=~s/__END_ARRAY_REF__$//;
3692: return $result;
3693: }
3694:
1.204 albertel 3695: sub arrayref2str {
3696: my ($arrayref) = @_;
1.265 albertel 3697: my $result='__ARRAY_REF__';
1.204 albertel 3698: foreach my $elem (@$arrayref) {
1.265 albertel 3699: if(ref($elem) eq 'ARRAY') {
3700: $result.=&arrayref2str($elem).'&';
3701: } elsif(ref($elem) eq 'HASH') {
3702: $result.=&hashref2str($elem).'&';
3703: } elsif(ref($elem)) {
3704: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 3705: } else {
3706: $result.=&escape($elem).'&';
3707: }
3708: }
3709: $result=~s/\&$//;
1.265 albertel 3710: $result .= '__END_ARRAY_REF__';
1.204 albertel 3711: return $result;
3712: }
3713:
1.168 albertel 3714: sub hash2str {
1.204 albertel 3715: my (%hash) = @_;
3716: my $result=&hashref2str(\%hash);
1.265 albertel 3717: $result=~s/^__HASH_REF__//;
3718: $result=~s/__END_HASH_REF__$//;
1.204 albertel 3719: return $result;
3720: }
3721:
3722: sub hashref2str {
3723: my ($hashref)=@_;
1.265 albertel 3724: my $result='__HASH_REF__';
1.800 albertel 3725: foreach my $key (sort(keys(%$hashref))) {
3726: if (ref($key) eq 'ARRAY') {
3727: $result.=&arrayref2str($key).'=';
3728: } elsif (ref($key) eq 'HASH') {
3729: $result.=&hashref2str($key).'=';
3730: } elsif (ref($key)) {
1.265 albertel 3731: $result.='=';
1.800 albertel 3732: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 3733: } else {
1.1056.4.32 raeburn 3734: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 3735: }
3736:
1.800 albertel 3737: if(ref($hashref->{$key}) eq 'ARRAY') {
3738: $result.=&arrayref2str($hashref->{$key}).'&';
3739: } elsif(ref($hashref->{$key}) eq 'HASH') {
3740: $result.=&hashref2str($hashref->{$key}).'&';
3741: } elsif(ref($hashref->{$key})) {
1.265 albertel 3742: $result.='&';
1.800 albertel 3743: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 3744: } else {
1.800 albertel 3745: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 3746: }
3747: }
1.168 albertel 3748: $result=~s/\&$//;
1.265 albertel 3749: $result .= '__END_HASH_REF__';
1.168 albertel 3750: return $result;
3751: }
3752:
3753: sub str2hash {
1.265 albertel 3754: my ($string)=@_;
3755: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
3756: return %$hash;
3757: }
3758:
3759: sub str2hashref {
1.168 albertel 3760: my ($string) = @_;
1.265 albertel 3761:
3762: my %hash;
3763:
3764: if($string !~ /^__HASH_REF__/) {
3765: if (! ($string eq '' || !defined($string))) {
3766: $hash{'error'}='Not hash reference';
3767: }
3768: return (\%hash, $string);
3769: }
3770:
3771: $string =~ s/^__HASH_REF__//;
3772:
3773: while($string !~ /^__END_HASH_REF__/) {
3774: #key
3775: my $key='';
3776: if($string =~ /^__HASH_REF__/) {
3777: ($key, $string)=&str2hashref($string);
3778: if(defined($key->{'error'})) {
3779: $hash{'error'}='Bad data';
3780: return (\%hash, $string);
3781: }
3782: } elsif($string =~ /^__ARRAY_REF__/) {
3783: ($key, $string)=&str2arrayref($string);
3784: if($key->[0] eq 'Array reference error') {
3785: $hash{'error'}='Bad data';
3786: return (\%hash, $string);
3787: }
3788: } else {
3789: $string =~ s/^(.*?)=//;
1.267 albertel 3790: $key=&unescape($1);
1.265 albertel 3791: }
3792: $string =~ s/^=//;
3793:
3794: #value
3795: my $value='';
3796: if($string =~ /^__HASH_REF__/) {
3797: ($value, $string)=&str2hashref($string);
3798: if(defined($value->{'error'})) {
3799: $hash{'error'}='Bad data';
3800: return (\%hash, $string);
3801: }
3802: } elsif($string =~ /^__ARRAY_REF__/) {
3803: ($value, $string)=&str2arrayref($string);
3804: if($value->[0] eq 'Array reference error') {
3805: $hash{'error'}='Bad data';
3806: return (\%hash, $string);
3807: }
3808: } else {
3809: $value=&get_scalar(\$string,'__END_HASH_REF__');
3810: }
3811: $string =~ s/^&//;
3812:
3813: $hash{$key}=$value;
1.204 albertel 3814: }
1.265 albertel 3815:
3816: $string =~ s/^__END_HASH_REF__//;
3817:
3818: return (\%hash, $string);
1.204 albertel 3819: }
3820:
3821: sub str2array {
1.265 albertel 3822: my ($string)=@_;
3823: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
3824: return @$array;
3825: }
3826:
3827: sub str2arrayref {
1.204 albertel 3828: my ($string) = @_;
1.265 albertel 3829: my @array;
3830:
3831: if($string !~ /^__ARRAY_REF__/) {
3832: if (! ($string eq '' || !defined($string))) {
3833: $array[0]='Array reference error';
3834: }
3835: return (\@array, $string);
3836: }
3837:
3838: $string =~ s/^__ARRAY_REF__//;
3839:
3840: while($string !~ /^__END_ARRAY_REF__/) {
3841: my $value='';
3842: if($string =~ /^__HASH_REF__/) {
3843: ($value, $string)=&str2hashref($string);
3844: if(defined($value->{'error'})) {
3845: $array[0] ='Array reference error';
3846: return (\@array, $string);
3847: }
3848: } elsif($string =~ /^__ARRAY_REF__/) {
3849: ($value, $string)=&str2arrayref($string);
3850: if($value->[0] eq 'Array reference error') {
3851: $array[0] ='Array reference error';
3852: return (\@array, $string);
3853: }
3854: } else {
3855: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
3856: }
3857: $string =~ s/^&//;
3858:
3859: push(@array, $value);
1.191 harris41 3860: }
1.265 albertel 3861:
3862: $string =~ s/^__END_ARRAY_REF__//;
3863:
3864: return (\@array, $string);
1.168 albertel 3865: }
3866:
1.167 albertel 3867: # -------------------------------------------------------------------Temp Store
3868:
1.168 albertel 3869: sub tmpreset {
3870: my ($symb,$namespace,$domain,$stuname) = @_;
3871: if (!$symb) {
3872: $symb=&symbread();
1.620 albertel 3873: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3874: }
3875: $symb=escape($symb);
3876:
1.620 albertel 3877: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 3878: $namespace=~s/\//\_/g;
3879: $namespace=~s/\W//g;
3880:
1.620 albertel 3881: if (!$domain) { $domain=$env{'user.domain'}; }
3882: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3883: if ($domain eq 'public' && $stuname eq 'public') {
3884: $stuname=$ENV{'REMOTE_ADDR'};
3885: }
1.168 albertel 3886: my $path=$perlvar{'lonDaemons'}.'/tmp';
3887: my %hash;
3888: if (tie(%hash,'GDBM_File',
3889: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3890: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 3891: foreach my $key (keys(%hash)) {
1.180 albertel 3892: if ($key=~ /:$symb/) {
1.168 albertel 3893: delete($hash{$key});
3894: }
3895: }
3896: }
3897: }
3898:
1.167 albertel 3899: sub tmpstore {
1.168 albertel 3900: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3901:
3902: if (!$symb) {
3903: $symb=&symbread();
1.620 albertel 3904: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3905: }
3906: $symb=escape($symb);
3907:
3908: if (!$namespace) {
3909: # I don't think we would ever want to store this for a course.
3910: # it seems this will only be used if we don't have a course.
1.620 albertel 3911: #$namespace=$env{'request.course.id'};
1.168 albertel 3912: #if (!$namespace) {
1.620 albertel 3913: $namespace=$env{'request.state'};
1.168 albertel 3914: #}
3915: }
3916: $namespace=~s/\//\_/g;
3917: $namespace=~s/\W//g;
1.620 albertel 3918: if (!$domain) { $domain=$env{'user.domain'}; }
3919: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3920: if ($domain eq 'public' && $stuname eq 'public') {
3921: $stuname=$ENV{'REMOTE_ADDR'};
3922: }
1.168 albertel 3923: my $now=time;
3924: my %hash;
3925: my $path=$perlvar{'lonDaemons'}.'/tmp';
3926: if (tie(%hash,'GDBM_File',
3927: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3928: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3929: $hash{"version:$symb"}++;
3930: my $version=$hash{"version:$symb"};
3931: my $allkeys='';
3932: foreach my $key (keys(%$storehash)) {
3933: $allkeys.=$key.':';
1.591 albertel 3934: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 3935: }
3936: $hash{"$version:$symb:timestamp"}=$now;
3937: $allkeys.='timestamp';
3938: $hash{"$version:keys:$symb"}=$allkeys;
3939: if (untie(%hash)) {
3940: return 'ok';
3941: } else {
3942: return "error:$!";
3943: }
3944: } else {
3945: return "error:$!";
3946: }
3947: }
1.167 albertel 3948:
1.168 albertel 3949: # -----------------------------------------------------------------Temp Restore
1.167 albertel 3950:
1.168 albertel 3951: sub tmprestore {
3952: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 3953:
1.168 albertel 3954: if (!$symb) {
3955: $symb=&symbread();
1.620 albertel 3956: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3957: }
3958: $symb=escape($symb);
3959:
1.620 albertel 3960: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 3961:
1.620 albertel 3962: if (!$domain) { $domain=$env{'user.domain'}; }
3963: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3964: if ($domain eq 'public' && $stuname eq 'public') {
3965: $stuname=$ENV{'REMOTE_ADDR'};
3966: }
1.168 albertel 3967: my %returnhash;
3968: $namespace=~s/\//\_/g;
3969: $namespace=~s/\W//g;
3970: my %hash;
3971: my $path=$perlvar{'lonDaemons'}.'/tmp';
3972: if (tie(%hash,'GDBM_File',
3973: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3974: &GDBM_READER(),0640)) {
1.168 albertel 3975: my $version=$hash{"version:$symb"};
3976: $returnhash{'version'}=$version;
3977: my $scope;
3978: for ($scope=1;$scope<=$version;$scope++) {
3979: my $vkeys=$hash{"$scope:keys:$symb"};
3980: my @keys=split(/:/,$vkeys);
3981: my $key;
3982: $returnhash{"$scope:keys"}=$vkeys;
3983: foreach $key (@keys) {
1.591 albertel 3984: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
3985: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 3986: }
3987: }
1.168 albertel 3988: if (!(untie(%hash))) {
3989: return "error:$!";
3990: }
3991: } else {
3992: return "error:$!";
3993: }
3994: return %returnhash;
1.167 albertel 3995: }
3996:
1.9 www 3997: # ----------------------------------------------------------------------- Store
3998:
3999: sub store {
1.124 www 4000: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
4001: my $home='';
4002:
1.168 albertel 4003: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4004:
1.213 www 4005: $symb=&symbclean($symb);
1.122 albertel 4006: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4007:
1.620 albertel 4008: if (!$domain) { $domain=$env{'user.domain'}; }
4009: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4010:
4011: &devalidate($symb,$stuname,$domain);
1.109 www 4012:
4013: $symb=escape($symb);
1.187 www 4014: if (!$namespace) {
1.620 albertel 4015: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4016: return '';
4017: }
4018: }
1.620 albertel 4019: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4020:
4021: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4022: $$storehash{'host'}=$perlvar{'lonHostID'};
4023:
1.12 www 4024: my $namevalue='';
1.800 albertel 4025: foreach my $key (keys(%$storehash)) {
4026: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4027: }
1.12 www 4028: $namevalue=~s/\&$//;
1.187 www 4029: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 4030: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 4031: }
4032:
1.47 www 4033: # -------------------------------------------------------------- Critical Store
4034:
4035: sub cstore {
1.124 www 4036: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
4037: my $home='';
4038:
1.168 albertel 4039: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4040:
1.213 www 4041: $symb=&symbclean($symb);
1.122 albertel 4042: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4043:
1.620 albertel 4044: if (!$domain) { $domain=$env{'user.domain'}; }
4045: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4046:
4047: &devalidate($symb,$stuname,$domain);
1.109 www 4048:
4049: $symb=escape($symb);
1.187 www 4050: if (!$namespace) {
1.620 albertel 4051: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4052: return '';
4053: }
4054: }
1.620 albertel 4055: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4056:
4057: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4058: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 4059:
1.47 www 4060: my $namevalue='';
1.800 albertel 4061: foreach my $key (keys(%$storehash)) {
4062: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4063: }
1.47 www 4064: $namevalue=~s/\&$//;
1.187 www 4065: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 4066: return critical
4067: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 4068: }
4069:
1.9 www 4070: # --------------------------------------------------------------------- Restore
4071:
4072: sub restore {
1.124 www 4073: my ($symb,$namespace,$domain,$stuname) = @_;
4074: my $home='';
4075:
1.168 albertel 4076: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4077:
1.122 albertel 4078: if (!$symb) {
4079: unless ($symb=escape(&symbread())) { return ''; }
4080: } else {
1.213 www 4081: $symb=&escape(&symbclean($symb));
1.122 albertel 4082: }
1.188 www 4083: if (!$namespace) {
1.620 albertel 4084: unless ($namespace=$env{'request.course.id'}) {
1.188 www 4085: return '';
4086: }
4087: }
1.620 albertel 4088: if (!$domain) { $domain=$env{'user.domain'}; }
4089: if (!$stuname) { $stuname=$env{'user.name'}; }
4090: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 4091: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
4092:
1.12 www 4093: my %returnhash=();
1.800 albertel 4094: foreach my $line (split(/\&/,$answer)) {
4095: my ($name,$value)=split(/\=/,$line);
1.591 albertel 4096: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 4097: }
1.75 www 4098: my $version;
4099: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 4100: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
4101: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 4102: }
1.75 www 4103: }
1.13 www 4104: return %returnhash;
1.34 www 4105: }
4106:
4107: # ---------------------------------------------------------- Course Description
4108:
4109: sub coursedescription {
1.731 albertel 4110: my ($courseid,$args)=@_;
1.34 www 4111: $courseid=~s/^\///;
1.49 www 4112: $courseid=~s/\_/\//g;
1.34 www 4113: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 4114: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 4115: my $normalid=$cdomain.'_'.$cnum;
4116: # need to always cache even if we get errors otherwise we keep
4117: # trying and trying and trying to get the course description.
4118: my %envhash=();
4119: my %returnhash=();
1.731 albertel 4120:
4121: my $expiretime=600;
4122: if ($env{'request.course.id'} eq $normalid) {
4123: $expiretime=120;
4124: }
4125:
4126: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
4127: if (!$args->{'freshen_cache'}
4128: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
4129: foreach my $key (keys(%env)) {
4130: next if ($key !~ /^\Q$prefix\E(.*)/);
4131: my ($setting) = $1;
4132: $returnhash{$setting} = $env{$key};
4133: }
4134: return %returnhash;
4135: }
4136:
4137: # get the data agin
4138: if (!$args->{'one_time'}) {
4139: $envhash{'course.'.$normalid.'.last_cache'}=time;
4140: }
1.811 albertel 4141:
1.34 www 4142: if ($chome ne 'no_host') {
1.302 albertel 4143: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 4144: if (!exists($returnhash{'con_lost'})) {
4145: $returnhash{'home'}= $chome;
4146: $returnhash{'domain'} = $cdomain;
4147: $returnhash{'num'} = $cnum;
1.741 raeburn 4148: if (!defined($returnhash{'type'})) {
4149: $returnhash{'type'} = 'Course';
4150: }
1.130 albertel 4151: while (my ($name,$value) = each %returnhash) {
1.53 www 4152: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 4153: }
1.270 www 4154: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 4155: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 4156: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 4157: $envhash{'course.'.$normalid.'.home'}=$chome;
4158: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
4159: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 4160: }
4161: }
1.731 albertel 4162: if (!$args->{'one_time'}) {
1.949 raeburn 4163: &appenv(\%envhash);
1.731 albertel 4164: }
1.302 albertel 4165: return %returnhash;
1.461 www 4166: }
4167:
1.1056.4.5 raeburn 4168: sub update_released_required {
4169: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
4170: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
4171: $cid = $env{'request.course.id'};
4172: $cdom = $env{'course.'.$cid.'.domain'};
4173: $cnum = $env{'course.'.$cid.'.num'};
4174: $chome = $env{'course.'.$cid.'.home'};
4175: }
4176: if ($needsrelease) {
4177: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
4178: my $needsupdate;
4179: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
4180: $needsupdate = 1;
4181: } else {
4182: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
4183: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
4184: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
4185: $needsupdate = 1;
4186: }
4187: }
4188: if ($needsupdate) {
4189: my %needshash = (
4190: 'internal.releaserequired' => $needsrelease,
4191: );
4192: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
4193: if ($putresult eq 'ok') {
4194: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
4195: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
4196: if (ref($crsinfo{$cid}) eq 'HASH') {
4197: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
4198: &courseidput($cdom,\%crsinfo,$chome,'notime');
4199: }
4200: }
4201: }
4202: }
4203: return;
4204: }
4205:
1.461 www 4206: # -------------------------------------------------See if a user is privileged
4207:
4208: sub privileged {
4209: my ($username,$domain)=@_;
4210: my $rolesdump=&reply("dump:$domain:$username:roles",
4211: &homeserver($username,$domain));
1.1033 raeburn 4212: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
4213: ($rolesdump =~ /^error:/)) {
4214: return 0;
4215: }
1.461 www 4216: my $now=time;
4217: if ($rolesdump ne '') {
1.800 albertel 4218: foreach my $entry (split(/&/,$rolesdump)) {
4219: if ($entry!~/^rolesdef_/) {
4220: my ($area,$role)=split(/=/,$entry);
1.461 www 4221: $area=~s/\_\w\w$//;
4222: my ($trole,$tend,$tstart)=split(/_/,$role);
4223: if (($trole eq 'dc') || ($trole eq 'su')) {
4224: my $active=1;
4225: if ($tend) {
4226: if ($tend<$now) { $active=0; }
4227: }
4228: if ($tstart) {
4229: if ($tstart>$now) { $active=0; }
4230: }
4231: if ($active) { return 1; }
4232: }
4233: }
4234: }
4235: }
4236: return 0;
1.9 www 4237: }
1.1 albertel 4238:
1.103 harris41 4239: # -------------------------------------------------------- Get user privileges
1.11 www 4240:
4241: sub rolesinit {
4242: my ($domain,$username,$authhost)=@_;
1.1034 raeburn 4243: my $now=time;
4244: my %userroles = ('user.login.time' => $now);
1.1056.4.10 raeburn 4245: my $extra = &freeze_escape({'skipcheck' => 1});
1.1056.4.3 raeburn 4246: my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033 raeburn 4247: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
1.1056.4.3 raeburn 4248: ($rolesdump =~ /^error:/)) {
1.1033 raeburn 4249: return \%userroles;
4250: }
1.11 www 4251: my %allroles=();
1.678 raeburn 4252: my %allgroups=();
1.11 www 4253:
4254: if ($rolesdump ne '') {
1.800 albertel 4255: foreach my $entry (split(/&/,$rolesdump)) {
4256: if ($entry!~/^rolesdef_/) {
4257: my ($area,$role)=split(/=/,$entry);
1.587 albertel 4258: $area=~s/\_\w\w$//;
1.678 raeburn 4259: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 4260: if ($role=~/^cr/) {
1.807 albertel 4261: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
4262: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 4263: ($tend,$tstart)=split('_',$trest);
4264: } else {
4265: $trole=$role;
4266: }
1.678 raeburn 4267: } elsif ($role =~ m|^gr/|) {
4268: ($trole,$tend,$tstart) = split(/_/,$role);
1.1056.4.33.2 2(raebur 4269:2): next if ($tstart eq '-1');
1.678 raeburn 4270: ($trole,$group_privs) = split(/\//,$trole);
4271: $group_privs = &unescape($group_privs);
1.587 albertel 4272: } else {
4273: ($trole,$tend,$tstart)=split(/_/,$role);
4274: }
1.743 albertel 4275: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
4276: $username);
4277: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 4278: if (($tend!=0) && ($tend<$now)) { $trole=''; }
4279: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 4280: if (($area ne '') && ($trole ne '')) {
1.347 albertel 4281: my $spec=$trole.'.'.$area;
4282: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
4283: if ($trole =~ /^cr\//) {
1.567 raeburn 4284: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 4285: } elsif ($trole eq 'gr') {
4286: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 4287: } else {
1.567 raeburn 4288: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 4289: }
1.12 www 4290: }
1.662 raeburn 4291: }
1.191 harris41 4292: }
1.743 albertel 4293: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
4294: $userroles{'user.adv'} = $adv;
4295: $userroles{'user.author'} = $author;
1.620 albertel 4296: $env{'user.adv'}=$adv;
1.11 www 4297: }
1.743 albertel 4298: return \%userroles;
1.11 www 4299: }
4300:
1.567 raeburn 4301: sub set_arearole {
4302: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
4303: # log the associated role with the area
4304: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 4305: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 4306: }
4307:
4308: sub custom_roleprivs {
4309: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
4310: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
4311: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 4312: if (&hostname($homsvr) ne '') {
1.567 raeburn 4313: my ($rdummy,$roledef)=
4314: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
4315: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
4316: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
4317: if (defined($syspriv)) {
1.1043 raeburn 4318: if ($trest =~ /^$match_community$/) {
4319: $syspriv =~ s/bre\&S//;
4320: }
1.567 raeburn 4321: $$allroles{'cm./'}.=':'.$syspriv;
4322: $$allroles{$spec.'./'}.=':'.$syspriv;
4323: }
4324: if ($tdomain ne '') {
4325: if (defined($dompriv)) {
4326: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
4327: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
4328: }
4329: if (($trest ne '') && (defined($coursepriv))) {
4330: $$allroles{'cm.'.$area}.=':'.$coursepriv;
4331: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
4332: }
4333: }
4334: }
4335: }
4336: }
4337:
1.678 raeburn 4338: sub group_roleprivs {
4339: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
4340: my $access = 1;
4341: my $now = time;
4342: if (($tend!=0) && ($tend<$now)) { $access = 0; }
4343: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
4344: if ($access) {
1.811 albertel 4345: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 4346: $$allgroups{$course}{$group} .=':'.$group_privs;
4347: }
4348: }
1.567 raeburn 4349:
4350: sub standard_roleprivs {
4351: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
4352: if (defined($pr{$trole.':s'})) {
4353: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
4354: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
4355: }
4356: if ($tdomain ne '') {
4357: if (defined($pr{$trole.':d'})) {
4358: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4359: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4360: }
4361: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
4362: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
4363: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
4364: }
4365: }
4366: }
4367:
4368: sub set_userprivs {
1.1056.4.2 raeburn 4369: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 4370: my $author=0;
4371: my $adv=0;
1.678 raeburn 4372: my %grouproles = ();
4373: if (keys(%{$allgroups}) > 0) {
1.1056.4.2 raeburn 4374: my @groupkeys;
1.1000 raeburn 4375: foreach my $role (keys(%{$allroles})) {
1.1056.4.2 raeburn 4376: push(@groupkeys,$role);
4377: }
4378: if (ref($groups_roles) eq 'HASH') {
4379: foreach my $key (keys(%{$groups_roles})) {
4380: unless (grep(/^\Q$key\E$/,@groupkeys)) {
4381: push(@groupkeys,$key);
4382: }
4383: }
4384: }
4385: if (@groupkeys > 0) {
4386: foreach my $role (@groupkeys) {
4387: my ($trole,$area,$sec,$extendedarea);
4388: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
4389: $trole = $1;
4390: $area = $2;
4391: $sec = $3;
4392: $extendedarea = $area.$sec;
4393: if (exists($$allgroups{$area})) {
4394: foreach my $group (keys(%{$$allgroups{$area}})) {
4395: my $spec = $trole.'.'.$extendedarea;
4396: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 4397: $$allgroups{$area}{$group};
1.1056.4.2 raeburn 4398: }
1.678 raeburn 4399: }
4400: }
4401: }
4402: }
4403: }
1.800 albertel 4404: foreach my $group (keys(%grouproles)) {
4405: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 4406: }
1.800 albertel 4407: foreach my $role (keys(%{$allroles})) {
4408: my %thesepriv;
1.941 raeburn 4409: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 4410: foreach my $item (split(/:/,$$allroles{$role})) {
4411: if ($item ne '') {
4412: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 4413: if ($restrictions eq '') {
4414: $thesepriv{$privilege}='F';
4415: } elsif ($thesepriv{$privilege} ne 'F') {
4416: $thesepriv{$privilege}.=$restrictions;
4417: }
4418: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
4419: }
4420: }
4421: my $thesestr='';
1.1056.4.33.2 2(raebur 4422:2): foreach my $priv (sort(keys(%thesepriv))) {
1.800 albertel 4423: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
4424: }
4425: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 4426: }
4427: return ($author,$adv);
4428: }
4429:
1.994 raeburn 4430: sub role_status {
1.1056.4.33.2 2(raebur 4431:2): my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 4432: my @pwhere = ();
4433: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
4434: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
4435: unless (!defined($$role) || $$role eq '') {
4436: $$where=join('.',@pwhere);
4437: $$trolecode=$$role.'.'.$$where;
4438: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
4439: $$tstatus='is';
1.1056.4.33.2 2(raebur 4440:2): if ($$tstart && $$tstart>$update) {
1.994 raeburn 4441: $$tstatus='future';
1.1034 raeburn 4442: if ($$tstart<$now) {
4443: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 4444: if (($$where ne '') && ($$role ne '')) {
1.1056.4.2 raeburn 4445: my (%allroles,%allgroups,$group_privs,
4446: %groups_roles,@rolecodes);
1.1002 raeburn 4447: my %userroles = (
4448: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
4449: );
1.1056.4.2 raeburn 4450: @rolecodes = ('cm');
1.1002 raeburn 4451: my $spec=$$role.'.'.$$where;
4452: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
4453: if ($$role =~ /^cr\//) {
4454: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1056.4.2 raeburn 4455: push(@rolecodes,'cr');
1.1002 raeburn 4456: } elsif ($$role eq 'gr') {
1.1056.4.2 raeburn 4457: push(@rolecodes,$$role);
1.1002 raeburn 4458: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
4459: $env{'user.name'});
1.1056.4.2 raeburn 4460: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 4461: (undef,my $group_privs) = split(/\//,$trole);
4462: $group_privs = &unescape($group_privs);
4463: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1056.4.2 raeburn 4464: 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.1056.4.33.2 2(raebur 4465:2): &get_groups_roles($tdomain,$trest,
4466:2): \%course_roles,\@rolecodes,
4467:2): \%groups_roles);
1.1002 raeburn 4468: } else {
1.1056.4.2 raeburn 4469: push(@rolecodes,$$role);
1.1002 raeburn 4470: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
4471: }
1.1056.4.2 raeburn 4472: my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
4473: &appenv(\%userroles,\@rolecodes);
1.1002 raeburn 4474: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
4475: }
4476: }
1.1034 raeburn 4477: $$tstatus = 'is';
1.1002 raeburn 4478: }
1.994 raeburn 4479: }
4480: if ($$tend) {
1.1056.4.33.2 2(raebur 4481:2): if ($$tend<$update) {
1.994 raeburn 4482: $$tstatus='expired';
4483: } elsif ($$tend<$now) {
4484: $$tstatus='will_not';
4485: }
4486: }
4487: }
4488: }
4489: }
4490:
1.1056.4.33.2 2(raebur 4491:2): sub get_groups_roles {
4492:2): my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
4493:2): return unless((ref($cdom_courseroles) eq 'HASH') &&
4494:2): (ref($rolecodes) eq 'ARRAY') &&
4495:2): (ref($groups_roles) eq 'HASH'));
4496:2): if (keys(%{$cdom_courseroles}) > 0) {
4497:2): my ($cnum) = ($rest =~ /^($match_courseid)/);
4498:2): if ($cdom ne '' && $cnum ne '') {
4499:2): foreach my $key (keys(%{$cdom_courseroles})) {
4500:2): if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
4501:2): my $crsrole = $1;
4502:2): my $crssec = $2;
4503:2): if ($crsrole =~ /^cr/) {
4504:2): unless (grep(/^cr$/,@{$rolecodes})) {
4505:2): push(@{$rolecodes},'cr');
4506:2): }
4507:2): } else {
4508:2): unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
4509:2): push(@{$rolecodes},$crsrole);
4510:2): }
4511:2): }
4512:2): my $rolekey = "$crsrole./$cdom/$cnum";
4513:2): if ($crssec ne '') {
4514:2): $rolekey .= "/$crssec";
4515:2): }
4516:2): $rolekey .= './';
4517:2): $groups_roles->{$rolekey} = $rolecodes;
4518:2): }
4519:2): }
4520:2): }
4521:2): }
4522:2): return;
4523:2): }
4524:2):
4525:2): sub delete_env_groupprivs {
4526:2): my ($where,$courseroles,$possroles) = @_;
4527:2): return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
4528:2): my ($dummy,$udom,$uname,$group) = split(/\//,$where);
4529:2): unless (ref($courseroles->{$udom}) eq 'HASH') {
4530:2): %{$courseroles->{$udom}} =
4531:2): &get_my_roles('','','userroles',['active'],
4532:2): $possroles,[$udom],1);
4533:2): }
4534:2): if (ref($courseroles->{$udom}) eq 'HASH') {
4535:2): foreach my $item (keys(%{$courseroles->{$udom}})) {
4536:2): my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
4537:2): my $area = '/'.$cdom.'/'.$cnum;
4538:2): my $privkey = "user.priv.$crsrole.$area";
4539:2): if ($crssec ne '') {
4540:2): $privkey .= '/'.$crssec;
4541:2): }
4542:2): $privkey .= ".$area/$group";
4543:2): &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
4544:2): }
4545:2): }
4546:2): return;
4547:2): }
4548:2):
1.994 raeburn 4549: sub check_adhoc_privs {
1.1056.4.33.2 2(raebur 4550:2): my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller) = @_;
1.994 raeburn 4551: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
4552: if ($env{$cckey}) {
4553: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1056.4.33.2 2(raebur 4554:2): &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 4555: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1056.4.12 raeburn 4556: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4557: }
4558: } else {
1.1056.4.12 raeburn 4559: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4560: }
4561: }
4562:
4563: sub set_adhoc_privileges {
4564: # role can be cc or ca
1.1056.4.12 raeburn 4565: my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994 raeburn 4566: my $area = '/'.$dcdom.'/'.$pickedcourse;
4567: my $spec = $role.'.'.$area;
4568: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
4569: $env{'user.name'});
4570: my %ccrole = ();
4571: &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
4572: my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
4573: &appenv(\%userroles,[$role,'cm']);
4574: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1056.4.12 raeburn 4575: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
4576: &appenv( {'request.role' => $spec,
4577: 'request.role.domain' => $dcdom,
4578: 'request.course.sec' => ''
4579: }
4580: );
4581: my $tadv=0;
4582: if (&allowed('adv') eq 'F') { $tadv=1; }
4583: &appenv({'request.role.adv' => $tadv});
4584: }
1.994 raeburn 4585: }
4586:
1.12 www 4587: # --------------------------------------------------------------- get interface
4588:
4589: sub get {
1.131 albertel 4590: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4591: my $items='';
1.800 albertel 4592: foreach my $item (@$storearr) {
4593: $items.=&escape($item).'&';
1.191 harris41 4594: }
1.12 www 4595: $items=~s/\&$//;
1.620 albertel 4596: if (!$udomain) { $udomain=$env{'user.domain'}; }
4597: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 4598: my $uhome=&homeserver($uname,$udomain);
4599:
1.133 albertel 4600: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4601: my @pairs=split(/\&/,$rep);
1.273 albertel 4602: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
4603: return @pairs;
4604: }
1.15 www 4605: my %returnhash=();
1.42 www 4606: my $i=0;
1.800 albertel 4607: foreach my $item (@$storearr) {
4608: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4609: $i++;
1.191 harris41 4610: }
1.15 www 4611: return %returnhash;
1.27 www 4612: }
4613:
4614: # --------------------------------------------------------------- del interface
4615:
4616: sub del {
1.133 albertel 4617: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 4618: my $items='';
1.800 albertel 4619: foreach my $item (@$storearr) {
4620: $items.=&escape($item).'&';
1.191 harris41 4621: }
1.984 neumanie 4622:
1.27 www 4623: $items=~s/\&$//;
1.620 albertel 4624: if (!$udomain) { $udomain=$env{'user.domain'}; }
4625: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4626: my $uhome=&homeserver($uname,$udomain);
4627: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4628: }
4629:
4630: # -------------------------------------------------------------- dump interface
4631:
4632: sub dump {
1.1056.4.10 raeburn 4633: my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755 albertel 4634: if (!$udomain) { $udomain=$env{'user.domain'}; }
4635: if (!$uname) { $uname=$env{'user.name'}; }
4636: my $uhome=&homeserver($uname,$udomain);
4637: if ($regexp) {
4638: $regexp=&escape($regexp);
4639: } else {
4640: $regexp='.';
4641: }
1.1056.4.10 raeburn 4642: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755 albertel 4643: my @pairs=split(/\&/,$rep);
4644: my %returnhash=();
4645: foreach my $item (@pairs) {
4646: my ($key,$value)=split(/=/,$item,2);
4647: $key = &unescape($key);
4648: next if ($key =~ /^error: 2 /);
4649: $returnhash{$key}=&thaw_unescape($value);
4650: }
4651: return %returnhash;
1.407 www 4652: }
4653:
1.717 albertel 4654: # --------------------------------------------------------- dumpstore interface
4655:
4656: sub dumpstore {
4657: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 4658: if (!$udomain) { $udomain=$env{'user.domain'}; }
4659: if (!$uname) { $uname=$env{'user.name'}; }
4660: my $uhome=&homeserver($uname,$udomain);
4661: if ($regexp) {
4662: $regexp=&escape($regexp);
4663: } else {
4664: $regexp='.';
4665: }
4666: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
4667: my @pairs=split(/\&/,$rep);
4668: my %returnhash=();
4669: foreach my $item (@pairs) {
4670: my ($key,$value)=split(/=/,$item,2);
4671: next if ($key =~ /^error: 2 /);
4672: $returnhash{$key}=&thaw_unescape($value);
4673: }
4674: return %returnhash;
1.717 albertel 4675: }
4676:
1.407 www 4677: # -------------------------------------------------------------- keys interface
4678:
4679: sub getkeys {
4680: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 4681: if (!$udomain) { $udomain=$env{'user.domain'}; }
4682: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 4683: my $uhome=&homeserver($uname,$udomain);
4684: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
4685: my @keyarray=();
1.800 albertel 4686: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 4687: next if ($key =~ /^error: 2 /);
1.800 albertel 4688: push(@keyarray,&unescape($key));
1.407 www 4689: }
4690: return @keyarray;
1.318 matthew 4691: }
4692:
1.319 matthew 4693: # --------------------------------------------------------------- currentdump
4694: sub currentdump {
1.328 matthew 4695: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 4696: $courseid = $env{'request.course.id'} if (! defined($courseid));
4697: $sdom = $env{'user.domain'} if (! defined($sdom));
4698: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 4699: my $uhome = &homeserver($sname,$sdom);
4700: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 4701: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 4702: #
1.318 matthew 4703: my %returnhash=();
1.319 matthew 4704: #
4705: if ($rep eq "unknown_cmd") {
4706: # an old lond will not know currentdump
4707: # Do a dump and make it look like a currentdump
1.822 albertel 4708: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 4709: return if ($tmp[0] =~ /^(error:|no_such_host)/);
4710: my %hash = @tmp;
4711: @tmp=();
1.424 matthew 4712: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 4713: } else {
4714: my @pairs=split(/\&/,$rep);
1.800 albertel 4715: foreach my $pair (@pairs) {
4716: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 4717: my ($symb,$param) = split(/:/,$key);
4718: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 4719: &thaw_unescape($value);
1.319 matthew 4720: }
1.191 harris41 4721: }
1.12 www 4722: return %returnhash;
1.424 matthew 4723: }
4724:
4725: sub convert_dump_to_currentdump{
4726: my %hash = %{shift()};
4727: my %returnhash;
4728: # Code ripped from lond, essentially. The only difference
4729: # here is the unescaping done by lonnet::dump(). Conceivably
4730: # we might run in to problems with parameter names =~ /^v\./
4731: while (my ($key,$value) = each(%hash)) {
4732: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 4733: $symb = &unescape($symb);
4734: $param = &unescape($param);
1.424 matthew 4735: next if ($v eq 'version' || $symb eq 'keys');
4736: next if (exists($returnhash{$symb}) &&
4737: exists($returnhash{$symb}->{$param}) &&
4738: $returnhash{$symb}->{'v.'.$param} > $v);
4739: $returnhash{$symb}->{$param}=$value;
4740: $returnhash{$symb}->{'v.'.$param}=$v;
4741: }
4742: #
4743: # Remove all of the keys in the hashes which keep track of
4744: # the version of the parameter.
4745: while (my ($symb,$param_hash) = each(%returnhash)) {
4746: # use a foreach because we are going to delete from the hash.
4747: foreach my $key (keys(%$param_hash)) {
4748: delete($param_hash->{$key}) if ($key =~ /^v\./);
4749: }
4750: }
4751: return \%returnhash;
1.12 www 4752: }
4753:
1.627 albertel 4754: # ------------------------------------------------------ critical inc interface
4755:
4756: sub cinc {
4757: return &inc(@_,'critical');
4758: }
4759:
1.449 matthew 4760: # --------------------------------------------------------------- inc interface
4761:
4762: sub inc {
1.627 albertel 4763: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 4764: if (!$udomain) { $udomain=$env{'user.domain'}; }
4765: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 4766: my $uhome=&homeserver($uname,$udomain);
4767: my $items='';
4768: if (! ref($store)) {
4769: # got a single value, so use that instead
4770: $items = &escape($store).'=&';
4771: } elsif (ref($store) eq 'SCALAR') {
4772: $items = &escape($$store).'=&';
4773: } elsif (ref($store) eq 'ARRAY') {
4774: $items = join('=&',map {&escape($_);} @{$store});
4775: } elsif (ref($store) eq 'HASH') {
4776: while (my($key,$value) = each(%{$store})) {
4777: $items.= &escape($key).'='.&escape($value).'&';
4778: }
4779: }
4780: $items=~s/\&$//;
1.627 albertel 4781: if ($critical) {
4782: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
4783: } else {
4784: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
4785: }
1.449 matthew 4786: }
4787:
1.12 www 4788: # --------------------------------------------------------------- put interface
4789:
4790: sub put {
1.134 albertel 4791: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4792: if (!$udomain) { $udomain=$env{'user.domain'}; }
4793: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4794: my $uhome=&homeserver($uname,$udomain);
1.12 www 4795: my $items='';
1.800 albertel 4796: foreach my $item (keys(%$storehash)) {
4797: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4798: }
1.12 www 4799: $items=~s/\&$//;
1.134 albertel 4800: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 4801: }
4802:
1.631 albertel 4803: # ------------------------------------------------------------ newput interface
4804:
4805: sub newput {
4806: my ($namespace,$storehash,$udomain,$uname)=@_;
4807: if (!$udomain) { $udomain=$env{'user.domain'}; }
4808: if (!$uname) { $uname=$env{'user.name'}; }
4809: my $uhome=&homeserver($uname,$udomain);
4810: my $items='';
4811: foreach my $key (keys(%$storehash)) {
4812: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
4813: }
4814: $items=~s/\&$//;
4815: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
4816: }
4817:
4818: # --------------------------------------------------------- putstore interface
4819:
1.524 raeburn 4820: sub putstore {
1.715 albertel 4821: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 4822: if (!$udomain) { $udomain=$env{'user.domain'}; }
4823: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 4824: my $uhome=&homeserver($uname,$udomain);
4825: my $items='';
1.715 albertel 4826: foreach my $key (keys(%$storehash)) {
4827: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 4828: }
1.715 albertel 4829: $items=~s/\&$//;
1.716 albertel 4830: my $esc_symb=&escape($symb);
4831: my $esc_v=&escape($version);
1.715 albertel 4832: my $reply =
1.716 albertel 4833: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 4834: $uhome);
4835: if ($reply eq 'unknown_cmd') {
1.716 albertel 4836: # gfall back to way things use to be done
1.715 albertel 4837: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
4838: $uname);
1.524 raeburn 4839: }
1.715 albertel 4840: return $reply;
4841: }
4842:
4843: sub old_putstore {
1.716 albertel 4844: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
4845: if (!$udomain) { $udomain=$env{'user.domain'}; }
4846: if (!$uname) { $uname=$env{'user.name'}; }
4847: my $uhome=&homeserver($uname,$udomain);
4848: my %newstorehash;
1.800 albertel 4849: foreach my $item (keys(%$storehash)) {
4850: my $key = $version.':'.&escape($symb).':'.$item;
4851: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 4852: }
4853: my $items='';
4854: my %allitems = ();
1.800 albertel 4855: foreach my $item (keys(%newstorehash)) {
4856: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 4857: my $key = $1.':keys:'.$2;
4858: $allitems{$key} .= $3.':';
4859: }
1.800 albertel 4860: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 4861: }
1.800 albertel 4862: foreach my $item (keys(%allitems)) {
4863: $allitems{$item} =~ s/\:$//;
4864: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 4865: }
4866: $items=~s/\&$//;
4867: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 4868: }
4869:
1.47 www 4870: # ------------------------------------------------------ critical put interface
4871:
4872: sub cput {
1.134 albertel 4873: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4874: if (!$udomain) { $udomain=$env{'user.domain'}; }
4875: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4876: my $uhome=&homeserver($uname,$udomain);
1.47 www 4877: my $items='';
1.800 albertel 4878: foreach my $item (keys(%$storehash)) {
4879: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4880: }
1.47 www 4881: $items=~s/\&$//;
1.134 albertel 4882: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4883: }
4884:
4885: # -------------------------------------------------------------- eget interface
4886:
4887: sub eget {
1.133 albertel 4888: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4889: my $items='';
1.800 albertel 4890: foreach my $item (@$storearr) {
4891: $items.=&escape($item).'&';
1.191 harris41 4892: }
1.12 www 4893: $items=~s/\&$//;
1.620 albertel 4894: if (!$udomain) { $udomain=$env{'user.domain'}; }
4895: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4896: my $uhome=&homeserver($uname,$udomain);
4897: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4898: my @pairs=split(/\&/,$rep);
4899: my %returnhash=();
1.42 www 4900: my $i=0;
1.800 albertel 4901: foreach my $item (@$storearr) {
4902: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4903: $i++;
1.191 harris41 4904: }
1.12 www 4905: return %returnhash;
4906: }
4907:
1.667 albertel 4908: # ------------------------------------------------------------ tmpput interface
4909: sub tmpput {
1.802 raeburn 4910: my ($storehash,$server,$context)=@_;
1.667 albertel 4911: my $items='';
1.800 albertel 4912: foreach my $item (keys(%$storehash)) {
4913: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 4914: }
4915: $items=~s/\&$//;
1.802 raeburn 4916: if (defined($context)) {
4917: $items .= ':'.&escape($context);
4918: }
1.667 albertel 4919: return &reply("tmpput:$items",$server);
4920: }
4921:
4922: # ------------------------------------------------------------ tmpget interface
4923: sub tmpget {
1.688 albertel 4924: my ($token,$server)=@_;
4925: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4926: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 4927: my %returnhash;
4928: foreach my $item (split(/\&/,$rep)) {
4929: my ($key,$value)=split(/=/,$item);
1.951 raeburn 4930: next if ($key =~ /^error: 2 /);
1.667 albertel 4931: $returnhash{&unescape($key)}=&thaw_unescape($value);
4932: }
4933: return %returnhash;
4934: }
4935:
1.688 albertel 4936: # ------------------------------------------------------------ tmpget interface
4937: sub tmpdel {
4938: my ($token,$server)=@_;
4939: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4940: return &reply("tmpdel:$token",$server);
4941: }
4942:
1.765 albertel 4943: # -------------------------------------------------- portfolio access checking
4944:
4945: sub portfolio_access {
1.766 albertel 4946: my ($requrl) = @_;
1.765 albertel 4947: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
4948: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 4949: if ($result) {
4950: my %setters;
4951: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4952: my ($startblock,$endblock) =
4953: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
4954: if ($startblock && $endblock) {
4955: return 'B';
4956: }
4957: } else {
4958: my ($startblock,$endblock) =
4959: &Apache::loncommon::blockcheck(\%setters,'port');
4960: if ($startblock && $endblock) {
4961: return 'B';
4962: }
4963: }
4964: }
1.765 albertel 4965: if ($result eq 'ok') {
1.766 albertel 4966: return 'F';
1.765 albertel 4967: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 4968: return 'A';
1.765 albertel 4969: }
1.766 albertel 4970: return '';
1.765 albertel 4971: }
4972:
4973: sub get_portfolio_access {
1.767 albertel 4974: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
4975:
4976: if (!ref($access_hash)) {
4977: my $current_perms = &get_portfile_permissions($udom,$unum);
4978: my %access_controls = &get_access_controls($current_perms,$group,
4979: $file_name);
4980: $access_hash = $access_controls{$file_name};
4981: }
4982:
1.765 albertel 4983: my ($public,$guest,@domains,@users,@courses,@groups);
4984: my $now = time;
4985: if (ref($access_hash) eq 'HASH') {
4986: foreach my $key (keys(%{$access_hash})) {
4987: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
4988: if ($start > $now) {
4989: next;
4990: }
4991: if ($end && $end<$now) {
4992: next;
4993: }
4994: if ($scope eq 'public') {
4995: $public = $key;
4996: last;
4997: } elsif ($scope eq 'guest') {
4998: $guest = $key;
4999: } elsif ($scope eq 'domains') {
5000: push(@domains,$key);
5001: } elsif ($scope eq 'users') {
5002: push(@users,$key);
5003: } elsif ($scope eq 'course') {
5004: push(@courses,$key);
5005: } elsif ($scope eq 'group') {
5006: push(@groups,$key);
5007: }
5008: }
5009: if ($public) {
5010: return 'ok';
5011: }
5012: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
5013: if ($guest) {
5014: return $guest;
5015: }
5016: } else {
5017: if (@domains > 0) {
5018: foreach my $domkey (@domains) {
5019: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
5020: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
5021: return 'ok';
5022: }
5023: }
5024: }
5025: }
5026: if (@users > 0) {
5027: foreach my $userkey (@users) {
1.865 raeburn 5028: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
5029: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
5030: if (ref($item) eq 'HASH') {
5031: if (($item->{'uname'} eq $env{'user.name'}) &&
5032: ($item->{'udom'} eq $env{'user.domain'})) {
5033: return 'ok';
5034: }
5035: }
5036: }
5037: }
1.765 albertel 5038: }
5039: }
5040: my %roleshash;
5041: my @courses_and_groups = @courses;
5042: push(@courses_and_groups,@groups);
5043: if (@courses_and_groups > 0) {
5044: my (%allgroups,%allroles);
5045: my ($start,$end,$role,$sec,$group);
5046: foreach my $envkey (%env) {
1.1056.4.1 raeburn 5047: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5048: my $cid = $2.'_'.$3;
5049: if ($1 eq 'gr') {
5050: $group = $4;
5051: $allgroups{$cid}{$group} = $env{$envkey};
5052: } else {
5053: if ($4 eq '') {
5054: $sec = 'none';
5055: } else {
5056: $sec = $4;
5057: }
5058: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5059: }
1.811 albertel 5060: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5061: my $cid = $2.'_'.$3;
5062: if ($4 eq '') {
5063: $sec = 'none';
5064: } else {
5065: $sec = $4;
5066: }
5067: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5068: }
5069: }
5070: if (keys(%allroles) == 0) {
5071: return;
5072: }
5073: foreach my $key (@courses_and_groups) {
5074: my %content = %{$$access_hash{$key}};
5075: my $cnum = $content{'number'};
5076: my $cdom = $content{'domain'};
5077: my $cid = $cdom.'_'.$cnum;
5078: if (!exists($allroles{$cid})) {
5079: next;
5080: }
5081: foreach my $role_id (keys(%{$content{'roles'}})) {
5082: my @sections = @{$content{'roles'}{$role_id}{'section'}};
5083: my @groups = @{$content{'roles'}{$role_id}{'group'}};
5084: my @status = @{$content{'roles'}{$role_id}{'access'}};
5085: my @roles = @{$content{'roles'}{$role_id}{'role'}};
5086: foreach my $role (keys(%{$allroles{$cid}})) {
5087: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
5088: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
5089: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
5090: if (grep/^all$/,@sections) {
5091: return 'ok';
5092: } else {
5093: if (grep/^$sec$/,@sections) {
5094: return 'ok';
5095: }
5096: }
5097: }
5098: }
5099: if (keys(%{$allgroups{$cid}}) == 0) {
5100: if (grep/^none$/,@groups) {
5101: return 'ok';
5102: }
5103: } else {
5104: if (grep/^all$/,@groups) {
5105: return 'ok';
5106: }
5107: foreach my $group (keys(%{$allgroups{$cid}})) {
5108: if (grep/^$group$/,@groups) {
5109: return 'ok';
5110: }
5111: }
5112: }
5113: }
5114: }
5115: }
5116: }
5117: }
5118: if ($guest) {
5119: return $guest;
5120: }
5121: }
5122: }
5123: return;
5124: }
5125:
5126: sub course_group_datechecker {
5127: my ($dates,$now,$status) = @_;
5128: my ($start,$end) = split(/\./,$dates);
5129: if (!$start && !$end) {
5130: return 'ok';
5131: }
5132: if (grep/^active$/,@{$status}) {
5133: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
5134: return 'ok';
5135: }
5136: }
5137: if (grep/^previous$/,@{$status}) {
5138: if ($end > $now ) {
5139: return 'ok';
5140: }
5141: }
5142: if (grep/^future$/,@{$status}) {
5143: if ($start > $now) {
5144: return 'ok';
5145: }
5146: }
5147: return;
5148: }
5149:
5150: sub parse_portfolio_url {
5151: my ($url) = @_;
5152:
5153: my ($type,$udom,$unum,$group,$file_name);
5154:
1.823 albertel 5155: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 5156: $type = 1;
5157: $udom = $1;
5158: $unum = $2;
5159: $file_name = $3;
1.823 albertel 5160: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 5161: $type = 2;
5162: $udom = $1;
5163: $unum = $2;
5164: $group = $3;
5165: $file_name = $3.'/'.$4;
5166: }
5167: if (wantarray) {
5168: return ($type,$udom,$unum,$file_name,$group);
5169: }
5170: return $type;
5171: }
5172:
5173: sub is_portfolio_url {
5174: my ($url) = @_;
5175: return scalar(&parse_portfolio_url($url));
5176: }
5177:
1.798 raeburn 5178: sub is_portfolio_file {
5179: my ($file) = @_;
1.820 raeburn 5180: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 5181: return 1;
5182: }
5183: return;
5184: }
5185:
1.976 raeburn 5186: sub usertools_access {
1.1056.4.9 raeburn 5187: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref) = @_;
1.985 raeburn 5188: my ($access,%tools);
5189: if ($context eq '') {
5190: $context = 'tools';
5191: }
5192: if ($context eq 'requestcourses') {
5193: %tools = (
5194: official => 1,
5195: unofficial => 1,
1.1006 raeburn 5196: community => 1,
1.985 raeburn 5197: );
5198: } else {
5199: %tools = (
5200: aboutme => 1,
5201: blog => 1,
5202: portfolio => 1,
5203: );
5204: }
1.976 raeburn 5205: return if (!defined($tools{$tool}));
5206:
5207: if ((!defined($udom)) || (!defined($uname))) {
5208: $udom = $env{'user.domain'};
5209: $uname = $env{'user.name'};
5210: }
5211:
1.978 raeburn 5212: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5213: if ($action ne 'reload') {
1.985 raeburn 5214: if ($context eq 'requestcourses') {
5215: return $env{'environment.canrequest.'.$tool};
5216: } else {
5217: return $env{'environment.availabletools.'.$tool};
5218: }
5219: }
1.976 raeburn 5220: }
5221:
5222: my ($toolstatus,$inststatus);
5223:
1.985 raeburn 5224: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
5225: ($action ne 'reload')) {
5226: $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976 raeburn 5227: $inststatus = $env{'environment.inststatus'};
5228: } else {
1.1056.4.9 raeburn 5229: if (ref($userenvref) eq 'HASH') {
5230: $toolstatus = $userenvref->{$context.'.'.$tool};
5231: $inststatus = $userenvref->{'inststatus'};
5232: } else {
5233: my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
5234: $toolstatus = $userenv{$context.'.'.$tool};
5235: $inststatus = $userenv{'inststatus'};
5236: }
1.976 raeburn 5237: }
5238:
5239: if ($toolstatus ne '') {
5240: if ($toolstatus) {
5241: $access = 1;
5242: } else {
5243: $access = 0;
5244: }
5245: return $access;
5246: }
5247:
1.1056.4.9 raeburn 5248: my ($is_adv,%domdef);
5249: if (ref($is_advref) eq 'HASH') {
5250: $is_adv = $is_advref->{'is_adv'};
5251: } else {
5252: $is_adv = &is_advanced_user($udom,$uname);
5253: }
5254: if (ref($domdefref) eq 'HASH') {
5255: %domdef = %{$domdefref};
5256: } else {
5257: %domdef = &get_domain_defaults($udom);
5258: }
1.976 raeburn 5259: if (ref($domdef{$tool}) eq 'HASH') {
5260: if ($is_adv) {
5261: if ($domdef{$tool}{'_LC_adv'} ne '') {
5262: if ($domdef{$tool}{'_LC_adv'}) {
5263: $access = 1;
5264: } else {
5265: $access = 0;
5266: }
5267: return $access;
5268: }
5269: }
5270: if ($inststatus ne '') {
5271: my ($hasaccess,$hasnoaccess);
5272: foreach my $affiliation (split(/:/,$inststatus)) {
5273: if ($domdef{$tool}{$affiliation} ne '') {
5274: if ($domdef{$tool}{$affiliation}) {
5275: $hasaccess = 1;
5276: } else {
5277: $hasnoaccess = 1;
5278: }
5279: }
5280: }
5281: if ($hasaccess || $hasnoaccess) {
5282: if ($hasaccess) {
5283: $access = 1;
5284: } elsif ($hasnoaccess) {
5285: $access = 0;
5286: }
5287: return $access;
5288: }
5289: } else {
5290: if ($domdef{$tool}{'default'} ne '') {
5291: if ($domdef{$tool}{'default'}) {
5292: $access = 1;
5293: } elsif ($domdef{$tool}{'default'} == 0) {
5294: $access = 0;
5295: }
5296: return $access;
5297: }
5298: }
5299: } else {
1.985 raeburn 5300: if ($context eq 'tools') {
5301: $access = 1;
5302: } else {
5303: $access = 0;
5304: }
1.976 raeburn 5305: return $access;
5306: }
5307: }
5308:
1.1050 raeburn 5309: sub is_course_owner {
5310: my ($cdom,$cnum,$udom,$uname) = @_;
5311: if (($udom eq '') || ($uname eq '')) {
5312: $udom = $env{'user.domain'};
5313: $uname = $env{'user.name'};
5314: }
5315: unless (($udom eq '') || ($uname eq '')) {
5316: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
5317: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
5318: return 1;
5319: } else {
5320: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
5321: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
5322: return 1;
5323: }
5324: }
5325: }
5326: }
5327: return;
5328: }
5329:
1.976 raeburn 5330: sub is_advanced_user {
5331: my ($udom,$uname) = @_;
1.1056.4.10 raeburn 5332: if ($udom ne '' && $uname ne '') {
5333: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5334: return $env{'user.adv'};
5335: }
5336: }
1.976 raeburn 5337: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
5338: my %allroles;
5339: my $is_adv;
5340: foreach my $role (keys(%roleshash)) {
5341: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
5342: my $area = '/'.$tdomain.'/'.$trest;
5343: if ($sec ne '') {
5344: $area .= '/'.$sec;
5345: }
5346: if (($area ne '') && ($trole ne '')) {
5347: my $spec=$trole.'.'.$area;
5348: if ($trole =~ /^cr\//) {
5349: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5350: } elsif ($trole ne 'gr') {
5351: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5352: }
5353: }
5354: }
5355: foreach my $role (keys(%allroles)) {
5356: last if ($is_adv);
5357: foreach my $item (split(/:/,$allroles{$role})) {
5358: if ($item ne '') {
5359: my ($privilege,$restrictions)=split(/&/,$item);
5360: if ($privilege eq 'adv') {
5361: $is_adv = 1;
5362: last;
5363: }
5364: }
5365: }
5366: }
5367: return $is_adv;
5368: }
1.798 raeburn 5369:
1.1035 raeburn 5370: sub check_can_request {
1.1036 raeburn 5371: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 5372: my $canreq = 0;
5373: my ($types,$typename) = &Apache::loncommon::course_types();
5374: my @options = ('approval','validate','autolimit');
5375: my $optregex = join('|',@options);
5376: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
5377: foreach my $type (@{$types}) {
5378: if (&usertools_access($env{'user.name'},
5379: $env{'user.domain'},
5380: $type,undef,'requestcourses')) {
5381: $canreq ++;
1.1036 raeburn 5382: if (ref($request_domains) eq 'HASH') {
5383: push(@{$request_domains->{$type}},$env{'user.domain'});
5384: }
1.1035 raeburn 5385: if ($dom eq $env{'user.domain'}) {
5386: $can_request->{$type} = 1;
5387: }
5388: }
5389: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
5390: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
5391: if (@curr > 0) {
1.1036 raeburn 5392: foreach my $item (@curr) {
5393: if (ref($request_domains) eq 'HASH') {
5394: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
5395: if ($otherdom ne '') {
5396: if (ref($request_domains->{$type}) eq 'ARRAY') {
5397: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
5398: push(@{$request_domains->{$type}},$otherdom);
5399: }
5400: } else {
5401: push(@{$request_domains->{$type}},$otherdom);
5402: }
5403: }
5404: }
5405: }
5406: unless($dom eq $env{'user.domain'}) {
5407: $canreq ++;
1.1035 raeburn 5408: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
5409: $can_request->{$type} = 1;
5410: }
5411: }
5412: }
5413: }
5414: }
5415: }
5416: return $canreq;
5417: }
5418:
1.341 www 5419: # ---------------------------------------------- Custom access rule evaluation
5420:
5421: sub customaccess {
5422: my ($priv,$uri)=@_;
1.807 albertel 5423: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 5424: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 5425: $udom = &LONCAPA::clean_domain($udom);
5426: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 5427: my $access=0;
1.800 albertel 5428: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 5429: my ($effect,$realm,$role,$type)=split(/\:/,$right);
5430: if ($type eq 'user') {
5431: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 5432: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 5433: if ($tdom) {
5434: if ($tdom ne $env{'user.domain'}) { next; }
5435: }
1.896 albertel 5436: if ($tuname) {
5437: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 5438: }
5439: $access=($effect eq 'allow');
5440: last;
5441: }
5442: } else {
5443: if ($role) {
5444: if ($role ne $urole) { next; }
5445: }
5446: foreach my $scope (split(/\s*\,\s*/,$realm)) {
5447: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
5448: if ($tdom) {
5449: if ($tdom ne $udom) { next; }
5450: }
5451: if ($tcrs) {
5452: if ($tcrs ne $ucrs) { next; }
5453: }
5454: if ($tsec) {
5455: if ($tsec ne $usec) { next; }
5456: }
5457: $access=($effect eq 'allow');
5458: last;
5459: }
5460: if ($realm eq '' && $role eq '') {
5461: $access=($effect eq 'allow');
5462: }
1.402 bowersj2 5463: }
1.341 www 5464: }
5465: return $access;
5466: }
5467:
1.103 harris41 5468: # ------------------------------------------------- Check for a user privilege
1.12 www 5469:
5470: sub allowed {
1.810 raeburn 5471: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 5472: my $ver_orguri=$uri;
1.439 www 5473: $uri=&deversion($uri);
1.152 www 5474: my $orguri=$uri;
1.52 www 5475: $uri=&declutter($uri);
1.809 raeburn 5476:
1.810 raeburn 5477: if ($priv eq 'evb') {
5478: # Evade communication block restrictions for specified role in a course
5479: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
5480: return $1;
5481: } else {
5482: return;
5483: }
5484: }
5485:
1.620 albertel 5486: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 5487: # Free bre access to adm and meta resources
1.775 albertel 5488: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 5489: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
5490: && ($priv eq 'bre')) {
1.14 www 5491: return 'F';
1.159 www 5492: }
5493:
1.545 banghart 5494: # Free bre access to user's own portfolio contents
1.714 raeburn 5495: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 5496: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 5497: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 5498: my %setters;
5499: my ($startblock,$endblock) =
5500: &Apache::loncommon::blockcheck(\%setters,'port');
5501: if ($startblock && $endblock) {
5502: return 'B';
5503: } else {
5504: return 'F';
5505: }
1.545 banghart 5506: }
5507:
1.762 raeburn 5508: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 5509: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
5510: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
5511: if (exists($env{'request.course.id'})) {
5512: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
5513: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
5514: if (($domain eq $cdom) && ($name eq $cnum)) {
5515: my $courseprivid=$env{'request.course.id'};
5516: $courseprivid=~s/\_/\//;
5517: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
5518: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
5519: return $1;
1.762 raeburn 5520: } else {
5521: if ($env{'request.course.sec'}) {
5522: $courseprivid.='/'.$env{'request.course.sec'};
5523: }
5524: if ($env{'user.priv.'.$env{'request.role'}.'./'.
5525: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
5526: return $2;
5527: }
1.714 raeburn 5528: }
5529: }
5530: }
5531: }
5532:
1.159 www 5533: # Free bre to public access
5534:
5535: if ($priv eq 'bre') {
1.238 www 5536: my $copyright=&metadata($uri,'copyright');
1.620 albertel 5537: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 5538: return 'F';
5539: }
1.238 www 5540: if ($copyright eq 'priv') {
5541: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5542: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 5543: return '';
5544: }
5545: }
5546: if ($copyright eq 'domain') {
5547: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5548: unless (($env{'user.domain'} eq $1) ||
5549: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 5550: return '';
5551: }
1.262 matthew 5552: }
1.620 albertel 5553: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 5554: # Library role, so allow browsing of resources in this domain.
5555: return 'F';
1.238 www 5556: }
1.341 www 5557: if ($copyright eq 'custom') {
5558: unless (&customaccess($priv,$uri)) { return ''; }
5559: }
1.14 www 5560: }
1.264 matthew 5561: # Domain coordinator is trying to create a course
1.620 albertel 5562: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 5563: # uri is the requested domain in this case.
5564: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 5565: # a role of dc for the domain in question.
1.620 albertel 5566: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 5567: }
1.29 www 5568:
1.52 www 5569: my $thisallowed='';
5570: my $statecond=0;
5571: my $courseprivid='';
5572:
1.1039 raeburn 5573: my $ownaccess;
1.1043 raeburn 5574: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 5575: if (($priv eq 'bro') && ($env{'user.author'})) {
5576: if ($uri eq '') {
5577: $ownaccess = 1;
5578: } else {
5579: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
5580: my $udom = $env{'user.domain'};
5581: my $uname = $env{'user.name'};
5582: if ($uri =~ m{^\Q$udom\E/?$}) {
5583: $ownaccess = 1;
1.1040 raeburn 5584: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 5585: unless ($uri =~ m{\.\./}) {
5586: $ownaccess = 1;
5587: }
5588: } elsif (($udom ne 'public') && ($uname ne 'public')) {
5589: my $now = time;
5590: if ($uri =~ m{^([^/]+)/?$}) {
5591: my $adom = $1;
5592: foreach my $key (keys(%env)) {
1.1042 raeburn 5593: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 5594: my ($start,$end) = split('.',$env{$key});
5595: if (($now >= $start) && (!$end || $end < $now)) {
5596: $ownaccess = 1;
5597: last;
5598: }
5599: }
5600: }
5601: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
5602: my $adom = $1;
5603: my $aname = $2;
1.1042 raeburn 5604: foreach my $role ('ca','aa') {
5605: if ($env{"user.role.$role./$adom/$aname"}) {
5606: my ($start,$end) =
5607: split('.',$env{"user.role.$role./$adom/$aname"});
5608: if (($now >= $start) && (!$end || $end < $now)) {
5609: $ownaccess = 1;
5610: last;
5611: }
1.1039 raeburn 5612: }
5613: }
5614: }
5615: }
5616: }
5617: }
5618: }
5619:
1.52 www 5620: # Course
5621:
1.620 albertel 5622: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5623: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5624: $thisallowed.=$1;
5625: }
1.52 www 5626: }
1.29 www 5627:
1.52 www 5628: # Domain
5629:
1.620 albertel 5630: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 5631: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5632: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5633: $thisallowed.=$1;
5634: }
1.12 www 5635: }
1.52 www 5636:
5637: # Course: uri itself is a course
1.66 www 5638: my $courseuri=$uri;
5639: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 5640: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 5641:
1.620 albertel 5642: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 5643: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5644: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5645: $thisallowed.=$1;
5646: }
1.12 www 5647: }
1.29 www 5648:
1.665 albertel 5649: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 5650: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 5651: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 5652: $thisallowed='';
1.671 raeburn 5653: my ($match)=&is_on_map($uri);
5654: if ($match) {
5655: if ($env{'user.priv.'.$env{'request.role'}.'./'}
5656: =~/\Q$priv\E\&([^\:]*)/) {
5657: $thisallowed.=$1;
5658: }
5659: } else {
1.705 albertel 5660: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 5661: if ($refuri) {
5662: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 5663: $thisallowed='F';
1.671 raeburn 5664: } else {
5665: $refuri=&declutter($refuri);
5666: my ($match) = &is_on_map($refuri);
5667: if ($match) {
5668: $thisallowed='F';
5669: }
1.669 raeburn 5670: }
1.671 raeburn 5671: }
5672: }
1.314 www 5673: }
1.492 albertel 5674:
1.766 albertel 5675: if ($priv eq 'bre'
5676: && $thisallowed ne 'F'
5677: && $thisallowed ne '2'
5678: && &is_portfolio_url($uri)) {
5679: $thisallowed = &portfolio_access($uri);
5680: }
5681:
1.52 www 5682: # Full access at system, domain or course-wide level? Exit.
1.29 www 5683: if ($thisallowed=~/F/) {
5684: return 'F';
5685: }
5686:
1.52 www 5687: # If this is generating or modifying users, exit with special codes
1.29 www 5688:
1.643 www 5689: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
5690: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 5691: my ($audom,$auname)=split('/',$uri);
1.643 www 5692: # no author name given, so this just checks on the general right to make a co-author in this domain
5693: unless ($auname) { return $thisallowed; }
5694: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 5695: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
5696: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
5697: ($audom ne $env{'request.role.domain'}))) { return ''; }
5698: }
1.52 www 5699: return $thisallowed;
5700: }
5701: #
1.103 harris41 5702: # Gathered so far: system, domain and course wide privileges
1.52 www 5703: #
5704: # Course: See if uri or referer is an individual resource that is part of
5705: # the course
5706:
1.620 albertel 5707: if ($env{'request.course.id'}) {
1.232 www 5708:
1.620 albertel 5709: $courseprivid=$env{'request.course.id'};
5710: if ($env{'request.course.sec'}) {
5711: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 5712: }
5713: $courseprivid=~s/\_/\//;
5714: my $checkreferer=1;
1.232 www 5715: my ($match,$cond)=&is_on_map($uri);
5716: if ($match) {
5717: $statecond=$cond;
1.620 albertel 5718: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5719: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5720: $thisallowed.=$1;
5721: $checkreferer=0;
5722: }
1.29 www 5723: }
1.83 www 5724:
1.148 www 5725: if ($checkreferer) {
1.620 albertel 5726: my $refuri=$env{'httpref.'.$orguri};
1.148 www 5727: unless ($refuri) {
1.800 albertel 5728: foreach my $key (keys(%env)) {
5729: if ($key=~/^httpref\..*\*/) {
5730: my $pattern=$key;
1.156 www 5731: $pattern=~s/^httpref\.\/res\///;
1.148 www 5732: $pattern=~s/\*/\[\^\/\]\+/g;
5733: $pattern=~s/\//\\\//g;
1.152 www 5734: if ($orguri=~/$pattern/) {
1.800 albertel 5735: $refuri=$env{$key};
1.148 www 5736: }
5737: }
1.191 harris41 5738: }
1.148 www 5739: }
1.232 www 5740:
1.148 www 5741: if ($refuri) {
1.152 www 5742: $refuri=&declutter($refuri);
1.232 www 5743: my ($match,$cond)=&is_on_map($refuri);
5744: if ($match) {
5745: my $refstatecond=$cond;
1.620 albertel 5746: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5747: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5748: $thisallowed.=$1;
1.53 www 5749: $uri=$refuri;
5750: $statecond=$refstatecond;
1.52 www 5751: }
5752: }
1.148 www 5753: }
1.29 www 5754: }
1.52 www 5755: }
1.29 www 5756:
1.52 www 5757: #
1.103 harris41 5758: # Gathered now: all privileges that could apply, and condition number
1.52 www 5759: #
5760: #
5761: # Full or no access?
5762: #
1.29 www 5763:
1.52 www 5764: if ($thisallowed=~/F/) {
5765: return 'F';
5766: }
1.29 www 5767:
1.52 www 5768: unless ($thisallowed) {
5769: return '';
5770: }
1.29 www 5771:
1.52 www 5772: # Restrictions exist, deal with them
5773: #
5774: # C:according to course preferences
5775: # R:according to resource settings
5776: # L:unless locked
5777: # X:according to user session state
5778: #
5779:
5780: # Possibly locked functionality, check all courses
1.54 www 5781: # Locks might take effect only after 10 minutes cache expiration for other
5782: # courses, and 2 minutes for current course
1.52 www 5783:
5784: my $envkey;
5785: if ($thisallowed=~/L/) {
1.1000 raeburn 5786: foreach $envkey (keys(%env)) {
1.54 www 5787: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
5788: my $courseid=$2;
5789: my $roleid=$1.'.'.$2;
1.92 www 5790: $courseid=~s/^\///;
1.54 www 5791: my $expiretime=600;
1.620 albertel 5792: if ($env{'request.role'} eq $roleid) {
1.54 www 5793: $expiretime=120;
5794: }
5795: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
5796: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 5797: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 5798: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 5799: }
1.620 albertel 5800: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
5801: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
5802: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
5803: &log($env{'user.domain'},$env{'user.name'},
5804: $env{'user.home'},
1.57 www 5805: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 5806: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5807: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5808: return '';
5809: }
5810: }
1.620 albertel 5811: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
5812: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
5813: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
5814: &log($env{'user.domain'},$env{'user.name'},
5815: $env{'user.home'},
1.57 www 5816: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 5817: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5818: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5819: return '';
5820: }
5821: }
5822: }
1.29 www 5823: }
1.52 www 5824: }
5825:
5826: #
5827: # Rest of the restrictions depend on selected course
5828: #
5829:
1.620 albertel 5830: unless ($env{'request.course.id'}) {
1.766 albertel 5831: if ($thisallowed eq 'A') {
5832: return 'A';
1.814 raeburn 5833: } elsif ($thisallowed eq 'B') {
5834: return 'B';
1.766 albertel 5835: } else {
5836: return '1';
5837: }
1.52 www 5838: }
1.29 www 5839:
1.52 www 5840: #
5841: # Now user is definitely in a course
5842: #
1.53 www 5843:
5844:
5845: # Course preferences
5846:
5847: if ($thisallowed=~/C/) {
1.620 albertel 5848: my $rolecode=(split(/\./,$env{'request.role'}))[0];
5849: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
5850: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 5851: =~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5852: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5853: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5854: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
5855: $env{'request.course.id'});
5856: }
1.237 www 5857: return '';
5858: }
5859:
1.620 albertel 5860: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 5861: =~/\Q$unamedom\E/) {
1.1056.4.22 raeburn 5862: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5863: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
5864: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
5865: $env{'request.course.id'});
5866: }
1.54 www 5867: return '';
5868: }
1.53 www 5869: }
5870:
5871: # Resource preferences
5872:
5873: if ($thisallowed=~/R/) {
1.620 albertel 5874: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 5875: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5876: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5877: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5878: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
5879: }
5880: return '';
1.54 www 5881: }
1.53 www 5882: }
1.30 www 5883:
1.246 www 5884: # Restricted by state or randomout?
1.30 www 5885:
1.52 www 5886: if ($thisallowed=~/X/) {
1.620 albertel 5887: if ($env{'acc.randomout'}) {
1.579 albertel 5888: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 5889: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 5890: return '';
5891: }
1.247 www 5892: }
5893: if (&condval($statecond)) {
1.52 www 5894: return '2';
5895: } else {
5896: return '';
5897: }
5898: }
1.30 www 5899:
1.766 albertel 5900: if ($thisallowed eq 'A') {
5901: return 'A';
1.814 raeburn 5902: } elsif ($thisallowed eq 'B') {
5903: return 'B';
1.766 albertel 5904: }
1.52 www 5905: return 'F';
1.232 www 5906: }
5907:
1.710 albertel 5908: sub split_uri_for_cond {
5909: my $uri=&deversion(&declutter(shift));
5910: my @uriparts=split(/\//,$uri);
5911: my $filename=pop(@uriparts);
5912: my $pathname=join('/',@uriparts);
5913: return ($pathname,$filename);
5914: }
1.232 www 5915: # --------------------------------------------------- Is a resource on the map?
5916:
5917: sub is_on_map {
1.710 albertel 5918: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 5919: #Trying to find the conditional for the file
1.620 albertel 5920: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 5921: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 5922: if ($match) {
1.289 bowersj2 5923: return (1,$1);
5924: } else {
1.434 www 5925: return (0,0);
1.289 bowersj2 5926: }
1.12 www 5927: }
5928:
1.427 www 5929: # --------------------------------------------------------- Get symb from alias
5930:
5931: sub get_symb_from_alias {
5932: my $symb=shift;
5933: my ($map,$resid,$url)=&decode_symb($symb);
5934: # Already is a symb
5935: if ($url) { return $symb; }
5936: # Must be an alias
5937: my $aliassymb='';
5938: my %bighash;
1.620 albertel 5939: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 5940: &GDBM_READER(),0640)) {
5941: my $rid=$bighash{'mapalias_'.$symb};
5942: if ($rid) {
5943: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 5944: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
5945: $resid,$bighash{'src_'.$rid});
1.427 www 5946: }
5947: untie %bighash;
5948: }
5949: return $aliassymb;
5950: }
5951:
1.12 www 5952: # ----------------------------------------------------------------- Define Role
5953:
5954: sub definerole {
5955: if (allowed('mcr','/')) {
5956: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 5957: foreach my $role (split(':',$sysrole)) {
5958: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5959: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
5960: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
5961: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5962: return "refused:s:$crole&$cqual";
5963: }
5964: }
1.191 harris41 5965: }
1.800 albertel 5966: foreach my $role (split(':',$domrole)) {
5967: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5968: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
5969: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
5970: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 5971: return "refused:d:$crole&$cqual";
5972: }
5973: }
1.191 harris41 5974: }
1.800 albertel 5975: foreach my $role (split(':',$courole)) {
5976: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5977: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
5978: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
5979: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5980: return "refused:c:$crole&$cqual";
5981: }
5982: }
1.191 harris41 5983: }
1.620 albertel 5984: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
5985: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 5986: "rolesdef_$rolename=".
5987: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 5988: return reply($command,$env{'user.home'});
1.12 www 5989: } else {
5990: return 'refused';
5991: }
1.105 harris41 5992: }
5993:
5994: # ---------------- Make a metadata query against the network of library servers
5995:
5996: sub metadata_query {
1.244 matthew 5997: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 5998: my %rhash;
1.845 albertel 5999: my %libserv = &all_library();
1.244 matthew 6000: my @server_list = (defined($server_array) ? @$server_array
6001: : keys(%libserv) );
6002: for my $server (@server_list) {
1.118 harris41 6003: unless ($custom or $customshow) {
6004: my $reply=&reply("querysend:".&escape($query),$server);
6005: $rhash{$server}=$reply;
6006: }
6007: else {
6008: my $reply=&reply("querysend:".&escape($query).':'.
6009: &escape($custom).':'.&escape($customshow),
6010: $server);
6011: $rhash{$server}=$reply;
6012: }
1.112 harris41 6013: }
1.118 harris41 6014: return \%rhash;
1.240 www 6015: }
6016:
6017: # ----------------------------------------- Send log queries and wait for reply
6018:
6019: sub log_query {
6020: my ($uname,$udom,$query,%filters)=@_;
6021: my $uhome=&homeserver($uname,$udom);
6022: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 6023: my $uhost=&hostname($uhome);
1.800 albertel 6024: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 6025: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
6026: $uhome);
1.479 albertel 6027: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 6028: return get_query_reply($queryid);
6029: }
6030:
1.818 raeburn 6031: # -------------------------- Update MySQL table for portfolio file
6032:
6033: sub update_portfolio_table {
1.821 raeburn 6034: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 6035: if ($group ne '') {
6036: $file_name =~s /^\Q$group\E//;
6037: }
1.818 raeburn 6038: my $homeserver = &homeserver($uname,$udom);
6039: my $queryid=
1.821 raeburn 6040: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
6041: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 6042: my $reply = &get_query_reply($queryid);
6043: return $reply;
6044: }
6045:
1.899 raeburn 6046: # -------------------------- Update MySQL allusers table
6047:
6048: sub update_allusers_table {
6049: my ($uname,$udom,$names) = @_;
6050: my $homeserver = &homeserver($uname,$udom);
6051: my $queryid=
6052: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
6053: 'lastname='.&escape($names->{'lastname'}).'%%'.
6054: 'firstname='.&escape($names->{'firstname'}).'%%'.
6055: 'middlename='.&escape($names->{'middlename'}).'%%'.
6056: 'generation='.&escape($names->{'generation'}).'%%'.
6057: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
6058: 'id='.&escape($names->{'id'}),$homeserver);
1.1056.4.4 raeburn 6059: return;
1.899 raeburn 6060: }
6061:
1.508 raeburn 6062: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 6063:
6064: sub fetch_enrollment_query {
1.511 raeburn 6065: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 6066: my $homeserver;
1.547 raeburn 6067: my $maxtries = 1;
1.508 raeburn 6068: if ($context eq 'automated') {
6069: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 6070: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 6071: } else {
6072: $homeserver = &homeserver($cnum,$dom);
6073: }
1.838 albertel 6074: my $host=&hostname($homeserver);
1.506 raeburn 6075: my $cmd = '';
1.1000 raeburn 6076: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 6077: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 6078: }
6079: $cmd =~ s/%%$//;
6080: $cmd = &escape($cmd);
6081: my $query = 'fetchenrollment';
1.620 albertel 6082: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 6083: unless ($queryid=~/^\Q$host\E\_/) {
6084: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
6085: return 'error: '.$queryid;
6086: }
1.506 raeburn 6087: my $reply = &get_query_reply($queryid);
1.547 raeburn 6088: my $tries = 1;
6089: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6090: $reply = &get_query_reply($queryid);
6091: $tries ++;
6092: }
1.526 raeburn 6093: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 6094: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 6095: } else {
1.901 albertel 6096: my @responses = split(/:/,$reply);
1.515 raeburn 6097: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 6098: foreach my $line (@responses) {
6099: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 6100: $$replyref{$key} = $value;
6101: }
6102: } else {
1.506 raeburn 6103: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 6104: foreach my $line (@responses) {
6105: my ($key,$value) = split(/=/,$line);
1.506 raeburn 6106: $$replyref{$key} = $value;
6107: if ($value > 0) {
1.800 albertel 6108: foreach my $item (@{$$affiliatesref{$key}}) {
6109: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 6110: my $destname = $pathname.'/'.$filename;
6111: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 6112: if ($xml_classlist =~ /^error/) {
6113: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
6114: } else {
1.506 raeburn 6115: if ( open(FILE,">$destname") ) {
6116: print FILE &unescape($xml_classlist);
6117: close(FILE);
1.526 raeburn 6118: } else {
6119: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 6120: }
6121: }
6122: }
6123: }
6124: }
6125: }
6126: return 'ok';
6127: }
6128: return 'error';
6129: }
6130:
1.242 www 6131: sub get_query_reply {
6132: my $queryid=shift;
1.240 www 6133: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
6134: my $reply='';
6135: for (1..100) {
6136: sleep 2;
6137: if (-e $replyfile.'.end') {
1.448 albertel 6138: if (open(my $fh,$replyfile)) {
1.904 albertel 6139: $reply = join('',<$fh>);
6140: close($fh);
1.240 www 6141: } else { return 'error: reply_file_error'; }
1.242 www 6142: return &unescape($reply);
6143: }
1.240 www 6144: }
1.242 www 6145: return 'timeout:'.$queryid;
1.240 www 6146: }
6147:
6148: sub courselog_query {
1.241 www 6149: #
6150: # possible filters:
6151: # url: url or symb
6152: # username
6153: # domain
6154: # action: view, submit, grade
6155: # start: timestamp
6156: # end: timestamp
6157: #
1.240 www 6158: my (%filters)=@_;
1.620 albertel 6159: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 6160: if ($filters{'url'}) {
6161: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
6162: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
6163: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
6164: }
1.620 albertel 6165: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
6166: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 6167: return &log_query($cname,$cdom,'courselog',%filters);
6168: }
6169:
6170: sub userlog_query {
1.858 raeburn 6171: #
6172: # possible filters:
6173: # action: log check role
6174: # start: timestamp
6175: # end: timestamp
6176: #
1.240 www 6177: my ($uname,$udom,%filters)=@_;
6178: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 6179: }
6180:
1.506 raeburn 6181: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
6182:
6183: sub auto_run {
1.508 raeburn 6184: my ($cnum,$cdom) = @_;
1.876 raeburn 6185: my $response = 0;
6186: my $settings;
6187: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
6188: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
6189: $settings = $domconfig{'autoenroll'};
6190: if ($settings->{'run'} eq '1') {
6191: $response = 1;
6192: }
6193: } else {
1.934 raeburn 6194: my $homeserver;
6195: if (&is_course($cdom,$cnum)) {
6196: $homeserver = &homeserver($cnum,$cdom);
6197: } else {
6198: $homeserver = &domain($cdom,'primary');
6199: }
6200: if ($homeserver ne 'no_host') {
6201: $response = &reply('autorun:'.$cdom,$homeserver);
6202: }
1.876 raeburn 6203: }
1.506 raeburn 6204: return $response;
6205: }
1.776 albertel 6206:
1.506 raeburn 6207: sub auto_get_sections {
1.508 raeburn 6208: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 6209: my $homeserver;
6210: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6211: $homeserver = &homeserver($cnum,$cdom);
6212: }
6213: if (!defined($homeserver)) {
6214: if ($cdom =~ /^$match_domain$/) {
6215: $homeserver = &domain($cdom,'primary');
6216: }
6217: }
6218: my @secs;
6219: if (defined($homeserver)) {
6220: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
6221: unless ($response eq 'refused') {
6222: @secs = split(/:/,$response);
6223: }
1.506 raeburn 6224: }
6225: return @secs;
6226: }
1.776 albertel 6227:
1.506 raeburn 6228: sub auto_new_course {
1.1056.4.19 raeburn 6229: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 6230: my $homeserver = &homeserver($cnum,$cdom);
1.1056.4.19 raeburn 6231: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 6232: return $response;
6233: }
1.776 albertel 6234:
1.506 raeburn 6235: sub auto_validate_courseID {
1.508 raeburn 6236: my ($cnum,$cdom,$inst_course_id) = @_;
6237: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 6238: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 6239: return $response;
6240: }
1.776 albertel 6241:
1.1007 raeburn 6242: sub auto_validate_instcode {
1.1020 raeburn 6243: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 6244: my ($homeserver,$response);
6245: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6246: $homeserver = &homeserver($cnum,$cdom);
6247: }
6248: if (!defined($homeserver)) {
6249: if ($cdom =~ /^$match_domain$/) {
6250: $homeserver = &domain($cdom,'primary');
6251: }
6252: }
1.1056.4.2 raeburn 6253: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
6254: &escape($instcode).':'.&escape($owner),$homeserver));
1.1027 raeburn 6255: my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
6256: return ($outcome,$description);
1.1007 raeburn 6257: }
6258:
1.506 raeburn 6259: sub auto_create_password {
1.873 raeburn 6260: my ($cnum,$cdom,$authparam,$udom) = @_;
6261: my ($homeserver,$response);
1.506 raeburn 6262: my $create_passwd = 0;
6263: my $authchk = '';
1.873 raeburn 6264: if ($udom =~ /^$match_domain$/) {
6265: $homeserver = &domain($udom,'primary');
6266: }
6267: if ($homeserver eq '') {
6268: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6269: $homeserver = &homeserver($cnum,$cdom);
6270: }
6271: }
6272: if ($homeserver eq '') {
6273: $authchk = 'nodomain';
1.506 raeburn 6274: } else {
1.873 raeburn 6275: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
6276: if ($response eq 'refused') {
6277: $authchk = 'refused';
6278: } else {
1.901 albertel 6279: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 6280: }
1.506 raeburn 6281: }
6282: return ($authparam,$create_passwd,$authchk);
6283: }
6284:
1.706 raeburn 6285: sub auto_photo_permission {
6286: my ($cnum,$cdom,$students) = @_;
6287: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 6288: my ($outcome,$perm_reqd,$conditions) =
6289: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 6290: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6291: return (undef,undef);
6292: }
1.706 raeburn 6293: return ($outcome,$perm_reqd,$conditions);
6294: }
6295:
6296: sub auto_checkphotos {
6297: my ($uname,$udom,$pid) = @_;
6298: my $homeserver = &homeserver($uname,$udom);
6299: my ($result,$resulttype);
6300: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 6301: &escape($uname).':'.&escape($pid),
6302: $homeserver));
1.709 albertel 6303: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6304: return (undef,undef);
6305: }
1.706 raeburn 6306: if ($outcome) {
6307: ($result,$resulttype) = split(/:/,$outcome);
6308: }
6309: return ($result,$resulttype);
6310: }
6311:
6312: sub auto_photochoice {
6313: my ($cnum,$cdom) = @_;
6314: my $homeserver = &homeserver($cnum,$cdom);
6315: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 6316: &escape($cdom),
6317: $homeserver)));
1.709 albertel 6318: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6319: return (undef,undef);
6320: }
1.706 raeburn 6321: return ($update,$comment);
6322: }
6323:
6324: sub auto_photoupdate {
6325: my ($affiliatesref,$dom,$cnum,$photo) = @_;
6326: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 6327: my $host=&hostname($homeserver);
1.706 raeburn 6328: my $cmd = '';
6329: my $maxtries = 1;
1.800 albertel 6330: foreach my $affiliate (keys(%{$affiliatesref})) {
6331: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 6332: }
6333: $cmd =~ s/%%$//;
6334: $cmd = &escape($cmd);
6335: my $query = 'institutionalphotos';
6336: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
6337: unless ($queryid=~/^\Q$host\E\_/) {
6338: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
6339: return 'error: '.$queryid;
6340: }
6341: my $reply = &get_query_reply($queryid);
6342: my $tries = 1;
6343: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6344: $reply = &get_query_reply($queryid);
6345: $tries ++;
6346: }
6347: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
6348: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
6349: } else {
6350: my @responses = split(/:/,$reply);
6351: my $outcome = shift(@responses);
6352: foreach my $item (@responses) {
6353: my ($key,$value) = split(/=/,$item);
6354: $$photo{$key} = $value;
6355: }
6356: return $outcome;
6357: }
6358: return 'error';
6359: }
6360:
1.521 raeburn 6361: sub auto_instcode_format {
1.793 albertel 6362: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
6363: $cat_order) = @_;
1.521 raeburn 6364: my $courses = '';
1.772 raeburn 6365: my @homeservers;
1.521 raeburn 6366: if ($caller eq 'global') {
1.841 albertel 6367: my %servers = &get_servers($codedom,'library');
6368: foreach my $tryserver (keys(%servers)) {
6369: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6370: push(@homeservers,$tryserver);
6371: }
1.584 raeburn 6372: }
1.1022 raeburn 6373: } elsif ($caller eq 'requests') {
6374: if ($codedom =~ /^$match_domain$/) {
6375: my $chome = &domain($codedom,'primary');
6376: unless ($chome eq 'no_host') {
6377: push(@homeservers,$chome);
6378: }
6379: }
1.521 raeburn 6380: } else {
1.772 raeburn 6381: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 6382: }
1.793 albertel 6383: foreach my $code (keys(%{$instcodes})) {
6384: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 6385: }
6386: chop($courses);
1.772 raeburn 6387: my $ok_response = 0;
6388: my $response;
6389: while (@homeservers > 0 && $ok_response == 0) {
6390: my $server = shift(@homeservers);
6391: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
6392: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
6393: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 6394: split(/:/,$response);
1.772 raeburn 6395: %{$codes} = (%{$codes},&str2hash($codes_str));
6396: push(@{$codetitles},&str2array($codetitles_str));
6397: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
6398: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
6399: $ok_response = 1;
6400: }
6401: }
6402: if ($ok_response) {
1.521 raeburn 6403: return 'ok';
1.772 raeburn 6404: } else {
6405: return $response;
1.521 raeburn 6406: }
6407: }
6408:
1.792 raeburn 6409: sub auto_instcode_defaults {
6410: my ($domain,$returnhash,$code_order) = @_;
6411: my @homeservers;
1.841 albertel 6412:
6413: my %servers = &get_servers($domain,'library');
6414: foreach my $tryserver (keys(%servers)) {
6415: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6416: push(@homeservers,$tryserver);
6417: }
1.792 raeburn 6418: }
1.841 albertel 6419:
1.792 raeburn 6420: my $response;
1.841 albertel 6421: foreach my $server (@homeservers) {
1.792 raeburn 6422: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 6423: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
6424:
6425: foreach my $pair (split(/\&/,$response)) {
6426: my ($name,$value)=split(/\=/,$pair);
6427: if ($name eq 'code_order') {
6428: @{$code_order} = split(/\&/,&unescape($value));
6429: } else {
6430: $returnhash->{&unescape($name)}=&unescape($value);
6431: }
6432: }
6433: return 'ok';
1.792 raeburn 6434: }
1.841 albertel 6435:
6436: return $response;
1.1003 raeburn 6437: }
6438:
6439: sub auto_possible_instcodes {
1.1007 raeburn 6440: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
6441: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
6442: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
6443: return;
6444: }
1.1003 raeburn 6445: my (@homeservers,$uhome);
6446: if (defined(&domain($domain,'primary'))) {
6447: $uhome=&domain($domain,'primary');
6448: push(@homeservers,&domain($domain,'primary'));
6449: } else {
6450: my %servers = &get_servers($domain,'library');
6451: foreach my $tryserver (keys(%servers)) {
6452: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6453: push(@homeservers,$tryserver);
6454: }
6455: }
6456: }
6457: my $response;
6458: foreach my $server (@homeservers) {
6459: $response=&reply('autopossibleinstcodes:'.$domain,$server);
6460: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 6461: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
6462: split(':',$response);
6463: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
6464: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 6465: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 6466: my ($name,$value)=split('=',$item);
6467: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6468: }
6469: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 6470: my ($name,$value)=split('=',$item);
6471: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6472: }
6473: return 'ok';
6474: }
6475: return $response;
6476: }
1.792 raeburn 6477:
1.1010 raeburn 6478: sub auto_courserequest_checks {
6479: my ($dom) = @_;
1.1020 raeburn 6480: my ($homeserver,%validations);
6481: if ($dom =~ /^$match_domain$/) {
6482: $homeserver = &domain($dom,'primary');
6483: }
6484: unless ($homeserver eq 'no_host') {
6485: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
6486: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
6487: my @items = split(/&/,$response);
6488: foreach my $item (@items) {
6489: my ($key,$value) = split('=',$item);
6490: $validations{&unescape($key)} = &thaw_unescape($value);
6491: }
6492: }
6493: }
1.1010 raeburn 6494: return %validations;
6495: }
6496:
1.1020 raeburn 6497: sub auto_courserequest_validation {
6498: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
6499: my ($homeserver,$response);
6500: if ($dom =~ /^$match_domain$/) {
6501: $homeserver = &domain($dom,'primary');
6502: }
6503: unless ($homeserver eq 'no_host') {
1.1021 raeburn 6504:
1.1020 raeburn 6505: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 6506: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020 raeburn 6507: ':'.&escape($instcode).':'.&escape($instseclist),
6508: $homeserver));
6509: }
6510: return $response;
6511: }
6512:
1.777 albertel 6513: sub auto_validate_class_sec {
1.918 raeburn 6514: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 6515: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 6516: my $ownerlist;
6517: if (ref($owners) eq 'ARRAY') {
6518: $ownerlist = join(',',@{$owners});
6519: } else {
6520: $ownerlist = $owners;
6521: }
1.773 raeburn 6522: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 6523: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 6524: return $response;
6525: }
6526:
1.679 raeburn 6527: # ------------------------------------------------------- Course Group routines
6528:
6529: sub get_coursegroups {
1.809 raeburn 6530: my ($cdom,$cnum,$group,$namespace) = @_;
6531: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 6532: }
6533:
1.679 raeburn 6534: sub modify_coursegroup {
6535: my ($cdom,$cnum,$groupsettings) = @_;
6536: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
6537: }
6538:
1.809 raeburn 6539: sub toggle_coursegroup_status {
6540: my ($cdom,$cnum,$group,$action) = @_;
6541: my ($from_namespace,$to_namespace);
6542: if ($action eq 'delete') {
6543: $from_namespace = 'coursegroups';
6544: $to_namespace = 'deleted_groups';
6545: } else {
6546: $from_namespace = 'deleted_groups';
6547: $to_namespace = 'coursegroups';
6548: }
6549: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 6550: if (my $tmp = &error(%curr_group)) {
6551: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
6552: return ('read error',$tmp);
6553: } else {
6554: my %savedsettings = %curr_group;
1.809 raeburn 6555: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 6556: my $deloutcome;
6557: if ($result eq 'ok') {
1.809 raeburn 6558: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 6559: } else {
6560: return ('write error',$result);
6561: }
6562: if ($deloutcome eq 'ok') {
6563: return 'ok';
6564: } else {
6565: return ('delete error',$deloutcome);
6566: }
6567: }
6568: }
6569:
1.679 raeburn 6570: sub modify_group_roles {
1.957 raeburn 6571: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 6572: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
6573: my $role = 'gr/'.&escape($userprivs);
6574: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 6575: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 6576: if ($result eq 'ok') {
6577: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
6578: }
1.679 raeburn 6579: return $result;
6580: }
6581:
6582: sub modify_coursegroup_membership {
6583: my ($cdom,$cnum,$membership) = @_;
6584: my $result = &put('groupmembership',$membership,$cdom,$cnum);
6585: return $result;
6586: }
6587:
1.682 raeburn 6588: sub get_active_groups {
6589: my ($udom,$uname,$cdom,$cnum) = @_;
6590: my $now = time;
6591: my %groups = ();
6592: foreach my $key (keys(%env)) {
1.811 albertel 6593: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 6594: my ($start,$end) = split(/\./,$env{$key});
6595: if (($end!=0) && ($end<$now)) { next; }
6596: if (($start!=0) && ($start>$now)) { next; }
6597: if ($1 eq $cdom && $2 eq $cnum) {
6598: $groups{$3} = $env{$key} ;
6599: }
6600: }
6601: }
6602: return %groups;
6603: }
6604:
1.683 raeburn 6605: sub get_group_membership {
6606: my ($cdom,$cnum,$group) = @_;
6607: return(&dump('groupmembership',$cdom,$cnum,$group));
6608: }
6609:
6610: sub get_users_groups {
6611: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 6612: my @usersgroups;
1.683 raeburn 6613: my $cachetime=1800;
6614:
6615: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 6616: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
6617: if (defined($cached)) {
1.734 albertel 6618: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 6619: } else {
6620: $grouplist = '';
1.816 raeburn 6621: my $courseurl = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 6622: my $extra = &freeze_escape({'skipcheck' => 1});
6623: my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817 raeburn 6624: my $access_end = $env{'course.'.$courseid.
6625: '.default_enrollment_end_date'};
6626: my $now = time;
6627: foreach my $key (keys(%roleshash)) {
6628: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
6629: my $group = $1;
6630: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
6631: my $start = $2;
6632: my $end = $1;
6633: if ($start == -1) { next; } # deleted from group
6634: if (($start!=0) && ($start>$now)) { next; }
6635: if (($end!=0) && ($end<$now)) {
6636: if ($access_end && $access_end < $now) {
6637: if ($access_end - $end < 86400) {
6638: push(@usersgroups,$group);
1.733 raeburn 6639: }
6640: }
1.817 raeburn 6641: next;
1.733 raeburn 6642: }
1.817 raeburn 6643: push(@usersgroups,$group);
1.683 raeburn 6644: }
6645: }
6646: }
1.817 raeburn 6647: @usersgroups = &sort_course_groups($courseid,@usersgroups);
6648: $grouplist = join(':',@usersgroups);
6649: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 6650: }
1.733 raeburn 6651: return @usersgroups;
1.683 raeburn 6652: }
6653:
6654: sub devalidate_getgroups_cache {
6655: my ($udom,$uname,$cdom,$cnum)=@_;
6656: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 6657:
1.683 raeburn 6658: my $hashid="$udom:$uname:$courseid";
6659: &devalidate_cache_new('getgroups',$hashid);
6660: }
6661:
1.12 www 6662: # ------------------------------------------------------------------ Plain Text
6663:
6664: sub plaintext {
1.988 raeburn 6665: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 6666: if ($short =~ m{^cr/}) {
1.758 albertel 6667: return (split('/',$short))[-1];
6668: }
1.742 raeburn 6669: if (!defined($cid)) {
6670: $cid = $env{'request.course.id'};
6671: }
6672: my %rolenames = (
1.1008 raeburn 6673: Course => 'std',
6674: Community => 'alt1',
1.742 raeburn 6675: );
1.1037 raeburn 6676: if ($cid ne '') {
6677: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
6678: unless ($forcedefault) {
6679: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
6680: &Apache::lonlocal::mt_escape(\$roletext);
6681: return &Apache::lonlocal::mt($roletext);
6682: }
6683: }
6684: }
6685: if ((defined($type)) && (defined($rolenames{$type})) &&
6686: (defined($rolenames{$type})) &&
6687: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 6688: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 6689: } elsif ($cid ne '') {
6690: my $crstype = $env{'course.'.$cid.'.type'};
6691: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
6692: (defined($prp{$short}{$rolenames{$crstype}}))) {
6693: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
6694: }
1.742 raeburn 6695: }
1.1037 raeburn 6696: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 6697: }
6698:
6699: # ----------------------------------------------------------------- Assign Role
6700:
6701: sub assignrole {
1.957 raeburn 6702: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
6703: $context)=@_;
1.21 www 6704: my $mrole;
6705: if ($role =~ /^cr\//) {
1.393 www 6706: my $cwosec=$url;
1.811 albertel 6707: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 6708: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 6709: my $refused = 1;
6710: if ($context eq 'requestcourses') {
6711: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
6712: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
6713: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
6714: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6715: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6716: if ($crsenv{'internal.courseowner'} eq
6717: $env{'user.name'}.':'.$env{'user.domain'}) {
6718: $refused = '';
6719: }
6720: }
6721: }
6722: }
6723: }
6724: if ($refused) {
6725: &logthis('Refused custom assignrole: '.
6726: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
6727: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
6728: return 'refused';
6729: }
1.104 www 6730: }
1.21 www 6731: $mrole='cr';
1.678 raeburn 6732: } elsif ($role =~ /^gr\//) {
6733: my $cwogrp=$url;
1.811 albertel 6734: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 6735: unless (&allowed('mdg',$cwogrp)) {
6736: &logthis('Refused group assignrole: '.
6737: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
6738: $env{'user.name'}.' at '.$env{'user.domain'});
6739: return 'refused';
6740: }
6741: $mrole='gr';
1.21 www 6742: } else {
1.82 www 6743: my $cwosec=$url;
1.811 albertel 6744: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 6745: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
6746: my $refused;
6747: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
6748: if (!(&allowed('c'.$role,$url))) {
6749: $refused = 1;
6750: }
6751: } else {
6752: $refused = 1;
6753: }
1.947 raeburn 6754: if ($refused) {
1.1045 raeburn 6755: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6756: if (!$selfenroll && $context eq 'course') {
6757: my %crsenv;
6758: if ($role eq 'cc' || $role eq 'co') {
6759: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6760: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
6761: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
6762: if ($crsenv{'internal.courseowner'} eq
6763: $env{'user.name'}.':'.$env{'user.domain'}) {
6764: $refused = '';
6765: }
6766: }
6767: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
6768: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
6769: if ($crsenv{'internal.courseowner'} eq
6770: $env{'user.name'}.':'.$env{'user.domain'}) {
6771: $refused = '';
6772: }
6773: }
6774: }
6775: }
6776: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 6777: $refused = '';
1.1017 raeburn 6778: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 6779: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 6780: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 6781: my $wrongcc;
6782: if ($cnum =~ /^$match_community$/) {
6783: $wrongcc = 1 if ($role eq 'cc');
6784: } else {
6785: $wrongcc = 1 if ($role eq 'co');
6786: }
6787: unless ($wrongcc) {
6788: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6789: if ($crsenv{'internal.courseowner'} eq
6790: $env{'user.name'}.':'.$env{'user.domain'}) {
6791: $refused = '';
6792: }
1.1017 raeburn 6793: }
6794: }
6795: }
6796: if ($refused) {
1.947 raeburn 6797: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
6798: ' '.$role.' '.$end.' '.$start.' by '.
6799: $env{'user.name'}.' at '.$env{'user.domain'});
6800: return 'refused';
6801: }
1.932 raeburn 6802: }
1.1056.4.30 raeburn 6803: } elsif ($role eq 'au') {
6804: if ($url ne '/'.$udom.'/') {
6805: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
6806: ' to assign author role for '.$uname.':'.$udom.
6807: ' in domain: '.$url.' refused (wrong domain).');
6808: return 'refused';
6809: }
1.104 www 6810: }
1.21 www 6811: $mrole=$role;
6812: }
1.620 albertel 6813: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 6814: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 6815: if ($end) { $command.='_'.$end; }
1.21 www 6816: if ($start) {
6817: if ($end) {
1.81 www 6818: $command.='_'.$start;
1.21 www 6819: } else {
1.81 www 6820: $command.='_0_'.$start;
1.21 www 6821: }
6822: }
1.739 raeburn 6823: my $origstart = $start;
6824: my $origend = $end;
1.957 raeburn 6825: my $delflag;
1.357 www 6826: # actually delete
6827: if ($deleteflag) {
1.373 www 6828: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 6829: # modify command to delete the role
1.620 albertel 6830: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 6831: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 6832: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 6833: # set start and finish to negative values for userrolelog
6834: $start=-1;
6835: $end=-1;
1.957 raeburn 6836: $delflag = 1;
1.357 www 6837: }
6838: }
6839: # send command
1.349 www 6840: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 6841: # log new user role if status is ok
1.349 www 6842: if ($answer eq 'ok') {
1.663 raeburn 6843: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 6844: # for course roles, perform group memberships changes triggered by role change.
1.957 raeburn 6845: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739 raeburn 6846: unless ($role =~ /^gr/) {
6847: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957 raeburn 6848: $origstart,$selfenroll,$context);
1.739 raeburn 6849: }
1.1053 raeburn 6850: if ($role eq 'cc') {
6851: &autoupdate_coowners($url,$end,$start,$uname,$udom);
6852: }
1.349 www 6853: }
6854: return $answer;
1.169 harris41 6855: }
6856:
1.1053 raeburn 6857: sub autoupdate_coowners {
6858: my ($url,$end,$start,$uname,$udom) = @_;
6859: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
6860: if (($cdom ne '') && ($cnum ne '')) {
6861: my $now = time;
6862: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
6863: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
6864: my %coursehash = &coursedescription($cdom.'_'.$cnum);
6865: my $instcode = $coursehash{'internal.coursecode'};
6866: if ($instcode ne '') {
1.1056 raeburn 6867: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
6868: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 6869: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 6870: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
6871: if ($result eq 'valid') {
6872: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 6873: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6874: push(@newcoowners,$coowner);
6875: }
6876: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
6877: push(@newcoowners,$uname.':'.$udom);
6878: }
6879: @newcoowners = sort(@newcoowners);
6880: } else {
6881: push(@newcoowners,$uname.':'.$udom);
6882: }
6883: } else {
6884: if ($coursehash{'internal.co-owners'}) {
6885: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6886: unless ($coowner eq $uname.':'.$udom) {
6887: push(@newcoowners,$coowner);
6888: }
6889: }
6890: unless (@newcoowners > 0) {
6891: $delcoowners = 1;
6892: $coowners = '';
6893: }
6894: }
6895: }
6896: if (@newcoowners || $delcoowners) {
6897: &store_coowners($cdom,$cnum,$coursehash{'home'},
6898: $delcoowners,@newcoowners);
6899: }
6900: }
6901: }
6902: }
6903: }
6904: }
6905: }
6906:
6907: sub store_coowners {
6908: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
6909: my $cid = $cdom.'_'.$cnum;
6910: my ($coowners,$delresult,$putresult);
6911: if (@newcoowners) {
6912: $coowners = join(',',@newcoowners);
6913: my %coownershash = (
6914: 'internal.co-owners' => $coowners,
6915: );
6916: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
6917: if ($putresult eq 'ok') {
6918: if ($env{'course.'.$cid.'.num'} eq $cnum) {
6919: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
6920: }
6921: }
6922: }
6923: if ($delcoowners) {
6924: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
6925: if ($delresult eq 'ok') {
6926: if ($env{'course.'.$cid.'.internal.co-owners'}) {
6927: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
6928: }
6929: }
6930: }
6931: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
6932: my %crsinfo =
6933: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6934: if (ref($crsinfo{$cid}) eq 'HASH') {
6935: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
6936: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
6937: }
6938: }
6939: }
6940:
1.169 harris41 6941: # -------------------------------------------------- Modify user authentication
1.197 www 6942: # Overrides without validation
6943:
1.169 harris41 6944: sub modifyuserauth {
6945: my ($udom,$uname,$umode,$upass)=@_;
6946: my $uhome=&homeserver($uname,$udom);
1.197 www 6947: unless (&allowed('mau',$udom)) { return 'refused'; }
6948: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 6949: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6950: ' in domain '.$env{'request.role.domain'});
1.169 harris41 6951: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
6952: &escape($upass),$uhome);
1.620 albertel 6953: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 6954: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
6955: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
6956: &log($udom,,$uname,$uhome,
1.620 albertel 6957: 'Authentication changed by '.$env{'user.domain'}.', '.
6958: $env{'user.name'}.', '.$umode.
1.197 www 6959: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 6960: unless ($reply eq 'ok') {
1.197 www 6961: &logthis('Authentication mode error: '.$reply);
1.169 harris41 6962: return 'error: '.$reply;
6963: }
1.170 harris41 6964: return 'ok';
1.80 www 6965: }
6966:
1.81 www 6967: # --------------------------------------------------------------- Modify a user
1.80 www 6968:
1.81 www 6969: sub modifyuser {
1.206 matthew 6970: my ($udom, $uname, $uid,
6971: $umode, $upass, $first,
6972: $middle, $last, $gene,
1.1056.4.1 raeburn 6973: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 6974: $udom= &LONCAPA::clean_domain($udom);
6975: $uname=&LONCAPA::clean_username($uname);
1.1056.4.1 raeburn 6976: my $showcandelete = 'none';
6977: if (ref($candelete) eq 'ARRAY') {
6978: if (@{$candelete} > 0) {
6979: $showcandelete = join(', ',@{$candelete});
6980: }
6981: }
1.81 www 6982: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 6983: $umode.', '.$first.', '.$middle.', '.
1.1056.4.1 raeburn 6984: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 6985: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
6986: ' desiredhome not specified').
1.620 albertel 6987: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6988: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 6989: my $uhome=&homeserver($uname,$udom,'true');
1.1056.4.4 raeburn 6990: my $newuser;
6991: if ($uhome eq 'no_host') {
6992: $newuser = 1;
6993: }
1.80 www 6994: # ----------------------------------------------------------------- Create User
1.406 albertel 6995: if (($uhome eq 'no_host') &&
6996: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 6997: my $unhome='';
1.844 albertel 6998: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 6999: $unhome = $desiredhome;
1.620 albertel 7000: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
7001: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 7002: } else { # load balancing routine for determining $unhome
1.81 www 7003: my $loadm=10000000;
1.841 albertel 7004: my %servers = &get_servers($udom,'library');
7005: foreach my $tryserver (keys(%servers)) {
7006: my $answer=reply('load',$tryserver);
7007: if (($answer=~/\d+/) && ($answer<$loadm)) {
7008: $loadm=$answer;
7009: $unhome=$tryserver;
7010: }
1.80 www 7011: }
7012: }
7013: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 7014: return 'error: unable to find a home server for '.$uname.
7015: ' in domain '.$udom;
1.80 www 7016: }
7017: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
7018: &escape($upass),$unhome);
7019: unless ($reply eq 'ok') {
7020: return 'error: '.$reply;
7021: }
1.230 stredwic 7022: $uhome=&homeserver($uname,$udom,'true');
1.80 www 7023: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 7024: return 'error: unable verify users home machine.';
1.80 www 7025: }
1.209 matthew 7026: } # End of creation of new user
1.80 www 7027: # ---------------------------------------------------------------------- Add ID
7028: if ($uid) {
7029: $uid=~tr/A-Z/a-z/;
7030: my %uidhash=&idrget($udom,$uname);
1.196 www 7031: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
7032: && (!$forceid)) {
1.80 www 7033: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 7034: return 'error: user id "'.$uid.'" does not match '.
7035: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 7036: }
7037: } else {
7038: &idput($udom,($uname => $uid));
7039: }
7040: }
7041: # -------------------------------------------------------------- Add names, etc
1.313 matthew 7042: my @tmp=&get('environment',
1.899 raeburn 7043: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 7044: 'permanentemail','inststatus'],
1.134 albertel 7045: $udom,$uname);
1.1056.4.4 raeburn 7046: my (%names,%oldnames);
1.313 matthew 7047: if ($tmp[0] =~ m/^error:.*/) {
7048: %names=();
7049: } else {
7050: %names = @tmp;
1.1056.4.4 raeburn 7051: %oldnames = %names;
1.313 matthew 7052: }
1.1056.4.6 raeburn 7053: #
1.1056.4.1 raeburn 7054: # If name, email and/or uid are blank (e.g., because an uploaded file
7055: # of users did not contain them), do not overwrite existing values
7056: # unless field is in $candelete array ref.
7057: #
7058:
7059: my @fields = ('firstname','middlename','lastname','generation',
7060: 'permanentemail','id');
7061: my %newvalues;
7062: if (ref($candelete) eq 'ARRAY') {
7063: foreach my $field (@fields) {
7064: if (grep(/^\Q$field\E$/,@{$candelete})) {
7065: if ($field eq 'firstname') {
7066: $names{$field} = $first;
7067: } elsif ($field eq 'middlename') {
7068: $names{$field} = $middle;
7069: } elsif ($field eq 'lastname') {
7070: $names{$field} = $last;
7071: } elsif ($field eq 'generation') {
7072: $names{$field} = $gene;
7073: } elsif ($field eq 'permanentemail') {
7074: $names{$field} = $email;
7075: } elsif ($field eq 'id') {
7076: $names{$field} = $uid;
7077: }
7078: }
7079: }
7080: }
1.388 www 7081: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 7082: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 7083: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 7084: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 7085: if ($email) {
7086: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 7087: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 7088: }
1.899 raeburn 7089: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 7090: if (defined($inststatus)) {
7091: $names{'inststatus'} = '';
7092: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
7093: if (ref($usertypes) eq 'HASH') {
7094: my @okstatuses;
7095: foreach my $item (split(/:/,$inststatus)) {
7096: if (defined($usertypes->{$item})) {
7097: push(@okstatuses,$item);
7098: }
7099: }
7100: if (@okstatuses) {
7101: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
7102: }
7103: }
7104: }
1.1056.4.4 raeburn 7105: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 7106: $umode.', '.$first.', '.$middle.', '.
1.1056.4.4 raeburn 7107: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 7108: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
7109: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
7110: } else {
7111: $logmsg .= ' during self creation';
7112: }
1.1056.4.4 raeburn 7113: my $changed;
7114: if ($newuser) {
7115: $changed = 1;
7116: } else {
7117: foreach my $field (@fields) {
7118: if ($names{$field} ne $oldnames{$field}) {
7119: $changed = 1;
7120: last;
7121: }
7122: }
7123: }
7124: unless ($changed) {
7125: $logmsg = 'No changes in user information needed for: '.$logmsg;
7126: &logthis($logmsg);
7127: return 'ok';
7128: }
7129: my $reply = &put('environment', \%names, $udom,$uname);
7130: if ($reply ne 'ok') {
7131: return 'error: '.$reply;
7132: }
1.1056.4.11 raeburn 7133: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
7134: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
7135: }
1.1056.4.4 raeburn 7136: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
7137: &devalidate_cache_new('namescache',$uname.':'.$udom);
7138: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 7139: &logthis($logmsg);
1.134 albertel 7140: return 'ok';
1.80 www 7141: }
7142:
1.81 www 7143: # -------------------------------------------------------------- Modify student
1.80 www 7144:
1.81 www 7145: sub modifystudent {
7146: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 7147: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990 raeburn 7148: $selfenroll,$context,$inststatus)=@_;
1.455 albertel 7149: if (!$cid) {
1.620 albertel 7150: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7151: return 'not_in_class';
7152: }
1.80 www 7153: }
7154: # --------------------------------------------------------------- Make the user
1.81 www 7155: my $reply=&modifyuser
1.209 matthew 7156: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 7157: $desiredhome,$email,$inststatus);
1.80 www 7158: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 7159: # This will cause &modify_student_enrollment to get the uid from the
7160: # students environment
7161: $uid = undef if (!$forceid);
1.455 albertel 7162: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957 raeburn 7163: $gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297 matthew 7164: return $reply;
7165: }
7166:
7167: sub modify_student_enrollment {
1.957 raeburn 7168: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455 albertel 7169: my ($cdom,$cnum,$chome);
7170: if (!$cid) {
1.620 albertel 7171: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7172: return 'not_in_class';
7173: }
1.620 albertel 7174: $cdom=$env{'course.'.$cid.'.domain'};
7175: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 7176: } else {
7177: ($cdom,$cnum)=split(/_/,$cid);
7178: }
1.620 albertel 7179: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 7180: if (!$chome) {
1.457 raeburn 7181: $chome=&homeserver($cnum,$cdom);
1.297 matthew 7182: }
1.455 albertel 7183: if (!$chome) { return 'unknown_course'; }
1.297 matthew 7184: # Make sure the user exists
1.81 www 7185: my $uhome=&homeserver($uname,$udom);
7186: if (($uhome eq '') || ($uhome eq 'no_host')) {
7187: return 'error: no such user';
7188: }
1.297 matthew 7189: # Get student data if we were not given enough information
7190: if (!defined($first) || $first eq '' ||
7191: !defined($last) || $last eq '' ||
7192: !defined($uid) || $uid eq '' ||
7193: !defined($middle) || $middle eq '' ||
7194: !defined($gene) || $gene eq '') {
1.294 matthew 7195: # They did not supply us with enough data to enroll the student, so
7196: # we need to pick up more information.
1.297 matthew 7197: my %tmp = &get('environment',
1.294 matthew 7198: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 7199: ,$udom,$uname);
7200:
1.800 albertel 7201: #foreach my $key (keys(%tmp)) {
7202: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 7203: #}
1.294 matthew 7204: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
7205: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
7206: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 7207: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 7208: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
7209: }
1.556 albertel 7210: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 7211: my $reply=cput('classlist',
7212: {"$uname:$udom" =>
1.515 raeburn 7213: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 7214: $cdom,$cnum);
1.81 www 7215: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
7216: return 'error: '.$reply;
1.652 albertel 7217: } else {
7218: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 7219: }
1.297 matthew 7220: # Add student role to user
1.83 www 7221: my $uurl='/'.$cid;
1.81 www 7222: $uurl=~s/\_/\//g;
7223: if ($usec) {
7224: $uurl.='/'.$usec;
7225: }
1.957 raeburn 7226: return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21 www 7227: }
7228:
1.556 albertel 7229: sub format_name {
7230: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
7231: my $name;
7232: if ($first ne 'lastname') {
7233: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
7234: } else {
7235: if ($lastname=~/\S/) {
7236: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
7237: $name=~s/\s+,/,/;
7238: } else {
7239: $name.= $firstname.' '.$middlename.' '.$generation;
7240: }
7241: }
7242: $name=~s/^\s+//;
7243: $name=~s/\s+$//;
7244: $name=~s/\s+/ /g;
7245: return $name;
7246: }
7247:
1.84 www 7248: # ------------------------------------------------- Write to course preferences
7249:
7250: sub writecoursepref {
7251: my ($courseid,%prefs)=@_;
7252: $courseid=~s/^\///;
7253: $courseid=~s/\_/\//g;
7254: my ($cdomain,$cnum)=split(/\//,$courseid);
7255: my $chome=homeserver($cnum,$cdomain);
7256: if (($chome eq '') || ($chome eq 'no_host')) {
7257: return 'error: no such course';
7258: }
7259: my $cstring='';
1.800 albertel 7260: foreach my $pref (keys(%prefs)) {
7261: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 7262: }
1.84 www 7263: $cstring=~s/\&$//;
7264: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
7265: }
7266:
7267: # ---------------------------------------------------------- Make/modify course
7268:
7269: sub createcourse {
1.741 raeburn 7270: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 7271: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 7272: $url=&declutter($url);
7273: my $cid='';
1.1028 raeburn 7274: if ($context eq 'requestcourses') {
7275: my $can_create = 0;
7276: my ($ownername,$ownerdom) = split(':',$course_owner);
7277: if ($udom eq $ownerdom) {
7278: if (&usertools_access($ownername,$ownerdom,$category,undef,
7279: $context)) {
7280: $can_create = 1;
7281: }
7282: } else {
7283: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
7284: $category);
7285: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
7286: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
7287: if (@curr > 0) {
7288: my @options = qw(approval validate autolimit);
7289: my $optregex = join('|',@options);
7290: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
7291: $can_create = 1;
7292: }
7293: }
7294: }
7295: }
7296: if ($can_create) {
7297: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
7298: unless (&allowed('ccc',$udom)) {
7299: return 'refused';
7300: }
1.1017 raeburn 7301: }
7302: } else {
7303: return 'refused';
7304: }
1.1028 raeburn 7305: } elsif (!&allowed('ccc',$udom)) {
7306: return 'refused';
1.84 www 7307: }
1.1011 raeburn 7308: # --------------------------------------------------------------- Get Unique ID
7309: my $uname;
7310: if ($cnum =~ /^$match_courseid$/) {
7311: my $chome=&homeserver($cnum,$udom,'true');
7312: if (($chome eq '') || ($chome eq 'no_host')) {
7313: $uname = $cnum;
7314: } else {
1.1038 raeburn 7315: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7316: }
7317: } else {
1.1038 raeburn 7318: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7319: }
7320: return $uname if ($uname =~ /^error/);
7321: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 7322: if (!defined($course_server)) {
7323: if (defined(&domain($udom,'primary'))) {
7324: $course_server = &domain($udom,'primary');
7325: } else {
7326: $course_server = $env{'user.home'};
7327: }
7328: }
7329: my %host_servers =
7330: &Apache::lonnet::get_servers($udom,'library');
7331: unless ($host_servers{$course_server}) {
7332: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 7333: }
1.84 www 7334: # ------------------------------------------------------------- Make the course
7335: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 7336: $course_server);
1.84 www 7337: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 7338: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 7339: if (($uhome eq '') || ($uhome eq 'no_host')) {
7340: return 'error: no such course';
7341: }
1.271 www 7342: # ----------------------------------------------------------------- Course made
1.516 raeburn 7343: # log existence
1.1029 raeburn 7344: my $now = time;
1.918 raeburn 7345: my $newcourse = {
7346: $udom.'_'.$uname => {
1.921 raeburn 7347: description => $description,
7348: inst_code => $inst_code,
7349: owner => $course_owner,
7350: type => $crstype,
1.1029 raeburn 7351: creator => $env{'user.name'}.':'.
7352: $env{'user.domain'},
7353: created => $now,
7354: context => $context,
1.918 raeburn 7355: },
7356: };
1.921 raeburn 7357: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 7358: # set toplevel url
1.271 www 7359: my $topurl=$url;
7360: unless ($nonstandard) {
7361: # ------------------------------------------ For standard courses, make top url
7362: my $mapurl=&clutter($url);
1.278 www 7363: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 7364: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 7365: <map>
7366: <resource id="1" type="start"></resource>
7367: <resource id="2" src="$mapurl"></resource>
7368: <resource id="3" type="finish"></resource>
7369: <link index="1" from="1" to="2"></link>
7370: <link index="2" from="2" to="3"></link>
7371: </map>
7372: ENDINITMAP
7373: $topurl=&declutter(
1.638 albertel 7374: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 7375: );
7376: }
7377: # ----------------------------------------------------------- Write preferences
1.84 www 7378: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 7379: ('description' => $description,
7380: 'url' => $topurl,
7381: 'internal.creator' => $env{'user.name'}.':'.
7382: $env{'user.domain'},
7383: 'internal.created' => $now,
7384: 'internal.creationcontext' => $context)
7385: );
1.84 www 7386: return '/'.$udom.'/'.$uname;
7387: }
7388:
1.1011 raeburn 7389: # ------------------------------------------------------------------- Create ID
7390: sub generate_coursenum {
1.1038 raeburn 7391: my ($udom,$crstype) = @_;
1.1011 raeburn 7392: my $domdesc = &domain($udom);
7393: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 7394: my $first;
7395: if ($crstype eq 'Community') {
7396: $first = '0';
7397: } else {
7398: $first = int(1+rand(9));
7399: }
7400: my $uname=$first.
1.1011 raeburn 7401: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7402: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7403: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7404: # ----------------------------------------------- Make sure that does not exist
7405: my $uhome=&homeserver($uname,$udom,'true');
7406: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 7407: if ($crstype eq 'Community') {
7408: $first = '0';
7409: } else {
7410: $first = int(1+rand(9));
7411: }
7412: $uname=$first.
1.1011 raeburn 7413: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7414: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7415: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7416: $uhome=&homeserver($uname,$udom,'true');
7417: unless (($uhome eq '') || ($uhome eq 'no_host')) {
7418: return 'error: unable to generate unique course-ID';
7419: }
7420: }
7421: return $uname;
7422: }
7423:
1.813 albertel 7424: sub is_course {
7425: my ($cdom,$cnum) = @_;
7426: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946 raeburn 7427: undef,'.');
1.813 albertel 7428: if (exists($courses{$cdom.'_'.$cnum})) {
7429: return 1;
7430: }
7431: return 0;
7432: }
7433:
1.1015 raeburn 7434: sub store_userdata {
7435: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 7436: my $result;
1.1016 raeburn 7437: if ($datakey ne '') {
1.1013 raeburn 7438: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 7439: if ($udom eq '' || $uname eq '') {
7440: $udom = $env{'user.domain'};
7441: $uname = $env{'user.name'};
7442: }
7443: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 7444: if (($uhome eq '') || ($uhome eq 'no_host')) {
7445: $result = 'error: no_host';
7446: } else {
7447: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
7448: $storehash->{'host'} = $perlvar{'lonHostID'};
7449:
7450: my $namevalue='';
7451: foreach my $key (keys(%{$storehash})) {
7452: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7453: }
7454: $namevalue=~s/\&$//;
1.1056.4.24 raeburn 7455: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
7456: $namevalue,$uhome);
1.1013 raeburn 7457: }
7458: } else {
7459: $result = 'error: data to store was not a hash reference';
7460: }
7461: } else {
7462: $result= 'error: invalid requestkey';
7463: }
7464: return $result;
7465: }
7466:
1.21 www 7467: # ---------------------------------------------------------- Assign Custom Role
7468:
7469: sub assigncustomrole {
1.957 raeburn 7470: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7471: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 7472: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 7473: }
7474:
7475: # ----------------------------------------------------------------- Revoke Role
7476:
7477: sub revokerole {
1.957 raeburn 7478: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7479: my $now=time;
1.965 raeburn 7480: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 7481: }
7482:
7483: # ---------------------------------------------------------- Revoke Custom Role
7484:
7485: sub revokecustomrole {
1.957 raeburn 7486: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7487: my $now=time;
1.357 www 7488: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 7489: $deleteflag,$selfenroll,$context);
1.17 www 7490: }
7491:
1.533 banghart 7492: # ------------------------------------------------------------ Disk usage
1.535 albertel 7493: sub diskusage {
1.955 raeburn 7494: my ($udom,$uname,$directorypath,$getpropath)=@_;
7495: $directorypath =~ s/\/$//;
7496: my $listing=&reply('du2:'.&escape($directorypath).':'
7497: .&escape($getpropath).':'.&escape($uname).':'
7498: .&escape($udom),homeserver($uname,$udom));
7499: if ($listing eq 'unknown_cmd') {
7500: if ($getpropath) {
7501: $directorypath = &propath($udom,$uname).'/'.$directorypath;
7502: }
7503: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
7504: }
1.514 albertel 7505: return $listing;
1.512 banghart 7506: }
7507:
1.566 banghart 7508: sub is_locked {
1.1056.4.17 raeburn 7509: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 7510: my @check;
7511: my $is_locked;
1.1056.4.14 raeburn 7512: push(@check,$file_name);
1.613 albertel 7513: my %locked = &get('file_permissions',\@check,
1.620 albertel 7514: $env{'user.domain'},$env{'user.name'});
1.615 albertel 7515: my ($tmp)=keys(%locked);
7516: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 7517:
1.566 banghart 7518: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 7519: $is_locked = 'false';
7520: foreach my $entry (@{$locked{$file_name}}) {
1.1056.4.17 raeburn 7521: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 7522: $is_locked = 'true';
1.1056.4.17 raeburn 7523: if (ref($which) eq 'ARRAY') {
7524: push(@{$which},$entry);
7525: } else {
7526: last;
7527: }
1.745 raeburn 7528: }
7529: }
1.566 banghart 7530: } else {
7531: $is_locked = 'false';
7532: }
1.1056.4.14 raeburn 7533: return $is_locked;
1.566 banghart 7534: }
7535:
1.759 albertel 7536: sub declutter_portfile {
7537: my ($file) = @_;
1.833 albertel 7538: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 7539: return $file;
7540: }
7541:
1.559 banghart 7542: # ------------------------------------------------------------- Mark as Read Only
7543:
7544: sub mark_as_readonly {
7545: my ($domain,$user,$files,$what) = @_;
1.613 albertel 7546: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7547: my ($tmp)=keys(%current_permissions);
7548: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 7549: foreach my $file (@{$files}) {
1.759 albertel 7550: $file = &declutter_portfile($file);
1.561 banghart 7551: push(@{$current_permissions{$file}},$what);
1.559 banghart 7552: }
1.613 albertel 7553: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7554: return;
7555: }
7556:
1.572 banghart 7557: # ------------------------------------------------------------Save Selected Files
7558:
7559: sub save_selected_files {
7560: my ($user, $path, @files) = @_;
7561: my $filename = $user."savedfiles";
1.573 banghart 7562: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 7563: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 7564: foreach my $file (@files) {
1.620 albertel 7565: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 7566: }
7567: foreach my $file (@other_files) {
1.574 banghart 7568: print (OUT $file."\n");
1.572 banghart 7569: }
1.574 banghart 7570: close (OUT);
1.572 banghart 7571: return 'ok';
7572: }
7573:
1.574 banghart 7574: sub clear_selected_files {
7575: my ($user) = @_;
7576: my $filename = $user."savedfiles";
7577: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7578: print (OUT undef);
7579: close (OUT);
7580: return ("ok");
7581: }
7582:
1.572 banghart 7583: sub files_in_path {
7584: my ($user, $path) = @_;
7585: my $filename = $user."savedfiles";
7586: my %return_files;
1.574 banghart 7587: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 7588: while (my $line_in = <IN>) {
1.574 banghart 7589: chomp ($line_in);
7590: my @paths_and_file = split (m!/!, $line_in);
7591: my $file_part = pop (@paths_and_file);
7592: my $path_part = join ('/', @paths_and_file);
1.573 banghart 7593: $path_part.='/';
7594: my $path_and_file = $path_part.$file_part;
7595: if ($path_part eq $path) {
7596: $return_files{$file_part}= 'selected';
7597: }
7598: }
1.574 banghart 7599: close (IN);
7600: return (\%return_files);
1.572 banghart 7601: }
7602:
7603: # called in portfolio select mode, to show files selected NOT in current directory
7604: sub files_not_in_path {
7605: my ($user, $path) = @_;
7606: my $filename = $user."savedfiles";
7607: my @return_files;
7608: my $path_part;
1.800 albertel 7609: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7610: while (my $line = <IN>) {
1.572 banghart 7611: #ok, I know it's clunky, but I want it to work
1.800 albertel 7612: my @paths_and_file = split(m|/|, $line);
7613: my $file_part = pop(@paths_and_file);
7614: chomp($file_part);
7615: my $path_part = join('/', @paths_and_file);
1.572 banghart 7616: $path_part .= '/';
7617: my $path_and_file = $path_part.$file_part;
7618: if ($path_part ne $path) {
1.800 albertel 7619: push(@return_files, ($path_and_file));
1.572 banghart 7620: }
7621: }
1.800 albertel 7622: close(OUT);
1.574 banghart 7623: return (@return_files);
1.572 banghart 7624: }
7625:
1.745 raeburn 7626: #----------------------------------------------Get portfolio file permissions
1.629 banghart 7627:
1.745 raeburn 7628: sub get_portfile_permissions {
7629: my ($domain,$user) = @_;
1.613 albertel 7630: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7631: my ($tmp)=keys(%current_permissions);
7632: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7633: return \%current_permissions;
7634: }
7635:
7636: #---------------------------------------------Get portfolio file access controls
7637:
1.749 raeburn 7638: sub get_access_controls {
1.745 raeburn 7639: my ($current_permissions,$group,$file) = @_;
1.769 albertel 7640: my %access;
7641: my $real_file = $file;
7642: $file =~ s/\.meta$//;
1.745 raeburn 7643: if (defined($file)) {
1.749 raeburn 7644: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
7645: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 7646: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 7647: }
7648: }
1.745 raeburn 7649: } else {
1.749 raeburn 7650: foreach my $key (keys(%{$current_permissions})) {
7651: if ($key =~ /\0accesscontrol$/) {
7652: if (defined($group)) {
7653: if ($key !~ m-^\Q$group\E/-) {
7654: next;
7655: }
7656: }
7657: my ($fullpath) = split(/\0/,$key);
7658: if (ref($$current_permissions{$key}) eq 'HASH') {
7659: foreach my $control (keys(%{$$current_permissions{$key}})) {
7660: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
7661: }
7662: }
7663: }
7664: }
7665: }
7666: return %access;
7667: }
7668:
7669: sub modify_access_controls {
7670: my ($file_name,$changes,$domain,$user)=@_;
7671: my ($outcome,$deloutcome);
7672: my %store_permissions;
7673: my %new_values;
7674: my %new_control;
7675: my %translation;
7676: my @deletions = ();
7677: my $now = time;
7678: if (exists($$changes{'activate'})) {
7679: if (ref($$changes{'activate'}) eq 'HASH') {
7680: my @newitems = sort(keys(%{$$changes{'activate'}}));
7681: my $numnew = scalar(@newitems);
7682: for (my $i=0; $i<$numnew; $i++) {
7683: my $newkey = $newitems[$i];
7684: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 7685: if ($newkey =~ /^\d+:/) {
7686: $newkey =~ s/^(\d+)/$newid/;
7687: $translation{$1} = $newid;
7688: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
7689: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
7690: $translation{$1} = $newid;
7691: }
1.749 raeburn 7692: $new_values{$file_name."\0".$newkey} =
7693: $$changes{'activate'}{$newitems[$i]};
7694: $new_control{$newkey} = $now;
7695: }
7696: }
7697: }
7698: my %todelete;
7699: my %changed_items;
7700: foreach my $action ('delete','update') {
7701: if (exists($$changes{$action})) {
7702: if (ref($$changes{$action}) eq 'HASH') {
7703: foreach my $key (keys(%{$$changes{$action}})) {
7704: my ($itemnum) = ($key =~ /^([^:]+):/);
7705: if ($action eq 'delete') {
7706: $todelete{$itemnum} = 1;
7707: } else {
7708: $changed_items{$itemnum} = $key;
7709: }
7710: }
1.745 raeburn 7711: }
7712: }
1.749 raeburn 7713: }
7714: # get lock on access controls for file.
7715: my $lockhash = {
7716: $file_name."\0".'locked_access_records' => $env{'user.name'}.
7717: ':'.$env{'user.domain'},
7718: };
7719: my $tries = 0;
7720: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7721:
7722: while (($gotlock ne 'ok') && $tries <3) {
7723: $tries ++;
7724: sleep 1;
7725: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7726: }
7727: if ($gotlock eq 'ok') {
7728: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
7729: my ($tmp)=keys(%curr_permissions);
7730: if ($tmp=~/^error:/) { undef(%curr_permissions); }
7731: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
7732: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
7733: if (ref($curr_controls) eq 'HASH') {
7734: foreach my $control_item (keys(%{$curr_controls})) {
7735: my ($itemnum) = ($control_item =~ /^([^:]+):/);
7736: if (defined($todelete{$itemnum})) {
7737: push(@deletions,$file_name."\0".$control_item);
7738: } else {
7739: if (defined($changed_items{$itemnum})) {
7740: $new_control{$changed_items{$itemnum}} = $now;
7741: push(@deletions,$file_name."\0".$control_item);
7742: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
7743: } else {
7744: $new_control{$control_item} = $$curr_controls{$control_item};
7745: }
7746: }
1.745 raeburn 7747: }
7748: }
7749: }
1.970 raeburn 7750: my ($group);
7751: if (&is_course($domain,$user)) {
7752: ($group,my $file) = split(/\//,$file_name,2);
7753: }
1.749 raeburn 7754: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
7755: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
7756: $outcome = &put('file_permissions',\%new_values,$domain,$user);
7757: # remove lock
7758: my @del_lock = ($file_name."\0".'locked_access_records');
7759: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 7760: my $sqlresult =
1.970 raeburn 7761: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 7762: $group);
1.749 raeburn 7763: } else {
7764: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 7765: }
1.749 raeburn 7766: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 7767: }
7768:
1.827 raeburn 7769: sub make_public_indefinitely {
7770: my ($requrl) = @_;
7771: my $now = time;
7772: my $action = 'activate';
7773: my $aclnum = 0;
7774: if (&is_portfolio_url($requrl)) {
7775: my (undef,$udom,$unum,$file_name,$group) =
7776: &parse_portfolio_url($requrl);
7777: my $current_perms = &get_portfile_permissions($udom,$unum);
7778: my %access_controls = &get_access_controls($current_perms,
7779: $group,$file_name);
7780: foreach my $key (keys(%{$access_controls{$file_name}})) {
7781: my ($num,$scope,$end,$start) =
7782: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
7783: if ($scope eq 'public') {
7784: if ($start <= $now && $end == 0) {
7785: $action = 'none';
7786: } else {
7787: $action = 'update';
7788: $aclnum = $num;
7789: }
7790: last;
7791: }
7792: }
7793: if ($action eq 'none') {
7794: return 'ok';
7795: } else {
7796: my %changes;
7797: my $newend = 0;
7798: my $newstart = $now;
7799: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
7800: $changes{$action}{$newkey} = {
7801: type => 'public',
7802: time => {
7803: start => $newstart,
7804: end => $newend,
7805: },
7806: };
7807: my ($outcome,$deloutcome,$new_values,$translation) =
7808: &modify_access_controls($file_name,\%changes,$udom,$unum);
7809: return $outcome;
7810: }
7811: } else {
7812: return 'invalid';
7813: }
7814: }
7815:
1.745 raeburn 7816: #------------------------------------------------------Get Marked as Read Only
7817:
7818: sub get_marked_as_readonly {
7819: my ($domain,$user,$what,$group) = @_;
7820: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 7821: my @readonly_files;
1.629 banghart 7822: my $cmp1=$what;
7823: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 7824: while (my ($file_name,$value) = each(%{$current_permissions})) {
7825: if (defined($group)) {
7826: if ($file_name !~ m-^\Q$group\E/-) {
7827: next;
7828: }
7829: }
1.561 banghart 7830: if (ref($value) eq "ARRAY"){
7831: foreach my $stored_what (@{$value}) {
1.629 banghart 7832: my $cmp2=$stored_what;
1.759 albertel 7833: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 7834: $cmp2=join('',@{$stored_what});
1.745 raeburn 7835: }
1.629 banghart 7836: if ($cmp1 eq $cmp2) {
1.561 banghart 7837: push(@readonly_files, $file_name);
1.745 raeburn 7838: last;
1.563 banghart 7839: } elsif (!defined($what)) {
7840: push(@readonly_files, $file_name);
1.745 raeburn 7841: last;
1.561 banghart 7842: }
7843: }
1.745 raeburn 7844: }
1.561 banghart 7845: }
7846: return @readonly_files;
7847: }
1.577 banghart 7848: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 7849:
1.577 banghart 7850: sub get_marked_as_readonly_hash {
1.745 raeburn 7851: my ($current_permissions,$group,$what) = @_;
1.577 banghart 7852: my %readonly_files;
1.745 raeburn 7853: while (my ($file_name,$value) = each(%{$current_permissions})) {
7854: if (defined($group)) {
7855: if ($file_name !~ m-^\Q$group\E/-) {
7856: next;
7857: }
7858: }
1.577 banghart 7859: if (ref($value) eq "ARRAY"){
7860: foreach my $stored_what (@{$value}) {
1.745 raeburn 7861: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 7862: foreach my $lock_descriptor(@{$stored_what}) {
7863: if ($lock_descriptor eq 'graded') {
7864: $readonly_files{$file_name} = 'graded';
7865: } elsif ($lock_descriptor eq 'handback') {
7866: $readonly_files{$file_name} = 'handback';
7867: } else {
7868: if (!exists($readonly_files{$file_name})) {
7869: $readonly_files{$file_name} = 'locked';
7870: }
7871: }
1.745 raeburn 7872: }
1.750 banghart 7873: }
1.577 banghart 7874: }
7875: }
7876: }
7877: return %readonly_files;
7878: }
1.559 banghart 7879: # ------------------------------------------------------------ Unmark as Read Only
7880:
7881: sub unmark_as_readonly {
1.629 banghart 7882: # unmarks $file_name (if $file_name is defined), or all files locked by $what
7883: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 7884: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 7885: $file_name = &declutter_portfile($file_name);
1.634 albertel 7886: my $symb_crs = $what;
7887: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 7888: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 7889: my ($tmp)=keys(%current_permissions);
7890: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7891: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 7892: foreach my $file (@readonly_files) {
1.759 albertel 7893: my $clean_file = &declutter_portfile($file);
7894: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 7895: my $current_locks = $current_permissions{$file};
1.563 banghart 7896: my @new_locks;
7897: my @del_keys;
7898: if (ref($current_locks) eq "ARRAY"){
7899: foreach my $locker (@{$current_locks}) {
1.632 albertel 7900: my $compare=$locker;
1.749 raeburn 7901: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 7902: $compare=join('',@{$locker});
1.746 raeburn 7903: if ($compare ne $symb_crs) {
7904: push(@new_locks, $locker);
7905: }
1.563 banghart 7906: }
7907: }
1.650 albertel 7908: if (scalar(@new_locks) > 0) {
1.563 banghart 7909: $current_permissions{$file} = \@new_locks;
7910: } else {
7911: push(@del_keys, $file);
1.613 albertel 7912: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 7913: delete($current_permissions{$file});
1.563 banghart 7914: }
7915: }
1.561 banghart 7916: }
1.613 albertel 7917: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7918: return;
7919: }
1.512 banghart 7920:
1.17 www 7921: # ------------------------------------------------------------ Directory lister
7922:
7923: sub dirlist {
1.955 raeburn 7924: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 7925: $uri=~s/^\///;
7926: $uri=~s/\/$//;
1.253 stredwic 7927: my ($udom, $uname);
1.955 raeburn 7928: if ($getuserdir) {
1.253 stredwic 7929: $udom = $userdomain;
7930: $uname = $username;
1.955 raeburn 7931: } else {
7932: (undef,$udom,$uname)=split(/\//,$uri);
7933: if(defined($userdomain)) {
7934: $udom = $userdomain;
7935: }
7936: if(defined($username)) {
7937: $uname = $username;
7938: }
1.253 stredwic 7939: }
1.955 raeburn 7940: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 7941:
1.955 raeburn 7942: $dirRoot = $perlvar{'lonDocRoot'};
7943: if (defined($getpropath)) {
7944: $dirRoot = &propath($udom,$uname);
1.253 stredwic 7945: $dirRoot =~ s/\/$//;
1.955 raeburn 7946: } elsif (defined($getuserdir)) {
7947: my $subdir=$uname.'__';
7948: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
7949: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
7950: ."/$udom/$subdir/$uname";
7951: } elsif (defined($alternateRoot)) {
7952: $dirRoot = $alternateRoot;
1.751 banghart 7953: }
1.253 stredwic 7954:
7955: if($udom) {
7956: if($uname) {
1.955 raeburn 7957: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 7958: .$getuserdir.':'.&escape($dirRoot)
1.955 raeburn 7959: .':'.&escape($uname).':'.&escape($udom),
7960: &homeserver($uname,$udom));
7961: if ($listing eq 'unknown_cmd') {
7962: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
7963: &homeserver($uname,$udom));
7964: } else {
7965: @listing_results = map { &unescape($_); } split(/:/,$listing);
7966: }
1.605 matthew 7967: if ($listing eq 'unknown_cmd') {
1.800 albertel 7968: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
7969: &homeserver($uname,$udom));
1.605 matthew 7970: @listing_results = split(/:/,$listing);
7971: } else {
7972: @listing_results = map { &unescape($_); } split(/:/,$listing);
7973: }
7974: return @listing_results;
1.955 raeburn 7975: } elsif(!$alternateRoot) {
1.800 albertel 7976: my %allusers;
1.841 albertel 7977: my %servers = &get_servers($udom,'library');
1.955 raeburn 7978: foreach my $tryserver (keys(%servers)) {
7979: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
7980: &escape($udom),$tryserver);
7981: if ($listing eq 'unknown_cmd') {
7982: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
7983: $udom, $tryserver);
7984: } else {
7985: @listing_results = map { &unescape($_); } split(/:/,$listing);
7986: }
1.841 albertel 7987: if ($listing eq 'unknown_cmd') {
7988: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
7989: $udom, $tryserver);
7990: @listing_results = split(/:/,$listing);
7991: } else {
7992: @listing_results =
7993: map { &unescape($_); } split(/:/,$listing);
7994: }
7995: if ($listing_results[0] ne 'no_such_dir' &&
7996: $listing_results[0] ne 'empty' &&
7997: $listing_results[0] ne 'con_lost') {
7998: foreach my $line (@listing_results) {
7999: my ($entry) = split(/&/,$line,2);
8000: $allusers{$entry} = 1;
8001: }
8002: }
1.253 stredwic 8003: }
8004: my $alluserstr='';
1.800 albertel 8005: foreach my $user (sort(keys(%allusers))) {
8006: $alluserstr.=$user.'&user:';
1.253 stredwic 8007: }
8008: $alluserstr=~s/:$//;
8009: return split(/:/,$alluserstr);
8010: } else {
1.800 albertel 8011: return ('missing user name');
1.253 stredwic 8012: }
1.955 raeburn 8013: } elsif(!defined($getpropath)) {
1.841 albertel 8014: my @all_domains = sort(&all_domains());
1.955 raeburn 8015: foreach my $domain (@all_domains) {
8016: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
8017: }
8018: return @all_domains;
8019: } else {
1.800 albertel 8020: return ('missing domain');
1.275 stredwic 8021: }
8022: }
8023:
8024: # --------------------------------------------- GetFileTimestamp
8025: # This function utilizes dirlist and returns the date stamp for
8026: # when it was last modified. It will also return an error of -1
8027: # if an error occurs
8028:
8029: sub GetFileTimestamp {
1.955 raeburn 8030: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 8031: $studentDomain = &LONCAPA::clean_domain($studentDomain);
8032: $studentName = &LONCAPA::clean_username($studentName);
1.955 raeburn 8033: my ($fileStat) =
8034: &Apache::lonnet::dirlist($filename,$studentDomain,$studentName,
8035: undef,$getuserdir);
1.275 stredwic 8036: my @stats = split('&', $fileStat);
8037: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 8038: # @stats contains first the filename, then the stat output
8039: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 8040: } else {
8041: return -1;
1.253 stredwic 8042: }
1.26 www 8043: }
8044:
1.712 albertel 8045: sub stat_file {
8046: my ($uri) = @_;
1.787 albertel 8047: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 8048:
1.955 raeburn 8049: my ($udom,$uname,$file);
1.712 albertel 8050: if ($uri =~ m-^/(uploaded|editupload)/-) {
8051: ($udom,$uname,$file) =
1.811 albertel 8052: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 8053: $file = 'userfiles/'.$file;
8054: }
8055: if ($uri =~ m-^/res/-) {
8056: ($udom,$uname) =
1.807 albertel 8057: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 8058: $file = $uri;
8059: }
8060:
8061: if (!$udom || !$uname || !$file) {
8062: # unable to handle the uri
8063: return ();
8064: }
1.956 raeburn 8065: my $getpropath;
8066: if ($file =~ /^userfiles\//) {
8067: $getpropath = 1;
8068: }
1.955 raeburn 8069: my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712 albertel 8070: my @stats = split('&', $result);
1.721 banghart 8071:
1.712 albertel 8072: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
8073: shift(@stats); #filename is first
8074: return @stats;
8075: }
8076: return ();
8077: }
8078:
1.26 www 8079: # -------------------------------------------------------- Value of a Condition
8080:
1.713 albertel 8081: # gets the value of a specific preevaluated condition
8082: # stored in the string $env{user.state.<cid>}
8083: # or looks up a condition reference in the bighash and if if hasn't
8084: # already been evaluated recurses into docondval to get the value of
8085: # the condition, then memoizing it to
8086: # $env{user.state.<cid>.<condition>}
1.40 www 8087: sub directcondval {
8088: my $number=shift;
1.620 albertel 8089: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 8090: &Apache::lonuserstate::evalstate();
8091: }
1.713 albertel 8092: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
8093: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
8094: } elsif ($number =~ /^_/) {
8095: my $sub_condition;
8096: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8097: &GDBM_READER(),0640)) {
8098: $sub_condition=$bighash{'conditions'.$number};
8099: untie(%bighash);
8100: }
8101: my $value = &docondval($sub_condition);
1.949 raeburn 8102: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 8103: return $value;
8104: }
1.620 albertel 8105: if ($env{'user.state.'.$env{'request.course.id'}}) {
8106: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 8107: } else {
8108: return 2;
8109: }
8110: }
8111:
1.713 albertel 8112: # get the collection of conditions for this resource
1.26 www 8113: sub condval {
8114: my $condidx=shift;
1.54 www 8115: my $allpathcond='';
1.713 albertel 8116: foreach my $cond (split(/\|/,$condidx)) {
8117: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
8118: $allpathcond.=
8119: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
8120: }
1.191 harris41 8121: }
1.54 www 8122: $allpathcond=~s/\|$//;
1.713 albertel 8123: return &docondval($allpathcond);
8124: }
8125:
8126: #evaluates an expression of conditions
8127: sub docondval {
8128: my ($allpathcond) = @_;
8129: my $result=0;
8130: if ($env{'request.course.id'}
8131: && defined($allpathcond)) {
8132: my $operand='|';
8133: my @stack;
8134: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
8135: if ($chunk eq '(') {
8136: push @stack,($operand,$result);
8137: } elsif ($chunk eq ')') {
8138: my $before=pop @stack;
8139: if (pop @stack eq '&') {
8140: $result=$result>$before?$before:$result;
8141: } else {
8142: $result=$result>$before?$result:$before;
8143: }
8144: } elsif (($chunk eq '&') || ($chunk eq '|')) {
8145: $operand=$chunk;
8146: } else {
8147: my $new=directcondval($chunk);
8148: if ($operand eq '&') {
8149: $result=$result>$new?$new:$result;
8150: } else {
8151: $result=$result>$new?$result:$new;
8152: }
8153: }
8154: }
1.26 www 8155: }
8156: return $result;
1.421 albertel 8157: }
8158:
8159: # ---------------------------------------------------- Devalidate courseresdata
8160:
8161: sub devalidatecourseresdata {
8162: my ($coursenum,$coursedomain)=@_;
8163: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8164: &devalidate_cache_new('courseres',$hashid);
1.28 www 8165: }
8166:
1.763 www 8167:
1.200 www 8168: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 8169: #
8170: # Parameters:
8171: # $coursenum - Number of the course.
8172: # $coursedomain - Domain at which the course was created.
8173: # Returns:
8174: # A hash of the course parameters along (I think) with timestamps
8175: # and version info.
1.877 foxr 8176:
1.624 albertel 8177: sub get_courseresdata {
8178: my ($coursenum,$coursedomain)=@_;
1.200 www 8179: my $coursehom=&homeserver($coursenum,$coursedomain);
8180: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8181: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 8182: my %dumpreply;
1.417 albertel 8183: unless (defined($cached)) {
1.624 albertel 8184: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 8185: $result=\%dumpreply;
1.251 albertel 8186: my ($tmp) = keys(%dumpreply);
8187: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 8188: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 8189: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
8190: return $tmp;
1.416 albertel 8191: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 8192: $result=undef;
1.599 albertel 8193: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 8194: }
8195: }
1.624 albertel 8196: return $result;
8197: }
8198:
1.633 albertel 8199: sub devalidateuserresdata {
8200: my ($uname,$udom)=@_;
8201: my $hashid="$udom:$uname";
8202: &devalidate_cache_new('userres',$hashid);
8203: }
8204:
1.624 albertel 8205: sub get_userresdata {
8206: my ($uname,$udom)=@_;
8207: #most student don\'t have any data set, check if there is some data
8208: if (&EXT_cache_status($udom,$uname)) { return undef; }
8209:
8210: my $hashid="$udom:$uname";
8211: my ($result,$cached)=&is_cached_new('userres',$hashid);
8212: if (!defined($cached)) {
8213: my %resourcedata=&dump('resourcedata',$udom,$uname);
8214: $result=\%resourcedata;
8215: &do_cache_new('userres',$hashid,$result,600);
8216: }
8217: my ($tmp)=keys(%$result);
8218: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
8219: return $result;
8220: }
8221: #error 2 occurs when the .db doesn't exist
8222: if ($tmp!~/error: 2 /) {
1.672 albertel 8223: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 8224: " Trying to get resource data for ".
8225: $uname." at ".$udom.": ".
8226: $tmp."</font>");
8227: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 8228: #&EXT_cache_set($udom,$uname);
8229: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 8230: undef($tmp); # not really an error so don't send it back
1.624 albertel 8231: }
8232: return $tmp;
8233: }
1.879 foxr 8234: #----------------------------------------------- resdata - return resource data
8235: # Purpose:
8236: # Return resource data for either users or for a course.
8237: # Parameters:
8238: # $name - Course/user name.
8239: # $domain - Name of the domain the user/course is registered on.
8240: # $type - Type of thing $name is (must be 'course' or 'user'
8241: # @which - Array of names of resources desired.
8242: # Returns:
8243: # The value of the first reasource in @which that is found in the
8244: # resource hash.
8245: # Exceptional Conditions:
8246: # If the $type passed in is not valid (not the string 'course' or
8247: # 'user', an undefined reference is returned.
8248: # If none of the resources are found, an undef is returned
1.624 albertel 8249: sub resdata {
8250: my ($name,$domain,$type,@which)=@_;
8251: my $result;
8252: if ($type eq 'course') {
8253: $result=&get_courseresdata($name,$domain);
8254: } elsif ($type eq 'user') {
8255: $result=&get_userresdata($name,$domain);
8256: }
8257: if (!ref($result)) { return $result; }
1.251 albertel 8258: foreach my $item (@which) {
1.927 albertel 8259: if (defined($result->{$item->[0]})) {
8260: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 8261: }
1.250 albertel 8262: }
1.291 albertel 8263: return undef;
1.200 www 8264: }
8265:
1.379 matthew 8266: #
8267: # EXT resource caching routines
8268: #
8269:
8270: sub clear_EXT_cache_status {
1.383 albertel 8271: &delenv('cache.EXT.');
1.379 matthew 8272: }
8273:
8274: sub EXT_cache_status {
8275: my ($target_domain,$target_user) = @_;
1.383 albertel 8276: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 8277: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 8278: # We know already the user has no data
8279: return 1;
8280: } else {
8281: return 0;
8282: }
8283: }
8284:
8285: sub EXT_cache_set {
8286: my ($target_domain,$target_user) = @_;
1.383 albertel 8287: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 8288: #&appenv({$cachename => time});
1.379 matthew 8289: }
8290:
1.28 www 8291: # --------------------------------------------------------- Value of a Variable
1.58 www 8292: sub EXT {
1.715 albertel 8293:
1.395 albertel 8294: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 8295: unless ($varname) { return ''; }
1.218 albertel 8296: #get real user name/domain, courseid and symb
8297: my $courseid;
1.359 albertel 8298: my $publicuser;
1.427 www 8299: if ($symbparm) {
8300: $symbparm=&get_symb_from_alias($symbparm);
8301: }
1.218 albertel 8302: if (!($uname && $udom)) {
1.790 albertel 8303: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 8304: if (!$symbparm) { $symbparm=$cursymb; }
8305: } else {
1.620 albertel 8306: $courseid=$env{'request.course.id'};
1.218 albertel 8307: }
1.48 www 8308: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
8309: my $rest;
1.320 albertel 8310: if (defined($therest[0])) {
1.48 www 8311: $rest=join('.',@therest);
8312: } else {
8313: $rest='';
8314: }
1.320 albertel 8315:
1.57 www 8316: my $qualifierrest=$qualifier;
8317: if ($rest) { $qualifierrest.='.'.$rest; }
8318: my $spacequalifierrest=$space;
8319: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 8320: if ($realm eq 'user') {
1.48 www 8321: # --------------------------------------------------------------- user.resource
8322: if ($space eq 'resource') {
1.651 albertel 8323: if ( (defined($Apache::lonhomework::parsing_a_problem)
8324: || defined($Apache::lonhomework::parsing_a_task))
8325: &&
1.744 albertel 8326: ($symbparm eq &symbread()) ) {
8327: # if we are in the middle of processing the resource the
8328: # get the value we are planning on committing
8329: if (defined($Apache::lonhomework::results{$qualifierrest})) {
8330: return $Apache::lonhomework::results{$qualifierrest};
8331: } else {
8332: return $Apache::lonhomework::history{$qualifierrest};
8333: }
1.335 albertel 8334: } else {
1.359 albertel 8335: my %restored;
1.620 albertel 8336: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 8337: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
8338: } else {
8339: %restored=&restore($symbparm,$courseid,$udom,$uname);
8340: }
1.335 albertel 8341: return $restored{$qualifierrest};
8342: }
1.48 www 8343: # ----------------------------------------------------------------- user.access
8344: } elsif ($space eq 'access') {
1.218 albertel 8345: # FIXME - not supporting calls for a specific user
1.48 www 8346: return &allowed($qualifier,$rest);
8347: # ------------------------------------------ user.preferences, user.environment
8348: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 8349: if (($uname eq $env{'user.name'}) &&
8350: ($udom eq $env{'user.domain'})) {
8351: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 8352: } else {
1.359 albertel 8353: my %returnhash;
8354: if (!$publicuser) {
8355: %returnhash=&userenvironment($udom,$uname,
8356: $qualifierrest);
8357: }
1.218 albertel 8358: return $returnhash{$qualifierrest};
8359: }
1.48 www 8360: # ----------------------------------------------------------------- user.course
8361: } elsif ($space eq 'course') {
1.218 albertel 8362: # FIXME - not supporting calls for a specific user
1.620 albertel 8363: return $env{join('.',('request.course',$qualifier))};
1.48 www 8364: # ------------------------------------------------------------------- user.role
8365: } elsif ($space eq 'role') {
1.218 albertel 8366: # FIXME - not supporting calls for a specific user
1.620 albertel 8367: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 8368: if ($qualifier eq 'value') {
8369: return $role;
8370: } elsif ($qualifier eq 'extent') {
8371: return $where;
8372: }
8373: # ----------------------------------------------------------------- user.domain
8374: } elsif ($space eq 'domain') {
1.218 albertel 8375: return $udom;
1.48 www 8376: # ------------------------------------------------------------------- user.name
8377: } elsif ($space eq 'name') {
1.218 albertel 8378: return $uname;
1.48 www 8379: # ---------------------------------------------------- Any other user namespace
1.29 www 8380: } else {
1.359 albertel 8381: my %reply;
8382: if (!$publicuser) {
8383: %reply=&get($space,[$qualifierrest],$udom,$uname);
8384: }
8385: return $reply{$qualifierrest};
1.48 www 8386: }
1.236 www 8387: } elsif ($realm eq 'query') {
8388: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 8389: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
8390: [$spacequalifierrest]);
1.620 albertel 8391: return $env{'form.'.$spacequalifierrest};
1.236 www 8392: } elsif ($realm eq 'request') {
1.48 www 8393: # ------------------------------------------------------------- request.browser
8394: if ($space eq 'browser') {
1.430 www 8395: if ($qualifier eq 'textremote') {
1.676 albertel 8396: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 8397: return 1;
8398: } else {
8399: return 0;
8400: }
8401: } else {
1.620 albertel 8402: return $env{'browser.'.$qualifier};
1.430 www 8403: }
1.57 www 8404: # ------------------------------------------------------------ request.filename
8405: } else {
1.620 albertel 8406: return $env{'request.'.$spacequalifierrest};
1.29 www 8407: }
1.28 www 8408: } elsif ($realm eq 'course') {
1.48 www 8409: # ---------------------------------------------------------- course.description
1.620 albertel 8410: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 8411: } elsif ($realm eq 'resource') {
1.165 www 8412:
1.620 albertel 8413: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 8414: if (!$symbparm) { $symbparm=&symbread(); }
8415: }
1.693 albertel 8416:
8417: if ($space eq 'title') {
8418: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
8419: return &gettitle($symbparm);
8420: }
8421:
8422: if ($space eq 'map') {
8423: my ($map) = &decode_symb($symbparm);
8424: return &symbread($map);
8425: }
1.905 albertel 8426: if ($space eq 'filename') {
8427: if ($symbparm) {
8428: return &clutter((&decode_symb($symbparm))[2]);
8429: }
8430: return &hreflocation('',$env{'request.filename'});
8431: }
1.693 albertel 8432:
8433: my ($section, $group, @groups);
1.593 albertel 8434: my ($courselevelm,$courselevel);
1.539 albertel 8435: if ($symbparm && defined($courseid) &&
1.620 albertel 8436: $courseid eq $env{'request.course.id'}) {
1.165 www 8437:
1.218 albertel 8438: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 8439:
1.60 www 8440: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 8441: my $symbp=$symbparm;
1.735 albertel 8442: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 8443:
8444: my $symbparm=$symbp.'.'.$spacequalifierrest;
8445: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
8446:
1.620 albertel 8447: if (($env{'user.name'} eq $uname) &&
8448: ($env{'user.domain'} eq $udom)) {
8449: $section=$env{'request.course.sec'};
1.733 raeburn 8450: @groups = split(/:/,$env{'request.course.groups'});
8451: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 8452: } else {
1.539 albertel 8453: if (! defined($usection)) {
1.551 albertel 8454: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 8455: } else {
8456: $section = $usection;
8457: }
1.733 raeburn 8458: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 8459: }
8460:
8461: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
8462: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
8463: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
8464:
1.593 albertel 8465: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 8466: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 8467: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 8468:
1.60 www 8469: # ----------------------------------------------------------- first, check user
1.624 albertel 8470:
8471: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 8472: ([$courselevelr,'resource'],
8473: [$courselevelm,'map' ],
8474: [$courselevel, 'course' ]));
1.931 albertel 8475: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 8476:
1.594 albertel 8477: # ------------------------------------------------ second, check some of course
1.684 raeburn 8478: my $coursereply;
1.691 raeburn 8479: if (@groups > 0) {
8480: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
8481: $mapparm,$spacequalifierrest);
1.927 albertel 8482: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 8483: }
1.96 www 8484:
1.684 raeburn 8485: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 8486: $env{'course.'.$courseid.'.domain'},
8487: 'course',
8488: ([$seclevelr, 'resource'],
8489: [$seclevelm, 'map' ],
8490: [$seclevel, 'course' ],
8491: [$courselevelr,'resource']));
8492: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 8493:
1.60 www 8494: # ------------------------------------------------------ third, check map parms
1.218 albertel 8495: my %parmhash=();
8496: my $thisparm='';
8497: if (tie(%parmhash,'GDBM_File',
1.620 albertel 8498: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 8499: &GDBM_READER(),0640)) {
1.218 albertel 8500: $thisparm=$parmhash{$symbparm};
8501: untie(%parmhash);
8502: }
1.927 albertel 8503: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 8504: }
1.594 albertel 8505: # ------------------------------------------ fourth, look in resource metadata
1.71 www 8506:
1.218 albertel 8507: $spacequalifierrest=~s/\./\_/;
1.282 albertel 8508: my $filename;
8509: if (!$symbparm) { $symbparm=&symbread(); }
8510: if ($symbparm) {
1.409 www 8511: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 8512: } else {
1.620 albertel 8513: $filename=$env{'request.filename'};
1.282 albertel 8514: }
8515: my $metadata=&metadata($filename,$spacequalifierrest);
1.927 albertel 8516: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282 albertel 8517: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927 albertel 8518: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 8519:
1.927 albertel 8520: # ---------------------------------------------- fourth, look in rest of course
1.593 albertel 8521: if ($symbparm && defined($courseid) &&
1.620 albertel 8522: $courseid eq $env{'request.course.id'}) {
1.624 albertel 8523: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
8524: $env{'course.'.$courseid.'.domain'},
8525: 'course',
1.927 albertel 8526: ([$courselevelm,'map' ],
8527: [$courselevel, 'course']));
8528: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 8529: }
1.145 www 8530: # ------------------------------------------------------------------ Cascade up
1.218 albertel 8531: unless ($space eq '0') {
1.336 albertel 8532: my @parts=split(/_/,$space);
8533: my $id=pop(@parts);
8534: my $part=join('_',@parts);
8535: if ($part eq '') { $part='0'; }
1.927 albertel 8536: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 8537: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 8538: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 8539: }
1.395 albertel 8540: if ($recurse) { return undef; }
8541: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 8542: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 8543: # ---------------------------------------------------- Any other user namespace
8544: } elsif ($realm eq 'environment') {
8545: # ----------------------------------------------------------------- environment
1.620 albertel 8546: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
8547: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 8548: } else {
1.770 albertel 8549: if ($uname eq 'anonymous' && $udom eq '') {
8550: return '';
8551: }
1.219 albertel 8552: my %returnhash=&userenvironment($udom,$uname,
8553: $spacequalifierrest);
8554: return $returnhash{$spacequalifierrest};
8555: }
1.28 www 8556: } elsif ($realm eq 'system') {
1.48 www 8557: # ----------------------------------------------------------------- system.time
8558: if ($space eq 'time') {
8559: return time;
8560: }
1.696 albertel 8561: } elsif ($realm eq 'server') {
8562: # ----------------------------------------------------------------- system.time
8563: if ($space eq 'name') {
8564: return $ENV{'SERVER_NAME'};
8565: }
1.28 www 8566: }
1.48 www 8567: return '';
1.61 www 8568: }
8569:
1.927 albertel 8570: sub get_reply {
8571: my ($reply_value) = @_;
1.940 raeburn 8572: if (ref($reply_value) eq 'ARRAY') {
8573: if (wantarray) {
8574: return @$reply_value;
8575: }
8576: return $reply_value->[0];
8577: } else {
8578: return $reply_value;
1.927 albertel 8579: }
8580: }
8581:
1.691 raeburn 8582: sub check_group_parms {
8583: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
8584: my @groupitems = ();
8585: my $resultitem;
1.927 albertel 8586: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 8587: foreach my $group (@{$groups}) {
8588: foreach my $level (@levels) {
1.927 albertel 8589: my $item = $courseid.'.['.$group.'].'.$level->[0];
8590: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 8591: }
8592: }
8593: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
8594: $env{'course.'.$courseid.'.domain'},
8595: 'course',@groupitems);
8596: return $coursereply;
8597: }
8598:
8599: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 8600: my ($courseid,@groups) = @_;
8601: @groups = sort(@groups);
1.691 raeburn 8602: return @groups;
8603: }
8604:
1.395 albertel 8605: sub packages_tab_default {
8606: my ($uri,$varname)=@_;
8607: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 8608:
8609: my (@extension,@specifics,$do_default);
8610: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 8611: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 8612: if ($pack_type eq 'default') {
8613: $do_default=1;
8614: } elsif ($pack_type eq 'extension') {
8615: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 8616: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 8617: # only look at packages defaults for packages that this id is
1.738 albertel 8618: push(@specifics,[$package,$pack_type,$pack_part]);
8619: }
8620: }
8621: # first look for a package that matches the requested part id
8622: foreach my $package (@specifics) {
8623: my (undef,$pack_type,$pack_part)=@{$package};
8624: next if ($pack_part ne $part);
8625: if (defined($packagetab{"$pack_type&$name&default"})) {
8626: return $packagetab{"$pack_type&$name&default"};
8627: }
8628: }
8629: # look for any possible matching non extension_ package
8630: foreach my $package (@specifics) {
8631: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 8632: if (defined($packagetab{"$pack_type&$name&default"})) {
8633: return $packagetab{"$pack_type&$name&default"};
8634: }
1.585 albertel 8635: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 8636: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
8637: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 8638: }
8639: }
1.738 albertel 8640: # look for any posible extension_ match
8641: foreach my $package (@extension) {
8642: my ($package,$pack_type)=@{$package};
8643: if (defined($packagetab{"$pack_type&$name&default"})) {
8644: return $packagetab{"$pack_type&$name&default"};
8645: }
8646: if (defined($packagetab{$package."&$name&default"})) {
8647: return $packagetab{$package."&$name&default"};
8648: }
8649: }
8650: # look for a global default setting
8651: if ($do_default && defined($packagetab{"default&$name&default"})) {
8652: return $packagetab{"default&$name&default"};
8653: }
1.395 albertel 8654: return undef;
8655: }
8656:
1.334 albertel 8657: sub add_prefix_and_part {
8658: my ($prefix,$part)=@_;
8659: my $keyroot;
8660: if (defined($prefix) && $prefix !~ /^__/) {
8661: # prefix that has a part already
8662: $keyroot=$prefix;
8663: } elsif (defined($prefix)) {
8664: # prefix that is missing a part
8665: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
8666: } else {
8667: # no prefix at all
8668: if (defined($part)) { $keyroot='_'.$part; }
8669: }
8670: return $keyroot;
8671: }
8672:
1.71 www 8673: # ---------------------------------------------------------------- Get metadata
8674:
1.599 albertel 8675: my %metaentry;
1.71 www 8676: sub metadata {
1.176 www 8677: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 8678: $uri=&declutter($uri);
1.288 albertel 8679: # if it is a non metadata possible uri return quickly
1.529 albertel 8680: if (($uri eq '') ||
8681: (($uri =~ m|^/*adm/|) &&
1.698 albertel 8682: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1056.4.26 raeburn 8683: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 8684: return undef;
8685: }
8686: if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/})
8687: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 8688: return undef;
1.288 albertel 8689: }
1.73 www 8690: my $filename=$uri;
8691: $uri=~s/\.meta$//;
1.172 www 8692: #
8693: # Is the metadata already cached?
1.177 www 8694: # Look at timestamp of caching
1.172 www 8695: # Everything is cached by the main uri, libraries are never directly cached
8696: #
1.428 albertel 8697: if (!defined($liburi)) {
1.599 albertel 8698: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 8699: if (defined($cached)) { return $result->{':'.$what}; }
8700: }
8701: {
1.172 www 8702: #
8703: # Is this a recursive call for a library?
8704: #
1.599 albertel 8705: # if (! exists($metacache{$uri})) {
8706: # $metacache{$uri}={};
8707: # }
1.924 albertel 8708: my $cachetime = 60*60;
1.171 www 8709: if ($liburi) {
8710: $liburi=&declutter($liburi);
8711: $filename=$liburi;
1.401 bowersj2 8712: } else {
1.599 albertel 8713: &devalidate_cache_new('meta',$uri);
8714: undef(%metaentry);
1.401 bowersj2 8715: }
1.140 www 8716: my %metathesekeys=();
1.73 www 8717: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 8718: my $metastring;
1.924 albertel 8719: if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929 albertel 8720: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 8721: $metastring =
1.929 albertel 8722: &Apache::lonnet::ssi_body($which,
1.924 albertel 8723: ('grade_target' => 'meta'));
8724: $cachetime = 1; # only want this cached in the child not long term
1.1056.4.26 raeburn 8725: } elsif (($uri !~ m -^(editupload)/-) &&
8726: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 8727: my $file=&filelocation('',&clutter($filename));
1.599 albertel 8728: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 8729: $metastring=&getfile($file);
1.489 albertel 8730: }
1.208 albertel 8731: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 8732: my $token;
1.140 www 8733: undef %metathesekeys;
1.71 www 8734: while ($token=$parser->get_token) {
1.339 albertel 8735: if ($token->[0] eq 'S') {
8736: if (defined($token->[2]->{'package'})) {
1.172 www 8737: #
8738: # This is a package - get package info
8739: #
1.339 albertel 8740: my $package=$token->[2]->{'package'};
8741: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8742: if (defined($token->[2]->{'id'})) {
8743: $keyroot.='_'.$token->[2]->{'id'};
8744: }
1.599 albertel 8745: if ($metaentry{':packages'}) {
8746: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 8747: } else {
1.599 albertel 8748: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 8749: }
1.736 albertel 8750: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 8751: my $part=$keyroot;
8752: $part=~s/^\_//;
1.736 albertel 8753: if ($pack_entry=~/^\Q$package\E\&/ ||
8754: $pack_entry=~/^\Q$package\E_0\&/) {
8755: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 8756: # ignore package.tab specified default values
8757: # here &package_tab_default() will fetch those
8758: if ($subp eq 'default') { next; }
1.736 albertel 8759: my $value=$packagetab{$pack_entry};
1.432 albertel 8760: my $unikey;
8761: if ($pack =~ /_0$/) {
8762: $unikey='parameter_0_'.$name;
8763: $part=0;
8764: } else {
8765: $unikey='parameter'.$keyroot.'_'.$name;
8766: }
1.339 albertel 8767: if ($subp eq 'display') {
8768: $value.=' [Part: '.$part.']';
8769: }
1.599 albertel 8770: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 8771: $metathesekeys{$unikey}=1;
1.599 albertel 8772: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8773: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 8774: }
1.599 albertel 8775: if (defined($metaentry{':'.$unikey.'.default'})) {
8776: $metaentry{':'.$unikey}=
8777: $metaentry{':'.$unikey.'.default'};
1.356 albertel 8778: }
1.339 albertel 8779: }
8780: }
8781: } else {
1.172 www 8782: #
8783: # This is not a package - some other kind of start tag
1.339 albertel 8784: #
8785: my $entry=$token->[1];
8786: my $unikey;
8787: if ($entry eq 'import') {
8788: $unikey='';
8789: } else {
8790: $unikey=$entry;
8791: }
8792: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8793:
8794: if (defined($token->[2]->{'id'})) {
8795: $unikey.='_'.$token->[2]->{'id'};
8796: }
1.175 www 8797:
1.339 albertel 8798: if ($entry eq 'import') {
1.175 www 8799: #
8800: # Importing a library here
1.339 albertel 8801: #
8802: if ($depthcount<20) {
8803: my $location=$parser->get_text('/import');
8804: my $dir=$filename;
8805: $dir=~s|[^/]*$||;
8806: $location=&filelocation($dir,$location);
1.736 albertel 8807: my $metadata =
8808: &metadata($uri,'keys', $location,$unikey,
8809: $depthcount+1);
8810: foreach my $meta (split(',',$metadata)) {
8811: $metaentry{':'.$meta}=$metaentry{':'.$meta};
8812: $metathesekeys{$meta}=1;
1.339 albertel 8813: }
8814: }
8815: } else {
8816: if (defined($token->[2]->{'name'})) {
8817: $unikey.='_'.$token->[2]->{'name'};
8818: }
8819: $metathesekeys{$unikey}=1;
1.736 albertel 8820: foreach my $param (@{$token->[3]}) {
8821: $metaentry{':'.$unikey.'.'.$param} =
8822: $token->[2]->{$param};
1.339 albertel 8823: }
8824: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 8825: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 8826: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
8827: # only ws inside the tag, and not in default, so use default
8828: # as value
1.599 albertel 8829: $metaentry{':'.$unikey}=$default;
1.908 albertel 8830: } elsif ( $internaltext =~ /\S/ ) {
8831: # something interesting inside the tag
8832: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 8833: } else {
1.908 albertel 8834: # no interesting values, don't set a default
1.339 albertel 8835: }
1.172 www 8836: # end of not-a-package not-a-library import
1.339 albertel 8837: }
1.172 www 8838: # end of not-a-package start tag
1.339 albertel 8839: }
1.172 www 8840: # the next is the end of "start tag"
1.339 albertel 8841: }
8842: }
1.483 albertel 8843: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 8844: $extension = lc($extension);
8845: if ($extension eq 'htm') { $extension='html'; }
8846:
1.737 albertel 8847: foreach my $key (keys(%packagetab)) {
1.483 albertel 8848: #no specific packages #how's our extension
8849: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 8850: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 8851: \%metathesekeys);
8852: }
1.883 albertel 8853:
8854: if (!exists($metaentry{':packages'})
8855: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 8856: foreach my $key (keys(%packagetab)) {
1.483 albertel 8857: #no specific packages well let's get default then
8858: if ($key!~/^default&/) { next; }
1.488 albertel 8859: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 8860: \%metathesekeys);
8861: }
8862: }
1.338 www 8863: # are there custom rights to evaluate
1.599 albertel 8864: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 8865:
1.338 www 8866: #
8867: # Importing a rights file here
1.339 albertel 8868: #
8869: unless ($depthcount) {
1.599 albertel 8870: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 8871: my $dir=$filename;
8872: $dir=~s|[^/]*$||;
8873: $location=&filelocation($dir,$location);
1.736 albertel 8874: my $rights_metadata =
8875: &metadata($uri,'keys',$location,'_rights',
8876: $depthcount+1);
8877: foreach my $rights (split(',',$rights_metadata)) {
8878: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
8879: $metathesekeys{$rights}=1;
1.339 albertel 8880: }
8881: }
8882: }
1.737 albertel 8883: # uniqifiy package listing
8884: my %seen;
8885: my @uniq_packages =
8886: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
8887: $metaentry{':packages'} = join(',',@uniq_packages);
8888:
8889: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 8890: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
8891: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924 albertel 8892: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 8893: # this is the end of "was not already recently cached
1.71 www 8894: }
1.599 albertel 8895: return $metaentry{':'.$what};
1.261 albertel 8896: }
8897:
1.488 albertel 8898: sub metadata_create_package_def {
1.483 albertel 8899: my ($uri,$key,$package,$metathesekeys)=@_;
8900: my ($pack,$name,$subp)=split(/\&/,$key);
8901: if ($subp eq 'default') { next; }
8902:
1.599 albertel 8903: if (defined($metaentry{':packages'})) {
8904: $metaentry{':packages'}.=','.$package;
1.483 albertel 8905: } else {
1.599 albertel 8906: $metaentry{':packages'}=$package;
1.483 albertel 8907: }
8908: my $value=$packagetab{$key};
8909: my $unikey;
8910: $unikey='parameter_0_'.$name;
1.599 albertel 8911: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 8912: $$metathesekeys{$unikey}=1;
1.599 albertel 8913: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8914: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 8915: }
1.599 albertel 8916: if (defined($metaentry{':'.$unikey.'.default'})) {
8917: $metaentry{':'.$unikey}=
8918: $metaentry{':'.$unikey.'.default'};
1.483 albertel 8919: }
8920: }
8921:
1.261 albertel 8922: sub metadata_generate_part0 {
8923: my ($metadata,$metacache,$uri) = @_;
8924: my %allnames;
1.737 albertel 8925: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 8926: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 8927: my $part=$$metacache{':'.$metakey.'.part'};
8928: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 8929: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 8930: $allnames{$name}=$part;
8931: }
8932: }
8933: }
8934: foreach my $name (keys(%allnames)) {
8935: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 8936: my $key=":parameter_0_$name";
1.261 albertel 8937: $$metacache{"$key.part"}='0';
8938: $$metacache{"$key.name"}=$name;
1.428 albertel 8939: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 8940: $allnames{$name}.'_'.$name.
8941: '.type'};
1.428 albertel 8942: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 8943: '.display'};
1.644 www 8944: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 8945: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 8946: $$metacache{"$key.display"}=$olddis;
8947: }
1.71 www 8948: }
8949:
1.764 albertel 8950: # ------------------------------------------------------ Devalidate title cache
8951:
8952: sub devalidate_title_cache {
8953: my ($url)=@_;
8954: if (!$env{'request.course.id'}) { return; }
8955: my $symb=&symbread($url);
8956: if (!$symb) { return; }
8957: my $key=$env{'request.course.id'}."\0".$symb;
8958: &devalidate_cache_new('title',$key);
8959: }
8960:
1.1014 droeschl 8961: # ------------------------------------------------- Get the title of a course
8962:
8963: sub current_course_title {
8964: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
8965: }
1.301 www 8966: # ------------------------------------------------- Get the title of a resource
8967:
8968: sub gettitle {
8969: my $urlsymb=shift;
8970: my $symb=&symbread($urlsymb);
1.534 albertel 8971: if ($symb) {
1.620 albertel 8972: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 8973: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 8974: if (defined($cached)) {
8975: return $result;
8976: }
1.534 albertel 8977: my ($map,$resid,$url)=&decode_symb($symb);
8978: my $title='';
1.907 albertel 8979: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
8980: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
8981: } else {
8982: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8983: &GDBM_READER(),0640)) {
8984: my $mapid=$bighash{'map_pc_'.&clutter($map)};
8985: $title=$bighash{'title_'.$mapid.'.'.$resid};
8986: untie(%bighash);
8987: }
1.534 albertel 8988: }
8989: $title=~s/\&colon\;/\:/gs;
8990: if ($title) {
1.599 albertel 8991: return &do_cache_new('title',$key,$title,600);
1.534 albertel 8992: }
8993: $urlsymb=$url;
8994: }
8995: my $title=&metadata($urlsymb,'title');
8996: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
8997: return $title;
1.301 www 8998: }
1.613 albertel 8999:
1.614 albertel 9000: sub get_slot {
9001: my ($which,$cnum,$cdom)=@_;
9002: if (!$cnum || !$cdom) {
1.790 albertel 9003: (undef,my $courseid)=&whichuser();
1.620 albertel 9004: $cdom=$env{'course.'.$courseid.'.domain'};
9005: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 9006: }
1.703 albertel 9007: my $key=join("\0",'slots',$cdom,$cnum,$which);
9008: my %slotinfo;
9009: if (exists($remembered{$key})) {
9010: $slotinfo{$which} = $remembered{$key};
9011: } else {
9012: %slotinfo=&get('slots',[$which],$cdom,$cnum);
9013: &Apache::lonhomework::showhash(%slotinfo);
9014: my ($tmp)=keys(%slotinfo);
9015: if ($tmp=~/^error:/) { return (); }
9016: $remembered{$key} = $slotinfo{$which};
9017: }
1.616 albertel 9018: if (ref($slotinfo{$which}) eq 'HASH') {
9019: return %{$slotinfo{$which}};
9020: }
9021: return $slotinfo{$which};
1.614 albertel 9022: }
1.31 www 9023: # ------------------------------------------------- Update symbolic store links
9024:
9025: sub symblist {
9026: my ($mapname,%newhash)=@_;
1.438 www 9027: $mapname=&deversion(&declutter($mapname));
1.31 www 9028: my %hash;
1.620 albertel 9029: if (($env{'request.course.fn'}) && (%newhash)) {
9030: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 9031: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 9032: foreach my $url (keys(%newhash)) {
1.711 albertel 9033: next if ($url eq 'last_known'
9034: && $env{'form.no_update_last_known'});
9035: $hash{declutter($url)}=&encode_symb($mapname,
9036: $newhash{$url}->[1],
9037: $newhash{$url}->[0]);
1.191 harris41 9038: }
1.31 www 9039: if (untie(%hash)) {
9040: return 'ok';
9041: }
9042: }
9043: }
9044: return 'error';
1.212 www 9045: }
9046:
9047: # --------------------------------------------------------------- Verify a symb
9048:
9049: sub symbverify {
1.510 www 9050: my ($symb,$thisurl)=@_;
9051: my $thisfn=$thisurl;
1.439 www 9052: $thisfn=&declutter($thisfn);
1.215 www 9053: # direct jump to resource in page or to a sequence - will construct own symbs
9054: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
9055: # check URL part
1.409 www 9056: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 9057:
1.431 www 9058: unless ($url eq $thisfn) { return 0; }
1.213 www 9059:
1.216 www 9060: $symb=&symbclean($symb);
1.510 www 9061: $thisurl=&deversion($thisurl);
1.439 www 9062: $thisfn=&deversion($thisfn);
1.213 www 9063:
9064: my %bighash;
9065: my $okay=0;
1.431 www 9066:
1.620 albertel 9067: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9068: &GDBM_READER(),0640)) {
1.1032 raeburn 9069: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
9070: $thisurl =~ s/\?.+$//;
9071: }
1.510 www 9072: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 9073: unless ($ids) {
1.1056.4.21 raeburn 9074: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
9075: $ids=$bighash{$idkey};
1.216 www 9076: }
9077: if ($ids) {
9078: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 9079: foreach my $id (split(/\,/,$ids)) {
9080: my ($mapid,$resid)=split(/\./,$id);
1.1032 raeburn 9081: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
9082: $symb =~ s/\?.+$//;
9083: }
1.216 www 9084: if (
9085: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
9086: eq $symb) {
1.620 albertel 9087: if (($env{'request.role.adv'}) ||
1.1056.4.20 raeburn 9088: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
9089: ($thisurl eq '/adm/navmaps')) {
1.582 albertel 9090: $okay=1;
9091: }
9092: }
1.216 www 9093: }
9094: }
1.213 www 9095: untie(%bighash);
9096: }
9097: return $okay;
1.31 www 9098: }
9099:
1.210 www 9100: # --------------------------------------------------------------- Clean-up symb
9101:
9102: sub symbclean {
9103: my $symb=shift;
1.568 albertel 9104: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 9105: # remove version from map
9106: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 9107:
1.210 www 9108: # remove version from URL
9109: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 9110:
1.507 www 9111: # remove wrapper
9112:
1.510 www 9113: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 9114: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 9115: return $symb;
1.409 www 9116: }
9117:
9118: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 9119:
9120: sub encode_symb {
9121: my ($map,$resid,$url)=@_;
9122: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
9123: }
1.409 www 9124:
9125: sub decode_symb {
1.568 albertel 9126: my $symb=shift;
9127: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
9128: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 9129: return (&fixversion($map),$resid,&fixversion($url));
9130: }
9131:
9132: sub fixversion {
9133: my $fn=shift;
1.609 banghart 9134: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 9135: my %bighash;
9136: my $uri=&clutter($fn);
1.620 albertel 9137: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 9138: # is this cached?
1.599 albertel 9139: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 9140: if (defined($cached)) { return $result; }
9141: # unfortunately not cached, or expired
1.620 albertel 9142: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 9143: &GDBM_READER(),0640)) {
9144: if ($bighash{'version_'.$uri}) {
9145: my $version=$bighash{'version_'.$uri};
1.444 www 9146: unless (($version eq 'mostrecent') ||
9147: ($version==&getversion($uri))) {
1.440 www 9148: $uri=~s/\.(\w+)$/\.$version\.$1/;
9149: }
9150: }
9151: untie %bighash;
1.413 www 9152: }
1.599 albertel 9153: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 9154: }
9155:
9156: sub deversion {
9157: my $url=shift;
9158: $url=~s/\.\d+\.(\w+)$/\.$1/;
9159: return $url;
1.210 www 9160: }
9161:
1.31 www 9162: # ------------------------------------------------------ Return symb list entry
9163:
9164: sub symbread {
1.249 www 9165: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 9166: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 9167: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 9168: # no filename provided? try from environment
1.44 www 9169: unless ($thisfn) {
1.620 albertel 9170: if ($env{'request.symb'}) {
9171: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 9172: }
1.620 albertel 9173: $thisfn=$env{'request.filename'};
1.44 www 9174: }
1.569 albertel 9175: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 9176: # is that filename actually a symb? Verify, clean, and return
9177: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 9178: if (&symbverify($thisfn,$1)) {
1.620 albertel 9179: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 9180: }
1.242 www 9181: }
1.44 www 9182: $thisfn=declutter($thisfn);
1.31 www 9183: my %hash;
1.37 www 9184: my %bighash;
9185: my $syval='';
1.620 albertel 9186: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 9187: my $targetfn = $thisfn;
1.609 banghart 9188: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 9189: $targetfn = 'adm/wrapper/'.$thisfn;
9190: }
1.687 albertel 9191: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
9192: $targetfn=$1;
9193: }
1.620 albertel 9194: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 9195: &GDBM_READER(),0640)) {
1.481 raeburn 9196: $syval=$hash{$targetfn};
1.37 www 9197: untie(%hash);
9198: }
9199: # ---------------------------------------------------------- There was an entry
9200: if ($syval) {
1.601 albertel 9201: #unless ($syval=~/\_\d+$/) {
1.620 albertel 9202: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 9203: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9204: #return $env{$cache_str}='';
1.601 albertel 9205: #}
9206: #$syval.=$1;
9207: #}
1.37 www 9208: } else {
9209: # ------------------------------------------------------- Was not in symb table
1.620 albertel 9210: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9211: &GDBM_READER(),0640)) {
1.37 www 9212: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 9213: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 9214: unless ($ids) {
9215: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 9216: }
9217: unless ($ids) {
9218: # alias?
9219: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 9220: }
1.37 www 9221: if ($ids) {
9222: # ------------------------------------------------------------------- Has ID(s)
9223: my @possibilities=split(/\,/,$ids);
1.39 www 9224: if ($#possibilities==0) {
9225: # ----------------------------------------------- There is only one possibility
1.37 www 9226: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 9227: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9228: $resid,$thisfn);
1.249 www 9229: } elsif (!$donotrecurse) {
1.39 www 9230: # ------------------------------------------ There is more than one possibility
9231: my $realpossible=0;
1.800 albertel 9232: foreach my $id (@possibilities) {
9233: my $file=$bighash{'src_'.$id};
1.39 www 9234: if (&allowed('bre',$file)) {
1.800 albertel 9235: my ($mapid,$resid)=split(/\./,$id);
1.39 www 9236: if ($bighash{'map_type_'.$mapid} ne 'page') {
9237: $realpossible++;
1.626 albertel 9238: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9239: $resid,$thisfn);
1.39 www 9240: }
9241: }
1.191 harris41 9242: }
1.39 www 9243: if ($realpossible!=1) { $syval=''; }
1.249 www 9244: } else {
9245: $syval='';
1.37 www 9246: }
9247: }
9248: untie(%bighash)
1.481 raeburn 9249: }
1.31 www 9250: }
1.62 www 9251: if ($syval) {
1.620 albertel 9252: return $env{$cache_str}=$syval;
1.62 www 9253: }
1.31 www 9254: }
1.949 raeburn 9255: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9256: return $env{$cache_str}='';
1.31 www 9257: }
9258:
9259: # ---------------------------------------------------------- Return random seed
9260:
1.32 www 9261: sub numval {
9262: my $txt=shift;
9263: $txt=~tr/A-J/0-9/;
9264: $txt=~tr/a-j/0-9/;
9265: $txt=~tr/K-T/0-9/;
9266: $txt=~tr/k-t/0-9/;
9267: $txt=~tr/U-Z/0-5/;
9268: $txt=~tr/u-z/0-5/;
9269: $txt=~s/\D//g;
1.564 albertel 9270: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 9271: return int($txt);
1.368 albertel 9272: }
9273:
1.484 albertel 9274: sub numval2 {
9275: my $txt=shift;
9276: $txt=~tr/A-J/0-9/;
9277: $txt=~tr/a-j/0-9/;
9278: $txt=~tr/K-T/0-9/;
9279: $txt=~tr/k-t/0-9/;
9280: $txt=~tr/U-Z/0-5/;
9281: $txt=~tr/u-z/0-5/;
9282: $txt=~s/\D//g;
9283: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9284: my $total;
9285: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 9286: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 9287: return int($total);
9288: }
9289:
1.575 albertel 9290: sub numval3 {
9291: use integer;
9292: my $txt=shift;
9293: $txt=~tr/A-J/0-9/;
9294: $txt=~tr/a-j/0-9/;
9295: $txt=~tr/K-T/0-9/;
9296: $txt=~tr/k-t/0-9/;
9297: $txt=~tr/U-Z/0-5/;
9298: $txt=~tr/u-z/0-5/;
9299: $txt=~s/\D//g;
9300: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9301: my $total;
9302: foreach my $val (@txts) { $total+=$val; }
9303: if ($_64bit) { $total=(($total<<32)>>32); }
9304: return $total;
9305: }
9306:
1.675 albertel 9307: sub digest {
9308: my ($data)=@_;
9309: my $digest=&Digest::MD5::md5($data);
9310: my ($a,$b,$c,$d)=unpack("iiii",$digest);
9311: my ($e,$f);
9312: {
9313: use integer;
9314: $e=($a+$b);
9315: $f=($c+$d);
9316: if ($_64bit) {
9317: $e=(($e<<32)>>32);
9318: $f=(($f<<32)>>32);
9319: }
9320: }
9321: if (wantarray) {
9322: return ($e,$f);
9323: } else {
9324: my $g;
9325: {
9326: use integer;
9327: $g=($e+$f);
9328: if ($_64bit) {
9329: $g=(($g<<32)>>32);
9330: }
9331: }
9332: return $g;
9333: }
9334: }
9335:
1.368 albertel 9336: sub latest_rnd_algorithm_id {
1.675 albertel 9337: return '64bit5';
1.366 albertel 9338: }
1.32 www 9339:
1.503 albertel 9340: sub get_rand_alg {
9341: my ($courseid)=@_;
1.790 albertel 9342: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 9343: if ($courseid) {
1.620 albertel 9344: return $env{"course.$courseid.rndseed"};
1.503 albertel 9345: }
9346: return &latest_rnd_algorithm_id();
9347: }
9348:
1.562 albertel 9349: sub validCODE {
9350: my ($CODE)=@_;
9351: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
9352: return 0;
9353: }
9354:
1.491 albertel 9355: sub getCODE {
1.620 albertel 9356: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 9357: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
9358: defined($Apache::lonhomework::parsing_a_task) ) &&
9359: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 9360: return $Apache::lonhomework::history{'resource.CODE'};
9361: }
9362: return undef;
9363: }
9364:
1.31 www 9365: sub rndseed {
1.155 albertel 9366: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 9367: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 9368: if (!defined($symb)) {
1.366 albertel 9369: unless ($symb=$wsymb) { return time; }
9370: }
9371: if (!$courseid) { $courseid=$wcourseid; }
9372: if (!$domain) { $domain=$wdomain; }
9373: if (!$username) { $username=$wusername }
1.503 albertel 9374: my $which=&get_rand_alg();
1.803 albertel 9375:
1.491 albertel 9376: if (defined(&getCODE())) {
1.675 albertel 9377: if ($which eq '64bit5') {
9378: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
9379: } elsif ($which eq '64bit4') {
1.575 albertel 9380: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
9381: } else {
9382: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
9383: }
1.675 albertel 9384: } elsif ($which eq '64bit5') {
9385: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 9386: } elsif ($which eq '64bit4') {
9387: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 9388: } elsif ($which eq '64bit3') {
9389: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 9390: } elsif ($which eq '64bit2') {
9391: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 9392: } elsif ($which eq '64bit') {
9393: return &rndseed_64bit($symb,$courseid,$domain,$username);
9394: }
9395: return &rndseed_32bit($symb,$courseid,$domain,$username);
9396: }
9397:
9398: sub rndseed_32bit {
9399: my ($symb,$courseid,$domain,$username)=@_;
9400: {
9401: use integer;
9402: my $symbchck=unpack("%32C*",$symb) << 27;
9403: my $symbseed=numval($symb) << 22;
9404: my $namechck=unpack("%32C*",$username) << 17;
9405: my $nameseed=numval($username) << 12;
9406: my $domainseed=unpack("%32C*",$domain) << 7;
9407: my $courseseed=unpack("%32C*",$courseid);
9408: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 9409: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9410: #&logthis("rndseed :$num:$symb");
1.564 albertel 9411: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 9412: return $num;
9413: }
9414: }
9415:
9416: sub rndseed_64bit {
9417: my ($symb,$courseid,$domain,$username)=@_;
9418: {
9419: use integer;
9420: my $symbchck=unpack("%32S*",$symb) << 21;
9421: my $symbseed=numval($symb) << 10;
9422: my $namechck=unpack("%32S*",$username);
9423:
9424: my $nameseed=numval($username) << 21;
9425: my $domainseed=unpack("%32S*",$domain) << 10;
9426: my $courseseed=unpack("%32S*",$courseid);
9427:
9428: my $num1=$symbchck+$symbseed+$namechck;
9429: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9430: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9431: #&logthis("rndseed :$num:$symb");
1.564 albertel 9432: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 9433: return "$num1,$num2";
1.155 albertel 9434: }
1.366 albertel 9435: }
9436:
1.443 albertel 9437: sub rndseed_64bit2 {
9438: my ($symb,$courseid,$domain,$username)=@_;
9439: {
9440: use integer;
9441: # strings need to be an even # of cahracters long, it it is odd the
9442: # last characters gets thrown away
9443: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9444: my $symbseed=numval($symb) << 10;
9445: my $namechck=unpack("%32S*",$username.' ');
9446:
9447: my $nameseed=numval($username) << 21;
1.501 albertel 9448: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9449: my $courseseed=unpack("%32S*",$courseid.' ');
9450:
9451: my $num1=$symbchck+$symbseed+$namechck;
9452: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9453: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9454: #&logthis("rndseed :$num:$symb");
1.803 albertel 9455: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 9456: return "$num1,$num2";
9457: }
9458: }
9459:
9460: sub rndseed_64bit3 {
9461: my ($symb,$courseid,$domain,$username)=@_;
9462: {
9463: use integer;
9464: # strings need to be an even # of cahracters long, it it is odd the
9465: # last characters gets thrown away
9466: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9467: my $symbseed=numval2($symb) << 10;
9468: my $namechck=unpack("%32S*",$username.' ');
9469:
9470: my $nameseed=numval2($username) << 21;
1.443 albertel 9471: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9472: my $courseseed=unpack("%32S*",$courseid.' ');
9473:
9474: my $num1=$symbchck+$symbseed+$namechck;
9475: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9476: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9477: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 9478: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9479:
1.503 albertel 9480: return "$num1:$num2";
1.443 albertel 9481: }
9482: }
9483:
1.575 albertel 9484: sub rndseed_64bit4 {
9485: my ($symb,$courseid,$domain,$username)=@_;
9486: {
9487: use integer;
9488: # strings need to be an even # of cahracters long, it it is odd the
9489: # last characters gets thrown away
9490: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9491: my $symbseed=numval3($symb) << 10;
9492: my $namechck=unpack("%32S*",$username.' ');
9493:
9494: my $nameseed=numval3($username) << 21;
9495: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9496: my $courseseed=unpack("%32S*",$courseid.' ');
9497:
9498: my $num1=$symbchck+$symbseed+$namechck;
9499: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9500: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9501: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 9502: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9503:
9504: return "$num1:$num2";
9505: }
9506: }
9507:
1.675 albertel 9508: sub rndseed_64bit5 {
9509: my ($symb,$courseid,$domain,$username)=@_;
9510: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
9511: return "$num1:$num2";
9512: }
9513:
1.366 albertel 9514: sub rndseed_CODE_64bit {
9515: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 9516: {
1.366 albertel 9517: use integer;
1.443 albertel 9518: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 9519: my $symbseed=numval2($symb);
1.491 albertel 9520: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9521: my $CODEseed=numval(&getCODE());
1.443 albertel 9522: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 9523: my $num1=$symbseed+$CODEchck;
9524: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9525: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9526: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 9527: if ($_64bit) { $num1=(($num1<<32)>>32); }
9528: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 9529: return "$num1:$num2";
1.366 albertel 9530: }
9531: }
9532:
1.575 albertel 9533: sub rndseed_CODE_64bit4 {
9534: my ($symb,$courseid,$domain,$username)=@_;
9535: {
9536: use integer;
9537: my $symbchck=unpack("%32S*",$symb.' ') << 16;
9538: my $symbseed=numval3($symb);
9539: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9540: my $CODEseed=numval3(&getCODE());
9541: my $courseseed=unpack("%32S*",$courseid.' ');
9542: my $num1=$symbseed+$CODEchck;
9543: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9544: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9545: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 9546: if ($_64bit) { $num1=(($num1<<32)>>32); }
9547: if ($_64bit) { $num2=(($num2<<32)>>32); }
9548: return "$num1:$num2";
9549: }
9550: }
9551:
1.675 albertel 9552: sub rndseed_CODE_64bit5 {
9553: my ($symb,$courseid,$domain,$username)=@_;
9554: my $code = &getCODE();
9555: my ($num1,$num2)=&digest("$symb,$courseid,$code");
9556: return "$num1:$num2";
9557: }
9558:
1.366 albertel 9559: sub setup_random_from_rndseed {
9560: my ($rndseed)=@_;
1.503 albertel 9561: if ($rndseed =~/([,:])/) {
9562: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 9563: &Math::Random::random_set_seed(abs($num1),abs($num2));
9564: } else {
9565: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 9566: }
1.36 albertel 9567: }
9568:
1.474 albertel 9569: sub latest_receipt_algorithm_id {
1.835 albertel 9570: return 'receipt3';
1.474 albertel 9571: }
9572:
1.480 www 9573: sub recunique {
9574: my $fucourseid=shift;
9575: my $unique;
1.835 albertel 9576: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
9577: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9578: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 9579: } else {
9580: $unique=$perlvar{'lonReceipt'};
9581: }
9582: return unpack("%32C*",$unique);
9583: }
9584:
9585: sub recprefix {
9586: my $fucourseid=shift;
9587: my $prefix;
1.835 albertel 9588: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
9589: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9590: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 9591: } else {
9592: $prefix=$perlvar{'lonHostID'};
9593: }
9594: return unpack("%32C*",$prefix);
9595: }
9596:
1.76 www 9597: sub ireceipt {
1.474 albertel 9598: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 9599:
9600: my $return =&recprefix($fucourseid).'-';
9601:
9602: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
9603: $env{'request.state'} eq 'construct') {
9604: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
9605: return $return;
9606: }
9607:
1.76 www 9608: my $cuname=unpack("%32C*",$funame);
9609: my $cudom=unpack("%32C*",$fudom);
9610: my $cucourseid=unpack("%32C*",$fucourseid);
9611: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 9612: my $cunique=&recunique($fucourseid);
1.474 albertel 9613: my $cpart=unpack("%32S*",$part);
1.835 albertel 9614: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
9615:
1.790 albertel 9616: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 9617:
9618: $return.= ($cunique%$cuname+
9619: $cunique%$cudom+
9620: $cusymb%$cuname+
9621: $cusymb%$cudom+
9622: $cucourseid%$cuname+
9623: $cucourseid%$cudom+
9624: $cpart%$cuname+
9625: $cpart%$cudom);
9626: } else {
9627: $return.= ($cunique%$cuname+
9628: $cunique%$cudom+
9629: $cusymb%$cuname+
9630: $cusymb%$cudom+
9631: $cucourseid%$cuname+
9632: $cucourseid%$cudom);
9633: }
9634: return $return;
1.76 www 9635: }
9636:
9637: sub receipt {
1.474 albertel 9638: my ($part)=@_;
1.790 albertel 9639: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 9640: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 9641: }
1.260 ng 9642:
1.790 albertel 9643: sub whichuser {
9644: my ($passedsymb)=@_;
9645: my ($symb,$courseid,$domain,$name,$publicuser);
9646: if (defined($env{'form.grade_symb'})) {
9647: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
9648: my $allowed=&allowed('vgr',$tmp_courseid);
9649: if (!$allowed &&
9650: exists($env{'request.course.sec'}) &&
9651: $env{'request.course.sec'} !~ /^\s*$/) {
9652: $allowed=&allowed('vgr',$tmp_courseid.
9653: '/'.$env{'request.course.sec'});
9654: }
9655: if ($allowed) {
9656: ($symb)=&get_env_multiple('form.grade_symb');
9657: $courseid=$tmp_courseid;
9658: ($domain)=&get_env_multiple('form.grade_domain');
9659: ($name)=&get_env_multiple('form.grade_username');
9660: return ($symb,$courseid,$domain,$name,$publicuser);
9661: }
9662: }
9663: if (!$passedsymb) {
9664: $symb=&symbread();
9665: } else {
9666: $symb=$passedsymb;
9667: }
9668: $courseid=$env{'request.course.id'};
9669: $domain=$env{'user.domain'};
9670: $name=$env{'user.name'};
9671: if ($name eq 'public' && $domain eq 'public') {
9672: if (!defined($env{'form.username'})) {
9673: $env{'form.username'}.=time.rand(10000000);
9674: }
9675: $name.=$env{'form.username'};
9676: }
9677: return ($symb,$courseid,$domain,$name,$publicuser);
9678:
9679: }
9680:
1.36 albertel 9681: # ------------------------------------------------------------ Serves up a file
1.472 albertel 9682: # returns either the contents of the file or
9683: # -1 if the file doesn't exist
1.481 raeburn 9684: #
9685: # if the target is a file that was uploaded via DOCS,
9686: # a check will be made to see if a current copy exists on the local server,
9687: # if it does this will be served, otherwise a copy will be retrieved from
9688: # the home server for the course and stored in /home/httpd/html/userfiles on
9689: # the local server.
1.472 albertel 9690:
1.36 albertel 9691: sub getfile {
1.538 albertel 9692: my ($file) = @_;
1.609 banghart 9693: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 9694: &repcopy($file);
9695: return &readfile($file);
9696: }
9697:
9698: sub repcopy_userfile {
9699: my ($file)=@_;
1.609 banghart 9700: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 9701: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 9702: my ($cdom,$cnum,$filename) =
1.811 albertel 9703: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 9704: my $uri="/uploaded/$cdom/$cnum/$filename";
9705: if (-e "$file") {
1.828 www 9706: # we already have a local copy, check it out
1.538 albertel 9707: my @fileinfo = stat($file);
1.828 www 9708: my $rtncode;
9709: my $info;
1.538 albertel 9710: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 9711: if ($lwpresp ne 'ok') {
1.828 www 9712: # there is no such file anymore, even though we had a local copy
1.482 albertel 9713: if ($rtncode eq '404') {
1.538 albertel 9714: unlink($file);
1.482 albertel 9715: }
9716: return -1;
9717: }
9718: if ($info < $fileinfo[9]) {
1.828 www 9719: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 9720: return 'ok';
1.828 www 9721: } else {
9722: # the file is outdated, get rid of it
9723: unlink($file);
1.482 albertel 9724: }
1.828 www 9725: }
9726: # one way or the other, at this point, we don't have the file
9727: # construct the correct path for the file
9728: my @parts = ($cdom,$cnum);
9729: if ($filename =~ m|^(.+)/[^/]+$|) {
9730: push @parts, split(/\//,$1);
9731: }
9732: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
9733: foreach my $part (@parts) {
9734: $path .= '/'.$part;
9735: if (!-e $path) {
9736: mkdir($path,0770);
1.482 albertel 9737: }
9738: }
1.828 www 9739: # now the path exists for sure
9740: # get a user agent
9741: my $ua=new LWP::UserAgent;
9742: my $transferfile=$file.'.in.transfer';
9743: # FIXME: this should flock
9744: if (-e $transferfile) { return 'ok'; }
9745: my $request;
9746: $uri=~s/^\///;
1.980 raeburn 9747: my $homeserver = &homeserver($cnum,$cdom);
9748: my $protocol = $protocol{$homeserver};
9749: $protocol = 'http' if ($protocol ne 'https');
9750: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 9751: my $response=$ua->request($request,$transferfile);
9752: # did it work?
9753: if ($response->is_error()) {
9754: unlink($transferfile);
9755: &logthis("Userfile repcopy failed for $uri");
9756: return -1;
9757: }
9758: # worked, rename the transfer file
9759: rename($transferfile,$file);
1.607 raeburn 9760: return 'ok';
1.481 raeburn 9761: }
9762:
1.517 albertel 9763: sub tokenwrapper {
9764: my $uri=shift;
1.980 raeburn 9765: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 9766: $uri=~s|^/||;
1.620 albertel 9767: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 9768: my $token=$1;
1.552 albertel 9769: my (undef,$udom,$uname,$file)=split('/',$uri,4);
9770: if ($udom && $uname && $file) {
9771: $file=~s|(\?\.*)*$||;
1.949 raeburn 9772: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 9773: my $homeserver = &homeserver($uname,$udom);
9774: my $protocol = $protocol{$homeserver};
9775: $protocol = 'http' if ($protocol ne 'https');
9776: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 9777: (($uri=~/\?/)?'&':'?').'token='.$token.
9778: '&tokenissued='.$perlvar{'lonHostID'};
9779: } else {
9780: return '/adm/notfound.html';
9781: }
9782: }
9783:
1.828 www 9784: # call with reqtype HEAD: get last modification time
9785: # call with reqtype GET: get the file contents
9786: # Do not call this with reqtype GET for large files! It loads everything into memory
9787: #
1.481 raeburn 9788: sub getuploaded {
9789: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
9790: $uri=~s/^\///;
1.980 raeburn 9791: my $homeserver = &homeserver($cnum,$cdom);
9792: my $protocol = $protocol{$homeserver};
9793: $protocol = 'http' if ($protocol ne 'https');
9794: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 9795: my $ua=new LWP::UserAgent;
9796: my $request=new HTTP::Request($reqtype,$uri);
9797: my $response=$ua->request($request);
9798: $$rtncode = $response->code;
1.482 albertel 9799: if (! $response->is_success()) {
9800: return 'failed';
9801: }
9802: if ($reqtype eq 'HEAD') {
1.486 www 9803: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 9804: } elsif ($reqtype eq 'GET') {
9805: $$info = $response->content;
1.472 albertel 9806: }
1.482 albertel 9807: return 'ok';
1.36 albertel 9808: }
9809:
1.481 raeburn 9810: sub readfile {
9811: my $file = shift;
9812: if ( (! -e $file ) || ($file eq '') ) { return -1; };
9813: my $fh;
9814: open($fh,"<$file");
9815: my $a='';
1.800 albertel 9816: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 9817: return $a;
9818: }
9819:
1.36 albertel 9820: sub filelocation {
1.590 banghart 9821: my ($dir,$file) = @_;
9822: my $location;
9823: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 9824:
9825: if ($file =~ m-^/adm/-) {
9826: $file=~s-^/adm/wrapper/-/-;
9827: $file=~s-^/adm/coursedocs/showdoc/-/-;
9828: }
1.882 albertel 9829:
1.590 banghart 9830: if ($file=~m:^/~:) { # is a contruction space reference
9831: $location = $file;
9832: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 9833: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 9834: # is a correct contruction space reference
9835: $location = $file;
1.956 raeburn 9836: } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
9837: $location = $file;
1.609 banghart 9838: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 9839: my ($udom,$uname,$filename)=
1.811 albertel 9840: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 9841: my $home=&homeserver($uname,$udom);
9842: my $is_me=0;
9843: my @ids=¤t_machine_ids();
9844: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
9845: if ($is_me) {
1.955 raeburn 9846: $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 9847: } else {
9848: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
9849: $udom.'/'.$uname.'/'.$filename;
9850: }
1.882 albertel 9851: } elsif ($file =~ m-^/adm/-) {
9852: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 9853: } else {
9854: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
9855: $file=~s:^/res/:/:;
9856: if ( !( $file =~ m:^/:) ) {
9857: $location = $dir. '/'.$file;
9858: } else {
9859: $location = '/home/httpd/html/res'.$file;
9860: }
1.59 albertel 9861: }
1.590 banghart 9862: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 9863: while ($location=~m{/\.\./}) {
9864: if ($location =~ m{/[^/]+/\.\./}) {
9865: $location=~ s{/[^/]+/\.\./}{/}g;
9866: } else {
9867: $location=~ s{/\.\./}{/}g;
9868: }
9869: } #remove dir/..
1.590 banghart 9870: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
9871: return $location;
1.46 www 9872: }
1.36 albertel 9873:
1.46 www 9874: sub hreflocation {
9875: my ($dir,$file)=@_;
1.980 raeburn 9876: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 9877: $file=filelocation($dir,$file);
1.700 albertel 9878: } elsif ($file=~m-^/adm/-) {
9879: $file=~s-^/adm/wrapper/-/-;
9880: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 9881: }
9882: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
9883: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 9884: } elsif ($file=~m-/home/($match_username)/public_html/-) {
9885: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 9886: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 9887: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 9888: -/uploaded/$1/$2/-x;
1.46 www 9889: }
1.913 albertel 9890: if ($file=~ m{^/userfiles/}) {
9891: $file =~ s{^/userfiles/}{/uploaded/};
9892: }
1.462 albertel 9893: return $file;
1.465 albertel 9894: }
9895:
9896: sub current_machine_domains {
1.853 albertel 9897: return &machine_domains(&hostname($perlvar{'lonHostID'}));
9898: }
9899:
9900: sub machine_domains {
9901: my ($hostname) = @_;
1.465 albertel 9902: my @domains;
1.838 albertel 9903: my %hostname = &all_hostnames();
1.465 albertel 9904: while( my($id, $name) = each(%hostname)) {
1.467 matthew 9905: # &logthis("-$id-$name-$hostname-");
1.465 albertel 9906: if ($hostname eq $name) {
1.844 albertel 9907: push(@domains,&host_domain($id));
1.465 albertel 9908: }
9909: }
9910: return @domains;
9911: }
9912:
9913: sub current_machine_ids {
1.853 albertel 9914: return &machine_ids(&hostname($perlvar{'lonHostID'}));
9915: }
9916:
9917: sub machine_ids {
9918: my ($hostname) = @_;
9919: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 9920: my @ids;
1.888 albertel 9921: my %name_to_host = &all_names();
1.889 albertel 9922: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
9923: return @{ $name_to_host{$hostname} };
9924: }
9925: return;
1.31 www 9926: }
9927:
1.824 raeburn 9928: sub additional_machine_domains {
9929: my @domains;
9930: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
9931: while( my $line = <$fh>) {
9932: $line =~ s/\s//g;
9933: push(@domains,$line);
9934: }
9935: return @domains;
9936: }
9937:
9938: sub default_login_domain {
9939: my $domain = $perlvar{'lonDefDomain'};
9940: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
9941: foreach my $posdom (¤t_machine_domains(),
9942: &additional_machine_domains()) {
9943: if (lc($posdom) eq lc($testdomain)) {
9944: $domain=$posdom;
9945: last;
9946: }
9947: }
9948: return $domain;
9949: }
9950:
1.31 www 9951: # ------------------------------------------------------------- Declutters URLs
9952:
9953: sub declutter {
9954: my $thisfn=shift;
1.569 albertel 9955: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 9956: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 9957: $thisfn=~s/^\///;
1.697 albertel 9958: $thisfn=~s|^adm/wrapper/||;
9959: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 9960: $thisfn=~s/^res\///;
1.1032 raeburn 9961: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
9962: $thisfn=~s/\?.+$//;
9963: }
1.268 www 9964: return $thisfn;
9965: }
9966:
9967: # ------------------------------------------------------------- Clutter up URLs
9968:
9969: sub clutter {
9970: my $thisfn='/'.&declutter(shift);
1.887 albertel 9971: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 9972: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 9973: $thisfn='/res'.$thisfn;
9974: }
1.1031 raeburn 9975: if ($thisfn !~m|^/adm|) {
9976: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 9977: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 9978: } else {
9979: my ($ext) = ($thisfn =~ /\.(\w+)$/);
9980: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 9981: if ($embstyle eq 'ssi'
9982: || ($embstyle eq 'hdn')
9983: || ($embstyle eq 'rat')
9984: || ($embstyle eq 'prv')
9985: || ($embstyle eq 'ign')) {
9986: #do nothing with these
9987: } elsif (($embstyle eq 'img')
1.695 albertel 9988: || ($embstyle eq 'emb')
9989: || ($embstyle eq 'wrp')) {
9990: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 9991: } elsif ($embstyle eq 'unk'
9992: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 9993: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 9994: } else {
1.718 www 9995: # &logthis("Got a blank emb style");
1.695 albertel 9996: }
1.694 albertel 9997: }
9998: }
1.31 www 9999: return $thisfn;
1.12 www 10000: }
10001:
1.787 albertel 10002: sub clutter_with_no_wrapper {
10003: my $uri = &clutter(shift);
10004: if ($uri =~ m-^/adm/-) {
10005: $uri =~ s-^/adm/wrapper/-/-;
10006: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
10007: }
10008: return $uri;
10009: }
10010:
1.557 albertel 10011: sub freeze_escape {
10012: my ($value)=@_;
10013: if (ref($value)) {
10014: $value=&nfreeze($value);
10015: return '__FROZEN__'.&escape($value);
10016: }
10017: return &escape($value);
10018: }
10019:
1.11 www 10020:
1.557 albertel 10021: sub thaw_unescape {
10022: my ($value)=@_;
10023: if ($value =~ /^__FROZEN__/) {
10024: substr($value,0,10,undef);
10025: $value=&unescape($value);
10026: return &thaw($value);
10027: }
10028: return &unescape($value);
10029: }
10030:
1.436 albertel 10031: sub correct_line_ends {
10032: my ($result)=@_;
10033: $$result =~s/\r\n/\n/mg;
10034: $$result =~s/\r/\n/mg;
1.415 albertel 10035: }
1.1 albertel 10036: # ================================================================ Main Program
10037:
1.184 www 10038: sub goodbye {
1.204 albertel 10039: &logthis("Starting Shut down");
1.443 albertel 10040: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 10041: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 10042: #converted
1.599 albertel 10043: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 10044: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
10045: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
10046: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 10047: #1.1 only
1.870 albertel 10048: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
10049: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
10050: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
10051: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
10052: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 10053: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
10054: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 10055: &flushcourselogs();
10056: &logthis("Shutting down");
10057: }
10058:
1.852 albertel 10059: sub get_dns {
1.869 albertel 10060: my ($url,$func,$ignore_cache) = @_;
10061: if (!$ignore_cache) {
10062: my ($content,$cached)=
10063: &Apache::lonnet::is_cached_new('dns',$url);
10064: if ($cached) {
10065: &$func($content);
10066: return;
10067: }
10068: }
10069:
10070: my %alldns;
1.852 albertel 10071: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10072: foreach my $dns (<$config>) {
10073: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 10074: my $line = $1;
10075: my ($host,$protocol) = split(/:/,$line);
10076: if ($protocol ne 'https') {
10077: $protocol = 'http';
10078: }
10079: $alldns{$host} = $protocol;
1.869 albertel 10080: }
10081: while (%alldns) {
10082: my ($dns) = keys(%alldns);
1.852 albertel 10083: my $ua=new LWP::UserAgent;
1.1056.4.33 raeburn 10084: $ua->timeout(30);
1.979 raeburn 10085: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 10086: my $response=$ua->request($request);
1.979 raeburn 10087: delete($alldns{$dns});
1.852 albertel 10088: next if ($response->is_error());
10089: my @content = split("\n",$response->content);
1.869 albertel 10090: &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852 albertel 10091: &$func(\@content);
1.869 albertel 10092: return;
1.852 albertel 10093: }
10094: close($config);
1.871 albertel 10095: my $which = (split('/',$url))[3];
10096: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
10097: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 10098: my @content = <$config>;
10099: &$func(\@content);
10100: return;
1.852 albertel 10101: }
1.327 albertel 10102: # ------------------------------------------------------------ Read domain file
10103: {
1.852 albertel 10104: my $loaded;
1.846 albertel 10105: my %domain;
10106:
1.852 albertel 10107: sub parse_domain_tab {
10108: my ($lines) = @_;
10109: foreach my $line (@$lines) {
10110: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 10111:
1.846 albertel 10112: chomp($line);
1.852 albertel 10113: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 10114: my %this_domain;
10115: foreach my $field ('description', 'auth_def', 'auth_arg_def',
10116: 'lang_def', 'city', 'longi', 'lati',
10117: 'primary') {
10118: $this_domain{$field} = shift(@elements);
10119: }
10120: $domain{$name} = \%this_domain;
1.852 albertel 10121: }
10122: }
1.864 albertel 10123:
10124: sub reset_domain_info {
10125: undef($loaded);
10126: undef(%domain);
10127: }
10128:
1.852 albertel 10129: sub load_domain_tab {
1.869 albertel 10130: my ($ignore_cache) = @_;
10131: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852 albertel 10132: my $fh;
10133: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
10134: my @lines = <$fh>;
10135: &parse_domain_tab(\@lines);
1.448 albertel 10136: }
1.852 albertel 10137: close($fh);
10138: $loaded = 1;
1.327 albertel 10139: }
1.846 albertel 10140:
10141: sub domain {
1.852 albertel 10142: &load_domain_tab() if (!$loaded);
10143:
1.846 albertel 10144: my ($name,$what) = @_;
10145: return if ( !exists($domain{$name}) );
10146:
10147: if (!$what) {
10148: return $domain{$name}{'description'};
10149: }
10150: return $domain{$name}{$what};
10151: }
1.974 raeburn 10152:
10153: sub domain_info {
10154: &load_domain_tab() if (!$loaded);
10155: return %domain;
10156: }
10157:
1.327 albertel 10158: }
10159:
10160:
1.1 albertel 10161: # ------------------------------------------------------------- Read hosts file
10162: {
1.838 albertel 10163: my %hostname;
1.844 albertel 10164: my %hostdom;
1.845 albertel 10165: my %libserv;
1.852 albertel 10166: my $loaded;
1.888 albertel 10167: my %name_to_host;
1.1056.4.6 raeburn 10168: my %internetdom;
1.1056.4.25 raeburn 10169: my %LC_dns_serv;
1.852 albertel 10170:
10171: sub parse_hosts_tab {
10172: my ($file) = @_;
10173: foreach my $configline (@$file) {
10174: next if ($configline =~ /^(\#|\s*$ )/x);
1.1056.4.25 raeburn 10175: chomp($configline);
10176: if ($configline =~ /^\^/) {
10177: if ($configline =~ /^\^([\w.\-]+)/) {
10178: $LC_dns_serv{$1} = 1;
10179: }
10180: next;
10181: }
1.1056.4.6 raeburn 10182: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 10183: $name=~s/\s//g;
10184: if ($id && $domain && $role && $name) {
10185: $hostname{$id}=$name;
1.888 albertel 10186: push(@{$name_to_host{$name}}, $id);
1.852 albertel 10187: $hostdom{$id}=$domain;
10188: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 10189: if (defined($protocol)) {
10190: if ($protocol eq 'https') {
10191: $protocol{$id} = $protocol;
10192: } else {
10193: $protocol{$id} = 'http';
10194: }
1.968 raeburn 10195: } else {
1.969 raeburn 10196: $protocol{$id} = 'http';
1.968 raeburn 10197: }
1.1056.4.6 raeburn 10198: if (defined($intdom)) {
10199: $internetdom{$id} = $intdom;
10200: }
1.852 albertel 10201: }
10202: }
10203: }
1.864 albertel 10204:
10205: sub reset_hosts_info {
1.897 albertel 10206: &purge_remembered();
1.864 albertel 10207: &reset_domain_info();
10208: &reset_hosts_ip_info();
1.892 albertel 10209: undef(%name_to_host);
1.864 albertel 10210: undef(%hostname);
10211: undef(%hostdom);
10212: undef(%libserv);
10213: undef($loaded);
10214: }
1.1 albertel 10215:
1.852 albertel 10216: sub load_hosts_tab {
1.869 albertel 10217: my ($ignore_cache) = @_;
10218: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852 albertel 10219: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10220: my @config = <$config>;
10221: &parse_hosts_tab(\@config);
10222: close($config);
10223: $loaded=1;
1.1 albertel 10224: }
1.852 albertel 10225:
1.838 albertel 10226: sub hostname {
1.852 albertel 10227: &load_hosts_tab() if (!$loaded);
10228:
1.838 albertel 10229: my ($lonid) = @_;
10230: return $hostname{$lonid};
10231: }
1.845 albertel 10232:
1.838 albertel 10233: sub all_hostnames {
1.852 albertel 10234: &load_hosts_tab() if (!$loaded);
10235:
1.838 albertel 10236: return %hostname;
10237: }
1.845 albertel 10238:
1.888 albertel 10239: sub all_names {
10240: &load_hosts_tab() if (!$loaded);
10241:
10242: return %name_to_host;
10243: }
10244:
1.974 raeburn 10245: sub all_host_domain {
10246: &load_hosts_tab() if (!$loaded);
10247: return %hostdom;
10248: }
10249:
1.845 albertel 10250: sub is_library {
1.852 albertel 10251: &load_hosts_tab() if (!$loaded);
10252:
1.845 albertel 10253: return exists($libserv{$_[0]});
10254: }
10255:
10256: sub all_library {
1.852 albertel 10257: &load_hosts_tab() if (!$loaded);
10258:
1.845 albertel 10259: return %libserv;
10260: }
10261:
1.1056.4.2 raeburn 10262: sub unique_library {
10263: #2x reverse removes all hostnames that appear more than once
10264: my %unique = reverse &all_library();
10265: return reverse %unique;
10266: }
10267:
1.841 albertel 10268: sub get_servers {
1.852 albertel 10269: &load_hosts_tab() if (!$loaded);
10270:
1.841 albertel 10271: my ($domain,$type) = @_;
10272: my %possible_hosts = ($type eq 'library') ? %libserv
10273: : %hostname;
10274: my %result;
1.842 albertel 10275: if (ref($domain) eq 'ARRAY') {
10276: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 10277: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 10278: $result{$host} = $hostname;
10279: }
10280: }
10281: } else {
10282: while ( my ($host,$hostname) = each(%possible_hosts)) {
10283: if ($hostdom{$host} eq $domain) {
10284: $result{$host} = $hostname;
10285: }
1.841 albertel 10286: }
10287: }
10288: return %result;
10289: }
1.845 albertel 10290:
1.1056.4.2 raeburn 10291: sub get_unique_servers {
10292: my %unique = reverse &get_servers(@_);
10293: return reverse %unique;
10294: }
10295:
1.844 albertel 10296: sub host_domain {
1.852 albertel 10297: &load_hosts_tab() if (!$loaded);
10298:
1.844 albertel 10299: my ($lonid) = @_;
10300: return $hostdom{$lonid};
10301: }
10302:
1.841 albertel 10303: sub all_domains {
1.852 albertel 10304: &load_hosts_tab() if (!$loaded);
10305:
1.841 albertel 10306: my %seen;
10307: my @uniq = grep(!$seen{$_}++, values(%hostdom));
10308: return @uniq;
10309: }
1.1056.4.3 raeburn 10310:
10311: sub internet_dom {
10312: &load_hosts_tab() if (!$loaded);
10313:
10314: my ($lonid) = @_;
10315: return $internetdom{$lonid};
10316: }
1.1056.4.27 raeburn 10317:
10318: sub is_LC_dns {
10319: &load_hosts_tab() if (!$loaded);
10320:
10321: my ($hostname) = @_;
10322: return exists($LC_dns_serv{$hostname});
10323: }
10324:
1.1 albertel 10325: }
10326:
1.847 albertel 10327: {
10328: my %iphost;
1.856 albertel 10329: my %name_to_ip;
10330: my %lonid_to_ip;
1.869 albertel 10331:
1.847 albertel 10332: sub get_hosts_from_ip {
10333: my ($ip) = @_;
10334: my %iphosts = &get_iphost();
10335: if (ref($iphosts{$ip})) {
10336: return @{$iphosts{$ip}};
10337: }
10338: return;
1.839 albertel 10339: }
1.864 albertel 10340:
10341: sub reset_hosts_ip_info {
10342: undef(%iphost);
10343: undef(%name_to_ip);
10344: undef(%lonid_to_ip);
10345: }
1.856 albertel 10346:
10347: sub get_host_ip {
10348: my ($lonid) = @_;
10349: if (exists($lonid_to_ip{$lonid})) {
10350: return $lonid_to_ip{$lonid};
10351: }
10352: my $name=&hostname($lonid);
10353: my $ip = gethostbyname($name);
10354: return if (!$ip || length($ip) ne 4);
10355: $ip=inet_ntoa($ip);
10356: $name_to_ip{$name} = $ip;
10357: $lonid_to_ip{$lonid} = $ip;
10358: return $ip;
10359: }
1.847 albertel 10360:
10361: sub get_iphost {
1.869 albertel 10362: my ($ignore_cache) = @_;
1.894 albertel 10363:
1.869 albertel 10364: if (!$ignore_cache) {
10365: if (%iphost) {
10366: return %iphost;
10367: }
10368: my ($ip_info,$cached)=
10369: &Apache::lonnet::is_cached_new('iphost','iphost');
10370: if ($cached) {
10371: %iphost = %{$ip_info->[0]};
10372: %name_to_ip = %{$ip_info->[1]};
10373: %lonid_to_ip = %{$ip_info->[2]};
10374: return %iphost;
10375: }
10376: }
1.894 albertel 10377:
10378: # get yesterday's info for fallback
10379: my %old_name_to_ip;
10380: my ($ip_info,$cached)=
10381: &Apache::lonnet::is_cached_new('iphost','iphost');
10382: if ($cached) {
10383: %old_name_to_ip = %{$ip_info->[1]};
10384: }
10385:
1.888 albertel 10386: my %name_to_host = &all_names();
10387: foreach my $name (keys(%name_to_host)) {
1.847 albertel 10388: my $ip;
10389: if (!exists($name_to_ip{$name})) {
10390: $ip = gethostbyname($name);
10391: if (!$ip || length($ip) ne 4) {
1.894 albertel 10392: if (defined($old_name_to_ip{$name})) {
10393: $ip = $old_name_to_ip{$name};
10394: &logthis("Can't find $name defaulting to old $ip");
10395: } else {
10396: &logthis("Name $name no IP found");
10397: next;
10398: }
10399: } else {
10400: $ip=inet_ntoa($ip);
1.847 albertel 10401: }
10402: $name_to_ip{$name} = $ip;
10403: } else {
10404: $ip = $name_to_ip{$name};
1.653 albertel 10405: }
1.888 albertel 10406: foreach my $id (@{ $name_to_host{$name} }) {
10407: $lonid_to_ip{$id} = $ip;
10408: }
10409: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 10410: }
1.869 albertel 10411: &Apache::lonnet::do_cache_new('iphost','iphost',
10412: [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894 albertel 10413: 48*60*60);
1.869 albertel 10414:
1.847 albertel 10415: return %iphost;
1.598 albertel 10416: }
10417:
1.992 raeburn 10418: #
10419: # Given a DNS returns the loncapa host name for that DNS
10420: #
10421: sub host_from_dns {
10422: my ($dns) = @_;
10423: my @hosts;
10424: my $ip;
10425:
1.993 raeburn 10426: if (exists($name_to_ip{$dns})) {
1.992 raeburn 10427: $ip = $name_to_ip{$dns};
10428: }
10429: if (!$ip) {
10430: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
10431: if (length($ip) == 4) {
10432: $ip = &IO::Socket::inet_ntoa($ip);
10433: }
10434: }
10435: if ($ip) {
10436: @hosts = get_hosts_from_ip($ip);
10437: return $hosts[0];
10438: }
10439: return undef;
1.986 foxr 10440: }
1.992 raeburn 10441:
1.1056.4.3 raeburn 10442: sub get_internet_names {
10443: my ($lonid) = @_;
10444: return if ($lonid eq '');
10445: my ($idnref,$cached)=
10446: &Apache::lonnet::is_cached_new('internetnames',$lonid);
10447: if ($cached) {
10448: return $idnref;
10449: }
10450: my $ip = &get_host_ip($lonid);
10451: my @hosts = &get_hosts_from_ip($ip);
10452: my %iphost = &get_iphost();
10453: my (@idns,%seen);
10454: foreach my $id (@hosts) {
10455: my $dom = &host_domain($id);
10456: my $prim_id = &domain($dom,'primary');
10457: my $prim_ip = &get_host_ip($prim_id);
10458: next if ($seen{$prim_ip});
10459: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
10460: foreach my $id (@{$iphost{$prim_ip}}) {
10461: my $intdom = &internet_dom($id);
10462: unless (grep(/^\Q$intdom\E$/,@idns)) {
10463: push(@idns,$intdom);
10464: }
10465: }
10466: }
10467: $seen{$prim_ip} = 1;
10468: }
10469: return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
10470: }
10471:
10472: }
10473:
10474: sub all_loncaparevs {
10475: 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);
1.986 foxr 10476: }
10477:
1.862 albertel 10478: BEGIN {
10479:
10480: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
10481: unless ($readit) {
10482: {
10483: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
10484: %perlvar = (%perlvar,%{$configvars});
10485: }
10486:
10487:
1.1 albertel 10488: # ------------------------------------------------------ Read spare server file
10489: {
1.448 albertel 10490: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 10491:
10492: while (my $configline=<$config>) {
10493: chomp($configline);
1.284 matthew 10494: if ($configline) {
1.784 albertel 10495: my ($host,$type) = split(':',$configline,2);
1.785 albertel 10496: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 10497: push(@{ $spareid{$type} }, $host);
1.1 albertel 10498: }
10499: }
1.448 albertel 10500: close($config);
1.1 albertel 10501: }
1.11 www 10502: # ------------------------------------------------------------ Read permissions
10503: {
1.448 albertel 10504: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 10505:
10506: while (my $configline=<$config>) {
1.448 albertel 10507: chomp($configline);
10508: if ($configline) {
10509: my ($role,$perm)=split(/ /,$configline);
10510: if ($perm ne '') { $pr{$role}=$perm; }
10511: }
1.11 www 10512: }
1.448 albertel 10513: close($config);
1.11 www 10514: }
10515:
10516: # -------------------------------------------- Read plain texts for permissions
10517: {
1.448 albertel 10518: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 10519:
10520: while (my $configline=<$config>) {
1.448 albertel 10521: chomp($configline);
10522: if ($configline) {
1.742 raeburn 10523: my ($short,@plain)=split(/:/,$configline);
10524: %{$prp{$short}} = ();
10525: if (@plain > 0) {
10526: $prp{$short}{'std'} = $plain[0];
10527: for (my $i=1; $i<@plain; $i++) {
10528: $prp{$short}{'alt'.$i} = $plain[$i];
10529: }
10530: }
1.448 albertel 10531: }
1.135 www 10532: }
1.448 albertel 10533: close($config);
1.135 www 10534: }
10535:
10536: # ---------------------------------------------------------- Read package table
10537: {
1.448 albertel 10538: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 10539:
10540: while (my $configline=<$config>) {
1.483 albertel 10541: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 10542: chomp($configline);
10543: my ($short,$plain)=split(/:/,$configline);
10544: my ($pack,$name)=split(/\&/,$short);
10545: if ($plain ne '') {
10546: $packagetab{$pack.'&'.$name.'&name'}=$name;
10547: $packagetab{$short}=$plain;
10548: }
1.11 www 10549: }
1.448 albertel 10550: close($config);
1.329 matthew 10551: }
10552:
1.1056.4.3 raeburn 10553: # ---------------------------------------------------------- Read loncaparev table
10554: {
10555: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
10556: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
10557: while (my $configline=<$config>) {
10558: chomp($configline);
10559: my ($hostid,$loncaparev)=split(/:/,$configline);
10560: $loncaparevs{$hostid}=$loncaparev;
10561: }
10562: close($config);
10563: }
10564: }
10565: }
10566:
10567: # ---------------------------------------------------------- Read serverhostID table
10568: {
10569: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
10570: if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
10571: while (my $configline=<$config>) {
10572: chomp($configline);
10573: my ($name,$id)=split(/:/,$configline);
10574: $serverhomeIDs{$name}=$id;
10575: }
10576: close($config);
10577: }
10578: }
10579: }
10580:
1.1056.4.5 raeburn 10581: {
10582: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
10583: if (-e $file) {
10584: my $parser = HTML::LCParser->new($file);
10585: while (my $token = $parser->get_token()) {
10586: if ($token->[0] eq 'S') {
10587: my $item = $token->[1];
10588: my $name = $token->[2]{'name'};
10589: my $value = $token->[2]{'value'};
10590: if ($item ne '' && $name ne '' && $value ne '') {
10591: my $release = $parser->get_text();
10592: $release =~ s/(^\s*|\s*$ )//gx;
10593: $needsrelease{$item.':'.$name.':'.$value} = $release;
10594: }
10595: }
10596: }
10597: }
10598: }
10599:
1.1056.4.33 raeburn 10600: # ---------------------------------------------------------- Read managers table
10601: {
10602: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
10603: if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
10604: while (my $configline=<$config>) {
10605: chomp($configline);
10606: next if ($configline =~ /^\#/);
10607: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
10608: $managerstab{$configline} = 1;
10609: }
10610: }
10611: close($config);
10612: }
10613: }
10614: }
10615:
1.329 matthew 10616: # ------------- set up temporary directory
10617: {
10618: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
10619:
1.11 www 10620: }
10621:
1.794 albertel 10622: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
10623: 'compress_threshold'=> 20_000,
10624: });
1.185 www 10625:
1.281 www 10626: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 10627: $dumpcount=0;
1.958 www 10628: $locknum=0;
1.22 www 10629:
1.163 harris41 10630: &logtouch();
1.672 albertel 10631: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 10632: $readit=1;
1.564 albertel 10633: {
10634: use integer;
10635: my $test=(2**32)+1;
1.568 albertel 10636: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 10637: &logthis(" Detected 64bit platform ($_64bit)");
10638: }
1.195 www 10639: }
1.1 albertel 10640: }
1.179 www 10641:
1.1 albertel 10642: 1;
1.191 harris41 10643: __END__
10644:
1.243 albertel 10645: =pod
10646:
1.191 harris41 10647: =head1 NAME
10648:
1.243 albertel 10649: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 10650:
10651: =head1 SYNOPSIS
10652:
1.243 albertel 10653: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 10654:
10655: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
10656:
1.243 albertel 10657: Common parameters:
10658:
10659: =over 4
10660:
10661: =item *
10662:
10663: $uname : an internal username (if $cname expecting a course Id specifically)
10664:
10665: =item *
10666:
10667: $udom : a domain (if $cdom expecting a course's domain specifically)
10668:
10669: =item *
10670:
10671: $symb : a resource instance identifier
10672:
10673: =item *
10674:
10675: $namespace : the name of a .db file that contains the data needed or
10676: being set.
10677:
10678: =back
10679:
1.394 bowersj2 10680: =head1 OVERVIEW
1.191 harris41 10681:
1.394 bowersj2 10682: lonnet provides subroutines which interact with the
10683: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
10684: about classes, users, and resources.
1.243 albertel 10685:
10686: For many of these objects you can also use this to store data about
10687: them or modify them in various ways.
1.191 harris41 10688:
1.394 bowersj2 10689: =head2 Symbs
1.191 harris41 10690:
1.394 bowersj2 10691: To identify a specific instance of a resource, LON-CAPA uses symbols
10692: or "symbs"X<symb>. These identifiers are built from the URL of the
10693: map, the resource number of the resource in the map, and the URL of
10694: the resource itself. The latter is somewhat redundant, but might help
10695: if maps change.
10696:
10697: An example is
10698:
10699: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
10700:
10701: The respective map entry is
10702:
10703: <resource id="19" src="/res/msu/korte/tests/part12.problem"
10704: title="Problem 2">
10705: </resource>
10706:
10707: Symbs are used by the random number generator, as well as to store and
10708: restore data specific to a certain instance of for example a problem.
10709:
10710: =head2 Storing And Retrieving Data
10711:
10712: X<store()>X<cstore()>X<restore()>Three of the most important functions
10713: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
10714: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
10715: is is the non-critical message twin of cstore. These functions are for
10716: handlers to store a perl hash to a user's permanent data space in an
10717: easy manner, and to retrieve it again on another call. It is expected
10718: that a handler would use this once at the beginning to retrieve data,
10719: and then again once at the end to send only the new data back.
10720:
10721: The data is stored in the user's data directory on the user's
10722: homeserver under the ID of the course.
10723:
10724: The hash that is returned by restore will have all of the previous
10725: value for all of the elements of the hash.
10726:
10727: Example:
10728:
10729: #creating a hash
10730: my %hash;
10731: $hash{'foo'}='bar';
10732:
10733: #storing it
10734: &Apache::lonnet::cstore(\%hash);
10735:
10736: #changing a value
10737: $hash{'foo'}='notbar';
10738:
10739: #adding a new value
10740: $hash{'bar'}='foo';
10741: &Apache::lonnet::cstore(\%hash);
10742:
10743: #retrieving the hash
10744: my %history=&Apache::lonnet::restore();
10745:
10746: #print the hash
10747: foreach my $key (sort(keys(%history))) {
10748: print("\%history{$key} = $history{$key}");
10749: }
10750:
10751: Will print out:
1.191 harris41 10752:
1.394 bowersj2 10753: %history{1:foo} = bar
10754: %history{1:keys} = foo:timestamp
10755: %history{1:timestamp} = 990455579
10756: %history{2:bar} = foo
10757: %history{2:foo} = notbar
10758: %history{2:keys} = foo:bar:timestamp
10759: %history{2:timestamp} = 990455580
10760: %history{bar} = foo
10761: %history{foo} = notbar
10762: %history{timestamp} = 990455580
10763: %history{version} = 2
10764:
10765: Note that the special hash entries C<keys>, C<version> and
10766: C<timestamp> were added to the hash. C<version> will be equal to the
10767: total number of versions of the data that have been stored. The
10768: C<timestamp> attribute will be the UNIX time the hash was
10769: stored. C<keys> is available in every historical section to list which
10770: keys were added or changed at a specific historical revision of a
10771: hash.
10772:
10773: B<Warning>: do not store the hash that restore returns directly. This
10774: will cause a mess since it will restore the historical keys as if the
10775: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 10776:
1.394 bowersj2 10777: Calling convention:
1.191 harris41 10778:
1.394 bowersj2 10779: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
10780: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 10781:
1.394 bowersj2 10782: For more detailed information, see lonnet specific documentation.
1.191 harris41 10783:
1.394 bowersj2 10784: =head1 RETURN MESSAGES
1.191 harris41 10785:
1.394 bowersj2 10786: =over 4
1.191 harris41 10787:
1.394 bowersj2 10788: =item * B<con_lost>: unable to contact remote host
1.191 harris41 10789:
1.394 bowersj2 10790: =item * B<con_delayed>: unable to contact remote host, message will be delivered
10791: when the connection is brought back up
1.191 harris41 10792:
1.394 bowersj2 10793: =item * B<con_failed>: unable to contact remote host and unable to save message
10794: for later delivery
1.191 harris41 10795:
1.967 bisitz 10796: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 10797:
1.394 bowersj2 10798: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 10799: that was requested
1.191 harris41 10800:
1.243 albertel 10801: =back
1.191 harris41 10802:
1.243 albertel 10803: =head1 PUBLIC SUBROUTINES
1.191 harris41 10804:
1.243 albertel 10805: =head2 Session Environment Functions
1.191 harris41 10806:
1.243 albertel 10807: =over 4
1.191 harris41 10808:
1.394 bowersj2 10809: =item *
10810: X<appenv()>
1.949 raeburn 10811: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 10812: the user envirnoment file, and will be restored for each access this
1.620 albertel 10813: user makes during this session, also modifies the %env for the current
1.949 raeburn 10814: process. Optional rolesarrayref - if defined contains a reference to an array
10815: of roles which are exempt from the restriction on modifying user.role entries
10816: in the user's environment.db and in %env.
1.191 harris41 10817:
10818: =item *
1.394 bowersj2 10819: X<delenv()>
1.987 raeburn 10820: B<delenv($delthis,$regexp)>: removes all items from the session
10821: environment file that begin with $delthis. If the
10822: optional second arg - $regexp - is true, $delthis is treated as a
10823: regular expression, otherwise \Q$delthis\E is used.
10824: The values are also deleted from the current processes %env.
1.191 harris41 10825:
1.795 albertel 10826: =item * get_env_multiple($name)
10827:
10828: gets $name from the %env hash, it seemlessly handles the cases where multiple
10829: values may be defined and end up as an array ref.
10830:
10831: returns an array of values
10832:
1.243 albertel 10833: =back
10834:
10835: =head2 User Information
1.191 harris41 10836:
1.243 albertel 10837: =over 4
1.191 harris41 10838:
10839: =item *
1.394 bowersj2 10840: X<queryauthenticate()>
10841: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 10842: authentication scheme
10843:
10844: =item *
1.394 bowersj2 10845: X<authenticate()>
1.1056.4.3 raeburn 10846: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 10847: authenticate user from domain's lib servers (first use the current
10848: one). C<$upass> should be the users password.
1.1056.4.3 raeburn 10849: $checkdefauth is optional (value is 1 if a check should be made to
10850: authenticate user using default authentication method, and allow
10851: account creation if username does not have account in the domain).
10852: $clientcancheckhost is optional (value is 1 if checking whether the
10853: server can host will occur on the client side in lonauth.pm).
1.191 harris41 10854:
10855: =item *
1.394 bowersj2 10856: X<homeserver()>
10857: B<homeserver($uname,$udom)>: find the server which has
10858: the user's directory and files (there must be only one), this caches
10859: the answer, and also caches if there is a borken connection.
1.191 harris41 10860:
10861: =item *
1.394 bowersj2 10862: X<idget()>
10863: B<idget($udom,@ids)>: find the usernames behind a list of IDs
10864: (IDs are a unique resource in a domain, there must be only 1 ID per
10865: username, and only 1 username per ID in a specific domain) (returns
10866: hash: id=>name,id=>name)
1.191 harris41 10867:
10868: =item *
1.394 bowersj2 10869: X<idrget()>
10870: B<idrget($udom,@unames)>: find the IDs behind a list of
10871: usernames (returns hash: name=>id,name=>id)
1.191 harris41 10872:
10873: =item *
1.394 bowersj2 10874: X<idput()>
10875: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 10876:
10877: =item *
1.394 bowersj2 10878: X<rolesinit()>
10879: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 10880:
10881: =item *
1.551 albertel 10882: X<getsection()>
10883: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 10884: course $cname, return section name/number or '' for "not in course"
10885: and '-1' for "no section"
10886:
10887: =item *
1.394 bowersj2 10888: X<userenvironment()>
10889: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 10890: passed in @what from the requested user's environment, returns a hash
10891:
1.858 raeburn 10892: =item *
10893: X<userlog_query()>
1.859 albertel 10894: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
10895: activity.log file. %filters defines filters applied when parsing the
10896: log file. These can be start or end timestamps, or the type of action
10897: - log to look for Login or Logout events, check for Checkin or
10898: Checkout, role for role selection. The response is in the form
10899: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
10900: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 10901:
1.243 albertel 10902: =back
10903:
10904: =head2 User Roles
10905:
10906: =over 4
10907:
10908: =item *
10909:
1.810 raeburn 10910: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 10911: F: full access
10912: U,I,K: authentication modes (cxx only)
10913: '': forbidden
10914: 1: user needs to choose course
10915: 2: browse allowed
1.766 albertel 10916: A: passphrase authentication needed
1.243 albertel 10917:
10918: =item *
10919:
10920: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
10921: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
10922: and course level
10923:
10924: =item *
10925:
1.988 raeburn 10926: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
10927: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 10928: $type is Course (default) or Community.
1.988 raeburn 10929: If $forcedefault evaluates to true, text returned will be default
10930: text for $type. Otherwise, if this is a course, the text returned
10931: will be a custom name for the role (if defined in the course's
10932: environment). If no custom name is defined the default is returned.
10933:
1.832 raeburn 10934: =item *
10935:
1.935 raeburn 10936: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858 raeburn 10937: All arguments are optional. Returns a hash of a roles, either for
10938: co-author/assistant author roles for a user's Construction Space
1.906 albertel 10939: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 10940: In the hash, keys are set to colon-separated $uname,$udom,$role, and
10941: (optionally) if $withsec is true, a fourth colon-separated item - $section.
10942: For each key, value is set to colon-separated start and end times for
10943: the role. If no username and domain are specified, will default to
1.934 raeburn 10944: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 10945: of role statuses (active, future or previous), roles
10946: (e.g., cc,in, st etc.) and domains of the roles which can be used
10947: to restrict the list of roles reported. If no array ref is
10948: provided for types, will default to return only active roles.
1.834 albertel 10949:
1.243 albertel 10950: =back
10951:
10952: =head2 User Modification
10953:
10954: =over 4
10955:
10956: =item *
10957:
1.957 raeburn 10958: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 10959: user for the level given by URL. Optional start and end dates (leave empty
10960: string or zero for "no date")
1.191 harris41 10961:
10962: =item *
10963:
1.243 albertel 10964: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
10965: change a users, password, possible return values are: ok,
10966: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
10967: refused
1.191 harris41 10968:
10969: =item *
10970:
1.243 albertel 10971: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 10972:
10973: =item *
10974:
1.1056.4.1 raeburn 10975: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
10976: $forceid,$desiredhome,$email,$inststatus,$candelete) :
10977:
10978: will update user information (firstname,middlename,lastname,generation,
10979: permanentemail), and if forceid is true, student/employee ID also.
10980: A user's institutional affiliation(s) can also be updated.
10981: User information fields will not be overwritten with empty entries
10982: unless the field is included in the $candelete array reference.
10983: This array is included when a single user is modified via "Manage Users",
10984: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 10985:
10986: =item *
10987:
1.286 matthew 10988: modifystudent
10989:
1.957 raeburn 10990: modify a student's enrollment and identification information.
1.286 matthew 10991: The course id is resolved based on the current users environment.
10992: This means the envoking user must be a course coordinator or otherwise
10993: associated with a course.
10994:
1.297 matthew 10995: This call is essentially a wrapper for lonnet::modifyuser and
10996: lonnet::modify_student_enrollment
1.286 matthew 10997:
10998: Inputs:
10999:
11000: =over 4
11001:
1.957 raeburn 11002: =item B<$udom> Student's loncapa domain
1.286 matthew 11003:
1.957 raeburn 11004: =item B<$uname> Student's loncapa login name
1.286 matthew 11005:
1.964 bisitz 11006: =item B<$uid> Student/Employee ID
1.286 matthew 11007:
1.957 raeburn 11008: =item B<$umode> Student's authentication mode
1.286 matthew 11009:
1.957 raeburn 11010: =item B<$upass> Student's password
1.286 matthew 11011:
1.957 raeburn 11012: =item B<$first> Student's first name
1.286 matthew 11013:
1.957 raeburn 11014: =item B<$middle> Student's middle name
1.286 matthew 11015:
1.957 raeburn 11016: =item B<$last> Student's last name
1.286 matthew 11017:
1.957 raeburn 11018: =item B<$gene> Student's generation
1.286 matthew 11019:
1.957 raeburn 11020: =item B<$usec> Student's section in course
1.286 matthew 11021:
11022: =item B<$end> Unix time of the roles expiration
11023:
11024: =item B<$start> Unix time of the roles start date
11025:
11026: =item B<$forceid> If defined, allow $uid to be changed
11027:
11028: =item B<$desiredhome> server to use as home server for student
11029:
1.957 raeburn 11030: =item B<$email> Student's permanent e-mail address
11031:
11032: =item B<$type> Type of enrollment (auto or manual)
11033:
1.963 raeburn 11034: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
11035:
11036: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 11037:
1.963 raeburn 11038: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 11039:
1.963 raeburn 11040: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 11041:
1.963 raeburn 11042: =item B<$inststatus> institutional status of user - : separated string of escaped status types
1.957 raeburn 11043:
1.286 matthew 11044: =back
1.297 matthew 11045:
11046: =item *
11047:
11048: modify_student_enrollment
11049:
11050: Change a students enrollment status in a class. The environment variable
11051: 'role.request.course' must be defined for this function to proceed.
11052:
11053: Inputs:
11054:
11055: =over 4
11056:
11057: =item $udom, students domain
11058:
11059: =item $uname, students name
11060:
11061: =item $uid, students user id
11062:
11063: =item $first, students first name
11064:
11065: =item $middle
11066:
11067: =item $last
11068:
11069: =item $gene
11070:
11071: =item $usec
11072:
11073: =item $end
11074:
11075: =item $start
11076:
1.957 raeburn 11077: =item $type
11078:
11079: =item $locktype
11080:
11081: =item $cid
11082:
11083: =item $selfenroll
11084:
11085: =item $context
11086:
1.297 matthew 11087: =back
11088:
1.191 harris41 11089:
11090: =item *
11091:
1.243 albertel 11092: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
11093: custom role; give a custom role to a user for the level given by URL. Specify
11094: name and domain of role author, and role name
1.191 harris41 11095:
11096: =item *
11097:
1.243 albertel 11098: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 11099:
11100: =item *
11101:
1.243 albertel 11102: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
11103:
11104: =back
11105:
11106: =head2 Course Infomation
11107:
11108: =over 4
1.191 harris41 11109:
11110: =item *
11111:
1.631 albertel 11112: coursedescription($courseid) : returns a hash of information about the
11113: specified course id, including all environment settings for the
11114: course, the description of the course will be in the hash under the
11115: key 'description'
1.191 harris41 11116:
11117: =item *
11118:
1.624 albertel 11119: resdata($name,$domain,$type,@which) : request for current parameter
11120: setting for a specific $type, where $type is either 'course' or 'user',
11121: @what should be a list of parameters to ask about. This routine caches
11122: answers for 5 minutes.
1.243 albertel 11123:
1.877 foxr 11124: =item *
11125:
11126: get_courseresdata($courseid, $domain) : dump the entire course resource
11127: data base, returning a hash that is keyed by the resource name and has
11128: values that are the resource value. I believe that the timestamps and
11129: versions are also returned.
11130:
11131:
1.243 albertel 11132: =back
11133:
11134: =head2 Course Modification
11135:
11136: =over 4
1.191 harris41 11137:
11138: =item *
11139:
1.243 albertel 11140: writecoursepref($courseid,%prefs) : write preferences (environment
11141: database) for a course
1.191 harris41 11142:
11143: =item *
11144:
1.1011 raeburn 11145: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
11146:
11147: =item *
11148:
1.1038 raeburn 11149: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 11150:
11151: =back
11152:
11153: =head2 Resource Subroutines
11154:
11155: =over 4
1.191 harris41 11156:
11157: =item *
11158:
1.243 albertel 11159: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 11160:
11161: =item *
11162:
1.243 albertel 11163: repcopy($filename) : subscribes to the requested file, and attempts to
11164: replicate from the owning library server, Might return
1.607 raeburn 11165: 'unavailable', 'not_found', 'forbidden', 'ok', or
11166: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 11167: resource. Expects the local filesystem pathname
11168: (/home/httpd/html/res/....)
11169:
11170: =back
11171:
11172: =head2 Resource Information
11173:
11174: =over 4
1.191 harris41 11175:
11176: =item *
11177:
1.243 albertel 11178: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
11179: a vairety of different possible values, $varname should be a request
11180: string, and the other parameters can be used to specify who and what
11181: one is asking about.
11182:
11183: Possible values for $varname are environment.lastname (or other item
11184: from the envirnment hash), user.name (or someother aspect about the
11185: user), resource.0.maxtries (or some other part and parameter of a
11186: resource)
1.204 albertel 11187:
11188: =item *
11189:
1.243 albertel 11190: directcondval($number) : get current value of a condition; reads from a state
11191: string
1.204 albertel 11192:
11193: =item *
11194:
1.243 albertel 11195: condval($condidx) : value of condition index based on state
1.204 albertel 11196:
11197: =item *
11198:
1.243 albertel 11199: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
11200: resource's metadata, $what should be either a specific key, or either
11201: 'keys' (to get a list of possible keys) or 'packages' to get a list of
11202: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
11203:
11204: this function automatically caches all requests
1.191 harris41 11205:
11206: =item *
11207:
1.243 albertel 11208: metadata_query($query,$custom,$customshow) : make a metadata query against the
11209: network of library servers; returns file handle of where SQL and regex results
11210: will be stored for query
1.191 harris41 11211:
11212: =item *
11213:
1.243 albertel 11214: symbread($filename) : return symbolic list entry (filename argument optional);
11215: returns the data handle
1.191 harris41 11216:
11217: =item *
11218:
1.243 albertel 11219: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 11220: a possible symb for the URL in $thisfn, and if is an encryypted
11221: resource that the user accessed using /enc/ returns a 1 on success, 0
11222: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 11223: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 11224:
1.191 harris41 11225:
11226: =item *
11227:
1.243 albertel 11228: symbclean($symb) : removes versions numbers from a symb, returns the
11229: cleaned symb
1.191 harris41 11230:
11231: =item *
11232:
1.243 albertel 11233: is_on_map($uri) : checks if the $uri is somewhere on the current
11234: course map, user must be in a course for it to work.
1.191 harris41 11235:
11236: =item *
11237:
1.243 albertel 11238: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 11239:
11240: =item *
11241:
1.243 albertel 11242: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
11243: a random seed, all arguments are optional, if they aren't sent it uses the
11244: environment to derive them. Note: if symb isn't sent and it can't get one
11245: from &symbread it will use the current time as its return value
1.191 harris41 11246:
11247: =item *
11248:
1.243 albertel 11249: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
11250: unfakeable, receipt
1.191 harris41 11251:
11252: =item *
11253:
1.620 albertel 11254: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 11255:
11256: =item *
11257:
1.243 albertel 11258: countacc($url) : count the number of accesses to a given URL
1.191 harris41 11259:
11260: =item *
11261:
1.243 albertel 11262: 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 11263:
11264: =item *
11265:
1.243 albertel 11266: 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 11267:
11268: =item *
11269:
1.243 albertel 11270: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 11271:
11272: =item *
11273:
1.243 albertel 11274: devalidate($symb) : devalidate temporary spreadsheet calculations,
11275: forcing spreadsheet to reevaluate the resource scores next time.
11276:
11277: =back
11278:
11279: =head2 Storing/Retreiving Data
11280:
11281: =over 4
1.191 harris41 11282:
11283: =item *
11284:
1.243 albertel 11285: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
11286: for this url; hashref needs to be given and should be a \%hashname; the
11287: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 11288: be derived from the env
1.191 harris41 11289:
11290: =item *
11291:
1.243 albertel 11292: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
11293: uses critical subroutine
1.191 harris41 11294:
11295: =item *
11296:
1.243 albertel 11297: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
11298: all args are optional
1.191 harris41 11299:
11300: =item *
11301:
1.717 albertel 11302: dumpstore($namespace,$udom,$uname,$regexp,$range) :
11303: dumps the complete (or key matching regexp) namespace into a hash
11304: ($udom, $uname, $regexp, $range are optional) for a namespace that is
11305: normally &store()ed into
11306:
11307: $range should be either an integer '100' (give me the first 100
11308: matching records)
11309: or be two integers sperated by a - with no spaces
11310: '30-50' (give me the 30th through the 50th matching
11311: records)
11312:
11313:
11314: =item *
11315:
11316: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
11317: replaces a &store() version of data with a replacement set of data
11318: for a particular resource in a namespace passed in the $storehash hash
11319: reference
11320:
11321: =item *
11322:
1.243 albertel 11323: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
11324: works very similar to store/cstore, but all data is stored in a
11325: temporary location and can be reset using tmpreset, $storehash should
11326: be a hash reference, returns nothing on success
1.191 harris41 11327:
11328: =item *
11329:
1.243 albertel 11330: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
11331: similar to restore, but all data is stored in a temporary location and
11332: can be reset using tmpreset. Returns a hash of values on success,
11333: error string otherwise.
1.191 harris41 11334:
11335: =item *
11336:
1.243 albertel 11337: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
11338: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 11339:
11340: =item *
11341:
1.243 albertel 11342: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11343: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 11344:
11345: =item *
11346:
1.243 albertel 11347: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
11348: namesp ($udom and $uname are optional)
1.191 harris41 11349:
11350: =item *
11351:
1.702 albertel 11352: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 11353: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 11354: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 11355:
1.702 albertel 11356: $range should be either an integer '100' (give me the first 100
11357: matching records)
11358: or be two integers sperated by a - with no spaces
11359: '30-50' (give me the 30th through the 50th matching
11360: records)
1.449 matthew 11361: =item *
11362:
11363: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
11364: $store can be a scalar, an array reference, or if the amount to be
11365: incremented is > 1, a hash reference.
11366:
11367: ($udom and $uname are optional)
1.191 harris41 11368:
11369: =item *
11370:
1.243 albertel 11371: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
11372: ($udom and $uname are optional)
1.191 harris41 11373:
11374: =item *
11375:
1.243 albertel 11376: cput($namespace,$storehash,$udom,$uname) : critical put
11377: ($udom and $uname are optional)
1.191 harris41 11378:
11379: =item *
11380:
1.748 albertel 11381: newput($namespace,$storehash,$udom,$uname) :
11382:
11383: Attempts to store the items in the $storehash, but only if they don't
11384: currently exist, if this succeeds you can be certain that you have
11385: successfully created a new key value pair in the $namespace db.
11386:
11387:
11388: Args:
11389: $namespace: name of database to store values to
11390: $storehash: hashref to store to the db
11391: $udom: (optional) domain of user containing the db
11392: $uname: (optional) name of user caontaining the db
11393:
11394: Returns:
11395: 'ok' -> succeeded in storing all keys of $storehash
11396: 'key_exists: <key>' -> failed to anything out of $storehash, as at
11397: least <key> already existed in the db (other
11398: requested keys may also already exist)
1.967 bisitz 11399: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 11400: 'con_lost' -> unable to contact request server
11401: 'refused' -> action was not allowed by remote machine
11402:
11403:
11404: =item *
11405:
1.243 albertel 11406: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11407: reference filled in from namesp (encrypts the return communication)
11408: ($udom and $uname are optional)
1.191 harris41 11409:
11410: =item *
11411:
1.243 albertel 11412: log($udom,$name,$home,$message) : write to permanent log for user; use
11413: critical subroutine
11414:
1.806 raeburn 11415: =item *
11416:
1.860 raeburn 11417: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
11418: array reference filled in from namespace found in domain level on either
11419: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 11420:
11421: =item *
11422:
1.860 raeburn 11423: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
11424: domain level either on specified domain server ($uhome) or primary domain
11425: server ($udom and $uhome are optional)
1.806 raeburn 11426:
1.943 raeburn 11427: =item *
11428:
11429: get_domain_defaults($target_domain) : returns hash with defaults for
11430: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
11431: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
11432: or localauth), initial password or a kerberos realm, language (e.g., en-us).
11433: Values are retrieved from cache (if current), or from domain's configuration.db
11434: (if available), or lastly from values in lonTabs/dns_domain,tab,
11435: or lonTabs/domain.tab.
11436:
11437: %domdefaults = &get_auth_defaults($target_domain);
11438:
1.243 albertel 11439: =back
11440:
11441: =head2 Network Status Functions
11442:
11443: =over 4
1.191 harris41 11444:
11445: =item *
11446:
11447: dirlist($uri) : return directory list based on URI
11448:
11449: =item *
11450:
1.243 albertel 11451: spareserver() : find server with least workload from spare.tab
11452:
1.986 foxr 11453:
11454: =item *
11455:
11456: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
11457: if there is no corresponding loncapa host.
11458:
1.243 albertel 11459: =back
11460:
1.986 foxr 11461:
1.243 albertel 11462: =head2 Apache Request
11463:
11464: =over 4
1.191 harris41 11465:
11466: =item *
11467:
1.243 albertel 11468: ssi($url,%hash) : server side include, does a complete request cycle on url to
11469: localhost, posts hash
11470:
11471: =back
11472:
11473: =head2 Data to String to Data
11474:
11475: =over 4
1.191 harris41 11476:
11477: =item *
11478:
1.243 albertel 11479: hash2str(%hash) : convert a hash into a string complete with escaping and '='
11480: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 11481:
11482: =item *
11483:
1.243 albertel 11484: hashref2str($hashref) : convert a hashref into a string complete with
11485: escaping and '=' and '&' separators, supports elements that are
11486: arrayrefs and hashrefs
1.191 harris41 11487:
11488: =item *
11489:
1.243 albertel 11490: arrayref2str($arrayref) : convert an arrayref into a string complete
11491: with escaping and '&' separators, supports elements that are arrayrefs
11492: and hashrefs
1.191 harris41 11493:
11494: =item *
11495:
1.243 albertel 11496: str2hash($string) : convert string to hash using unescaping and
11497: splitting on '=' and '&', supports elements that are arrayrefs and
11498: hashrefs
1.191 harris41 11499:
11500: =item *
11501:
1.243 albertel 11502: str2array($string) : convert string to hash using unescaping and
11503: splitting on '&', supports elements that are arrayrefs and hashrefs
11504:
11505: =back
11506:
11507: =head2 Logging Routines
11508:
11509: These routines allow one to make log messages in the lonnet.log and
11510: lonnet.perm logfiles.
1.191 harris41 11511:
1.1056.4.31 raeburn 11512: =over 4
11513:
1.191 harris41 11514: =item *
11515:
1.243 albertel 11516: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 11517:
11518: =item *
11519:
1.243 albertel 11520: logthis() : append message to the normal lonnet.log file, it gets
11521: preiodically rolled over and deleted.
1.191 harris41 11522:
11523: =item *
11524:
1.243 albertel 11525: logperm() : append a permanent message to lonnet.perm.log, this log
11526: file never gets deleted by any automated portion of the system, only
11527: messages of critical importance should go in here.
11528:
11529: =back
11530:
11531: =head2 General File Helper Routines
11532:
11533: =over 4
1.191 harris41 11534:
11535: =item *
11536:
1.481 raeburn 11537: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
11538: (a) files in /uploaded
11539: (i) If a local copy of the file exists -
11540: compares modification date of local copy with last-modified date for
11541: definitive version stored on home server for course. If local copy is
11542: stale, requests a new version from the home server and stores it.
11543: If the original has been removed from the home server, then local copy
11544: is unlinked.
11545: (ii) If local copy does not exist -
11546: requests the file from the home server and stores it.
11547:
11548: If $caller is 'uploadrep':
11549: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
11550: for request for files originally uploaded via DOCS.
11551: - returns 'ok' if fresh local copy now available, -1 otherwise.
11552:
11553: Otherwise:
11554: This indicates a call from the content generation phase of the request.
11555: - returns the entire contents of the file or -1.
11556:
11557: (b) files in /res
11558: - returns the entire contents of a file or -1;
11559: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 11560:
1.712 albertel 11561:
11562: =item *
11563:
11564: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
11565: reference
11566:
11567: returns either a stat() list of data about the file or an empty list
11568: if the file doesn't exist or couldn't find out about it (connection
11569: problems or user unknown)
11570:
1.191 harris41 11571: =item *
11572:
1.243 albertel 11573: filelocation($dir,$file) : returns file system location of a file
11574: based on URI; meant to be "fairly clean" absolute reference, $dir is a
11575: directory that relative $file lookups are to looked in ($dir of /a/dir
11576: and a file of ../bob will become /a/bob)
1.191 harris41 11577:
11578: =item *
11579:
11580: hreflocation($dir,$file) : returns file system location or a URL; same as
11581: filelocation except for hrefs
11582:
11583: =item *
11584:
11585: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
11586:
1.243 albertel 11587: =back
11588:
1.608 albertel 11589: =head2 Usererfile file routines (/uploaded*)
11590:
11591: =over 4
11592:
11593: =item *
11594:
11595: userfileupload(): main rotine for putting a file in a user or course's
11596: filespace, arguments are,
11597:
1.620 albertel 11598: formname - required - this is the name of the element in $env where the
1.608 albertel 11599: filename, and the contents of the file to create/modifed exist
1.620 albertel 11600: the filename is in $env{'form.'.$formname.'.filename'} and the
11601: contents of the file is located in $env{'form.'.$formname}
1.1056.4.13 raeburn 11602: context - if coursedoc, store the file in the course of the active role
11603: of the current user;
11604: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
11605: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 11606: subdir - required - subdirectory to put the file in under ../userfiles/
11607: if undefined, it will be placed in "unknown"
11608:
11609: (This routine calls clean_filename() to remove any dangerous
11610: characters from the filename, and then calls finuserfileupload() to
11611: complete the transaction)
11612:
11613: returns either the url of the uploaded file (/uploaded/....) if successful
11614: and /adm/notfound.html if unsuccessful
11615:
11616: =item *
11617:
11618: clean_filename(): routine for cleaing a filename up for storage in
11619: userfile space, argument is:
11620:
11621: filename - proposed filename
11622:
11623: returns: the new clean filename
11624:
11625: =item *
11626:
1.1056.4.13 raeburn 11627: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 11628: userspace, probably shouldn't be called directly
11629:
11630: docuname: username or courseid of destination for the file
11631: docudom: domain of user/course of destination for the file
11632: formname: same as for userfileupload()
1.1056.4.13 raeburn 11633: fname: filename (including subdirectories) for the file
11634: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
11635: allfiles: reference to hash used to store objects found by parser
11636: codebase: reference to hash used for codebases of java objects found by parser
11637: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
11638: thumbheight: height (pixels) of thumbnail to be created for uploaded image
11639: resizewidth: width to be used to resize image using resizeImage from ImageMagick
11640: resizeheight: height to be used to resize image using resizeImage from ImageMagick
11641: context: if 'overwrite', will move the uploaded file from its temporary location to
11642: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1056.4.16 raeburn 11643: mimetype: reference to scalar to accommodate mime type determined
11644: from File::MMagic if $parser = parse.
1.608 albertel 11645:
11646: returns either the url of the uploaded file (/uploaded/....) if successful
1.1056.4.13 raeburn 11647: and /adm/notfound.html if unsuccessful (or an error message if context
11648: was 'overwrite').
11649:
1.608 albertel 11650:
11651: =item *
11652:
11653: renameuserfile(): renames an existing userfile to a new name
11654:
11655: Args:
11656: docuname: username or courseid of destination for the file
11657: docudom: domain of user/course of destination for the file
11658: old: current file name (including any subdirs under userfiles)
11659: new: desired file name (including any subdirs under userfiles)
11660:
11661: =item *
11662:
11663: mkdiruserfile(): creates a directory is a userfiles dir
11664:
11665: Args:
11666: docuname: username or courseid of destination for the file
11667: docudom: domain of user/course of destination for the file
11668: dir: dir to create (including any subdirs under userfiles)
11669:
11670: =item *
11671:
11672: removeuserfile(): removes a file that exists in userfiles
11673:
11674: Args:
11675: docuname: username or courseid of destination for the file
11676: docudom: domain of user/course of destination for the file
11677: fname: filname to delete (including any subdirs under userfiles)
11678:
11679: =item *
11680:
11681: removeuploadedurl(): convience function for removeuserfile()
11682:
11683: Args:
11684: url: a full /uploaded/... url to delete
11685:
1.747 albertel 11686: =item *
11687:
11688: get_portfile_permissions():
11689: Args:
11690: domain: domain of user or course contain the portfolio files
11691: user: name of user or num of course contain the portfolio files
11692: Returns:
11693: hashref of a dump of the proper file_permissions.db
11694:
11695:
11696: =item *
11697:
11698: get_access_controls():
11699:
11700: Args:
11701: current_permissions: the hash ref returned from get_portfile_permissions()
11702: group: (optional) the group you want the files associated with
11703: file: (optional) the file you want access info on
11704:
11705: Returns:
1.749 raeburn 11706: a hash (keys are file names) of hashes containing
11707: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
11708: values are XML containing access control settings (see below)
1.747 albertel 11709:
11710: Internal notes:
11711:
1.749 raeburn 11712: access controls are stored in file_permissions.db as key=value pairs.
11713: key -> path to file/file_name\0uniqueID:scope_end_start
11714: where scope -> public,guest,course,group,domains or users.
11715: end -> UNIX time for end of access (0 -> no end date)
11716: start -> UNIX time for start of access
11717:
11718: value -> XML description of access control
11719: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
11720: <start></start>
11721: <end></end>
11722:
11723: <password></password> for scope type = guest
11724:
11725: <domain></domain> for scope type = course or group
11726: <number></number>
11727: <roles id="">
11728: <role></role>
11729: <access></access>
11730: <section></section>
11731: <group></group>
11732: </roles>
11733:
11734: <dom></dom> for scope type = domains
11735:
11736: <users> for scope type = users
11737: <user>
11738: <uname></uname>
11739: <udom></udom>
11740: </user>
11741: </users>
11742: </scope>
11743:
11744: Access data is also aggregated for each file in an additional key=value pair:
11745: key -> path to file/file_name\0accesscontrol
11746: value -> reference to hash
11747: hash contains key = value pairs
11748: where key = uniqueID:scope_end_start
11749: value = UNIX time record was last updated
11750:
11751: Used to improve speed of look-ups of access controls for each file.
11752:
11753: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
11754:
11755: modify_access_controls():
11756:
11757: Modifies access controls for a portfolio file
11758: Args
11759: 1. file name
11760: 2. reference to hash of required changes,
11761: 3. domain
11762: 4. username
11763: where domain,username are the domain of the portfolio owner
11764: (either a user or a course)
11765:
11766: Returns:
11767: 1. result of additions or updates ('ok' or 'error', with error message).
11768: 2. result of deletions ('ok' or 'error', with error message).
11769: 3. reference to hash of any new or updated access controls.
11770: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
11771: key = integer (inbound ID)
11772: value = uniqueID
1.747 albertel 11773:
1.608 albertel 11774: =back
11775:
1.243 albertel 11776: =head2 HTTP Helper Routines
11777:
11778: =over 4
11779:
1.191 harris41 11780: =item *
11781:
11782: escape() : unpack non-word characters into CGI-compatible hex codes
11783:
11784: =item *
11785:
11786: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
11787:
1.243 albertel 11788: =back
11789:
11790: =head1 PRIVATE SUBROUTINES
11791:
11792: =head2 Underlying communication routines (Shouldn't call)
11793:
11794: =over 4
11795:
11796: =item *
11797:
11798: subreply() : tries to pass a message to lonc, returns con_lost if incapable
11799:
11800: =item *
11801:
11802: reply() : uses subreply to send a message to remote machine, logs all failures
11803:
11804: =item *
11805:
11806: critical() : passes a critical message to another server; if cannot
11807: get through then place message in connection buffer directory and
11808: returns con_delayed, if incapable of saving message, returns
11809: con_failed
11810:
11811: =item *
11812:
11813: reconlonc() : tries to reconnect lonc client processes.
11814:
11815: =back
11816:
11817: =head2 Resource Access Logging
11818:
11819: =over 4
11820:
11821: =item *
11822:
11823: flushcourselogs() : flush (save) buffer logs and access logs
11824:
11825: =item *
11826:
11827: courselog($what) : save message for course in hash
11828:
11829: =item *
11830:
11831: courseacclog($what) : save message for course using &courselog(). Perform
11832: special processing for specific resource types (problems, exams, quizzes, etc).
11833:
1.191 harris41 11834: =item *
11835:
11836: goodbye() : flush course logs and log shutting down; it is called in srm.conf
11837: as a PerlChildExitHandler
1.243 albertel 11838:
11839: =back
11840:
11841: =head2 Other
11842:
11843: =over 4
11844:
11845: =item *
11846:
11847: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 11848:
11849: =back
11850:
11851: =cut
1.877 foxr 11852:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>