Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1056.4.39
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1056.4.39! raeburn 4: # $Id: lonnet.pm,v 1.1056.4.38 2014/03/25 10:39:01 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.987 raeburn 639: my ($delthis,$regexp) = @_;
1.56 www 640: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 641: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 642: "Attempt to delete from environment ".$delthis);
643: return 'error';
644: }
1.917 albertel 645: my $opened = open(my $env_file,'+<',$env{'user.environment'});
646: if ($opened
1.915 albertel 647: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 648: &&
649: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
650: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 651: foreach my $key (keys(%disk_env)) {
1.987 raeburn 652: if ($regexp) {
653: if ($key=~/^$delthis/) {
654: delete($env{$key});
655: delete($disk_env{$key});
656: }
657: } else {
658: if ($key=~/^\Q$delthis\E/) {
659: delete($env{$key});
660: delete($disk_env{$key});
661: }
662: }
1.448 albertel 663: }
1.783 albertel 664: untie(%disk_env);
1.5 www 665: }
666: return 'ok';
1.369 albertel 667: }
668:
1.790 albertel 669: sub get_env_multiple {
670: my ($name) = @_;
671: my @values;
672: if (defined($env{$name})) {
673: # exists is it an array
674: if (ref($env{$name})) {
675: @values=@{ $env{$name} };
676: } else {
677: $values[0]=$env{$name};
678: }
679: }
680: return(@values);
681: }
682:
1.958 www 683: # ------------------------------------------------------------------- Locking
684:
685: sub set_lock {
686: my ($text)=@_;
687: $locknum++;
688: my $id=$$.'-'.$locknum;
689: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
690: 'session.lock.'.$id => $text});
691: return $id;
692: }
693:
694: sub get_locks {
695: my $num=0;
696: my %texts=();
697: foreach my $lock (split(/\,/,$env{'session.locks'})) {
698: if ($lock=~/\w/) {
699: $num++;
700: $texts{$lock}=$env{'session.lock.'.$lock};
701: }
702: }
703: return ($num,%texts);
704: }
705:
706: sub remove_lock {
707: my ($id)=@_;
708: my $newlocks='';
709: foreach my $lock (split(/\,/,$env{'session.locks'})) {
710: if (($lock=~/\w/) && ($lock ne $id)) {
711: $newlocks.=','.$lock;
712: }
713: }
714: &appenv({'session.locks' => $newlocks});
715: &delenv('session.lock.'.$id);
716: }
717:
718: sub remove_all_locks {
719: my $activelocks=$env{'session.locks'};
720: foreach my $lock (split(/\,/,$env{'session.locks'})) {
721: if ($lock=~/\w/) {
722: &remove_lock($lock);
723: }
724: }
725: }
726:
727:
1.369 albertel 728: # ------------------------------------------ Find out current server userload
729: sub userload {
730: my $numusers=0;
731: {
732: opendir(LONIDS,$perlvar{'lonIDsDir'});
733: my $filename;
734: my $curtime=time;
735: while ($filename=readdir(LONIDS)) {
1.925 albertel 736: next if ($filename eq '.' || $filename eq '..');
737: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 738: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 739: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 740: }
741: closedir(LONIDS);
742: }
743: my $userloadpercent=0;
744: my $maxuserload=$perlvar{'lonUserLoadLim'};
745: if ($maxuserload) {
1.371 albertel 746: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 747: }
1.372 albertel 748: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 749: return $userloadpercent;
1.283 www 750: }
751:
752: # ------------------------------------------ Fight off request when overloaded
753:
754: sub overloaderror {
755: my ($r,$checkserver)=@_;
756: unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
757: my $loadavg;
758: if ($checkserver eq $perlvar{'lonHostID'}) {
1.448 albertel 759: open(my $loadfile,'/proc/loadavg');
1.283 www 760: $loadavg=<$loadfile>;
761: $loadavg =~ s/\s.*//g;
1.285 matthew 762: $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448 albertel 763: close($loadfile);
1.283 www 764: } else {
765: $loadavg=&reply('load',$checkserver);
766: }
1.285 matthew 767: my $overload=$loadavg-100;
1.283 www 768: if ($overload>0) {
1.285 matthew 769: $r->err_headers_out->{'Retry-After'}=$overload;
1.283 www 770: $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554 www 771: return 413;
1.283 www 772: }
773: return '';
1.5 www 774: }
1.1 albertel 775:
776: # ------------------------------ Find server with least workload from spare.tab
1.11 www 777:
1.1 albertel 778: sub spareserver {
1.1056.4.23 raeburn 779: my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784 albertel 780: my $spare_server;
1.370 albertel 781: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 782: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
783: : $userloadpercent;
1.1056.4.8 raeburn 784: my ($uint_dom,$remotesessions);
1.1056.4.23 raeburn 785: if (($udom ne '') && (&domain($udom) ne '')) {
786: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1.1056.4.8 raeburn 787: $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.23 raeburn 788: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
1.1056.4.8 raeburn 789: $remotesessions = $udomdefaults{'remotesessions'};
790: }
1.784 albertel 791: foreach my $try_server (@{ $spareid{'primary'} }) {
1.1056.4.8 raeburn 792: if ($uint_dom) {
1.1056.4.23 raeburn 793: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
794: $try_server));
1.1056.4.8 raeburn 795: }
1.784 albertel 796: ($spare_server, $lowest_load) =
797: &compare_server_load($try_server, $spare_server, $lowest_load);
798: }
799:
800: my $found_server = ($spare_server ne '' && $lowest_load < 100);
801:
802: if (!$found_server) {
803: foreach my $try_server (@{ $spareid{'default'} }) {
1.1056.4.8 raeburn 804: if ($uint_dom) {
1.1056.4.23 raeburn 805: next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
806: $try_server));
1.1056.4.8 raeburn 807: }
1.784 albertel 808: ($spare_server, $lowest_load) =
809: &compare_server_load($try_server, $spare_server, $lowest_load);
810: }
811: }
812:
813: if (!$want_server_name) {
1.968 raeburn 814: my $protocol = 'http';
815: if ($protocol{$spare_server} eq 'https') {
816: $protocol = $protocol{$spare_server};
817: }
1.1001 raeburn 818: if (defined($spare_server)) {
819: my $hostname = &hostname($spare_server);
1.1056.4.8 raeburn 820: if (defined($hostname)) {
1.1001 raeburn 821: $spare_server = $protocol.'://'.$hostname;
822: }
823: }
1.784 albertel 824: }
825: return $spare_server;
826: }
827:
828: sub compare_server_load {
1.1056.4.38 raeburn 829: my ($try_server, $spare_server, $lowest_load, $required) = @_;
830:
831: if ($required) {
832: my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
833: my $remoterev = &get_server_loncaparev(undef,$try_server);
834: my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
835: if (($major eq '' && $minor eq '') ||
836: (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
837: return ($spare_server,$lowest_load);
838: }
839: }
1.784 albertel 840:
841: my $loadans = &reply('load', $try_server);
842: my $userloadans = &reply('userload',$try_server);
843:
844: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1056.4.29 raeburn 845: return ($spare_server, $lowest_load); #didn't get a number from the server
1.784 albertel 846: }
847:
848: my $load;
849: if ($loadans =~ /\d/) {
850: if ($userloadans =~ /\d/) {
851: #both are numbers, pick the bigger one
852: $load = ($loadans > $userloadans) ? $loadans
853: : $userloadans;
1.411 albertel 854: } else {
1.784 albertel 855: $load = $loadans;
1.411 albertel 856: }
1.784 albertel 857: } else {
858: $load = $userloadans;
859: }
860:
861: if (($load =~ /\d/) && ($load < $lowest_load)) {
862: $spare_server = $try_server;
863: $lowest_load = $load;
1.370 albertel 864: }
1.784 albertel 865: return ($spare_server,$lowest_load);
1.202 matthew 866: }
1.914 albertel 867:
868: # --------------------------- ask offload servers if user already has a session
869: sub find_existing_session {
870: my ($udom,$uname) = @_;
871: foreach my $try_server (@{ $spareid{'primary'} },
872: @{ $spareid{'default'} }) {
873: return $try_server if (&has_user_session($try_server, $udom, $uname));
874: }
875: return;
876: }
877:
878: # -------------------------------- ask if server already has a session for user
879: sub has_user_session {
880: my ($lonid,$udom,$uname) = @_;
881: my $result = &reply(join(':','userhassession',
882: map {&escape($_)} ($udom,$uname)),$lonid);
883: return 1 if ($result eq 'ok');
884:
885: return 0;
886: }
887:
1.1056.4.3 raeburn 888: # --------- determine least loaded server in a user's domain which allows login
889:
890: sub choose_server {
1.1056.4.39! raeburn 891: my ($udom,$checkloginvia,$required,$notloadbal) = @_;
1.1056.4.3 raeburn 892: my %domconfhash = &Apache::loncommon::get_domainconf($udom);
893: my %servers = &get_servers($udom);
894: my $lowest_load = 30000;
1.1056.4.39! raeburn 895: my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
! 896: if ($notloadbal) {
! 897: ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
! 898: unless (defined($cached)) {
! 899: my $cachetime = 60*60*24;
! 900: my %domconfig =
! 901: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
! 902: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
! 903: $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
! 904: $cachetime);
! 905: }
! 906: }
! 907: }
1.1056.4.3 raeburn 908: foreach my $lonhost (keys(%servers)) {
1.1056.4.39! raeburn 909: if ($notloadbal) {
! 910: if (ref($balancers) eq 'HASH') {
! 911: next if (exists($balancers->{$lonhost}));
! 912: }
! 913: }
1.1056.4.31 raeburn 914: my $loginvia;
915: if ($checkloginvia) {
916: $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
917: if ($loginvia) {
918: my ($server,$path) = split(/:/,$loginvia);
919: ($login_host, $lowest_load) =
1.1056.4.38 raeburn 920: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.31 raeburn 921: if ($login_host eq $server) {
922: $portal_path = $path;
1.1056.4.36 raeburn 923: $isredirect = 1;
1.1056.4.31 raeburn 924: }
925: } else {
926: ($login_host, $lowest_load) =
1.1056.4.38 raeburn 927: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.31 raeburn 928: if ($login_host eq $lonhost) {
929: $portal_path = '';
1.1056.4.36 raeburn 930: $isredirect = '';
1.1056.4.31 raeburn 931: }
932: }
933: } else {
1.1056.4.3 raeburn 934: ($login_host, $lowest_load) =
1.1056.4.38 raeburn 935: &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1056.4.3 raeburn 936: }
937: }
938: if ($login_host ne '') {
1.1056.4.31 raeburn 939: $hostname = &hostname($login_host);
1.1056.4.3 raeburn 940: }
1.1056.4.36 raeburn 941: return ($login_host,$hostname,$portal_path,$isredirect);
1.1056.4.3 raeburn 942: }
943:
1.202 matthew 944: # --------------------------------------------- Try to change a user's password
945:
946: sub changepass {
1.799 raeburn 947: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 948: $currentpass = &escape($currentpass);
949: $newpass = &escape($newpass);
1.1030 raeburn 950: my $lonhost = $perlvar{'lonHostID'};
951: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 952: $server);
953: if (! $answer) {
954: &logthis("No reply on password change request to $server ".
955: "by $uname in domain $udom.");
956: } elsif ($answer =~ "^ok") {
957: &logthis("$uname in $udom successfully changed their password ".
958: "on $server.");
959: } elsif ($answer =~ "^pwchange_failure") {
960: &logthis("$uname in $udom was unable to change their password ".
961: "on $server. The action was blocked by either lcpasswd ".
962: "or pwchange");
963: } elsif ($answer =~ "^non_authorized") {
964: &logthis("$uname in $udom did not get their password correct when ".
965: "attempting to change it on $server.");
966: } elsif ($answer =~ "^auth_mode_error") {
967: &logthis("$uname in $udom attempted to change their password despite ".
968: "not being locally or internally authenticated on $server.");
969: } elsif ($answer =~ "^unknown_user") {
970: &logthis("$uname in $udom attempted to change their password ".
971: "on $server but were unable to because $server is not ".
972: "their home server.");
973: } elsif ($answer =~ "^refused") {
974: &logthis("$server refused to change $uname in $udom password because ".
975: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 976: } elsif ($answer =~ "invalid_client") {
977: &logthis("$server refused to change $uname in $udom password because ".
978: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 979: }
980: return $answer;
1.1 albertel 981: }
982:
1.169 harris41 983: # ----------------------- Try to determine user's current authentication scheme
984:
985: sub queryauthenticate {
986: my ($uname,$udom)=@_;
1.456 albertel 987: my $uhome=&homeserver($uname,$udom);
988: if (!$uhome) {
989: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
990: return 'no_host';
991: }
992: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
993: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
994: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 995: }
1.456 albertel 996: return $answer;
1.169 harris41 997: }
998:
1.1 albertel 999: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 1000:
1.1 albertel 1001: sub authenticate {
1.1056.4.3 raeburn 1002: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 1003: $upass=&escape($upass);
1004: $uname= &LONCAPA::clean_username($uname);
1.836 www 1005: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 1006: my $newhome;
1.836 www 1007: if ((!$uhome) || ($uhome eq 'no_host')) {
1008: # Maybe the machine was offline and only re-appeared again recently?
1009: &reconlonc();
1010: # One more
1.952 raeburn 1011: $uhome=&homeserver($uname,$udom,1);
1012: if (($uhome eq 'no_host') && $checkdefauth) {
1013: if (defined(&domain($udom,'primary'))) {
1014: $newhome=&domain($udom,'primary');
1015: }
1016: if ($newhome ne '') {
1017: $uhome = $newhome;
1018: }
1019: }
1.836 www 1020: if ((!$uhome) || ($uhome eq 'no_host')) {
1021: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 1022: return 'no_host';
1023: }
1.1 albertel 1024: }
1.1056.4.3 raeburn 1025: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 1026: if ($answer eq 'authorized') {
1.952 raeburn 1027: if ($newhome) {
1028: &logthis("User $uname at $udom authorized by $uhome, but needs account");
1029: return 'no_account_on_host';
1030: } else {
1031: &logthis("User $uname at $udom authorized by $uhome");
1032: return $uhome;
1033: }
1.471 albertel 1034: }
1035: if ($answer eq 'non_authorized') {
1036: &logthis("User $uname at $udom rejected by $uhome");
1037: return 'no_host';
1.9 www 1038: }
1.471 albertel 1039: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 1040: return 'no_host';
1041: }
1042:
1.1056.4.3 raeburn 1043: sub can_host_session {
1044: my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1045: my $canhost = 1;
1046: my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1047: if (ref($remotesessions) eq 'HASH') {
1048: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1049: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1050: $canhost = 0;
1051: } else {
1052: $canhost = 1;
1053: }
1054: }
1055: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1056: if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1057: $canhost = 1;
1058: } else {
1059: $canhost = 0;
1060: }
1061: }
1062: if ($canhost) {
1063: if ($remotesessions->{'version'} ne '') {
1064: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
1065: if ($reqmajor ne '' && $reqminor ne '') {
1066: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
1067: my $major = $1;
1068: my $minor = $2;
1069: if (($major < $reqmajor ) ||
1070: (($major == $reqmajor) && ($minor < $reqminor))) {
1071: $canhost = 0;
1072: }
1073: } else {
1074: $canhost = 0;
1075: }
1076: }
1077: }
1078: }
1079: }
1080: if ($canhost) {
1081: if (ref($hostedsessions) eq 'HASH') {
1.1056.4.28 raeburn 1082: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
1083: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1056.4.3 raeburn 1084: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1085: if (($uint_dom ne '') &&
1086: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1056.4.3 raeburn 1087: $canhost = 0;
1088: } else {
1089: $canhost = 1;
1090: }
1091: }
1092: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1056.4.28 raeburn 1093: if (($uint_dom ne '') &&
1094: (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1056.4.3 raeburn 1095: $canhost = 1;
1096: } else {
1097: $canhost = 0;
1098: }
1099: }
1100: }
1101: }
1102: return $canhost;
1103: }
1104:
1.1056.4.8 raeburn 1105: sub spare_can_host {
1106: my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
1107: my $canhost=1;
1108: my @intdoms;
1109: my $internet_names = &Apache::lonnet::get_internet_names($try_server);
1110: if (ref($internet_names) eq 'ARRAY') {
1111: @intdoms = @{$internet_names};
1112: }
1113: unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
1114: my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
1115: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
1116: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
1117: my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
1118: $canhost = &can_host_session($udom,$try_server,$remoterev,
1119: $remotesessions,
1120: $defdomdefaults{'hostedsessions'});
1121: }
1122: return $canhost;
1123: }
1124:
1.1 albertel 1125: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 1126:
1.599 albertel 1127: my %homecache;
1.1 albertel 1128: sub homeserver {
1.230 stredwic 1129: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 1130: my $index="$uname:$udom";
1.426 albertel 1131:
1.599 albertel 1132: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 1133:
1134: my %servers = &get_servers($udom,'library');
1135: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 1136: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 1137: exists($badServerCache{$tryserver}));
1.841 albertel 1138:
1139: my $answer=reply("home:$udom:$uname",$tryserver);
1140: if ($answer eq 'found') {
1141: delete($badServerCache{$tryserver});
1142: return $homecache{$index}=$tryserver;
1143: } elsif ($answer eq 'no_host') {
1144: $badServerCache{$tryserver}=1;
1145: }
1.1 albertel 1146: }
1147: return 'no_host';
1.70 www 1148: }
1149:
1150: # ------------------------------------- Find the usernames behind a list of IDs
1151:
1152: sub idget {
1153: my ($udom,@ids)=@_;
1154: my %returnhash=();
1155:
1.841 albertel 1156: my %servers = &get_servers($udom,'library');
1157: foreach my $tryserver (keys(%servers)) {
1158: my $idlist=join('&',@ids);
1159: $idlist=~tr/A-Z/a-z/;
1160: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
1161: my @answer=();
1162: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1163: @answer=split(/\&/,$reply);
1164: } ;
1165: my $i;
1166: for ($i=0;$i<=$#ids;$i++) {
1167: if ($answer[$i]) {
1168: $returnhash{$ids[$i]}=$answer[$i];
1169: }
1170: }
1171: }
1.70 www 1172: return %returnhash;
1173: }
1174:
1175: # ------------------------------------- Find the IDs behind a list of usernames
1176:
1177: sub idrget {
1178: my ($udom,@unames)=@_;
1179: my %returnhash=();
1.800 albertel 1180: foreach my $uname (@unames) {
1181: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1182: }
1.70 www 1183: return %returnhash;
1184: }
1185:
1186: # ------------------------------- Store away a list of names and associated IDs
1187:
1188: sub idput {
1189: my ($udom,%ids)=@_;
1190: my %servers=();
1.800 albertel 1191: foreach my $uname (keys(%ids)) {
1192: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1193: my $uhom=&homeserver($uname,$udom);
1.70 www 1194: if ($uhom ne 'no_host') {
1.800 albertel 1195: my $id=&escape($ids{$uname});
1.70 www 1196: $id=~tr/A-Z/a-z/;
1.800 albertel 1197: my $esc_unam=&escape($uname);
1.70 www 1198: if ($servers{$uhom}) {
1.800 albertel 1199: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1200: } else {
1.800 albertel 1201: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1202: }
1203: }
1.191 harris41 1204: }
1.800 albertel 1205: foreach my $server (keys(%servers)) {
1206: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1207: }
1.344 www 1208: }
1209:
1.1023 raeburn 1210: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1211: sub dump_dom {
1.1023 raeburn 1212: my ($namespace,$udom,$regexp,$range)=@_;
1.1012 raeburn 1213: if (!$udom) {
1214: $udom=$env{'user.domain'};
1215: }
1216: my %returnhash;
1.1023 raeburn 1217: if ($udom) {
1218: my $uname = &get_domainconfiguser($udom);
1219: %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012 raeburn 1220: }
1221: return %returnhash;
1222: }
1223:
1.1023 raeburn 1224: # ------------------------------------------ get items from domain db files
1.806 raeburn 1225:
1226: sub get_dom {
1.860 raeburn 1227: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 1228: my $items='';
1229: foreach my $item (@$storearr) {
1230: $items.=&escape($item).'&';
1231: }
1232: $items=~s/\&$//;
1.860 raeburn 1233: if (!$udom) {
1234: $udom=$env{'user.domain'};
1235: if (defined(&domain($udom,'primary'))) {
1236: $uhome=&domain($udom,'primary');
1237: } else {
1.874 albertel 1238: undef($uhome);
1.860 raeburn 1239: }
1240: } else {
1241: if (!$uhome) {
1242: if (defined(&domain($udom,'primary'))) {
1243: $uhome=&domain($udom,'primary');
1244: }
1245: }
1246: }
1247: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1248: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1249: my %returnhash;
1.875 albertel 1250: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1251: return %returnhash;
1252: }
1.806 raeburn 1253: my @pairs=split(/\&/,$rep);
1254: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1255: return @pairs;
1256: }
1257: my $i=0;
1258: foreach my $item (@$storearr) {
1259: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1260: $i++;
1261: }
1262: return %returnhash;
1263: } else {
1.880 banghart 1264: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1265: }
1266: }
1267:
1268: # -------------------------------------------- put items in domain db files
1269:
1270: sub put_dom {
1.860 raeburn 1271: my ($namespace,$storehash,$udom,$uhome)=@_;
1272: if (!$udom) {
1273: $udom=$env{'user.domain'};
1274: if (defined(&domain($udom,'primary'))) {
1275: $uhome=&domain($udom,'primary');
1276: } else {
1.874 albertel 1277: undef($uhome);
1.860 raeburn 1278: }
1279: } else {
1280: if (!$uhome) {
1281: if (defined(&domain($udom,'primary'))) {
1282: $uhome=&domain($udom,'primary');
1283: }
1284: }
1285: }
1286: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1287: my $items='';
1288: foreach my $item (keys(%$storehash)) {
1289: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1290: }
1291: $items=~s/\&$//;
1292: return &reply("putdom:$udom:$namespace:$items",$uhome);
1293: } else {
1.860 raeburn 1294: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1295: }
1296: }
1297:
1.1023 raeburn 1298: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1299: sub newput_dom {
1.1023 raeburn 1300: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1301: my $result;
1302: if (!$udom) {
1303: $udom=$env{'user.domain'};
1304: }
1.1023 raeburn 1305: if ($udom) {
1306: my $uname = &get_domainconfiguser($udom);
1307: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1308: }
1309: return $result;
1310: }
1311:
1.1023 raeburn 1312: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1313: sub del_dom {
1.1023 raeburn 1314: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1315: if (ref($storearr) eq 'ARRAY') {
1316: if (!$udom) {
1317: $udom=$env{'user.domain'};
1318: }
1.1023 raeburn 1319: if ($udom) {
1320: my $uname = &get_domainconfiguser($udom);
1321: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1322: }
1323: }
1324: }
1325:
1.1023 raeburn 1326: # ----------------------------------construct domainconfig user for a domain
1327: sub get_domainconfiguser {
1328: my ($udom) = @_;
1329: return $udom.'-domainconfig';
1330: }
1331:
1.837 raeburn 1332: sub retrieve_inst_usertypes {
1333: my ($udom) = @_;
1334: my (%returnhash,@order);
1.989 raeburn 1335: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1336: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1337: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1338: %returnhash = %{$domdefs{'inststatustypes'}};
1339: @order = @{$domdefs{'inststatusorder'}};
1340: } else {
1341: if (defined(&domain($udom,'primary'))) {
1342: my $uhome=&domain($udom,'primary');
1343: my $rep=&reply("inst_usertypes:$udom",$uhome);
1344: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1345: &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
1346: return (\%returnhash,\@order);
1347: }
1348: my ($hashitems,$orderitems) = split(/:/,$rep);
1349: my @pairs=split(/\&/,$hashitems);
1350: foreach my $item (@pairs) {
1351: my ($key,$value)=split(/=/,$item,2);
1352: $key = &unescape($key);
1353: next if ($key =~ /^error: 2 /);
1354: $returnhash{$key}=&thaw_unescape($value);
1355: }
1356: my @esc_order = split(/\&/,$orderitems);
1357: foreach my $item (@esc_order) {
1358: push(@order,&unescape($item));
1359: }
1360: } else {
1361: &logthis("get_dom failed - no primary domain server for $udom");
1.837 raeburn 1362: }
1363: }
1364: return (\%returnhash,\@order);
1365: }
1366:
1.868 raeburn 1367: sub is_domainimage {
1368: my ($url) = @_;
1369: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
1370: if (&domain($1) ne '') {
1371: return '1';
1372: }
1373: }
1374: return;
1375: }
1376:
1.899 raeburn 1377: sub inst_directory_query {
1378: my ($srch) = @_;
1379: my $udom = $srch->{'srchdomain'};
1380: my %results;
1381: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1382: my $outcome;
1.899 raeburn 1383: if ($homeserver ne '') {
1.904 albertel 1384: my $queryid=&reply("querysend:instdirsearch:".
1385: &escape($srch->{'srchby'}).':'.
1386: &escape($srch->{'srchterm'}).':'.
1387: &escape($srch->{'srchtype'}),$homeserver);
1388: my $host=&hostname($homeserver);
1389: if ($queryid !~/^\Q$host\E\_/) {
1390: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1391: return;
1392: }
1393: my $response = &get_query_reply($queryid);
1394: my $maxtries = 5;
1395: my $tries = 1;
1396: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1397: $response = &get_query_reply($queryid);
1398: $tries ++;
1399: }
1400:
1401: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 1402: if ($response eq 'unavailable') {
1403: $outcome = $response;
1404: } else {
1405: $outcome = 'ok';
1406: my @matches = split(/\n/,$response);
1407: foreach my $match (@matches) {
1408: my ($key,$value) = split(/=/,$match);
1409: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
1410: }
1.899 raeburn 1411: }
1412: }
1413: }
1.909 raeburn 1414: return ($outcome,%results);
1.899 raeburn 1415: }
1416:
1417: sub usersearch {
1418: my ($srch) = @_;
1419: my $dom = $srch->{'srchdomain'};
1420: my %results;
1421: my %libserv = &all_library();
1422: my $query = 'usersearch';
1423: foreach my $tryserver (keys(%libserv)) {
1424: if (&host_domain($tryserver) eq $dom) {
1425: my $host=&hostname($tryserver);
1426: my $queryid=
1.911 raeburn 1427: &reply("querysend:".&escape($query).':'.
1428: &escape($srch->{'srchby'}).':'.
1.899 raeburn 1429: &escape($srch->{'srchtype'}).':'.
1430: &escape($srch->{'srchterm'}),$tryserver);
1431: if ($queryid !~/^\Q$host\E\_/) {
1432: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 1433: next;
1.899 raeburn 1434: }
1435: my $reply = &get_query_reply($queryid);
1436: my $maxtries = 1;
1437: my $tries = 1;
1438: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1439: $reply = &get_query_reply($queryid);
1440: $tries ++;
1441: }
1442: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1443: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
1444: } else {
1.911 raeburn 1445: my @matches;
1446: if ($reply =~ /\n/) {
1447: @matches = split(/\n/,$reply);
1448: } else {
1449: @matches = split(/\&/,$reply);
1450: }
1.899 raeburn 1451: foreach my $match (@matches) {
1452: my ($uname,$udom,%userhash);
1.911 raeburn 1453: foreach my $entry (split(/:/,$match)) {
1454: my ($key,$value) =
1455: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 1456: $userhash{$key} = $value;
1457: if ($key eq 'username') {
1458: $uname = $value;
1459: } elsif ($key eq 'domain') {
1460: $udom = $value;
1.911 raeburn 1461: }
1.899 raeburn 1462: }
1463: $results{$uname.':'.$udom} = \%userhash;
1464: }
1465: }
1466: }
1467: }
1468: return %results;
1469: }
1470:
1.912 raeburn 1471: sub get_instuser {
1472: my ($udom,$uname,$id) = @_;
1473: my $homeserver = &domain($udom,'primary');
1474: my ($outcome,%results);
1475: if ($homeserver ne '') {
1476: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
1477: &escape($id).':'.&escape($udom),$homeserver);
1478: my $host=&hostname($homeserver);
1479: if ($queryid !~/^\Q$host\E\_/) {
1480: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1481: return;
1482: }
1483: my $response = &get_query_reply($queryid);
1484: my $maxtries = 5;
1485: my $tries = 1;
1486: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1487: $response = &get_query_reply($queryid);
1488: $tries ++;
1489: }
1490: if (!&error($response) && $response ne 'refused') {
1491: if ($response eq 'unavailable') {
1492: $outcome = $response;
1493: } else {
1494: $outcome = 'ok';
1495: my @matches = split(/\n/,$response);
1496: foreach my $match (@matches) {
1497: my ($key,$value) = split(/=/,$match);
1498: $results{&unescape($key)} = &thaw_unescape($value);
1499: }
1500: }
1501: }
1502: }
1503: my %userinfo;
1504: if (ref($results{$uname}) eq 'HASH') {
1505: %userinfo = %{$results{$uname}};
1506: }
1507: return ($outcome,%userinfo);
1508: }
1509:
1510: sub inst_rulecheck {
1.923 raeburn 1511: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 1512: my %returnhash;
1513: if ($udom ne '') {
1514: if (ref($rules) eq 'ARRAY') {
1515: @{$rules} = map {&escape($_);} (@{$rules});
1516: my $rulestr = join(':',@{$rules});
1517: my $homeserver=&domain($udom,'primary');
1518: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1519: my $response;
1520: if ($item eq 'username') {
1521: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
1522: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 1523: $homeserver));
1.923 raeburn 1524: } elsif ($item eq 'id') {
1525: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
1526: ':'.&escape($id).':'.$rulestr,
1527: $homeserver));
1.945 raeburn 1528: } elsif ($item eq 'selfcreate') {
1529: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 1530: &escape($udom).':'.&escape($uname).
1531: ':'.$rulestr,$homeserver));
1.923 raeburn 1532: }
1.912 raeburn 1533: if ($response ne 'refused') {
1534: my @pairs=split(/\&/,$response);
1535: foreach my $item (@pairs) {
1536: my ($key,$value)=split(/=/,$item,2);
1537: $key = &unescape($key);
1538: next if ($key =~ /^error: 2 /);
1539: $returnhash{$key}=&thaw_unescape($value);
1540: }
1541: }
1542: }
1543: }
1544: }
1545: return %returnhash;
1546: }
1547:
1548: sub inst_userrules {
1.923 raeburn 1549: my ($udom,$check) = @_;
1.912 raeburn 1550: my (%ruleshash,@ruleorder);
1551: if ($udom ne '') {
1552: my $homeserver=&domain($udom,'primary');
1553: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1554: my $response;
1555: if ($check eq 'id') {
1556: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 1557: $homeserver);
1.943 raeburn 1558: } elsif ($check eq 'email') {
1559: $response=&reply('instemailrules:'.&escape($udom),
1560: $homeserver);
1.923 raeburn 1561: } else {
1562: $response=&reply('instuserrules:'.&escape($udom),
1563: $homeserver);
1564: }
1.912 raeburn 1565: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 1566: ($response ne 'unknown_cmd') &&
1.912 raeburn 1567: ($response ne 'no_such_host')) {
1568: my ($hashitems,$orderitems) = split(/:/,$response);
1569: my @pairs=split(/\&/,$hashitems);
1570: foreach my $item (@pairs) {
1571: my ($key,$value)=split(/=/,$item,2);
1572: $key = &unescape($key);
1573: next if ($key =~ /^error: 2 /);
1574: $ruleshash{$key}=&thaw_unescape($value);
1575: }
1576: my @esc_order = split(/\&/,$orderitems);
1577: foreach my $item (@esc_order) {
1578: push(@ruleorder,&unescape($item));
1579: }
1580: }
1581: }
1582: }
1583: return (\%ruleshash,\@ruleorder);
1584: }
1585:
1.976 raeburn 1586: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 1587:
1588: sub get_domain_defaults {
1589: my ($domain) = @_;
1590: my $cachetime = 60*60*24;
1591: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
1592: if (defined($cached)) {
1593: if (ref($result) eq 'HASH') {
1594: return %{$result};
1595: }
1596: }
1597: my %domdefaults;
1598: my %domconfig =
1.989 raeburn 1599: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 1600: 'requestcourses','inststatus',
1.1056.4.3 raeburn 1601: 'coursedefaults','usersessions'],$domain);
1.943 raeburn 1602: if (ref($domconfig{'defaults'}) eq 'HASH') {
1603: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
1604: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
1605: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 1606: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 1607: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1056.4.34 raeburn 1608: $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943 raeburn 1609: } else {
1610: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
1611: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
1612: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
1613: }
1.976 raeburn 1614: if (ref($domconfig{'quotas'}) eq 'HASH') {
1615: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
1616: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
1617: } else {
1618: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1619: }
1620: my @usertools = ('aboutme','blog','portfolio');
1621: foreach my $item (@usertools) {
1622: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
1623: $domdefaults{$item} = $domconfig{'quotas'}{$item};
1624: }
1625: }
1626: }
1.985 raeburn 1627: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006 raeburn 1628: foreach my $item ('official','unofficial','community') {
1.985 raeburn 1629: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
1630: }
1631: }
1.989 raeburn 1632: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1633: foreach my $item ('inststatustypes','inststatusorder') {
1634: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
1635: }
1636: }
1.1047 raeburn 1637: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1638: foreach my $item ('canuse_pdfforms') {
1639: $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
1640: }
1641: }
1.1056.4.3 raeburn 1642: if (ref($domconfig{'usersessions'}) eq 'HASH') {
1643: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
1644: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
1645: }
1646: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
1647: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
1648: }
1649: }
1.943 raeburn 1650: &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
1651: $cachetime);
1652: return %domdefaults;
1653: }
1654:
1.344 www 1655: # --------------------------------------------------- Assign a key to a student
1656:
1657: sub assign_access_key {
1.364 www 1658: #
1659: # a valid key looks like uname:udom#comments
1660: # comments are being appended
1661: #
1.498 www 1662: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
1663: $kdom=
1.620 albertel 1664: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 1665: $knum=
1.620 albertel 1666: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 1667: $cdom=
1.620 albertel 1668: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1669: $cnum=
1.620 albertel 1670: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1671: $udom=$env{'user.name'} unless (defined($udom));
1672: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 1673: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 1674: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 1675: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 1676: # assigned to this person
1677: # - this should not happen,
1.345 www 1678: # unless something went wrong
1679: # the first time around
1680: # ready to assign
1.364 www 1681: $logentry=$1.'; '.$logentry;
1.496 www 1682: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 1683: $kdom,$knum) eq 'ok') {
1.345 www 1684: # key now belongs to user
1.346 www 1685: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 1686: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 1687: &appenv({'environment.'.$envkey => $ckey});
1.345 www 1688: return 'ok';
1689: } else {
1690: return
1691: 'error: Count not permanently assign key, will need to be re-entered later.';
1692: }
1693: } else {
1694: return 'error: Could not assign key, try again later.';
1695: }
1.364 www 1696: } elsif (!$existing{$ckey}) {
1.345 www 1697: # the key does not exist
1698: return 'error: The key does not exist';
1699: } else {
1700: # the key is somebody else's
1701: return 'error: The key is already in use';
1702: }
1.344 www 1703: }
1704:
1.364 www 1705: # ------------------------------------------ put an additional comment on a key
1706:
1707: sub comment_access_key {
1708: #
1709: # a valid key looks like uname:udom#comments
1710: # comments are being appended
1711: #
1712: my ($ckey,$cdom,$cnum,$logentry)=@_;
1713: $cdom=
1.620 albertel 1714: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 1715: $cnum=
1.620 albertel 1716: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 1717: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1718: if ($existing{$ckey}) {
1719: $existing{$ckey}.='; '.$logentry;
1720: # ready to assign
1.367 www 1721: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 1722: $cdom,$cnum) eq 'ok') {
1723: return 'ok';
1724: } else {
1725: return 'error: Count not store comment.';
1726: }
1727: } else {
1728: # the key does not exist
1729: return 'error: The key does not exist';
1730: }
1731: }
1732:
1.344 www 1733: # ------------------------------------------------------ Generate a set of keys
1734:
1735: sub generate_access_keys {
1.364 www 1736: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 1737: $cdom=
1.620 albertel 1738: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1739: $cnum=
1.620 albertel 1740: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 1741: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 1742: unless (($cdom) && ($cnum)) { return 0; }
1743: if ($number>10000) { return 0; }
1744: sleep(2); # make sure don't get same seed twice
1745: srand(time()^($$+($$<<15))); # from "Programming Perl"
1746: my $total=0;
1747: for (my $i=1;$i<=$number;$i++) {
1748: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
1749: sprintf("%lx",int(100000*rand)).'-'.
1750: sprintf("%lx",int(100000*rand));
1751: $newkey=~s/1/g/g; # folks mix up 1 and l
1752: $newkey=~s/0/h/g; # and also 0 and O
1753: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
1754: if ($existing{$newkey}) {
1755: $i--;
1756: } else {
1.364 www 1757: if (&put('accesskeys',
1758: { $newkey => '# generated '.localtime().
1.620 albertel 1759: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 1760: '; '.$logentry },
1761: $cdom,$cnum) eq 'ok') {
1.344 www 1762: $total++;
1763: }
1764: }
1765: }
1.620 albertel 1766: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 1767: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
1768: return $total;
1769: }
1770:
1771: # ------------------------------------------------------- Validate an accesskey
1772:
1773: sub validate_access_key {
1774: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
1775: $cdom=
1.620 albertel 1776: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1777: $cnum=
1.620 albertel 1778: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1779: $udom=$env{'user.domain'} unless (defined($udom));
1780: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 1781: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 1782: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 1783: }
1784:
1785: # ------------------------------------- Find the section of student in a course
1.652 albertel 1786: sub devalidate_getsection_cache {
1787: my ($udom,$unam,$courseid)=@_;
1788: my $hashid="$udom:$unam:$courseid";
1789: &devalidate_cache_new('getsection',$hashid);
1790: }
1.298 matthew 1791:
1.815 albertel 1792: sub courseid_to_courseurl {
1793: my ($courseid) = @_;
1794: #already url style courseid
1795: return $courseid if ($courseid =~ m{^/});
1796:
1797: if (exists($env{'course.'.$courseid.'.num'})) {
1798: my $cnum = $env{'course.'.$courseid.'.num'};
1799: my $cdom = $env{'course.'.$courseid.'.domain'};
1800: return "/$cdom/$cnum";
1801: }
1802:
1803: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
1804: if (exists($courseinfo{'num'})) {
1805: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
1806: }
1807:
1808: return undef;
1809: }
1810:
1.298 matthew 1811: sub getsection {
1812: my ($udom,$unam,$courseid)=@_;
1.599 albertel 1813: my $cachetime=1800;
1.551 albertel 1814:
1815: my $hashid="$udom:$unam:$courseid";
1.599 albertel 1816: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 1817: if (defined($cached)) { return $result; }
1818:
1.298 matthew 1819: my %Pending;
1820: my %Expired;
1821: #
1822: # Each role can either have not started yet (pending), be active,
1823: # or have expired.
1824: #
1825: # If there is an active role, we are done.
1826: #
1827: # If there is more than one role which has not started yet,
1828: # choose the one which will start sooner
1829: # If there is one role which has not started yet, return it.
1830: #
1831: # If there is more than one expired role, choose the one which ended last.
1832: # If there is a role which has expired, return it.
1833: #
1.815 albertel 1834: $courseid = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 1835: my $extra = &freeze_escape({'skipcheck' => 1});
1836: my %roleshash = &dump('roles',$udom,$unam,$courseid,undef,$extra);
1.817 raeburn 1837: foreach my $key (keys(%roleshash)) {
1.479 albertel 1838: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1839: my $section=$1;
1840: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1841: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1842: my $now=time;
1.548 albertel 1843: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1844: $Expired{$end}=$section;
1845: next;
1846: }
1.548 albertel 1847: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1848: $Pending{$start}=$section;
1849: next;
1850: }
1.599 albertel 1851: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1852: }
1853: #
1854: # Presumedly there will be few matching roles from the above
1855: # loop and the sorting time will be negligible.
1856: if (scalar(keys(%Pending))) {
1857: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1858: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1859: }
1860: if (scalar(keys(%Expired))) {
1861: my @sorted = sort {$a <=> $b} keys(%Expired);
1862: my $time = pop(@sorted);
1.599 albertel 1863: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1864: }
1.599 albertel 1865: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1866: }
1.70 www 1867:
1.599 albertel 1868: sub save_cache {
1869: &purge_remembered();
1.722 albertel 1870: #&Apache::loncommon::validate_page();
1.620 albertel 1871: undef(%env);
1.780 albertel 1872: undef($env_loaded);
1.599 albertel 1873: }
1.452 albertel 1874:
1.599 albertel 1875: my $to_remember=-1;
1876: my %remembered;
1877: my %accessed;
1878: my $kicks=0;
1879: my $hits=0;
1.849 albertel 1880: sub make_key {
1881: my ($name,$id) = @_;
1.872 albertel 1882: if (length($id) > 65
1883: && length(&escape($id)) > 200) {
1884: $id=length($id).':'.&Digest::MD5::md5_hex($id);
1885: }
1.849 albertel 1886: return &escape($name.':'.$id);
1887: }
1888:
1.599 albertel 1889: sub devalidate_cache_new {
1890: my ($name,$id,$debug) = @_;
1891: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1892: $id=&make_key($name,$id);
1.599 albertel 1893: $memcache->delete($id);
1894: delete($remembered{$id});
1895: delete($accessed{$id});
1896: }
1897:
1898: sub is_cached_new {
1899: my ($name,$id,$debug) = @_;
1.849 albertel 1900: $id=&make_key($name,$id);
1.599 albertel 1901: if (exists($remembered{$id})) {
1902: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1903: $accessed{$id}=[&gettimeofday()];
1904: $hits++;
1905: return ($remembered{$id},1);
1906: }
1907: my $value = $memcache->get($id);
1908: if (!(defined($value))) {
1909: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1910: return (undef,undef);
1.416 albertel 1911: }
1.599 albertel 1912: if ($value eq '__undef__') {
1913: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1914: $value=undef;
1915: }
1916: &make_room($id,$value,$debug);
1917: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1918: return ($value,1);
1919: }
1920:
1921: sub do_cache_new {
1922: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1923: $id=&make_key($name,$id);
1.599 albertel 1924: my $setvalue=$value;
1925: if (!defined($setvalue)) {
1926: $setvalue='__undef__';
1927: }
1.623 albertel 1928: if (!defined($time) ) {
1929: $time=600;
1930: }
1.599 albertel 1931: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 1932: my $result = $memcache->set($id,$setvalue,$time);
1933: if (! $result) {
1.872 albertel 1934: &logthis("caching of id -> $id failed");
1.910 albertel 1935: $memcache->disconnect_all();
1.872 albertel 1936: }
1.600 albertel 1937: # need to make a copy of $value
1.919 albertel 1938: &make_room($id,$value,$debug);
1.599 albertel 1939: return $value;
1940: }
1941:
1942: sub make_room {
1943: my ($id,$value,$debug)=@_;
1.919 albertel 1944:
1945: $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
1946: : $value;
1.599 albertel 1947: if ($to_remember<0) { return; }
1948: $accessed{$id}=[&gettimeofday()];
1949: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1950: my $to_kick;
1951: my $max_time=0;
1952: foreach my $other (keys(%accessed)) {
1953: if (&tv_interval($accessed{$other}) > $max_time) {
1954: $to_kick=$other;
1955: $max_time=&tv_interval($accessed{$other});
1956: }
1957: }
1958: delete($remembered{$to_kick});
1959: delete($accessed{$to_kick});
1960: $kicks++;
1961: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1962: return;
1963: }
1964:
1.599 albertel 1965: sub purge_remembered {
1.604 albertel 1966: #&logthis("Tossing ".scalar(keys(%remembered)));
1967: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1968: undef(%remembered);
1969: undef(%accessed);
1.428 albertel 1970: }
1.70 www 1971: # ------------------------------------- Read an entry from a user's environment
1972:
1973: sub userenvironment {
1974: my ($udom,$unam,@what)=@_;
1.976 raeburn 1975: my $items;
1976: foreach my $item (@what) {
1977: $items.=&escape($item).'&';
1978: }
1979: $items=~s/\&$//;
1.70 www 1980: my %returnhash=();
1.1009 raeburn 1981: my $uhome = &homeserver($unam,$udom);
1982: unless ($uhome eq 'no_host') {
1983: my @answer=split(/\&/,
1984: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 1985: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
1986: return %returnhash;
1987: }
1.1009 raeburn 1988: my $i;
1989: for ($i=0;$i<=$#what;$i++) {
1990: $returnhash{$what[$i]}=&unescape($answer[$i]);
1991: }
1.70 www 1992: }
1993: return %returnhash;
1.1 albertel 1994: }
1995:
1.617 albertel 1996: # ---------------------------------------------------------- Get a studentphoto
1997: sub studentphoto {
1998: my ($udom,$unam,$ext) = @_;
1999: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 2000: if (defined($env{'request.course.id'})) {
1.708 raeburn 2001: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 2002: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
2003: return(&retrievestudentphoto($udom,$unam,$ext));
2004: } else {
2005: my ($result,$perm_reqd)=
1.707 albertel 2006: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2007: if ($result eq 'ok') {
2008: if (!($perm_reqd eq 'yes')) {
2009: return(&retrievestudentphoto($udom,$unam,$ext));
2010: }
2011: }
2012: }
2013: }
2014: } else {
2015: my ($result,$perm_reqd) =
1.707 albertel 2016: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 2017: if ($result eq 'ok') {
2018: if (!($perm_reqd eq 'yes')) {
2019: return(&retrievestudentphoto($udom,$unam,$ext));
2020: }
2021: }
2022: }
2023: return '/adm/lonKaputt/lonlogo_broken.gif';
2024: }
2025:
2026: sub retrievestudentphoto {
2027: my ($udom,$unam,$ext,$type) = @_;
2028: my $home=&Apache::lonnet::homeserver($unam,$udom);
2029: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
2030: if ($ret eq 'ok') {
2031: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
2032: if ($type eq 'thumbnail') {
2033: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
2034: }
2035: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
2036: return $tokenurl;
2037: } else {
2038: if ($type eq 'thumbnail') {
2039: return '/adm/lonKaputt/genericstudent_tn.gif';
2040: } else {
2041: return '/adm/lonKaputt/lonlogo_broken.gif';
2042: }
1.617 albertel 2043: }
2044: }
2045:
1.263 www 2046: # -------------------------------------------------------------------- New chat
2047:
2048: sub chatsend {
1.724 raeburn 2049: my ($newentry,$anon,$group)=@_;
1.620 albertel 2050: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
2051: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2052: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 2053: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 2054: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 2055: &escape($newentry)).':'.$group,$chome);
1.292 www 2056: }
2057:
2058: # ------------------------------------------ Find current version of a resource
2059:
2060: sub getversion {
2061: my $fname=&clutter(shift);
2062: unless ($fname=~/^\/res\//) { return -1; }
2063: return ¤tversion(&filelocation('',$fname));
2064: }
2065:
2066: sub currentversion {
2067: my $fname=shift;
1.599 albertel 2068: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 2069: if (defined($cached)) { return $result; }
1.292 www 2070: my $author=$fname;
2071: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2072: my ($udom,$uname)=split(/\//,$author);
2073: my $home=homeserver($uname,$udom);
2074: if ($home eq 'no_host') {
2075: return -1;
2076: }
2077: my $answer=reply("currentversion:$fname",$home);
2078: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2079: return -1;
2080: }
1.599 albertel 2081: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 2082: }
2083:
1.1 albertel 2084: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 2085:
1.1 albertel 2086: sub subscribe {
2087: my $fname=shift;
1.761 raeburn 2088: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 2089: $fname=~s/[\n\r]//g;
1.1 albertel 2090: my $author=$fname;
2091: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2092: my ($udom,$uname)=split(/\//,$author);
2093: my $home=homeserver($uname,$udom);
1.335 albertel 2094: if ($home eq 'no_host') {
2095: return 'not_found';
1.1 albertel 2096: }
2097: my $answer=reply("sub:$fname",$home);
1.64 www 2098: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
2099: $answer.=' by '.$home;
2100: }
1.1 albertel 2101: return $answer;
2102: }
2103:
1.8 www 2104: # -------------------------------------------------------------- Replicate file
2105:
2106: sub repcopy {
2107: my $filename=shift;
1.23 www 2108: $filename=~s/\/+/\//g;
1.607 raeburn 2109: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
2110: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 2111: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 2112: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 2113: return &repcopy_userfile($filename);
2114: }
1.532 albertel 2115: $filename=~s/[\n\r]//g;
1.8 www 2116: my $transname="$filename.in.transfer";
1.828 www 2117: # FIXME: this should flock
1.607 raeburn 2118: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 2119: my $remoteurl=subscribe($filename);
1.64 www 2120: if ($remoteurl =~ /^con_lost by/) {
2121: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2122: return 'unavailable';
1.8 www 2123: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 2124: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 2125: return 'not_found';
1.64 www 2126: } elsif ($remoteurl =~ /^rejected by/) {
2127: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 2128: return 'forbidden';
1.20 www 2129: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 2130: return 'ok';
1.8 www 2131: } else {
1.290 www 2132: my $author=$filename;
2133: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
2134: my ($udom,$uname)=split(/\//,$author);
2135: my $home=homeserver($uname,$udom);
2136: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 2137: my @parts=split(/\//,$filename);
2138: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
2139: if ($path ne "$perlvar{'lonDocRoot'}/res") {
2140: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 2141: return 'bad_request';
1.8 www 2142: }
2143: my $count;
2144: for ($count=5;$count<$#parts;$count++) {
2145: $path.="/$parts[$count]";
2146: if ((-e $path)!=1) {
2147: mkdir($path,0777);
2148: }
2149: }
2150: my $ua=new LWP::UserAgent;
2151: my $request=new HTTP::Request('GET',"$remoteurl");
2152: my $response=$ua->request($request,$transname);
2153: if ($response->is_error()) {
2154: unlink($transname);
2155: my $message=$response->status_line;
1.672 albertel 2156: &logthis("<font color=\"blue\">WARNING:"
1.12 www 2157: ." LWP get: $message: $filename</font>");
1.607 raeburn 2158: return 'unavailable';
1.8 www 2159: } else {
1.16 www 2160: if ($remoteurl!~/\.meta$/) {
2161: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
2162: my $mresponse=$ua->request($mrequest,$filename.'.meta');
2163: if ($mresponse->is_error()) {
2164: unlink($filename.'.meta');
2165: &logthis(
1.672 albertel 2166: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 2167: }
2168: }
1.8 www 2169: rename($transname,$filename);
1.607 raeburn 2170: return 'ok';
1.8 www 2171: }
1.290 www 2172: }
1.8 www 2173: }
1.330 www 2174: }
2175:
2176: # ------------------------------------------------ Get server side include body
2177: sub ssi_body {
1.381 albertel 2178: my ($filelink,%form)=@_;
1.606 matthew 2179: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
2180: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
2181: }
1.953 www 2182: my $output='';
2183: my $response;
1.980 raeburn 2184: if ($filelink=~/^https?\:/) {
1.954 raeburn 2185: ($output,$response)=&externalssi($filelink);
1.953 www 2186: } else {
1.1004 droeschl 2187: $filelink .= $filelink=~/\?/ ? '&' : '?';
2188: $filelink .= 'inhibitmenu=yes';
1.953 www 2189: ($output,$response)=&ssi($filelink,%form);
2190: }
1.778 albertel 2191: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2192: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2193: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2194: if (wantarray) {
2195: return ($output, $response);
2196: } else {
2197: return $output;
2198: }
1.8 www 2199: }
2200:
1.15 www 2201: # --------------------------------------------------------- Server Side Include
2202:
1.782 albertel 2203: sub absolute_url {
2204: my ($host_name) = @_;
2205: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
2206: if ($host_name eq '') {
2207: $host_name = $ENV{'SERVER_NAME'};
2208: }
2209: return $protocol.$host_name;
2210: }
2211:
1.942 foxr 2212: #
2213: # Server side include.
2214: # Parameters:
2215: # fn Possibly encrypted resource name/id.
2216: # form Hash that describes how the rendering should be done
2217: # and other things.
1.944 foxr 2218: # Returns:
1.950 raeburn 2219: # Scalar context: The content of the response.
2220: # Array context: 2 element list of the content and the full response object.
1.942 foxr 2221: #
1.15 www 2222: sub ssi {
2223:
1.944 foxr 2224: my ($fn,%form)=@_;
1.15 www 2225: my $ua=new LWP::UserAgent;
1.23 www 2226: my $request;
1.711 albertel 2227:
2228: $form{'no_update_last_known'}=1;
1.895 albertel 2229: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 2230: if (%form) {
1.782 albertel 2231: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000 raeburn 2232: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23 www 2233: } else {
1.782 albertel 2234: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 2235: }
2236:
1.15 www 2237: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
2238: my $response=$ua->request($request);
2239:
1.944 foxr 2240: if (wantarray) {
2241: return ($response->content, $response);
2242: } else {
2243: return $response->content;
1.942 foxr 2244: }
1.324 www 2245: }
2246:
2247: sub externalssi {
2248: my ($url)=@_;
2249: my $ua=new LWP::UserAgent;
2250: my $request=new HTTP::Request('GET',$url);
2251: my $response=$ua->request($request);
1.954 raeburn 2252: if (wantarray) {
2253: return ($response->content, $response);
2254: } else {
2255: return $response->content;
2256: }
1.15 www 2257: }
1.254 www 2258:
1.492 albertel 2259: # -------------------------------- Allow a /uploaded/ URI to be vouched for
2260:
2261: sub allowuploaded {
2262: my ($srcurl,$url)=@_;
2263: $url=&clutter(&declutter($url));
2264: my $dir=$url;
2265: $dir=~s/\/[^\/]+$//;
2266: my %httpref=();
2267: my $httpurl=&hreflocation('',$url);
2268: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 2269: &Apache::lonnet::appenv(\%httpref);
1.254 www 2270: }
1.477 raeburn 2271:
1.478 albertel 2272: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 2273: # input: action, courseID, current domain, intended
1.637 raeburn 2274: # path to file, source of file, instruction to parse file for objects,
2275: # ref to hash for embedded objects,
2276: # ref to hash for codebase of java objects.
1.1056.4.16 raeburn 2277: # reference to scalar to accommodate mime type determined
2278: # from File::MMagic if $parser = parse.
1.637 raeburn 2279: #
1.485 raeburn 2280: # output: url to file (if action was uploaddoc),
2281: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 2282: #
1.478 albertel 2283: # Allows directory structure to be used within lonUsers/../userfiles/ for a
2284: # course.
1.477 raeburn 2285: #
1.478 albertel 2286: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2287: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
2288: # course's home server.
1.477 raeburn 2289: #
1.478 albertel 2290: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
2291: # be copied from $source (current location) to
2292: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2293: # and will then be copied to
2294: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
2295: # course's home server.
1.485 raeburn 2296: #
1.481 raeburn 2297: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 2298: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 2299: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2300: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
2301: # in course's home server.
1.637 raeburn 2302: #
1.477 raeburn 2303:
2304: sub process_coursefile {
1.1056.4.16 raeburn 2305: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
2306: $mimetype)=@_;
1.477 raeburn 2307: my $fetchresult;
1.638 albertel 2308: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 2309: if ($action eq 'propagate') {
1.638 albertel 2310: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
2311: $home);
1.481 raeburn 2312: } else {
1.477 raeburn 2313: my $fpath = '';
2314: my $fname = $file;
1.478 albertel 2315: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 2316: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 2317: my $filepath = &build_filepath($fpath);
1.481 raeburn 2318: if ($action eq 'copy') {
2319: if ($source eq '') {
2320: $fetchresult = 'no source file';
2321: return $fetchresult;
2322: } else {
2323: my $destination = $filepath.'/'.$fname;
2324: rename($source,$destination);
2325: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2326: $home);
1.481 raeburn 2327: }
2328: } elsif ($action eq 'uploaddoc') {
2329: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 2330: print $fh $env{'form.'.$source};
1.481 raeburn 2331: close($fh);
1.637 raeburn 2332: if ($parser eq 'parse') {
1.1024 raeburn 2333: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2334: my $type = $mm->checktype_filename($filepath.'/'.$fname);
2335: if ($type eq 'text/html') {
1.1024 raeburn 2336: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
2337: unless ($parse_result eq 'ok') {
2338: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
2339: }
1.637 raeburn 2340: }
1.1056.4.16 raeburn 2341: if (ref($mimetype)) {
2342: $$mimetype = $type;
2343: }
1.637 raeburn 2344: }
1.477 raeburn 2345: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2346: $home);
1.481 raeburn 2347: if ($fetchresult eq 'ok') {
2348: return '/uploaded/'.$fpath.'/'.$fname;
2349: } else {
2350: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2351: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 2352: return '/adm/notfound.html';
2353: }
1.477 raeburn 2354: }
2355: }
1.485 raeburn 2356: unless ( $fetchresult eq 'ok') {
1.477 raeburn 2357: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2358: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 2359: }
2360: return $fetchresult;
2361: }
2362:
1.637 raeburn 2363: sub build_filepath {
2364: my ($fpath) = @_;
2365: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
2366: unless ($fpath eq '') {
2367: my @parts=split('/',$fpath);
2368: foreach my $part (@parts) {
2369: $filepath.= '/'.$part;
2370: if ((-e $filepath)!=1) {
2371: mkdir($filepath,0777);
2372: }
2373: }
2374: }
2375: return $filepath;
2376: }
2377:
2378: sub store_edited_file {
1.638 albertel 2379: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 2380: my $file = $primary_url;
2381: $file =~ s#^/uploaded/$docudom/$docuname/##;
2382: my $fpath = '';
2383: my $fname = $file;
2384: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
2385: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
2386: my $filepath = &build_filepath($fpath);
2387: open(my $fh,'>'.$filepath.'/'.$fname);
2388: print $fh $content;
2389: close($fh);
1.638 albertel 2390: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 2391: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2392: $home);
1.637 raeburn 2393: if ($$fetchresult eq 'ok') {
2394: return '/uploaded/'.$fpath.'/'.$fname;
2395: } else {
1.638 albertel 2396: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
2397: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 2398: return '/adm/notfound.html';
2399: }
2400: }
2401:
1.531 albertel 2402: sub clean_filename {
1.831 albertel 2403: my ($fname,$args)=@_;
1.315 www 2404: # Replace Windows backslashes by forward slashes
1.257 www 2405: $fname=~s/\\/\//g;
1.831 albertel 2406: if (!$args->{'keep_path'}) {
2407: # Get rid of everything but the actual filename
2408: $fname=~s/^.*\/([^\/]+)$/$1/;
2409: }
1.315 www 2410: # Replace spaces by underscores
2411: $fname=~s/\s+/\_/g;
2412: # Replace all other weird characters by nothing
1.831 albertel 2413: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 2414: # Replace all .\d. sequences with _\d. so they no longer look like version
2415: # numbers
2416: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 2417: return $fname;
2418: }
1.1051 raeburn 2419: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
2420: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
2421: # image with the same aspect ratio as the original, but with dimensions which do
2422: # not exceed $resizewidth and $resizeheight.
2423:
1.984 neumanie 2424: sub resizeImage {
1.1051 raeburn 2425: my ($img_path,$resizewidth,$resizeheight) = @_;
2426: my $ima = Image::Magick->new;
2427: my $resized;
2428: if (-e $img_path) {
2429: $ima->Read($img_path);
2430: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
2431: my $width = $ima->Get('width');
2432: my $height = $ima->Get('height');
2433: if ($width > $resizewidth) {
2434: my $factor = $width/$resizewidth;
2435: my $newheight = $height/$factor;
2436: $ima->Scale(width=>$resizewidth,height=>$newheight);
2437: $resized = 1;
2438: }
2439: }
2440: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
2441: my $width = $ima->Get('width');
2442: my $height = $ima->Get('height');
2443: if ($height > $resizeheight) {
2444: my $factor = $height/$resizeheight;
2445: my $newwidth = $width/$factor;
2446: $ima->Scale(width=>$newwidth,height=>$resizeheight);
2447: $resized = 1;
2448: }
2449: }
2450: if ($resized) {
2451: $ima->Write($img_path);
2452: }
2453: }
2454: return;
1.977 amueller 2455: }
2456:
1.608 albertel 2457: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 2458: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1056.4.13 raeburn 2459: # the desired filename is in $env{"form.$formname.filename"}
2460: # $context - possible values: coursedoc, existingfile, overwrite,
2461: # canceloverwrite, or ''.
2462: # if 'coursedoc': upload to the current course
2463: # if 'existingfile': write file to tmp/overwrites directory
2464: # if 'canceloverwrite': delete file written to tmp/overwrites directory
2465: # $context is passed as argument to &finishuserfileupload
1.686 albertel 2466: # $subdir - directory in userfile to store the file into
1.858 raeburn 2467: # $parser - instruction to parse file for objects ($parser = parse)
2468: # $allfiles - reference to hash for embedded objects
2469: # $codebase - reference to hash for codebase of java objects
2470: # $desuname - username for permanent storage of uploaded file
2471: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 2472: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
2473: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 2474: # $resizewidth - width (pixels) to which to resize uploaded image
2475: # $resizeheight - height (pixels) to which to resize uploaded image
1.1056.4.16 raeburn 2476: # $mimetype - reference to scalar to accommodate mime type determined
2477: # from File::MMagic if $parser = parse.
1.858 raeburn 2478: #
1.686 albertel 2479: # output: url of file in userspace, or error: <message>
2480: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 2481:
1.531 albertel 2482: sub userfileupload {
1.1056.4.13 raeburn 2483: my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1056.4.16 raeburn 2484: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531 albertel 2485: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 2486: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 2487: $fname=&clean_filename($fname);
1.1056.4.13 raeburn 2488: # See if there is anything left
1.257 www 2489: unless ($fname) { return 'error: no uploaded file'; }
1.1056.4.13 raeburn 2490: # Files uploaded to help request form, or uploaded to "create course" page are handled differently
2491: if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
2492: (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
2493: ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523 raeburn 2494: my $now = time;
1.1056.4.13 raeburn 2495: my $filepath;
2496: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
2497: $filepath = 'tmp/helprequests/'.$now;
2498: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
2499: $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
2500: '_'.$env{'user.domain'}.'/pending';
2501: } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
2502: my ($docuname,$docudom);
2503: if ($destudom) {
2504: $docudom = $destudom;
2505: } else {
2506: $docudom = $env{'user.domain'};
2507: }
2508: if ($destuname) {
2509: $docuname = $destuname;
2510: } else {
2511: $docuname = $env{'user.name'};
2512: }
2513: if (exists($env{'form.group'})) {
2514: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2515: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2516: }
2517: $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
2518: if ($context eq 'canceloverwrite') {
2519: my $tempfile = $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
2520: if (-e $tempfile) {
2521: my @info = stat($tempfile);
2522: if ($info[9] eq $env{'form.timestamp'}) {
2523: unlink($tempfile);
2524: }
2525: }
2526: return;
1.523 raeburn 2527: }
2528: }
1.1056.4.13 raeburn 2529: # Create the directory if not present
1.741 raeburn 2530: my @parts=split(/\//,$filepath);
2531: my $fullpath = $perlvar{'lonDaemons'};
2532: for (my $i=0;$i<@parts;$i++) {
2533: $fullpath .= '/'.$parts[$i];
2534: if ((-e $fullpath)!=1) {
2535: mkdir($fullpath,0777);
2536: }
2537: }
2538: open(my $fh,'>'.$fullpath.'/'.$fname);
2539: print $fh $env{'form.'.$formname};
2540: close($fh);
1.1056.4.13 raeburn 2541: if ($context eq 'existingfile') {
2542: my @info = stat($fullpath.'/'.$fname);
2543: return ($fullpath.'/'.$fname,$info[9]);
2544: } else {
2545: return $fullpath.'/'.$fname;
2546: }
1.523 raeburn 2547: }
1.995 raeburn 2548: if ($subdir eq 'scantron') {
2549: $fname = 'scantron_orig_'.$fname;
1.1056.4.14 raeburn 2550: } else {
1.995 raeburn 2551: $fname="$subdir/$fname";
2552: }
1.1056.4.13 raeburn 2553: if ($context eq 'coursedoc') {
1.638 albertel 2554: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2555: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 2556: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 2557: return &finishuserfileupload($docuname,$docudom,
2558: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 2559: $codebase,$thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2560: $resizewidth,$resizeheight,$context,$mimetype);
1.481 raeburn 2561: } else {
1.620 albertel 2562: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 2563: return &process_coursefile('uploaddoc',$docuname,$docudom,
2564: $fname,$formname,$parser,
1.1056.4.16 raeburn 2565: $allfiles,$codebase,$mimetype);
1.481 raeburn 2566: }
1.719 banghart 2567: } elsif (defined($destuname)) {
2568: my $docuname=$destuname;
2569: my $docudom=$destudom;
1.860 raeburn 2570: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2571: $parser,$allfiles,$codebase,
1.1051 raeburn 2572: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2573: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2574: } else {
1.638 albertel 2575: my $docuname=$env{'user.name'};
2576: my $docudom=$env{'user.domain'};
1.714 raeburn 2577: if (exists($env{'form.group'})) {
2578: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2579: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2580: }
1.860 raeburn 2581: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2582: $parser,$allfiles,$codebase,
1.1051 raeburn 2583: $thumbwidth,$thumbheight,
1.1056.4.16 raeburn 2584: $resizewidth,$resizeheight,$context,$mimetype);
1.259 www 2585: }
1.271 www 2586: }
2587:
2588: sub finishuserfileupload {
1.860 raeburn 2589: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1056.4.16 raeburn 2590: $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477 raeburn 2591: my $path=$docudom.'/'.$docuname.'/';
1.258 www 2592: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 2593:
1.860 raeburn 2594: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 2595: $file=$fname;
2596: if ($fname=~m|/|) {
2597: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
2598: $path.=$fnamepath.'/';
2599: }
1.259 www 2600: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 2601: my $count;
2602: for ($count=4;$count<=$#parts;$count++) {
2603: $filepath.="/$parts[$count]";
2604: if ((-e $filepath)!=1) {
2605: mkdir($filepath,0777);
2606: }
2607: }
1.984 neumanie 2608:
1.258 www 2609: # Save the file
2610: {
1.701 albertel 2611: if (!open(FH,'>'.$filepath.'/'.$file)) {
2612: &logthis('Failed to create '.$filepath.'/'.$file);
2613: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
2614: return '/adm/notfound.html';
2615: }
1.1056.4.13 raeburn 2616: if ($context eq 'overwrite') {
2617: my $source = $perlvar{'lonDaemons'}.'/tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
2618: my $target = $filepath.'/'.$file;
2619: if (-e $source) {
2620: my @info = stat($source);
2621: if ($info[9] eq $env{'form.timestamp'}) {
2622: unless (&File::Copy::move($source,$target)) {
2623: &logthis('Failed to overwrite '.$filepath.'/'.$file);
2624: return "Moving from $source failed";
2625: }
2626: } else {
2627: return "Temporary file: $source had unexpected date/time for last modification";
2628: }
2629: } else {
2630: return "Temporary file: $source missing";
2631: }
2632: } elsif (!print FH ($env{'form.'.$formname})) {
1.701 albertel 2633: &logthis('Failed to write to '.$filepath.'/'.$file);
2634: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
2635: return '/adm/notfound.html';
2636: }
1.570 albertel 2637: close(FH);
1.1051 raeburn 2638: if ($resizewidth && $resizeheight) {
2639: my $mm = new File::MMagic;
2640: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
2641: if ($mime_type =~ m{^image/}) {
2642: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
2643: }
1.977 amueller 2644: }
1.258 www 2645: }
1.637 raeburn 2646: if ($parser eq 'parse') {
1.1024 raeburn 2647: my $mm = new File::MMagic;
1.1056.4.16 raeburn 2648: my $type = $mm->checktype_filename($filepath.'/'.$file);
2649: if ($type eq 'text/html') {
1.1024 raeburn 2650: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
2651: $allfiles,$codebase);
2652: unless ($parse_result eq 'ok') {
2653: &logthis('Failed to parse '.$filepath.$file.
2654: ' for embedded media: '.$parse_result);
2655: }
1.637 raeburn 2656: }
1.1056.4.16 raeburn 2657: if (ref($mimetype)) {
2658: $$mimetype = $type;
2659: }
1.637 raeburn 2660: }
1.860 raeburn 2661: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
2662: my $input = $filepath.'/'.$file;
2663: my $output = $filepath.'/'.'tn-'.$file;
2664: my $thumbsize = $thumbwidth.'x'.$thumbheight;
2665: system("convert -sample $thumbsize $input $output");
2666: if (-e $filepath.'/'.'tn-'.$file) {
2667: $fetchthumb = 1;
2668: }
2669: }
1.858 raeburn 2670:
1.259 www 2671: # Notify homeserver to grep it
2672: #
1.984 neumanie 2673: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 2674: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 2675: if ($fetchresult eq 'ok') {
1.860 raeburn 2676: if ($fetchthumb) {
2677: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
2678: if ($thumbresult ne 'ok') {
2679: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
2680: $docuhome.': '.$thumbresult);
2681: }
2682: }
1.259 www 2683: #
1.258 www 2684: # Return the URL to it
1.494 albertel 2685: return '/uploaded/'.$path.$file;
1.263 www 2686: } else {
1.494 albertel 2687: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
2688: ': '.$fetchresult);
1.263 www 2689: return '/adm/notfound.html';
1.858 raeburn 2690: }
1.493 albertel 2691: }
2692:
1.637 raeburn 2693: sub extract_embedded_items {
1.961 raeburn 2694: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 2695: my @state = ();
2696: my %javafiles = (
2697: codebase => '',
2698: code => '',
2699: archive => ''
2700: );
2701: my %mediafiles = (
2702: src => '',
2703: movie => '',
2704: );
1.648 raeburn 2705: my $p;
2706: if ($content) {
2707: $p = HTML::LCParser->new($content);
2708: } else {
1.961 raeburn 2709: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 2710: }
1.641 albertel 2711: while (my $t=$p->get_token()) {
1.640 albertel 2712: if ($t->[0] eq 'S') {
2713: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 2714: push(@state, $tagname);
1.648 raeburn 2715: if (lc($tagname) eq 'allow') {
2716: &add_filetype($allfiles,$attr->{'src'},'src');
2717: }
1.640 albertel 2718: if (lc($tagname) eq 'img') {
2719: &add_filetype($allfiles,$attr->{'src'},'src');
2720: }
1.886 albertel 2721: if (lc($tagname) eq 'a') {
2722: &add_filetype($allfiles,$attr->{'href'},'href');
2723: }
1.645 raeburn 2724: if (lc($tagname) eq 'script') {
2725: if ($attr->{'archive'} =~ /\.jar$/i) {
2726: &add_filetype($allfiles,$attr->{'archive'},'archive');
2727: } else {
2728: &add_filetype($allfiles,$attr->{'src'},'src');
2729: }
2730: }
2731: if (lc($tagname) eq 'link') {
2732: if (lc($attr->{'rel'}) eq 'stylesheet') {
2733: &add_filetype($allfiles,$attr->{'href'},'href');
2734: }
2735: }
1.640 albertel 2736: if (lc($tagname) eq 'object' ||
2737: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
2738: foreach my $item (keys(%javafiles)) {
2739: $javafiles{$item} = '';
2740: }
2741: }
2742: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
2743: my $name = lc($attr->{'name'});
2744: foreach my $item (keys(%javafiles)) {
2745: if ($name eq $item) {
2746: $javafiles{$item} = $attr->{'value'};
2747: last;
2748: }
2749: }
2750: foreach my $item (keys(%mediafiles)) {
2751: if ($name eq $item) {
2752: &add_filetype($allfiles, $attr->{'value'}, 'value');
2753: last;
2754: }
2755: }
2756: }
2757: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
2758: foreach my $item (keys(%javafiles)) {
2759: if ($attr->{$item}) {
2760: $javafiles{$item} = $attr->{$item};
2761: last;
2762: }
2763: }
2764: foreach my $item (keys(%mediafiles)) {
2765: if ($attr->{$item}) {
2766: &add_filetype($allfiles,$attr->{$item},$item);
2767: last;
2768: }
2769: }
2770: }
2771: } elsif ($t->[0] eq 'E') {
2772: my ($tagname) = ($t->[1]);
2773: if ($javafiles{'codebase'} ne '') {
2774: $javafiles{'codebase'} .= '/';
2775: }
2776: if (lc($tagname) eq 'applet' ||
2777: lc($tagname) eq 'object' ||
2778: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
2779: ) {
2780: foreach my $item (keys(%javafiles)) {
2781: if ($item ne 'codebase' && $javafiles{$item} ne '') {
2782: my $file=$javafiles{'codebase'}.$javafiles{$item};
2783: &add_filetype($allfiles,$file,$item);
2784: }
2785: }
2786: }
2787: pop @state;
2788: }
2789: }
1.637 raeburn 2790: return 'ok';
2791: }
2792:
1.639 albertel 2793: sub add_filetype {
2794: my ($allfiles,$file,$type)=@_;
2795: if (exists($allfiles->{$file})) {
2796: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
2797: push(@{$allfiles->{$file}}, &escape($type));
2798: }
2799: } else {
2800: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 2801: }
2802: }
2803:
1.493 albertel 2804: sub removeuploadedurl {
1.984 neumanie 2805: my ($url)=@_;
2806: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 2807: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 2808: }
2809:
2810: sub removeuserfile {
2811: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 2812: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2813: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 2814: if ($result eq 'ok') {
1.798 raeburn 2815: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
2816: my $metafile = $fname.'.meta';
2817: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 2818: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 2819: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2820: my $sqlresult =
1.823 albertel 2821: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2822: 'portfolio_metadata',$group,
2823: 'delete');
1.798 raeburn 2824: }
2825: }
2826: return $result;
1.257 www 2827: }
1.15 www 2828:
1.530 albertel 2829: sub mkdiruserfile {
2830: my ($docuname,$docudom,$dir)=@_;
2831: my $home=&homeserver($docuname,$docudom);
2832: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
2833: }
2834:
1.531 albertel 2835: sub renameuserfile {
2836: my ($docuname,$docudom,$old,$new)=@_;
2837: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2838: my $result = &reply("renameuserfile:$docudom:$docuname:".
2839: &escape("$old").':'.&escape("$new"),$home);
2840: if ($result eq 'ok') {
2841: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
2842: my $oldmeta = $old.'.meta';
2843: my $newmeta = $new.'.meta';
2844: my $metaresult =
2845: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 2846: my $url = "/uploaded/$docudom/$docuname/$old";
2847: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2848: my $sqlresult =
1.823 albertel 2849: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2850: 'portfolio_metadata',$group,
2851: 'delete');
1.798 raeburn 2852: }
2853: }
2854: return $result;
1.531 albertel 2855: }
2856:
1.14 www 2857: # ------------------------------------------------------------------------- Log
2858:
2859: sub log {
2860: my ($dom,$nam,$hom,$what)=@_;
1.47 www 2861: return critical("log:$dom:$nam:$what",$hom);
1.157 www 2862: }
2863:
2864: # ------------------------------------------------------------------ Course Log
1.352 www 2865: #
2866: # This routine flushes several buffers of non-mission-critical nature
2867: #
1.157 www 2868:
2869: sub flushcourselogs {
1.352 www 2870: &logthis('Flushing log buffers');
2871: #
2872: # course logs
2873: # This is a log of all transactions in a course, which can be used
2874: # for data mining purposes
2875: #
2876: # It also collects the courseid database, which lists last transaction
2877: # times and course titles for all courseids
2878: #
2879: my %courseidbuffer=();
1.921 raeburn 2880: foreach my $crsid (keys(%courselogs)) {
1.352 www 2881: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 2882: &escape($courselogs{$crsid}),
2883: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 2884: delete $courselogs{$crsid};
2885: } else {
2886: &logthis('Failed to flush log buffer for '.$crsid);
2887: if (length($courselogs{$crsid})>40000) {
1.672 albertel 2888: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 2889: " exceeded maximum size, deleting.</font>");
2890: delete $courselogs{$crsid};
2891: }
1.352 www 2892: }
1.920 raeburn 2893: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 2894: 'description' => $coursedescrbuf{$crsid},
2895: 'inst_code' => $courseinstcodebuf{$crsid},
2896: 'type' => $coursetypebuf{$crsid},
2897: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 2898: };
1.191 harris41 2899: }
1.352 www 2900: #
2901: # Write course id database (reverse lookup) to homeserver of courses
2902: # Is used in pickcourse
2903: #
1.840 albertel 2904: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 2905: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 2906: $courseidbuffer{$crs_home},
2907: $crs_home,'timeonly');
1.352 www 2908: }
2909: #
2910: # File accesses
2911: # Writes to the dynamic metadata of resources to get hit counts, etc.
2912: #
1.449 matthew 2913: foreach my $entry (keys(%accesshash)) {
1.458 matthew 2914: if ($entry =~ /___count$/) {
2915: my ($dom,$name);
1.807 albertel 2916: ($dom,$name,undef)=
1.811 albertel 2917: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 2918: if (! defined($dom) || $dom eq '' ||
2919: ! defined($name) || $name eq '') {
1.620 albertel 2920: my $cid = $env{'request.course.id'};
2921: $dom = $env{'request.'.$cid.'.domain'};
2922: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 2923: }
1.450 matthew 2924: my $value = $accesshash{$entry};
2925: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
2926: my %temphash=($url => $value);
1.449 matthew 2927: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
2928: if ($result eq 'ok') {
2929: delete $accesshash{$entry};
2930: } elsif ($result eq 'unknown_cmd') {
2931: # Target server has old code running on it.
1.450 matthew 2932: my %temphash=($entry => $value);
1.449 matthew 2933: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2934: delete $accesshash{$entry};
2935: }
2936: }
2937: } else {
1.811 albertel 2938: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 2939: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 2940: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2941: delete $accesshash{$entry};
2942: }
1.185 www 2943: }
1.191 harris41 2944: }
1.352 www 2945: #
2946: # Roles
2947: # Reverse lookup of user roles for course faculty/staff and co-authorship
2948: #
1.800 albertel 2949: foreach my $entry (keys(%userrolehash)) {
1.351 www 2950: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 2951: split(/\:/,$entry);
2952: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 2953: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 2954: $rudom,$runame) eq 'ok') {
2955: delete $userrolehash{$entry};
2956: }
2957: }
1.662 raeburn 2958: #
2959: # Reverse lookup of domain roles (dc, ad, li, sc, au)
2960: #
2961: my %domrolebuffer = ();
1.1000 raeburn 2962: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 2963: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 2964: if ($domrolebuffer{$rudom}) {
2965: $domrolebuffer{$rudom}.='&'.&escape($entry).
2966: '='.&escape($domainrolehash{$entry});
2967: } else {
2968: $domrolebuffer{$rudom}.=&escape($entry).
2969: '='.&escape($domainrolehash{$entry});
2970: }
2971: delete $domainrolehash{$entry};
2972: }
2973: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 2974: my %servers = &get_servers($dom,'library');
2975: foreach my $tryserver (keys(%servers)) {
2976: unless (&reply('domroleput:'.$dom.':'.
2977: $domrolebuffer{$dom},$tryserver) eq 'ok') {
2978: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
2979: }
1.662 raeburn 2980: }
2981: }
1.186 www 2982: $dumpcount++;
1.157 www 2983: }
2984:
2985: sub courselog {
2986: my $what=shift;
1.158 www 2987: $what=time.':'.$what;
1.620 albertel 2988: unless ($env{'request.course.id'}) { return ''; }
2989: $coursedombuf{$env{'request.course.id'}}=
2990: $env{'course.'.$env{'request.course.id'}.'.domain'};
2991: $coursenumbuf{$env{'request.course.id'}}=
2992: $env{'course.'.$env{'request.course.id'}.'.num'};
2993: $coursehombuf{$env{'request.course.id'}}=
2994: $env{'course.'.$env{'request.course.id'}.'.home'};
2995: $coursedescrbuf{$env{'request.course.id'}}=
2996: $env{'course.'.$env{'request.course.id'}.'.description'};
2997: $courseinstcodebuf{$env{'request.course.id'}}=
2998: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2999: $courseownerbuf{$env{'request.course.id'}}=
3000: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 3001: $coursetypebuf{$env{'request.course.id'}}=
3002: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 3003: if (defined $courselogs{$env{'request.course.id'}}) {
3004: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 3005: } else {
1.620 albertel 3006: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 3007: }
1.620 albertel 3008: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 3009: &flushcourselogs();
3010: }
1.158 www 3011: }
3012:
3013: sub courseacclog {
3014: my $fnsymb=shift;
1.620 albertel 3015: unless ($env{'request.course.id'}) { return ''; }
3016: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 3017: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 3018: $what.=':POST';
1.583 matthew 3019: # FIXME: Probably ought to escape things....
1.800 albertel 3020: foreach my $key (keys(%env)) {
3021: if ($key=~/^form\.(.*)/) {
1.975 raeburn 3022: my $formitem = $1;
3023: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
3024: $what.=':'.$formitem.'='.$env{$key};
3025: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
3026: $what.=':'.$formitem.'='.$env{$key};
3027: }
1.158 www 3028: }
1.191 harris41 3029: }
1.583 matthew 3030: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
3031: # FIXME: We should not be depending on a form parameter that someone
3032: # editing lonsearchcat.pm might change in the future.
1.620 albertel 3033: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 3034: $what.= ':POST';
3035: # FIXME: Probably ought to escape things....
3036: foreach my $element ('courseexp','crsfulltext','crsrelated',
3037: 'crsdiscuss') {
1.620 albertel 3038: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 3039: }
3040: }
1.158 www 3041: }
3042: &courselog($what);
1.149 www 3043: }
3044:
1.185 www 3045: sub countacc {
3046: my $url=&declutter(shift);
1.458 matthew 3047: return if (! defined($url) || $url eq '');
1.620 albertel 3048: unless ($env{'request.course.id'}) { return ''; }
3049: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 3050: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 3051: $accesshash{$key}++;
1.185 www 3052: }
1.349 www 3053:
1.361 www 3054: sub linklog {
3055: my ($from,$to)=@_;
3056: $from=&declutter($from);
3057: $to=&declutter($to);
3058: $accesshash{$from.'___'.$to.'___comefrom'}=1;
3059: $accesshash{$to.'___'.$from.'___goto'}=1;
3060: }
3061:
1.349 www 3062: sub userrolelog {
3063: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 3064: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 3065: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 3066: ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041 raeburn 3067: ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350 www 3068: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3069: $userrolehash
3070: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 3071: =$tend.':'.$tstart;
1.662 raeburn 3072: }
1.898 albertel 3073: if (($env{'request.role'} =~ /dc\./) &&
3074: (($trole=~/^au/) || ($trole=~/^in/) ||
3075: ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041 raeburn 3076: ($trole=~/^cr/) || ($trole=~/^ta/) ||
3077: ($trole=~/^co/))) {
1.898 albertel 3078: $userrolehash
3079: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
3080: =$tend.':'.$tstart;
3081: }
1.662 raeburn 3082: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
3083: ($trole=~/^li/) || ($trole=~/^li/) ||
3084: ($trole=~/^au/) || ($trole=~/^dg/) ||
3085: ($trole=~/^sc/)) {
3086: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
3087: $domainrolehash
3088: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
3089: = $tend.':'.$tstart;
3090: }
1.351 www 3091: }
3092:
1.957 raeburn 3093: sub courserolelog {
3094: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
3095: if (($trole eq 'cc') || ($trole eq 'in') ||
3096: ($trole eq 'ep') || ($trole eq 'ad') ||
3097: ($trole eq 'ta') || ($trole eq 'st') ||
1.1044 raeburn 3098: ($trole=~/^cr/) || ($trole eq 'gr') ||
3099: ($trole eq 'co')) {
1.957 raeburn 3100: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
3101: my $cdom = $1;
3102: my $cnum = $2;
3103: my $sec = $3;
3104: my $namespace = 'rolelog';
3105: my %storehash = (
3106: role => $trole,
3107: start => $tstart,
3108: end => $tend,
3109: selfenroll => $selfenroll,
3110: context => $context,
3111: );
3112: if ($trole eq 'gr') {
3113: $namespace = 'groupslog';
3114: $storehash{'group'} = $sec;
3115: } else {
3116: $storehash{'section'} = $sec;
3117: }
3118: &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996 raeburn 3119: if (($trole ne 'st') || ($sec ne '')) {
3120: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
3121: }
1.957 raeburn 3122: }
3123: }
3124: return;
3125: }
3126:
1.351 www 3127: sub get_course_adv_roles {
1.948 raeburn 3128: my ($cid,$codes) = @_;
1.620 albertel 3129: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 3130: my %coursehash=&coursedescription($cid);
1.988 raeburn 3131: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 3132: my %nothide=();
1.800 albertel 3133: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 3134: if ($user !~ /:/) {
3135: $nothide{join(':',split(/[\@]/,$user))}=1;
3136: } else {
3137: $nothide{$user}=1;
3138: }
1.470 www 3139: }
1.351 www 3140: my %returnhash=();
3141: my %dumphash=
3142: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
3143: my $now=time;
1.997 raeburn 3144: my %privileged;
1.1000 raeburn 3145: foreach my $entry (keys(%dumphash)) {
1.800 albertel 3146: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 3147: if (($tstart) && ($tstart<0)) { next; }
3148: if (($tend) && ($tend<$now)) { next; }
3149: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 3150: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 3151: if ($username eq '' || $domain eq '') { next; }
1.997 raeburn 3152: unless (ref($privileged{$domain}) eq 'HASH') {
3153: my %dompersonnel =
1.998 raeburn 3154: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997 raeburn 3155: $privileged{$domain} = {};
3156: foreach my $server (keys(%dompersonnel)) {
1.999 raeburn 3157: if (ref($dompersonnel{$server}) eq 'HASH') {
1.997 raeburn 3158: foreach my $user (keys(%{$dompersonnel{$server}})) {
3159: my ($trole,$uname,$udom) = split(/:/,$user);
3160: $privileged{$udom}{$uname} = 1;
3161: }
3162: }
3163: }
3164: }
3165: if ((exists($privileged{$domain}{$username})) &&
3166: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 3167: if ($role eq 'cr') { next; }
1.948 raeburn 3168: if ($codes) {
3169: if ($section) { $role .= ':'.$section; }
3170: if ($returnhash{$role}) {
3171: $returnhash{$role}.=','.$username.':'.$domain;
3172: } else {
3173: $returnhash{$role}=$username.':'.$domain;
3174: }
1.351 www 3175: } else {
1.988 raeburn 3176: my $key=&plaintext($role,$crstype);
1.973 bisitz 3177: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 3178: if ($returnhash{$key}) {
3179: $returnhash{$key}.=','.$username.':'.$domain;
3180: } else {
3181: $returnhash{$key}=$username.':'.$domain;
3182: }
1.351 www 3183: }
1.948 raeburn 3184: }
1.400 www 3185: return %returnhash;
3186: }
3187:
3188: sub get_my_roles {
1.937 raeburn 3189: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 3190: unless (defined($uname)) { $uname=$env{'user.name'}; }
3191: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 3192: my (%dumphash,%nothide);
1.858 raeburn 3193: if ($context eq 'userroles') {
1.1056.4.10 raeburn 3194: my $extra = &freeze_escape({'skipcheck' => 1});
3195: %dumphash = &dump('roles',$udom,$uname,'.',undef,$extra);
1.858 raeburn 3196: } else {
3197: %dumphash=
1.400 www 3198: &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 3199: if ($hidepriv) {
3200: my %coursehash=&coursedescription($udom.'_'.$uname);
3201: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
3202: if ($user !~ /:/) {
3203: $nothide{join(':',split(/[\@]/,$user))} = 1;
3204: } else {
3205: $nothide{$user} = 1;
3206: }
3207: }
3208: }
1.858 raeburn 3209: }
1.400 www 3210: my %returnhash=();
3211: my $now=time;
1.999 raeburn 3212: my %privileged;
1.800 albertel 3213: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 3214: my ($role,$tend,$tstart);
3215: if ($context eq 'userroles') {
3216: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
3217: } else {
3218: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
3219: }
1.400 www 3220: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 3221: my $status = 'active';
1.939 raeburn 3222: if (($tend) && ($tend<=$now)) {
1.832 raeburn 3223: $status = 'previous';
3224: }
3225: if (($tstart) && ($now<$tstart)) {
3226: $status = 'future';
3227: }
3228: if (ref($types) eq 'ARRAY') {
3229: if (!grep(/^\Q$status\E$/,@{$types})) {
3230: next;
3231: }
3232: } else {
3233: if ($status ne 'active') {
3234: next;
3235: }
3236: }
1.867 raeburn 3237: my ($rolecode,$username,$domain,$section,$area);
3238: if ($context eq 'userroles') {
3239: ($area,$rolecode) = split(/_/,$entry);
3240: (undef,$domain,$username,$section) = split(/\//,$area);
3241: } else {
3242: ($role,$username,$domain,$section) = split(/\:/,$entry);
3243: }
1.832 raeburn 3244: if (ref($roledoms) eq 'ARRAY') {
3245: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
3246: next;
3247: }
3248: }
3249: if (ref($roles) eq 'ARRAY') {
3250: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 3251: if ($role =~ /^cr\//) {
3252: if (!grep(/^cr$/,@{$roles})) {
3253: next;
3254: }
3255: } else {
3256: next;
3257: }
1.832 raeburn 3258: }
1.867 raeburn 3259: }
1.937 raeburn 3260: if ($hidepriv) {
1.999 raeburn 3261: if ($context eq 'userroles') {
3262: if ((&privileged($username,$domain)) &&
3263: (!$nothide{$username.':'.$domain})) {
3264: next;
3265: }
3266: } else {
3267: unless (ref($privileged{$domain}) eq 'HASH') {
3268: my %dompersonnel =
3269: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
3270: $privileged{$domain} = {};
3271: if (keys(%dompersonnel)) {
3272: foreach my $server (keys(%dompersonnel)) {
3273: if (ref($dompersonnel{$server}) eq 'HASH') {
3274: foreach my $user (keys(%{$dompersonnel{$server}})) {
3275: my ($trole,$uname,$udom) = split(/:/,$user);
3276: $privileged{$udom}{$uname} = $trole;
3277: }
3278: }
3279: }
3280: }
3281: }
3282: if (exists($privileged{$domain}{$username})) {
3283: if (!$nothide{$username.':'.$domain}) {
3284: next;
3285: }
3286: }
1.937 raeburn 3287: }
3288: }
1.933 raeburn 3289: if ($withsec) {
3290: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
3291: $tstart.':'.$tend;
3292: } else {
3293: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
3294: }
1.832 raeburn 3295: }
1.373 www 3296: return %returnhash;
1.399 www 3297: }
3298:
3299: # ----------------------------------------------------- Frontpage Announcements
3300: #
3301: #
3302:
3303: sub postannounce {
3304: my ($server,$text)=@_;
1.844 albertel 3305: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 3306: unless ($text=~/\w/) { $text=''; }
3307: return &reply('setannounce:'.&escape($text),$server);
3308: }
3309:
3310: sub getannounce {
1.448 albertel 3311:
3312: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 3313: my $announcement='';
1.800 albertel 3314: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 3315: close($fh);
1.399 www 3316: if ($announcement=~/\w/) {
3317: return
3318: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 3319: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 3320: } else {
3321: return '';
3322: }
3323: } else {
3324: return '';
3325: }
1.351 www 3326: }
1.353 www 3327:
3328: # ---------------------------------------------------------- Course ID routines
3329: # Deal with domain's nohist_courseid.db files
3330: #
3331:
3332: sub courseidput {
1.921 raeburn 3333: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 3334: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 3335: my $outcome;
3336: if ($caller eq 'timeonly') {
3337: my $cids = '';
3338: foreach my $item (keys(%$storehash)) {
3339: $cids.=&escape($item).'&';
3340: }
3341: $cids=~s/\&$//;
3342: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
3343: $coursehome);
3344: } else {
3345: my $items = '';
3346: foreach my $item (keys(%$storehash)) {
3347: $items.= &escape($item).'='.
3348: &freeze_escape($$storehash{$item}).'&';
3349: }
3350: $items=~s/\&$//;
3351: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
3352: $coursehome);
1.918 raeburn 3353: }
3354: if ($outcome eq 'unknown_cmd') {
3355: my $what;
3356: foreach my $cid (keys(%$storehash)) {
3357: $what .= &escape($cid).'=';
1.921 raeburn 3358: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 3359: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 3360: }
3361: $what =~ s/\:$/&/;
3362: }
3363: $what =~ s/\&$//;
3364: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
3365: } else {
3366: return $outcome;
3367: }
1.353 www 3368: }
3369:
3370: sub courseiddump {
1.921 raeburn 3371: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 3372: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 3373: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1056.4.3 raeburn 3374: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918 raeburn 3375: my $as_hash = 1;
3376: my %returnhash;
3377: if (!$domfilter) { $domfilter=''; }
1.845 albertel 3378: my %libserv = &all_library();
3379: foreach my $tryserver (keys(%libserv)) {
3380: if ( ( $hostidflag == 1
3381: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
3382: || (!defined($hostidflag)) ) {
3383:
1.918 raeburn 3384: if (($domfilter eq '') ||
3385: (&host_domain($tryserver) eq $domfilter)) {
3386: my $rep =
3387: &reply('courseiddump:'.&host_domain($tryserver).':'.
3388: $sincefilter.':'.&escape($descfilter).':'.
3389: &escape($instcodefilter).':'.&escape($ownerfilter).
3390: ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947 raeburn 3391: ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962 raeburn 3392: &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008 raeburn 3393: $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029 raeburn 3394: &escape($cc_clone).':'.$cloneonly.':'.
3395: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1056.4.3 raeburn 3396: &escape($creationcontext).':'.$domcloner,
3397: $tryserver);
1.918 raeburn 3398: my @pairs=split(/\&/,$rep);
3399: foreach my $item (@pairs) {
3400: my ($key,$value)=split(/\=/,$item,2);
3401: $key = &unescape($key);
3402: next if ($key =~ /^error: 2 /);
3403: my $result = &thaw_unescape($value);
3404: if (ref($result) eq 'HASH') {
3405: $returnhash{$key}=$result;
3406: } else {
1.921 raeburn 3407: my @responses = split(/:/,$value);
3408: my @items = ('description','inst_code','owner','type');
1.918 raeburn 3409: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 3410: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 3411: }
1.1008 raeburn 3412: }
1.353 www 3413: }
3414: }
3415: }
3416: }
3417: return %returnhash;
3418: }
3419:
1.1055 raeburn 3420: sub courselastaccess {
3421: my ($cdom,$cnum,$hostidref) = @_;
3422: my %returnhash;
3423: if ($cdom && $cnum) {
3424: my $chome = &homeserver($cnum,$cdom);
3425: if ($chome ne 'no_host') {
3426: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
3427: &extract_lastaccess(\%returnhash,$rep);
3428: }
3429: } else {
3430: if (!$cdom) { $cdom=''; }
3431: my %libserv = &all_library();
3432: foreach my $tryserver (keys(%libserv)) {
3433: if (ref($hostidref) eq 'ARRAY') {
3434: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
3435: }
3436: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
3437: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
3438: &extract_lastaccess(\%returnhash,$rep);
3439: }
3440: }
3441: }
3442: return %returnhash;
3443: }
3444:
3445: sub extract_lastaccess {
3446: my ($returnhash,$rep) = @_;
3447: if (ref($returnhash) eq 'HASH') {
3448: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
3449: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
3450: $rep eq '') {
3451: my @pairs=split(/\&/,$rep);
3452: foreach my $item (@pairs) {
3453: my ($key,$value)=split(/\=/,$item,2);
3454: $key = &unescape($key);
3455: next if ($key =~ /^error: 2 /);
3456: $returnhash->{$key} = &thaw_unescape($value);
3457: }
3458: }
3459: }
3460: return;
3461: }
3462:
1.658 raeburn 3463: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 3464:
3465: sub dcmailput {
1.685 raeburn 3466: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 3467: my $status = &Apache::lonnet::critical(
1.740 www 3468: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
3469: &escape($message),$server);
1.662 raeburn 3470: return $status;
3471: }
3472:
1.658 raeburn 3473: sub dcmaildump {
3474: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 3475: my %returnhash=();
1.846 albertel 3476:
3477: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 3478: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
3479: &escape($enddate).':';
3480: my @esc_senders=map { &escape($_)} @$senders;
3481: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 3482: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 3483: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 3484: if (($key) && ($value)) {
3485: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 3486: }
3487: }
3488: }
3489: return %returnhash;
3490: }
1.662 raeburn 3491: # ---------------------------------------------------------- Domain roles
3492:
3493: sub get_domain_roles {
3494: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 3495: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 3496: $startdate = '.';
3497: }
1.1018 raeburn 3498: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 3499: $enddate = '.';
3500: }
1.922 raeburn 3501: my $rolelist;
3502: if (ref($roles) eq 'ARRAY') {
3503: $rolelist = join(':',@{$roles});
3504: }
1.662 raeburn 3505: my %personnel = ();
1.841 albertel 3506:
3507: my %servers = &get_servers($dom,'library');
3508: foreach my $tryserver (keys(%servers)) {
3509: %{$personnel{$tryserver}}=();
3510: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
3511: &escape($startdate).':'.
3512: &escape($enddate).':'.
3513: &escape($rolelist), $tryserver))) {
3514: my ($key,$value) = split(/\=/,$line,2);
3515: if (($key) && ($value)) {
3516: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
3517: }
3518: }
1.662 raeburn 3519: }
3520: return %personnel;
3521: }
1.658 raeburn 3522:
1.149 www 3523: # ----------------------------------------------------------- Check out an item
3524:
1.504 albertel 3525: sub get_first_access {
3526: my ($type,$argsymb)=@_;
1.790 albertel 3527: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3528: if ($argsymb) { $symb=$argsymb; }
3529: my ($map,$id,$res)=&decode_symb($symb);
1.926 albertel 3530: if ($type eq 'course') {
3531: $res='course';
3532: } elsif ($type eq 'map') {
1.588 albertel 3533: $res=&symbread($map);
3534: } else {
3535: $res=$symb;
3536: }
3537: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
3538: return $times{"$courseid\0$res"};
1.504 albertel 3539: }
3540:
3541: sub set_first_access {
3542: my ($type)=@_;
1.790 albertel 3543: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3544: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 3545: if ($type eq 'course') {
3546: $res='course';
3547: } elsif ($type eq 'map') {
1.588 albertel 3548: $res=&symbread($map);
3549: } else {
3550: $res=$symb;
3551: }
3552: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 3553: if (!$firstaccess) {
1.588 albertel 3554: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 3555: }
3556: return 'already_set';
1.504 albertel 3557: }
3558:
1.149 www 3559: sub checkout {
3560: my ($symb,$tuname,$tudom,$tcrsid)=@_;
3561: my $now=time;
3562: my $lonhost=$perlvar{'lonHostID'};
3563: my $infostr=&escape(
1.234 www 3564: 'CHECKOUTTOKEN&'.
1.149 www 3565: $tuname.'&'.
3566: $tudom.'&'.
3567: $tcrsid.'&'.
3568: $symb.'&'.
3569: $now.'&'.$ENV{'REMOTE_ADDR'});
3570: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 3571: if ($token=~/^error\:/) {
1.672 albertel 3572: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3573: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
3574: "</font>");
3575: return '';
3576: }
3577:
1.149 www 3578: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
3579: $token=~tr/a-z/A-Z/;
3580:
1.153 www 3581: my %infohash=('resource.0.outtoken' => $token,
3582: 'resource.0.checkouttime' => $now,
3583: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 3584:
3585: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3586: return '';
1.151 www 3587: } else {
1.672 albertel 3588: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3589: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
3590: "</font>");
1.149 www 3591: }
3592:
3593: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3594: &escape('Checkout '.$infostr.' - '.
3595: $token)) ne 'ok') {
3596: return '';
1.151 www 3597: } else {
1.672 albertel 3598: &logthis("<font color=\"blue\">WARNING: ".
1.151 www 3599: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
3600: "</font>");
1.149 www 3601: }
1.151 www 3602: return $token;
1.149 www 3603: }
3604:
3605: # ------------------------------------------------------------ Check in an item
3606:
3607: sub checkin {
3608: my $token=shift;
1.150 www 3609: my $now=time;
3610: my ($ta,$tb,$lonhost)=split(/\*/,$token);
3611: $lonhost=~tr/A-Z/a-z/;
1.838 albertel 3612: my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150 www 3613: $dtoken=~s/\W/\_/g;
1.234 www 3614: my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150 www 3615: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
3616:
1.154 www 3617: unless (($tuname) && ($tudom)) {
3618: &logthis('Check in '.$token.' ('.$dtoken.') failed');
3619: return '';
3620: }
3621:
3622: unless (&allowed('mgr',$tcrsid)) {
3623: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620 albertel 3624: $env{'user.name'}.' - '.$env{'user.domain'});
1.154 www 3625: return '';
3626: }
3627:
1.153 www 3628: my %infohash=('resource.0.intoken' => $token,
3629: 'resource.0.checkintime' => $now,
3630: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 3631:
3632: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
3633: return '';
3634: }
3635:
3636: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
3637: &escape('Checkin - '.$token)) ne 'ok') {
3638: return '';
3639: }
3640:
3641: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 3642: }
3643:
3644: # --------------------------------------------- Set Expire Date for Spreadsheet
3645:
3646: sub expirespread {
3647: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 3648: my $cid=$env{'request.course.id'};
1.110 www 3649: if ($cid) {
3650: my $now=time;
3651: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 3652: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
3653: $env{'course.'.$cid.'.num'}.
1.110 www 3654: ':nohist_expirationdates:'.
3655: &escape($key).'='.$now,
1.620 albertel 3656: $env{'course.'.$cid.'.home'})
1.110 www 3657: }
3658: return 'ok';
1.14 www 3659: }
3660:
1.109 www 3661: # ----------------------------------------------------- Devalidate Spreadsheets
3662:
3663: sub devalidate {
1.325 www 3664: my ($symb,$uname,$udom)=@_;
1.620 albertel 3665: my $cid=$env{'request.course.id'};
1.109 www 3666: if ($cid) {
1.391 matthew 3667: # delete the stored spreadsheets for
3668: # - the student level sheet of this user in course's homespace
3669: # - the assessment level sheet for this resource
3670: # for this user in user's homespace
1.553 albertel 3671: # - current conditional state info
1.325 www 3672: my $key=$uname.':'.$udom.':';
1.109 www 3673: my $status=
1.299 matthew 3674: &del('nohist_calculatedsheets',
1.391 matthew 3675: [$key.'studentcalc:'],
1.620 albertel 3676: $env{'course.'.$cid.'.domain'},
3677: $env{'course.'.$cid.'.num'})
1.133 albertel 3678: .' '.
3679: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 3680: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 3681: unless ($status eq 'ok ok') {
3682: &logthis('Could not devalidate spreadsheet '.
1.325 www 3683: $uname.' at '.$udom.' for '.
1.109 www 3684: $symb.': '.$status);
1.133 albertel 3685: }
1.553 albertel 3686: &delenv('user.state.'.$cid);
1.109 www 3687: }
3688: }
3689:
1.265 albertel 3690: sub get_scalar {
3691: my ($string,$end) = @_;
3692: my $value;
3693: if ($$string =~ s/^([^&]*?)($end)/$2/) {
3694: $value = $1;
3695: } elsif ($$string =~ s/^([^&]*?)&//) {
3696: $value = $1;
3697: }
3698: return &unescape($value);
3699: }
3700:
3701: sub array2str {
3702: my (@array) = @_;
3703: my $result=&arrayref2str(\@array);
3704: $result=~s/^__ARRAY_REF__//;
3705: $result=~s/__END_ARRAY_REF__$//;
3706: return $result;
3707: }
3708:
1.204 albertel 3709: sub arrayref2str {
3710: my ($arrayref) = @_;
1.265 albertel 3711: my $result='__ARRAY_REF__';
1.204 albertel 3712: foreach my $elem (@$arrayref) {
1.265 albertel 3713: if(ref($elem) eq 'ARRAY') {
3714: $result.=&arrayref2str($elem).'&';
3715: } elsif(ref($elem) eq 'HASH') {
3716: $result.=&hashref2str($elem).'&';
3717: } elsif(ref($elem)) {
3718: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 3719: } else {
3720: $result.=&escape($elem).'&';
3721: }
3722: }
3723: $result=~s/\&$//;
1.265 albertel 3724: $result .= '__END_ARRAY_REF__';
1.204 albertel 3725: return $result;
3726: }
3727:
1.168 albertel 3728: sub hash2str {
1.204 albertel 3729: my (%hash) = @_;
3730: my $result=&hashref2str(\%hash);
1.265 albertel 3731: $result=~s/^__HASH_REF__//;
3732: $result=~s/__END_HASH_REF__$//;
1.204 albertel 3733: return $result;
3734: }
3735:
3736: sub hashref2str {
3737: my ($hashref)=@_;
1.265 albertel 3738: my $result='__HASH_REF__';
1.800 albertel 3739: foreach my $key (sort(keys(%$hashref))) {
3740: if (ref($key) eq 'ARRAY') {
3741: $result.=&arrayref2str($key).'=';
3742: } elsif (ref($key) eq 'HASH') {
3743: $result.=&hashref2str($key).'=';
3744: } elsif (ref($key)) {
1.265 albertel 3745: $result.='=';
1.800 albertel 3746: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 3747: } else {
1.1056.4.32 raeburn 3748: if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 3749: }
3750:
1.800 albertel 3751: if(ref($hashref->{$key}) eq 'ARRAY') {
3752: $result.=&arrayref2str($hashref->{$key}).'&';
3753: } elsif(ref($hashref->{$key}) eq 'HASH') {
3754: $result.=&hashref2str($hashref->{$key}).'&';
3755: } elsif(ref($hashref->{$key})) {
1.265 albertel 3756: $result.='&';
1.800 albertel 3757: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 3758: } else {
1.800 albertel 3759: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 3760: }
3761: }
1.168 albertel 3762: $result=~s/\&$//;
1.265 albertel 3763: $result .= '__END_HASH_REF__';
1.168 albertel 3764: return $result;
3765: }
3766:
3767: sub str2hash {
1.265 albertel 3768: my ($string)=@_;
3769: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
3770: return %$hash;
3771: }
3772:
3773: sub str2hashref {
1.168 albertel 3774: my ($string) = @_;
1.265 albertel 3775:
3776: my %hash;
3777:
3778: if($string !~ /^__HASH_REF__/) {
3779: if (! ($string eq '' || !defined($string))) {
3780: $hash{'error'}='Not hash reference';
3781: }
3782: return (\%hash, $string);
3783: }
3784:
3785: $string =~ s/^__HASH_REF__//;
3786:
3787: while($string !~ /^__END_HASH_REF__/) {
3788: #key
3789: my $key='';
3790: if($string =~ /^__HASH_REF__/) {
3791: ($key, $string)=&str2hashref($string);
3792: if(defined($key->{'error'})) {
3793: $hash{'error'}='Bad data';
3794: return (\%hash, $string);
3795: }
3796: } elsif($string =~ /^__ARRAY_REF__/) {
3797: ($key, $string)=&str2arrayref($string);
3798: if($key->[0] eq 'Array reference error') {
3799: $hash{'error'}='Bad data';
3800: return (\%hash, $string);
3801: }
3802: } else {
3803: $string =~ s/^(.*?)=//;
1.267 albertel 3804: $key=&unescape($1);
1.265 albertel 3805: }
3806: $string =~ s/^=//;
3807:
3808: #value
3809: my $value='';
3810: if($string =~ /^__HASH_REF__/) {
3811: ($value, $string)=&str2hashref($string);
3812: if(defined($value->{'error'})) {
3813: $hash{'error'}='Bad data';
3814: return (\%hash, $string);
3815: }
3816: } elsif($string =~ /^__ARRAY_REF__/) {
3817: ($value, $string)=&str2arrayref($string);
3818: if($value->[0] eq 'Array reference error') {
3819: $hash{'error'}='Bad data';
3820: return (\%hash, $string);
3821: }
3822: } else {
3823: $value=&get_scalar(\$string,'__END_HASH_REF__');
3824: }
3825: $string =~ s/^&//;
3826:
3827: $hash{$key}=$value;
1.204 albertel 3828: }
1.265 albertel 3829:
3830: $string =~ s/^__END_HASH_REF__//;
3831:
3832: return (\%hash, $string);
1.204 albertel 3833: }
3834:
3835: sub str2array {
1.265 albertel 3836: my ($string)=@_;
3837: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
3838: return @$array;
3839: }
3840:
3841: sub str2arrayref {
1.204 albertel 3842: my ($string) = @_;
1.265 albertel 3843: my @array;
3844:
3845: if($string !~ /^__ARRAY_REF__/) {
3846: if (! ($string eq '' || !defined($string))) {
3847: $array[0]='Array reference error';
3848: }
3849: return (\@array, $string);
3850: }
3851:
3852: $string =~ s/^__ARRAY_REF__//;
3853:
3854: while($string !~ /^__END_ARRAY_REF__/) {
3855: my $value='';
3856: if($string =~ /^__HASH_REF__/) {
3857: ($value, $string)=&str2hashref($string);
3858: if(defined($value->{'error'})) {
3859: $array[0] ='Array reference error';
3860: return (\@array, $string);
3861: }
3862: } elsif($string =~ /^__ARRAY_REF__/) {
3863: ($value, $string)=&str2arrayref($string);
3864: if($value->[0] eq 'Array reference error') {
3865: $array[0] ='Array reference error';
3866: return (\@array, $string);
3867: }
3868: } else {
3869: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
3870: }
3871: $string =~ s/^&//;
3872:
3873: push(@array, $value);
1.191 harris41 3874: }
1.265 albertel 3875:
3876: $string =~ s/^__END_ARRAY_REF__//;
3877:
3878: return (\@array, $string);
1.168 albertel 3879: }
3880:
1.167 albertel 3881: # -------------------------------------------------------------------Temp Store
3882:
1.168 albertel 3883: sub tmpreset {
3884: my ($symb,$namespace,$domain,$stuname) = @_;
3885: if (!$symb) {
3886: $symb=&symbread();
1.620 albertel 3887: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3888: }
3889: $symb=escape($symb);
3890:
1.620 albertel 3891: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 3892: $namespace=~s/\//\_/g;
3893: $namespace=~s/\W//g;
3894:
1.620 albertel 3895: if (!$domain) { $domain=$env{'user.domain'}; }
3896: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3897: if ($domain eq 'public' && $stuname eq 'public') {
3898: $stuname=$ENV{'REMOTE_ADDR'};
3899: }
1.168 albertel 3900: my $path=$perlvar{'lonDaemons'}.'/tmp';
3901: my %hash;
3902: if (tie(%hash,'GDBM_File',
3903: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3904: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 3905: foreach my $key (keys(%hash)) {
1.180 albertel 3906: if ($key=~ /:$symb/) {
1.168 albertel 3907: delete($hash{$key});
3908: }
3909: }
3910: }
3911: }
3912:
1.167 albertel 3913: sub tmpstore {
1.168 albertel 3914: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3915:
3916: if (!$symb) {
3917: $symb=&symbread();
1.620 albertel 3918: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3919: }
3920: $symb=escape($symb);
3921:
3922: if (!$namespace) {
3923: # I don't think we would ever want to store this for a course.
3924: # it seems this will only be used if we don't have a course.
1.620 albertel 3925: #$namespace=$env{'request.course.id'};
1.168 albertel 3926: #if (!$namespace) {
1.620 albertel 3927: $namespace=$env{'request.state'};
1.168 albertel 3928: #}
3929: }
3930: $namespace=~s/\//\_/g;
3931: $namespace=~s/\W//g;
1.620 albertel 3932: if (!$domain) { $domain=$env{'user.domain'}; }
3933: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3934: if ($domain eq 'public' && $stuname eq 'public') {
3935: $stuname=$ENV{'REMOTE_ADDR'};
3936: }
1.168 albertel 3937: my $now=time;
3938: my %hash;
3939: my $path=$perlvar{'lonDaemons'}.'/tmp';
3940: if (tie(%hash,'GDBM_File',
3941: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3942: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3943: $hash{"version:$symb"}++;
3944: my $version=$hash{"version:$symb"};
3945: my $allkeys='';
3946: foreach my $key (keys(%$storehash)) {
3947: $allkeys.=$key.':';
1.591 albertel 3948: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 3949: }
3950: $hash{"$version:$symb:timestamp"}=$now;
3951: $allkeys.='timestamp';
3952: $hash{"$version:keys:$symb"}=$allkeys;
3953: if (untie(%hash)) {
3954: return 'ok';
3955: } else {
3956: return "error:$!";
3957: }
3958: } else {
3959: return "error:$!";
3960: }
3961: }
1.167 albertel 3962:
1.168 albertel 3963: # -----------------------------------------------------------------Temp Restore
1.167 albertel 3964:
1.168 albertel 3965: sub tmprestore {
3966: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 3967:
1.168 albertel 3968: if (!$symb) {
3969: $symb=&symbread();
1.620 albertel 3970: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3971: }
3972: $symb=escape($symb);
3973:
1.620 albertel 3974: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 3975:
1.620 albertel 3976: if (!$domain) { $domain=$env{'user.domain'}; }
3977: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3978: if ($domain eq 'public' && $stuname eq 'public') {
3979: $stuname=$ENV{'REMOTE_ADDR'};
3980: }
1.168 albertel 3981: my %returnhash;
3982: $namespace=~s/\//\_/g;
3983: $namespace=~s/\W//g;
3984: my %hash;
3985: my $path=$perlvar{'lonDaemons'}.'/tmp';
3986: if (tie(%hash,'GDBM_File',
3987: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3988: &GDBM_READER(),0640)) {
1.168 albertel 3989: my $version=$hash{"version:$symb"};
3990: $returnhash{'version'}=$version;
3991: my $scope;
3992: for ($scope=1;$scope<=$version;$scope++) {
3993: my $vkeys=$hash{"$scope:keys:$symb"};
3994: my @keys=split(/:/,$vkeys);
3995: my $key;
3996: $returnhash{"$scope:keys"}=$vkeys;
3997: foreach $key (@keys) {
1.591 albertel 3998: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
3999: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 4000: }
4001: }
1.168 albertel 4002: if (!(untie(%hash))) {
4003: return "error:$!";
4004: }
4005: } else {
4006: return "error:$!";
4007: }
4008: return %returnhash;
1.167 albertel 4009: }
4010:
1.9 www 4011: # ----------------------------------------------------------------------- Store
4012:
4013: sub store {
1.124 www 4014: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
4015: my $home='';
4016:
1.168 albertel 4017: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4018:
1.213 www 4019: $symb=&symbclean($symb);
1.122 albertel 4020: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4021:
1.620 albertel 4022: if (!$domain) { $domain=$env{'user.domain'}; }
4023: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4024:
4025: &devalidate($symb,$stuname,$domain);
1.109 www 4026:
4027: $symb=escape($symb);
1.187 www 4028: if (!$namespace) {
1.620 albertel 4029: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4030: return '';
4031: }
4032: }
1.620 albertel 4033: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4034:
4035: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4036: $$storehash{'host'}=$perlvar{'lonHostID'};
4037:
1.12 www 4038: my $namevalue='';
1.800 albertel 4039: foreach my $key (keys(%$storehash)) {
4040: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4041: }
1.12 www 4042: $namevalue=~s/\&$//;
1.187 www 4043: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 4044: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 4045: }
4046:
1.47 www 4047: # -------------------------------------------------------------- Critical Store
4048:
4049: sub cstore {
1.124 www 4050: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
4051: my $home='';
4052:
1.168 albertel 4053: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4054:
1.213 www 4055: $symb=&symbclean($symb);
1.122 albertel 4056: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 4057:
1.620 albertel 4058: if (!$domain) { $domain=$env{'user.domain'}; }
4059: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 4060:
4061: &devalidate($symb,$stuname,$domain);
1.109 www 4062:
4063: $symb=escape($symb);
1.187 www 4064: if (!$namespace) {
1.620 albertel 4065: unless ($namespace=$env{'request.course.id'}) {
1.187 www 4066: return '';
4067: }
4068: }
1.620 albertel 4069: if (!$home) { $home=$env{'user.home'}; }
1.447 www 4070:
4071: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
4072: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 4073:
1.47 www 4074: my $namevalue='';
1.800 albertel 4075: foreach my $key (keys(%$storehash)) {
4076: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 4077: }
1.47 www 4078: $namevalue=~s/\&$//;
1.187 www 4079: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 4080: return critical
4081: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 4082: }
4083:
1.9 www 4084: # --------------------------------------------------------------------- Restore
4085:
4086: sub restore {
1.124 www 4087: my ($symb,$namespace,$domain,$stuname) = @_;
4088: my $home='';
4089:
1.168 albertel 4090: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 4091:
1.122 albertel 4092: if (!$symb) {
1.1056.4.37 raeburn 4093: return if ($namespace eq 'courserequests');
4094: unless ($symb=escape(&symbread())) { return ''; }
1.122 albertel 4095: } else {
1.1056.4.37 raeburn 4096: unless ($namespace eq 'courserequests') {
4097: $symb=&escape(&symbclean($symb));
4098: }
1.122 albertel 4099: }
1.188 www 4100: if (!$namespace) {
1.620 albertel 4101: unless ($namespace=$env{'request.course.id'}) {
1.188 www 4102: return '';
4103: }
4104: }
1.620 albertel 4105: if (!$domain) { $domain=$env{'user.domain'}; }
4106: if (!$stuname) { $stuname=$env{'user.name'}; }
4107: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 4108: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
4109:
1.12 www 4110: my %returnhash=();
1.800 albertel 4111: foreach my $line (split(/\&/,$answer)) {
4112: my ($name,$value)=split(/\=/,$line);
1.591 albertel 4113: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 4114: }
1.75 www 4115: my $version;
4116: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 4117: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
4118: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 4119: }
1.75 www 4120: }
1.13 www 4121: return %returnhash;
1.34 www 4122: }
4123:
4124: # ---------------------------------------------------------- Course Description
4125:
4126: sub coursedescription {
1.731 albertel 4127: my ($courseid,$args)=@_;
1.34 www 4128: $courseid=~s/^\///;
1.49 www 4129: $courseid=~s/\_/\//g;
1.34 www 4130: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 4131: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 4132: my $normalid=$cdomain.'_'.$cnum;
4133: # need to always cache even if we get errors otherwise we keep
4134: # trying and trying and trying to get the course description.
4135: my %envhash=();
4136: my %returnhash=();
1.731 albertel 4137:
4138: my $expiretime=600;
4139: if ($env{'request.course.id'} eq $normalid) {
4140: $expiretime=120;
4141: }
4142:
4143: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
4144: if (!$args->{'freshen_cache'}
4145: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
4146: foreach my $key (keys(%env)) {
4147: next if ($key !~ /^\Q$prefix\E(.*)/);
4148: my ($setting) = $1;
4149: $returnhash{$setting} = $env{$key};
4150: }
4151: return %returnhash;
4152: }
4153:
4154: # get the data agin
4155: if (!$args->{'one_time'}) {
4156: $envhash{'course.'.$normalid.'.last_cache'}=time;
4157: }
1.811 albertel 4158:
1.34 www 4159: if ($chome ne 'no_host') {
1.302 albertel 4160: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 4161: if (!exists($returnhash{'con_lost'})) {
4162: $returnhash{'home'}= $chome;
4163: $returnhash{'domain'} = $cdomain;
4164: $returnhash{'num'} = $cnum;
1.741 raeburn 4165: if (!defined($returnhash{'type'})) {
4166: $returnhash{'type'} = 'Course';
4167: }
1.130 albertel 4168: while (my ($name,$value) = each %returnhash) {
1.53 www 4169: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 4170: }
1.270 www 4171: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 4172: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 4173: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 4174: $envhash{'course.'.$normalid.'.home'}=$chome;
4175: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
4176: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 4177: }
4178: }
1.731 albertel 4179: if (!$args->{'one_time'}) {
1.949 raeburn 4180: &appenv(\%envhash);
1.731 albertel 4181: }
1.302 albertel 4182: return %returnhash;
1.461 www 4183: }
4184:
1.1056.4.5 raeburn 4185: sub update_released_required {
4186: my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
4187: if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
4188: $cid = $env{'request.course.id'};
4189: $cdom = $env{'course.'.$cid.'.domain'};
4190: $cnum = $env{'course.'.$cid.'.num'};
4191: $chome = $env{'course.'.$cid.'.home'};
4192: }
4193: if ($needsrelease) {
4194: my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
4195: my $needsupdate;
4196: if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
4197: $needsupdate = 1;
4198: } else {
4199: my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
4200: my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
4201: if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
4202: $needsupdate = 1;
4203: }
4204: }
4205: if ($needsupdate) {
4206: my %needshash = (
4207: 'internal.releaserequired' => $needsrelease,
4208: );
4209: my $putresult = &put('environment',\%needshash,$cdom,$cnum);
4210: if ($putresult eq 'ok') {
4211: &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
4212: my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
4213: if (ref($crsinfo{$cid}) eq 'HASH') {
4214: $crsinfo{$cid}{'releaserequired'} = $needsrelease;
4215: &courseidput($cdom,\%crsinfo,$chome,'notime');
4216: }
4217: }
4218: }
4219: }
4220: return;
4221: }
4222:
1.461 www 4223: # -------------------------------------------------See if a user is privileged
4224:
4225: sub privileged {
4226: my ($username,$domain)=@_;
4227: my $rolesdump=&reply("dump:$domain:$username:roles",
4228: &homeserver($username,$domain));
1.1033 raeburn 4229: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
4230: ($rolesdump =~ /^error:/)) {
4231: return 0;
4232: }
1.461 www 4233: my $now=time;
4234: if ($rolesdump ne '') {
1.800 albertel 4235: foreach my $entry (split(/&/,$rolesdump)) {
4236: if ($entry!~/^rolesdef_/) {
4237: my ($area,$role)=split(/=/,$entry);
1.461 www 4238: $area=~s/\_\w\w$//;
4239: my ($trole,$tend,$tstart)=split(/_/,$role);
4240: if (($trole eq 'dc') || ($trole eq 'su')) {
4241: my $active=1;
4242: if ($tend) {
4243: if ($tend<$now) { $active=0; }
4244: }
4245: if ($tstart) {
4246: if ($tstart>$now) { $active=0; }
4247: }
4248: if ($active) { return 1; }
4249: }
4250: }
4251: }
4252: }
4253: return 0;
1.9 www 4254: }
1.1 albertel 4255:
1.103 harris41 4256: # -------------------------------------------------------- Get user privileges
1.11 www 4257:
4258: sub rolesinit {
4259: my ($domain,$username,$authhost)=@_;
1.1034 raeburn 4260: my $now=time;
4261: my %userroles = ('user.login.time' => $now);
1.1056.4.10 raeburn 4262: my $extra = &freeze_escape({'skipcheck' => 1});
1.1056.4.3 raeburn 4263: my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033 raeburn 4264: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
1.1056.4.3 raeburn 4265: ($rolesdump =~ /^error:/)) {
1.1033 raeburn 4266: return \%userroles;
4267: }
1.11 www 4268: my %allroles=();
1.678 raeburn 4269: my %allgroups=();
4270: my $group_privs;
1.11 www 4271:
4272: if ($rolesdump ne '') {
1.800 albertel 4273: foreach my $entry (split(/&/,$rolesdump)) {
4274: if ($entry!~/^rolesdef_/) {
4275: my ($area,$role)=split(/=/,$entry);
1.587 albertel 4276: $area=~s/\_\w\w$//;
1.678 raeburn 4277: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 4278: if ($role=~/^cr/) {
1.807 albertel 4279: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
4280: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 4281: ($tend,$tstart)=split('_',$trest);
4282: } else {
4283: $trole=$role;
4284: }
1.678 raeburn 4285: } elsif ($role =~ m|^gr/|) {
4286: ($trole,$tend,$tstart) = split(/_/,$role);
4287: ($trole,$group_privs) = split(/\//,$trole);
4288: $group_privs = &unescape($group_privs);
1.587 albertel 4289: } else {
4290: ($trole,$tend,$tstart)=split(/_/,$role);
4291: }
1.743 albertel 4292: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
4293: $username);
4294: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 4295: if (($tend!=0) && ($tend<$now)) { $trole=''; }
4296: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 4297: if (($area ne '') && ($trole ne '')) {
1.347 albertel 4298: my $spec=$trole.'.'.$area;
4299: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
4300: if ($trole =~ /^cr\//) {
1.567 raeburn 4301: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 4302: } elsif ($trole eq 'gr') {
4303: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 4304: } else {
1.567 raeburn 4305: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 4306: }
1.12 www 4307: }
1.662 raeburn 4308: }
1.191 harris41 4309: }
1.743 albertel 4310: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
4311: $userroles{'user.adv'} = $adv;
4312: $userroles{'user.author'} = $author;
1.620 albertel 4313: $env{'user.adv'}=$adv;
1.11 www 4314: }
1.743 albertel 4315: return \%userroles;
1.11 www 4316: }
4317:
1.567 raeburn 4318: sub set_arearole {
4319: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
4320: # log the associated role with the area
4321: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 4322: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 4323: }
4324:
4325: sub custom_roleprivs {
4326: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
4327: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
4328: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 4329: if (&hostname($homsvr) ne '') {
1.567 raeburn 4330: my ($rdummy,$roledef)=
4331: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
4332: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
4333: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
4334: if (defined($syspriv)) {
1.1043 raeburn 4335: if ($trest =~ /^$match_community$/) {
4336: $syspriv =~ s/bre\&S//;
4337: }
1.567 raeburn 4338: $$allroles{'cm./'}.=':'.$syspriv;
4339: $$allroles{$spec.'./'}.=':'.$syspriv;
4340: }
4341: if ($tdomain ne '') {
4342: if (defined($dompriv)) {
4343: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
4344: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
4345: }
4346: if (($trest ne '') && (defined($coursepriv))) {
4347: $$allroles{'cm.'.$area}.=':'.$coursepriv;
4348: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
4349: }
4350: }
4351: }
4352: }
4353: }
4354:
1.678 raeburn 4355: sub group_roleprivs {
4356: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
4357: my $access = 1;
4358: my $now = time;
4359: if (($tend!=0) && ($tend<$now)) { $access = 0; }
4360: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
4361: if ($access) {
1.811 albertel 4362: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 4363: $$allgroups{$course}{$group} .=':'.$group_privs;
4364: }
4365: }
1.567 raeburn 4366:
4367: sub standard_roleprivs {
4368: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
4369: if (defined($pr{$trole.':s'})) {
4370: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
4371: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
4372: }
4373: if ($tdomain ne '') {
4374: if (defined($pr{$trole.':d'})) {
4375: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4376: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4377: }
4378: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
4379: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
4380: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
4381: }
4382: }
4383: }
4384:
4385: sub set_userprivs {
1.1056.4.2 raeburn 4386: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 4387: my $author=0;
4388: my $adv=0;
1.678 raeburn 4389: my %grouproles = ();
4390: if (keys(%{$allgroups}) > 0) {
1.1056.4.2 raeburn 4391: my @groupkeys;
1.1000 raeburn 4392: foreach my $role (keys(%{$allroles})) {
1.1056.4.2 raeburn 4393: push(@groupkeys,$role);
4394: }
4395: if (ref($groups_roles) eq 'HASH') {
4396: foreach my $key (keys(%{$groups_roles})) {
4397: unless (grep(/^\Q$key\E$/,@groupkeys)) {
4398: push(@groupkeys,$key);
4399: }
4400: }
4401: }
4402: if (@groupkeys > 0) {
4403: foreach my $role (@groupkeys) {
4404: my ($trole,$area,$sec,$extendedarea);
4405: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
4406: $trole = $1;
4407: $area = $2;
4408: $sec = $3;
4409: $extendedarea = $area.$sec;
4410: if (exists($$allgroups{$area})) {
4411: foreach my $group (keys(%{$$allgroups{$area}})) {
4412: my $spec = $trole.'.'.$extendedarea;
4413: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 4414: $$allgroups{$area}{$group};
1.1056.4.2 raeburn 4415: }
1.678 raeburn 4416: }
4417: }
4418: }
4419: }
4420: }
1.800 albertel 4421: foreach my $group (keys(%grouproles)) {
4422: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 4423: }
1.800 albertel 4424: foreach my $role (keys(%{$allroles})) {
4425: my %thesepriv;
1.941 raeburn 4426: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 4427: foreach my $item (split(/:/,$$allroles{$role})) {
4428: if ($item ne '') {
4429: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 4430: if ($restrictions eq '') {
4431: $thesepriv{$privilege}='F';
4432: } elsif ($thesepriv{$privilege} ne 'F') {
4433: $thesepriv{$privilege}.=$restrictions;
4434: }
4435: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
4436: }
4437: }
4438: my $thesestr='';
1.800 albertel 4439: foreach my $priv (keys(%thesepriv)) {
4440: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
4441: }
4442: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 4443: }
4444: return ($author,$adv);
4445: }
4446:
1.994 raeburn 4447: sub role_status {
1.1002 raeburn 4448: my ($rolekey,$then,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 4449: my @pwhere = ();
4450: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
4451: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
4452: unless (!defined($$role) || $$role eq '') {
4453: $$where=join('.',@pwhere);
4454: $$trolecode=$$role.'.'.$$where;
4455: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
4456: $$tstatus='is';
4457: if ($$tstart && $$tstart>$then) {
4458: $$tstatus='future';
1.1034 raeburn 4459: if ($$tstart<$now) {
4460: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 4461: if (($$where ne '') && ($$role ne '')) {
1.1056.4.2 raeburn 4462: my (%allroles,%allgroups,$group_privs,
4463: %groups_roles,@rolecodes);
1.1002 raeburn 4464: my %userroles = (
4465: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
4466: );
1.1056.4.2 raeburn 4467: @rolecodes = ('cm');
1.1002 raeburn 4468: my $spec=$$role.'.'.$$where;
4469: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
4470: if ($$role =~ /^cr\//) {
4471: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1056.4.2 raeburn 4472: push(@rolecodes,'cr');
1.1002 raeburn 4473: } elsif ($$role eq 'gr') {
1.1056.4.2 raeburn 4474: push(@rolecodes,$$role);
1.1002 raeburn 4475: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
4476: $env{'user.name'});
1.1056.4.2 raeburn 4477: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 4478: (undef,my $group_privs) = split(/\//,$trole);
4479: $group_privs = &unescape($group_privs);
4480: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1056.4.2 raeburn 4481: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
4482: if (keys(%course_roles) > 0) {
4483: my ($tnum) = ($trest =~ /^($match_courseid)/);
4484: if ($tdomain ne '' && $tnum ne '') {
4485: foreach my $key (keys(%course_roles)) {
4486: if ($key =~ /^\Q$tnum\E:\Q$tdomain\E:([^:]+):?([^:]*)/) {
4487: my $crsrole = $1;
4488: my $crssec = $2;
4489: if ($crsrole =~ /^cr/) {
4490: unless (grep(/^cr$/,@rolecodes)) {
4491: push(@rolecodes,'cr');
4492: }
4493: } else {
4494: unless(grep(/^\Q$crsrole\E$/,@rolecodes)) {
4495: push(@rolecodes,$crsrole);
4496: }
4497: }
4498: my $rolekey = $crsrole.'./'.$tdomain.'/'.$tnum;
4499: if ($crssec ne '') {
4500: $rolekey .= '/'.$crssec;
4501: }
4502: $rolekey .= './';
4503: $groups_roles{$rolekey} = \@rolecodes;
4504: }
4505: }
4506: }
4507: }
1.1002 raeburn 4508: } else {
1.1056.4.2 raeburn 4509: push(@rolecodes,$$role);
1.1002 raeburn 4510: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
4511: }
1.1056.4.2 raeburn 4512: my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
4513: &appenv(\%userroles,\@rolecodes);
1.1002 raeburn 4514: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
4515: }
4516: }
1.1034 raeburn 4517: $$tstatus = 'is';
1.1002 raeburn 4518: }
1.994 raeburn 4519: }
4520: if ($$tend) {
4521: if ($$tend<$then) {
4522: $$tstatus='expired';
4523: } elsif ($$tend<$now) {
4524: $$tstatus='will_not';
4525: }
4526: }
4527: }
4528: }
4529: }
4530:
4531: sub check_adhoc_privs {
1.1056.4.12 raeburn 4532: my ($cdom,$cnum,$then,$refresh,$now,$checkrole,$caller) = @_;
1.994 raeburn 4533: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
4534: if ($env{$cckey}) {
4535: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1002 raeburn 4536: &role_status($cckey,$then,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 4537: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1056.4.12 raeburn 4538: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4539: }
4540: } else {
1.1056.4.12 raeburn 4541: &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994 raeburn 4542: }
4543: }
4544:
4545: sub set_adhoc_privileges {
4546: # role can be cc or ca
1.1056.4.12 raeburn 4547: my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994 raeburn 4548: my $area = '/'.$dcdom.'/'.$pickedcourse;
4549: my $spec = $role.'.'.$area;
4550: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
4551: $env{'user.name'});
4552: my %ccrole = ();
4553: &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
4554: my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
4555: &appenv(\%userroles,[$role,'cm']);
4556: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1056.4.12 raeburn 4557: unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
4558: &appenv( {'request.role' => $spec,
4559: 'request.role.domain' => $dcdom,
4560: 'request.course.sec' => ''
4561: }
4562: );
4563: my $tadv=0;
4564: if (&allowed('adv') eq 'F') { $tadv=1; }
4565: &appenv({'request.role.adv' => $tadv});
4566: }
1.994 raeburn 4567: }
4568:
1.12 www 4569: # --------------------------------------------------------------- get interface
4570:
4571: sub get {
1.131 albertel 4572: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4573: my $items='';
1.800 albertel 4574: foreach my $item (@$storearr) {
4575: $items.=&escape($item).'&';
1.191 harris41 4576: }
1.12 www 4577: $items=~s/\&$//;
1.620 albertel 4578: if (!$udomain) { $udomain=$env{'user.domain'}; }
4579: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 4580: my $uhome=&homeserver($uname,$udomain);
4581:
1.133 albertel 4582: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4583: my @pairs=split(/\&/,$rep);
1.273 albertel 4584: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
4585: return @pairs;
4586: }
1.15 www 4587: my %returnhash=();
1.42 www 4588: my $i=0;
1.800 albertel 4589: foreach my $item (@$storearr) {
4590: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4591: $i++;
1.191 harris41 4592: }
1.15 www 4593: return %returnhash;
1.27 www 4594: }
4595:
4596: # --------------------------------------------------------------- del interface
4597:
4598: sub del {
1.133 albertel 4599: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 4600: my $items='';
1.800 albertel 4601: foreach my $item (@$storearr) {
4602: $items.=&escape($item).'&';
1.191 harris41 4603: }
1.984 neumanie 4604:
1.27 www 4605: $items=~s/\&$//;
1.620 albertel 4606: if (!$udomain) { $udomain=$env{'user.domain'}; }
4607: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4608: my $uhome=&homeserver($uname,$udomain);
4609: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4610: }
4611:
4612: # -------------------------------------------------------------- dump interface
4613:
4614: sub dump {
1.1056.4.10 raeburn 4615: my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755 albertel 4616: if (!$udomain) { $udomain=$env{'user.domain'}; }
4617: if (!$uname) { $uname=$env{'user.name'}; }
4618: my $uhome=&homeserver($uname,$udomain);
4619: if ($regexp) {
4620: $regexp=&escape($regexp);
4621: } else {
4622: $regexp='.';
4623: }
1.1056.4.10 raeburn 4624: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755 albertel 4625: my @pairs=split(/\&/,$rep);
4626: my %returnhash=();
4627: foreach my $item (@pairs) {
4628: my ($key,$value)=split(/=/,$item,2);
4629: $key = &unescape($key);
4630: next if ($key =~ /^error: 2 /);
4631: $returnhash{$key}=&thaw_unescape($value);
4632: }
4633: return %returnhash;
1.407 www 4634: }
4635:
1.717 albertel 4636: # --------------------------------------------------------- dumpstore interface
4637:
4638: sub dumpstore {
4639: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 4640: if (!$udomain) { $udomain=$env{'user.domain'}; }
4641: if (!$uname) { $uname=$env{'user.name'}; }
4642: my $uhome=&homeserver($uname,$udomain);
4643: if ($regexp) {
4644: $regexp=&escape($regexp);
4645: } else {
4646: $regexp='.';
4647: }
4648: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
4649: my @pairs=split(/\&/,$rep);
4650: my %returnhash=();
4651: foreach my $item (@pairs) {
4652: my ($key,$value)=split(/=/,$item,2);
4653: next if ($key =~ /^error: 2 /);
4654: $returnhash{$key}=&thaw_unescape($value);
4655: }
4656: return %returnhash;
1.717 albertel 4657: }
4658:
1.407 www 4659: # -------------------------------------------------------------- keys interface
4660:
4661: sub getkeys {
4662: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 4663: if (!$udomain) { $udomain=$env{'user.domain'}; }
4664: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 4665: my $uhome=&homeserver($uname,$udomain);
4666: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
4667: my @keyarray=();
1.800 albertel 4668: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 4669: next if ($key =~ /^error: 2 /);
1.800 albertel 4670: push(@keyarray,&unescape($key));
1.407 www 4671: }
4672: return @keyarray;
1.318 matthew 4673: }
4674:
1.319 matthew 4675: # --------------------------------------------------------------- currentdump
4676: sub currentdump {
1.328 matthew 4677: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 4678: $courseid = $env{'request.course.id'} if (! defined($courseid));
4679: $sdom = $env{'user.domain'} if (! defined($sdom));
4680: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 4681: my $uhome = &homeserver($sname,$sdom);
4682: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 4683: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 4684: #
1.318 matthew 4685: my %returnhash=();
1.319 matthew 4686: #
4687: if ($rep eq "unknown_cmd") {
4688: # an old lond will not know currentdump
4689: # Do a dump and make it look like a currentdump
1.822 albertel 4690: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 4691: return if ($tmp[0] =~ /^(error:|no_such_host)/);
4692: my %hash = @tmp;
4693: @tmp=();
1.424 matthew 4694: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 4695: } else {
4696: my @pairs=split(/\&/,$rep);
1.800 albertel 4697: foreach my $pair (@pairs) {
4698: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 4699: my ($symb,$param) = split(/:/,$key);
4700: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 4701: &thaw_unescape($value);
1.319 matthew 4702: }
1.191 harris41 4703: }
1.12 www 4704: return %returnhash;
1.424 matthew 4705: }
4706:
4707: sub convert_dump_to_currentdump{
4708: my %hash = %{shift()};
4709: my %returnhash;
4710: # Code ripped from lond, essentially. The only difference
4711: # here is the unescaping done by lonnet::dump(). Conceivably
4712: # we might run in to problems with parameter names =~ /^v\./
4713: while (my ($key,$value) = each(%hash)) {
4714: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 4715: $symb = &unescape($symb);
4716: $param = &unescape($param);
1.424 matthew 4717: next if ($v eq 'version' || $symb eq 'keys');
4718: next if (exists($returnhash{$symb}) &&
4719: exists($returnhash{$symb}->{$param}) &&
4720: $returnhash{$symb}->{'v.'.$param} > $v);
4721: $returnhash{$symb}->{$param}=$value;
4722: $returnhash{$symb}->{'v.'.$param}=$v;
4723: }
4724: #
4725: # Remove all of the keys in the hashes which keep track of
4726: # the version of the parameter.
4727: while (my ($symb,$param_hash) = each(%returnhash)) {
4728: # use a foreach because we are going to delete from the hash.
4729: foreach my $key (keys(%$param_hash)) {
4730: delete($param_hash->{$key}) if ($key =~ /^v\./);
4731: }
4732: }
4733: return \%returnhash;
1.12 www 4734: }
4735:
1.627 albertel 4736: # ------------------------------------------------------ critical inc interface
4737:
4738: sub cinc {
4739: return &inc(@_,'critical');
4740: }
4741:
1.449 matthew 4742: # --------------------------------------------------------------- inc interface
4743:
4744: sub inc {
1.627 albertel 4745: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 4746: if (!$udomain) { $udomain=$env{'user.domain'}; }
4747: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 4748: my $uhome=&homeserver($uname,$udomain);
4749: my $items='';
4750: if (! ref($store)) {
4751: # got a single value, so use that instead
4752: $items = &escape($store).'=&';
4753: } elsif (ref($store) eq 'SCALAR') {
4754: $items = &escape($$store).'=&';
4755: } elsif (ref($store) eq 'ARRAY') {
4756: $items = join('=&',map {&escape($_);} @{$store});
4757: } elsif (ref($store) eq 'HASH') {
4758: while (my($key,$value) = each(%{$store})) {
4759: $items.= &escape($key).'='.&escape($value).'&';
4760: }
4761: }
4762: $items=~s/\&$//;
1.627 albertel 4763: if ($critical) {
4764: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
4765: } else {
4766: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
4767: }
1.449 matthew 4768: }
4769:
1.12 www 4770: # --------------------------------------------------------------- put interface
4771:
4772: sub put {
1.134 albertel 4773: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4774: if (!$udomain) { $udomain=$env{'user.domain'}; }
4775: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4776: my $uhome=&homeserver($uname,$udomain);
1.12 www 4777: my $items='';
1.800 albertel 4778: foreach my $item (keys(%$storehash)) {
4779: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4780: }
1.12 www 4781: $items=~s/\&$//;
1.134 albertel 4782: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 4783: }
4784:
1.631 albertel 4785: # ------------------------------------------------------------ newput interface
4786:
4787: sub newput {
4788: my ($namespace,$storehash,$udomain,$uname)=@_;
4789: if (!$udomain) { $udomain=$env{'user.domain'}; }
4790: if (!$uname) { $uname=$env{'user.name'}; }
4791: my $uhome=&homeserver($uname,$udomain);
4792: my $items='';
4793: foreach my $key (keys(%$storehash)) {
4794: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
4795: }
4796: $items=~s/\&$//;
4797: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
4798: }
4799:
4800: # --------------------------------------------------------- putstore interface
4801:
1.524 raeburn 4802: sub putstore {
1.715 albertel 4803: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 4804: if (!$udomain) { $udomain=$env{'user.domain'}; }
4805: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 4806: my $uhome=&homeserver($uname,$udomain);
4807: my $items='';
1.715 albertel 4808: foreach my $key (keys(%$storehash)) {
4809: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 4810: }
1.715 albertel 4811: $items=~s/\&$//;
1.716 albertel 4812: my $esc_symb=&escape($symb);
4813: my $esc_v=&escape($version);
1.715 albertel 4814: my $reply =
1.716 albertel 4815: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 4816: $uhome);
4817: if ($reply eq 'unknown_cmd') {
1.716 albertel 4818: # gfall back to way things use to be done
1.715 albertel 4819: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
4820: $uname);
1.524 raeburn 4821: }
1.715 albertel 4822: return $reply;
4823: }
4824:
4825: sub old_putstore {
1.716 albertel 4826: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
4827: if (!$udomain) { $udomain=$env{'user.domain'}; }
4828: if (!$uname) { $uname=$env{'user.name'}; }
4829: my $uhome=&homeserver($uname,$udomain);
4830: my %newstorehash;
1.800 albertel 4831: foreach my $item (keys(%$storehash)) {
4832: my $key = $version.':'.&escape($symb).':'.$item;
4833: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 4834: }
4835: my $items='';
4836: my %allitems = ();
1.800 albertel 4837: foreach my $item (keys(%newstorehash)) {
4838: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 4839: my $key = $1.':keys:'.$2;
4840: $allitems{$key} .= $3.':';
4841: }
1.800 albertel 4842: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 4843: }
1.800 albertel 4844: foreach my $item (keys(%allitems)) {
4845: $allitems{$item} =~ s/\:$//;
4846: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 4847: }
4848: $items=~s/\&$//;
4849: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 4850: }
4851:
1.47 www 4852: # ------------------------------------------------------ critical put interface
4853:
4854: sub cput {
1.134 albertel 4855: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4856: if (!$udomain) { $udomain=$env{'user.domain'}; }
4857: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4858: my $uhome=&homeserver($uname,$udomain);
1.47 www 4859: my $items='';
1.800 albertel 4860: foreach my $item (keys(%$storehash)) {
4861: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4862: }
1.47 www 4863: $items=~s/\&$//;
1.134 albertel 4864: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4865: }
4866:
4867: # -------------------------------------------------------------- eget interface
4868:
4869: sub eget {
1.133 albertel 4870: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4871: my $items='';
1.800 albertel 4872: foreach my $item (@$storearr) {
4873: $items.=&escape($item).'&';
1.191 harris41 4874: }
1.12 www 4875: $items=~s/\&$//;
1.620 albertel 4876: if (!$udomain) { $udomain=$env{'user.domain'}; }
4877: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4878: my $uhome=&homeserver($uname,$udomain);
4879: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4880: my @pairs=split(/\&/,$rep);
4881: my %returnhash=();
1.42 www 4882: my $i=0;
1.800 albertel 4883: foreach my $item (@$storearr) {
4884: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4885: $i++;
1.191 harris41 4886: }
1.12 www 4887: return %returnhash;
4888: }
4889:
1.667 albertel 4890: # ------------------------------------------------------------ tmpput interface
4891: sub tmpput {
1.802 raeburn 4892: my ($storehash,$server,$context)=@_;
1.667 albertel 4893: my $items='';
1.800 albertel 4894: foreach my $item (keys(%$storehash)) {
4895: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 4896: }
4897: $items=~s/\&$//;
1.802 raeburn 4898: if (defined($context)) {
4899: $items .= ':'.&escape($context);
4900: }
1.667 albertel 4901: return &reply("tmpput:$items",$server);
4902: }
4903:
4904: # ------------------------------------------------------------ tmpget interface
4905: sub tmpget {
1.688 albertel 4906: my ($token,$server)=@_;
4907: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4908: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 4909: my %returnhash;
4910: foreach my $item (split(/\&/,$rep)) {
4911: my ($key,$value)=split(/=/,$item);
1.951 raeburn 4912: next if ($key =~ /^error: 2 /);
1.667 albertel 4913: $returnhash{&unescape($key)}=&thaw_unescape($value);
4914: }
4915: return %returnhash;
4916: }
4917:
1.688 albertel 4918: # ------------------------------------------------------------ tmpget interface
4919: sub tmpdel {
4920: my ($token,$server)=@_;
4921: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4922: return &reply("tmpdel:$token",$server);
4923: }
4924:
1.765 albertel 4925: # -------------------------------------------------- portfolio access checking
4926:
4927: sub portfolio_access {
1.766 albertel 4928: my ($requrl) = @_;
1.765 albertel 4929: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
4930: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 4931: if ($result) {
4932: my %setters;
4933: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4934: my ($startblock,$endblock) =
4935: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
4936: if ($startblock && $endblock) {
4937: return 'B';
4938: }
4939: } else {
4940: my ($startblock,$endblock) =
4941: &Apache::loncommon::blockcheck(\%setters,'port');
4942: if ($startblock && $endblock) {
4943: return 'B';
4944: }
4945: }
4946: }
1.765 albertel 4947: if ($result eq 'ok') {
1.766 albertel 4948: return 'F';
1.765 albertel 4949: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 4950: return 'A';
1.765 albertel 4951: }
1.766 albertel 4952: return '';
1.765 albertel 4953: }
4954:
4955: sub get_portfolio_access {
1.767 albertel 4956: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
4957:
4958: if (!ref($access_hash)) {
4959: my $current_perms = &get_portfile_permissions($udom,$unum);
4960: my %access_controls = &get_access_controls($current_perms,$group,
4961: $file_name);
4962: $access_hash = $access_controls{$file_name};
4963: }
4964:
1.765 albertel 4965: my ($public,$guest,@domains,@users,@courses,@groups);
4966: my $now = time;
4967: if (ref($access_hash) eq 'HASH') {
4968: foreach my $key (keys(%{$access_hash})) {
4969: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
4970: if ($start > $now) {
4971: next;
4972: }
4973: if ($end && $end<$now) {
4974: next;
4975: }
4976: if ($scope eq 'public') {
4977: $public = $key;
4978: last;
4979: } elsif ($scope eq 'guest') {
4980: $guest = $key;
4981: } elsif ($scope eq 'domains') {
4982: push(@domains,$key);
4983: } elsif ($scope eq 'users') {
4984: push(@users,$key);
4985: } elsif ($scope eq 'course') {
4986: push(@courses,$key);
4987: } elsif ($scope eq 'group') {
4988: push(@groups,$key);
4989: }
4990: }
4991: if ($public) {
4992: return 'ok';
4993: }
4994: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4995: if ($guest) {
4996: return $guest;
4997: }
4998: } else {
4999: if (@domains > 0) {
5000: foreach my $domkey (@domains) {
5001: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
5002: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
5003: return 'ok';
5004: }
5005: }
5006: }
5007: }
5008: if (@users > 0) {
5009: foreach my $userkey (@users) {
1.865 raeburn 5010: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
5011: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
5012: if (ref($item) eq 'HASH') {
5013: if (($item->{'uname'} eq $env{'user.name'}) &&
5014: ($item->{'udom'} eq $env{'user.domain'})) {
5015: return 'ok';
5016: }
5017: }
5018: }
5019: }
1.765 albertel 5020: }
5021: }
5022: my %roleshash;
5023: my @courses_and_groups = @courses;
5024: push(@courses_and_groups,@groups);
5025: if (@courses_and_groups > 0) {
5026: my (%allgroups,%allroles);
5027: my ($start,$end,$role,$sec,$group);
5028: foreach my $envkey (%env) {
1.1056.4.1 raeburn 5029: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5030: my $cid = $2.'_'.$3;
5031: if ($1 eq 'gr') {
5032: $group = $4;
5033: $allgroups{$cid}{$group} = $env{$envkey};
5034: } else {
5035: if ($4 eq '') {
5036: $sec = 'none';
5037: } else {
5038: $sec = $4;
5039: }
5040: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5041: }
1.811 albertel 5042: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 5043: my $cid = $2.'_'.$3;
5044: if ($4 eq '') {
5045: $sec = 'none';
5046: } else {
5047: $sec = $4;
5048: }
5049: $allroles{$cid}{$1}{$sec} = $env{$envkey};
5050: }
5051: }
5052: if (keys(%allroles) == 0) {
5053: return;
5054: }
5055: foreach my $key (@courses_and_groups) {
5056: my %content = %{$$access_hash{$key}};
5057: my $cnum = $content{'number'};
5058: my $cdom = $content{'domain'};
5059: my $cid = $cdom.'_'.$cnum;
5060: if (!exists($allroles{$cid})) {
5061: next;
5062: }
5063: foreach my $role_id (keys(%{$content{'roles'}})) {
5064: my @sections = @{$content{'roles'}{$role_id}{'section'}};
5065: my @groups = @{$content{'roles'}{$role_id}{'group'}};
5066: my @status = @{$content{'roles'}{$role_id}{'access'}};
5067: my @roles = @{$content{'roles'}{$role_id}{'role'}};
5068: foreach my $role (keys(%{$allroles{$cid}})) {
5069: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
5070: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
5071: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
5072: if (grep/^all$/,@sections) {
5073: return 'ok';
5074: } else {
5075: if (grep/^$sec$/,@sections) {
5076: return 'ok';
5077: }
5078: }
5079: }
5080: }
5081: if (keys(%{$allgroups{$cid}}) == 0) {
5082: if (grep/^none$/,@groups) {
5083: return 'ok';
5084: }
5085: } else {
5086: if (grep/^all$/,@groups) {
5087: return 'ok';
5088: }
5089: foreach my $group (keys(%{$allgroups{$cid}})) {
5090: if (grep/^$group$/,@groups) {
5091: return 'ok';
5092: }
5093: }
5094: }
5095: }
5096: }
5097: }
5098: }
5099: }
5100: if ($guest) {
5101: return $guest;
5102: }
5103: }
5104: }
5105: return;
5106: }
5107:
5108: sub course_group_datechecker {
5109: my ($dates,$now,$status) = @_;
5110: my ($start,$end) = split(/\./,$dates);
5111: if (!$start && !$end) {
5112: return 'ok';
5113: }
5114: if (grep/^active$/,@{$status}) {
5115: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
5116: return 'ok';
5117: }
5118: }
5119: if (grep/^previous$/,@{$status}) {
5120: if ($end > $now ) {
5121: return 'ok';
5122: }
5123: }
5124: if (grep/^future$/,@{$status}) {
5125: if ($start > $now) {
5126: return 'ok';
5127: }
5128: }
5129: return;
5130: }
5131:
5132: sub parse_portfolio_url {
5133: my ($url) = @_;
5134:
5135: my ($type,$udom,$unum,$group,$file_name);
5136:
1.823 albertel 5137: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 5138: $type = 1;
5139: $udom = $1;
5140: $unum = $2;
5141: $file_name = $3;
1.823 albertel 5142: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 5143: $type = 2;
5144: $udom = $1;
5145: $unum = $2;
5146: $group = $3;
5147: $file_name = $3.'/'.$4;
5148: }
5149: if (wantarray) {
5150: return ($type,$udom,$unum,$file_name,$group);
5151: }
5152: return $type;
5153: }
5154:
5155: sub is_portfolio_url {
5156: my ($url) = @_;
5157: return scalar(&parse_portfolio_url($url));
5158: }
5159:
1.798 raeburn 5160: sub is_portfolio_file {
5161: my ($file) = @_;
1.820 raeburn 5162: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 5163: return 1;
5164: }
5165: return;
5166: }
5167:
1.976 raeburn 5168: sub usertools_access {
1.1056.4.9 raeburn 5169: my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref) = @_;
1.985 raeburn 5170: my ($access,%tools);
5171: if ($context eq '') {
5172: $context = 'tools';
5173: }
5174: if ($context eq 'requestcourses') {
5175: %tools = (
5176: official => 1,
5177: unofficial => 1,
1.1006 raeburn 5178: community => 1,
1.985 raeburn 5179: );
5180: } else {
5181: %tools = (
5182: aboutme => 1,
5183: blog => 1,
5184: portfolio => 1,
5185: );
5186: }
1.976 raeburn 5187: return if (!defined($tools{$tool}));
5188:
5189: if ((!defined($udom)) || (!defined($uname))) {
5190: $udom = $env{'user.domain'};
5191: $uname = $env{'user.name'};
5192: }
5193:
1.978 raeburn 5194: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5195: if ($action ne 'reload') {
1.985 raeburn 5196: if ($context eq 'requestcourses') {
5197: return $env{'environment.canrequest.'.$tool};
5198: } else {
5199: return $env{'environment.availabletools.'.$tool};
5200: }
5201: }
1.976 raeburn 5202: }
5203:
5204: my ($toolstatus,$inststatus);
5205:
1.985 raeburn 5206: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
5207: ($action ne 'reload')) {
5208: $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976 raeburn 5209: $inststatus = $env{'environment.inststatus'};
5210: } else {
1.1056.4.9 raeburn 5211: if (ref($userenvref) eq 'HASH') {
5212: $toolstatus = $userenvref->{$context.'.'.$tool};
5213: $inststatus = $userenvref->{'inststatus'};
5214: } else {
5215: my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
5216: $toolstatus = $userenv{$context.'.'.$tool};
5217: $inststatus = $userenv{'inststatus'};
5218: }
1.976 raeburn 5219: }
5220:
5221: if ($toolstatus ne '') {
5222: if ($toolstatus) {
5223: $access = 1;
5224: } else {
5225: $access = 0;
5226: }
5227: return $access;
5228: }
5229:
1.1056.4.9 raeburn 5230: my ($is_adv,%domdef);
5231: if (ref($is_advref) eq 'HASH') {
5232: $is_adv = $is_advref->{'is_adv'};
5233: } else {
5234: $is_adv = &is_advanced_user($udom,$uname);
5235: }
5236: if (ref($domdefref) eq 'HASH') {
5237: %domdef = %{$domdefref};
5238: } else {
5239: %domdef = &get_domain_defaults($udom);
5240: }
1.976 raeburn 5241: if (ref($domdef{$tool}) eq 'HASH') {
5242: if ($is_adv) {
5243: if ($domdef{$tool}{'_LC_adv'} ne '') {
5244: if ($domdef{$tool}{'_LC_adv'}) {
5245: $access = 1;
5246: } else {
5247: $access = 0;
5248: }
5249: return $access;
5250: }
5251: }
5252: if ($inststatus ne '') {
5253: my ($hasaccess,$hasnoaccess);
5254: foreach my $affiliation (split(/:/,$inststatus)) {
5255: if ($domdef{$tool}{$affiliation} ne '') {
5256: if ($domdef{$tool}{$affiliation}) {
5257: $hasaccess = 1;
5258: } else {
5259: $hasnoaccess = 1;
5260: }
5261: }
5262: }
5263: if ($hasaccess || $hasnoaccess) {
5264: if ($hasaccess) {
5265: $access = 1;
5266: } elsif ($hasnoaccess) {
5267: $access = 0;
5268: }
5269: return $access;
5270: }
5271: } else {
5272: if ($domdef{$tool}{'default'} ne '') {
5273: if ($domdef{$tool}{'default'}) {
5274: $access = 1;
5275: } elsif ($domdef{$tool}{'default'} == 0) {
5276: $access = 0;
5277: }
5278: return $access;
5279: }
5280: }
5281: } else {
1.985 raeburn 5282: if ($context eq 'tools') {
5283: $access = 1;
5284: } else {
5285: $access = 0;
5286: }
1.976 raeburn 5287: return $access;
5288: }
5289: }
5290:
1.1050 raeburn 5291: sub is_course_owner {
5292: my ($cdom,$cnum,$udom,$uname) = @_;
5293: if (($udom eq '') || ($uname eq '')) {
5294: $udom = $env{'user.domain'};
5295: $uname = $env{'user.name'};
5296: }
5297: unless (($udom eq '') || ($uname eq '')) {
5298: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
5299: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
5300: return 1;
5301: } else {
5302: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
5303: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
5304: return 1;
5305: }
5306: }
5307: }
5308: }
5309: return;
5310: }
5311:
1.976 raeburn 5312: sub is_advanced_user {
5313: my ($udom,$uname) = @_;
1.1056.4.10 raeburn 5314: if ($udom ne '' && $uname ne '') {
5315: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
5316: return $env{'user.adv'};
5317: }
5318: }
1.976 raeburn 5319: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
5320: my %allroles;
5321: my $is_adv;
5322: foreach my $role (keys(%roleshash)) {
5323: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
5324: my $area = '/'.$tdomain.'/'.$trest;
5325: if ($sec ne '') {
5326: $area .= '/'.$sec;
5327: }
5328: if (($area ne '') && ($trole ne '')) {
5329: my $spec=$trole.'.'.$area;
5330: if ($trole =~ /^cr\//) {
5331: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
5332: } elsif ($trole ne 'gr') {
5333: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
5334: }
5335: }
5336: }
5337: foreach my $role (keys(%allroles)) {
5338: last if ($is_adv);
5339: foreach my $item (split(/:/,$allroles{$role})) {
5340: if ($item ne '') {
5341: my ($privilege,$restrictions)=split(/&/,$item);
5342: if ($privilege eq 'adv') {
5343: $is_adv = 1;
5344: last;
5345: }
5346: }
5347: }
5348: }
5349: return $is_adv;
5350: }
1.798 raeburn 5351:
1.1035 raeburn 5352: sub check_can_request {
1.1036 raeburn 5353: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 5354: my $canreq = 0;
5355: my ($types,$typename) = &Apache::loncommon::course_types();
5356: my @options = ('approval','validate','autolimit');
5357: my $optregex = join('|',@options);
5358: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
5359: foreach my $type (@{$types}) {
5360: if (&usertools_access($env{'user.name'},
5361: $env{'user.domain'},
5362: $type,undef,'requestcourses')) {
5363: $canreq ++;
1.1036 raeburn 5364: if (ref($request_domains) eq 'HASH') {
5365: push(@{$request_domains->{$type}},$env{'user.domain'});
5366: }
1.1035 raeburn 5367: if ($dom eq $env{'user.domain'}) {
5368: $can_request->{$type} = 1;
5369: }
5370: }
5371: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
5372: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
5373: if (@curr > 0) {
1.1036 raeburn 5374: foreach my $item (@curr) {
5375: if (ref($request_domains) eq 'HASH') {
5376: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
5377: if ($otherdom ne '') {
5378: if (ref($request_domains->{$type}) eq 'ARRAY') {
5379: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
5380: push(@{$request_domains->{$type}},$otherdom);
5381: }
5382: } else {
5383: push(@{$request_domains->{$type}},$otherdom);
5384: }
5385: }
5386: }
5387: }
5388: unless($dom eq $env{'user.domain'}) {
5389: $canreq ++;
1.1035 raeburn 5390: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
5391: $can_request->{$type} = 1;
5392: }
5393: }
5394: }
5395: }
5396: }
5397: }
5398: return $canreq;
5399: }
5400:
1.341 www 5401: # ---------------------------------------------- Custom access rule evaluation
5402:
5403: sub customaccess {
5404: my ($priv,$uri)=@_;
1.807 albertel 5405: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 5406: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 5407: $udom = &LONCAPA::clean_domain($udom);
5408: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 5409: my $access=0;
1.800 albertel 5410: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 5411: my ($effect,$realm,$role,$type)=split(/\:/,$right);
5412: if ($type eq 'user') {
5413: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 5414: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 5415: if ($tdom) {
5416: if ($tdom ne $env{'user.domain'}) { next; }
5417: }
1.896 albertel 5418: if ($tuname) {
5419: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 5420: }
5421: $access=($effect eq 'allow');
5422: last;
5423: }
5424: } else {
5425: if ($role) {
5426: if ($role ne $urole) { next; }
5427: }
5428: foreach my $scope (split(/\s*\,\s*/,$realm)) {
5429: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
5430: if ($tdom) {
5431: if ($tdom ne $udom) { next; }
5432: }
5433: if ($tcrs) {
5434: if ($tcrs ne $ucrs) { next; }
5435: }
5436: if ($tsec) {
5437: if ($tsec ne $usec) { next; }
5438: }
5439: $access=($effect eq 'allow');
5440: last;
5441: }
5442: if ($realm eq '' && $role eq '') {
5443: $access=($effect eq 'allow');
5444: }
1.402 bowersj2 5445: }
1.341 www 5446: }
5447: return $access;
5448: }
5449:
1.103 harris41 5450: # ------------------------------------------------- Check for a user privilege
1.12 www 5451:
5452: sub allowed {
1.810 raeburn 5453: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 5454: my $ver_orguri=$uri;
1.439 www 5455: $uri=&deversion($uri);
1.152 www 5456: my $orguri=$uri;
1.52 www 5457: $uri=&declutter($uri);
1.809 raeburn 5458:
1.810 raeburn 5459: if ($priv eq 'evb') {
5460: # Evade communication block restrictions for specified role in a course
5461: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
5462: return $1;
5463: } else {
5464: return;
5465: }
5466: }
5467:
1.620 albertel 5468: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 5469: # Free bre access to adm and meta resources
1.775 albertel 5470: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 5471: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
5472: && ($priv eq 'bre')) {
1.14 www 5473: return 'F';
1.159 www 5474: }
5475:
1.545 banghart 5476: # Free bre access to user's own portfolio contents
1.714 raeburn 5477: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 5478: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 5479: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 5480: my %setters;
5481: my ($startblock,$endblock) =
5482: &Apache::loncommon::blockcheck(\%setters,'port');
5483: if ($startblock && $endblock) {
5484: return 'B';
5485: } else {
5486: return 'F';
5487: }
1.545 banghart 5488: }
5489:
1.762 raeburn 5490: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 5491: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
5492: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
5493: if (exists($env{'request.course.id'})) {
5494: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
5495: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
5496: if (($domain eq $cdom) && ($name eq $cnum)) {
5497: my $courseprivid=$env{'request.course.id'};
5498: $courseprivid=~s/\_/\//;
5499: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
5500: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
5501: return $1;
1.762 raeburn 5502: } else {
5503: if ($env{'request.course.sec'}) {
5504: $courseprivid.='/'.$env{'request.course.sec'};
5505: }
5506: if ($env{'user.priv.'.$env{'request.role'}.'./'.
5507: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
5508: return $2;
5509: }
1.714 raeburn 5510: }
5511: }
5512: }
5513: }
5514:
1.159 www 5515: # Free bre to public access
5516:
5517: if ($priv eq 'bre') {
1.238 www 5518: my $copyright=&metadata($uri,'copyright');
1.620 albertel 5519: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 5520: return 'F';
5521: }
1.238 www 5522: if ($copyright eq 'priv') {
5523: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5524: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 5525: return '';
5526: }
5527: }
5528: if ($copyright eq 'domain') {
5529: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5530: unless (($env{'user.domain'} eq $1) ||
5531: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 5532: return '';
5533: }
1.262 matthew 5534: }
1.620 albertel 5535: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 5536: # Library role, so allow browsing of resources in this domain.
5537: return 'F';
1.238 www 5538: }
1.341 www 5539: if ($copyright eq 'custom') {
5540: unless (&customaccess($priv,$uri)) { return ''; }
5541: }
1.14 www 5542: }
1.264 matthew 5543: # Domain coordinator is trying to create a course
1.620 albertel 5544: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 5545: # uri is the requested domain in this case.
5546: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 5547: # a role of dc for the domain in question.
1.620 albertel 5548: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 5549: }
1.29 www 5550:
1.52 www 5551: my $thisallowed='';
5552: my $statecond=0;
5553: my $courseprivid='';
5554:
1.1039 raeburn 5555: my $ownaccess;
1.1043 raeburn 5556: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 5557: if (($priv eq 'bro') && ($env{'user.author'})) {
5558: if ($uri eq '') {
5559: $ownaccess = 1;
5560: } else {
5561: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
5562: my $udom = $env{'user.domain'};
5563: my $uname = $env{'user.name'};
5564: if ($uri =~ m{^\Q$udom\E/?$}) {
5565: $ownaccess = 1;
1.1040 raeburn 5566: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 5567: unless ($uri =~ m{\.\./}) {
5568: $ownaccess = 1;
5569: }
5570: } elsif (($udom ne 'public') && ($uname ne 'public')) {
5571: my $now = time;
5572: if ($uri =~ m{^([^/]+)/?$}) {
5573: my $adom = $1;
5574: foreach my $key (keys(%env)) {
1.1042 raeburn 5575: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 5576: my ($start,$end) = split('.',$env{$key});
5577: if (($now >= $start) && (!$end || $end < $now)) {
5578: $ownaccess = 1;
5579: last;
5580: }
5581: }
5582: }
5583: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
5584: my $adom = $1;
5585: my $aname = $2;
1.1042 raeburn 5586: foreach my $role ('ca','aa') {
5587: if ($env{"user.role.$role./$adom/$aname"}) {
5588: my ($start,$end) =
5589: split('.',$env{"user.role.$role./$adom/$aname"});
5590: if (($now >= $start) && (!$end || $end < $now)) {
5591: $ownaccess = 1;
5592: last;
5593: }
1.1039 raeburn 5594: }
5595: }
5596: }
5597: }
5598: }
5599: }
5600: }
5601:
1.52 www 5602: # Course
5603:
1.620 albertel 5604: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5605: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5606: $thisallowed.=$1;
5607: }
1.52 www 5608: }
1.29 www 5609:
1.52 www 5610: # Domain
5611:
1.620 albertel 5612: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 5613: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5614: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5615: $thisallowed.=$1;
5616: }
1.12 www 5617: }
1.52 www 5618:
5619: # Course: uri itself is a course
1.66 www 5620: my $courseuri=$uri;
5621: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 5622: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 5623:
1.620 albertel 5624: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 5625: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5626: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5627: $thisallowed.=$1;
5628: }
1.12 www 5629: }
1.29 www 5630:
1.665 albertel 5631: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 5632: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 5633: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 5634: $thisallowed='';
1.671 raeburn 5635: my ($match)=&is_on_map($uri);
5636: if ($match) {
5637: if ($env{'user.priv.'.$env{'request.role'}.'./'}
5638: =~/\Q$priv\E\&([^\:]*)/) {
5639: $thisallowed.=$1;
5640: }
5641: } else {
1.705 albertel 5642: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 5643: if ($refuri) {
5644: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 5645: $thisallowed='F';
1.671 raeburn 5646: } else {
5647: $refuri=&declutter($refuri);
5648: my ($match) = &is_on_map($refuri);
5649: if ($match) {
5650: $thisallowed='F';
5651: }
1.669 raeburn 5652: }
1.671 raeburn 5653: }
5654: }
1.314 www 5655: }
1.492 albertel 5656:
1.766 albertel 5657: if ($priv eq 'bre'
5658: && $thisallowed ne 'F'
5659: && $thisallowed ne '2'
5660: && &is_portfolio_url($uri)) {
5661: $thisallowed = &portfolio_access($uri);
5662: }
5663:
1.52 www 5664: # Full access at system, domain or course-wide level? Exit.
1.29 www 5665: if ($thisallowed=~/F/) {
5666: return 'F';
5667: }
5668:
1.52 www 5669: # If this is generating or modifying users, exit with special codes
1.29 www 5670:
1.643 www 5671: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
5672: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 5673: my ($audom,$auname)=split('/',$uri);
1.643 www 5674: # no author name given, so this just checks on the general right to make a co-author in this domain
5675: unless ($auname) { return $thisallowed; }
5676: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 5677: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
5678: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
5679: ($audom ne $env{'request.role.domain'}))) { return ''; }
5680: }
1.52 www 5681: return $thisallowed;
5682: }
5683: #
1.103 harris41 5684: # Gathered so far: system, domain and course wide privileges
1.52 www 5685: #
5686: # Course: See if uri or referer is an individual resource that is part of
5687: # the course
5688:
1.620 albertel 5689: if ($env{'request.course.id'}) {
1.232 www 5690:
1.620 albertel 5691: $courseprivid=$env{'request.course.id'};
5692: if ($env{'request.course.sec'}) {
5693: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 5694: }
5695: $courseprivid=~s/\_/\//;
5696: my $checkreferer=1;
1.232 www 5697: my ($match,$cond)=&is_on_map($uri);
5698: if ($match) {
5699: $statecond=$cond;
1.620 albertel 5700: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5701: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5702: $thisallowed.=$1;
5703: $checkreferer=0;
5704: }
1.29 www 5705: }
1.83 www 5706:
1.148 www 5707: if ($checkreferer) {
1.620 albertel 5708: my $refuri=$env{'httpref.'.$orguri};
1.148 www 5709: unless ($refuri) {
1.800 albertel 5710: foreach my $key (keys(%env)) {
5711: if ($key=~/^httpref\..*\*/) {
5712: my $pattern=$key;
1.156 www 5713: $pattern=~s/^httpref\.\/res\///;
1.148 www 5714: $pattern=~s/\*/\[\^\/\]\+/g;
5715: $pattern=~s/\//\\\//g;
1.152 www 5716: if ($orguri=~/$pattern/) {
1.800 albertel 5717: $refuri=$env{$key};
1.148 www 5718: }
5719: }
1.191 harris41 5720: }
1.148 www 5721: }
1.232 www 5722:
1.148 www 5723: if ($refuri) {
1.152 www 5724: $refuri=&declutter($refuri);
1.232 www 5725: my ($match,$cond)=&is_on_map($refuri);
5726: if ($match) {
5727: my $refstatecond=$cond;
1.620 albertel 5728: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5729: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5730: $thisallowed.=$1;
1.53 www 5731: $uri=$refuri;
5732: $statecond=$refstatecond;
1.52 www 5733: }
5734: }
1.148 www 5735: }
1.29 www 5736: }
1.52 www 5737: }
1.29 www 5738:
1.52 www 5739: #
1.103 harris41 5740: # Gathered now: all privileges that could apply, and condition number
1.52 www 5741: #
5742: #
5743: # Full or no access?
5744: #
1.29 www 5745:
1.52 www 5746: if ($thisallowed=~/F/) {
5747: return 'F';
5748: }
1.29 www 5749:
1.52 www 5750: unless ($thisallowed) {
5751: return '';
5752: }
1.29 www 5753:
1.52 www 5754: # Restrictions exist, deal with them
5755: #
5756: # C:according to course preferences
5757: # R:according to resource settings
5758: # L:unless locked
5759: # X:according to user session state
5760: #
5761:
5762: # Possibly locked functionality, check all courses
1.54 www 5763: # Locks might take effect only after 10 minutes cache expiration for other
5764: # courses, and 2 minutes for current course
1.52 www 5765:
5766: my $envkey;
5767: if ($thisallowed=~/L/) {
1.1000 raeburn 5768: foreach $envkey (keys(%env)) {
1.54 www 5769: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
5770: my $courseid=$2;
5771: my $roleid=$1.'.'.$2;
1.92 www 5772: $courseid=~s/^\///;
1.54 www 5773: my $expiretime=600;
1.620 albertel 5774: if ($env{'request.role'} eq $roleid) {
1.54 www 5775: $expiretime=120;
5776: }
5777: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
5778: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 5779: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 5780: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 5781: }
1.620 albertel 5782: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
5783: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
5784: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
5785: &log($env{'user.domain'},$env{'user.name'},
5786: $env{'user.home'},
1.57 www 5787: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 5788: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5789: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5790: return '';
5791: }
5792: }
1.620 albertel 5793: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
5794: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
5795: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
5796: &log($env{'user.domain'},$env{'user.name'},
5797: $env{'user.home'},
1.57 www 5798: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 5799: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5800: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5801: return '';
5802: }
5803: }
5804: }
1.29 www 5805: }
1.52 www 5806: }
5807:
5808: #
5809: # Rest of the restrictions depend on selected course
5810: #
5811:
1.620 albertel 5812: unless ($env{'request.course.id'}) {
1.766 albertel 5813: if ($thisallowed eq 'A') {
5814: return 'A';
1.814 raeburn 5815: } elsif ($thisallowed eq 'B') {
5816: return 'B';
1.766 albertel 5817: } else {
5818: return '1';
5819: }
1.52 www 5820: }
1.29 www 5821:
1.52 www 5822: #
5823: # Now user is definitely in a course
5824: #
1.53 www 5825:
5826:
5827: # Course preferences
5828:
5829: if ($thisallowed=~/C/) {
1.620 albertel 5830: my $rolecode=(split(/\./,$env{'request.role'}))[0];
5831: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
5832: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 5833: =~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5834: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5835: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5836: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
5837: $env{'request.course.id'});
5838: }
1.237 www 5839: return '';
5840: }
5841:
1.620 albertel 5842: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 5843: =~/\Q$unamedom\E/) {
1.1056.4.22 raeburn 5844: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5845: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
5846: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
5847: $env{'request.course.id'});
5848: }
1.54 www 5849: return '';
5850: }
1.53 www 5851: }
5852:
5853: # Resource preferences
5854:
5855: if ($thisallowed=~/R/) {
1.620 albertel 5856: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 5857: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1056.4.22 raeburn 5858: if (($priv ne 'pch') && ($priv ne 'plc')) {
1.689 albertel 5859: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5860: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
5861: }
5862: return '';
1.54 www 5863: }
1.53 www 5864: }
1.30 www 5865:
1.246 www 5866: # Restricted by state or randomout?
1.30 www 5867:
1.52 www 5868: if ($thisallowed=~/X/) {
1.620 albertel 5869: if ($env{'acc.randomout'}) {
1.579 albertel 5870: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 5871: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 5872: return '';
5873: }
1.247 www 5874: }
5875: if (&condval($statecond)) {
1.52 www 5876: return '2';
5877: } else {
5878: return '';
5879: }
5880: }
1.30 www 5881:
1.766 albertel 5882: if ($thisallowed eq 'A') {
5883: return 'A';
1.814 raeburn 5884: } elsif ($thisallowed eq 'B') {
5885: return 'B';
1.766 albertel 5886: }
1.52 www 5887: return 'F';
1.232 www 5888: }
5889:
1.710 albertel 5890: sub split_uri_for_cond {
5891: my $uri=&deversion(&declutter(shift));
5892: my @uriparts=split(/\//,$uri);
5893: my $filename=pop(@uriparts);
5894: my $pathname=join('/',@uriparts);
5895: return ($pathname,$filename);
5896: }
1.232 www 5897: # --------------------------------------------------- Is a resource on the map?
5898:
5899: sub is_on_map {
1.710 albertel 5900: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 5901: #Trying to find the conditional for the file
1.620 albertel 5902: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 5903: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 5904: if ($match) {
1.289 bowersj2 5905: return (1,$1);
5906: } else {
1.434 www 5907: return (0,0);
1.289 bowersj2 5908: }
1.12 www 5909: }
5910:
1.427 www 5911: # --------------------------------------------------------- Get symb from alias
5912:
5913: sub get_symb_from_alias {
5914: my $symb=shift;
5915: my ($map,$resid,$url)=&decode_symb($symb);
5916: # Already is a symb
5917: if ($url) { return $symb; }
5918: # Must be an alias
5919: my $aliassymb='';
5920: my %bighash;
1.620 albertel 5921: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 5922: &GDBM_READER(),0640)) {
5923: my $rid=$bighash{'mapalias_'.$symb};
5924: if ($rid) {
5925: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 5926: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
5927: $resid,$bighash{'src_'.$rid});
1.427 www 5928: }
5929: untie %bighash;
5930: }
5931: return $aliassymb;
5932: }
5933:
1.12 www 5934: # ----------------------------------------------------------------- Define Role
5935:
5936: sub definerole {
5937: if (allowed('mcr','/')) {
5938: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 5939: foreach my $role (split(':',$sysrole)) {
5940: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5941: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
5942: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
5943: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5944: return "refused:s:$crole&$cqual";
5945: }
5946: }
1.191 harris41 5947: }
1.800 albertel 5948: foreach my $role (split(':',$domrole)) {
5949: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5950: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
5951: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
5952: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 5953: return "refused:d:$crole&$cqual";
5954: }
5955: }
1.191 harris41 5956: }
1.800 albertel 5957: foreach my $role (split(':',$courole)) {
5958: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5959: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
5960: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
5961: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5962: return "refused:c:$crole&$cqual";
5963: }
5964: }
1.191 harris41 5965: }
1.620 albertel 5966: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
5967: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 5968: "rolesdef_$rolename=".
5969: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 5970: return reply($command,$env{'user.home'});
1.12 www 5971: } else {
5972: return 'refused';
5973: }
1.105 harris41 5974: }
5975:
5976: # ---------------- Make a metadata query against the network of library servers
5977:
5978: sub metadata_query {
1.244 matthew 5979: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 5980: my %rhash;
1.845 albertel 5981: my %libserv = &all_library();
1.244 matthew 5982: my @server_list = (defined($server_array) ? @$server_array
5983: : keys(%libserv) );
5984: for my $server (@server_list) {
1.118 harris41 5985: unless ($custom or $customshow) {
5986: my $reply=&reply("querysend:".&escape($query),$server);
5987: $rhash{$server}=$reply;
5988: }
5989: else {
5990: my $reply=&reply("querysend:".&escape($query).':'.
5991: &escape($custom).':'.&escape($customshow),
5992: $server);
5993: $rhash{$server}=$reply;
5994: }
1.112 harris41 5995: }
1.118 harris41 5996: return \%rhash;
1.240 www 5997: }
5998:
5999: # ----------------------------------------- Send log queries and wait for reply
6000:
6001: sub log_query {
6002: my ($uname,$udom,$query,%filters)=@_;
6003: my $uhome=&homeserver($uname,$udom);
6004: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 6005: my $uhost=&hostname($uhome);
1.800 albertel 6006: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 6007: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
6008: $uhome);
1.479 albertel 6009: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 6010: return get_query_reply($queryid);
6011: }
6012:
1.818 raeburn 6013: # -------------------------- Update MySQL table for portfolio file
6014:
6015: sub update_portfolio_table {
1.821 raeburn 6016: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 6017: if ($group ne '') {
6018: $file_name =~s /^\Q$group\E//;
6019: }
1.818 raeburn 6020: my $homeserver = &homeserver($uname,$udom);
6021: my $queryid=
1.821 raeburn 6022: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
6023: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 6024: my $reply = &get_query_reply($queryid);
6025: return $reply;
6026: }
6027:
1.899 raeburn 6028: # -------------------------- Update MySQL allusers table
6029:
6030: sub update_allusers_table {
6031: my ($uname,$udom,$names) = @_;
6032: my $homeserver = &homeserver($uname,$udom);
6033: my $queryid=
6034: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
6035: 'lastname='.&escape($names->{'lastname'}).'%%'.
6036: 'firstname='.&escape($names->{'firstname'}).'%%'.
6037: 'middlename='.&escape($names->{'middlename'}).'%%'.
6038: 'generation='.&escape($names->{'generation'}).'%%'.
6039: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
6040: 'id='.&escape($names->{'id'}),$homeserver);
1.1056.4.4 raeburn 6041: return;
1.899 raeburn 6042: }
6043:
1.508 raeburn 6044: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 6045:
6046: sub fetch_enrollment_query {
1.511 raeburn 6047: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 6048: my $homeserver;
1.547 raeburn 6049: my $maxtries = 1;
1.508 raeburn 6050: if ($context eq 'automated') {
6051: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 6052: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 6053: } else {
6054: $homeserver = &homeserver($cnum,$dom);
6055: }
1.838 albertel 6056: my $host=&hostname($homeserver);
1.506 raeburn 6057: my $cmd = '';
1.1000 raeburn 6058: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 6059: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 6060: }
6061: $cmd =~ s/%%$//;
6062: $cmd = &escape($cmd);
6063: my $query = 'fetchenrollment';
1.620 albertel 6064: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 6065: unless ($queryid=~/^\Q$host\E\_/) {
6066: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
6067: return 'error: '.$queryid;
6068: }
1.506 raeburn 6069: my $reply = &get_query_reply($queryid);
1.547 raeburn 6070: my $tries = 1;
6071: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6072: $reply = &get_query_reply($queryid);
6073: $tries ++;
6074: }
1.526 raeburn 6075: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 6076: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 6077: } else {
1.901 albertel 6078: my @responses = split(/:/,$reply);
1.515 raeburn 6079: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 6080: foreach my $line (@responses) {
6081: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 6082: $$replyref{$key} = $value;
6083: }
6084: } else {
1.506 raeburn 6085: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 6086: foreach my $line (@responses) {
6087: my ($key,$value) = split(/=/,$line);
1.506 raeburn 6088: $$replyref{$key} = $value;
6089: if ($value > 0) {
1.800 albertel 6090: foreach my $item (@{$$affiliatesref{$key}}) {
6091: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 6092: my $destname = $pathname.'/'.$filename;
6093: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 6094: if ($xml_classlist =~ /^error/) {
6095: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
6096: } else {
1.506 raeburn 6097: if ( open(FILE,">$destname") ) {
6098: print FILE &unescape($xml_classlist);
6099: close(FILE);
1.526 raeburn 6100: } else {
6101: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 6102: }
6103: }
6104: }
6105: }
6106: }
6107: }
6108: return 'ok';
6109: }
6110: return 'error';
6111: }
6112:
1.242 www 6113: sub get_query_reply {
6114: my $queryid=shift;
1.240 www 6115: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
6116: my $reply='';
6117: for (1..100) {
6118: sleep 2;
6119: if (-e $replyfile.'.end') {
1.448 albertel 6120: if (open(my $fh,$replyfile)) {
1.904 albertel 6121: $reply = join('',<$fh>);
6122: close($fh);
1.240 www 6123: } else { return 'error: reply_file_error'; }
1.242 www 6124: return &unescape($reply);
6125: }
1.240 www 6126: }
1.242 www 6127: return 'timeout:'.$queryid;
1.240 www 6128: }
6129:
6130: sub courselog_query {
1.241 www 6131: #
6132: # possible filters:
6133: # url: url or symb
6134: # username
6135: # domain
6136: # action: view, submit, grade
6137: # start: timestamp
6138: # end: timestamp
6139: #
1.240 www 6140: my (%filters)=@_;
1.620 albertel 6141: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 6142: if ($filters{'url'}) {
6143: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
6144: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
6145: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
6146: }
1.620 albertel 6147: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
6148: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 6149: return &log_query($cname,$cdom,'courselog',%filters);
6150: }
6151:
6152: sub userlog_query {
1.858 raeburn 6153: #
6154: # possible filters:
6155: # action: log check role
6156: # start: timestamp
6157: # end: timestamp
6158: #
1.240 www 6159: my ($uname,$udom,%filters)=@_;
6160: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 6161: }
6162:
1.506 raeburn 6163: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
6164:
6165: sub auto_run {
1.508 raeburn 6166: my ($cnum,$cdom) = @_;
1.876 raeburn 6167: my $response = 0;
6168: my $settings;
6169: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
6170: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
6171: $settings = $domconfig{'autoenroll'};
6172: if ($settings->{'run'} eq '1') {
6173: $response = 1;
6174: }
6175: } else {
1.934 raeburn 6176: my $homeserver;
6177: if (&is_course($cdom,$cnum)) {
6178: $homeserver = &homeserver($cnum,$cdom);
6179: } else {
6180: $homeserver = &domain($cdom,'primary');
6181: }
6182: if ($homeserver ne 'no_host') {
6183: $response = &reply('autorun:'.$cdom,$homeserver);
6184: }
1.876 raeburn 6185: }
1.506 raeburn 6186: return $response;
6187: }
1.776 albertel 6188:
1.506 raeburn 6189: sub auto_get_sections {
1.508 raeburn 6190: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 6191: my $homeserver;
6192: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6193: $homeserver = &homeserver($cnum,$cdom);
6194: }
6195: if (!defined($homeserver)) {
6196: if ($cdom =~ /^$match_domain$/) {
6197: $homeserver = &domain($cdom,'primary');
6198: }
6199: }
6200: my @secs;
6201: if (defined($homeserver)) {
6202: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
6203: unless ($response eq 'refused') {
6204: @secs = split(/:/,$response);
6205: }
1.506 raeburn 6206: }
6207: return @secs;
6208: }
1.776 albertel 6209:
1.506 raeburn 6210: sub auto_new_course {
1.1056.4.19 raeburn 6211: my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508 raeburn 6212: my $homeserver = &homeserver($cnum,$cdom);
1.1056.4.19 raeburn 6213: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506 raeburn 6214: return $response;
6215: }
1.776 albertel 6216:
1.506 raeburn 6217: sub auto_validate_courseID {
1.508 raeburn 6218: my ($cnum,$cdom,$inst_course_id) = @_;
6219: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 6220: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 6221: return $response;
6222: }
1.776 albertel 6223:
1.1007 raeburn 6224: sub auto_validate_instcode {
1.1020 raeburn 6225: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 6226: my ($homeserver,$response);
6227: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6228: $homeserver = &homeserver($cnum,$cdom);
6229: }
6230: if (!defined($homeserver)) {
6231: if ($cdom =~ /^$match_domain$/) {
6232: $homeserver = &domain($cdom,'primary');
6233: }
6234: }
1.1056.4.2 raeburn 6235: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
6236: &escape($instcode).':'.&escape($owner),$homeserver));
1.1027 raeburn 6237: my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
6238: return ($outcome,$description);
1.1007 raeburn 6239: }
6240:
1.506 raeburn 6241: sub auto_create_password {
1.873 raeburn 6242: my ($cnum,$cdom,$authparam,$udom) = @_;
6243: my ($homeserver,$response);
1.506 raeburn 6244: my $create_passwd = 0;
6245: my $authchk = '';
1.873 raeburn 6246: if ($udom =~ /^$match_domain$/) {
6247: $homeserver = &domain($udom,'primary');
6248: }
6249: if ($homeserver eq '') {
6250: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
6251: $homeserver = &homeserver($cnum,$cdom);
6252: }
6253: }
6254: if ($homeserver eq '') {
6255: $authchk = 'nodomain';
1.506 raeburn 6256: } else {
1.873 raeburn 6257: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
6258: if ($response eq 'refused') {
6259: $authchk = 'refused';
6260: } else {
1.901 albertel 6261: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 6262: }
1.506 raeburn 6263: }
6264: return ($authparam,$create_passwd,$authchk);
6265: }
6266:
1.706 raeburn 6267: sub auto_photo_permission {
6268: my ($cnum,$cdom,$students) = @_;
6269: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 6270: my ($outcome,$perm_reqd,$conditions) =
6271: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 6272: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6273: return (undef,undef);
6274: }
1.706 raeburn 6275: return ($outcome,$perm_reqd,$conditions);
6276: }
6277:
6278: sub auto_checkphotos {
6279: my ($uname,$udom,$pid) = @_;
6280: my $homeserver = &homeserver($uname,$udom);
6281: my ($result,$resulttype);
6282: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 6283: &escape($uname).':'.&escape($pid),
6284: $homeserver));
1.709 albertel 6285: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6286: return (undef,undef);
6287: }
1.706 raeburn 6288: if ($outcome) {
6289: ($result,$resulttype) = split(/:/,$outcome);
6290: }
6291: return ($result,$resulttype);
6292: }
6293:
6294: sub auto_photochoice {
6295: my ($cnum,$cdom) = @_;
6296: my $homeserver = &homeserver($cnum,$cdom);
6297: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 6298: &escape($cdom),
6299: $homeserver)));
1.709 albertel 6300: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
6301: return (undef,undef);
6302: }
1.706 raeburn 6303: return ($update,$comment);
6304: }
6305:
6306: sub auto_photoupdate {
6307: my ($affiliatesref,$dom,$cnum,$photo) = @_;
6308: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 6309: my $host=&hostname($homeserver);
1.706 raeburn 6310: my $cmd = '';
6311: my $maxtries = 1;
1.800 albertel 6312: foreach my $affiliate (keys(%{$affiliatesref})) {
6313: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 6314: }
6315: $cmd =~ s/%%$//;
6316: $cmd = &escape($cmd);
6317: my $query = 'institutionalphotos';
6318: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
6319: unless ($queryid=~/^\Q$host\E\_/) {
6320: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
6321: return 'error: '.$queryid;
6322: }
6323: my $reply = &get_query_reply($queryid);
6324: my $tries = 1;
6325: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
6326: $reply = &get_query_reply($queryid);
6327: $tries ++;
6328: }
6329: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
6330: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
6331: } else {
6332: my @responses = split(/:/,$reply);
6333: my $outcome = shift(@responses);
6334: foreach my $item (@responses) {
6335: my ($key,$value) = split(/=/,$item);
6336: $$photo{$key} = $value;
6337: }
6338: return $outcome;
6339: }
6340: return 'error';
6341: }
6342:
1.521 raeburn 6343: sub auto_instcode_format {
1.793 albertel 6344: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
6345: $cat_order) = @_;
1.521 raeburn 6346: my $courses = '';
1.772 raeburn 6347: my @homeservers;
1.521 raeburn 6348: if ($caller eq 'global') {
1.841 albertel 6349: my %servers = &get_servers($codedom,'library');
6350: foreach my $tryserver (keys(%servers)) {
6351: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6352: push(@homeservers,$tryserver);
6353: }
1.584 raeburn 6354: }
1.1022 raeburn 6355: } elsif ($caller eq 'requests') {
6356: if ($codedom =~ /^$match_domain$/) {
6357: my $chome = &domain($codedom,'primary');
6358: unless ($chome eq 'no_host') {
6359: push(@homeservers,$chome);
6360: }
6361: }
1.521 raeburn 6362: } else {
1.772 raeburn 6363: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 6364: }
1.793 albertel 6365: foreach my $code (keys(%{$instcodes})) {
6366: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 6367: }
6368: chop($courses);
1.772 raeburn 6369: my $ok_response = 0;
6370: my $response;
6371: while (@homeservers > 0 && $ok_response == 0) {
6372: my $server = shift(@homeservers);
6373: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
6374: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
6375: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 6376: split(/:/,$response);
1.772 raeburn 6377: %{$codes} = (%{$codes},&str2hash($codes_str));
6378: push(@{$codetitles},&str2array($codetitles_str));
6379: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
6380: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
6381: $ok_response = 1;
6382: }
6383: }
6384: if ($ok_response) {
1.521 raeburn 6385: return 'ok';
1.772 raeburn 6386: } else {
6387: return $response;
1.521 raeburn 6388: }
6389: }
6390:
1.792 raeburn 6391: sub auto_instcode_defaults {
6392: my ($domain,$returnhash,$code_order) = @_;
6393: my @homeservers;
1.841 albertel 6394:
6395: my %servers = &get_servers($domain,'library');
6396: foreach my $tryserver (keys(%servers)) {
6397: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6398: push(@homeservers,$tryserver);
6399: }
1.792 raeburn 6400: }
1.841 albertel 6401:
1.792 raeburn 6402: my $response;
1.841 albertel 6403: foreach my $server (@homeservers) {
1.792 raeburn 6404: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 6405: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
6406:
6407: foreach my $pair (split(/\&/,$response)) {
6408: my ($name,$value)=split(/\=/,$pair);
6409: if ($name eq 'code_order') {
6410: @{$code_order} = split(/\&/,&unescape($value));
6411: } else {
6412: $returnhash->{&unescape($name)}=&unescape($value);
6413: }
6414: }
6415: return 'ok';
1.792 raeburn 6416: }
1.841 albertel 6417:
6418: return $response;
1.1003 raeburn 6419: }
6420:
6421: sub auto_possible_instcodes {
1.1007 raeburn 6422: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
6423: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
6424: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
6425: return;
6426: }
1.1003 raeburn 6427: my (@homeservers,$uhome);
6428: if (defined(&domain($domain,'primary'))) {
6429: $uhome=&domain($domain,'primary');
6430: push(@homeservers,&domain($domain,'primary'));
6431: } else {
6432: my %servers = &get_servers($domain,'library');
6433: foreach my $tryserver (keys(%servers)) {
6434: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6435: push(@homeservers,$tryserver);
6436: }
6437: }
6438: }
6439: my $response;
6440: foreach my $server (@homeservers) {
6441: $response=&reply('autopossibleinstcodes:'.$domain,$server);
6442: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 6443: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
6444: split(':',$response);
6445: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
6446: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 6447: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 6448: my ($name,$value)=split('=',$item);
6449: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6450: }
6451: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 6452: my ($name,$value)=split('=',$item);
6453: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6454: }
6455: return 'ok';
6456: }
6457: return $response;
6458: }
1.792 raeburn 6459:
1.1010 raeburn 6460: sub auto_courserequest_checks {
6461: my ($dom) = @_;
1.1020 raeburn 6462: my ($homeserver,%validations);
6463: if ($dom =~ /^$match_domain$/) {
6464: $homeserver = &domain($dom,'primary');
6465: }
6466: unless ($homeserver eq 'no_host') {
6467: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
6468: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
6469: my @items = split(/&/,$response);
6470: foreach my $item (@items) {
6471: my ($key,$value) = split('=',$item);
6472: $validations{&unescape($key)} = &thaw_unescape($value);
6473: }
6474: }
6475: }
1.1010 raeburn 6476: return %validations;
6477: }
6478:
1.1020 raeburn 6479: sub auto_courserequest_validation {
6480: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
6481: my ($homeserver,$response);
6482: if ($dom =~ /^$match_domain$/) {
6483: $homeserver = &domain($dom,'primary');
6484: }
6485: unless ($homeserver eq 'no_host') {
1.1021 raeburn 6486:
1.1020 raeburn 6487: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 6488: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020 raeburn 6489: ':'.&escape($instcode).':'.&escape($instseclist),
6490: $homeserver));
6491: }
6492: return $response;
6493: }
6494:
1.777 albertel 6495: sub auto_validate_class_sec {
1.918 raeburn 6496: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 6497: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 6498: my $ownerlist;
6499: if (ref($owners) eq 'ARRAY') {
6500: $ownerlist = join(',',@{$owners});
6501: } else {
6502: $ownerlist = $owners;
6503: }
1.773 raeburn 6504: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 6505: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 6506: return $response;
6507: }
6508:
1.679 raeburn 6509: # ------------------------------------------------------- Course Group routines
6510:
6511: sub get_coursegroups {
1.809 raeburn 6512: my ($cdom,$cnum,$group,$namespace) = @_;
6513: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 6514: }
6515:
1.679 raeburn 6516: sub modify_coursegroup {
6517: my ($cdom,$cnum,$groupsettings) = @_;
6518: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
6519: }
6520:
1.809 raeburn 6521: sub toggle_coursegroup_status {
6522: my ($cdom,$cnum,$group,$action) = @_;
6523: my ($from_namespace,$to_namespace);
6524: if ($action eq 'delete') {
6525: $from_namespace = 'coursegroups';
6526: $to_namespace = 'deleted_groups';
6527: } else {
6528: $from_namespace = 'deleted_groups';
6529: $to_namespace = 'coursegroups';
6530: }
6531: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 6532: if (my $tmp = &error(%curr_group)) {
6533: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
6534: return ('read error',$tmp);
6535: } else {
6536: my %savedsettings = %curr_group;
1.809 raeburn 6537: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 6538: my $deloutcome;
6539: if ($result eq 'ok') {
1.809 raeburn 6540: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 6541: } else {
6542: return ('write error',$result);
6543: }
6544: if ($deloutcome eq 'ok') {
6545: return 'ok';
6546: } else {
6547: return ('delete error',$deloutcome);
6548: }
6549: }
6550: }
6551:
1.679 raeburn 6552: sub modify_group_roles {
1.957 raeburn 6553: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 6554: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
6555: my $role = 'gr/'.&escape($userprivs);
6556: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 6557: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 6558: if ($result eq 'ok') {
6559: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
6560: }
1.679 raeburn 6561: return $result;
6562: }
6563:
6564: sub modify_coursegroup_membership {
6565: my ($cdom,$cnum,$membership) = @_;
6566: my $result = &put('groupmembership',$membership,$cdom,$cnum);
6567: return $result;
6568: }
6569:
1.682 raeburn 6570: sub get_active_groups {
6571: my ($udom,$uname,$cdom,$cnum) = @_;
6572: my $now = time;
6573: my %groups = ();
6574: foreach my $key (keys(%env)) {
1.811 albertel 6575: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 6576: my ($start,$end) = split(/\./,$env{$key});
6577: if (($end!=0) && ($end<$now)) { next; }
6578: if (($start!=0) && ($start>$now)) { next; }
6579: if ($1 eq $cdom && $2 eq $cnum) {
6580: $groups{$3} = $env{$key} ;
6581: }
6582: }
6583: }
6584: return %groups;
6585: }
6586:
1.683 raeburn 6587: sub get_group_membership {
6588: my ($cdom,$cnum,$group) = @_;
6589: return(&dump('groupmembership',$cdom,$cnum,$group));
6590: }
6591:
6592: sub get_users_groups {
6593: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 6594: my @usersgroups;
1.683 raeburn 6595: my $cachetime=1800;
6596:
6597: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 6598: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
6599: if (defined($cached)) {
1.734 albertel 6600: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 6601: } else {
6602: $grouplist = '';
1.816 raeburn 6603: my $courseurl = &courseid_to_courseurl($courseid);
1.1056.4.10 raeburn 6604: my $extra = &freeze_escape({'skipcheck' => 1});
6605: my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817 raeburn 6606: my $access_end = $env{'course.'.$courseid.
6607: '.default_enrollment_end_date'};
6608: my $now = time;
6609: foreach my $key (keys(%roleshash)) {
6610: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
6611: my $group = $1;
6612: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
6613: my $start = $2;
6614: my $end = $1;
6615: if ($start == -1) { next; } # deleted from group
6616: if (($start!=0) && ($start>$now)) { next; }
6617: if (($end!=0) && ($end<$now)) {
6618: if ($access_end && $access_end < $now) {
6619: if ($access_end - $end < 86400) {
6620: push(@usersgroups,$group);
1.733 raeburn 6621: }
6622: }
1.817 raeburn 6623: next;
1.733 raeburn 6624: }
1.817 raeburn 6625: push(@usersgroups,$group);
1.683 raeburn 6626: }
6627: }
6628: }
1.817 raeburn 6629: @usersgroups = &sort_course_groups($courseid,@usersgroups);
6630: $grouplist = join(':',@usersgroups);
6631: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 6632: }
1.733 raeburn 6633: return @usersgroups;
1.683 raeburn 6634: }
6635:
6636: sub devalidate_getgroups_cache {
6637: my ($udom,$uname,$cdom,$cnum)=@_;
6638: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 6639:
1.683 raeburn 6640: my $hashid="$udom:$uname:$courseid";
6641: &devalidate_cache_new('getgroups',$hashid);
6642: }
6643:
1.12 www 6644: # ------------------------------------------------------------------ Plain Text
6645:
6646: sub plaintext {
1.988 raeburn 6647: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 6648: if ($short =~ m{^cr/}) {
1.758 albertel 6649: return (split('/',$short))[-1];
6650: }
1.742 raeburn 6651: if (!defined($cid)) {
6652: $cid = $env{'request.course.id'};
6653: }
6654: my %rolenames = (
1.1008 raeburn 6655: Course => 'std',
6656: Community => 'alt1',
1.742 raeburn 6657: );
1.1037 raeburn 6658: if ($cid ne '') {
6659: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
6660: unless ($forcedefault) {
6661: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
6662: &Apache::lonlocal::mt_escape(\$roletext);
6663: return &Apache::lonlocal::mt($roletext);
6664: }
6665: }
6666: }
6667: if ((defined($type)) && (defined($rolenames{$type})) &&
6668: (defined($rolenames{$type})) &&
6669: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 6670: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 6671: } elsif ($cid ne '') {
6672: my $crstype = $env{'course.'.$cid.'.type'};
6673: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
6674: (defined($prp{$short}{$rolenames{$crstype}}))) {
6675: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
6676: }
1.742 raeburn 6677: }
1.1037 raeburn 6678: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 6679: }
6680:
6681: # ----------------------------------------------------------------- Assign Role
6682:
6683: sub assignrole {
1.957 raeburn 6684: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
6685: $context)=@_;
1.21 www 6686: my $mrole;
6687: if ($role =~ /^cr\//) {
1.393 www 6688: my $cwosec=$url;
1.811 albertel 6689: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 6690: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 6691: my $refused = 1;
6692: if ($context eq 'requestcourses') {
6693: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
6694: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
6695: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
6696: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6697: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6698: if ($crsenv{'internal.courseowner'} eq
6699: $env{'user.name'}.':'.$env{'user.domain'}) {
6700: $refused = '';
6701: }
6702: }
6703: }
6704: }
6705: }
6706: if ($refused) {
6707: &logthis('Refused custom assignrole: '.
6708: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
6709: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
6710: return 'refused';
6711: }
1.104 www 6712: }
1.21 www 6713: $mrole='cr';
1.678 raeburn 6714: } elsif ($role =~ /^gr\//) {
6715: my $cwogrp=$url;
1.811 albertel 6716: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 6717: unless (&allowed('mdg',$cwogrp)) {
6718: &logthis('Refused group assignrole: '.
6719: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
6720: $env{'user.name'}.' at '.$env{'user.domain'});
6721: return 'refused';
6722: }
6723: $mrole='gr';
1.21 www 6724: } else {
1.82 www 6725: my $cwosec=$url;
1.811 albertel 6726: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 6727: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
6728: my $refused;
6729: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
6730: if (!(&allowed('c'.$role,$url))) {
6731: $refused = 1;
6732: }
6733: } else {
6734: $refused = 1;
6735: }
1.947 raeburn 6736: if ($refused) {
1.1045 raeburn 6737: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6738: if (!$selfenroll && $context eq 'course') {
6739: my %crsenv;
6740: if ($role eq 'cc' || $role eq 'co') {
6741: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6742: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
6743: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
6744: if ($crsenv{'internal.courseowner'} eq
6745: $env{'user.name'}.':'.$env{'user.domain'}) {
6746: $refused = '';
6747: }
6748: }
6749: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
6750: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
6751: if ($crsenv{'internal.courseowner'} eq
6752: $env{'user.name'}.':'.$env{'user.domain'}) {
6753: $refused = '';
6754: }
6755: }
6756: }
6757: }
6758: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 6759: $refused = '';
1.1017 raeburn 6760: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 6761: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 6762: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 6763: my $wrongcc;
6764: if ($cnum =~ /^$match_community$/) {
6765: $wrongcc = 1 if ($role eq 'cc');
6766: } else {
6767: $wrongcc = 1 if ($role eq 'co');
6768: }
6769: unless ($wrongcc) {
6770: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6771: if ($crsenv{'internal.courseowner'} eq
6772: $env{'user.name'}.':'.$env{'user.domain'}) {
6773: $refused = '';
6774: }
1.1017 raeburn 6775: }
6776: }
6777: }
6778: if ($refused) {
1.947 raeburn 6779: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
6780: ' '.$role.' '.$end.' '.$start.' by '.
6781: $env{'user.name'}.' at '.$env{'user.domain'});
6782: return 'refused';
6783: }
1.932 raeburn 6784: }
1.1056.4.30 raeburn 6785: } elsif ($role eq 'au') {
6786: if ($url ne '/'.$udom.'/') {
6787: &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
6788: ' to assign author role for '.$uname.':'.$udom.
6789: ' in domain: '.$url.' refused (wrong domain).');
6790: return 'refused';
6791: }
1.104 www 6792: }
1.21 www 6793: $mrole=$role;
6794: }
1.620 albertel 6795: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 6796: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 6797: if ($end) { $command.='_'.$end; }
1.21 www 6798: if ($start) {
6799: if ($end) {
1.81 www 6800: $command.='_'.$start;
1.21 www 6801: } else {
1.81 www 6802: $command.='_0_'.$start;
1.21 www 6803: }
6804: }
1.739 raeburn 6805: my $origstart = $start;
6806: my $origend = $end;
1.957 raeburn 6807: my $delflag;
1.357 www 6808: # actually delete
6809: if ($deleteflag) {
1.373 www 6810: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 6811: # modify command to delete the role
1.620 albertel 6812: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 6813: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 6814: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 6815: # set start and finish to negative values for userrolelog
6816: $start=-1;
6817: $end=-1;
1.957 raeburn 6818: $delflag = 1;
1.357 www 6819: }
6820: }
6821: # send command
1.349 www 6822: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 6823: # log new user role if status is ok
1.349 www 6824: if ($answer eq 'ok') {
1.663 raeburn 6825: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 6826: # for course roles, perform group memberships changes triggered by role change.
1.957 raeburn 6827: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739 raeburn 6828: unless ($role =~ /^gr/) {
6829: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957 raeburn 6830: $origstart,$selfenroll,$context);
1.739 raeburn 6831: }
1.1053 raeburn 6832: if ($role eq 'cc') {
6833: &autoupdate_coowners($url,$end,$start,$uname,$udom);
6834: }
1.349 www 6835: }
6836: return $answer;
1.169 harris41 6837: }
6838:
1.1053 raeburn 6839: sub autoupdate_coowners {
6840: my ($url,$end,$start,$uname,$udom) = @_;
6841: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
6842: if (($cdom ne '') && ($cnum ne '')) {
6843: my $now = time;
6844: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
6845: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
6846: my %coursehash = &coursedescription($cdom.'_'.$cnum);
6847: my $instcode = $coursehash{'internal.coursecode'};
6848: if ($instcode ne '') {
1.1056 raeburn 6849: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
6850: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 6851: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 6852: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
6853: if ($result eq 'valid') {
6854: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 6855: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6856: push(@newcoowners,$coowner);
6857: }
6858: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
6859: push(@newcoowners,$uname.':'.$udom);
6860: }
6861: @newcoowners = sort(@newcoowners);
6862: } else {
6863: push(@newcoowners,$uname.':'.$udom);
6864: }
6865: } else {
6866: if ($coursehash{'internal.co-owners'}) {
6867: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6868: unless ($coowner eq $uname.':'.$udom) {
6869: push(@newcoowners,$coowner);
6870: }
6871: }
6872: unless (@newcoowners > 0) {
6873: $delcoowners = 1;
6874: $coowners = '';
6875: }
6876: }
6877: }
6878: if (@newcoowners || $delcoowners) {
6879: &store_coowners($cdom,$cnum,$coursehash{'home'},
6880: $delcoowners,@newcoowners);
6881: }
6882: }
6883: }
6884: }
6885: }
6886: }
6887: }
6888:
6889: sub store_coowners {
6890: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
6891: my $cid = $cdom.'_'.$cnum;
6892: my ($coowners,$delresult,$putresult);
6893: if (@newcoowners) {
6894: $coowners = join(',',@newcoowners);
6895: my %coownershash = (
6896: 'internal.co-owners' => $coowners,
6897: );
6898: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
6899: if ($putresult eq 'ok') {
6900: if ($env{'course.'.$cid.'.num'} eq $cnum) {
6901: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
6902: }
6903: }
6904: }
6905: if ($delcoowners) {
6906: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
6907: if ($delresult eq 'ok') {
6908: if ($env{'course.'.$cid.'.internal.co-owners'}) {
6909: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
6910: }
6911: }
6912: }
6913: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
6914: my %crsinfo =
6915: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6916: if (ref($crsinfo{$cid}) eq 'HASH') {
6917: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
6918: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
6919: }
6920: }
6921: }
6922:
1.169 harris41 6923: # -------------------------------------------------- Modify user authentication
1.197 www 6924: # Overrides without validation
6925:
1.169 harris41 6926: sub modifyuserauth {
6927: my ($udom,$uname,$umode,$upass)=@_;
6928: my $uhome=&homeserver($uname,$udom);
1.197 www 6929: unless (&allowed('mau',$udom)) { return 'refused'; }
6930: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 6931: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6932: ' in domain '.$env{'request.role.domain'});
1.169 harris41 6933: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
6934: &escape($upass),$uhome);
1.620 albertel 6935: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 6936: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
6937: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
6938: &log($udom,,$uname,$uhome,
1.620 albertel 6939: 'Authentication changed by '.$env{'user.domain'}.', '.
6940: $env{'user.name'}.', '.$umode.
1.197 www 6941: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 6942: unless ($reply eq 'ok') {
1.197 www 6943: &logthis('Authentication mode error: '.$reply);
1.169 harris41 6944: return 'error: '.$reply;
6945: }
1.170 harris41 6946: return 'ok';
1.80 www 6947: }
6948:
1.81 www 6949: # --------------------------------------------------------------- Modify a user
1.80 www 6950:
1.81 www 6951: sub modifyuser {
1.206 matthew 6952: my ($udom, $uname, $uid,
6953: $umode, $upass, $first,
6954: $middle, $last, $gene,
1.1056.4.1 raeburn 6955: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 6956: $udom= &LONCAPA::clean_domain($udom);
6957: $uname=&LONCAPA::clean_username($uname);
1.1056.4.1 raeburn 6958: my $showcandelete = 'none';
6959: if (ref($candelete) eq 'ARRAY') {
6960: if (@{$candelete} > 0) {
6961: $showcandelete = join(', ',@{$candelete});
6962: }
6963: }
1.81 www 6964: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 6965: $umode.', '.$first.', '.$middle.', '.
1.1056.4.1 raeburn 6966: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 6967: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
6968: ' desiredhome not specified').
1.620 albertel 6969: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6970: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 6971: my $uhome=&homeserver($uname,$udom,'true');
1.1056.4.4 raeburn 6972: my $newuser;
6973: if ($uhome eq 'no_host') {
6974: $newuser = 1;
6975: }
1.80 www 6976: # ----------------------------------------------------------------- Create User
1.406 albertel 6977: if (($uhome eq 'no_host') &&
6978: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 6979: my $unhome='';
1.844 albertel 6980: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 6981: $unhome = $desiredhome;
1.620 albertel 6982: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
6983: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 6984: } else { # load balancing routine for determining $unhome
1.81 www 6985: my $loadm=10000000;
1.841 albertel 6986: my %servers = &get_servers($udom,'library');
6987: foreach my $tryserver (keys(%servers)) {
6988: my $answer=reply('load',$tryserver);
6989: if (($answer=~/\d+/) && ($answer<$loadm)) {
6990: $loadm=$answer;
6991: $unhome=$tryserver;
6992: }
1.80 www 6993: }
6994: }
6995: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 6996: return 'error: unable to find a home server for '.$uname.
6997: ' in domain '.$udom;
1.80 www 6998: }
6999: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
7000: &escape($upass),$unhome);
7001: unless ($reply eq 'ok') {
7002: return 'error: '.$reply;
7003: }
1.230 stredwic 7004: $uhome=&homeserver($uname,$udom,'true');
1.80 www 7005: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 7006: return 'error: unable verify users home machine.';
1.80 www 7007: }
1.209 matthew 7008: } # End of creation of new user
1.80 www 7009: # ---------------------------------------------------------------------- Add ID
7010: if ($uid) {
7011: $uid=~tr/A-Z/a-z/;
7012: my %uidhash=&idrget($udom,$uname);
1.196 www 7013: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
7014: && (!$forceid)) {
1.80 www 7015: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 7016: return 'error: user id "'.$uid.'" does not match '.
7017: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 7018: }
7019: } else {
7020: &idput($udom,($uname => $uid));
7021: }
7022: }
7023: # -------------------------------------------------------------- Add names, etc
1.313 matthew 7024: my @tmp=&get('environment',
1.899 raeburn 7025: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 7026: 'permanentemail','inststatus'],
1.134 albertel 7027: $udom,$uname);
1.1056.4.4 raeburn 7028: my (%names,%oldnames);
1.313 matthew 7029: if ($tmp[0] =~ m/^error:.*/) {
7030: %names=();
7031: } else {
7032: %names = @tmp;
1.1056.4.4 raeburn 7033: %oldnames = %names;
1.313 matthew 7034: }
1.1056.4.6 raeburn 7035: #
1.1056.4.1 raeburn 7036: # If name, email and/or uid are blank (e.g., because an uploaded file
7037: # of users did not contain them), do not overwrite existing values
7038: # unless field is in $candelete array ref.
7039: #
7040:
7041: my @fields = ('firstname','middlename','lastname','generation',
7042: 'permanentemail','id');
7043: my %newvalues;
7044: if (ref($candelete) eq 'ARRAY') {
7045: foreach my $field (@fields) {
7046: if (grep(/^\Q$field\E$/,@{$candelete})) {
7047: if ($field eq 'firstname') {
7048: $names{$field} = $first;
7049: } elsif ($field eq 'middlename') {
7050: $names{$field} = $middle;
7051: } elsif ($field eq 'lastname') {
7052: $names{$field} = $last;
7053: } elsif ($field eq 'generation') {
7054: $names{$field} = $gene;
7055: } elsif ($field eq 'permanentemail') {
7056: $names{$field} = $email;
7057: } elsif ($field eq 'id') {
7058: $names{$field} = $uid;
7059: }
7060: }
7061: }
7062: }
1.388 www 7063: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 7064: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 7065: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 7066: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 7067: if ($email) {
7068: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 7069: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 7070: }
1.899 raeburn 7071: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 7072: if (defined($inststatus)) {
7073: $names{'inststatus'} = '';
7074: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
7075: if (ref($usertypes) eq 'HASH') {
7076: my @okstatuses;
7077: foreach my $item (split(/:/,$inststatus)) {
7078: if (defined($usertypes->{$item})) {
7079: push(@okstatuses,$item);
7080: }
7081: }
7082: if (@okstatuses) {
7083: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
7084: }
7085: }
7086: }
1.1056.4.4 raeburn 7087: my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963 raeburn 7088: $umode.', '.$first.', '.$middle.', '.
1.1056.4.4 raeburn 7089: $last.', '.$gene.', '.$email.', '.$inststatus;
1.963 raeburn 7090: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
7091: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
7092: } else {
7093: $logmsg .= ' during self creation';
7094: }
1.1056.4.4 raeburn 7095: my $changed;
7096: if ($newuser) {
7097: $changed = 1;
7098: } else {
7099: foreach my $field (@fields) {
7100: if ($names{$field} ne $oldnames{$field}) {
7101: $changed = 1;
7102: last;
7103: }
7104: }
7105: }
7106: unless ($changed) {
7107: $logmsg = 'No changes in user information needed for: '.$logmsg;
7108: &logthis($logmsg);
7109: return 'ok';
7110: }
7111: my $reply = &put('environment', \%names, $udom,$uname);
7112: if ($reply ne 'ok') {
7113: return 'error: '.$reply;
7114: }
1.1056.4.11 raeburn 7115: if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
7116: &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
7117: }
1.1056.4.4 raeburn 7118: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
7119: &devalidate_cache_new('namescache',$uname.':'.$udom);
7120: $logmsg = 'Success modifying user '.$logmsg;
1.963 raeburn 7121: &logthis($logmsg);
1.134 albertel 7122: return 'ok';
1.80 www 7123: }
7124:
1.81 www 7125: # -------------------------------------------------------------- Modify student
1.80 www 7126:
1.81 www 7127: sub modifystudent {
7128: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 7129: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990 raeburn 7130: $selfenroll,$context,$inststatus)=@_;
1.455 albertel 7131: if (!$cid) {
1.620 albertel 7132: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7133: return 'not_in_class';
7134: }
1.80 www 7135: }
7136: # --------------------------------------------------------------- Make the user
1.81 www 7137: my $reply=&modifyuser
1.209 matthew 7138: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 7139: $desiredhome,$email,$inststatus);
1.80 www 7140: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 7141: # This will cause &modify_student_enrollment to get the uid from the
7142: # students environment
7143: $uid = undef if (!$forceid);
1.455 albertel 7144: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957 raeburn 7145: $gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297 matthew 7146: return $reply;
7147: }
7148:
7149: sub modify_student_enrollment {
1.957 raeburn 7150: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455 albertel 7151: my ($cdom,$cnum,$chome);
7152: if (!$cid) {
1.620 albertel 7153: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 7154: return 'not_in_class';
7155: }
1.620 albertel 7156: $cdom=$env{'course.'.$cid.'.domain'};
7157: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 7158: } else {
7159: ($cdom,$cnum)=split(/_/,$cid);
7160: }
1.620 albertel 7161: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 7162: if (!$chome) {
1.457 raeburn 7163: $chome=&homeserver($cnum,$cdom);
1.297 matthew 7164: }
1.455 albertel 7165: if (!$chome) { return 'unknown_course'; }
1.297 matthew 7166: # Make sure the user exists
1.81 www 7167: my $uhome=&homeserver($uname,$udom);
7168: if (($uhome eq '') || ($uhome eq 'no_host')) {
7169: return 'error: no such user';
7170: }
1.297 matthew 7171: # Get student data if we were not given enough information
7172: if (!defined($first) || $first eq '' ||
7173: !defined($last) || $last eq '' ||
7174: !defined($uid) || $uid eq '' ||
7175: !defined($middle) || $middle eq '' ||
7176: !defined($gene) || $gene eq '') {
1.294 matthew 7177: # They did not supply us with enough data to enroll the student, so
7178: # we need to pick up more information.
1.297 matthew 7179: my %tmp = &get('environment',
1.294 matthew 7180: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 7181: ,$udom,$uname);
7182:
1.800 albertel 7183: #foreach my $key (keys(%tmp)) {
7184: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 7185: #}
1.294 matthew 7186: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
7187: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
7188: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 7189: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 7190: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
7191: }
1.556 albertel 7192: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 7193: my $reply=cput('classlist',
7194: {"$uname:$udom" =>
1.515 raeburn 7195: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 7196: $cdom,$cnum);
1.81 www 7197: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
7198: return 'error: '.$reply;
1.652 albertel 7199: } else {
7200: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 7201: }
1.297 matthew 7202: # Add student role to user
1.83 www 7203: my $uurl='/'.$cid;
1.81 www 7204: $uurl=~s/\_/\//g;
7205: if ($usec) {
7206: $uurl.='/'.$usec;
7207: }
1.957 raeburn 7208: return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21 www 7209: }
7210:
1.556 albertel 7211: sub format_name {
7212: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
7213: my $name;
7214: if ($first ne 'lastname') {
7215: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
7216: } else {
7217: if ($lastname=~/\S/) {
7218: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
7219: $name=~s/\s+,/,/;
7220: } else {
7221: $name.= $firstname.' '.$middlename.' '.$generation;
7222: }
7223: }
7224: $name=~s/^\s+//;
7225: $name=~s/\s+$//;
7226: $name=~s/\s+/ /g;
7227: return $name;
7228: }
7229:
1.84 www 7230: # ------------------------------------------------- Write to course preferences
7231:
7232: sub writecoursepref {
7233: my ($courseid,%prefs)=@_;
7234: $courseid=~s/^\///;
7235: $courseid=~s/\_/\//g;
7236: my ($cdomain,$cnum)=split(/\//,$courseid);
7237: my $chome=homeserver($cnum,$cdomain);
7238: if (($chome eq '') || ($chome eq 'no_host')) {
7239: return 'error: no such course';
7240: }
7241: my $cstring='';
1.800 albertel 7242: foreach my $pref (keys(%prefs)) {
7243: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 7244: }
1.84 www 7245: $cstring=~s/\&$//;
7246: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
7247: }
7248:
7249: # ---------------------------------------------------------- Make/modify course
7250:
7251: sub createcourse {
1.741 raeburn 7252: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 7253: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 7254: $url=&declutter($url);
7255: my $cid='';
1.1028 raeburn 7256: if ($context eq 'requestcourses') {
7257: my $can_create = 0;
7258: my ($ownername,$ownerdom) = split(':',$course_owner);
7259: if ($udom eq $ownerdom) {
7260: if (&usertools_access($ownername,$ownerdom,$category,undef,
7261: $context)) {
7262: $can_create = 1;
7263: }
7264: } else {
7265: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
7266: $category);
7267: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
7268: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
7269: if (@curr > 0) {
7270: my @options = qw(approval validate autolimit);
7271: my $optregex = join('|',@options);
7272: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
7273: $can_create = 1;
7274: }
7275: }
7276: }
7277: }
7278: if ($can_create) {
7279: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
7280: unless (&allowed('ccc',$udom)) {
7281: return 'refused';
7282: }
1.1017 raeburn 7283: }
7284: } else {
7285: return 'refused';
7286: }
1.1028 raeburn 7287: } elsif (!&allowed('ccc',$udom)) {
7288: return 'refused';
1.84 www 7289: }
1.1011 raeburn 7290: # --------------------------------------------------------------- Get Unique ID
7291: my $uname;
7292: if ($cnum =~ /^$match_courseid$/) {
7293: my $chome=&homeserver($cnum,$udom,'true');
7294: if (($chome eq '') || ($chome eq 'no_host')) {
7295: $uname = $cnum;
7296: } else {
1.1038 raeburn 7297: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7298: }
7299: } else {
1.1038 raeburn 7300: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 7301: }
7302: return $uname if ($uname =~ /^error/);
7303: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 7304: if (!defined($course_server)) {
7305: if (defined(&domain($udom,'primary'))) {
7306: $course_server = &domain($udom,'primary');
7307: } else {
7308: $course_server = $env{'user.home'};
7309: }
7310: }
7311: my %host_servers =
7312: &Apache::lonnet::get_servers($udom,'library');
7313: unless ($host_servers{$course_server}) {
7314: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 7315: }
1.84 www 7316: # ------------------------------------------------------------- Make the course
7317: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 7318: $course_server);
1.84 www 7319: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 7320: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 7321: if (($uhome eq '') || ($uhome eq 'no_host')) {
7322: return 'error: no such course';
7323: }
1.271 www 7324: # ----------------------------------------------------------------- Course made
1.516 raeburn 7325: # log existence
1.1029 raeburn 7326: my $now = time;
1.918 raeburn 7327: my $newcourse = {
7328: $udom.'_'.$uname => {
1.921 raeburn 7329: description => $description,
7330: inst_code => $inst_code,
7331: owner => $course_owner,
7332: type => $crstype,
1.1029 raeburn 7333: creator => $env{'user.name'}.':'.
7334: $env{'user.domain'},
7335: created => $now,
7336: context => $context,
1.918 raeburn 7337: },
7338: };
1.921 raeburn 7339: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 7340: # set toplevel url
1.271 www 7341: my $topurl=$url;
7342: unless ($nonstandard) {
7343: # ------------------------------------------ For standard courses, make top url
7344: my $mapurl=&clutter($url);
1.278 www 7345: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 7346: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 7347: <map>
7348: <resource id="1" type="start"></resource>
7349: <resource id="2" src="$mapurl"></resource>
7350: <resource id="3" type="finish"></resource>
7351: <link index="1" from="1" to="2"></link>
7352: <link index="2" from="2" to="3"></link>
7353: </map>
7354: ENDINITMAP
7355: $topurl=&declutter(
1.638 albertel 7356: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 7357: );
7358: }
7359: # ----------------------------------------------------------- Write preferences
1.84 www 7360: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 7361: ('description' => $description,
7362: 'url' => $topurl,
7363: 'internal.creator' => $env{'user.name'}.':'.
7364: $env{'user.domain'},
7365: 'internal.created' => $now,
7366: 'internal.creationcontext' => $context)
7367: );
1.84 www 7368: return '/'.$udom.'/'.$uname;
7369: }
7370:
1.1011 raeburn 7371: # ------------------------------------------------------------------- Create ID
7372: sub generate_coursenum {
1.1038 raeburn 7373: my ($udom,$crstype) = @_;
1.1011 raeburn 7374: my $domdesc = &domain($udom);
7375: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 7376: my $first;
7377: if ($crstype eq 'Community') {
7378: $first = '0';
7379: } else {
7380: $first = int(1+rand(9));
7381: }
7382: my $uname=$first.
1.1011 raeburn 7383: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7384: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7385: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7386: # ----------------------------------------------- Make sure that does not exist
7387: my $uhome=&homeserver($uname,$udom,'true');
7388: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 7389: if ($crstype eq 'Community') {
7390: $first = '0';
7391: } else {
7392: $first = int(1+rand(9));
7393: }
7394: $uname=$first.
1.1011 raeburn 7395: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
7396: substr($$.time,0,5).unpack("H8",pack("I32",time)).
7397: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
7398: $uhome=&homeserver($uname,$udom,'true');
7399: unless (($uhome eq '') || ($uhome eq 'no_host')) {
7400: return 'error: unable to generate unique course-ID';
7401: }
7402: }
7403: return $uname;
7404: }
7405:
1.813 albertel 7406: sub is_course {
7407: my ($cdom,$cnum) = @_;
7408: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946 raeburn 7409: undef,'.');
1.813 albertel 7410: if (exists($courses{$cdom.'_'.$cnum})) {
7411: return 1;
7412: }
7413: return 0;
7414: }
7415:
1.1015 raeburn 7416: sub store_userdata {
7417: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 7418: my $result;
1.1016 raeburn 7419: if ($datakey ne '') {
1.1013 raeburn 7420: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 7421: if ($udom eq '' || $uname eq '') {
7422: $udom = $env{'user.domain'};
7423: $uname = $env{'user.name'};
7424: }
7425: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 7426: if (($uhome eq '') || ($uhome eq 'no_host')) {
7427: $result = 'error: no_host';
7428: } else {
7429: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
7430: $storehash->{'host'} = $perlvar{'lonHostID'};
7431:
7432: my $namevalue='';
7433: foreach my $key (keys(%{$storehash})) {
7434: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7435: }
7436: $namevalue=~s/\&$//;
1.1056.4.37 raeburn 7437: unless ($namespace eq 'courserequests') {
7438: $datakey = &escape($datakey);
7439: }
1.1056.4.24 raeburn 7440: $result = &reply("store:$udom:$uname:$namespace:$datakey:".
7441: $namevalue,$uhome);
1.1013 raeburn 7442: }
7443: } else {
7444: $result = 'error: data to store was not a hash reference';
7445: }
7446: } else {
7447: $result= 'error: invalid requestkey';
7448: }
7449: return $result;
7450: }
7451:
1.21 www 7452: # ---------------------------------------------------------- Assign Custom Role
7453:
7454: sub assigncustomrole {
1.957 raeburn 7455: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7456: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 7457: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 7458: }
7459:
7460: # ----------------------------------------------------------------- Revoke Role
7461:
7462: sub revokerole {
1.957 raeburn 7463: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7464: my $now=time;
1.965 raeburn 7465: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 7466: }
7467:
7468: # ---------------------------------------------------------- Revoke Custom Role
7469:
7470: sub revokecustomrole {
1.957 raeburn 7471: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7472: my $now=time;
1.357 www 7473: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 7474: $deleteflag,$selfenroll,$context);
1.17 www 7475: }
7476:
1.533 banghart 7477: # ------------------------------------------------------------ Disk usage
1.535 albertel 7478: sub diskusage {
1.955 raeburn 7479: my ($udom,$uname,$directorypath,$getpropath)=@_;
7480: $directorypath =~ s/\/$//;
7481: my $listing=&reply('du2:'.&escape($directorypath).':'
7482: .&escape($getpropath).':'.&escape($uname).':'
7483: .&escape($udom),homeserver($uname,$udom));
7484: if ($listing eq 'unknown_cmd') {
7485: if ($getpropath) {
7486: $directorypath = &propath($udom,$uname).'/'.$directorypath;
7487: }
7488: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
7489: }
1.514 albertel 7490: return $listing;
1.512 banghart 7491: }
7492:
1.566 banghart 7493: sub is_locked {
1.1056.4.17 raeburn 7494: my ($file_name, $domain, $user, $which) = @_;
1.566 banghart 7495: my @check;
7496: my $is_locked;
1.1056.4.14 raeburn 7497: push(@check,$file_name);
1.613 albertel 7498: my %locked = &get('file_permissions',\@check,
1.620 albertel 7499: $env{'user.domain'},$env{'user.name'});
1.615 albertel 7500: my ($tmp)=keys(%locked);
7501: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 7502:
1.566 banghart 7503: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 7504: $is_locked = 'false';
7505: foreach my $entry (@{$locked{$file_name}}) {
1.1056.4.17 raeburn 7506: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 7507: $is_locked = 'true';
1.1056.4.17 raeburn 7508: if (ref($which) eq 'ARRAY') {
7509: push(@{$which},$entry);
7510: } else {
7511: last;
7512: }
1.745 raeburn 7513: }
7514: }
1.566 banghart 7515: } else {
7516: $is_locked = 'false';
7517: }
1.1056.4.14 raeburn 7518: return $is_locked;
1.566 banghart 7519: }
7520:
1.759 albertel 7521: sub declutter_portfile {
7522: my ($file) = @_;
1.833 albertel 7523: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 7524: return $file;
7525: }
7526:
1.559 banghart 7527: # ------------------------------------------------------------- Mark as Read Only
7528:
7529: sub mark_as_readonly {
7530: my ($domain,$user,$files,$what) = @_;
1.613 albertel 7531: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7532: my ($tmp)=keys(%current_permissions);
7533: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 7534: foreach my $file (@{$files}) {
1.759 albertel 7535: $file = &declutter_portfile($file);
1.561 banghart 7536: push(@{$current_permissions{$file}},$what);
1.559 banghart 7537: }
1.613 albertel 7538: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7539: return;
7540: }
7541:
1.572 banghart 7542: # ------------------------------------------------------------Save Selected Files
7543:
7544: sub save_selected_files {
7545: my ($user, $path, @files) = @_;
7546: my $filename = $user."savedfiles";
1.573 banghart 7547: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 7548: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 7549: foreach my $file (@files) {
1.620 albertel 7550: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 7551: }
7552: foreach my $file (@other_files) {
1.574 banghart 7553: print (OUT $file."\n");
1.572 banghart 7554: }
1.574 banghart 7555: close (OUT);
1.572 banghart 7556: return 'ok';
7557: }
7558:
1.574 banghart 7559: sub clear_selected_files {
7560: my ($user) = @_;
7561: my $filename = $user."savedfiles";
7562: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7563: print (OUT undef);
7564: close (OUT);
7565: return ("ok");
7566: }
7567:
1.572 banghart 7568: sub files_in_path {
7569: my ($user, $path) = @_;
7570: my $filename = $user."savedfiles";
7571: my %return_files;
1.574 banghart 7572: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 7573: while (my $line_in = <IN>) {
1.574 banghart 7574: chomp ($line_in);
7575: my @paths_and_file = split (m!/!, $line_in);
7576: my $file_part = pop (@paths_and_file);
7577: my $path_part = join ('/', @paths_and_file);
1.573 banghart 7578: $path_part.='/';
7579: my $path_and_file = $path_part.$file_part;
7580: if ($path_part eq $path) {
7581: $return_files{$file_part}= 'selected';
7582: }
7583: }
1.574 banghart 7584: close (IN);
7585: return (\%return_files);
1.572 banghart 7586: }
7587:
7588: # called in portfolio select mode, to show files selected NOT in current directory
7589: sub files_not_in_path {
7590: my ($user, $path) = @_;
7591: my $filename = $user."savedfiles";
7592: my @return_files;
7593: my $path_part;
1.800 albertel 7594: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7595: while (my $line = <IN>) {
1.572 banghart 7596: #ok, I know it's clunky, but I want it to work
1.800 albertel 7597: my @paths_and_file = split(m|/|, $line);
7598: my $file_part = pop(@paths_and_file);
7599: chomp($file_part);
7600: my $path_part = join('/', @paths_and_file);
1.572 banghart 7601: $path_part .= '/';
7602: my $path_and_file = $path_part.$file_part;
7603: if ($path_part ne $path) {
1.800 albertel 7604: push(@return_files, ($path_and_file));
1.572 banghart 7605: }
7606: }
1.800 albertel 7607: close(OUT);
1.574 banghart 7608: return (@return_files);
1.572 banghart 7609: }
7610:
1.745 raeburn 7611: #----------------------------------------------Get portfolio file permissions
1.629 banghart 7612:
1.745 raeburn 7613: sub get_portfile_permissions {
7614: my ($domain,$user) = @_;
1.613 albertel 7615: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7616: my ($tmp)=keys(%current_permissions);
7617: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7618: return \%current_permissions;
7619: }
7620:
7621: #---------------------------------------------Get portfolio file access controls
7622:
1.749 raeburn 7623: sub get_access_controls {
1.745 raeburn 7624: my ($current_permissions,$group,$file) = @_;
1.769 albertel 7625: my %access;
7626: my $real_file = $file;
7627: $file =~ s/\.meta$//;
1.745 raeburn 7628: if (defined($file)) {
1.749 raeburn 7629: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
7630: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 7631: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 7632: }
7633: }
1.745 raeburn 7634: } else {
1.749 raeburn 7635: foreach my $key (keys(%{$current_permissions})) {
7636: if ($key =~ /\0accesscontrol$/) {
7637: if (defined($group)) {
7638: if ($key !~ m-^\Q$group\E/-) {
7639: next;
7640: }
7641: }
7642: my ($fullpath) = split(/\0/,$key);
7643: if (ref($$current_permissions{$key}) eq 'HASH') {
7644: foreach my $control (keys(%{$$current_permissions{$key}})) {
7645: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
7646: }
7647: }
7648: }
7649: }
7650: }
7651: return %access;
7652: }
7653:
7654: sub modify_access_controls {
7655: my ($file_name,$changes,$domain,$user)=@_;
7656: my ($outcome,$deloutcome);
7657: my %store_permissions;
7658: my %new_values;
7659: my %new_control;
7660: my %translation;
7661: my @deletions = ();
7662: my $now = time;
7663: if (exists($$changes{'activate'})) {
7664: if (ref($$changes{'activate'}) eq 'HASH') {
7665: my @newitems = sort(keys(%{$$changes{'activate'}}));
7666: my $numnew = scalar(@newitems);
7667: for (my $i=0; $i<$numnew; $i++) {
7668: my $newkey = $newitems[$i];
7669: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 7670: if ($newkey =~ /^\d+:/) {
7671: $newkey =~ s/^(\d+)/$newid/;
7672: $translation{$1} = $newid;
7673: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
7674: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
7675: $translation{$1} = $newid;
7676: }
1.749 raeburn 7677: $new_values{$file_name."\0".$newkey} =
7678: $$changes{'activate'}{$newitems[$i]};
7679: $new_control{$newkey} = $now;
7680: }
7681: }
7682: }
7683: my %todelete;
7684: my %changed_items;
7685: foreach my $action ('delete','update') {
7686: if (exists($$changes{$action})) {
7687: if (ref($$changes{$action}) eq 'HASH') {
7688: foreach my $key (keys(%{$$changes{$action}})) {
7689: my ($itemnum) = ($key =~ /^([^:]+):/);
7690: if ($action eq 'delete') {
7691: $todelete{$itemnum} = 1;
7692: } else {
7693: $changed_items{$itemnum} = $key;
7694: }
7695: }
1.745 raeburn 7696: }
7697: }
1.749 raeburn 7698: }
7699: # get lock on access controls for file.
7700: my $lockhash = {
7701: $file_name."\0".'locked_access_records' => $env{'user.name'}.
7702: ':'.$env{'user.domain'},
7703: };
7704: my $tries = 0;
7705: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7706:
7707: while (($gotlock ne 'ok') && $tries <3) {
7708: $tries ++;
7709: sleep 1;
7710: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7711: }
7712: if ($gotlock eq 'ok') {
7713: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
7714: my ($tmp)=keys(%curr_permissions);
7715: if ($tmp=~/^error:/) { undef(%curr_permissions); }
7716: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
7717: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
7718: if (ref($curr_controls) eq 'HASH') {
7719: foreach my $control_item (keys(%{$curr_controls})) {
7720: my ($itemnum) = ($control_item =~ /^([^:]+):/);
7721: if (defined($todelete{$itemnum})) {
7722: push(@deletions,$file_name."\0".$control_item);
7723: } else {
7724: if (defined($changed_items{$itemnum})) {
7725: $new_control{$changed_items{$itemnum}} = $now;
7726: push(@deletions,$file_name."\0".$control_item);
7727: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
7728: } else {
7729: $new_control{$control_item} = $$curr_controls{$control_item};
7730: }
7731: }
1.745 raeburn 7732: }
7733: }
7734: }
1.970 raeburn 7735: my ($group);
7736: if (&is_course($domain,$user)) {
7737: ($group,my $file) = split(/\//,$file_name,2);
7738: }
1.749 raeburn 7739: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
7740: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
7741: $outcome = &put('file_permissions',\%new_values,$domain,$user);
7742: # remove lock
7743: my @del_lock = ($file_name."\0".'locked_access_records');
7744: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 7745: my $sqlresult =
1.970 raeburn 7746: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 7747: $group);
1.749 raeburn 7748: } else {
7749: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 7750: }
1.749 raeburn 7751: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 7752: }
7753:
1.827 raeburn 7754: sub make_public_indefinitely {
7755: my ($requrl) = @_;
7756: my $now = time;
7757: my $action = 'activate';
7758: my $aclnum = 0;
7759: if (&is_portfolio_url($requrl)) {
7760: my (undef,$udom,$unum,$file_name,$group) =
7761: &parse_portfolio_url($requrl);
7762: my $current_perms = &get_portfile_permissions($udom,$unum);
7763: my %access_controls = &get_access_controls($current_perms,
7764: $group,$file_name);
7765: foreach my $key (keys(%{$access_controls{$file_name}})) {
7766: my ($num,$scope,$end,$start) =
7767: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
7768: if ($scope eq 'public') {
7769: if ($start <= $now && $end == 0) {
7770: $action = 'none';
7771: } else {
7772: $action = 'update';
7773: $aclnum = $num;
7774: }
7775: last;
7776: }
7777: }
7778: if ($action eq 'none') {
7779: return 'ok';
7780: } else {
7781: my %changes;
7782: my $newend = 0;
7783: my $newstart = $now;
7784: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
7785: $changes{$action}{$newkey} = {
7786: type => 'public',
7787: time => {
7788: start => $newstart,
7789: end => $newend,
7790: },
7791: };
7792: my ($outcome,$deloutcome,$new_values,$translation) =
7793: &modify_access_controls($file_name,\%changes,$udom,$unum);
7794: return $outcome;
7795: }
7796: } else {
7797: return 'invalid';
7798: }
7799: }
7800:
1.745 raeburn 7801: #------------------------------------------------------Get Marked as Read Only
7802:
7803: sub get_marked_as_readonly {
7804: my ($domain,$user,$what,$group) = @_;
7805: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 7806: my @readonly_files;
1.629 banghart 7807: my $cmp1=$what;
7808: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 7809: while (my ($file_name,$value) = each(%{$current_permissions})) {
7810: if (defined($group)) {
7811: if ($file_name !~ m-^\Q$group\E/-) {
7812: next;
7813: }
7814: }
1.561 banghart 7815: if (ref($value) eq "ARRAY"){
7816: foreach my $stored_what (@{$value}) {
1.629 banghart 7817: my $cmp2=$stored_what;
1.759 albertel 7818: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 7819: $cmp2=join('',@{$stored_what});
1.745 raeburn 7820: }
1.629 banghart 7821: if ($cmp1 eq $cmp2) {
1.561 banghart 7822: push(@readonly_files, $file_name);
1.745 raeburn 7823: last;
1.563 banghart 7824: } elsif (!defined($what)) {
7825: push(@readonly_files, $file_name);
1.745 raeburn 7826: last;
1.561 banghart 7827: }
7828: }
1.745 raeburn 7829: }
1.561 banghart 7830: }
7831: return @readonly_files;
7832: }
1.577 banghart 7833: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 7834:
1.577 banghart 7835: sub get_marked_as_readonly_hash {
1.745 raeburn 7836: my ($current_permissions,$group,$what) = @_;
1.577 banghart 7837: my %readonly_files;
1.745 raeburn 7838: while (my ($file_name,$value) = each(%{$current_permissions})) {
7839: if (defined($group)) {
7840: if ($file_name !~ m-^\Q$group\E/-) {
7841: next;
7842: }
7843: }
1.577 banghart 7844: if (ref($value) eq "ARRAY"){
7845: foreach my $stored_what (@{$value}) {
1.745 raeburn 7846: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 7847: foreach my $lock_descriptor(@{$stored_what}) {
7848: if ($lock_descriptor eq 'graded') {
7849: $readonly_files{$file_name} = 'graded';
7850: } elsif ($lock_descriptor eq 'handback') {
7851: $readonly_files{$file_name} = 'handback';
7852: } else {
7853: if (!exists($readonly_files{$file_name})) {
7854: $readonly_files{$file_name} = 'locked';
7855: }
7856: }
1.745 raeburn 7857: }
1.750 banghart 7858: }
1.577 banghart 7859: }
7860: }
7861: }
7862: return %readonly_files;
7863: }
1.559 banghart 7864: # ------------------------------------------------------------ Unmark as Read Only
7865:
7866: sub unmark_as_readonly {
1.629 banghart 7867: # unmarks $file_name (if $file_name is defined), or all files locked by $what
7868: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 7869: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 7870: $file_name = &declutter_portfile($file_name);
1.634 albertel 7871: my $symb_crs = $what;
7872: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 7873: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 7874: my ($tmp)=keys(%current_permissions);
7875: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7876: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 7877: foreach my $file (@readonly_files) {
1.759 albertel 7878: my $clean_file = &declutter_portfile($file);
7879: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 7880: my $current_locks = $current_permissions{$file};
1.563 banghart 7881: my @new_locks;
7882: my @del_keys;
7883: if (ref($current_locks) eq "ARRAY"){
7884: foreach my $locker (@{$current_locks}) {
1.632 albertel 7885: my $compare=$locker;
1.749 raeburn 7886: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 7887: $compare=join('',@{$locker});
1.746 raeburn 7888: if ($compare ne $symb_crs) {
7889: push(@new_locks, $locker);
7890: }
1.563 banghart 7891: }
7892: }
1.650 albertel 7893: if (scalar(@new_locks) > 0) {
1.563 banghart 7894: $current_permissions{$file} = \@new_locks;
7895: } else {
7896: push(@del_keys, $file);
1.613 albertel 7897: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 7898: delete($current_permissions{$file});
1.563 banghart 7899: }
7900: }
1.561 banghart 7901: }
1.613 albertel 7902: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7903: return;
7904: }
1.512 banghart 7905:
1.17 www 7906: # ------------------------------------------------------------ Directory lister
7907:
7908: sub dirlist {
1.955 raeburn 7909: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 7910: $uri=~s/^\///;
7911: $uri=~s/\/$//;
1.253 stredwic 7912: my ($udom, $uname);
1.955 raeburn 7913: if ($getuserdir) {
1.253 stredwic 7914: $udom = $userdomain;
7915: $uname = $username;
1.955 raeburn 7916: } else {
7917: (undef,$udom,$uname)=split(/\//,$uri);
7918: if(defined($userdomain)) {
7919: $udom = $userdomain;
7920: }
7921: if(defined($username)) {
7922: $uname = $username;
7923: }
1.253 stredwic 7924: }
1.955 raeburn 7925: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 7926:
1.955 raeburn 7927: $dirRoot = $perlvar{'lonDocRoot'};
7928: if (defined($getpropath)) {
7929: $dirRoot = &propath($udom,$uname);
1.253 stredwic 7930: $dirRoot =~ s/\/$//;
1.955 raeburn 7931: } elsif (defined($getuserdir)) {
7932: my $subdir=$uname.'__';
7933: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
7934: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
7935: ."/$udom/$subdir/$uname";
7936: } elsif (defined($alternateRoot)) {
7937: $dirRoot = $alternateRoot;
1.751 banghart 7938: }
1.253 stredwic 7939:
7940: if($udom) {
7941: if($uname) {
1.955 raeburn 7942: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 7943: .$getuserdir.':'.&escape($dirRoot)
1.955 raeburn 7944: .':'.&escape($uname).':'.&escape($udom),
7945: &homeserver($uname,$udom));
7946: if ($listing eq 'unknown_cmd') {
7947: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
7948: &homeserver($uname,$udom));
7949: } else {
7950: @listing_results = map { &unescape($_); } split(/:/,$listing);
7951: }
1.605 matthew 7952: if ($listing eq 'unknown_cmd') {
1.800 albertel 7953: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
7954: &homeserver($uname,$udom));
1.605 matthew 7955: @listing_results = split(/:/,$listing);
7956: } else {
7957: @listing_results = map { &unescape($_); } split(/:/,$listing);
7958: }
7959: return @listing_results;
1.955 raeburn 7960: } elsif(!$alternateRoot) {
1.800 albertel 7961: my %allusers;
1.841 albertel 7962: my %servers = &get_servers($udom,'library');
1.955 raeburn 7963: foreach my $tryserver (keys(%servers)) {
7964: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
7965: &escape($udom),$tryserver);
7966: if ($listing eq 'unknown_cmd') {
7967: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
7968: $udom, $tryserver);
7969: } else {
7970: @listing_results = map { &unescape($_); } split(/:/,$listing);
7971: }
1.841 albertel 7972: if ($listing eq 'unknown_cmd') {
7973: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
7974: $udom, $tryserver);
7975: @listing_results = split(/:/,$listing);
7976: } else {
7977: @listing_results =
7978: map { &unescape($_); } split(/:/,$listing);
7979: }
7980: if ($listing_results[0] ne 'no_such_dir' &&
7981: $listing_results[0] ne 'empty' &&
7982: $listing_results[0] ne 'con_lost') {
7983: foreach my $line (@listing_results) {
7984: my ($entry) = split(/&/,$line,2);
7985: $allusers{$entry} = 1;
7986: }
7987: }
1.253 stredwic 7988: }
7989: my $alluserstr='';
1.800 albertel 7990: foreach my $user (sort(keys(%allusers))) {
7991: $alluserstr.=$user.'&user:';
1.253 stredwic 7992: }
7993: $alluserstr=~s/:$//;
7994: return split(/:/,$alluserstr);
7995: } else {
1.800 albertel 7996: return ('missing user name');
1.253 stredwic 7997: }
1.955 raeburn 7998: } elsif(!defined($getpropath)) {
1.841 albertel 7999: my @all_domains = sort(&all_domains());
1.955 raeburn 8000: foreach my $domain (@all_domains) {
8001: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
8002: }
8003: return @all_domains;
8004: } else {
1.800 albertel 8005: return ('missing domain');
1.275 stredwic 8006: }
8007: }
8008:
8009: # --------------------------------------------- GetFileTimestamp
8010: # This function utilizes dirlist and returns the date stamp for
8011: # when it was last modified. It will also return an error of -1
8012: # if an error occurs
8013:
8014: sub GetFileTimestamp {
1.955 raeburn 8015: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 8016: $studentDomain = &LONCAPA::clean_domain($studentDomain);
8017: $studentName = &LONCAPA::clean_username($studentName);
1.955 raeburn 8018: my ($fileStat) =
8019: &Apache::lonnet::dirlist($filename,$studentDomain,$studentName,
8020: undef,$getuserdir);
1.275 stredwic 8021: my @stats = split('&', $fileStat);
8022: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 8023: # @stats contains first the filename, then the stat output
8024: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 8025: } else {
8026: return -1;
1.253 stredwic 8027: }
1.26 www 8028: }
8029:
1.712 albertel 8030: sub stat_file {
8031: my ($uri) = @_;
1.787 albertel 8032: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 8033:
1.955 raeburn 8034: my ($udom,$uname,$file);
1.712 albertel 8035: if ($uri =~ m-^/(uploaded|editupload)/-) {
8036: ($udom,$uname,$file) =
1.811 albertel 8037: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 8038: $file = 'userfiles/'.$file;
8039: }
8040: if ($uri =~ m-^/res/-) {
8041: ($udom,$uname) =
1.807 albertel 8042: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 8043: $file = $uri;
8044: }
8045:
8046: if (!$udom || !$uname || !$file) {
8047: # unable to handle the uri
8048: return ();
8049: }
1.956 raeburn 8050: my $getpropath;
8051: if ($file =~ /^userfiles\//) {
8052: $getpropath = 1;
8053: }
1.955 raeburn 8054: my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712 albertel 8055: my @stats = split('&', $result);
1.721 banghart 8056:
1.712 albertel 8057: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
8058: shift(@stats); #filename is first
8059: return @stats;
8060: }
8061: return ();
8062: }
8063:
1.26 www 8064: # -------------------------------------------------------- Value of a Condition
8065:
1.713 albertel 8066: # gets the value of a specific preevaluated condition
8067: # stored in the string $env{user.state.<cid>}
8068: # or looks up a condition reference in the bighash and if if hasn't
8069: # already been evaluated recurses into docondval to get the value of
8070: # the condition, then memoizing it to
8071: # $env{user.state.<cid>.<condition>}
1.40 www 8072: sub directcondval {
8073: my $number=shift;
1.620 albertel 8074: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 8075: &Apache::lonuserstate::evalstate();
8076: }
1.713 albertel 8077: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
8078: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
8079: } elsif ($number =~ /^_/) {
8080: my $sub_condition;
8081: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8082: &GDBM_READER(),0640)) {
8083: $sub_condition=$bighash{'conditions'.$number};
8084: untie(%bighash);
8085: }
8086: my $value = &docondval($sub_condition);
1.949 raeburn 8087: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 8088: return $value;
8089: }
1.620 albertel 8090: if ($env{'user.state.'.$env{'request.course.id'}}) {
8091: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 8092: } else {
8093: return 2;
8094: }
8095: }
8096:
1.713 albertel 8097: # get the collection of conditions for this resource
1.26 www 8098: sub condval {
8099: my $condidx=shift;
1.54 www 8100: my $allpathcond='';
1.713 albertel 8101: foreach my $cond (split(/\|/,$condidx)) {
8102: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
8103: $allpathcond.=
8104: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
8105: }
1.191 harris41 8106: }
1.54 www 8107: $allpathcond=~s/\|$//;
1.713 albertel 8108: return &docondval($allpathcond);
8109: }
8110:
8111: #evaluates an expression of conditions
8112: sub docondval {
8113: my ($allpathcond) = @_;
8114: my $result=0;
8115: if ($env{'request.course.id'}
8116: && defined($allpathcond)) {
8117: my $operand='|';
8118: my @stack;
8119: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
8120: if ($chunk eq '(') {
8121: push @stack,($operand,$result);
8122: } elsif ($chunk eq ')') {
8123: my $before=pop @stack;
8124: if (pop @stack eq '&') {
8125: $result=$result>$before?$before:$result;
8126: } else {
8127: $result=$result>$before?$result:$before;
8128: }
8129: } elsif (($chunk eq '&') || ($chunk eq '|')) {
8130: $operand=$chunk;
8131: } else {
8132: my $new=directcondval($chunk);
8133: if ($operand eq '&') {
8134: $result=$result>$new?$new:$result;
8135: } else {
8136: $result=$result>$new?$result:$new;
8137: }
8138: }
8139: }
1.26 www 8140: }
8141: return $result;
1.421 albertel 8142: }
8143:
8144: # ---------------------------------------------------- Devalidate courseresdata
8145:
8146: sub devalidatecourseresdata {
8147: my ($coursenum,$coursedomain)=@_;
8148: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8149: &devalidate_cache_new('courseres',$hashid);
1.28 www 8150: }
8151:
1.763 www 8152:
1.200 www 8153: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 8154: #
8155: # Parameters:
8156: # $coursenum - Number of the course.
8157: # $coursedomain - Domain at which the course was created.
8158: # Returns:
8159: # A hash of the course parameters along (I think) with timestamps
8160: # and version info.
1.877 foxr 8161:
1.624 albertel 8162: sub get_courseresdata {
8163: my ($coursenum,$coursedomain)=@_;
1.200 www 8164: my $coursehom=&homeserver($coursenum,$coursedomain);
8165: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 8166: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 8167: my %dumpreply;
1.417 albertel 8168: unless (defined($cached)) {
1.624 albertel 8169: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 8170: $result=\%dumpreply;
1.251 albertel 8171: my ($tmp) = keys(%dumpreply);
8172: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 8173: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 8174: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
8175: return $tmp;
1.416 albertel 8176: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 8177: $result=undef;
1.599 albertel 8178: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 8179: }
8180: }
1.624 albertel 8181: return $result;
8182: }
8183:
1.633 albertel 8184: sub devalidateuserresdata {
8185: my ($uname,$udom)=@_;
8186: my $hashid="$udom:$uname";
8187: &devalidate_cache_new('userres',$hashid);
8188: }
8189:
1.624 albertel 8190: sub get_userresdata {
8191: my ($uname,$udom)=@_;
8192: #most student don\'t have any data set, check if there is some data
8193: if (&EXT_cache_status($udom,$uname)) { return undef; }
8194:
8195: my $hashid="$udom:$uname";
8196: my ($result,$cached)=&is_cached_new('userres',$hashid);
8197: if (!defined($cached)) {
8198: my %resourcedata=&dump('resourcedata',$udom,$uname);
8199: $result=\%resourcedata;
8200: &do_cache_new('userres',$hashid,$result,600);
8201: }
8202: my ($tmp)=keys(%$result);
8203: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
8204: return $result;
8205: }
8206: #error 2 occurs when the .db doesn't exist
8207: if ($tmp!~/error: 2 /) {
1.672 albertel 8208: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 8209: " Trying to get resource data for ".
8210: $uname." at ".$udom.": ".
8211: $tmp."</font>");
8212: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 8213: #&EXT_cache_set($udom,$uname);
8214: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 8215: undef($tmp); # not really an error so don't send it back
1.624 albertel 8216: }
8217: return $tmp;
8218: }
1.879 foxr 8219: #----------------------------------------------- resdata - return resource data
8220: # Purpose:
8221: # Return resource data for either users or for a course.
8222: # Parameters:
8223: # $name - Course/user name.
8224: # $domain - Name of the domain the user/course is registered on.
8225: # $type - Type of thing $name is (must be 'course' or 'user'
8226: # @which - Array of names of resources desired.
8227: # Returns:
8228: # The value of the first reasource in @which that is found in the
8229: # resource hash.
8230: # Exceptional Conditions:
8231: # If the $type passed in is not valid (not the string 'course' or
8232: # 'user', an undefined reference is returned.
8233: # If none of the resources are found, an undef is returned
1.624 albertel 8234: sub resdata {
8235: my ($name,$domain,$type,@which)=@_;
8236: my $result;
8237: if ($type eq 'course') {
8238: $result=&get_courseresdata($name,$domain);
8239: } elsif ($type eq 'user') {
8240: $result=&get_userresdata($name,$domain);
8241: }
8242: if (!ref($result)) { return $result; }
1.251 albertel 8243: foreach my $item (@which) {
1.927 albertel 8244: if (defined($result->{$item->[0]})) {
8245: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 8246: }
1.250 albertel 8247: }
1.291 albertel 8248: return undef;
1.200 www 8249: }
8250:
1.379 matthew 8251: #
8252: # EXT resource caching routines
8253: #
8254:
8255: sub clear_EXT_cache_status {
1.383 albertel 8256: &delenv('cache.EXT.');
1.379 matthew 8257: }
8258:
8259: sub EXT_cache_status {
8260: my ($target_domain,$target_user) = @_;
1.383 albertel 8261: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 8262: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 8263: # We know already the user has no data
8264: return 1;
8265: } else {
8266: return 0;
8267: }
8268: }
8269:
8270: sub EXT_cache_set {
8271: my ($target_domain,$target_user) = @_;
1.383 albertel 8272: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 8273: #&appenv({$cachename => time});
1.379 matthew 8274: }
8275:
1.28 www 8276: # --------------------------------------------------------- Value of a Variable
1.58 www 8277: sub EXT {
1.715 albertel 8278:
1.395 albertel 8279: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 8280: unless ($varname) { return ''; }
1.218 albertel 8281: #get real user name/domain, courseid and symb
8282: my $courseid;
1.359 albertel 8283: my $publicuser;
1.427 www 8284: if ($symbparm) {
8285: $symbparm=&get_symb_from_alias($symbparm);
8286: }
1.218 albertel 8287: if (!($uname && $udom)) {
1.790 albertel 8288: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 8289: if (!$symbparm) { $symbparm=$cursymb; }
8290: } else {
1.620 albertel 8291: $courseid=$env{'request.course.id'};
1.218 albertel 8292: }
1.48 www 8293: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
8294: my $rest;
1.320 albertel 8295: if (defined($therest[0])) {
1.48 www 8296: $rest=join('.',@therest);
8297: } else {
8298: $rest='';
8299: }
1.320 albertel 8300:
1.57 www 8301: my $qualifierrest=$qualifier;
8302: if ($rest) { $qualifierrest.='.'.$rest; }
8303: my $spacequalifierrest=$space;
8304: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 8305: if ($realm eq 'user') {
1.48 www 8306: # --------------------------------------------------------------- user.resource
8307: if ($space eq 'resource') {
1.651 albertel 8308: if ( (defined($Apache::lonhomework::parsing_a_problem)
8309: || defined($Apache::lonhomework::parsing_a_task))
8310: &&
1.744 albertel 8311: ($symbparm eq &symbread()) ) {
8312: # if we are in the middle of processing the resource the
8313: # get the value we are planning on committing
8314: if (defined($Apache::lonhomework::results{$qualifierrest})) {
8315: return $Apache::lonhomework::results{$qualifierrest};
8316: } else {
8317: return $Apache::lonhomework::history{$qualifierrest};
8318: }
1.335 albertel 8319: } else {
1.359 albertel 8320: my %restored;
1.620 albertel 8321: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 8322: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
8323: } else {
8324: %restored=&restore($symbparm,$courseid,$udom,$uname);
8325: }
1.335 albertel 8326: return $restored{$qualifierrest};
8327: }
1.48 www 8328: # ----------------------------------------------------------------- user.access
8329: } elsif ($space eq 'access') {
1.218 albertel 8330: # FIXME - not supporting calls for a specific user
1.48 www 8331: return &allowed($qualifier,$rest);
8332: # ------------------------------------------ user.preferences, user.environment
8333: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 8334: if (($uname eq $env{'user.name'}) &&
8335: ($udom eq $env{'user.domain'})) {
8336: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 8337: } else {
1.359 albertel 8338: my %returnhash;
8339: if (!$publicuser) {
8340: %returnhash=&userenvironment($udom,$uname,
8341: $qualifierrest);
8342: }
1.218 albertel 8343: return $returnhash{$qualifierrest};
8344: }
1.48 www 8345: # ----------------------------------------------------------------- user.course
8346: } elsif ($space eq 'course') {
1.218 albertel 8347: # FIXME - not supporting calls for a specific user
1.620 albertel 8348: return $env{join('.',('request.course',$qualifier))};
1.48 www 8349: # ------------------------------------------------------------------- user.role
8350: } elsif ($space eq 'role') {
1.218 albertel 8351: # FIXME - not supporting calls for a specific user
1.620 albertel 8352: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 8353: if ($qualifier eq 'value') {
8354: return $role;
8355: } elsif ($qualifier eq 'extent') {
8356: return $where;
8357: }
8358: # ----------------------------------------------------------------- user.domain
8359: } elsif ($space eq 'domain') {
1.218 albertel 8360: return $udom;
1.48 www 8361: # ------------------------------------------------------------------- user.name
8362: } elsif ($space eq 'name') {
1.218 albertel 8363: return $uname;
1.48 www 8364: # ---------------------------------------------------- Any other user namespace
1.29 www 8365: } else {
1.359 albertel 8366: my %reply;
8367: if (!$publicuser) {
8368: %reply=&get($space,[$qualifierrest],$udom,$uname);
8369: }
8370: return $reply{$qualifierrest};
1.48 www 8371: }
1.236 www 8372: } elsif ($realm eq 'query') {
8373: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 8374: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
8375: [$spacequalifierrest]);
1.620 albertel 8376: return $env{'form.'.$spacequalifierrest};
1.236 www 8377: } elsif ($realm eq 'request') {
1.48 www 8378: # ------------------------------------------------------------- request.browser
8379: if ($space eq 'browser') {
1.430 www 8380: if ($qualifier eq 'textremote') {
1.676 albertel 8381: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 8382: return 1;
8383: } else {
8384: return 0;
8385: }
8386: } else {
1.620 albertel 8387: return $env{'browser.'.$qualifier};
1.430 www 8388: }
1.57 www 8389: # ------------------------------------------------------------ request.filename
8390: } else {
1.620 albertel 8391: return $env{'request.'.$spacequalifierrest};
1.29 www 8392: }
1.28 www 8393: } elsif ($realm eq 'course') {
1.48 www 8394: # ---------------------------------------------------------- course.description
1.620 albertel 8395: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 8396: } elsif ($realm eq 'resource') {
1.165 www 8397:
1.620 albertel 8398: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 8399: if (!$symbparm) { $symbparm=&symbread(); }
8400: }
1.693 albertel 8401:
8402: if ($space eq 'title') {
8403: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
8404: return &gettitle($symbparm);
8405: }
8406:
8407: if ($space eq 'map') {
8408: my ($map) = &decode_symb($symbparm);
8409: return &symbread($map);
8410: }
1.905 albertel 8411: if ($space eq 'filename') {
8412: if ($symbparm) {
8413: return &clutter((&decode_symb($symbparm))[2]);
8414: }
8415: return &hreflocation('',$env{'request.filename'});
8416: }
1.693 albertel 8417:
8418: my ($section, $group, @groups);
1.593 albertel 8419: my ($courselevelm,$courselevel);
1.539 albertel 8420: if ($symbparm && defined($courseid) &&
1.620 albertel 8421: $courseid eq $env{'request.course.id'}) {
1.165 www 8422:
1.218 albertel 8423: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 8424:
1.60 www 8425: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 8426: my $symbp=$symbparm;
1.735 albertel 8427: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 8428:
8429: my $symbparm=$symbp.'.'.$spacequalifierrest;
8430: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
8431:
1.620 albertel 8432: if (($env{'user.name'} eq $uname) &&
8433: ($env{'user.domain'} eq $udom)) {
8434: $section=$env{'request.course.sec'};
1.733 raeburn 8435: @groups = split(/:/,$env{'request.course.groups'});
8436: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 8437: } else {
1.539 albertel 8438: if (! defined($usection)) {
1.551 albertel 8439: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 8440: } else {
8441: $section = $usection;
8442: }
1.733 raeburn 8443: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 8444: }
8445:
8446: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
8447: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
8448: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
8449:
1.593 albertel 8450: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 8451: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 8452: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 8453:
1.60 www 8454: # ----------------------------------------------------------- first, check user
1.624 albertel 8455:
8456: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 8457: ([$courselevelr,'resource'],
8458: [$courselevelm,'map' ],
8459: [$courselevel, 'course' ]));
1.931 albertel 8460: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 8461:
1.594 albertel 8462: # ------------------------------------------------ second, check some of course
1.684 raeburn 8463: my $coursereply;
1.691 raeburn 8464: if (@groups > 0) {
8465: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
8466: $mapparm,$spacequalifierrest);
1.927 albertel 8467: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 8468: }
1.96 www 8469:
1.684 raeburn 8470: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 8471: $env{'course.'.$courseid.'.domain'},
8472: 'course',
8473: ([$seclevelr, 'resource'],
8474: [$seclevelm, 'map' ],
8475: [$seclevel, 'course' ],
8476: [$courselevelr,'resource']));
8477: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 8478:
1.60 www 8479: # ------------------------------------------------------ third, check map parms
1.218 albertel 8480: my %parmhash=();
8481: my $thisparm='';
8482: if (tie(%parmhash,'GDBM_File',
1.620 albertel 8483: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 8484: &GDBM_READER(),0640)) {
1.218 albertel 8485: $thisparm=$parmhash{$symbparm};
8486: untie(%parmhash);
8487: }
1.927 albertel 8488: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 8489: }
1.594 albertel 8490: # ------------------------------------------ fourth, look in resource metadata
1.71 www 8491:
1.218 albertel 8492: $spacequalifierrest=~s/\./\_/;
1.282 albertel 8493: my $filename;
8494: if (!$symbparm) { $symbparm=&symbread(); }
8495: if ($symbparm) {
1.409 www 8496: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 8497: } else {
1.620 albertel 8498: $filename=$env{'request.filename'};
1.282 albertel 8499: }
8500: my $metadata=&metadata($filename,$spacequalifierrest);
1.927 albertel 8501: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282 albertel 8502: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927 albertel 8503: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 8504:
1.927 albertel 8505: # ---------------------------------------------- fourth, look in rest of course
1.593 albertel 8506: if ($symbparm && defined($courseid) &&
1.620 albertel 8507: $courseid eq $env{'request.course.id'}) {
1.624 albertel 8508: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
8509: $env{'course.'.$courseid.'.domain'},
8510: 'course',
1.927 albertel 8511: ([$courselevelm,'map' ],
8512: [$courselevel, 'course']));
8513: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 8514: }
1.145 www 8515: # ------------------------------------------------------------------ Cascade up
1.218 albertel 8516: unless ($space eq '0') {
1.336 albertel 8517: my @parts=split(/_/,$space);
8518: my $id=pop(@parts);
8519: my $part=join('_',@parts);
8520: if ($part eq '') { $part='0'; }
1.927 albertel 8521: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 8522: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 8523: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 8524: }
1.395 albertel 8525: if ($recurse) { return undef; }
8526: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 8527: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 8528: # ---------------------------------------------------- Any other user namespace
8529: } elsif ($realm eq 'environment') {
8530: # ----------------------------------------------------------------- environment
1.620 albertel 8531: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
8532: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 8533: } else {
1.770 albertel 8534: if ($uname eq 'anonymous' && $udom eq '') {
8535: return '';
8536: }
1.219 albertel 8537: my %returnhash=&userenvironment($udom,$uname,
8538: $spacequalifierrest);
8539: return $returnhash{$spacequalifierrest};
8540: }
1.28 www 8541: } elsif ($realm eq 'system') {
1.48 www 8542: # ----------------------------------------------------------------- system.time
8543: if ($space eq 'time') {
8544: return time;
8545: }
1.696 albertel 8546: } elsif ($realm eq 'server') {
8547: # ----------------------------------------------------------------- system.time
8548: if ($space eq 'name') {
8549: return $ENV{'SERVER_NAME'};
8550: }
1.28 www 8551: }
1.48 www 8552: return '';
1.61 www 8553: }
8554:
1.927 albertel 8555: sub get_reply {
8556: my ($reply_value) = @_;
1.940 raeburn 8557: if (ref($reply_value) eq 'ARRAY') {
8558: if (wantarray) {
8559: return @$reply_value;
8560: }
8561: return $reply_value->[0];
8562: } else {
8563: return $reply_value;
1.927 albertel 8564: }
8565: }
8566:
1.691 raeburn 8567: sub check_group_parms {
8568: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
8569: my @groupitems = ();
8570: my $resultitem;
1.927 albertel 8571: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 8572: foreach my $group (@{$groups}) {
8573: foreach my $level (@levels) {
1.927 albertel 8574: my $item = $courseid.'.['.$group.'].'.$level->[0];
8575: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 8576: }
8577: }
8578: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
8579: $env{'course.'.$courseid.'.domain'},
8580: 'course',@groupitems);
8581: return $coursereply;
8582: }
8583:
8584: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 8585: my ($courseid,@groups) = @_;
8586: @groups = sort(@groups);
1.691 raeburn 8587: return @groups;
8588: }
8589:
1.395 albertel 8590: sub packages_tab_default {
8591: my ($uri,$varname)=@_;
8592: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 8593:
8594: my (@extension,@specifics,$do_default);
8595: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 8596: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 8597: if ($pack_type eq 'default') {
8598: $do_default=1;
8599: } elsif ($pack_type eq 'extension') {
8600: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 8601: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 8602: # only look at packages defaults for packages that this id is
1.738 albertel 8603: push(@specifics,[$package,$pack_type,$pack_part]);
8604: }
8605: }
8606: # first look for a package that matches the requested part id
8607: foreach my $package (@specifics) {
8608: my (undef,$pack_type,$pack_part)=@{$package};
8609: next if ($pack_part ne $part);
8610: if (defined($packagetab{"$pack_type&$name&default"})) {
8611: return $packagetab{"$pack_type&$name&default"};
8612: }
8613: }
8614: # look for any possible matching non extension_ package
8615: foreach my $package (@specifics) {
8616: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 8617: if (defined($packagetab{"$pack_type&$name&default"})) {
8618: return $packagetab{"$pack_type&$name&default"};
8619: }
1.585 albertel 8620: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 8621: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
8622: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 8623: }
8624: }
1.738 albertel 8625: # look for any posible extension_ match
8626: foreach my $package (@extension) {
8627: my ($package,$pack_type)=@{$package};
8628: if (defined($packagetab{"$pack_type&$name&default"})) {
8629: return $packagetab{"$pack_type&$name&default"};
8630: }
8631: if (defined($packagetab{$package."&$name&default"})) {
8632: return $packagetab{$package."&$name&default"};
8633: }
8634: }
8635: # look for a global default setting
8636: if ($do_default && defined($packagetab{"default&$name&default"})) {
8637: return $packagetab{"default&$name&default"};
8638: }
1.395 albertel 8639: return undef;
8640: }
8641:
1.334 albertel 8642: sub add_prefix_and_part {
8643: my ($prefix,$part)=@_;
8644: my $keyroot;
8645: if (defined($prefix) && $prefix !~ /^__/) {
8646: # prefix that has a part already
8647: $keyroot=$prefix;
8648: } elsif (defined($prefix)) {
8649: # prefix that is missing a part
8650: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
8651: } else {
8652: # no prefix at all
8653: if (defined($part)) { $keyroot='_'.$part; }
8654: }
8655: return $keyroot;
8656: }
8657:
1.71 www 8658: # ---------------------------------------------------------------- Get metadata
8659:
1.599 albertel 8660: my %metaentry;
1.71 www 8661: sub metadata {
1.176 www 8662: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 8663: $uri=&declutter($uri);
1.288 albertel 8664: # if it is a non metadata possible uri return quickly
1.529 albertel 8665: if (($uri eq '') ||
8666: (($uri =~ m|^/*adm/|) &&
1.698 albertel 8667: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1056.4.26 raeburn 8668: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924 albertel 8669: return undef;
8670: }
8671: if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/})
8672: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 8673: return undef;
1.288 albertel 8674: }
1.73 www 8675: my $filename=$uri;
8676: $uri=~s/\.meta$//;
1.172 www 8677: #
8678: # Is the metadata already cached?
1.177 www 8679: # Look at timestamp of caching
1.172 www 8680: # Everything is cached by the main uri, libraries are never directly cached
8681: #
1.428 albertel 8682: if (!defined($liburi)) {
1.599 albertel 8683: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 8684: if (defined($cached)) { return $result->{':'.$what}; }
8685: }
8686: {
1.172 www 8687: #
8688: # Is this a recursive call for a library?
8689: #
1.599 albertel 8690: # if (! exists($metacache{$uri})) {
8691: # $metacache{$uri}={};
8692: # }
1.924 albertel 8693: my $cachetime = 60*60;
1.171 www 8694: if ($liburi) {
8695: $liburi=&declutter($liburi);
8696: $filename=$liburi;
1.401 bowersj2 8697: } else {
1.599 albertel 8698: &devalidate_cache_new('meta',$uri);
8699: undef(%metaentry);
1.401 bowersj2 8700: }
1.140 www 8701: my %metathesekeys=();
1.73 www 8702: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 8703: my $metastring;
1.924 albertel 8704: if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929 albertel 8705: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 8706: $metastring =
1.929 albertel 8707: &Apache::lonnet::ssi_body($which,
1.924 albertel 8708: ('grade_target' => 'meta'));
8709: $cachetime = 1; # only want this cached in the child not long term
1.1056.4.26 raeburn 8710: } elsif (($uri !~ m -^(editupload)/-) &&
8711: ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543 albertel 8712: my $file=&filelocation('',&clutter($filename));
1.599 albertel 8713: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 8714: $metastring=&getfile($file);
1.489 albertel 8715: }
1.208 albertel 8716: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 8717: my $token;
1.140 www 8718: undef %metathesekeys;
1.71 www 8719: while ($token=$parser->get_token) {
1.339 albertel 8720: if ($token->[0] eq 'S') {
8721: if (defined($token->[2]->{'package'})) {
1.172 www 8722: #
8723: # This is a package - get package info
8724: #
1.339 albertel 8725: my $package=$token->[2]->{'package'};
8726: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8727: if (defined($token->[2]->{'id'})) {
8728: $keyroot.='_'.$token->[2]->{'id'};
8729: }
1.599 albertel 8730: if ($metaentry{':packages'}) {
8731: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 8732: } else {
1.599 albertel 8733: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 8734: }
1.736 albertel 8735: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 8736: my $part=$keyroot;
8737: $part=~s/^\_//;
1.736 albertel 8738: if ($pack_entry=~/^\Q$package\E\&/ ||
8739: $pack_entry=~/^\Q$package\E_0\&/) {
8740: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 8741: # ignore package.tab specified default values
8742: # here &package_tab_default() will fetch those
8743: if ($subp eq 'default') { next; }
1.736 albertel 8744: my $value=$packagetab{$pack_entry};
1.432 albertel 8745: my $unikey;
8746: if ($pack =~ /_0$/) {
8747: $unikey='parameter_0_'.$name;
8748: $part=0;
8749: } else {
8750: $unikey='parameter'.$keyroot.'_'.$name;
8751: }
1.339 albertel 8752: if ($subp eq 'display') {
8753: $value.=' [Part: '.$part.']';
8754: }
1.599 albertel 8755: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 8756: $metathesekeys{$unikey}=1;
1.599 albertel 8757: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8758: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 8759: }
1.599 albertel 8760: if (defined($metaentry{':'.$unikey.'.default'})) {
8761: $metaentry{':'.$unikey}=
8762: $metaentry{':'.$unikey.'.default'};
1.356 albertel 8763: }
1.339 albertel 8764: }
8765: }
8766: } else {
1.172 www 8767: #
8768: # This is not a package - some other kind of start tag
1.339 albertel 8769: #
8770: my $entry=$token->[1];
8771: my $unikey;
8772: if ($entry eq 'import') {
8773: $unikey='';
8774: } else {
8775: $unikey=$entry;
8776: }
8777: $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8778:
8779: if (defined($token->[2]->{'id'})) {
8780: $unikey.='_'.$token->[2]->{'id'};
8781: }
1.175 www 8782:
1.339 albertel 8783: if ($entry eq 'import') {
1.175 www 8784: #
8785: # Importing a library here
1.339 albertel 8786: #
8787: if ($depthcount<20) {
8788: my $location=$parser->get_text('/import');
8789: my $dir=$filename;
8790: $dir=~s|[^/]*$||;
8791: $location=&filelocation($dir,$location);
1.736 albertel 8792: my $metadata =
8793: &metadata($uri,'keys', $location,$unikey,
8794: $depthcount+1);
8795: foreach my $meta (split(',',$metadata)) {
8796: $metaentry{':'.$meta}=$metaentry{':'.$meta};
8797: $metathesekeys{$meta}=1;
1.339 albertel 8798: }
8799: }
8800: } else {
8801: if (defined($token->[2]->{'name'})) {
8802: $unikey.='_'.$token->[2]->{'name'};
8803: }
8804: $metathesekeys{$unikey}=1;
1.736 albertel 8805: foreach my $param (@{$token->[3]}) {
8806: $metaentry{':'.$unikey.'.'.$param} =
8807: $token->[2]->{$param};
1.339 albertel 8808: }
8809: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 8810: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 8811: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
8812: # only ws inside the tag, and not in default, so use default
8813: # as value
1.599 albertel 8814: $metaentry{':'.$unikey}=$default;
1.908 albertel 8815: } elsif ( $internaltext =~ /\S/ ) {
8816: # something interesting inside the tag
8817: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 8818: } else {
1.908 albertel 8819: # no interesting values, don't set a default
1.339 albertel 8820: }
1.172 www 8821: # end of not-a-package not-a-library import
1.339 albertel 8822: }
1.172 www 8823: # end of not-a-package start tag
1.339 albertel 8824: }
1.172 www 8825: # the next is the end of "start tag"
1.339 albertel 8826: }
8827: }
1.483 albertel 8828: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 8829: $extension = lc($extension);
8830: if ($extension eq 'htm') { $extension='html'; }
8831:
1.737 albertel 8832: foreach my $key (keys(%packagetab)) {
1.483 albertel 8833: #no specific packages #how's our extension
8834: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 8835: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 8836: \%metathesekeys);
8837: }
1.883 albertel 8838:
8839: if (!exists($metaentry{':packages'})
8840: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 8841: foreach my $key (keys(%packagetab)) {
1.483 albertel 8842: #no specific packages well let's get default then
8843: if ($key!~/^default&/) { next; }
1.488 albertel 8844: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 8845: \%metathesekeys);
8846: }
8847: }
1.338 www 8848: # are there custom rights to evaluate
1.599 albertel 8849: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 8850:
1.338 www 8851: #
8852: # Importing a rights file here
1.339 albertel 8853: #
8854: unless ($depthcount) {
1.599 albertel 8855: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 8856: my $dir=$filename;
8857: $dir=~s|[^/]*$||;
8858: $location=&filelocation($dir,$location);
1.736 albertel 8859: my $rights_metadata =
8860: &metadata($uri,'keys',$location,'_rights',
8861: $depthcount+1);
8862: foreach my $rights (split(',',$rights_metadata)) {
8863: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
8864: $metathesekeys{$rights}=1;
1.339 albertel 8865: }
8866: }
8867: }
1.737 albertel 8868: # uniqifiy package listing
8869: my %seen;
8870: my @uniq_packages =
8871: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
8872: $metaentry{':packages'} = join(',',@uniq_packages);
8873:
8874: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 8875: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
8876: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924 albertel 8877: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 8878: # this is the end of "was not already recently cached
1.71 www 8879: }
1.599 albertel 8880: return $metaentry{':'.$what};
1.261 albertel 8881: }
8882:
1.488 albertel 8883: sub metadata_create_package_def {
1.483 albertel 8884: my ($uri,$key,$package,$metathesekeys)=@_;
8885: my ($pack,$name,$subp)=split(/\&/,$key);
8886: if ($subp eq 'default') { next; }
8887:
1.599 albertel 8888: if (defined($metaentry{':packages'})) {
8889: $metaentry{':packages'}.=','.$package;
1.483 albertel 8890: } else {
1.599 albertel 8891: $metaentry{':packages'}=$package;
1.483 albertel 8892: }
8893: my $value=$packagetab{$key};
8894: my $unikey;
8895: $unikey='parameter_0_'.$name;
1.599 albertel 8896: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 8897: $$metathesekeys{$unikey}=1;
1.599 albertel 8898: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8899: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 8900: }
1.599 albertel 8901: if (defined($metaentry{':'.$unikey.'.default'})) {
8902: $metaentry{':'.$unikey}=
8903: $metaentry{':'.$unikey.'.default'};
1.483 albertel 8904: }
8905: }
8906:
1.261 albertel 8907: sub metadata_generate_part0 {
8908: my ($metadata,$metacache,$uri) = @_;
8909: my %allnames;
1.737 albertel 8910: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 8911: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 8912: my $part=$$metacache{':'.$metakey.'.part'};
8913: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 8914: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 8915: $allnames{$name}=$part;
8916: }
8917: }
8918: }
8919: foreach my $name (keys(%allnames)) {
8920: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 8921: my $key=":parameter_0_$name";
1.261 albertel 8922: $$metacache{"$key.part"}='0';
8923: $$metacache{"$key.name"}=$name;
1.428 albertel 8924: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 8925: $allnames{$name}.'_'.$name.
8926: '.type'};
1.428 albertel 8927: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 8928: '.display'};
1.644 www 8929: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 8930: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 8931: $$metacache{"$key.display"}=$olddis;
8932: }
1.71 www 8933: }
8934:
1.764 albertel 8935: # ------------------------------------------------------ Devalidate title cache
8936:
8937: sub devalidate_title_cache {
8938: my ($url)=@_;
8939: if (!$env{'request.course.id'}) { return; }
8940: my $symb=&symbread($url);
8941: if (!$symb) { return; }
8942: my $key=$env{'request.course.id'}."\0".$symb;
8943: &devalidate_cache_new('title',$key);
8944: }
8945:
1.1014 droeschl 8946: # ------------------------------------------------- Get the title of a course
8947:
8948: sub current_course_title {
8949: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
8950: }
1.301 www 8951: # ------------------------------------------------- Get the title of a resource
8952:
8953: sub gettitle {
8954: my $urlsymb=shift;
8955: my $symb=&symbread($urlsymb);
1.534 albertel 8956: if ($symb) {
1.620 albertel 8957: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 8958: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 8959: if (defined($cached)) {
8960: return $result;
8961: }
1.534 albertel 8962: my ($map,$resid,$url)=&decode_symb($symb);
8963: my $title='';
1.907 albertel 8964: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
8965: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
8966: } else {
8967: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8968: &GDBM_READER(),0640)) {
8969: my $mapid=$bighash{'map_pc_'.&clutter($map)};
8970: $title=$bighash{'title_'.$mapid.'.'.$resid};
8971: untie(%bighash);
8972: }
1.534 albertel 8973: }
8974: $title=~s/\&colon\;/\:/gs;
8975: if ($title) {
1.599 albertel 8976: return &do_cache_new('title',$key,$title,600);
1.534 albertel 8977: }
8978: $urlsymb=$url;
8979: }
8980: my $title=&metadata($urlsymb,'title');
8981: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
8982: return $title;
1.301 www 8983: }
1.613 albertel 8984:
1.614 albertel 8985: sub get_slot {
8986: my ($which,$cnum,$cdom)=@_;
8987: if (!$cnum || !$cdom) {
1.790 albertel 8988: (undef,my $courseid)=&whichuser();
1.620 albertel 8989: $cdom=$env{'course.'.$courseid.'.domain'};
8990: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 8991: }
1.703 albertel 8992: my $key=join("\0",'slots',$cdom,$cnum,$which);
8993: my %slotinfo;
8994: if (exists($remembered{$key})) {
8995: $slotinfo{$which} = $remembered{$key};
8996: } else {
8997: %slotinfo=&get('slots',[$which],$cdom,$cnum);
8998: &Apache::lonhomework::showhash(%slotinfo);
8999: my ($tmp)=keys(%slotinfo);
9000: if ($tmp=~/^error:/) { return (); }
9001: $remembered{$key} = $slotinfo{$which};
9002: }
1.616 albertel 9003: if (ref($slotinfo{$which}) eq 'HASH') {
9004: return %{$slotinfo{$which}};
9005: }
9006: return $slotinfo{$which};
1.614 albertel 9007: }
1.31 www 9008: # ------------------------------------------------- Update symbolic store links
9009:
9010: sub symblist {
9011: my ($mapname,%newhash)=@_;
1.438 www 9012: $mapname=&deversion(&declutter($mapname));
1.31 www 9013: my %hash;
1.620 albertel 9014: if (($env{'request.course.fn'}) && (%newhash)) {
9015: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 9016: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 9017: foreach my $url (keys(%newhash)) {
1.711 albertel 9018: next if ($url eq 'last_known'
9019: && $env{'form.no_update_last_known'});
9020: $hash{declutter($url)}=&encode_symb($mapname,
9021: $newhash{$url}->[1],
9022: $newhash{$url}->[0]);
1.191 harris41 9023: }
1.31 www 9024: if (untie(%hash)) {
9025: return 'ok';
9026: }
9027: }
9028: }
9029: return 'error';
1.212 www 9030: }
9031:
9032: # --------------------------------------------------------------- Verify a symb
9033:
9034: sub symbverify {
1.510 www 9035: my ($symb,$thisurl)=@_;
9036: my $thisfn=$thisurl;
1.439 www 9037: $thisfn=&declutter($thisfn);
1.215 www 9038: # direct jump to resource in page or to a sequence - will construct own symbs
9039: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
9040: # check URL part
1.409 www 9041: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 9042:
1.431 www 9043: unless ($url eq $thisfn) { return 0; }
1.213 www 9044:
1.216 www 9045: $symb=&symbclean($symb);
1.510 www 9046: $thisurl=&deversion($thisurl);
1.439 www 9047: $thisfn=&deversion($thisfn);
1.213 www 9048:
9049: my %bighash;
9050: my $okay=0;
1.431 www 9051:
1.620 albertel 9052: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9053: &GDBM_READER(),0640)) {
1.1032 raeburn 9054: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
9055: $thisurl =~ s/\?.+$//;
9056: }
1.510 www 9057: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 9058: unless ($ids) {
1.1056.4.21 raeburn 9059: my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;
9060: $ids=$bighash{$idkey};
1.216 www 9061: }
9062: if ($ids) {
9063: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 9064: foreach my $id (split(/\,/,$ids)) {
9065: my ($mapid,$resid)=split(/\./,$id);
1.1032 raeburn 9066: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
9067: $symb =~ s/\?.+$//;
9068: }
1.216 www 9069: if (
9070: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
9071: eq $symb) {
1.620 albertel 9072: if (($env{'request.role.adv'}) ||
1.1056.4.20 raeburn 9073: ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
9074: ($thisurl eq '/adm/navmaps')) {
1.582 albertel 9075: $okay=1;
9076: }
9077: }
1.216 www 9078: }
9079: }
1.213 www 9080: untie(%bighash);
9081: }
9082: return $okay;
1.31 www 9083: }
9084:
1.210 www 9085: # --------------------------------------------------------------- Clean-up symb
9086:
9087: sub symbclean {
9088: my $symb=shift;
1.568 albertel 9089: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 9090: # remove version from map
9091: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 9092:
1.210 www 9093: # remove version from URL
9094: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 9095:
1.507 www 9096: # remove wrapper
9097:
1.510 www 9098: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 9099: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 9100: return $symb;
1.409 www 9101: }
9102:
9103: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 9104:
9105: sub encode_symb {
9106: my ($map,$resid,$url)=@_;
9107: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
9108: }
1.409 www 9109:
9110: sub decode_symb {
1.568 albertel 9111: my $symb=shift;
9112: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
9113: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 9114: return (&fixversion($map),$resid,&fixversion($url));
9115: }
9116:
9117: sub fixversion {
9118: my $fn=shift;
1.609 banghart 9119: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 9120: my %bighash;
9121: my $uri=&clutter($fn);
1.620 albertel 9122: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 9123: # is this cached?
1.599 albertel 9124: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 9125: if (defined($cached)) { return $result; }
9126: # unfortunately not cached, or expired
1.620 albertel 9127: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 9128: &GDBM_READER(),0640)) {
9129: if ($bighash{'version_'.$uri}) {
9130: my $version=$bighash{'version_'.$uri};
1.444 www 9131: unless (($version eq 'mostrecent') ||
9132: ($version==&getversion($uri))) {
1.440 www 9133: $uri=~s/\.(\w+)$/\.$version\.$1/;
9134: }
9135: }
9136: untie %bighash;
1.413 www 9137: }
1.599 albertel 9138: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 9139: }
9140:
9141: sub deversion {
9142: my $url=shift;
9143: $url=~s/\.\d+\.(\w+)$/\.$1/;
9144: return $url;
1.210 www 9145: }
9146:
1.31 www 9147: # ------------------------------------------------------ Return symb list entry
9148:
9149: sub symbread {
1.249 www 9150: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 9151: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 9152: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 9153: # no filename provided? try from environment
1.44 www 9154: unless ($thisfn) {
1.620 albertel 9155: if ($env{'request.symb'}) {
9156: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 9157: }
1.620 albertel 9158: $thisfn=$env{'request.filename'};
1.44 www 9159: }
1.569 albertel 9160: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 9161: # is that filename actually a symb? Verify, clean, and return
9162: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 9163: if (&symbverify($thisfn,$1)) {
1.620 albertel 9164: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 9165: }
1.242 www 9166: }
1.44 www 9167: $thisfn=declutter($thisfn);
1.31 www 9168: my %hash;
1.37 www 9169: my %bighash;
9170: my $syval='';
1.620 albertel 9171: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 9172: my $targetfn = $thisfn;
1.609 banghart 9173: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 9174: $targetfn = 'adm/wrapper/'.$thisfn;
9175: }
1.687 albertel 9176: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
9177: $targetfn=$1;
9178: }
1.620 albertel 9179: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 9180: &GDBM_READER(),0640)) {
1.481 raeburn 9181: $syval=$hash{$targetfn};
1.37 www 9182: untie(%hash);
9183: }
9184: # ---------------------------------------------------------- There was an entry
9185: if ($syval) {
1.601 albertel 9186: #unless ($syval=~/\_\d+$/) {
1.620 albertel 9187: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 9188: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9189: #return $env{$cache_str}='';
1.601 albertel 9190: #}
9191: #$syval.=$1;
9192: #}
1.37 www 9193: } else {
9194: # ------------------------------------------------------- Was not in symb table
1.620 albertel 9195: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 9196: &GDBM_READER(),0640)) {
1.37 www 9197: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 9198: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 9199: unless ($ids) {
9200: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 9201: }
9202: unless ($ids) {
9203: # alias?
9204: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 9205: }
1.37 www 9206: if ($ids) {
9207: # ------------------------------------------------------------------- Has ID(s)
9208: my @possibilities=split(/\,/,$ids);
1.39 www 9209: if ($#possibilities==0) {
9210: # ----------------------------------------------- There is only one possibility
1.37 www 9211: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 9212: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9213: $resid,$thisfn);
1.249 www 9214: } elsif (!$donotrecurse) {
1.39 www 9215: # ------------------------------------------ There is more than one possibility
9216: my $realpossible=0;
1.800 albertel 9217: foreach my $id (@possibilities) {
9218: my $file=$bighash{'src_'.$id};
1.39 www 9219: if (&allowed('bre',$file)) {
1.800 albertel 9220: my ($mapid,$resid)=split(/\./,$id);
1.39 www 9221: if ($bighash{'map_type_'.$mapid} ne 'page') {
9222: $realpossible++;
1.626 albertel 9223: $syval=&encode_symb($bighash{'map_id_'.$mapid},
9224: $resid,$thisfn);
1.39 www 9225: }
9226: }
1.191 harris41 9227: }
1.39 www 9228: if ($realpossible!=1) { $syval=''; }
1.249 www 9229: } else {
9230: $syval='';
1.37 www 9231: }
9232: }
9233: untie(%bighash)
1.481 raeburn 9234: }
1.31 www 9235: }
1.62 www 9236: if ($syval) {
1.620 albertel 9237: return $env{$cache_str}=$syval;
1.62 www 9238: }
1.31 www 9239: }
1.949 raeburn 9240: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 9241: return $env{$cache_str}='';
1.31 www 9242: }
9243:
9244: # ---------------------------------------------------------- Return random seed
9245:
1.32 www 9246: sub numval {
9247: my $txt=shift;
9248: $txt=~tr/A-J/0-9/;
9249: $txt=~tr/a-j/0-9/;
9250: $txt=~tr/K-T/0-9/;
9251: $txt=~tr/k-t/0-9/;
9252: $txt=~tr/U-Z/0-5/;
9253: $txt=~tr/u-z/0-5/;
9254: $txt=~s/\D//g;
1.564 albertel 9255: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 9256: return int($txt);
1.368 albertel 9257: }
9258:
1.484 albertel 9259: sub numval2 {
9260: my $txt=shift;
9261: $txt=~tr/A-J/0-9/;
9262: $txt=~tr/a-j/0-9/;
9263: $txt=~tr/K-T/0-9/;
9264: $txt=~tr/k-t/0-9/;
9265: $txt=~tr/U-Z/0-5/;
9266: $txt=~tr/u-z/0-5/;
9267: $txt=~s/\D//g;
9268: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9269: my $total;
9270: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 9271: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 9272: return int($total);
9273: }
9274:
1.575 albertel 9275: sub numval3 {
9276: use integer;
9277: my $txt=shift;
9278: $txt=~tr/A-J/0-9/;
9279: $txt=~tr/a-j/0-9/;
9280: $txt=~tr/K-T/0-9/;
9281: $txt=~tr/k-t/0-9/;
9282: $txt=~tr/U-Z/0-5/;
9283: $txt=~tr/u-z/0-5/;
9284: $txt=~s/\D//g;
9285: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
9286: my $total;
9287: foreach my $val (@txts) { $total+=$val; }
9288: if ($_64bit) { $total=(($total<<32)>>32); }
9289: return $total;
9290: }
9291:
1.675 albertel 9292: sub digest {
9293: my ($data)=@_;
9294: my $digest=&Digest::MD5::md5($data);
9295: my ($a,$b,$c,$d)=unpack("iiii",$digest);
9296: my ($e,$f);
9297: {
9298: use integer;
9299: $e=($a+$b);
9300: $f=($c+$d);
9301: if ($_64bit) {
9302: $e=(($e<<32)>>32);
9303: $f=(($f<<32)>>32);
9304: }
9305: }
9306: if (wantarray) {
9307: return ($e,$f);
9308: } else {
9309: my $g;
9310: {
9311: use integer;
9312: $g=($e+$f);
9313: if ($_64bit) {
9314: $g=(($g<<32)>>32);
9315: }
9316: }
9317: return $g;
9318: }
9319: }
9320:
1.368 albertel 9321: sub latest_rnd_algorithm_id {
1.675 albertel 9322: return '64bit5';
1.366 albertel 9323: }
1.32 www 9324:
1.503 albertel 9325: sub get_rand_alg {
9326: my ($courseid)=@_;
1.790 albertel 9327: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 9328: if ($courseid) {
1.620 albertel 9329: return $env{"course.$courseid.rndseed"};
1.503 albertel 9330: }
9331: return &latest_rnd_algorithm_id();
9332: }
9333:
1.562 albertel 9334: sub validCODE {
9335: my ($CODE)=@_;
9336: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
9337: return 0;
9338: }
9339:
1.491 albertel 9340: sub getCODE {
1.620 albertel 9341: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 9342: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
9343: defined($Apache::lonhomework::parsing_a_task) ) &&
9344: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 9345: return $Apache::lonhomework::history{'resource.CODE'};
9346: }
9347: return undef;
9348: }
9349:
1.31 www 9350: sub rndseed {
1.155 albertel 9351: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 9352: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 9353: if (!defined($symb)) {
1.366 albertel 9354: unless ($symb=$wsymb) { return time; }
9355: }
9356: if (!$courseid) { $courseid=$wcourseid; }
9357: if (!$domain) { $domain=$wdomain; }
9358: if (!$username) { $username=$wusername }
1.503 albertel 9359: my $which=&get_rand_alg();
1.803 albertel 9360:
1.491 albertel 9361: if (defined(&getCODE())) {
1.675 albertel 9362: if ($which eq '64bit5') {
9363: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
9364: } elsif ($which eq '64bit4') {
1.575 albertel 9365: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
9366: } else {
9367: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
9368: }
1.675 albertel 9369: } elsif ($which eq '64bit5') {
9370: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 9371: } elsif ($which eq '64bit4') {
9372: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 9373: } elsif ($which eq '64bit3') {
9374: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 9375: } elsif ($which eq '64bit2') {
9376: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 9377: } elsif ($which eq '64bit') {
9378: return &rndseed_64bit($symb,$courseid,$domain,$username);
9379: }
9380: return &rndseed_32bit($symb,$courseid,$domain,$username);
9381: }
9382:
9383: sub rndseed_32bit {
9384: my ($symb,$courseid,$domain,$username)=@_;
9385: {
9386: use integer;
9387: my $symbchck=unpack("%32C*",$symb) << 27;
9388: my $symbseed=numval($symb) << 22;
9389: my $namechck=unpack("%32C*",$username) << 17;
9390: my $nameseed=numval($username) << 12;
9391: my $domainseed=unpack("%32C*",$domain) << 7;
9392: my $courseseed=unpack("%32C*",$courseid);
9393: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 9394: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9395: #&logthis("rndseed :$num:$symb");
1.564 albertel 9396: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 9397: return $num;
9398: }
9399: }
9400:
9401: sub rndseed_64bit {
9402: my ($symb,$courseid,$domain,$username)=@_;
9403: {
9404: use integer;
9405: my $symbchck=unpack("%32S*",$symb) << 21;
9406: my $symbseed=numval($symb) << 10;
9407: my $namechck=unpack("%32S*",$username);
9408:
9409: my $nameseed=numval($username) << 21;
9410: my $domainseed=unpack("%32S*",$domain) << 10;
9411: my $courseseed=unpack("%32S*",$courseid);
9412:
9413: my $num1=$symbchck+$symbseed+$namechck;
9414: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9415: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9416: #&logthis("rndseed :$num:$symb");
1.564 albertel 9417: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 9418: return "$num1,$num2";
1.155 albertel 9419: }
1.366 albertel 9420: }
9421:
1.443 albertel 9422: sub rndseed_64bit2 {
9423: my ($symb,$courseid,$domain,$username)=@_;
9424: {
9425: use integer;
9426: # strings need to be an even # of cahracters long, it it is odd the
9427: # last characters gets thrown away
9428: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9429: my $symbseed=numval($symb) << 10;
9430: my $namechck=unpack("%32S*",$username.' ');
9431:
9432: my $nameseed=numval($username) << 21;
1.501 albertel 9433: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9434: my $courseseed=unpack("%32S*",$courseid.' ');
9435:
9436: my $num1=$symbchck+$symbseed+$namechck;
9437: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9438: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9439: #&logthis("rndseed :$num:$symb");
1.803 albertel 9440: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 9441: return "$num1,$num2";
9442: }
9443: }
9444:
9445: sub rndseed_64bit3 {
9446: my ($symb,$courseid,$domain,$username)=@_;
9447: {
9448: use integer;
9449: # strings need to be an even # of cahracters long, it it is odd the
9450: # last characters gets thrown away
9451: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9452: my $symbseed=numval2($symb) << 10;
9453: my $namechck=unpack("%32S*",$username.' ');
9454:
9455: my $nameseed=numval2($username) << 21;
1.443 albertel 9456: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9457: my $courseseed=unpack("%32S*",$courseid.' ');
9458:
9459: my $num1=$symbchck+$symbseed+$namechck;
9460: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9461: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9462: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 9463: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9464:
1.503 albertel 9465: return "$num1:$num2";
1.443 albertel 9466: }
9467: }
9468:
1.575 albertel 9469: sub rndseed_64bit4 {
9470: my ($symb,$courseid,$domain,$username)=@_;
9471: {
9472: use integer;
9473: # strings need to be an even # of cahracters long, it it is odd the
9474: # last characters gets thrown away
9475: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9476: my $symbseed=numval3($symb) << 10;
9477: my $namechck=unpack("%32S*",$username.' ');
9478:
9479: my $nameseed=numval3($username) << 21;
9480: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9481: my $courseseed=unpack("%32S*",$courseid.' ');
9482:
9483: my $num1=$symbchck+$symbseed+$namechck;
9484: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9485: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9486: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 9487: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9488:
9489: return "$num1:$num2";
9490: }
9491: }
9492:
1.675 albertel 9493: sub rndseed_64bit5 {
9494: my ($symb,$courseid,$domain,$username)=@_;
9495: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
9496: return "$num1:$num2";
9497: }
9498:
1.366 albertel 9499: sub rndseed_CODE_64bit {
9500: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 9501: {
1.366 albertel 9502: use integer;
1.443 albertel 9503: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 9504: my $symbseed=numval2($symb);
1.491 albertel 9505: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9506: my $CODEseed=numval(&getCODE());
1.443 albertel 9507: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 9508: my $num1=$symbseed+$CODEchck;
9509: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9510: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9511: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 9512: if ($_64bit) { $num1=(($num1<<32)>>32); }
9513: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 9514: return "$num1:$num2";
1.366 albertel 9515: }
9516: }
9517:
1.575 albertel 9518: sub rndseed_CODE_64bit4 {
9519: my ($symb,$courseid,$domain,$username)=@_;
9520: {
9521: use integer;
9522: my $symbchck=unpack("%32S*",$symb.' ') << 16;
9523: my $symbseed=numval3($symb);
9524: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9525: my $CODEseed=numval3(&getCODE());
9526: my $courseseed=unpack("%32S*",$courseid.' ');
9527: my $num1=$symbseed+$CODEchck;
9528: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9529: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9530: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 9531: if ($_64bit) { $num1=(($num1<<32)>>32); }
9532: if ($_64bit) { $num2=(($num2<<32)>>32); }
9533: return "$num1:$num2";
9534: }
9535: }
9536:
1.675 albertel 9537: sub rndseed_CODE_64bit5 {
9538: my ($symb,$courseid,$domain,$username)=@_;
9539: my $code = &getCODE();
9540: my ($num1,$num2)=&digest("$symb,$courseid,$code");
9541: return "$num1:$num2";
9542: }
9543:
1.366 albertel 9544: sub setup_random_from_rndseed {
9545: my ($rndseed)=@_;
1.503 albertel 9546: if ($rndseed =~/([,:])/) {
9547: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 9548: &Math::Random::random_set_seed(abs($num1),abs($num2));
9549: } else {
9550: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 9551: }
1.36 albertel 9552: }
9553:
1.474 albertel 9554: sub latest_receipt_algorithm_id {
1.835 albertel 9555: return 'receipt3';
1.474 albertel 9556: }
9557:
1.480 www 9558: sub recunique {
9559: my $fucourseid=shift;
9560: my $unique;
1.835 albertel 9561: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
9562: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9563: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 9564: } else {
9565: $unique=$perlvar{'lonReceipt'};
9566: }
9567: return unpack("%32C*",$unique);
9568: }
9569:
9570: sub recprefix {
9571: my $fucourseid=shift;
9572: my $prefix;
1.835 albertel 9573: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
9574: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9575: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 9576: } else {
9577: $prefix=$perlvar{'lonHostID'};
9578: }
9579: return unpack("%32C*",$prefix);
9580: }
9581:
1.76 www 9582: sub ireceipt {
1.474 albertel 9583: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 9584:
9585: my $return =&recprefix($fucourseid).'-';
9586:
9587: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
9588: $env{'request.state'} eq 'construct') {
9589: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
9590: return $return;
9591: }
9592:
1.76 www 9593: my $cuname=unpack("%32C*",$funame);
9594: my $cudom=unpack("%32C*",$fudom);
9595: my $cucourseid=unpack("%32C*",$fucourseid);
9596: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 9597: my $cunique=&recunique($fucourseid);
1.474 albertel 9598: my $cpart=unpack("%32S*",$part);
1.835 albertel 9599: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
9600:
1.790 albertel 9601: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 9602:
9603: $return.= ($cunique%$cuname+
9604: $cunique%$cudom+
9605: $cusymb%$cuname+
9606: $cusymb%$cudom+
9607: $cucourseid%$cuname+
9608: $cucourseid%$cudom+
9609: $cpart%$cuname+
9610: $cpart%$cudom);
9611: } else {
9612: $return.= ($cunique%$cuname+
9613: $cunique%$cudom+
9614: $cusymb%$cuname+
9615: $cusymb%$cudom+
9616: $cucourseid%$cuname+
9617: $cucourseid%$cudom);
9618: }
9619: return $return;
1.76 www 9620: }
9621:
9622: sub receipt {
1.474 albertel 9623: my ($part)=@_;
1.790 albertel 9624: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 9625: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 9626: }
1.260 ng 9627:
1.790 albertel 9628: sub whichuser {
9629: my ($passedsymb)=@_;
9630: my ($symb,$courseid,$domain,$name,$publicuser);
9631: if (defined($env{'form.grade_symb'})) {
9632: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
9633: my $allowed=&allowed('vgr',$tmp_courseid);
9634: if (!$allowed &&
9635: exists($env{'request.course.sec'}) &&
9636: $env{'request.course.sec'} !~ /^\s*$/) {
9637: $allowed=&allowed('vgr',$tmp_courseid.
9638: '/'.$env{'request.course.sec'});
9639: }
9640: if ($allowed) {
9641: ($symb)=&get_env_multiple('form.grade_symb');
9642: $courseid=$tmp_courseid;
9643: ($domain)=&get_env_multiple('form.grade_domain');
9644: ($name)=&get_env_multiple('form.grade_username');
9645: return ($symb,$courseid,$domain,$name,$publicuser);
9646: }
9647: }
9648: if (!$passedsymb) {
9649: $symb=&symbread();
9650: } else {
9651: $symb=$passedsymb;
9652: }
9653: $courseid=$env{'request.course.id'};
9654: $domain=$env{'user.domain'};
9655: $name=$env{'user.name'};
9656: if ($name eq 'public' && $domain eq 'public') {
9657: if (!defined($env{'form.username'})) {
9658: $env{'form.username'}.=time.rand(10000000);
9659: }
9660: $name.=$env{'form.username'};
9661: }
9662: return ($symb,$courseid,$domain,$name,$publicuser);
9663:
9664: }
9665:
1.36 albertel 9666: # ------------------------------------------------------------ Serves up a file
1.472 albertel 9667: # returns either the contents of the file or
9668: # -1 if the file doesn't exist
1.481 raeburn 9669: #
9670: # if the target is a file that was uploaded via DOCS,
9671: # a check will be made to see if a current copy exists on the local server,
9672: # if it does this will be served, otherwise a copy will be retrieved from
9673: # the home server for the course and stored in /home/httpd/html/userfiles on
9674: # the local server.
1.472 albertel 9675:
1.36 albertel 9676: sub getfile {
1.538 albertel 9677: my ($file) = @_;
1.609 banghart 9678: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 9679: &repcopy($file);
9680: return &readfile($file);
9681: }
9682:
9683: sub repcopy_userfile {
9684: my ($file)=@_;
1.609 banghart 9685: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 9686: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 9687: my ($cdom,$cnum,$filename) =
1.811 albertel 9688: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 9689: my $uri="/uploaded/$cdom/$cnum/$filename";
9690: if (-e "$file") {
1.828 www 9691: # we already have a local copy, check it out
1.538 albertel 9692: my @fileinfo = stat($file);
1.828 www 9693: my $rtncode;
9694: my $info;
1.538 albertel 9695: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 9696: if ($lwpresp ne 'ok') {
1.828 www 9697: # there is no such file anymore, even though we had a local copy
1.482 albertel 9698: if ($rtncode eq '404') {
1.538 albertel 9699: unlink($file);
1.482 albertel 9700: }
9701: return -1;
9702: }
9703: if ($info < $fileinfo[9]) {
1.828 www 9704: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 9705: return 'ok';
1.828 www 9706: } else {
9707: # the file is outdated, get rid of it
9708: unlink($file);
1.482 albertel 9709: }
1.828 www 9710: }
9711: # one way or the other, at this point, we don't have the file
9712: # construct the correct path for the file
9713: my @parts = ($cdom,$cnum);
9714: if ($filename =~ m|^(.+)/[^/]+$|) {
9715: push @parts, split(/\//,$1);
9716: }
9717: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
9718: foreach my $part (@parts) {
9719: $path .= '/'.$part;
9720: if (!-e $path) {
9721: mkdir($path,0770);
1.482 albertel 9722: }
9723: }
1.828 www 9724: # now the path exists for sure
9725: # get a user agent
9726: my $ua=new LWP::UserAgent;
9727: my $transferfile=$file.'.in.transfer';
9728: # FIXME: this should flock
9729: if (-e $transferfile) { return 'ok'; }
9730: my $request;
9731: $uri=~s/^\///;
1.980 raeburn 9732: my $homeserver = &homeserver($cnum,$cdom);
9733: my $protocol = $protocol{$homeserver};
9734: $protocol = 'http' if ($protocol ne 'https');
9735: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 9736: my $response=$ua->request($request,$transferfile);
9737: # did it work?
9738: if ($response->is_error()) {
9739: unlink($transferfile);
9740: &logthis("Userfile repcopy failed for $uri");
9741: return -1;
9742: }
9743: # worked, rename the transfer file
9744: rename($transferfile,$file);
1.607 raeburn 9745: return 'ok';
1.481 raeburn 9746: }
9747:
1.517 albertel 9748: sub tokenwrapper {
9749: my $uri=shift;
1.980 raeburn 9750: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 9751: $uri=~s|^/||;
1.620 albertel 9752: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 9753: my $token=$1;
1.552 albertel 9754: my (undef,$udom,$uname,$file)=split('/',$uri,4);
9755: if ($udom && $uname && $file) {
9756: $file=~s|(\?\.*)*$||;
1.949 raeburn 9757: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 9758: my $homeserver = &homeserver($uname,$udom);
9759: my $protocol = $protocol{$homeserver};
9760: $protocol = 'http' if ($protocol ne 'https');
9761: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 9762: (($uri=~/\?/)?'&':'?').'token='.$token.
9763: '&tokenissued='.$perlvar{'lonHostID'};
9764: } else {
9765: return '/adm/notfound.html';
9766: }
9767: }
9768:
1.828 www 9769: # call with reqtype HEAD: get last modification time
9770: # call with reqtype GET: get the file contents
9771: # Do not call this with reqtype GET for large files! It loads everything into memory
9772: #
1.481 raeburn 9773: sub getuploaded {
9774: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
9775: $uri=~s/^\///;
1.980 raeburn 9776: my $homeserver = &homeserver($cnum,$cdom);
9777: my $protocol = $protocol{$homeserver};
9778: $protocol = 'http' if ($protocol ne 'https');
9779: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 9780: my $ua=new LWP::UserAgent;
9781: my $request=new HTTP::Request($reqtype,$uri);
9782: my $response=$ua->request($request);
9783: $$rtncode = $response->code;
1.482 albertel 9784: if (! $response->is_success()) {
9785: return 'failed';
9786: }
9787: if ($reqtype eq 'HEAD') {
1.486 www 9788: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 9789: } elsif ($reqtype eq 'GET') {
9790: $$info = $response->content;
1.472 albertel 9791: }
1.482 albertel 9792: return 'ok';
1.36 albertel 9793: }
9794:
1.481 raeburn 9795: sub readfile {
9796: my $file = shift;
9797: if ( (! -e $file ) || ($file eq '') ) { return -1; };
9798: my $fh;
9799: open($fh,"<$file");
9800: my $a='';
1.800 albertel 9801: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 9802: return $a;
9803: }
9804:
1.36 albertel 9805: sub filelocation {
1.590 banghart 9806: my ($dir,$file) = @_;
9807: my $location;
9808: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 9809:
9810: if ($file =~ m-^/adm/-) {
9811: $file=~s-^/adm/wrapper/-/-;
9812: $file=~s-^/adm/coursedocs/showdoc/-/-;
9813: }
1.882 albertel 9814:
1.590 banghart 9815: if ($file=~m:^/~:) { # is a contruction space reference
9816: $location = $file;
9817: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 9818: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 9819: # is a correct contruction space reference
9820: $location = $file;
1.956 raeburn 9821: } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
9822: $location = $file;
1.609 banghart 9823: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 9824: my ($udom,$uname,$filename)=
1.811 albertel 9825: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 9826: my $home=&homeserver($uname,$udom);
9827: my $is_me=0;
9828: my @ids=¤t_machine_ids();
9829: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
9830: if ($is_me) {
1.955 raeburn 9831: $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 9832: } else {
9833: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
9834: $udom.'/'.$uname.'/'.$filename;
9835: }
1.882 albertel 9836: } elsif ($file =~ m-^/adm/-) {
9837: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 9838: } else {
9839: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
9840: $file=~s:^/res/:/:;
9841: if ( !( $file =~ m:^/:) ) {
9842: $location = $dir. '/'.$file;
9843: } else {
9844: $location = '/home/httpd/html/res'.$file;
9845: }
1.59 albertel 9846: }
1.590 banghart 9847: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 9848: while ($location=~m{/\.\./}) {
9849: if ($location =~ m{/[^/]+/\.\./}) {
9850: $location=~ s{/[^/]+/\.\./}{/}g;
9851: } else {
9852: $location=~ s{/\.\./}{/}g;
9853: }
9854: } #remove dir/..
1.590 banghart 9855: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
9856: return $location;
1.46 www 9857: }
1.36 albertel 9858:
1.46 www 9859: sub hreflocation {
9860: my ($dir,$file)=@_;
1.980 raeburn 9861: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 9862: $file=filelocation($dir,$file);
1.700 albertel 9863: } elsif ($file=~m-^/adm/-) {
9864: $file=~s-^/adm/wrapper/-/-;
9865: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 9866: }
9867: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
9868: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 9869: } elsif ($file=~m-/home/($match_username)/public_html/-) {
9870: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 9871: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 9872: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 9873: -/uploaded/$1/$2/-x;
1.46 www 9874: }
1.913 albertel 9875: if ($file=~ m{^/userfiles/}) {
9876: $file =~ s{^/userfiles/}{/uploaded/};
9877: }
1.462 albertel 9878: return $file;
1.465 albertel 9879: }
9880:
9881: sub current_machine_domains {
1.853 albertel 9882: return &machine_domains(&hostname($perlvar{'lonHostID'}));
9883: }
9884:
9885: sub machine_domains {
9886: my ($hostname) = @_;
1.465 albertel 9887: my @domains;
1.838 albertel 9888: my %hostname = &all_hostnames();
1.465 albertel 9889: while( my($id, $name) = each(%hostname)) {
1.467 matthew 9890: # &logthis("-$id-$name-$hostname-");
1.465 albertel 9891: if ($hostname eq $name) {
1.844 albertel 9892: push(@domains,&host_domain($id));
1.465 albertel 9893: }
9894: }
9895: return @domains;
9896: }
9897:
9898: sub current_machine_ids {
1.853 albertel 9899: return &machine_ids(&hostname($perlvar{'lonHostID'}));
9900: }
9901:
9902: sub machine_ids {
9903: my ($hostname) = @_;
9904: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 9905: my @ids;
1.888 albertel 9906: my %name_to_host = &all_names();
1.889 albertel 9907: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
9908: return @{ $name_to_host{$hostname} };
9909: }
9910: return;
1.31 www 9911: }
9912:
1.824 raeburn 9913: sub additional_machine_domains {
9914: my @domains;
9915: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
9916: while( my $line = <$fh>) {
9917: $line =~ s/\s//g;
9918: push(@domains,$line);
9919: }
9920: return @domains;
9921: }
9922:
9923: sub default_login_domain {
9924: my $domain = $perlvar{'lonDefDomain'};
9925: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
9926: foreach my $posdom (¤t_machine_domains(),
9927: &additional_machine_domains()) {
9928: if (lc($posdom) eq lc($testdomain)) {
9929: $domain=$posdom;
9930: last;
9931: }
9932: }
9933: return $domain;
9934: }
9935:
1.31 www 9936: # ------------------------------------------------------------- Declutters URLs
9937:
9938: sub declutter {
9939: my $thisfn=shift;
1.569 albertel 9940: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 9941: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 9942: $thisfn=~s/^\///;
1.697 albertel 9943: $thisfn=~s|^adm/wrapper/||;
9944: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 9945: $thisfn=~s/^res\///;
1.1032 raeburn 9946: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
9947: $thisfn=~s/\?.+$//;
9948: }
1.268 www 9949: return $thisfn;
9950: }
9951:
9952: # ------------------------------------------------------------- Clutter up URLs
9953:
9954: sub clutter {
9955: my $thisfn='/'.&declutter(shift);
1.887 albertel 9956: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 9957: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 9958: $thisfn='/res'.$thisfn;
9959: }
1.1031 raeburn 9960: if ($thisfn !~m|^/adm|) {
9961: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 9962: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 9963: } else {
9964: my ($ext) = ($thisfn =~ /\.(\w+)$/);
9965: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 9966: if ($embstyle eq 'ssi'
9967: || ($embstyle eq 'hdn')
9968: || ($embstyle eq 'rat')
9969: || ($embstyle eq 'prv')
9970: || ($embstyle eq 'ign')) {
9971: #do nothing with these
9972: } elsif (($embstyle eq 'img')
1.695 albertel 9973: || ($embstyle eq 'emb')
9974: || ($embstyle eq 'wrp')) {
9975: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 9976: } elsif ($embstyle eq 'unk'
9977: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 9978: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 9979: } else {
1.718 www 9980: # &logthis("Got a blank emb style");
1.695 albertel 9981: }
1.694 albertel 9982: }
9983: }
1.31 www 9984: return $thisfn;
1.12 www 9985: }
9986:
1.787 albertel 9987: sub clutter_with_no_wrapper {
9988: my $uri = &clutter(shift);
9989: if ($uri =~ m-^/adm/-) {
9990: $uri =~ s-^/adm/wrapper/-/-;
9991: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
9992: }
9993: return $uri;
9994: }
9995:
1.557 albertel 9996: sub freeze_escape {
9997: my ($value)=@_;
9998: if (ref($value)) {
9999: $value=&nfreeze($value);
10000: return '__FROZEN__'.&escape($value);
10001: }
10002: return &escape($value);
10003: }
10004:
1.11 www 10005:
1.557 albertel 10006: sub thaw_unescape {
10007: my ($value)=@_;
10008: if ($value =~ /^__FROZEN__/) {
10009: substr($value,0,10,undef);
10010: $value=&unescape($value);
10011: return &thaw($value);
10012: }
10013: return &unescape($value);
10014: }
10015:
1.436 albertel 10016: sub correct_line_ends {
10017: my ($result)=@_;
10018: $$result =~s/\r\n/\n/mg;
10019: $$result =~s/\r/\n/mg;
1.415 albertel 10020: }
1.1 albertel 10021: # ================================================================ Main Program
10022:
1.184 www 10023: sub goodbye {
1.204 albertel 10024: &logthis("Starting Shut down");
1.443 albertel 10025: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 10026: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 10027: #converted
1.599 albertel 10028: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 10029: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
10030: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
10031: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 10032: #1.1 only
1.870 albertel 10033: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
10034: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
10035: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
10036: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
10037: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 10038: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
10039: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 10040: &flushcourselogs();
10041: &logthis("Shutting down");
10042: }
10043:
1.852 albertel 10044: sub get_dns {
1.869 albertel 10045: my ($url,$func,$ignore_cache) = @_;
10046: if (!$ignore_cache) {
10047: my ($content,$cached)=
10048: &Apache::lonnet::is_cached_new('dns',$url);
10049: if ($cached) {
10050: &$func($content);
10051: return;
10052: }
10053: }
10054:
10055: my %alldns;
1.852 albertel 10056: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10057: foreach my $dns (<$config>) {
10058: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 10059: my $line = $1;
10060: my ($host,$protocol) = split(/:/,$line);
10061: if ($protocol ne 'https') {
10062: $protocol = 'http';
10063: }
10064: $alldns{$host} = $protocol;
1.869 albertel 10065: }
10066: while (%alldns) {
10067: my ($dns) = keys(%alldns);
1.852 albertel 10068: my $ua=new LWP::UserAgent;
1.1056.4.33 raeburn 10069: $ua->timeout(30);
1.979 raeburn 10070: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 10071: my $response=$ua->request($request);
1.979 raeburn 10072: delete($alldns{$dns});
1.852 albertel 10073: next if ($response->is_error());
10074: my @content = split("\n",$response->content);
1.869 albertel 10075: &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852 albertel 10076: &$func(\@content);
1.869 albertel 10077: return;
1.852 albertel 10078: }
10079: close($config);
1.871 albertel 10080: my $which = (split('/',$url))[3];
10081: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
10082: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 10083: my @content = <$config>;
10084: &$func(\@content);
10085: return;
1.852 albertel 10086: }
1.327 albertel 10087: # ------------------------------------------------------------ Read domain file
10088: {
1.852 albertel 10089: my $loaded;
1.846 albertel 10090: my %domain;
10091:
1.852 albertel 10092: sub parse_domain_tab {
10093: my ($lines) = @_;
10094: foreach my $line (@$lines) {
10095: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 10096:
1.846 albertel 10097: chomp($line);
1.852 albertel 10098: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 10099: my %this_domain;
10100: foreach my $field ('description', 'auth_def', 'auth_arg_def',
10101: 'lang_def', 'city', 'longi', 'lati',
10102: 'primary') {
10103: $this_domain{$field} = shift(@elements);
10104: }
10105: $domain{$name} = \%this_domain;
1.852 albertel 10106: }
10107: }
1.864 albertel 10108:
10109: sub reset_domain_info {
10110: undef($loaded);
10111: undef(%domain);
10112: }
10113:
1.852 albertel 10114: sub load_domain_tab {
1.869 albertel 10115: my ($ignore_cache) = @_;
10116: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852 albertel 10117: my $fh;
10118: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
10119: my @lines = <$fh>;
10120: &parse_domain_tab(\@lines);
1.448 albertel 10121: }
1.852 albertel 10122: close($fh);
10123: $loaded = 1;
1.327 albertel 10124: }
1.846 albertel 10125:
10126: sub domain {
1.852 albertel 10127: &load_domain_tab() if (!$loaded);
10128:
1.846 albertel 10129: my ($name,$what) = @_;
10130: return if ( !exists($domain{$name}) );
10131:
10132: if (!$what) {
10133: return $domain{$name}{'description'};
10134: }
10135: return $domain{$name}{$what};
10136: }
1.974 raeburn 10137:
10138: sub domain_info {
10139: &load_domain_tab() if (!$loaded);
10140: return %domain;
10141: }
10142:
1.327 albertel 10143: }
10144:
10145:
1.1 albertel 10146: # ------------------------------------------------------------- Read hosts file
10147: {
1.838 albertel 10148: my %hostname;
1.844 albertel 10149: my %hostdom;
1.845 albertel 10150: my %libserv;
1.852 albertel 10151: my $loaded;
1.888 albertel 10152: my %name_to_host;
1.1056.4.6 raeburn 10153: my %internetdom;
1.1056.4.25 raeburn 10154: my %LC_dns_serv;
1.852 albertel 10155:
10156: sub parse_hosts_tab {
10157: my ($file) = @_;
10158: foreach my $configline (@$file) {
10159: next if ($configline =~ /^(\#|\s*$ )/x);
1.1056.4.25 raeburn 10160: chomp($configline);
10161: if ($configline =~ /^\^/) {
10162: if ($configline =~ /^\^([\w.\-]+)/) {
10163: $LC_dns_serv{$1} = 1;
10164: }
10165: next;
10166: }
1.1056.4.6 raeburn 10167: my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852 albertel 10168: $name=~s/\s//g;
10169: if ($id && $domain && $role && $name) {
10170: $hostname{$id}=$name;
1.888 albertel 10171: push(@{$name_to_host{$name}}, $id);
1.852 albertel 10172: $hostdom{$id}=$domain;
10173: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 10174: if (defined($protocol)) {
10175: if ($protocol eq 'https') {
10176: $protocol{$id} = $protocol;
10177: } else {
10178: $protocol{$id} = 'http';
10179: }
1.968 raeburn 10180: } else {
1.969 raeburn 10181: $protocol{$id} = 'http';
1.968 raeburn 10182: }
1.1056.4.6 raeburn 10183: if (defined($intdom)) {
10184: $internetdom{$id} = $intdom;
10185: }
1.852 albertel 10186: }
10187: }
10188: }
1.864 albertel 10189:
10190: sub reset_hosts_info {
1.897 albertel 10191: &purge_remembered();
1.864 albertel 10192: &reset_domain_info();
10193: &reset_hosts_ip_info();
1.892 albertel 10194: undef(%name_to_host);
1.864 albertel 10195: undef(%hostname);
10196: undef(%hostdom);
10197: undef(%libserv);
10198: undef($loaded);
10199: }
1.1 albertel 10200:
1.852 albertel 10201: sub load_hosts_tab {
1.869 albertel 10202: my ($ignore_cache) = @_;
10203: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852 albertel 10204: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
10205: my @config = <$config>;
10206: &parse_hosts_tab(\@config);
10207: close($config);
10208: $loaded=1;
1.1 albertel 10209: }
1.852 albertel 10210:
1.838 albertel 10211: sub hostname {
1.852 albertel 10212: &load_hosts_tab() if (!$loaded);
10213:
1.838 albertel 10214: my ($lonid) = @_;
10215: return $hostname{$lonid};
10216: }
1.845 albertel 10217:
1.838 albertel 10218: sub all_hostnames {
1.852 albertel 10219: &load_hosts_tab() if (!$loaded);
10220:
1.838 albertel 10221: return %hostname;
10222: }
1.845 albertel 10223:
1.888 albertel 10224: sub all_names {
10225: &load_hosts_tab() if (!$loaded);
10226:
10227: return %name_to_host;
10228: }
10229:
1.974 raeburn 10230: sub all_host_domain {
10231: &load_hosts_tab() if (!$loaded);
10232: return %hostdom;
10233: }
10234:
1.845 albertel 10235: sub is_library {
1.852 albertel 10236: &load_hosts_tab() if (!$loaded);
10237:
1.845 albertel 10238: return exists($libserv{$_[0]});
10239: }
10240:
10241: sub all_library {
1.852 albertel 10242: &load_hosts_tab() if (!$loaded);
10243:
1.845 albertel 10244: return %libserv;
10245: }
10246:
1.1056.4.2 raeburn 10247: sub unique_library {
10248: #2x reverse removes all hostnames that appear more than once
10249: my %unique = reverse &all_library();
10250: return reverse %unique;
10251: }
10252:
1.841 albertel 10253: sub get_servers {
1.852 albertel 10254: &load_hosts_tab() if (!$loaded);
10255:
1.841 albertel 10256: my ($domain,$type) = @_;
10257: my %possible_hosts = ($type eq 'library') ? %libserv
10258: : %hostname;
10259: my %result;
1.842 albertel 10260: if (ref($domain) eq 'ARRAY') {
10261: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 10262: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 10263: $result{$host} = $hostname;
10264: }
10265: }
10266: } else {
10267: while ( my ($host,$hostname) = each(%possible_hosts)) {
10268: if ($hostdom{$host} eq $domain) {
10269: $result{$host} = $hostname;
10270: }
1.841 albertel 10271: }
10272: }
10273: return %result;
10274: }
1.845 albertel 10275:
1.1056.4.2 raeburn 10276: sub get_unique_servers {
10277: my %unique = reverse &get_servers(@_);
10278: return reverse %unique;
10279: }
10280:
1.844 albertel 10281: sub host_domain {
1.852 albertel 10282: &load_hosts_tab() if (!$loaded);
10283:
1.844 albertel 10284: my ($lonid) = @_;
10285: return $hostdom{$lonid};
10286: }
10287:
1.841 albertel 10288: sub all_domains {
1.852 albertel 10289: &load_hosts_tab() if (!$loaded);
10290:
1.841 albertel 10291: my %seen;
10292: my @uniq = grep(!$seen{$_}++, values(%hostdom));
10293: return @uniq;
10294: }
1.1056.4.3 raeburn 10295:
10296: sub internet_dom {
10297: &load_hosts_tab() if (!$loaded);
10298:
10299: my ($lonid) = @_;
10300: return $internetdom{$lonid};
10301: }
1.1056.4.27 raeburn 10302:
10303: sub is_LC_dns {
10304: &load_hosts_tab() if (!$loaded);
10305:
10306: my ($hostname) = @_;
10307: return exists($LC_dns_serv{$hostname});
10308: }
10309:
1.1 albertel 10310: }
10311:
1.847 albertel 10312: {
10313: my %iphost;
1.856 albertel 10314: my %name_to_ip;
10315: my %lonid_to_ip;
1.869 albertel 10316:
1.847 albertel 10317: sub get_hosts_from_ip {
10318: my ($ip) = @_;
10319: my %iphosts = &get_iphost();
10320: if (ref($iphosts{$ip})) {
10321: return @{$iphosts{$ip}};
10322: }
10323: return;
1.839 albertel 10324: }
1.864 albertel 10325:
10326: sub reset_hosts_ip_info {
10327: undef(%iphost);
10328: undef(%name_to_ip);
10329: undef(%lonid_to_ip);
10330: }
1.856 albertel 10331:
10332: sub get_host_ip {
10333: my ($lonid) = @_;
10334: if (exists($lonid_to_ip{$lonid})) {
10335: return $lonid_to_ip{$lonid};
10336: }
10337: my $name=&hostname($lonid);
10338: my $ip = gethostbyname($name);
10339: return if (!$ip || length($ip) ne 4);
10340: $ip=inet_ntoa($ip);
10341: $name_to_ip{$name} = $ip;
10342: $lonid_to_ip{$lonid} = $ip;
10343: return $ip;
10344: }
1.847 albertel 10345:
10346: sub get_iphost {
1.869 albertel 10347: my ($ignore_cache) = @_;
1.894 albertel 10348:
1.869 albertel 10349: if (!$ignore_cache) {
10350: if (%iphost) {
10351: return %iphost;
10352: }
10353: my ($ip_info,$cached)=
10354: &Apache::lonnet::is_cached_new('iphost','iphost');
10355: if ($cached) {
10356: %iphost = %{$ip_info->[0]};
10357: %name_to_ip = %{$ip_info->[1]};
10358: %lonid_to_ip = %{$ip_info->[2]};
10359: return %iphost;
10360: }
10361: }
1.894 albertel 10362:
10363: # get yesterday's info for fallback
10364: my %old_name_to_ip;
10365: my ($ip_info,$cached)=
10366: &Apache::lonnet::is_cached_new('iphost','iphost');
10367: if ($cached) {
10368: %old_name_to_ip = %{$ip_info->[1]};
10369: }
10370:
1.888 albertel 10371: my %name_to_host = &all_names();
10372: foreach my $name (keys(%name_to_host)) {
1.847 albertel 10373: my $ip;
10374: if (!exists($name_to_ip{$name})) {
10375: $ip = gethostbyname($name);
10376: if (!$ip || length($ip) ne 4) {
1.894 albertel 10377: if (defined($old_name_to_ip{$name})) {
10378: $ip = $old_name_to_ip{$name};
10379: &logthis("Can't find $name defaulting to old $ip");
10380: } else {
10381: &logthis("Name $name no IP found");
10382: next;
10383: }
10384: } else {
10385: $ip=inet_ntoa($ip);
1.847 albertel 10386: }
10387: $name_to_ip{$name} = $ip;
10388: } else {
10389: $ip = $name_to_ip{$name};
1.653 albertel 10390: }
1.888 albertel 10391: foreach my $id (@{ $name_to_host{$name} }) {
10392: $lonid_to_ip{$id} = $ip;
10393: }
10394: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 10395: }
1.869 albertel 10396: &Apache::lonnet::do_cache_new('iphost','iphost',
10397: [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894 albertel 10398: 48*60*60);
1.869 albertel 10399:
1.847 albertel 10400: return %iphost;
1.598 albertel 10401: }
10402:
1.992 raeburn 10403: #
10404: # Given a DNS returns the loncapa host name for that DNS
10405: #
10406: sub host_from_dns {
10407: my ($dns) = @_;
10408: my @hosts;
10409: my $ip;
10410:
1.993 raeburn 10411: if (exists($name_to_ip{$dns})) {
1.992 raeburn 10412: $ip = $name_to_ip{$dns};
10413: }
10414: if (!$ip) {
10415: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
10416: if (length($ip) == 4) {
10417: $ip = &IO::Socket::inet_ntoa($ip);
10418: }
10419: }
10420: if ($ip) {
10421: @hosts = get_hosts_from_ip($ip);
10422: return $hosts[0];
10423: }
10424: return undef;
1.986 foxr 10425: }
1.992 raeburn 10426:
1.1056.4.3 raeburn 10427: sub get_internet_names {
10428: my ($lonid) = @_;
10429: return if ($lonid eq '');
10430: my ($idnref,$cached)=
10431: &Apache::lonnet::is_cached_new('internetnames',$lonid);
10432: if ($cached) {
10433: return $idnref;
10434: }
10435: my $ip = &get_host_ip($lonid);
10436: my @hosts = &get_hosts_from_ip($ip);
10437: my %iphost = &get_iphost();
10438: my (@idns,%seen);
10439: foreach my $id (@hosts) {
10440: my $dom = &host_domain($id);
10441: my $prim_id = &domain($dom,'primary');
10442: my $prim_ip = &get_host_ip($prim_id);
10443: next if ($seen{$prim_ip});
10444: if (ref($iphost{$prim_ip}) eq 'ARRAY') {
10445: foreach my $id (@{$iphost{$prim_ip}}) {
10446: my $intdom = &internet_dom($id);
10447: unless (grep(/^\Q$intdom\E$/,@idns)) {
10448: push(@idns,$intdom);
10449: }
10450: }
10451: }
10452: $seen{$prim_ip} = 1;
10453: }
10454: return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
10455: }
10456:
10457: }
10458:
10459: sub all_loncaparevs {
10460: 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 10461: }
10462:
1.862 albertel 10463: BEGIN {
10464:
10465: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
10466: unless ($readit) {
10467: {
10468: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
10469: %perlvar = (%perlvar,%{$configvars});
10470: }
10471:
10472:
1.1 albertel 10473: # ------------------------------------------------------ Read spare server file
10474: {
1.448 albertel 10475: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 10476:
10477: while (my $configline=<$config>) {
10478: chomp($configline);
1.284 matthew 10479: if ($configline) {
1.784 albertel 10480: my ($host,$type) = split(':',$configline,2);
1.785 albertel 10481: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 10482: push(@{ $spareid{$type} }, $host);
1.1 albertel 10483: }
10484: }
1.448 albertel 10485: close($config);
1.1 albertel 10486: }
1.11 www 10487: # ------------------------------------------------------------ Read permissions
10488: {
1.448 albertel 10489: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 10490:
10491: while (my $configline=<$config>) {
1.448 albertel 10492: chomp($configline);
10493: if ($configline) {
10494: my ($role,$perm)=split(/ /,$configline);
10495: if ($perm ne '') { $pr{$role}=$perm; }
10496: }
1.11 www 10497: }
1.448 albertel 10498: close($config);
1.11 www 10499: }
10500:
10501: # -------------------------------------------- Read plain texts for permissions
10502: {
1.448 albertel 10503: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 10504:
10505: while (my $configline=<$config>) {
1.448 albertel 10506: chomp($configline);
10507: if ($configline) {
1.742 raeburn 10508: my ($short,@plain)=split(/:/,$configline);
10509: %{$prp{$short}} = ();
10510: if (@plain > 0) {
10511: $prp{$short}{'std'} = $plain[0];
10512: for (my $i=1; $i<@plain; $i++) {
10513: $prp{$short}{'alt'.$i} = $plain[$i];
10514: }
10515: }
1.448 albertel 10516: }
1.135 www 10517: }
1.448 albertel 10518: close($config);
1.135 www 10519: }
10520:
10521: # ---------------------------------------------------------- Read package table
10522: {
1.448 albertel 10523: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 10524:
10525: while (my $configline=<$config>) {
1.483 albertel 10526: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 10527: chomp($configline);
10528: my ($short,$plain)=split(/:/,$configline);
10529: my ($pack,$name)=split(/\&/,$short);
10530: if ($plain ne '') {
10531: $packagetab{$pack.'&'.$name.'&name'}=$name;
10532: $packagetab{$short}=$plain;
10533: }
1.11 www 10534: }
1.448 albertel 10535: close($config);
1.329 matthew 10536: }
10537:
1.1056.4.3 raeburn 10538: # ---------------------------------------------------------- Read loncaparev table
10539: {
10540: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
10541: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
10542: while (my $configline=<$config>) {
10543: chomp($configline);
10544: my ($hostid,$loncaparev)=split(/:/,$configline);
10545: $loncaparevs{$hostid}=$loncaparev;
10546: }
10547: close($config);
10548: }
10549: }
10550: }
10551:
10552: # ---------------------------------------------------------- Read serverhostID table
10553: {
10554: if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
10555: if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
10556: while (my $configline=<$config>) {
10557: chomp($configline);
10558: my ($name,$id)=split(/:/,$configline);
10559: $serverhomeIDs{$name}=$id;
10560: }
10561: close($config);
10562: }
10563: }
10564: }
10565:
1.1056.4.5 raeburn 10566: {
10567: my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
10568: if (-e $file) {
10569: my $parser = HTML::LCParser->new($file);
10570: while (my $token = $parser->get_token()) {
10571: if ($token->[0] eq 'S') {
10572: my $item = $token->[1];
10573: my $name = $token->[2]{'name'};
10574: my $value = $token->[2]{'value'};
10575: if ($item ne '' && $name ne '' && $value ne '') {
10576: my $release = $parser->get_text();
10577: $release =~ s/(^\s*|\s*$ )//gx;
10578: $needsrelease{$item.':'.$name.':'.$value} = $release;
10579: }
10580: }
10581: }
10582: }
10583: }
10584:
1.1056.4.33 raeburn 10585: # ---------------------------------------------------------- Read managers table
10586: {
10587: if (-e "$perlvar{'lonTabDir'}/managers.tab") {
10588: if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
10589: while (my $configline=<$config>) {
10590: chomp($configline);
10591: next if ($configline =~ /^\#/);
10592: if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
10593: $managerstab{$configline} = 1;
10594: }
10595: }
10596: close($config);
10597: }
10598: }
10599: }
10600:
1.329 matthew 10601: # ------------- set up temporary directory
10602: {
10603: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
10604:
1.11 www 10605: }
10606:
1.794 albertel 10607: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
10608: 'compress_threshold'=> 20_000,
10609: });
1.185 www 10610:
1.281 www 10611: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 10612: $dumpcount=0;
1.958 www 10613: $locknum=0;
1.22 www 10614:
1.163 harris41 10615: &logtouch();
1.672 albertel 10616: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 10617: $readit=1;
1.564 albertel 10618: {
10619: use integer;
10620: my $test=(2**32)+1;
1.568 albertel 10621: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 10622: &logthis(" Detected 64bit platform ($_64bit)");
10623: }
1.195 www 10624: }
1.1 albertel 10625: }
1.179 www 10626:
1.1 albertel 10627: 1;
1.191 harris41 10628: __END__
10629:
1.243 albertel 10630: =pod
10631:
1.191 harris41 10632: =head1 NAME
10633:
1.243 albertel 10634: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 10635:
10636: =head1 SYNOPSIS
10637:
1.243 albertel 10638: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 10639:
10640: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
10641:
1.243 albertel 10642: Common parameters:
10643:
10644: =over 4
10645:
10646: =item *
10647:
10648: $uname : an internal username (if $cname expecting a course Id specifically)
10649:
10650: =item *
10651:
10652: $udom : a domain (if $cdom expecting a course's domain specifically)
10653:
10654: =item *
10655:
10656: $symb : a resource instance identifier
10657:
10658: =item *
10659:
10660: $namespace : the name of a .db file that contains the data needed or
10661: being set.
10662:
10663: =back
10664:
1.394 bowersj2 10665: =head1 OVERVIEW
1.191 harris41 10666:
1.394 bowersj2 10667: lonnet provides subroutines which interact with the
10668: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
10669: about classes, users, and resources.
1.243 albertel 10670:
10671: For many of these objects you can also use this to store data about
10672: them or modify them in various ways.
1.191 harris41 10673:
1.394 bowersj2 10674: =head2 Symbs
1.191 harris41 10675:
1.394 bowersj2 10676: To identify a specific instance of a resource, LON-CAPA uses symbols
10677: or "symbs"X<symb>. These identifiers are built from the URL of the
10678: map, the resource number of the resource in the map, and the URL of
10679: the resource itself. The latter is somewhat redundant, but might help
10680: if maps change.
10681:
10682: An example is
10683:
10684: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
10685:
10686: The respective map entry is
10687:
10688: <resource id="19" src="/res/msu/korte/tests/part12.problem"
10689: title="Problem 2">
10690: </resource>
10691:
10692: Symbs are used by the random number generator, as well as to store and
10693: restore data specific to a certain instance of for example a problem.
10694:
10695: =head2 Storing And Retrieving Data
10696:
10697: X<store()>X<cstore()>X<restore()>Three of the most important functions
10698: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
10699: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
10700: is is the non-critical message twin of cstore. These functions are for
10701: handlers to store a perl hash to a user's permanent data space in an
10702: easy manner, and to retrieve it again on another call. It is expected
10703: that a handler would use this once at the beginning to retrieve data,
10704: and then again once at the end to send only the new data back.
10705:
10706: The data is stored in the user's data directory on the user's
10707: homeserver under the ID of the course.
10708:
10709: The hash that is returned by restore will have all of the previous
10710: value for all of the elements of the hash.
10711:
10712: Example:
10713:
10714: #creating a hash
10715: my %hash;
10716: $hash{'foo'}='bar';
10717:
10718: #storing it
10719: &Apache::lonnet::cstore(\%hash);
10720:
10721: #changing a value
10722: $hash{'foo'}='notbar';
10723:
10724: #adding a new value
10725: $hash{'bar'}='foo';
10726: &Apache::lonnet::cstore(\%hash);
10727:
10728: #retrieving the hash
10729: my %history=&Apache::lonnet::restore();
10730:
10731: #print the hash
10732: foreach my $key (sort(keys(%history))) {
10733: print("\%history{$key} = $history{$key}");
10734: }
10735:
10736: Will print out:
1.191 harris41 10737:
1.394 bowersj2 10738: %history{1:foo} = bar
10739: %history{1:keys} = foo:timestamp
10740: %history{1:timestamp} = 990455579
10741: %history{2:bar} = foo
10742: %history{2:foo} = notbar
10743: %history{2:keys} = foo:bar:timestamp
10744: %history{2:timestamp} = 990455580
10745: %history{bar} = foo
10746: %history{foo} = notbar
10747: %history{timestamp} = 990455580
10748: %history{version} = 2
10749:
10750: Note that the special hash entries C<keys>, C<version> and
10751: C<timestamp> were added to the hash. C<version> will be equal to the
10752: total number of versions of the data that have been stored. The
10753: C<timestamp> attribute will be the UNIX time the hash was
10754: stored. C<keys> is available in every historical section to list which
10755: keys were added or changed at a specific historical revision of a
10756: hash.
10757:
10758: B<Warning>: do not store the hash that restore returns directly. This
10759: will cause a mess since it will restore the historical keys as if the
10760: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 10761:
1.394 bowersj2 10762: Calling convention:
1.191 harris41 10763:
1.394 bowersj2 10764: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
10765: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 10766:
1.394 bowersj2 10767: For more detailed information, see lonnet specific documentation.
1.191 harris41 10768:
1.394 bowersj2 10769: =head1 RETURN MESSAGES
1.191 harris41 10770:
1.394 bowersj2 10771: =over 4
1.191 harris41 10772:
1.394 bowersj2 10773: =item * B<con_lost>: unable to contact remote host
1.191 harris41 10774:
1.394 bowersj2 10775: =item * B<con_delayed>: unable to contact remote host, message will be delivered
10776: when the connection is brought back up
1.191 harris41 10777:
1.394 bowersj2 10778: =item * B<con_failed>: unable to contact remote host and unable to save message
10779: for later delivery
1.191 harris41 10780:
1.967 bisitz 10781: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 10782:
1.394 bowersj2 10783: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 10784: that was requested
1.191 harris41 10785:
1.243 albertel 10786: =back
1.191 harris41 10787:
1.243 albertel 10788: =head1 PUBLIC SUBROUTINES
1.191 harris41 10789:
1.243 albertel 10790: =head2 Session Environment Functions
1.191 harris41 10791:
1.243 albertel 10792: =over 4
1.191 harris41 10793:
1.394 bowersj2 10794: =item *
10795: X<appenv()>
1.949 raeburn 10796: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 10797: the user envirnoment file, and will be restored for each access this
1.620 albertel 10798: user makes during this session, also modifies the %env for the current
1.949 raeburn 10799: process. Optional rolesarrayref - if defined contains a reference to an array
10800: of roles which are exempt from the restriction on modifying user.role entries
10801: in the user's environment.db and in %env.
1.191 harris41 10802:
10803: =item *
1.394 bowersj2 10804: X<delenv()>
1.987 raeburn 10805: B<delenv($delthis,$regexp)>: removes all items from the session
10806: environment file that begin with $delthis. If the
10807: optional second arg - $regexp - is true, $delthis is treated as a
10808: regular expression, otherwise \Q$delthis\E is used.
10809: The values are also deleted from the current processes %env.
1.191 harris41 10810:
1.795 albertel 10811: =item * get_env_multiple($name)
10812:
10813: gets $name from the %env hash, it seemlessly handles the cases where multiple
10814: values may be defined and end up as an array ref.
10815:
10816: returns an array of values
10817:
1.243 albertel 10818: =back
10819:
10820: =head2 User Information
1.191 harris41 10821:
1.243 albertel 10822: =over 4
1.191 harris41 10823:
10824: =item *
1.394 bowersj2 10825: X<queryauthenticate()>
10826: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 10827: authentication scheme
10828:
10829: =item *
1.394 bowersj2 10830: X<authenticate()>
1.1056.4.3 raeburn 10831: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 10832: authenticate user from domain's lib servers (first use the current
10833: one). C<$upass> should be the users password.
1.1056.4.3 raeburn 10834: $checkdefauth is optional (value is 1 if a check should be made to
10835: authenticate user using default authentication method, and allow
10836: account creation if username does not have account in the domain).
10837: $clientcancheckhost is optional (value is 1 if checking whether the
10838: server can host will occur on the client side in lonauth.pm).
1.191 harris41 10839:
10840: =item *
1.394 bowersj2 10841: X<homeserver()>
10842: B<homeserver($uname,$udom)>: find the server which has
10843: the user's directory and files (there must be only one), this caches
10844: the answer, and also caches if there is a borken connection.
1.191 harris41 10845:
10846: =item *
1.394 bowersj2 10847: X<idget()>
10848: B<idget($udom,@ids)>: find the usernames behind a list of IDs
10849: (IDs are a unique resource in a domain, there must be only 1 ID per
10850: username, and only 1 username per ID in a specific domain) (returns
10851: hash: id=>name,id=>name)
1.191 harris41 10852:
10853: =item *
1.394 bowersj2 10854: X<idrget()>
10855: B<idrget($udom,@unames)>: find the IDs behind a list of
10856: usernames (returns hash: name=>id,name=>id)
1.191 harris41 10857:
10858: =item *
1.394 bowersj2 10859: X<idput()>
10860: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 10861:
10862: =item *
1.394 bowersj2 10863: X<rolesinit()>
10864: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 10865:
10866: =item *
1.551 albertel 10867: X<getsection()>
10868: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 10869: course $cname, return section name/number or '' for "not in course"
10870: and '-1' for "no section"
10871:
10872: =item *
1.394 bowersj2 10873: X<userenvironment()>
10874: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 10875: passed in @what from the requested user's environment, returns a hash
10876:
1.858 raeburn 10877: =item *
10878: X<userlog_query()>
1.859 albertel 10879: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
10880: activity.log file. %filters defines filters applied when parsing the
10881: log file. These can be start or end timestamps, or the type of action
10882: - log to look for Login or Logout events, check for Checkin or
10883: Checkout, role for role selection. The response is in the form
10884: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
10885: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 10886:
1.243 albertel 10887: =back
10888:
10889: =head2 User Roles
10890:
10891: =over 4
10892:
10893: =item *
10894:
1.810 raeburn 10895: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 10896: F: full access
10897: U,I,K: authentication modes (cxx only)
10898: '': forbidden
10899: 1: user needs to choose course
10900: 2: browse allowed
1.766 albertel 10901: A: passphrase authentication needed
1.243 albertel 10902:
10903: =item *
10904:
10905: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
10906: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
10907: and course level
10908:
10909: =item *
10910:
1.988 raeburn 10911: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
10912: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 10913: $type is Course (default) or Community.
1.988 raeburn 10914: If $forcedefault evaluates to true, text returned will be default
10915: text for $type. Otherwise, if this is a course, the text returned
10916: will be a custom name for the role (if defined in the course's
10917: environment). If no custom name is defined the default is returned.
10918:
1.832 raeburn 10919: =item *
10920:
1.935 raeburn 10921: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858 raeburn 10922: All arguments are optional. Returns a hash of a roles, either for
10923: co-author/assistant author roles for a user's Construction Space
1.906 albertel 10924: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 10925: In the hash, keys are set to colon-separated $uname,$udom,$role, and
10926: (optionally) if $withsec is true, a fourth colon-separated item - $section.
10927: For each key, value is set to colon-separated start and end times for
10928: the role. If no username and domain are specified, will default to
1.934 raeburn 10929: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 10930: of role statuses (active, future or previous), roles
10931: (e.g., cc,in, st etc.) and domains of the roles which can be used
10932: to restrict the list of roles reported. If no array ref is
10933: provided for types, will default to return only active roles.
1.834 albertel 10934:
1.243 albertel 10935: =back
10936:
10937: =head2 User Modification
10938:
10939: =over 4
10940:
10941: =item *
10942:
1.957 raeburn 10943: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 10944: user for the level given by URL. Optional start and end dates (leave empty
10945: string or zero for "no date")
1.191 harris41 10946:
10947: =item *
10948:
1.243 albertel 10949: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
10950: change a users, password, possible return values are: ok,
10951: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
10952: refused
1.191 harris41 10953:
10954: =item *
10955:
1.243 albertel 10956: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 10957:
10958: =item *
10959:
1.1056.4.1 raeburn 10960: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
10961: $forceid,$desiredhome,$email,$inststatus,$candelete) :
10962:
10963: will update user information (firstname,middlename,lastname,generation,
10964: permanentemail), and if forceid is true, student/employee ID also.
10965: A user's institutional affiliation(s) can also be updated.
10966: User information fields will not be overwritten with empty entries
10967: unless the field is included in the $candelete array reference.
10968: This array is included when a single user is modified via "Manage Users",
10969: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 10970:
10971: =item *
10972:
1.286 matthew 10973: modifystudent
10974:
1.957 raeburn 10975: modify a student's enrollment and identification information.
1.286 matthew 10976: The course id is resolved based on the current users environment.
10977: This means the envoking user must be a course coordinator or otherwise
10978: associated with a course.
10979:
1.297 matthew 10980: This call is essentially a wrapper for lonnet::modifyuser and
10981: lonnet::modify_student_enrollment
1.286 matthew 10982:
10983: Inputs:
10984:
10985: =over 4
10986:
1.957 raeburn 10987: =item B<$udom> Student's loncapa domain
1.286 matthew 10988:
1.957 raeburn 10989: =item B<$uname> Student's loncapa login name
1.286 matthew 10990:
1.964 bisitz 10991: =item B<$uid> Student/Employee ID
1.286 matthew 10992:
1.957 raeburn 10993: =item B<$umode> Student's authentication mode
1.286 matthew 10994:
1.957 raeburn 10995: =item B<$upass> Student's password
1.286 matthew 10996:
1.957 raeburn 10997: =item B<$first> Student's first name
1.286 matthew 10998:
1.957 raeburn 10999: =item B<$middle> Student's middle name
1.286 matthew 11000:
1.957 raeburn 11001: =item B<$last> Student's last name
1.286 matthew 11002:
1.957 raeburn 11003: =item B<$gene> Student's generation
1.286 matthew 11004:
1.957 raeburn 11005: =item B<$usec> Student's section in course
1.286 matthew 11006:
11007: =item B<$end> Unix time of the roles expiration
11008:
11009: =item B<$start> Unix time of the roles start date
11010:
11011: =item B<$forceid> If defined, allow $uid to be changed
11012:
11013: =item B<$desiredhome> server to use as home server for student
11014:
1.957 raeburn 11015: =item B<$email> Student's permanent e-mail address
11016:
11017: =item B<$type> Type of enrollment (auto or manual)
11018:
1.963 raeburn 11019: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
11020:
11021: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 11022:
1.963 raeburn 11023: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 11024:
1.963 raeburn 11025: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 11026:
1.963 raeburn 11027: =item B<$inststatus> institutional status of user - : separated string of escaped status types
1.957 raeburn 11028:
1.286 matthew 11029: =back
1.297 matthew 11030:
11031: =item *
11032:
11033: modify_student_enrollment
11034:
11035: Change a students enrollment status in a class. The environment variable
11036: 'role.request.course' must be defined for this function to proceed.
11037:
11038: Inputs:
11039:
11040: =over 4
11041:
11042: =item $udom, students domain
11043:
11044: =item $uname, students name
11045:
11046: =item $uid, students user id
11047:
11048: =item $first, students first name
11049:
11050: =item $middle
11051:
11052: =item $last
11053:
11054: =item $gene
11055:
11056: =item $usec
11057:
11058: =item $end
11059:
11060: =item $start
11061:
1.957 raeburn 11062: =item $type
11063:
11064: =item $locktype
11065:
11066: =item $cid
11067:
11068: =item $selfenroll
11069:
11070: =item $context
11071:
1.297 matthew 11072: =back
11073:
1.191 harris41 11074:
11075: =item *
11076:
1.243 albertel 11077: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
11078: custom role; give a custom role to a user for the level given by URL. Specify
11079: name and domain of role author, and role name
1.191 harris41 11080:
11081: =item *
11082:
1.243 albertel 11083: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 11084:
11085: =item *
11086:
1.243 albertel 11087: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
11088:
11089: =back
11090:
11091: =head2 Course Infomation
11092:
11093: =over 4
1.191 harris41 11094:
11095: =item *
11096:
1.631 albertel 11097: coursedescription($courseid) : returns a hash of information about the
11098: specified course id, including all environment settings for the
11099: course, the description of the course will be in the hash under the
11100: key 'description'
1.191 harris41 11101:
11102: =item *
11103:
1.624 albertel 11104: resdata($name,$domain,$type,@which) : request for current parameter
11105: setting for a specific $type, where $type is either 'course' or 'user',
11106: @what should be a list of parameters to ask about. This routine caches
11107: answers for 5 minutes.
1.243 albertel 11108:
1.877 foxr 11109: =item *
11110:
11111: get_courseresdata($courseid, $domain) : dump the entire course resource
11112: data base, returning a hash that is keyed by the resource name and has
11113: values that are the resource value. I believe that the timestamps and
11114: versions are also returned.
11115:
11116:
1.243 albertel 11117: =back
11118:
11119: =head2 Course Modification
11120:
11121: =over 4
1.191 harris41 11122:
11123: =item *
11124:
1.243 albertel 11125: writecoursepref($courseid,%prefs) : write preferences (environment
11126: database) for a course
1.191 harris41 11127:
11128: =item *
11129:
1.1011 raeburn 11130: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
11131:
11132: =item *
11133:
1.1038 raeburn 11134: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 11135:
11136: =back
11137:
11138: =head2 Resource Subroutines
11139:
11140: =over 4
1.191 harris41 11141:
11142: =item *
11143:
1.243 albertel 11144: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 11145:
11146: =item *
11147:
1.243 albertel 11148: repcopy($filename) : subscribes to the requested file, and attempts to
11149: replicate from the owning library server, Might return
1.607 raeburn 11150: 'unavailable', 'not_found', 'forbidden', 'ok', or
11151: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 11152: resource. Expects the local filesystem pathname
11153: (/home/httpd/html/res/....)
11154:
11155: =back
11156:
11157: =head2 Resource Information
11158:
11159: =over 4
1.191 harris41 11160:
11161: =item *
11162:
1.243 albertel 11163: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
11164: a vairety of different possible values, $varname should be a request
11165: string, and the other parameters can be used to specify who and what
11166: one is asking about.
11167:
11168: Possible values for $varname are environment.lastname (or other item
11169: from the envirnment hash), user.name (or someother aspect about the
11170: user), resource.0.maxtries (or some other part and parameter of a
11171: resource)
1.204 albertel 11172:
11173: =item *
11174:
1.243 albertel 11175: directcondval($number) : get current value of a condition; reads from a state
11176: string
1.204 albertel 11177:
11178: =item *
11179:
1.243 albertel 11180: condval($condidx) : value of condition index based on state
1.204 albertel 11181:
11182: =item *
11183:
1.243 albertel 11184: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
11185: resource's metadata, $what should be either a specific key, or either
11186: 'keys' (to get a list of possible keys) or 'packages' to get a list of
11187: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
11188:
11189: this function automatically caches all requests
1.191 harris41 11190:
11191: =item *
11192:
1.243 albertel 11193: metadata_query($query,$custom,$customshow) : make a metadata query against the
11194: network of library servers; returns file handle of where SQL and regex results
11195: will be stored for query
1.191 harris41 11196:
11197: =item *
11198:
1.243 albertel 11199: symbread($filename) : return symbolic list entry (filename argument optional);
11200: returns the data handle
1.191 harris41 11201:
11202: =item *
11203:
1.243 albertel 11204: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 11205: a possible symb for the URL in $thisfn, and if is an encryypted
11206: resource that the user accessed using /enc/ returns a 1 on success, 0
11207: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 11208: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 11209:
1.191 harris41 11210:
11211: =item *
11212:
1.243 albertel 11213: symbclean($symb) : removes versions numbers from a symb, returns the
11214: cleaned symb
1.191 harris41 11215:
11216: =item *
11217:
1.243 albertel 11218: is_on_map($uri) : checks if the $uri is somewhere on the current
11219: course map, user must be in a course for it to work.
1.191 harris41 11220:
11221: =item *
11222:
1.243 albertel 11223: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 11224:
11225: =item *
11226:
1.243 albertel 11227: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
11228: a random seed, all arguments are optional, if they aren't sent it uses the
11229: environment to derive them. Note: if symb isn't sent and it can't get one
11230: from &symbread it will use the current time as its return value
1.191 harris41 11231:
11232: =item *
11233:
1.243 albertel 11234: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
11235: unfakeable, receipt
1.191 harris41 11236:
11237: =item *
11238:
1.620 albertel 11239: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 11240:
11241: =item *
11242:
1.243 albertel 11243: countacc($url) : count the number of accesses to a given URL
1.191 harris41 11244:
11245: =item *
11246:
1.243 albertel 11247: 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 11248:
11249: =item *
11250:
1.243 albertel 11251: 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 11252:
11253: =item *
11254:
1.243 albertel 11255: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 11256:
11257: =item *
11258:
1.243 albertel 11259: devalidate($symb) : devalidate temporary spreadsheet calculations,
11260: forcing spreadsheet to reevaluate the resource scores next time.
11261:
11262: =back
11263:
11264: =head2 Storing/Retreiving Data
11265:
11266: =over 4
1.191 harris41 11267:
11268: =item *
11269:
1.243 albertel 11270: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
11271: for this url; hashref needs to be given and should be a \%hashname; the
11272: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 11273: be derived from the env
1.191 harris41 11274:
11275: =item *
11276:
1.243 albertel 11277: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
11278: uses critical subroutine
1.191 harris41 11279:
11280: =item *
11281:
1.243 albertel 11282: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
11283: all args are optional
1.191 harris41 11284:
11285: =item *
11286:
1.717 albertel 11287: dumpstore($namespace,$udom,$uname,$regexp,$range) :
11288: dumps the complete (or key matching regexp) namespace into a hash
11289: ($udom, $uname, $regexp, $range are optional) for a namespace that is
11290: normally &store()ed into
11291:
11292: $range should be either an integer '100' (give me the first 100
11293: matching records)
11294: or be two integers sperated by a - with no spaces
11295: '30-50' (give me the 30th through the 50th matching
11296: records)
11297:
11298:
11299: =item *
11300:
11301: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
11302: replaces a &store() version of data with a replacement set of data
11303: for a particular resource in a namespace passed in the $storehash hash
11304: reference
11305:
11306: =item *
11307:
1.243 albertel 11308: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
11309: works very similar to store/cstore, but all data is stored in a
11310: temporary location and can be reset using tmpreset, $storehash should
11311: be a hash reference, returns nothing on success
1.191 harris41 11312:
11313: =item *
11314:
1.243 albertel 11315: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
11316: similar to restore, but all data is stored in a temporary location and
11317: can be reset using tmpreset. Returns a hash of values on success,
11318: error string otherwise.
1.191 harris41 11319:
11320: =item *
11321:
1.243 albertel 11322: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
11323: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 11324:
11325: =item *
11326:
1.243 albertel 11327: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11328: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 11329:
11330: =item *
11331:
1.243 albertel 11332: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
11333: namesp ($udom and $uname are optional)
1.191 harris41 11334:
11335: =item *
11336:
1.702 albertel 11337: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 11338: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 11339: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 11340:
1.702 albertel 11341: $range should be either an integer '100' (give me the first 100
11342: matching records)
11343: or be two integers sperated by a - with no spaces
11344: '30-50' (give me the 30th through the 50th matching
11345: records)
1.449 matthew 11346: =item *
11347:
11348: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
11349: $store can be a scalar, an array reference, or if the amount to be
11350: incremented is > 1, a hash reference.
11351:
11352: ($udom and $uname are optional)
1.191 harris41 11353:
11354: =item *
11355:
1.243 albertel 11356: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
11357: ($udom and $uname are optional)
1.191 harris41 11358:
11359: =item *
11360:
1.243 albertel 11361: cput($namespace,$storehash,$udom,$uname) : critical put
11362: ($udom and $uname are optional)
1.191 harris41 11363:
11364: =item *
11365:
1.748 albertel 11366: newput($namespace,$storehash,$udom,$uname) :
11367:
11368: Attempts to store the items in the $storehash, but only if they don't
11369: currently exist, if this succeeds you can be certain that you have
11370: successfully created a new key value pair in the $namespace db.
11371:
11372:
11373: Args:
11374: $namespace: name of database to store values to
11375: $storehash: hashref to store to the db
11376: $udom: (optional) domain of user containing the db
11377: $uname: (optional) name of user caontaining the db
11378:
11379: Returns:
11380: 'ok' -> succeeded in storing all keys of $storehash
11381: 'key_exists: <key>' -> failed to anything out of $storehash, as at
11382: least <key> already existed in the db (other
11383: requested keys may also already exist)
1.967 bisitz 11384: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 11385: 'con_lost' -> unable to contact request server
11386: 'refused' -> action was not allowed by remote machine
11387:
11388:
11389: =item *
11390:
1.243 albertel 11391: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
11392: reference filled in from namesp (encrypts the return communication)
11393: ($udom and $uname are optional)
1.191 harris41 11394:
11395: =item *
11396:
1.243 albertel 11397: log($udom,$name,$home,$message) : write to permanent log for user; use
11398: critical subroutine
11399:
1.806 raeburn 11400: =item *
11401:
1.860 raeburn 11402: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
11403: array reference filled in from namespace found in domain level on either
11404: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 11405:
11406: =item *
11407:
1.860 raeburn 11408: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
11409: domain level either on specified domain server ($uhome) or primary domain
11410: server ($udom and $uhome are optional)
1.806 raeburn 11411:
1.943 raeburn 11412: =item *
11413:
11414: get_domain_defaults($target_domain) : returns hash with defaults for
11415: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
11416: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
11417: or localauth), initial password or a kerberos realm, language (e.g., en-us).
11418: Values are retrieved from cache (if current), or from domain's configuration.db
11419: (if available), or lastly from values in lonTabs/dns_domain,tab,
11420: or lonTabs/domain.tab.
11421:
11422: %domdefaults = &get_auth_defaults($target_domain);
11423:
1.243 albertel 11424: =back
11425:
11426: =head2 Network Status Functions
11427:
11428: =over 4
1.191 harris41 11429:
11430: =item *
11431:
11432: dirlist($uri) : return directory list based on URI
11433:
11434: =item *
11435:
1.243 albertel 11436: spareserver() : find server with least workload from spare.tab
11437:
1.986 foxr 11438:
11439: =item *
11440:
11441: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
11442: if there is no corresponding loncapa host.
11443:
1.243 albertel 11444: =back
11445:
1.986 foxr 11446:
1.243 albertel 11447: =head2 Apache Request
11448:
11449: =over 4
1.191 harris41 11450:
11451: =item *
11452:
1.243 albertel 11453: ssi($url,%hash) : server side include, does a complete request cycle on url to
11454: localhost, posts hash
11455:
11456: =back
11457:
11458: =head2 Data to String to Data
11459:
11460: =over 4
1.191 harris41 11461:
11462: =item *
11463:
1.243 albertel 11464: hash2str(%hash) : convert a hash into a string complete with escaping and '='
11465: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 11466:
11467: =item *
11468:
1.243 albertel 11469: hashref2str($hashref) : convert a hashref into a string complete with
11470: escaping and '=' and '&' separators, supports elements that are
11471: arrayrefs and hashrefs
1.191 harris41 11472:
11473: =item *
11474:
1.243 albertel 11475: arrayref2str($arrayref) : convert an arrayref into a string complete
11476: with escaping and '&' separators, supports elements that are arrayrefs
11477: and hashrefs
1.191 harris41 11478:
11479: =item *
11480:
1.243 albertel 11481: str2hash($string) : convert string to hash using unescaping and
11482: splitting on '=' and '&', supports elements that are arrayrefs and
11483: hashrefs
1.191 harris41 11484:
11485: =item *
11486:
1.243 albertel 11487: str2array($string) : convert string to hash using unescaping and
11488: splitting on '&', supports elements that are arrayrefs and hashrefs
11489:
11490: =back
11491:
11492: =head2 Logging Routines
11493:
11494: These routines allow one to make log messages in the lonnet.log and
11495: lonnet.perm logfiles.
1.191 harris41 11496:
1.1056.4.31 raeburn 11497: =over 4
11498:
1.191 harris41 11499: =item *
11500:
1.243 albertel 11501: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 11502:
11503: =item *
11504:
1.243 albertel 11505: logthis() : append message to the normal lonnet.log file, it gets
11506: preiodically rolled over and deleted.
1.191 harris41 11507:
11508: =item *
11509:
1.243 albertel 11510: logperm() : append a permanent message to lonnet.perm.log, this log
11511: file never gets deleted by any automated portion of the system, only
11512: messages of critical importance should go in here.
11513:
11514: =back
11515:
11516: =head2 General File Helper Routines
11517:
11518: =over 4
1.191 harris41 11519:
11520: =item *
11521:
1.481 raeburn 11522: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
11523: (a) files in /uploaded
11524: (i) If a local copy of the file exists -
11525: compares modification date of local copy with last-modified date for
11526: definitive version stored on home server for course. If local copy is
11527: stale, requests a new version from the home server and stores it.
11528: If the original has been removed from the home server, then local copy
11529: is unlinked.
11530: (ii) If local copy does not exist -
11531: requests the file from the home server and stores it.
11532:
11533: If $caller is 'uploadrep':
11534: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
11535: for request for files originally uploaded via DOCS.
11536: - returns 'ok' if fresh local copy now available, -1 otherwise.
11537:
11538: Otherwise:
11539: This indicates a call from the content generation phase of the request.
11540: - returns the entire contents of the file or -1.
11541:
11542: (b) files in /res
11543: - returns the entire contents of a file or -1;
11544: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 11545:
1.712 albertel 11546:
11547: =item *
11548:
11549: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
11550: reference
11551:
11552: returns either a stat() list of data about the file or an empty list
11553: if the file doesn't exist or couldn't find out about it (connection
11554: problems or user unknown)
11555:
1.191 harris41 11556: =item *
11557:
1.243 albertel 11558: filelocation($dir,$file) : returns file system location of a file
11559: based on URI; meant to be "fairly clean" absolute reference, $dir is a
11560: directory that relative $file lookups are to looked in ($dir of /a/dir
11561: and a file of ../bob will become /a/bob)
1.191 harris41 11562:
11563: =item *
11564:
11565: hreflocation($dir,$file) : returns file system location or a URL; same as
11566: filelocation except for hrefs
11567:
11568: =item *
11569:
11570: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
11571:
1.243 albertel 11572: =back
11573:
1.608 albertel 11574: =head2 Usererfile file routines (/uploaded*)
11575:
11576: =over 4
11577:
11578: =item *
11579:
11580: userfileupload(): main rotine for putting a file in a user or course's
11581: filespace, arguments are,
11582:
1.620 albertel 11583: formname - required - this is the name of the element in $env where the
1.608 albertel 11584: filename, and the contents of the file to create/modifed exist
1.620 albertel 11585: the filename is in $env{'form.'.$formname.'.filename'} and the
11586: contents of the file is located in $env{'form.'.$formname}
1.1056.4.13 raeburn 11587: context - if coursedoc, store the file in the course of the active role
11588: of the current user;
11589: if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
11590: if 'canceloverwrite': delete file in tmp/overwrites directory
1.608 albertel 11591: subdir - required - subdirectory to put the file in under ../userfiles/
11592: if undefined, it will be placed in "unknown"
11593:
11594: (This routine calls clean_filename() to remove any dangerous
11595: characters from the filename, and then calls finuserfileupload() to
11596: complete the transaction)
11597:
11598: returns either the url of the uploaded file (/uploaded/....) if successful
11599: and /adm/notfound.html if unsuccessful
11600:
11601: =item *
11602:
11603: clean_filename(): routine for cleaing a filename up for storage in
11604: userfile space, argument is:
11605:
11606: filename - proposed filename
11607:
11608: returns: the new clean filename
11609:
11610: =item *
11611:
1.1056.4.13 raeburn 11612: finishuserfileupload(): routine that creates and sends the file to
1.608 albertel 11613: userspace, probably shouldn't be called directly
11614:
11615: docuname: username or courseid of destination for the file
11616: docudom: domain of user/course of destination for the file
11617: formname: same as for userfileupload()
1.1056.4.13 raeburn 11618: fname: filename (including subdirectories) for the file
11619: parser: if 'parse', will parse (html) file to extract references to objects, links etc.
11620: allfiles: reference to hash used to store objects found by parser
11621: codebase: reference to hash used for codebases of java objects found by parser
11622: thumbwidth: width (pixels) of thumbnail to be created for uploaded image
11623: thumbheight: height (pixels) of thumbnail to be created for uploaded image
11624: resizewidth: width to be used to resize image using resizeImage from ImageMagick
11625: resizeheight: height to be used to resize image using resizeImage from ImageMagick
11626: context: if 'overwrite', will move the uploaded file from its temporary location to
11627: userfiles to facilitate overwriting a previously uploaded file with same name.
1.1056.4.16 raeburn 11628: mimetype: reference to scalar to accommodate mime type determined
11629: from File::MMagic if $parser = parse.
1.608 albertel 11630:
11631: returns either the url of the uploaded file (/uploaded/....) if successful
1.1056.4.13 raeburn 11632: and /adm/notfound.html if unsuccessful (or an error message if context
11633: was 'overwrite').
11634:
1.608 albertel 11635:
11636: =item *
11637:
11638: renameuserfile(): renames an existing userfile to a new name
11639:
11640: Args:
11641: docuname: username or courseid of destination for the file
11642: docudom: domain of user/course of destination for the file
11643: old: current file name (including any subdirs under userfiles)
11644: new: desired file name (including any subdirs under userfiles)
11645:
11646: =item *
11647:
11648: mkdiruserfile(): creates a directory is a userfiles dir
11649:
11650: Args:
11651: docuname: username or courseid of destination for the file
11652: docudom: domain of user/course of destination for the file
11653: dir: dir to create (including any subdirs under userfiles)
11654:
11655: =item *
11656:
11657: removeuserfile(): removes a file that exists in userfiles
11658:
11659: Args:
11660: docuname: username or courseid of destination for the file
11661: docudom: domain of user/course of destination for the file
11662: fname: filname to delete (including any subdirs under userfiles)
11663:
11664: =item *
11665:
11666: removeuploadedurl(): convience function for removeuserfile()
11667:
11668: Args:
11669: url: a full /uploaded/... url to delete
11670:
1.747 albertel 11671: =item *
11672:
11673: get_portfile_permissions():
11674: Args:
11675: domain: domain of user or course contain the portfolio files
11676: user: name of user or num of course contain the portfolio files
11677: Returns:
11678: hashref of a dump of the proper file_permissions.db
11679:
11680:
11681: =item *
11682:
11683: get_access_controls():
11684:
11685: Args:
11686: current_permissions: the hash ref returned from get_portfile_permissions()
11687: group: (optional) the group you want the files associated with
11688: file: (optional) the file you want access info on
11689:
11690: Returns:
1.749 raeburn 11691: a hash (keys are file names) of hashes containing
11692: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
11693: values are XML containing access control settings (see below)
1.747 albertel 11694:
11695: Internal notes:
11696:
1.749 raeburn 11697: access controls are stored in file_permissions.db as key=value pairs.
11698: key -> path to file/file_name\0uniqueID:scope_end_start
11699: where scope -> public,guest,course,group,domains or users.
11700: end -> UNIX time for end of access (0 -> no end date)
11701: start -> UNIX time for start of access
11702:
11703: value -> XML description of access control
11704: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
11705: <start></start>
11706: <end></end>
11707:
11708: <password></password> for scope type = guest
11709:
11710: <domain></domain> for scope type = course or group
11711: <number></number>
11712: <roles id="">
11713: <role></role>
11714: <access></access>
11715: <section></section>
11716: <group></group>
11717: </roles>
11718:
11719: <dom></dom> for scope type = domains
11720:
11721: <users> for scope type = users
11722: <user>
11723: <uname></uname>
11724: <udom></udom>
11725: </user>
11726: </users>
11727: </scope>
11728:
11729: Access data is also aggregated for each file in an additional key=value pair:
11730: key -> path to file/file_name\0accesscontrol
11731: value -> reference to hash
11732: hash contains key = value pairs
11733: where key = uniqueID:scope_end_start
11734: value = UNIX time record was last updated
11735:
11736: Used to improve speed of look-ups of access controls for each file.
11737:
11738: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
11739:
11740: modify_access_controls():
11741:
11742: Modifies access controls for a portfolio file
11743: Args
11744: 1. file name
11745: 2. reference to hash of required changes,
11746: 3. domain
11747: 4. username
11748: where domain,username are the domain of the portfolio owner
11749: (either a user or a course)
11750:
11751: Returns:
11752: 1. result of additions or updates ('ok' or 'error', with error message).
11753: 2. result of deletions ('ok' or 'error', with error message).
11754: 3. reference to hash of any new or updated access controls.
11755: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
11756: key = integer (inbound ID)
11757: value = uniqueID
1.747 albertel 11758:
1.608 albertel 11759: =back
11760:
1.243 albertel 11761: =head2 HTTP Helper Routines
11762:
11763: =over 4
11764:
1.191 harris41 11765: =item *
11766:
11767: escape() : unpack non-word characters into CGI-compatible hex codes
11768:
11769: =item *
11770:
11771: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
11772:
1.243 albertel 11773: =back
11774:
11775: =head1 PRIVATE SUBROUTINES
11776:
11777: =head2 Underlying communication routines (Shouldn't call)
11778:
11779: =over 4
11780:
11781: =item *
11782:
11783: subreply() : tries to pass a message to lonc, returns con_lost if incapable
11784:
11785: =item *
11786:
11787: reply() : uses subreply to send a message to remote machine, logs all failures
11788:
11789: =item *
11790:
11791: critical() : passes a critical message to another server; if cannot
11792: get through then place message in connection buffer directory and
11793: returns con_delayed, if incapable of saving message, returns
11794: con_failed
11795:
11796: =item *
11797:
11798: reconlonc() : tries to reconnect lonc client processes.
11799:
11800: =back
11801:
11802: =head2 Resource Access Logging
11803:
11804: =over 4
11805:
11806: =item *
11807:
11808: flushcourselogs() : flush (save) buffer logs and access logs
11809:
11810: =item *
11811:
11812: courselog($what) : save message for course in hash
11813:
11814: =item *
11815:
11816: courseacclog($what) : save message for course using &courselog(). Perform
11817: special processing for specific resource types (problems, exams, quizzes, etc).
11818:
1.191 harris41 11819: =item *
11820:
11821: goodbye() : flush course logs and log shutting down; it is called in srm.conf
11822: as a PerlChildExitHandler
1.243 albertel 11823:
11824: =back
11825:
11826: =head2 Other
11827:
11828: =over 4
11829:
11830: =item *
11831:
11832: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 11833:
11834: =back
11835:
11836: =cut
1.877 foxr 11837:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>