Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1073
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.1073 ! raeburn 4: # $Id: lonnet.pm,v 1.1072 2010/07/06 18:36:56 www 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.1073 ! raeburn 79: $_64bit %env %protocol %loncaparevs);
1.871 albertel 80:
81: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
82: %userrolehash, $processmarker, $dumpcount, %coursedombuf,
83: %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958 www 84: %courseownerbuf, %coursetypebuf,$locknum);
1.403 www 85:
1.1 albertel 86: use IO::Socket;
1.31 www 87: use GDBM_File;
1.208 albertel 88: use HTML::LCParser;
1.88 www 89: use Fcntl qw(:flock);
1.870 albertel 90: use Storable qw(thaw nfreeze);
1.539 albertel 91: use Time::HiRes qw( gettimeofday tv_interval );
1.599 albertel 92: use Cache::Memcached;
1.676 albertel 93: use Digest::MD5;
1.790 albertel 94: use Math::Random;
1.1024 raeburn 95: use File::MMagic;
1.807 albertel 96: use LONCAPA qw(:DEFAULT :match);
1.740 www 97: use LONCAPA::Configuration;
1.676 albertel 98:
1.195 www 99: my $readit;
1.550 foxr 100: my $max_connection_retries = 10; # Or some such value.
1.1 albertel 101:
1.619 albertel 102: require Exporter;
103:
104: our @ISA = qw (Exporter);
105: our @EXPORT = qw(%env);
106:
1.449 matthew 107:
1.1 albertel 108: # --------------------------------------------------------------------- Logging
1.729 www 109: {
110: my $logid;
111: sub instructor_log {
1.957 raeburn 112: my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
113: if (($cnum eq '') || ($cdom eq '')) {
114: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
115: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
116: }
1.729 www 117: $logid++;
1.957 raeburn 118: my $now = time();
119: my $id=$now.'00000'.$$.'00000'.$logid;
1.729 www 120: return &Apache::lonnet::put('nohist_'.$hash_name,
1.730 www 121: { $id => {
122: 'exe_uname' => $env{'user.name'},
123: 'exe_udom' => $env{'user.domain'},
1.957 raeburn 124: 'exe_time' => $now,
1.730 www 125: 'exe_ip' => $ENV{'REMOTE_ADDR'},
126: 'delflag' => $delflag,
127: 'logentry' => $storehash,
128: 'uname' => $uname,
129: 'udom' => $udom,
130: }
1.957 raeburn 131: },$cdom,$cnum);
1.729 www 132: }
133: }
1.1 albertel 134:
1.163 harris41 135: sub logtouch {
136: my $execdir=$perlvar{'lonDaemons'};
1.448 albertel 137: unless (-e "$execdir/logs/lonnet.log") {
138: open(my $fh,">>$execdir/logs/lonnet.log");
1.163 harris41 139: close $fh;
140: }
141: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
142: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
143: }
144:
1.1 albertel 145: sub logthis {
146: my $message=shift;
147: my $execdir=$perlvar{'lonDaemons'};
148: my $now=time;
149: my $local=localtime($now);
1.448 albertel 150: if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986 foxr 151: my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
152: print $fh $logstring;
1.448 albertel 153: close($fh);
154: }
1.1 albertel 155: return 1;
156: }
157:
158: sub logperm {
159: my $message=shift;
160: my $execdir=$perlvar{'lonDaemons'};
161: my $now=time;
162: my $local=localtime($now);
1.448 albertel 163: if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
164: print $fh "$now:$message:$local\n";
165: close($fh);
166: }
1.1 albertel 167: return 1;
168: }
169:
1.850 albertel 170: sub create_connection {
1.853 albertel 171: my ($hostname,$lonid) = @_;
1.851 albertel 172: my $client=IO::Socket::UNIX->new(Peer => $perlvar{'lonSockCreate'},
1.850 albertel 173: Type => SOCK_STREAM,
174: Timeout => 10);
175: return 0 if (!$client);
1.890 albertel 176: print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850 albertel 177: my $result = <$client>;
178: chomp($result);
179: return 1 if ($result eq 'done');
180: return 0;
181: }
182:
1.983 raeburn 183: sub get_server_timezone {
184: my ($cnum,$cdom) = @_;
185: my $home=&homeserver($cnum,$cdom);
186: if ($home ne 'no_host') {
187: my $cachetime = 24*3600;
188: my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
189: if (defined($cached)) {
190: return $timezone;
191: } else {
192: my $timezone = &reply('servertimezone',$home);
193: return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
194: }
195: }
196: }
1.850 albertel 197:
1.993 raeburn 198: sub get_server_loncaparev {
1.1073 ! raeburn 199: my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993 raeburn 200: if (defined($lonhost)) {
201: if (!defined(&hostname($lonhost))) {
202: undef($lonhost);
203: }
204: }
205: if (!defined($lonhost)) {
206: if (defined(&domain($dom,'primary'))) {
207: $lonhost=&domain($dom,'primary');
208: if ($lonhost eq 'no_host') {
209: undef($lonhost);
210: }
211: }
212: }
213: if (defined($lonhost)) {
1.1073 ! raeburn 214: my $cachetime = 12*3600;
! 215: if (!$ignore_cache) {
! 216: my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
! 217: if (defined($cached)) {
! 218: return $loncaparev;
! 219: }
! 220: }
! 221: my ($answer,$loncaparev);
! 222: my @ids=¤t_machine_ids();
! 223: if (grep(/^\Q$lonhost\E$/,@ids)) {
! 224: $answer = $perlvar{'lonVersion'};
! 225: if ($answer =~ /^[\'\"]?([\d.\-]+)[\'\"]?$/) {
! 226: $loncaparev = $1;
! 227: }
! 228: } else {
! 229: $answer = &reply('serverloncaparev',$lonhost);
! 230: if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
! 231: if ($caller eq 'loncron') {
! 232: my $ua=new LWP::UserAgent;
! 233: $ua->timeout(20);
! 234: my $protocol = $protocol{$lonhost};
! 235: $protocol = 'http' if ($protocol ne 'https');
! 236: my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
! 237: my $request=new HTTP::Request('GET',$url);
! 238: my $response=$ua->request($request);
! 239: unless ($response->is_error()) {
! 240: my $content = $response->content;
! 241: if ($content =~ /<p>VERSION\:\s*([\d.\-]+)<\/p>/) {
! 242: $loncaparev = $1;
! 243: }
! 244: }
! 245: } else {
! 246: $loncaparev = $loncaparevs{$lonhost};
! 247: }
! 248: } elsif ($answer =~ /^[\'\"]?([\d.\-]+)[\'\"]?$/) {
! 249: $loncaparev = $1;
! 250: }
1.993 raeburn 251: }
1.1073 ! raeburn 252: return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993 raeburn 253: }
254: }
255:
1.1 albertel 256: # -------------------------------------------------- Non-critical communication
257: sub subreply {
258: my ($cmd,$server)=@_;
1.838 albertel 259: my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549 foxr 260: #
261: # With loncnew process trimming, there's a timing hole between lonc server
262: # process exit and the master server picking up the listen on the AF_UNIX
263: # socket. In that time interval, a lock file will exist:
264:
265: my $lockfile=$peerfile.".lock";
266: while (-e $lockfile) { # Need to wait for the lockfile to disappear.
267: sleep(1);
268: }
269: # At this point, either a loncnew parent is listening or an old lonc
1.550 foxr 270: # or loncnew child is listening so we can connect or everything's dead.
1.549 foxr 271: #
1.550 foxr 272: # We'll give the connection a few tries before abandoning it. If
273: # connection is not possible, we'll con_lost back to the client.
274: #
275: my $client;
276: for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
277: $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
278: Type => SOCK_STREAM,
279: Timeout => 10);
1.869 albertel 280: if ($client) {
1.550 foxr 281: last; # Connected!
1.850 albertel 282: } else {
1.853 albertel 283: &create_connection(&hostname($server),$server);
1.550 foxr 284: }
1.850 albertel 285: sleep(1); # Try again later if failed connection.
1.550 foxr 286: }
287: my $answer;
288: if ($client) {
1.704 albertel 289: print $client "sethost:$server:$cmd\n";
1.550 foxr 290: $answer=<$client>;
291: if (!$answer) { $answer="con_lost"; }
292: chomp($answer);
293: } else {
294: $answer = 'con_lost'; # Failed connection.
295: }
1.1 albertel 296: return $answer;
297: }
298:
299: sub reply {
300: my ($cmd,$server)=@_;
1.838 albertel 301: unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1 albertel 302: my $answer=subreply($cmd,$server);
1.65 www 303: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672 albertel 304: &logthis("<font color=\"blue\">WARNING:".
1.12 www 305: " $cmd to $server returned $answer</font>");
306: }
1.1 albertel 307: return $answer;
308: }
309:
310: # ----------------------------------------------------------- Send USR1 to lonc
311:
312: sub reconlonc {
1.891 albertel 313: my ($lonid) = @_;
314: my $hostname = &hostname($lonid);
315: if ($lonid) {
316: my $peerfile="$perlvar{'lonSockDir'}/$hostname";
317: if ($hostname && -e $peerfile) {
318: &logthis("Trying to reconnect lonc for $lonid ($hostname)");
319: my $client=IO::Socket::UNIX->new(Peer => $peerfile,
320: Type => SOCK_STREAM,
321: Timeout => 10);
322: if ($client) {
323: print $client ("reset_retries\n");
324: my $answer=<$client>;
325: #reset just this one.
326: }
327: }
328: return;
329: }
330:
1.836 www 331: &logthis("Trying to reconnect lonc");
1.1 albertel 332: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448 albertel 333: if (open(my $fh,"<$loncfile")) {
1.1 albertel 334: my $loncpid=<$fh>;
335: chomp($loncpid);
336: if (kill 0 => $loncpid) {
337: &logthis("lonc at pid $loncpid responding, sending USR1");
338: kill USR1 => $loncpid;
339: sleep 1;
1.836 www 340: } else {
1.12 www 341: &logthis(
1.672 albertel 342: "<font color=\"blue\">WARNING:".
1.12 www 343: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 344: }
345: } else {
1.836 www 346: &logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1 albertel 347: }
348: }
349:
350: # ------------------------------------------------------ Critical communication
1.12 www 351:
1.1 albertel 352: sub critical {
353: my ($cmd,$server)=@_;
1.838 albertel 354: unless (&hostname($server)) {
1.672 albertel 355: &logthis("<font color=\"blue\">WARNING:".
1.89 www 356: " Critical message to unknown server ($server)</font>");
357: return 'no_such_host';
358: }
1.1 albertel 359: my $answer=reply($cmd,$server);
360: if ($answer eq 'con_lost') {
361: &reconlonc("$perlvar{'lonSockDir'}/$server");
1.589 albertel 362: my $answer=reply($cmd,$server);
1.1 albertel 363: if ($answer eq 'con_lost') {
364: my $now=time;
365: my $middlename=$cmd;
1.5 www 366: $middlename=substr($middlename,0,16);
1.1 albertel 367: $middlename=~s/\W//g;
368: my $dfilename=
1.305 www 369: "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
370: $dumpcount++;
1.1 albertel 371: {
1.448 albertel 372: my $dfh;
373: if (open($dfh,">$dfilename")) {
374: print $dfh "$cmd\n";
375: close($dfh);
376: }
1.1 albertel 377: }
378: sleep 2;
379: my $wcmd='';
380: {
1.448 albertel 381: my $dfh;
382: if (open($dfh,"<$dfilename")) {
383: $wcmd=<$dfh>;
384: close($dfh);
385: }
1.1 albertel 386: }
387: chomp($wcmd);
1.7 www 388: if ($wcmd eq $cmd) {
1.672 albertel 389: &logthis("<font color=\"blue\">WARNING: ".
1.12 www 390: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 391: &logperm("D:$server:$cmd");
392: return 'con_delayed';
393: } else {
1.672 albertel 394: &logthis("<font color=\"red\">CRITICAL:"
1.12 www 395: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 396: &logperm("F:$server:$cmd");
397: return 'con_failed';
398: }
399: }
400: }
401: return $answer;
1.405 albertel 402: }
403:
1.755 albertel 404: # ------------------------------------------- check if return value is an error
405:
406: sub error {
407: my ($result) = @_;
1.756 albertel 408: if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755 albertel 409: if ($2 == 2) { return undef; }
410: return $1;
411: }
412: return undef;
413: }
414:
1.783 albertel 415: sub convert_and_load_session_env {
416: my ($lonidsdir,$handle)=@_;
417: my @profile;
418: {
1.917 albertel 419: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
420: if (!$opened) {
1.915 albertel 421: return 0;
422: }
1.783 albertel 423: flock($idf,LOCK_SH);
424: @profile=<$idf>;
425: close($idf);
426: }
427: my %temp_env;
428: foreach my $line (@profile) {
1.786 albertel 429: if ($line !~ m/=/) {
430: return 0;
431: }
1.783 albertel 432: chomp($line);
433: my ($envname,$envvalue)=split(/=/,$line,2);
434: $temp_env{&unescape($envname)} = &unescape($envvalue);
435: }
436: unlink("$lonidsdir/$handle.id");
437: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
438: 0640)) {
439: %disk_env = %temp_env;
440: @env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
441: untie(%disk_env);
442: }
1.786 albertel 443: return 1;
1.783 albertel 444: }
445:
1.374 www 446: # ------------------------------------------- Transfer profile into environment
1.780 albertel 447: my $env_loaded;
448: sub transfer_profile_to_env {
1.788 albertel 449: my ($lonidsdir,$handle,$force_transfer) = @_;
450: if (!$force_transfer && $env_loaded) { return; }
1.374 www 451:
1.720 albertel 452: if (!defined($lonidsdir)) {
453: $lonidsdir = $perlvar{'lonIDsDir'};
454: }
455: if (!defined($handle)) {
456: ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
457: }
458:
1.786 albertel 459: my $convert;
460: {
1.917 albertel 461: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
462: if (!$opened) {
1.915 albertel 463: return;
464: }
1.786 albertel 465: flock($idf,LOCK_SH);
466: if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
467: &GDBM_READER(),0640)) {
468: @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
469: untie(%disk_env);
470: } else {
471: $convert = 1;
472: }
473: }
474: if ($convert) {
475: if (!&convert_and_load_session_env($lonidsdir,$handle)) {
476: &logthis("Failed to load session, or convert session.");
477: }
1.374 www 478: }
1.783 albertel 479:
1.786 albertel 480: my %remove;
1.783 albertel 481: while ( my $envname = each(%env) ) {
1.433 matthew 482: if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
483: if ($time < time-300) {
1.783 albertel 484: $remove{$key}++;
1.433 matthew 485: }
486: }
487: }
1.783 albertel 488:
1.619 albertel 489: $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780 albertel 490: $env_loaded=1;
1.783 albertel 491: foreach my $expired_key (keys(%remove)) {
1.433 matthew 492: &delenv($expired_key);
1.374 www 493: }
1.1 albertel 494: }
495:
1.916 albertel 496: # ---------------------------------------------------- Check for valid session
497: sub check_for_valid_session {
498: my ($r) = @_;
499: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
500: my $lonid=$cookies{'lonID'};
501: return undef if (!$lonid);
502:
503: my $handle=&LONCAPA::clean_handle($lonid->value);
504: my $lonidsdir=$r->dir_config('lonIDsDir');
505: return undef if (!-e "$lonidsdir/$handle.id");
506:
1.917 albertel 507: my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
508: return undef if (!$opened);
1.916 albertel 509:
510: flock($idf,LOCK_SH);
511: my %disk_env;
512: if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
513: &GDBM_READER(),0640)) {
514: return undef;
515: }
516:
517: if (!defined($disk_env{'user.name'})
518: || !defined($disk_env{'user.domain'})) {
519: return undef;
520: }
521: return $handle;
522: }
523:
1.830 albertel 524: sub timed_flock {
525: my ($file,$lock_type) = @_;
526: my $failed=0;
527: eval {
528: local $SIG{__DIE__}='DEFAULT';
529: local $SIG{ALRM}=sub {
530: $failed=1;
531: die("failed lock");
532: };
533: alarm(13);
534: flock($file,$lock_type);
535: alarm(0);
536: };
537: if ($failed) {
538: return undef;
539: } else {
540: return 1;
541: }
542: }
543:
1.5 www 544: # ---------------------------------------------------------- Append Environment
545:
546: sub appenv {
1.949 raeburn 547: my ($newenv,$roles) = @_;
548: if (ref($newenv) eq 'HASH') {
549: foreach my $key (keys(%{$newenv})) {
550: my $refused = 0;
551: if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
552: $refused = 1;
553: if (ref($roles) eq 'ARRAY') {
554: my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
555: if (grep(/^\Q$role\E$/,@{$roles})) {
556: $refused = 0;
557: }
558: }
559: }
560: if ($refused) {
561: &logthis("<font color=\"blue\">WARNING: ".
562: "Attempt to modify environment ".$key." to ".$newenv->{$key}
563: .'</font>');
564: delete($newenv->{$key});
565: } else {
566: $env{$key}=$newenv->{$key};
567: }
568: }
569: my $opened = open(my $env_file,'+<',$env{'user.environment'});
570: if ($opened
571: && &timed_flock($env_file,LOCK_EX)
572: &&
573: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
574: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
575: while (my ($key,$value) = each(%{$newenv})) {
576: $disk_env{$key} = $value;
577: }
578: untie(%disk_env);
1.35 www 579: }
1.191 harris41 580: }
1.56 www 581: return 'ok';
582: }
583: # ----------------------------------------------------- Delete from Environment
584:
585: sub delenv {
1.987 raeburn 586: my ($delthis,$regexp) = @_;
1.56 www 587: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672 albertel 588: &logthis("<font color=\"blue\">WARNING: ".
1.56 www 589: "Attempt to delete from environment ".$delthis);
590: return 'error';
591: }
1.917 albertel 592: my $opened = open(my $env_file,'+<',$env{'user.environment'});
593: if ($opened
1.915 albertel 594: && &timed_flock($env_file,LOCK_EX)
1.830 albertel 595: &&
596: tie(my %disk_env,'GDBM_File',$env{'user.environment'},
597: (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783 albertel 598: foreach my $key (keys(%disk_env)) {
1.987 raeburn 599: if ($regexp) {
600: if ($key=~/^$delthis/) {
601: delete($env{$key});
602: delete($disk_env{$key});
603: }
604: } else {
605: if ($key=~/^\Q$delthis\E/) {
606: delete($env{$key});
607: delete($disk_env{$key});
608: }
609: }
1.448 albertel 610: }
1.783 albertel 611: untie(%disk_env);
1.5 www 612: }
613: return 'ok';
1.369 albertel 614: }
615:
1.790 albertel 616: sub get_env_multiple {
617: my ($name) = @_;
618: my @values;
619: if (defined($env{$name})) {
620: # exists is it an array
621: if (ref($env{$name})) {
622: @values=@{ $env{$name} };
623: } else {
624: $values[0]=$env{$name};
625: }
626: }
627: return(@values);
628: }
629:
1.958 www 630: # ------------------------------------------------------------------- Locking
631:
632: sub set_lock {
633: my ($text)=@_;
634: $locknum++;
635: my $id=$$.'-'.$locknum;
636: &appenv({'session.locks' => $env{'session.locks'}.','.$id,
637: 'session.lock.'.$id => $text});
638: return $id;
639: }
640:
641: sub get_locks {
642: my $num=0;
643: my %texts=();
644: foreach my $lock (split(/\,/,$env{'session.locks'})) {
645: if ($lock=~/\w/) {
646: $num++;
647: $texts{$lock}=$env{'session.lock.'.$lock};
648: }
649: }
650: return ($num,%texts);
651: }
652:
653: sub remove_lock {
654: my ($id)=@_;
655: my $newlocks='';
656: foreach my $lock (split(/\,/,$env{'session.locks'})) {
657: if (($lock=~/\w/) && ($lock ne $id)) {
658: $newlocks.=','.$lock;
659: }
660: }
661: &appenv({'session.locks' => $newlocks});
662: &delenv('session.lock.'.$id);
663: }
664:
665: sub remove_all_locks {
666: my $activelocks=$env{'session.locks'};
667: foreach my $lock (split(/\,/,$env{'session.locks'})) {
668: if ($lock=~/\w/) {
669: &remove_lock($lock);
670: }
671: }
672: }
673:
674:
1.369 albertel 675: # ------------------------------------------ Find out current server userload
676: sub userload {
677: my $numusers=0;
678: {
679: opendir(LONIDS,$perlvar{'lonIDsDir'});
680: my $filename;
681: my $curtime=time;
682: while ($filename=readdir(LONIDS)) {
1.925 albertel 683: next if ($filename eq '.' || $filename eq '..');
684: next if ($filename =~ /publicuser_\d+\.id/);
1.404 albertel 685: my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437 albertel 686: if ($curtime-$mtime < 1800) { $numusers++; }
1.369 albertel 687: }
688: closedir(LONIDS);
689: }
690: my $userloadpercent=0;
691: my $maxuserload=$perlvar{'lonUserLoadLim'};
692: if ($maxuserload) {
1.371 albertel 693: $userloadpercent=100*$numusers/$maxuserload;
1.369 albertel 694: }
1.372 albertel 695: $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369 albertel 696: return $userloadpercent;
1.283 www 697: }
698:
1.1 albertel 699: # ------------------------------ Find server with least workload from spare.tab
1.11 www 700:
1.1 albertel 701: sub spareserver {
1.670 albertel 702: my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784 albertel 703: my $spare_server;
1.370 albertel 704: if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784 albertel 705: my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent
706: : $userloadpercent;
707:
708: foreach my $try_server (@{ $spareid{'primary'} }) {
709: ($spare_server, $lowest_load) =
710: &compare_server_load($try_server, $spare_server, $lowest_load);
711: }
712:
713: my $found_server = ($spare_server ne '' && $lowest_load < 100);
714:
715: if (!$found_server) {
716: foreach my $try_server (@{ $spareid{'default'} }) {
717: ($spare_server, $lowest_load) =
718: &compare_server_load($try_server, $spare_server, $lowest_load);
719: }
720: }
721:
722: if (!$want_server_name) {
1.968 raeburn 723: my $protocol = 'http';
724: if ($protocol{$spare_server} eq 'https') {
725: $protocol = $protocol{$spare_server};
726: }
1.1001 raeburn 727: if (defined($spare_server)) {
728: my $hostname = &hostname($spare_server);
729: if (defined($hostname)) {
730: $spare_server = $protocol.'://'.$hostname;
731: }
732: }
1.784 albertel 733: }
734: return $spare_server;
735: }
736:
737: sub compare_server_load {
738: my ($try_server, $spare_server, $lowest_load) = @_;
739:
740: my $loadans = &reply('load', $try_server);
741: my $userloadans = &reply('userload',$try_server);
742:
743: if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1072 www 744: return; #didn't get a number from the server
1.784 albertel 745: }
746:
747: my $load;
748: if ($loadans =~ /\d/) {
749: if ($userloadans =~ /\d/) {
750: #both are numbers, pick the bigger one
751: $load = ($loadans > $userloadans) ? $loadans
752: : $userloadans;
1.411 albertel 753: } else {
1.784 albertel 754: $load = $loadans;
1.411 albertel 755: }
1.784 albertel 756: } else {
757: $load = $userloadans;
758: }
759:
760: if (($load =~ /\d/) && ($load < $lowest_load)) {
761: $spare_server = $try_server;
762: $lowest_load = $load;
1.370 albertel 763: }
1.784 albertel 764: return ($spare_server,$lowest_load);
1.202 matthew 765: }
1.914 albertel 766:
767: # --------------------------- ask offload servers if user already has a session
768: sub find_existing_session {
769: my ($udom,$uname) = @_;
770: foreach my $try_server (@{ $spareid{'primary'} },
771: @{ $spareid{'default'} }) {
772: return $try_server if (&has_user_session($try_server, $udom, $uname));
773: }
774: return;
775: }
776:
777: # -------------------------------- ask if server already has a session for user
778: sub has_user_session {
779: my ($lonid,$udom,$uname) = @_;
780: my $result = &reply(join(':','userhassession',
781: map {&escape($_)} ($udom,$uname)),$lonid);
782: return 1 if ($result eq 'ok');
783:
784: return 0;
785: }
786:
1.202 matthew 787: # --------------------------------------------- Try to change a user's password
788:
789: sub changepass {
1.799 raeburn 790: my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202 matthew 791: $currentpass = &escape($currentpass);
792: $newpass = &escape($newpass);
1.1030 raeburn 793: my $lonhost = $perlvar{'lonHostID'};
794: my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202 matthew 795: $server);
796: if (! $answer) {
797: &logthis("No reply on password change request to $server ".
798: "by $uname in domain $udom.");
799: } elsif ($answer =~ "^ok") {
800: &logthis("$uname in $udom successfully changed their password ".
801: "on $server.");
802: } elsif ($answer =~ "^pwchange_failure") {
803: &logthis("$uname in $udom was unable to change their password ".
804: "on $server. The action was blocked by either lcpasswd ".
805: "or pwchange");
806: } elsif ($answer =~ "^non_authorized") {
807: &logthis("$uname in $udom did not get their password correct when ".
808: "attempting to change it on $server.");
809: } elsif ($answer =~ "^auth_mode_error") {
810: &logthis("$uname in $udom attempted to change their password despite ".
811: "not being locally or internally authenticated on $server.");
812: } elsif ($answer =~ "^unknown_user") {
813: &logthis("$uname in $udom attempted to change their password ".
814: "on $server but were unable to because $server is not ".
815: "their home server.");
816: } elsif ($answer =~ "^refused") {
817: &logthis("$server refused to change $uname in $udom password because ".
818: "it was sent an unencrypted request to change the password.");
1.1030 raeburn 819: } elsif ($answer =~ "invalid_client") {
820: &logthis("$server refused to change $uname in $udom password because ".
821: "it was a reset by e-mail originating from an invalid server.");
1.202 matthew 822: }
823: return $answer;
1.1 albertel 824: }
825:
1.169 harris41 826: # ----------------------- Try to determine user's current authentication scheme
827:
828: sub queryauthenticate {
829: my ($uname,$udom)=@_;
1.456 albertel 830: my $uhome=&homeserver($uname,$udom);
831: if (!$uhome) {
832: &logthis("User $uname at $udom is unknown when looking for authentication mechanism");
833: return 'no_host';
834: }
835: my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
836: if ($answer =~ /^(unknown_user|refused|con_lost)/) {
837: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169 harris41 838: }
1.456 albertel 839: return $answer;
1.169 harris41 840: }
841:
1.1 albertel 842: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 843:
1.1 albertel 844: sub authenticate {
1.1073 ! raeburn 845: my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807 albertel 846: $upass=&escape($upass);
847: $uname= &LONCAPA::clean_username($uname);
1.836 www 848: my $uhome=&homeserver($uname,$udom,1);
1.952 raeburn 849: my $newhome;
1.836 www 850: if ((!$uhome) || ($uhome eq 'no_host')) {
851: # Maybe the machine was offline and only re-appeared again recently?
852: &reconlonc();
853: # One more
1.952 raeburn 854: $uhome=&homeserver($uname,$udom,1);
855: if (($uhome eq 'no_host') && $checkdefauth) {
856: if (defined(&domain($udom,'primary'))) {
857: $newhome=&domain($udom,'primary');
858: }
859: if ($newhome ne '') {
860: $uhome = $newhome;
861: }
862: }
1.836 www 863: if ((!$uhome) || ($uhome eq 'no_host')) {
864: &logthis("User $uname at $udom is unknown in authenticate");
1.952 raeburn 865: return 'no_host';
866: }
1.1 albertel 867: }
1.1073 ! raeburn 868: my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471 albertel 869: if ($answer eq 'authorized') {
1.952 raeburn 870: if ($newhome) {
871: &logthis("User $uname at $udom authorized by $uhome, but needs account");
872: return 'no_account_on_host';
873: } else {
874: &logthis("User $uname at $udom authorized by $uhome");
875: return $uhome;
876: }
1.471 albertel 877: }
878: if ($answer eq 'non_authorized') {
879: &logthis("User $uname at $udom rejected by $uhome");
880: return 'no_host';
1.9 www 881: }
1.471 albertel 882: &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1 albertel 883: return 'no_host';
884: }
885:
1.1073 ! raeburn 886: sub can_host_session {
! 887: my ($udom,$machinedom,$remoterev,$remotesessions,$hostedsessions) = @_;
! 888: my $canhost = 1;
! 889: if (ref($remotesessions) eq 'HASH') {
! 890: if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
! 891: if (grep(/^\Q$machinedom\E$/,@{$remotesessions->{'excludedomain'}})) {
! 892: $canhost = 0;
! 893: } else {
! 894: $canhost = 1;
! 895: }
! 896: }
! 897: if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
! 898: if (grep(/^\Q$machinedom\E$/,@{$remotesessions->{'includedomain'}})) {
! 899: $canhost = 1;
! 900: } else {
! 901: $canhost = 0;
! 902: }
! 903: }
! 904: if ($canhost) {
! 905: if ($remotesessions->{'version'} ne '') {
! 906: my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
! 907: if ($reqmajor ne '' && $reqminor ne '') {
! 908: if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
! 909: my $major = $1;
! 910: my $minor = $2;
! 911: if (($major < $reqmajor ) ||
! 912: (($major == $reqmajor) && ($minor < $reqminor))) {
! 913: $canhost = 0;
! 914: }
! 915: } else {
! 916: $canhost = 0;
! 917: }
! 918: }
! 919: }
! 920: }
! 921: }
! 922: if ($canhost) {
! 923: if (ref($hostedsessions) eq 'HASH') {
! 924: if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
! 925: if (grep(/^\Q$udom\E$/,@{$hostedsessions->{'excludedomain'}})) {
! 926: $canhost = 0;
! 927: } else {
! 928: $canhost = 1;
! 929: }
! 930: }
! 931: if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
! 932: if (grep(/^\Q$udom\E$/,@{$hostedsessions->{'includedomain'}})) {
! 933: $canhost = 1;
! 934: } else {
! 935: $canhost = 0;
! 936: }
! 937: }
! 938: }
! 939: }
! 940: return $canhost;
! 941: }
! 942:
1.1 albertel 943: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 944:
1.599 albertel 945: my %homecache;
1.1 albertel 946: sub homeserver {
1.230 stredwic 947: my ($uname,$udom,$ignoreBadCache)=@_;
1.1 albertel 948: my $index="$uname:$udom";
1.426 albertel 949:
1.599 albertel 950: if (exists($homecache{$index})) { return $homecache{$index}; }
1.841 albertel 951:
952: my %servers = &get_servers($udom,'library');
953: foreach my $tryserver (keys(%servers)) {
1.230 stredwic 954: next if ($ignoreBadCache ne 'true' &&
1.231 stredwic 955: exists($badServerCache{$tryserver}));
1.841 albertel 956:
957: my $answer=reply("home:$udom:$uname",$tryserver);
958: if ($answer eq 'found') {
959: delete($badServerCache{$tryserver});
960: return $homecache{$index}=$tryserver;
961: } elsif ($answer eq 'no_host') {
962: $badServerCache{$tryserver}=1;
963: }
1.1 albertel 964: }
965: return 'no_host';
1.70 www 966: }
967:
968: # ------------------------------------- Find the usernames behind a list of IDs
969:
970: sub idget {
971: my ($udom,@ids)=@_;
972: my %returnhash=();
973:
1.841 albertel 974: my %servers = &get_servers($udom,'library');
975: foreach my $tryserver (keys(%servers)) {
976: my $idlist=join('&',@ids);
977: $idlist=~tr/A-Z/a-z/;
978: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
979: my @answer=();
980: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
981: @answer=split(/\&/,$reply);
982: } ;
983: my $i;
984: for ($i=0;$i<=$#ids;$i++) {
985: if ($answer[$i]) {
986: $returnhash{$ids[$i]}=$answer[$i];
987: }
988: }
989: }
1.70 www 990: return %returnhash;
991: }
992:
993: # ------------------------------------- Find the IDs behind a list of usernames
994:
995: sub idrget {
996: my ($udom,@unames)=@_;
997: my %returnhash=();
1.800 albertel 998: foreach my $uname (@unames) {
999: $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191 harris41 1000: }
1.70 www 1001: return %returnhash;
1002: }
1003:
1004: # ------------------------------- Store away a list of names and associated IDs
1005:
1006: sub idput {
1007: my ($udom,%ids)=@_;
1008: my %servers=();
1.800 albertel 1009: foreach my $uname (keys(%ids)) {
1010: &cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1011: my $uhom=&homeserver($uname,$udom);
1.70 www 1012: if ($uhom ne 'no_host') {
1.800 albertel 1013: my $id=&escape($ids{$uname});
1.70 www 1014: $id=~tr/A-Z/a-z/;
1.800 albertel 1015: my $esc_unam=&escape($uname);
1.70 www 1016: if ($servers{$uhom}) {
1.800 albertel 1017: $servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70 www 1018: } else {
1.800 albertel 1019: $servers{$uhom}=$id.'='.$esc_unam;
1.70 www 1020: }
1021: }
1.191 harris41 1022: }
1.800 albertel 1023: foreach my $server (keys(%servers)) {
1024: &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191 harris41 1025: }
1.344 www 1026: }
1027:
1.1023 raeburn 1028: # ------------------------------dump from db file owned by domainconfig user
1.1012 raeburn 1029: sub dump_dom {
1.1023 raeburn 1030: my ($namespace,$udom,$regexp,$range)=@_;
1.1012 raeburn 1031: if (!$udom) {
1032: $udom=$env{'user.domain'};
1033: }
1034: my %returnhash;
1.1023 raeburn 1035: if ($udom) {
1036: my $uname = &get_domainconfiguser($udom);
1037: %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012 raeburn 1038: }
1039: return %returnhash;
1040: }
1041:
1.1023 raeburn 1042: # ------------------------------------------ get items from domain db files
1.806 raeburn 1043:
1044: sub get_dom {
1.860 raeburn 1045: my ($namespace,$storearr,$udom,$uhome)=@_;
1.806 raeburn 1046: my $items='';
1047: foreach my $item (@$storearr) {
1048: $items.=&escape($item).'&';
1049: }
1050: $items=~s/\&$//;
1.860 raeburn 1051: if (!$udom) {
1052: $udom=$env{'user.domain'};
1053: if (defined(&domain($udom,'primary'))) {
1054: $uhome=&domain($udom,'primary');
1055: } else {
1.874 albertel 1056: undef($uhome);
1.860 raeburn 1057: }
1058: } else {
1059: if (!$uhome) {
1060: if (defined(&domain($udom,'primary'))) {
1061: $uhome=&domain($udom,'primary');
1062: }
1063: }
1064: }
1065: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1066: my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866 raeburn 1067: my %returnhash;
1.875 albertel 1068: if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866 raeburn 1069: return %returnhash;
1070: }
1.806 raeburn 1071: my @pairs=split(/\&/,$rep);
1072: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
1073: return @pairs;
1074: }
1075: my $i=0;
1076: foreach my $item (@$storearr) {
1077: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1078: $i++;
1079: }
1080: return %returnhash;
1081: } else {
1.880 banghart 1082: &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806 raeburn 1083: }
1084: }
1085:
1086: # -------------------------------------------- put items in domain db files
1087:
1088: sub put_dom {
1.860 raeburn 1089: my ($namespace,$storehash,$udom,$uhome)=@_;
1090: if (!$udom) {
1091: $udom=$env{'user.domain'};
1092: if (defined(&domain($udom,'primary'))) {
1093: $uhome=&domain($udom,'primary');
1094: } else {
1.874 albertel 1095: undef($uhome);
1.860 raeburn 1096: }
1097: } else {
1098: if (!$uhome) {
1099: if (defined(&domain($udom,'primary'))) {
1100: $uhome=&domain($udom,'primary');
1101: }
1102: }
1103: }
1104: if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806 raeburn 1105: my $items='';
1106: foreach my $item (keys(%$storehash)) {
1107: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1108: }
1109: $items=~s/\&$//;
1110: return &reply("putdom:$udom:$namespace:$items",$uhome);
1111: } else {
1.860 raeburn 1112: &logthis("put_dom failed - no homeserver and/or domain");
1.806 raeburn 1113: }
1114: }
1115:
1.1023 raeburn 1116: # --------------------- newput for items in db file owned by domainconfig user
1.1012 raeburn 1117: sub newput_dom {
1.1023 raeburn 1118: my ($namespace,$storehash,$udom) = @_;
1.1012 raeburn 1119: my $result;
1120: if (!$udom) {
1121: $udom=$env{'user.domain'};
1122: }
1.1023 raeburn 1123: if ($udom) {
1124: my $uname = &get_domainconfiguser($udom);
1125: $result = &newput($namespace,$storehash,$udom,$uname);
1.1012 raeburn 1126: }
1127: return $result;
1128: }
1129:
1.1023 raeburn 1130: # --------------------- delete for items in db file owned by domainconfig user
1.1012 raeburn 1131: sub del_dom {
1.1023 raeburn 1132: my ($namespace,$storearr,$udom)=@_;
1.1012 raeburn 1133: if (ref($storearr) eq 'ARRAY') {
1134: if (!$udom) {
1135: $udom=$env{'user.domain'};
1136: }
1.1023 raeburn 1137: if ($udom) {
1138: my $uname = &get_domainconfiguser($udom);
1139: return &del($namespace,$storearr,$udom,$uname);
1.1012 raeburn 1140: }
1141: }
1142: }
1143:
1.1023 raeburn 1144: # ----------------------------------construct domainconfig user for a domain
1145: sub get_domainconfiguser {
1146: my ($udom) = @_;
1147: return $udom.'-domainconfig';
1148: }
1149:
1.837 raeburn 1150: sub retrieve_inst_usertypes {
1151: my ($udom) = @_;
1152: my (%returnhash,@order);
1.989 raeburn 1153: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1154: if ((ref($domdefs{'inststatustypes'}) eq 'HASH') &&
1155: (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1156: %returnhash = %{$domdefs{'inststatustypes'}};
1157: @order = @{$domdefs{'inststatusorder'}};
1158: } else {
1159: if (defined(&domain($udom,'primary'))) {
1160: my $uhome=&domain($udom,'primary');
1161: my $rep=&reply("inst_usertypes:$udom",$uhome);
1162: if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1163: &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
1164: return (\%returnhash,\@order);
1165: }
1166: my ($hashitems,$orderitems) = split(/:/,$rep);
1167: my @pairs=split(/\&/,$hashitems);
1168: foreach my $item (@pairs) {
1169: my ($key,$value)=split(/=/,$item,2);
1170: $key = &unescape($key);
1171: next if ($key =~ /^error: 2 /);
1172: $returnhash{$key}=&thaw_unescape($value);
1173: }
1174: my @esc_order = split(/\&/,$orderitems);
1175: foreach my $item (@esc_order) {
1176: push(@order,&unescape($item));
1177: }
1178: } else {
1179: &logthis("get_dom failed - no primary domain server for $udom");
1.837 raeburn 1180: }
1181: }
1182: return (\%returnhash,\@order);
1183: }
1184:
1.868 raeburn 1185: sub is_domainimage {
1186: my ($url) = @_;
1187: if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
1188: if (&domain($1) ne '') {
1189: return '1';
1190: }
1191: }
1192: return;
1193: }
1194:
1.899 raeburn 1195: sub inst_directory_query {
1196: my ($srch) = @_;
1197: my $udom = $srch->{'srchdomain'};
1198: my %results;
1199: my $homeserver = &domain($udom,'primary');
1.909 raeburn 1200: my $outcome;
1.899 raeburn 1201: if ($homeserver ne '') {
1.904 albertel 1202: my $queryid=&reply("querysend:instdirsearch:".
1203: &escape($srch->{'srchby'}).':'.
1204: &escape($srch->{'srchterm'}).':'.
1205: &escape($srch->{'srchtype'}),$homeserver);
1206: my $host=&hostname($homeserver);
1207: if ($queryid !~/^\Q$host\E\_/) {
1208: &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1209: return;
1210: }
1211: my $response = &get_query_reply($queryid);
1212: my $maxtries = 5;
1213: my $tries = 1;
1214: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1215: $response = &get_query_reply($queryid);
1216: $tries ++;
1217: }
1218:
1219: if (!&error($response) && $response ne 'refused') {
1.909 raeburn 1220: if ($response eq 'unavailable') {
1221: $outcome = $response;
1222: } else {
1223: $outcome = 'ok';
1224: my @matches = split(/\n/,$response);
1225: foreach my $match (@matches) {
1226: my ($key,$value) = split(/=/,$match);
1227: $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
1228: }
1.899 raeburn 1229: }
1230: }
1231: }
1.909 raeburn 1232: return ($outcome,%results);
1.899 raeburn 1233: }
1234:
1235: sub usersearch {
1236: my ($srch) = @_;
1237: my $dom = $srch->{'srchdomain'};
1238: my %results;
1239: my %libserv = &all_library();
1240: my $query = 'usersearch';
1241: foreach my $tryserver (keys(%libserv)) {
1242: if (&host_domain($tryserver) eq $dom) {
1243: my $host=&hostname($tryserver);
1244: my $queryid=
1.911 raeburn 1245: &reply("querysend:".&escape($query).':'.
1246: &escape($srch->{'srchby'}).':'.
1.899 raeburn 1247: &escape($srch->{'srchtype'}).':'.
1248: &escape($srch->{'srchterm'}),$tryserver);
1249: if ($queryid !~/^\Q$host\E\_/) {
1250: &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902 raeburn 1251: next;
1.899 raeburn 1252: }
1253: my $reply = &get_query_reply($queryid);
1254: my $maxtries = 1;
1255: my $tries = 1;
1256: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1257: $reply = &get_query_reply($queryid);
1258: $tries ++;
1259: }
1260: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1261: &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') - maxtries: '.$maxtries.' tries: '.$tries);
1262: } else {
1.911 raeburn 1263: my @matches;
1264: if ($reply =~ /\n/) {
1265: @matches = split(/\n/,$reply);
1266: } else {
1267: @matches = split(/\&/,$reply);
1268: }
1.899 raeburn 1269: foreach my $match (@matches) {
1270: my ($uname,$udom,%userhash);
1.911 raeburn 1271: foreach my $entry (split(/:/,$match)) {
1272: my ($key,$value) =
1273: map {&unescape($_);} split(/=/,$entry);
1.899 raeburn 1274: $userhash{$key} = $value;
1275: if ($key eq 'username') {
1276: $uname = $value;
1277: } elsif ($key eq 'domain') {
1278: $udom = $value;
1.911 raeburn 1279: }
1.899 raeburn 1280: }
1281: $results{$uname.':'.$udom} = \%userhash;
1282: }
1283: }
1284: }
1285: }
1286: return %results;
1287: }
1288:
1.912 raeburn 1289: sub get_instuser {
1290: my ($udom,$uname,$id) = @_;
1291: my $homeserver = &domain($udom,'primary');
1292: my ($outcome,%results);
1293: if ($homeserver ne '') {
1294: my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
1295: &escape($id).':'.&escape($udom),$homeserver);
1296: my $host=&hostname($homeserver);
1297: if ($queryid !~/^\Q$host\E\_/) {
1298: &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
1299: return;
1300: }
1301: my $response = &get_query_reply($queryid);
1302: my $maxtries = 5;
1303: my $tries = 1;
1304: while (($response=~/^timeout/) && ($tries < $maxtries)) {
1305: $response = &get_query_reply($queryid);
1306: $tries ++;
1307: }
1308: if (!&error($response) && $response ne 'refused') {
1309: if ($response eq 'unavailable') {
1310: $outcome = $response;
1311: } else {
1312: $outcome = 'ok';
1313: my @matches = split(/\n/,$response);
1314: foreach my $match (@matches) {
1315: my ($key,$value) = split(/=/,$match);
1316: $results{&unescape($key)} = &thaw_unescape($value);
1317: }
1318: }
1319: }
1320: }
1321: my %userinfo;
1322: if (ref($results{$uname}) eq 'HASH') {
1323: %userinfo = %{$results{$uname}};
1324: }
1325: return ($outcome,%userinfo);
1326: }
1327:
1328: sub inst_rulecheck {
1.923 raeburn 1329: my ($udom,$uname,$id,$item,$rules) = @_;
1.912 raeburn 1330: my %returnhash;
1331: if ($udom ne '') {
1332: if (ref($rules) eq 'ARRAY') {
1333: @{$rules} = map {&escape($_);} (@{$rules});
1334: my $rulestr = join(':',@{$rules});
1335: my $homeserver=&domain($udom,'primary');
1336: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1337: my $response;
1338: if ($item eq 'username') {
1339: $response=&unescape(&reply('instrulecheck:'.&escape($udom).
1340: ':'.&escape($uname).':'.$rulestr,
1.912 raeburn 1341: $homeserver));
1.923 raeburn 1342: } elsif ($item eq 'id') {
1343: $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
1344: ':'.&escape($id).':'.$rulestr,
1345: $homeserver));
1.945 raeburn 1346: } elsif ($item eq 'selfcreate') {
1347: $response=&unescape(&reply('instselfcreatecheck:'.
1.943 raeburn 1348: &escape($udom).':'.&escape($uname).
1349: ':'.$rulestr,$homeserver));
1.923 raeburn 1350: }
1.912 raeburn 1351: if ($response ne 'refused') {
1352: my @pairs=split(/\&/,$response);
1353: foreach my $item (@pairs) {
1354: my ($key,$value)=split(/=/,$item,2);
1355: $key = &unescape($key);
1356: next if ($key =~ /^error: 2 /);
1357: $returnhash{$key}=&thaw_unescape($value);
1358: }
1359: }
1360: }
1361: }
1362: }
1363: return %returnhash;
1364: }
1365:
1366: sub inst_userrules {
1.923 raeburn 1367: my ($udom,$check) = @_;
1.912 raeburn 1368: my (%ruleshash,@ruleorder);
1369: if ($udom ne '') {
1370: my $homeserver=&domain($udom,'primary');
1371: if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923 raeburn 1372: my $response;
1373: if ($check eq 'id') {
1374: $response=&reply('instidrules:'.&escape($udom),
1.912 raeburn 1375: $homeserver);
1.943 raeburn 1376: } elsif ($check eq 'email') {
1377: $response=&reply('instemailrules:'.&escape($udom),
1378: $homeserver);
1.923 raeburn 1379: } else {
1380: $response=&reply('instuserrules:'.&escape($udom),
1381: $homeserver);
1382: }
1.912 raeburn 1383: if (($response ne 'refused') && ($response ne 'error') &&
1.923 raeburn 1384: ($response ne 'unknown_cmd') &&
1.912 raeburn 1385: ($response ne 'no_such_host')) {
1386: my ($hashitems,$orderitems) = split(/:/,$response);
1387: my @pairs=split(/\&/,$hashitems);
1388: foreach my $item (@pairs) {
1389: my ($key,$value)=split(/=/,$item,2);
1390: $key = &unescape($key);
1391: next if ($key =~ /^error: 2 /);
1392: $ruleshash{$key}=&thaw_unescape($value);
1393: }
1394: my @esc_order = split(/\&/,$orderitems);
1395: foreach my $item (@esc_order) {
1396: push(@ruleorder,&unescape($item));
1397: }
1398: }
1399: }
1400: }
1401: return (\%ruleshash,\@ruleorder);
1402: }
1403:
1.976 raeburn 1404: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943 raeburn 1405:
1406: sub get_domain_defaults {
1407: my ($domain) = @_;
1408: my $cachetime = 60*60*24;
1409: my ($result,$cached)=&is_cached_new('domdefaults',$domain);
1410: if (defined($cached)) {
1411: if (ref($result) eq 'HASH') {
1412: return %{$result};
1413: }
1414: }
1415: my %domdefaults;
1416: my %domconfig =
1.989 raeburn 1417: &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047 raeburn 1418: 'requestcourses','inststatus',
1.1073 ! raeburn 1419: 'coursedefaults','usersessions'],$domain);
1.943 raeburn 1420: if (ref($domconfig{'defaults'}) eq 'HASH') {
1421: $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};
1422: $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
1423: $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982 raeburn 1424: $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985 raeburn 1425: $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.943 raeburn 1426: } else {
1427: $domdefaults{'lang_def'} = &domain($domain,'lang_def');
1428: $domdefaults{'auth_def'} = &domain($domain,'auth_def');
1429: $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
1430: }
1.976 raeburn 1431: if (ref($domconfig{'quotas'}) eq 'HASH') {
1432: if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
1433: $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
1434: } else {
1435: $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1436: }
1437: my @usertools = ('aboutme','blog','portfolio');
1438: foreach my $item (@usertools) {
1439: if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
1440: $domdefaults{$item} = $domconfig{'quotas'}{$item};
1441: }
1442: }
1443: }
1.985 raeburn 1444: if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006 raeburn 1445: foreach my $item ('official','unofficial','community') {
1.985 raeburn 1446: $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
1447: }
1448: }
1.989 raeburn 1449: if (ref($domconfig{'inststatus'}) eq 'HASH') {
1450: foreach my $item ('inststatustypes','inststatusorder') {
1451: $domdefaults{$item} = $domconfig{'inststatus'}{$item};
1452: }
1453: }
1.1047 raeburn 1454: if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1455: foreach my $item ('canuse_pdfforms') {
1456: $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
1457: }
1458: }
1.1073 ! raeburn 1459: if (ref($domconfig{'usersessions'}) eq 'HASH') {
! 1460: if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
! 1461: $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
! 1462: }
! 1463: if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
! 1464: $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
! 1465: }
! 1466: }
1.943 raeburn 1467: &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
1468: $cachetime);
1469: return %domdefaults;
1470: }
1471:
1.344 www 1472: # --------------------------------------------------- Assign a key to a student
1473:
1474: sub assign_access_key {
1.364 www 1475: #
1476: # a valid key looks like uname:udom#comments
1477: # comments are being appended
1478: #
1.498 www 1479: my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
1480: $kdom=
1.620 albertel 1481: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498 www 1482: $knum=
1.620 albertel 1483: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344 www 1484: $cdom=
1.620 albertel 1485: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1486: $cnum=
1.620 albertel 1487: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1488: $udom=$env{'user.name'} unless (defined($udom));
1489: $uname=$env{'user.domain'} unless (defined($uname));
1.498 www 1490: my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364 www 1491: if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479 albertel 1492: ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) {
1.364 www 1493: # assigned to this person
1494: # - this should not happen,
1.345 www 1495: # unless something went wrong
1496: # the first time around
1497: # ready to assign
1.364 www 1498: $logentry=$1.'; '.$logentry;
1.496 www 1499: if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498 www 1500: $kdom,$knum) eq 'ok') {
1.345 www 1501: # key now belongs to user
1.346 www 1502: my $envkey='key.'.$cdom.'_'.$cnum;
1.345 www 1503: if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949 raeburn 1504: &appenv({'environment.'.$envkey => $ckey});
1.345 www 1505: return 'ok';
1506: } else {
1507: return
1508: 'error: Count not permanently assign key, will need to be re-entered later.';
1509: }
1510: } else {
1511: return 'error: Could not assign key, try again later.';
1512: }
1.364 www 1513: } elsif (!$existing{$ckey}) {
1.345 www 1514: # the key does not exist
1515: return 'error: The key does not exist';
1516: } else {
1517: # the key is somebody else's
1518: return 'error: The key is already in use';
1519: }
1.344 www 1520: }
1521:
1.364 www 1522: # ------------------------------------------ put an additional comment on a key
1523:
1524: sub comment_access_key {
1525: #
1526: # a valid key looks like uname:udom#comments
1527: # comments are being appended
1528: #
1529: my ($ckey,$cdom,$cnum,$logentry)=@_;
1530: $cdom=
1.620 albertel 1531: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364 www 1532: $cnum=
1.620 albertel 1533: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364 www 1534: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1535: if ($existing{$ckey}) {
1536: $existing{$ckey}.='; '.$logentry;
1537: # ready to assign
1.367 www 1538: if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364 www 1539: $cdom,$cnum) eq 'ok') {
1540: return 'ok';
1541: } else {
1542: return 'error: Count not store comment.';
1543: }
1544: } else {
1545: # the key does not exist
1546: return 'error: The key does not exist';
1547: }
1548: }
1549:
1.344 www 1550: # ------------------------------------------------------ Generate a set of keys
1551:
1552: sub generate_access_keys {
1.364 www 1553: my ($number,$cdom,$cnum,$logentry)=@_;
1.344 www 1554: $cdom=
1.620 albertel 1555: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1556: $cnum=
1.620 albertel 1557: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361 www 1558: unless (&allowed('mky',$cdom)) { return 0; }
1.344 www 1559: unless (($cdom) && ($cnum)) { return 0; }
1560: if ($number>10000) { return 0; }
1561: sleep(2); # make sure don't get same seed twice
1562: srand(time()^($$+($$<<15))); # from "Programming Perl"
1563: my $total=0;
1564: for (my $i=1;$i<=$number;$i++) {
1565: my $newkey=sprintf("%lx",int(100000*rand)).'-'.
1566: sprintf("%lx",int(100000*rand)).'-'.
1567: sprintf("%lx",int(100000*rand));
1568: $newkey=~s/1/g/g; # folks mix up 1 and l
1569: $newkey=~s/0/h/g; # and also 0 and O
1570: my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
1571: if ($existing{$newkey}) {
1572: $i--;
1573: } else {
1.364 www 1574: if (&put('accesskeys',
1575: { $newkey => '# generated '.localtime().
1.620 albertel 1576: ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364 www 1577: '; '.$logentry },
1578: $cdom,$cnum) eq 'ok') {
1.344 www 1579: $total++;
1580: }
1581: }
1582: }
1.620 albertel 1583: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344 www 1584: 'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
1585: return $total;
1586: }
1587:
1588: # ------------------------------------------------------- Validate an accesskey
1589:
1590: sub validate_access_key {
1591: my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
1592: $cdom=
1.620 albertel 1593: $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344 www 1594: $cnum=
1.620 albertel 1595: $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1596: $udom=$env{'user.domain'} unless (defined($udom));
1597: $uname=$env{'user.name'} unless (defined($uname));
1.345 www 1598: my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479 albertel 1599: return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70 www 1600: }
1601:
1602: # ------------------------------------- Find the section of student in a course
1.652 albertel 1603: sub devalidate_getsection_cache {
1604: my ($udom,$unam,$courseid)=@_;
1605: my $hashid="$udom:$unam:$courseid";
1606: &devalidate_cache_new('getsection',$hashid);
1607: }
1.298 matthew 1608:
1.815 albertel 1609: sub courseid_to_courseurl {
1610: my ($courseid) = @_;
1611: #already url style courseid
1612: return $courseid if ($courseid =~ m{^/});
1613:
1614: if (exists($env{'course.'.$courseid.'.num'})) {
1615: my $cnum = $env{'course.'.$courseid.'.num'};
1616: my $cdom = $env{'course.'.$courseid.'.domain'};
1617: return "/$cdom/$cnum";
1618: }
1619:
1620: my %courseinfo=&Apache::lonnet::coursedescription($courseid);
1621: if (exists($courseinfo{'num'})) {
1622: return "/$courseinfo{'domain'}/$courseinfo{'num'}";
1623: }
1624:
1625: return undef;
1626: }
1627:
1.298 matthew 1628: sub getsection {
1629: my ($udom,$unam,$courseid)=@_;
1.599 albertel 1630: my $cachetime=1800;
1.551 albertel 1631:
1632: my $hashid="$udom:$unam:$courseid";
1.599 albertel 1633: my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551 albertel 1634: if (defined($cached)) { return $result; }
1635:
1.298 matthew 1636: my %Pending;
1637: my %Expired;
1638: #
1639: # Each role can either have not started yet (pending), be active,
1640: # or have expired.
1641: #
1642: # If there is an active role, we are done.
1643: #
1644: # If there is more than one role which has not started yet,
1645: # choose the one which will start sooner
1646: # If there is one role which has not started yet, return it.
1647: #
1648: # If there is more than one expired role, choose the one which ended last.
1649: # If there is a role which has expired, return it.
1650: #
1.815 albertel 1651: $courseid = &courseid_to_courseurl($courseid);
1.817 raeburn 1652: my %roleshash = &dump('roles',$udom,$unam,$courseid);
1653: foreach my $key (keys(%roleshash)) {
1.479 albertel 1654: next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298 matthew 1655: my $section=$1;
1656: if ($key eq $courseid.'_st') { $section=''; }
1.817 raeburn 1657: my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298 matthew 1658: my $now=time;
1.548 albertel 1659: if (defined($end) && $end && ($now > $end)) {
1.298 matthew 1660: $Expired{$end}=$section;
1661: next;
1662: }
1.548 albertel 1663: if (defined($start) && $start && ($now < $start)) {
1.298 matthew 1664: $Pending{$start}=$section;
1665: next;
1666: }
1.599 albertel 1667: return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298 matthew 1668: }
1669: #
1670: # Presumedly there will be few matching roles from the above
1671: # loop and the sorting time will be negligible.
1672: if (scalar(keys(%Pending))) {
1673: my ($time) = sort {$a <=> $b} keys(%Pending);
1.599 albertel 1674: return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298 matthew 1675: }
1676: if (scalar(keys(%Expired))) {
1677: my @sorted = sort {$a <=> $b} keys(%Expired);
1678: my $time = pop(@sorted);
1.599 albertel 1679: return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298 matthew 1680: }
1.599 albertel 1681: return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298 matthew 1682: }
1.70 www 1683:
1.599 albertel 1684: sub save_cache {
1685: &purge_remembered();
1.722 albertel 1686: #&Apache::loncommon::validate_page();
1.620 albertel 1687: undef(%env);
1.780 albertel 1688: undef($env_loaded);
1.599 albertel 1689: }
1.452 albertel 1690:
1.599 albertel 1691: my $to_remember=-1;
1692: my %remembered;
1693: my %accessed;
1694: my $kicks=0;
1695: my $hits=0;
1.849 albertel 1696: sub make_key {
1697: my ($name,$id) = @_;
1.872 albertel 1698: if (length($id) > 65
1699: && length(&escape($id)) > 200) {
1700: $id=length($id).':'.&Digest::MD5::md5_hex($id);
1701: }
1.849 albertel 1702: return &escape($name.':'.$id);
1703: }
1704:
1.599 albertel 1705: sub devalidate_cache_new {
1706: my ($name,$id,$debug) = @_;
1707: if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849 albertel 1708: $id=&make_key($name,$id);
1.599 albertel 1709: $memcache->delete($id);
1710: delete($remembered{$id});
1711: delete($accessed{$id});
1712: }
1713:
1714: sub is_cached_new {
1715: my ($name,$id,$debug) = @_;
1.849 albertel 1716: $id=&make_key($name,$id);
1.599 albertel 1717: if (exists($remembered{$id})) {
1718: if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
1719: $accessed{$id}=[&gettimeofday()];
1720: $hits++;
1721: return ($remembered{$id},1);
1722: }
1723: my $value = $memcache->get($id);
1724: if (!(defined($value))) {
1725: if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417 albertel 1726: return (undef,undef);
1.416 albertel 1727: }
1.599 albertel 1728: if ($value eq '__undef__') {
1729: if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
1730: $value=undef;
1731: }
1732: &make_room($id,$value,$debug);
1733: if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
1734: return ($value,1);
1735: }
1736:
1737: sub do_cache_new {
1738: my ($name,$id,$value,$time,$debug) = @_;
1.849 albertel 1739: $id=&make_key($name,$id);
1.599 albertel 1740: my $setvalue=$value;
1741: if (!defined($setvalue)) {
1742: $setvalue='__undef__';
1743: }
1.623 albertel 1744: if (!defined($time) ) {
1745: $time=600;
1746: }
1.599 albertel 1747: if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910 albertel 1748: my $result = $memcache->set($id,$setvalue,$time);
1749: if (! $result) {
1.872 albertel 1750: &logthis("caching of id -> $id failed");
1.910 albertel 1751: $memcache->disconnect_all();
1.872 albertel 1752: }
1.600 albertel 1753: # need to make a copy of $value
1.919 albertel 1754: &make_room($id,$value,$debug);
1.599 albertel 1755: return $value;
1756: }
1757:
1758: sub make_room {
1759: my ($id,$value,$debug)=@_;
1.919 albertel 1760:
1761: $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
1762: : $value;
1.599 albertel 1763: if ($to_remember<0) { return; }
1764: $accessed{$id}=[&gettimeofday()];
1765: if (scalar(keys(%remembered)) <= $to_remember) { return; }
1766: my $to_kick;
1767: my $max_time=0;
1768: foreach my $other (keys(%accessed)) {
1769: if (&tv_interval($accessed{$other}) > $max_time) {
1770: $to_kick=$other;
1771: $max_time=&tv_interval($accessed{$other});
1772: }
1773: }
1774: delete($remembered{$to_kick});
1775: delete($accessed{$to_kick});
1776: $kicks++;
1777: if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541 albertel 1778: return;
1779: }
1780:
1.599 albertel 1781: sub purge_remembered {
1.604 albertel 1782: #&logthis("Tossing ".scalar(keys(%remembered)));
1783: #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599 albertel 1784: undef(%remembered);
1785: undef(%accessed);
1.428 albertel 1786: }
1.70 www 1787: # ------------------------------------- Read an entry from a user's environment
1788:
1789: sub userenvironment {
1790: my ($udom,$unam,@what)=@_;
1.976 raeburn 1791: my $items;
1792: foreach my $item (@what) {
1793: $items.=&escape($item).'&';
1794: }
1795: $items=~s/\&$//;
1.70 www 1796: my %returnhash=();
1.1009 raeburn 1797: my $uhome = &homeserver($unam,$udom);
1798: unless ($uhome eq 'no_host') {
1799: my @answer=split(/\&/,
1800: &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048 raeburn 1801: if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
1802: return %returnhash;
1803: }
1.1009 raeburn 1804: my $i;
1805: for ($i=0;$i<=$#what;$i++) {
1806: $returnhash{$what[$i]}=&unescape($answer[$i]);
1807: }
1.70 www 1808: }
1809: return %returnhash;
1.1 albertel 1810: }
1811:
1.617 albertel 1812: # ---------------------------------------------------------- Get a studentphoto
1813: sub studentphoto {
1814: my ($udom,$unam,$ext) = @_;
1815: my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706 raeburn 1816: if (defined($env{'request.course.id'})) {
1.708 raeburn 1817: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706 raeburn 1818: if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
1819: return(&retrievestudentphoto($udom,$unam,$ext));
1820: } else {
1821: my ($result,$perm_reqd)=
1.707 albertel 1822: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1823: if ($result eq 'ok') {
1824: if (!($perm_reqd eq 'yes')) {
1825: return(&retrievestudentphoto($udom,$unam,$ext));
1826: }
1827: }
1828: }
1829: }
1830: } else {
1831: my ($result,$perm_reqd) =
1.707 albertel 1832: &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706 raeburn 1833: if ($result eq 'ok') {
1834: if (!($perm_reqd eq 'yes')) {
1835: return(&retrievestudentphoto($udom,$unam,$ext));
1836: }
1837: }
1838: }
1839: return '/adm/lonKaputt/lonlogo_broken.gif';
1840: }
1841:
1842: sub retrievestudentphoto {
1843: my ($udom,$unam,$ext,$type) = @_;
1844: my $home=&Apache::lonnet::homeserver($unam,$udom);
1845: my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
1846: if ($ret eq 'ok') {
1847: my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
1848: if ($type eq 'thumbnail') {
1849: $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext";
1850: }
1851: my $tokenurl=&Apache::lonnet::tokenwrapper($url);
1852: return $tokenurl;
1853: } else {
1854: if ($type eq 'thumbnail') {
1855: return '/adm/lonKaputt/genericstudent_tn.gif';
1856: } else {
1857: return '/adm/lonKaputt/lonlogo_broken.gif';
1858: }
1.617 albertel 1859: }
1860: }
1861:
1.263 www 1862: # -------------------------------------------------------------------- New chat
1863:
1864: sub chatsend {
1.724 raeburn 1865: my ($newentry,$anon,$group)=@_;
1.620 albertel 1866: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1867: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1868: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263 www 1869: &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620 albertel 1870: &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724 raeburn 1871: &escape($newentry)).':'.$group,$chome);
1.292 www 1872: }
1873:
1874: # ------------------------------------------ Find current version of a resource
1875:
1876: sub getversion {
1877: my $fname=&clutter(shift);
1878: unless ($fname=~/^\/res\//) { return -1; }
1879: return ¤tversion(&filelocation('',$fname));
1880: }
1881:
1882: sub currentversion {
1883: my $fname=shift;
1.599 albertel 1884: my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440 www 1885: if (defined($cached)) { return $result; }
1.292 www 1886: my $author=$fname;
1887: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1888: my ($udom,$uname)=split(/\//,$author);
1889: my $home=homeserver($uname,$udom);
1890: if ($home eq 'no_host') {
1891: return -1;
1892: }
1893: my $answer=reply("currentversion:$fname",$home);
1894: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1895: return -1;
1896: }
1.599 albertel 1897: return &do_cache_new('resversion',$fname,$answer,600);
1.263 www 1898: }
1899:
1.1 albertel 1900: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 1901:
1.1 albertel 1902: sub subscribe {
1903: my $fname=shift;
1.761 raeburn 1904: if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532 albertel 1905: $fname=~s/[\n\r]//g;
1.1 albertel 1906: my $author=$fname;
1907: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1908: my ($udom,$uname)=split(/\//,$author);
1909: my $home=homeserver($uname,$udom);
1.335 albertel 1910: if ($home eq 'no_host') {
1911: return 'not_found';
1.1 albertel 1912: }
1913: my $answer=reply("sub:$fname",$home);
1.64 www 1914: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
1915: $answer.=' by '.$home;
1916: }
1.1 albertel 1917: return $answer;
1918: }
1919:
1.8 www 1920: # -------------------------------------------------------------- Replicate file
1921:
1922: sub repcopy {
1923: my $filename=shift;
1.23 www 1924: $filename=~s/\/+/\//g;
1.607 raeburn 1925: if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
1926: if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 1927: if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609 banghart 1928: $filename=~m -^/*(uploaded|editupload)/-) {
1.538 albertel 1929: return &repcopy_userfile($filename);
1930: }
1.532 albertel 1931: $filename=~s/[\n\r]//g;
1.8 www 1932: my $transname="$filename.in.transfer";
1.828 www 1933: # FIXME: this should flock
1.607 raeburn 1934: if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8 www 1935: my $remoteurl=subscribe($filename);
1.64 www 1936: if ($remoteurl =~ /^con_lost by/) {
1937: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1938: return 'unavailable';
1.8 www 1939: } elsif ($remoteurl eq 'not_found') {
1.441 albertel 1940: #&logthis("Subscribe returned not_found: $filename");
1.607 raeburn 1941: return 'not_found';
1.64 www 1942: } elsif ($remoteurl =~ /^rejected by/) {
1943: &logthis("Subscribe returned $remoteurl: $filename");
1.607 raeburn 1944: return 'forbidden';
1.20 www 1945: } elsif ($remoteurl eq 'directory') {
1.607 raeburn 1946: return 'ok';
1.8 www 1947: } else {
1.290 www 1948: my $author=$filename;
1949: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
1950: my ($udom,$uname)=split(/\//,$author);
1951: my $home=homeserver($uname,$udom);
1952: unless ($home eq $perlvar{'lonHostID'}) {
1.8 www 1953: my @parts=split(/\//,$filename);
1954: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1955: if ($path ne "$perlvar{'lonDocRoot'}/res") {
1956: &logthis("Malconfiguration for replication: $filename");
1.607 raeburn 1957: return 'bad_request';
1.8 www 1958: }
1959: my $count;
1960: for ($count=5;$count<$#parts;$count++) {
1961: $path.="/$parts[$count]";
1962: if ((-e $path)!=1) {
1963: mkdir($path,0777);
1964: }
1965: }
1966: my $ua=new LWP::UserAgent;
1967: my $request=new HTTP::Request('GET',"$remoteurl");
1968: my $response=$ua->request($request,$transname);
1969: if ($response->is_error()) {
1970: unlink($transname);
1971: my $message=$response->status_line;
1.672 albertel 1972: &logthis("<font color=\"blue\">WARNING:"
1.12 www 1973: ." LWP get: $message: $filename</font>");
1.607 raeburn 1974: return 'unavailable';
1.8 www 1975: } else {
1.16 www 1976: if ($remoteurl!~/\.meta$/) {
1977: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1978: my $mresponse=$ua->request($mrequest,$filename.'.meta');
1979: if ($mresponse->is_error()) {
1980: unlink($filename.'.meta');
1981: &logthis(
1.672 albertel 1982: "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16 www 1983: }
1984: }
1.8 www 1985: rename($transname,$filename);
1.607 raeburn 1986: return 'ok';
1.8 www 1987: }
1.290 www 1988: }
1.8 www 1989: }
1.330 www 1990: }
1991:
1992: # ------------------------------------------------ Get server side include body
1993: sub ssi_body {
1.381 albertel 1994: my ($filelink,%form)=@_;
1.606 matthew 1995: if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
1996: $form{'LONCAPA_INTERNAL_no_discussion'}='true';
1997: }
1.953 www 1998: my $output='';
1999: my $response;
1.980 raeburn 2000: if ($filelink=~/^https?\:/) {
1.954 raeburn 2001: ($output,$response)=&externalssi($filelink);
1.953 www 2002: } else {
1.1004 droeschl 2003: $filelink .= $filelink=~/\?/ ? '&' : '?';
2004: $filelink .= 'inhibitmenu=yes';
1.953 www 2005: ($output,$response)=&ssi($filelink,%form);
2006: }
1.778 albertel 2007: $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451 albertel 2008: $output=~s/^.*?\<body[^\>]*\>//si;
1.930 albertel 2009: $output=~s/\<\/body\s*\>.*?$//si;
1.953 www 2010: if (wantarray) {
2011: return ($output, $response);
2012: } else {
2013: return $output;
2014: }
1.8 www 2015: }
2016:
1.15 www 2017: # --------------------------------------------------------- Server Side Include
2018:
1.782 albertel 2019: sub absolute_url {
2020: my ($host_name) = @_;
2021: my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
2022: if ($host_name eq '') {
2023: $host_name = $ENV{'SERVER_NAME'};
2024: }
2025: return $protocol.$host_name;
2026: }
2027:
1.942 foxr 2028: #
2029: # Server side include.
2030: # Parameters:
2031: # fn Possibly encrypted resource name/id.
2032: # form Hash that describes how the rendering should be done
2033: # and other things.
1.944 foxr 2034: # Returns:
1.950 raeburn 2035: # Scalar context: The content of the response.
2036: # Array context: 2 element list of the content and the full response object.
1.942 foxr 2037: #
1.15 www 2038: sub ssi {
2039:
1.944 foxr 2040: my ($fn,%form)=@_;
1.15 www 2041: my $ua=new LWP::UserAgent;
1.23 www 2042: my $request;
1.711 albertel 2043:
2044: $form{'no_update_last_known'}=1;
1.895 albertel 2045: &Apache::lonenc::check_encrypt(\$fn);
1.23 www 2046: if (%form) {
1.782 albertel 2047: $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000 raeburn 2048: $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23 www 2049: } else {
1.782 albertel 2050: $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23 www 2051: }
2052:
1.15 www 2053: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
2054: my $response=$ua->request($request);
2055:
1.944 foxr 2056: if (wantarray) {
2057: return ($response->content, $response);
2058: } else {
2059: return $response->content;
1.942 foxr 2060: }
1.324 www 2061: }
2062:
2063: sub externalssi {
2064: my ($url)=@_;
2065: my $ua=new LWP::UserAgent;
2066: my $request=new HTTP::Request('GET',$url);
2067: my $response=$ua->request($request);
1.954 raeburn 2068: if (wantarray) {
2069: return ($response->content, $response);
2070: } else {
2071: return $response->content;
2072: }
1.15 www 2073: }
1.254 www 2074:
1.492 albertel 2075: # -------------------------------- Allow a /uploaded/ URI to be vouched for
2076:
2077: sub allowuploaded {
2078: my ($srcurl,$url)=@_;
2079: $url=&clutter(&declutter($url));
2080: my $dir=$url;
2081: $dir=~s/\/[^\/]+$//;
2082: my %httpref=();
2083: my $httpurl=&hreflocation('',$url);
2084: $httpref{'httpref.'.$httpurl}=$srcurl;
1.949 raeburn 2085: &Apache::lonnet::appenv(\%httpref);
1.254 www 2086: }
1.477 raeburn 2087:
1.478 albertel 2088: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638 albertel 2089: # input: action, courseID, current domain, intended
1.637 raeburn 2090: # path to file, source of file, instruction to parse file for objects,
2091: # ref to hash for embedded objects,
2092: # ref to hash for codebase of java objects.
2093: #
1.485 raeburn 2094: # output: url to file (if action was uploaddoc),
2095: # ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477 raeburn 2096: #
1.478 albertel 2097: # Allows directory structure to be used within lonUsers/../userfiles/ for a
2098: # course.
1.477 raeburn 2099: #
1.478 albertel 2100: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2101: # will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
2102: # course's home server.
1.477 raeburn 2103: #
1.478 albertel 2104: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
2105: # be copied from $source (current location) to
2106: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2107: # and will then be copied to
2108: # /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
2109: # course's home server.
1.485 raeburn 2110: #
1.481 raeburn 2111: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620 albertel 2112: # will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481 raeburn 2113: # /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
2114: # and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
2115: # in course's home server.
1.637 raeburn 2116: #
1.477 raeburn 2117:
2118: sub process_coursefile {
1.638 albertel 2119: my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477 raeburn 2120: my $fetchresult;
1.638 albertel 2121: my $home=&homeserver($docuname,$docudom);
1.477 raeburn 2122: if ($action eq 'propagate') {
1.638 albertel 2123: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
2124: $home);
1.481 raeburn 2125: } else {
1.477 raeburn 2126: my $fpath = '';
2127: my $fname = $file;
1.478 albertel 2128: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477 raeburn 2129: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637 raeburn 2130: my $filepath = &build_filepath($fpath);
1.481 raeburn 2131: if ($action eq 'copy') {
2132: if ($source eq '') {
2133: $fetchresult = 'no source file';
2134: return $fetchresult;
2135: } else {
2136: my $destination = $filepath.'/'.$fname;
2137: rename($source,$destination);
2138: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2139: $home);
1.481 raeburn 2140: }
2141: } elsif ($action eq 'uploaddoc') {
2142: open(my $fh,'>'.$filepath.'/'.$fname);
1.620 albertel 2143: print $fh $env{'form.'.$source};
1.481 raeburn 2144: close($fh);
1.637 raeburn 2145: if ($parser eq 'parse') {
1.1024 raeburn 2146: my $mm = new File::MMagic;
2147: my $mime_type = $mm->checktype_filename($filepath.'/'.$fname);
2148: if ($mime_type eq 'text/html') {
2149: my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
2150: unless ($parse_result eq 'ok') {
2151: &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
2152: }
1.637 raeburn 2153: }
2154: }
1.477 raeburn 2155: $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2156: $home);
1.481 raeburn 2157: if ($fetchresult eq 'ok') {
2158: return '/uploaded/'.$fpath.'/'.$fname;
2159: } else {
2160: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2161: ' to host '.$home.': '.$fetchresult);
1.481 raeburn 2162: return '/adm/notfound.html';
2163: }
1.477 raeburn 2164: }
2165: }
1.485 raeburn 2166: unless ( $fetchresult eq 'ok') {
1.477 raeburn 2167: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638 albertel 2168: ' to host '.$home.': '.$fetchresult);
1.477 raeburn 2169: }
2170: return $fetchresult;
2171: }
2172:
1.637 raeburn 2173: sub build_filepath {
2174: my ($fpath) = @_;
2175: my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
2176: unless ($fpath eq '') {
2177: my @parts=split('/',$fpath);
2178: foreach my $part (@parts) {
2179: $filepath.= '/'.$part;
2180: if ((-e $filepath)!=1) {
2181: mkdir($filepath,0777);
2182: }
2183: }
2184: }
2185: return $filepath;
2186: }
2187:
2188: sub store_edited_file {
1.638 albertel 2189: my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637 raeburn 2190: my $file = $primary_url;
2191: $file =~ s#^/uploaded/$docudom/$docuname/##;
2192: my $fpath = '';
2193: my $fname = $file;
2194: ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
2195: $fpath=$docudom.'/'.$docuname.'/'.$fpath;
2196: my $filepath = &build_filepath($fpath);
2197: open(my $fh,'>'.$filepath.'/'.$fname);
2198: print $fh $content;
2199: close($fh);
1.638 albertel 2200: my $home=&homeserver($docuname,$docudom);
1.637 raeburn 2201: $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638 albertel 2202: $home);
1.637 raeburn 2203: if ($$fetchresult eq 'ok') {
2204: return '/uploaded/'.$fpath.'/'.$fname;
2205: } else {
1.638 albertel 2206: &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
2207: ' to host '.$home.': '.$$fetchresult);
1.637 raeburn 2208: return '/adm/notfound.html';
2209: }
2210: }
2211:
1.531 albertel 2212: sub clean_filename {
1.831 albertel 2213: my ($fname,$args)=@_;
1.315 www 2214: # Replace Windows backslashes by forward slashes
1.257 www 2215: $fname=~s/\\/\//g;
1.831 albertel 2216: if (!$args->{'keep_path'}) {
2217: # Get rid of everything but the actual filename
2218: $fname=~s/^.*\/([^\/]+)$/$1/;
2219: }
1.315 www 2220: # Replace spaces by underscores
2221: $fname=~s/\s+/\_/g;
2222: # Replace all other weird characters by nothing
1.831 albertel 2223: $fname=~s{[^/\w\.\-]}{}g;
1.540 albertel 2224: # Replace all .\d. sequences with _\d. so they no longer look like version
2225: # numbers
2226: $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531 albertel 2227: return $fname;
2228: }
1.1051 raeburn 2229: # This Function checks if an Image's dimensions exceed either $resizewidth (width)
2230: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an
2231: # image with the same aspect ratio as the original, but with dimensions which do
2232: # not exceed $resizewidth and $resizeheight.
2233:
1.984 neumanie 2234: sub resizeImage {
1.1051 raeburn 2235: my ($img_path,$resizewidth,$resizeheight) = @_;
2236: my $ima = Image::Magick->new;
2237: my $resized;
2238: if (-e $img_path) {
2239: $ima->Read($img_path);
2240: if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
2241: my $width = $ima->Get('width');
2242: my $height = $ima->Get('height');
2243: if ($width > $resizewidth) {
2244: my $factor = $width/$resizewidth;
2245: my $newheight = $height/$factor;
2246: $ima->Scale(width=>$resizewidth,height=>$newheight);
2247: $resized = 1;
2248: }
2249: }
2250: if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
2251: my $width = $ima->Get('width');
2252: my $height = $ima->Get('height');
2253: if ($height > $resizeheight) {
2254: my $factor = $height/$resizeheight;
2255: my $newwidth = $width/$factor;
2256: $ima->Scale(width=>$newwidth,height=>$resizeheight);
2257: $resized = 1;
2258: }
2259: }
2260: if ($resized) {
2261: $ima->Write($img_path);
2262: }
2263: }
2264: return;
1.977 amueller 2265: }
2266:
1.608 albertel 2267: # --------------- Take an uploaded file and put it into the userfiles directory
1.686 albertel 2268: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719 banghart 2269: # the desired filenam is in $env{"form.$formname.filename"}
1.686 albertel 2270: # $coursedoc - if true up to the current course
2271: # if false
2272: # $subdir - directory in userfile to store the file into
1.858 raeburn 2273: # $parser - instruction to parse file for objects ($parser = parse)
2274: # $allfiles - reference to hash for embedded objects
2275: # $codebase - reference to hash for codebase of java objects
2276: # $desuname - username for permanent storage of uploaded file
2277: # $dsetudom - domain for permanaent storage of uploaded file
1.860 raeburn 2278: # $thumbwidth - width (pixels) of thumbnail to make for uploaded image
2279: # $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051 raeburn 2280: # $resizewidth - width (pixels) to which to resize uploaded image
2281: # $resizeheight - height (pixels) to which to resize uploaded image
1.858 raeburn 2282: #
1.686 albertel 2283: # output: url of file in userspace, or error: <message>
2284: # or /adm/notfound.html if failure to upload occurse
1.608 albertel 2285:
1.531 albertel 2286: sub userfileupload {
1.860 raeburn 2287: my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1051 raeburn 2288: $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight)=@_;
1.531 albertel 2289: if (!defined($subdir)) { $subdir='unknown'; }
1.620 albertel 2290: my $fname=$env{'form.'.$formname.'.filename'};
1.531 albertel 2291: $fname=&clean_filename($fname);
1.315 www 2292: # See if there is anything left
1.257 www 2293: unless ($fname) { return 'error: no uploaded file'; }
1.620 albertel 2294: chop($env{'form.'.$formname});
1.523 raeburn 2295: if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
2296: my $now = time;
2297: my $filepath = 'tmp/helprequests/'.$now;
2298: my @parts=split(/\//,$filepath);
2299: my $fullpath = $perlvar{'lonDaemons'};
2300: for (my $i=0;$i<@parts;$i++) {
2301: $fullpath .= '/'.$parts[$i];
2302: if ((-e $fullpath)!=1) {
2303: mkdir($fullpath,0777);
2304: }
2305: }
2306: open(my $fh,'>'.$fullpath.'/'.$fname);
1.620 albertel 2307: print $fh $env{'form.'.$formname};
1.523 raeburn 2308: close($fh);
1.741 raeburn 2309: return $fullpath.'/'.$fname;
2310: } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
2311: my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
2312: '_'.$env{'user.domain'}.'/pending';
2313: my @parts=split(/\//,$filepath);
2314: my $fullpath = $perlvar{'lonDaemons'};
2315: for (my $i=0;$i<@parts;$i++) {
2316: $fullpath .= '/'.$parts[$i];
2317: if ((-e $fullpath)!=1) {
2318: mkdir($fullpath,0777);
2319: }
2320: }
2321: open(my $fh,'>'.$fullpath.'/'.$fname);
2322: print $fh $env{'form.'.$formname};
2323: close($fh);
2324: return $fullpath.'/'.$fname;
1.523 raeburn 2325: }
1.995 raeburn 2326: if ($subdir eq 'scantron') {
2327: $fname = 'scantron_orig_'.$fname;
2328: } else {
1.258 www 2329: # Create the directory if not present
1.995 raeburn 2330: $fname="$subdir/$fname";
2331: }
1.259 www 2332: if ($coursedoc) {
1.638 albertel 2333: my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2334: my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646 raeburn 2335: if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638 albertel 2336: return &finishuserfileupload($docuname,$docudom,
2337: $formname,$fname,$parser,$allfiles,
1.1051 raeburn 2338: $codebase,$thumbwidth,$thumbheight,
2339: $resizewidth,$resizeheight);
1.481 raeburn 2340: } else {
1.620 albertel 2341: $fname=$env{'form.folder'}.'/'.$fname;
1.638 albertel 2342: return &process_coursefile('uploaddoc',$docuname,$docudom,
2343: $fname,$formname,$parser,
2344: $allfiles,$codebase);
1.481 raeburn 2345: }
1.719 banghart 2346: } elsif (defined($destuname)) {
2347: my $docuname=$destuname;
2348: my $docudom=$destudom;
1.860 raeburn 2349: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2350: $parser,$allfiles,$codebase,
1.1051 raeburn 2351: $thumbwidth,$thumbheight,
2352: $resizewidth,$resizeheight);
1.719 banghart 2353:
1.259 www 2354: } else {
1.638 albertel 2355: my $docuname=$env{'user.name'};
2356: my $docudom=$env{'user.domain'};
1.714 raeburn 2357: if (exists($env{'form.group'})) {
2358: $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
2359: $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
2360: }
1.860 raeburn 2361: return &finishuserfileupload($docuname,$docudom,$formname,$fname,
2362: $parser,$allfiles,$codebase,
1.1051 raeburn 2363: $thumbwidth,$thumbheight,
2364: $resizewidth,$resizeheight);
1.259 www 2365: }
1.271 www 2366: }
2367:
2368: sub finishuserfileupload {
1.860 raeburn 2369: my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1051 raeburn 2370: $thumbwidth,$thumbheight,$resizewidth,$resizeheight) = @_;
1.477 raeburn 2371: my $path=$docudom.'/'.$docuname.'/';
1.258 www 2372: my $filepath=$perlvar{'lonDocRoot'};
1.984 neumanie 2373:
1.860 raeburn 2374: my ($fnamepath,$file,$fetchthumb);
1.494 albertel 2375: $file=$fname;
2376: if ($fname=~m|/|) {
2377: ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
2378: $path.=$fnamepath.'/';
2379: }
1.259 www 2380: my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258 www 2381: my $count;
2382: for ($count=4;$count<=$#parts;$count++) {
2383: $filepath.="/$parts[$count]";
2384: if ((-e $filepath)!=1) {
2385: mkdir($filepath,0777);
2386: }
2387: }
1.984 neumanie 2388:
1.258 www 2389: # Save the file
2390: {
1.701 albertel 2391: if (!open(FH,'>'.$filepath.'/'.$file)) {
2392: &logthis('Failed to create '.$filepath.'/'.$file);
2393: print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
2394: return '/adm/notfound.html';
2395: }
2396: if (!print FH ($env{'form.'.$formname})) {
2397: &logthis('Failed to write to '.$filepath.'/'.$file);
2398: print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
2399: return '/adm/notfound.html';
2400: }
1.570 albertel 2401: close(FH);
1.1051 raeburn 2402: if ($resizewidth && $resizeheight) {
2403: my $mm = new File::MMagic;
2404: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
2405: if ($mime_type =~ m{^image/}) {
2406: &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
2407: }
1.977 amueller 2408: }
1.258 www 2409: }
1.637 raeburn 2410: if ($parser eq 'parse') {
1.1024 raeburn 2411: my $mm = new File::MMagic;
2412: my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
2413: if ($mime_type eq 'text/html') {
2414: my $parse_result = &extract_embedded_items($filepath.'/'.$file,
2415: $allfiles,$codebase);
2416: unless ($parse_result eq 'ok') {
2417: &logthis('Failed to parse '.$filepath.$file.
2418: ' for embedded media: '.$parse_result);
2419: }
1.637 raeburn 2420: }
2421: }
1.860 raeburn 2422: if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
2423: my $input = $filepath.'/'.$file;
2424: my $output = $filepath.'/'.'tn-'.$file;
2425: my $thumbsize = $thumbwidth.'x'.$thumbheight;
2426: system("convert -sample $thumbsize $input $output");
2427: if (-e $filepath.'/'.'tn-'.$file) {
2428: $fetchthumb = 1;
2429: }
2430: }
1.858 raeburn 2431:
1.259 www 2432: # Notify homeserver to grep it
2433: #
1.984 neumanie 2434: my $docuhome=&homeserver($docuname,$docudom);
1.494 albertel 2435: my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295 www 2436: if ($fetchresult eq 'ok') {
1.860 raeburn 2437: if ($fetchthumb) {
2438: my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
2439: if ($thumbresult ne 'ok') {
2440: &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
2441: $docuhome.': '.$thumbresult);
2442: }
2443: }
1.259 www 2444: #
1.258 www 2445: # Return the URL to it
1.494 albertel 2446: return '/uploaded/'.$path.$file;
1.263 www 2447: } else {
1.494 albertel 2448: &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
2449: ': '.$fetchresult);
1.263 www 2450: return '/adm/notfound.html';
1.858 raeburn 2451: }
1.493 albertel 2452: }
2453:
1.637 raeburn 2454: sub extract_embedded_items {
1.961 raeburn 2455: my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637 raeburn 2456: my @state = ();
2457: my %javafiles = (
2458: codebase => '',
2459: code => '',
2460: archive => ''
2461: );
2462: my %mediafiles = (
2463: src => '',
2464: movie => '',
2465: );
1.648 raeburn 2466: my $p;
2467: if ($content) {
2468: $p = HTML::LCParser->new($content);
2469: } else {
1.961 raeburn 2470: $p = HTML::LCParser->new($fullpath);
1.648 raeburn 2471: }
1.641 albertel 2472: while (my $t=$p->get_token()) {
1.640 albertel 2473: if ($t->[0] eq 'S') {
2474: my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886 albertel 2475: push(@state, $tagname);
1.648 raeburn 2476: if (lc($tagname) eq 'allow') {
2477: &add_filetype($allfiles,$attr->{'src'},'src');
2478: }
1.640 albertel 2479: if (lc($tagname) eq 'img') {
2480: &add_filetype($allfiles,$attr->{'src'},'src');
2481: }
1.886 albertel 2482: if (lc($tagname) eq 'a') {
2483: &add_filetype($allfiles,$attr->{'href'},'href');
2484: }
1.645 raeburn 2485: if (lc($tagname) eq 'script') {
2486: if ($attr->{'archive'} =~ /\.jar$/i) {
2487: &add_filetype($allfiles,$attr->{'archive'},'archive');
2488: } else {
2489: &add_filetype($allfiles,$attr->{'src'},'src');
2490: }
2491: }
2492: if (lc($tagname) eq 'link') {
2493: if (lc($attr->{'rel'}) eq 'stylesheet') {
2494: &add_filetype($allfiles,$attr->{'href'},'href');
2495: }
2496: }
1.640 albertel 2497: if (lc($tagname) eq 'object' ||
2498: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
2499: foreach my $item (keys(%javafiles)) {
2500: $javafiles{$item} = '';
2501: }
2502: }
2503: if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
2504: my $name = lc($attr->{'name'});
2505: foreach my $item (keys(%javafiles)) {
2506: if ($name eq $item) {
2507: $javafiles{$item} = $attr->{'value'};
2508: last;
2509: }
2510: }
2511: foreach my $item (keys(%mediafiles)) {
2512: if ($name eq $item) {
2513: &add_filetype($allfiles, $attr->{'value'}, 'value');
2514: last;
2515: }
2516: }
2517: }
2518: if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
2519: foreach my $item (keys(%javafiles)) {
2520: if ($attr->{$item}) {
2521: $javafiles{$item} = $attr->{$item};
2522: last;
2523: }
2524: }
2525: foreach my $item (keys(%mediafiles)) {
2526: if ($attr->{$item}) {
2527: &add_filetype($allfiles,$attr->{$item},$item);
2528: last;
2529: }
2530: }
2531: }
2532: } elsif ($t->[0] eq 'E') {
2533: my ($tagname) = ($t->[1]);
2534: if ($javafiles{'codebase'} ne '') {
2535: $javafiles{'codebase'} .= '/';
2536: }
2537: if (lc($tagname) eq 'applet' ||
2538: lc($tagname) eq 'object' ||
2539: (lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
2540: ) {
2541: foreach my $item (keys(%javafiles)) {
2542: if ($item ne 'codebase' && $javafiles{$item} ne '') {
2543: my $file=$javafiles{'codebase'}.$javafiles{$item};
2544: &add_filetype($allfiles,$file,$item);
2545: }
2546: }
2547: }
2548: pop @state;
2549: }
2550: }
1.637 raeburn 2551: return 'ok';
2552: }
2553:
1.639 albertel 2554: sub add_filetype {
2555: my ($allfiles,$file,$type)=@_;
2556: if (exists($allfiles->{$file})) {
2557: unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
2558: push(@{$allfiles->{$file}}, &escape($type));
2559: }
2560: } else {
2561: @{$allfiles->{$file}} = (&escape($type));
1.637 raeburn 2562: }
2563: }
2564:
1.493 albertel 2565: sub removeuploadedurl {
1.984 neumanie 2566: my ($url)=@_;
2567: my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613 albertel 2568: return &removeuserfile($uname,$udom,$fname);
1.490 albertel 2569: }
2570:
2571: sub removeuserfile {
2572: my ($docuname,$docudom,$fname)=@_;
1.984 neumanie 2573: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2574: my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984 neumanie 2575: if ($result eq 'ok') {
1.798 raeburn 2576: if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
2577: my $metafile = $fname.'.meta';
2578: my $metaresult = &removeuserfile($docuname,$docudom,$metafile);
1.823 albertel 2579: my $url = "/uploaded/$docudom/$docuname/$fname";
1.984 neumanie 2580: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2581: my $sqlresult =
1.823 albertel 2582: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2583: 'portfolio_metadata',$group,
2584: 'delete');
1.798 raeburn 2585: }
2586: }
2587: return $result;
1.257 www 2588: }
1.15 www 2589:
1.530 albertel 2590: sub mkdiruserfile {
2591: my ($docuname,$docudom,$dir)=@_;
2592: my $home=&homeserver($docuname,$docudom);
2593: return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
2594: }
2595:
1.531 albertel 2596: sub renameuserfile {
2597: my ($docuname,$docudom,$old,$new)=@_;
2598: my $home=&homeserver($docuname,$docudom);
1.798 raeburn 2599: my $result = &reply("renameuserfile:$docudom:$docuname:".
2600: &escape("$old").':'.&escape("$new"),$home);
2601: if ($result eq 'ok') {
2602: if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
2603: my $oldmeta = $old.'.meta';
2604: my $newmeta = $new.'.meta';
2605: my $metaresult =
2606: &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823 albertel 2607: my $url = "/uploaded/$docudom/$docuname/$old";
2608: my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821 raeburn 2609: my $sqlresult =
1.823 albertel 2610: &update_portfolio_table($docuname,$docudom,$file,
1.821 raeburn 2611: 'portfolio_metadata',$group,
2612: 'delete');
1.798 raeburn 2613: }
2614: }
2615: return $result;
1.531 albertel 2616: }
2617:
1.14 www 2618: # ------------------------------------------------------------------------- Log
2619:
2620: sub log {
2621: my ($dom,$nam,$hom,$what)=@_;
1.47 www 2622: return critical("log:$dom:$nam:$what",$hom);
1.157 www 2623: }
2624:
2625: # ------------------------------------------------------------------ Course Log
1.352 www 2626: #
2627: # This routine flushes several buffers of non-mission-critical nature
2628: #
1.157 www 2629:
2630: sub flushcourselogs {
1.352 www 2631: &logthis('Flushing log buffers');
2632: #
2633: # course logs
2634: # This is a log of all transactions in a course, which can be used
2635: # for data mining purposes
2636: #
2637: # It also collects the courseid database, which lists last transaction
2638: # times and course titles for all courseids
2639: #
2640: my %courseidbuffer=();
1.921 raeburn 2641: foreach my $crsid (keys(%courselogs)) {
1.352 www 2642: if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188 www 2643: &escape($courselogs{$crsid}),
2644: $coursehombuf{$crsid}) eq 'ok') {
1.157 www 2645: delete $courselogs{$crsid};
2646: } else {
2647: &logthis('Failed to flush log buffer for '.$crsid);
2648: if (length($courselogs{$crsid})>40000) {
1.672 albertel 2649: &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157 www 2650: " exceeded maximum size, deleting.</font>");
2651: delete $courselogs{$crsid};
2652: }
1.352 www 2653: }
1.920 raeburn 2654: $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936 raeburn 2655: 'description' => $coursedescrbuf{$crsid},
2656: 'inst_code' => $courseinstcodebuf{$crsid},
2657: 'type' => $coursetypebuf{$crsid},
2658: 'owner' => $courseownerbuf{$crsid},
1.920 raeburn 2659: };
1.191 harris41 2660: }
1.352 www 2661: #
2662: # Write course id database (reverse lookup) to homeserver of courses
2663: # Is used in pickcourse
2664: #
1.840 albertel 2665: foreach my $crs_home (keys(%courseidbuffer)) {
1.918 raeburn 2666: my $response = &courseidput(&host_domain($crs_home),
1.921 raeburn 2667: $courseidbuffer{$crs_home},
2668: $crs_home,'timeonly');
1.352 www 2669: }
2670: #
2671: # File accesses
2672: # Writes to the dynamic metadata of resources to get hit counts, etc.
2673: #
1.449 matthew 2674: foreach my $entry (keys(%accesshash)) {
1.458 matthew 2675: if ($entry =~ /___count$/) {
2676: my ($dom,$name);
1.807 albertel 2677: ($dom,$name,undef)=
1.811 albertel 2678: ($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458 matthew 2679: if (! defined($dom) || $dom eq '' ||
2680: ! defined($name) || $name eq '') {
1.620 albertel 2681: my $cid = $env{'request.course.id'};
2682: $dom = $env{'request.'.$cid.'.domain'};
2683: $name = $env{'request.'.$cid.'.num'};
1.458 matthew 2684: }
1.450 matthew 2685: my $value = $accesshash{$entry};
2686: my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
2687: my %temphash=($url => $value);
1.449 matthew 2688: my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
2689: if ($result eq 'ok') {
2690: delete $accesshash{$entry};
2691: } elsif ($result eq 'unknown_cmd') {
2692: # Target server has old code running on it.
1.450 matthew 2693: my %temphash=($entry => $value);
1.449 matthew 2694: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2695: delete $accesshash{$entry};
2696: }
2697: }
2698: } else {
1.811 albertel 2699: my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450 matthew 2700: my %temphash=($entry => $accesshash{$entry});
1.449 matthew 2701: if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
2702: delete $accesshash{$entry};
2703: }
1.185 www 2704: }
1.191 harris41 2705: }
1.352 www 2706: #
2707: # Roles
2708: # Reverse lookup of user roles for course faculty/staff and co-authorship
2709: #
1.800 albertel 2710: foreach my $entry (keys(%userrolehash)) {
1.351 www 2711: my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349 www 2712: split(/\:/,$entry);
2713: if (&Apache::lonnet::put('nohist_userroles',
1.351 www 2714: { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349 www 2715: $rudom,$runame) eq 'ok') {
2716: delete $userrolehash{$entry};
2717: }
2718: }
1.662 raeburn 2719: #
2720: # Reverse lookup of domain roles (dc, ad, li, sc, au)
2721: #
2722: my %domrolebuffer = ();
1.1000 raeburn 2723: foreach my $entry (keys(%domainrolehash)) {
1.901 albertel 2724: my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662 raeburn 2725: if ($domrolebuffer{$rudom}) {
2726: $domrolebuffer{$rudom}.='&'.&escape($entry).
2727: '='.&escape($domainrolehash{$entry});
2728: } else {
2729: $domrolebuffer{$rudom}.=&escape($entry).
2730: '='.&escape($domainrolehash{$entry});
2731: }
2732: delete $domainrolehash{$entry};
2733: }
2734: foreach my $dom (keys(%domrolebuffer)) {
1.841 albertel 2735: my %servers = &get_servers($dom,'library');
2736: foreach my $tryserver (keys(%servers)) {
2737: unless (&reply('domroleput:'.$dom.':'.
2738: $domrolebuffer{$dom},$tryserver) eq 'ok') {
2739: &logthis('Put of domain roles failed for '.$dom.' and '.$tryserver);
2740: }
1.662 raeburn 2741: }
2742: }
1.186 www 2743: $dumpcount++;
1.157 www 2744: }
2745:
2746: sub courselog {
2747: my $what=shift;
1.158 www 2748: $what=time.':'.$what;
1.620 albertel 2749: unless ($env{'request.course.id'}) { return ''; }
2750: $coursedombuf{$env{'request.course.id'}}=
2751: $env{'course.'.$env{'request.course.id'}.'.domain'};
2752: $coursenumbuf{$env{'request.course.id'}}=
2753: $env{'course.'.$env{'request.course.id'}.'.num'};
2754: $coursehombuf{$env{'request.course.id'}}=
2755: $env{'course.'.$env{'request.course.id'}.'.home'};
2756: $coursedescrbuf{$env{'request.course.id'}}=
2757: $env{'course.'.$env{'request.course.id'}.'.description'};
2758: $courseinstcodebuf{$env{'request.course.id'}}=
2759: $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
2760: $courseownerbuf{$env{'request.course.id'}}=
2761: $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741 raeburn 2762: $coursetypebuf{$env{'request.course.id'}}=
2763: $env{'course.'.$env{'request.course.id'}.'.type'};
1.620 albertel 2764: if (defined $courselogs{$env{'request.course.id'}}) {
2765: $courselogs{$env{'request.course.id'}}.='&'.$what;
1.157 www 2766: } else {
1.620 albertel 2767: $courselogs{$env{'request.course.id'}}.=$what;
1.157 www 2768: }
1.620 albertel 2769: if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157 www 2770: &flushcourselogs();
2771: }
1.158 www 2772: }
2773:
2774: sub courseacclog {
2775: my $fnsymb=shift;
1.620 albertel 2776: unless ($env{'request.course.id'}) { return ''; }
2777: my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657 albertel 2778: if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187 www 2779: $what.=':POST';
1.583 matthew 2780: # FIXME: Probably ought to escape things....
1.800 albertel 2781: foreach my $key (keys(%env)) {
2782: if ($key=~/^form\.(.*)/) {
1.975 raeburn 2783: my $formitem = $1;
2784: if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
2785: $what.=':'.$formitem.'='.$env{$key};
2786: } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
2787: $what.=':'.$formitem.'='.$env{$key};
2788: }
1.158 www 2789: }
1.191 harris41 2790: }
1.583 matthew 2791: } elsif ($fnsymb =~ m:^/adm/searchcat:) {
2792: # FIXME: We should not be depending on a form parameter that someone
2793: # editing lonsearchcat.pm might change in the future.
1.620 albertel 2794: if ($env{'form.phase'} eq 'course_search') {
1.583 matthew 2795: $what.= ':POST';
2796: # FIXME: Probably ought to escape things....
2797: foreach my $element ('courseexp','crsfulltext','crsrelated',
2798: 'crsdiscuss') {
1.620 albertel 2799: $what.=':'.$element.'='.$env{'form.'.$element};
1.583 matthew 2800: }
2801: }
1.158 www 2802: }
2803: &courselog($what);
1.149 www 2804: }
2805:
1.185 www 2806: sub countacc {
2807: my $url=&declutter(shift);
1.458 matthew 2808: return if (! defined($url) || $url eq '');
1.620 albertel 2809: unless ($env{'request.course.id'}) { return ''; }
2810: $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281 www 2811: my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450 matthew 2812: $accesshash{$key}++;
1.185 www 2813: }
1.349 www 2814:
1.361 www 2815: sub linklog {
2816: my ($from,$to)=@_;
2817: $from=&declutter($from);
2818: $to=&declutter($to);
2819: $accesshash{$from.'___'.$to.'___comefrom'}=1;
2820: $accesshash{$to.'___'.$from.'___goto'}=1;
2821: }
2822:
1.349 www 2823: sub userrolelog {
2824: my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661 raeburn 2825: if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662 raeburn 2826: ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661 raeburn 2827: ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041 raeburn 2828: ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350 www 2829: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2830: $userrolehash
2831: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349 www 2832: =$tend.':'.$tstart;
1.662 raeburn 2833: }
1.898 albertel 2834: if (($env{'request.role'} =~ /dc\./) &&
2835: (($trole=~/^au/) || ($trole=~/^in/) ||
2836: ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041 raeburn 2837: ($trole=~/^cr/) || ($trole=~/^ta/) ||
2838: ($trole=~/^co/))) {
1.898 albertel 2839: $userrolehash
2840: {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
2841: =$tend.':'.$tstart;
2842: }
1.662 raeburn 2843: if (($trole=~/^dc/) || ($trole=~/^ad/) ||
2844: ($trole=~/^li/) || ($trole=~/^li/) ||
2845: ($trole=~/^au/) || ($trole=~/^dg/) ||
2846: ($trole=~/^sc/)) {
2847: my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
2848: $domainrolehash
2849: {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
2850: = $tend.':'.$tstart;
2851: }
1.351 www 2852: }
2853:
1.957 raeburn 2854: sub courserolelog {
2855: my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
2856: if (($trole eq 'cc') || ($trole eq 'in') ||
2857: ($trole eq 'ep') || ($trole eq 'ad') ||
2858: ($trole eq 'ta') || ($trole eq 'st') ||
1.1044 raeburn 2859: ($trole=~/^cr/) || ($trole eq 'gr') ||
2860: ($trole eq 'co')) {
1.957 raeburn 2861: if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
2862: my $cdom = $1;
2863: my $cnum = $2;
2864: my $sec = $3;
2865: my $namespace = 'rolelog';
2866: my %storehash = (
2867: role => $trole,
2868: start => $tstart,
2869: end => $tend,
2870: selfenroll => $selfenroll,
2871: context => $context,
2872: );
2873: if ($trole eq 'gr') {
2874: $namespace = 'groupslog';
2875: $storehash{'group'} = $sec;
2876: } else {
2877: $storehash{'section'} = $sec;
2878: }
2879: &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996 raeburn 2880: if (($trole ne 'st') || ($sec ne '')) {
2881: &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
2882: }
1.957 raeburn 2883: }
2884: }
2885: return;
2886: }
2887:
1.351 www 2888: sub get_course_adv_roles {
1.948 raeburn 2889: my ($cid,$codes) = @_;
1.620 albertel 2890: $cid=$env{'request.course.id'} unless (defined($cid));
1.351 www 2891: my %coursehash=&coursedescription($cid);
1.988 raeburn 2892: my $crstype = &Apache::loncommon::course_type($cid);
1.470 www 2893: my %nothide=();
1.800 albertel 2894: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937 raeburn 2895: if ($user !~ /:/) {
2896: $nothide{join(':',split(/[\@]/,$user))}=1;
2897: } else {
2898: $nothide{$user}=1;
2899: }
1.470 www 2900: }
1.351 www 2901: my %returnhash=();
2902: my %dumphash=
2903: &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
2904: my $now=time;
1.997 raeburn 2905: my %privileged;
1.1000 raeburn 2906: foreach my $entry (keys(%dumphash)) {
1.800 albertel 2907: my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351 www 2908: if (($tstart) && ($tstart<0)) { next; }
2909: if (($tend) && ($tend<$now)) { next; }
2910: if (($tstart) && ($now<$tstart)) { next; }
1.800 albertel 2911: my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576 albertel 2912: if ($username eq '' || $domain eq '') { next; }
1.997 raeburn 2913: unless (ref($privileged{$domain}) eq 'HASH') {
2914: my %dompersonnel =
1.998 raeburn 2915: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997 raeburn 2916: $privileged{$domain} = {};
2917: foreach my $server (keys(%dompersonnel)) {
1.999 raeburn 2918: if (ref($dompersonnel{$server}) eq 'HASH') {
1.997 raeburn 2919: foreach my $user (keys(%{$dompersonnel{$server}})) {
2920: my ($trole,$uname,$udom) = split(/:/,$user);
2921: $privileged{$udom}{$uname} = 1;
2922: }
2923: }
2924: }
2925: }
2926: if ((exists($privileged{$domain}{$username})) &&
2927: (!$nothide{$username.':'.$domain})) { next; }
1.656 albertel 2928: if ($role eq 'cr') { next; }
1.948 raeburn 2929: if ($codes) {
2930: if ($section) { $role .= ':'.$section; }
2931: if ($returnhash{$role}) {
2932: $returnhash{$role}.=','.$username.':'.$domain;
2933: } else {
2934: $returnhash{$role}=$username.':'.$domain;
2935: }
1.351 www 2936: } else {
1.988 raeburn 2937: my $key=&plaintext($role,$crstype);
1.973 bisitz 2938: if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948 raeburn 2939: if ($returnhash{$key}) {
2940: $returnhash{$key}.=','.$username.':'.$domain;
2941: } else {
2942: $returnhash{$key}=$username.':'.$domain;
2943: }
1.351 www 2944: }
1.948 raeburn 2945: }
1.400 www 2946: return %returnhash;
2947: }
2948:
2949: sub get_my_roles {
1.937 raeburn 2950: my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620 albertel 2951: unless (defined($uname)) { $uname=$env{'user.name'}; }
2952: unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937 raeburn 2953: my (%dumphash,%nothide);
1.858 raeburn 2954: if ($context eq 'userroles') {
2955: %dumphash = &dump('roles',$udom,$uname);
2956: } else {
2957: %dumphash=
1.400 www 2958: &dump('nohist_userroles',$udom,$uname);
1.937 raeburn 2959: if ($hidepriv) {
2960: my %coursehash=&coursedescription($udom.'_'.$uname);
2961: foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
2962: if ($user !~ /:/) {
2963: $nothide{join(':',split(/[\@]/,$user))} = 1;
2964: } else {
2965: $nothide{$user} = 1;
2966: }
2967: }
2968: }
1.858 raeburn 2969: }
1.400 www 2970: my %returnhash=();
2971: my $now=time;
1.999 raeburn 2972: my %privileged;
1.800 albertel 2973: foreach my $entry (keys(%dumphash)) {
1.867 raeburn 2974: my ($role,$tend,$tstart);
2975: if ($context eq 'userroles') {
2976: ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
2977: } else {
2978: ($tend,$tstart)=split(/\:/,$dumphash{$entry});
2979: }
1.400 www 2980: if (($tstart) && ($tstart<0)) { next; }
1.832 raeburn 2981: my $status = 'active';
1.939 raeburn 2982: if (($tend) && ($tend<=$now)) {
1.832 raeburn 2983: $status = 'previous';
2984: }
2985: if (($tstart) && ($now<$tstart)) {
2986: $status = 'future';
2987: }
2988: if (ref($types) eq 'ARRAY') {
2989: if (!grep(/^\Q$status\E$/,@{$types})) {
2990: next;
2991: }
2992: } else {
2993: if ($status ne 'active') {
2994: next;
2995: }
2996: }
1.867 raeburn 2997: my ($rolecode,$username,$domain,$section,$area);
2998: if ($context eq 'userroles') {
2999: ($area,$rolecode) = split(/_/,$entry);
3000: (undef,$domain,$username,$section) = split(/\//,$area);
3001: } else {
3002: ($role,$username,$domain,$section) = split(/\:/,$entry);
3003: }
1.832 raeburn 3004: if (ref($roledoms) eq 'ARRAY') {
3005: if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
3006: next;
3007: }
3008: }
3009: if (ref($roles) eq 'ARRAY') {
3010: if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922 raeburn 3011: if ($role =~ /^cr\//) {
3012: if (!grep(/^cr$/,@{$roles})) {
3013: next;
3014: }
3015: } else {
3016: next;
3017: }
1.832 raeburn 3018: }
1.867 raeburn 3019: }
1.937 raeburn 3020: if ($hidepriv) {
1.999 raeburn 3021: if ($context eq 'userroles') {
3022: if ((&privileged($username,$domain)) &&
3023: (!$nothide{$username.':'.$domain})) {
3024: next;
3025: }
3026: } else {
3027: unless (ref($privileged{$domain}) eq 'HASH') {
3028: my %dompersonnel =
3029: &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
3030: $privileged{$domain} = {};
3031: if (keys(%dompersonnel)) {
3032: foreach my $server (keys(%dompersonnel)) {
3033: if (ref($dompersonnel{$server}) eq 'HASH') {
3034: foreach my $user (keys(%{$dompersonnel{$server}})) {
3035: my ($trole,$uname,$udom) = split(/:/,$user);
3036: $privileged{$udom}{$uname} = $trole;
3037: }
3038: }
3039: }
3040: }
3041: }
3042: if (exists($privileged{$domain}{$username})) {
3043: if (!$nothide{$username.':'.$domain}) {
3044: next;
3045: }
3046: }
1.937 raeburn 3047: }
3048: }
1.933 raeburn 3049: if ($withsec) {
3050: $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
3051: $tstart.':'.$tend;
3052: } else {
3053: $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
3054: }
1.832 raeburn 3055: }
1.373 www 3056: return %returnhash;
1.399 www 3057: }
3058:
3059: # ----------------------------------------------------- Frontpage Announcements
3060: #
3061: #
3062:
3063: sub postannounce {
3064: my ($server,$text)=@_;
1.844 albertel 3065: unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399 www 3066: unless ($text=~/\w/) { $text=''; }
3067: return &reply('setannounce:'.&escape($text),$server);
3068: }
3069:
3070: sub getannounce {
1.448 albertel 3071:
3072: if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399 www 3073: my $announcement='';
1.800 albertel 3074: while (my $line = <$fh>) { $announcement .= $line; }
1.448 albertel 3075: close($fh);
1.399 www 3076: if ($announcement=~/\w/) {
3077: return
3078: '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518 albertel 3079: '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>';
1.399 www 3080: } else {
3081: return '';
3082: }
3083: } else {
3084: return '';
3085: }
1.351 www 3086: }
1.353 www 3087:
3088: # ---------------------------------------------------------- Course ID routines
3089: # Deal with domain's nohist_courseid.db files
3090: #
3091:
3092: sub courseidput {
1.921 raeburn 3093: my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054 raeburn 3094: return unless (ref($storehash) eq 'HASH');
1.921 raeburn 3095: my $outcome;
3096: if ($caller eq 'timeonly') {
3097: my $cids = '';
3098: foreach my $item (keys(%$storehash)) {
3099: $cids.=&escape($item).'&';
3100: }
3101: $cids=~s/\&$//;
3102: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
3103: $coursehome);
3104: } else {
3105: my $items = '';
3106: foreach my $item (keys(%$storehash)) {
3107: $items.= &escape($item).'='.
3108: &freeze_escape($$storehash{$item}).'&';
3109: }
3110: $items=~s/\&$//;
3111: $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
3112: $coursehome);
1.918 raeburn 3113: }
3114: if ($outcome eq 'unknown_cmd') {
3115: my $what;
3116: foreach my $cid (keys(%$storehash)) {
3117: $what .= &escape($cid).'=';
1.921 raeburn 3118: foreach my $item ('description','inst_code','owner','type') {
1.936 raeburn 3119: $what .= &escape($storehash->{$cid}{$item}).':';
1.918 raeburn 3120: }
3121: $what =~ s/\:$/&/;
3122: }
3123: $what =~ s/\&$//;
3124: return &reply('courseidput:'.$domain.':'.$what,$coursehome);
3125: } else {
3126: return $outcome;
3127: }
1.353 www 3128: }
3129:
3130: sub courseiddump {
1.921 raeburn 3131: my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947 raeburn 3132: $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029 raeburn 3133: $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1071 raeburn 3134: $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918 raeburn 3135: my $as_hash = 1;
3136: my %returnhash;
3137: if (!$domfilter) { $domfilter=''; }
1.845 albertel 3138: my %libserv = &all_library();
3139: foreach my $tryserver (keys(%libserv)) {
3140: if ( ( $hostidflag == 1
3141: && grep(/^\Q$tryserver\E$/,@{$hostidref}) )
3142: || (!defined($hostidflag)) ) {
3143:
1.918 raeburn 3144: if (($domfilter eq '') ||
3145: (&host_domain($tryserver) eq $domfilter)) {
3146: my $rep =
3147: &reply('courseiddump:'.&host_domain($tryserver).':'.
3148: $sincefilter.':'.&escape($descfilter).':'.
3149: &escape($instcodefilter).':'.&escape($ownerfilter).
3150: ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947 raeburn 3151: ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962 raeburn 3152: &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008 raeburn 3153: $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029 raeburn 3154: &escape($cc_clone).':'.$cloneonly.':'.
3155: &escape($createdbefore).':'.&escape($createdafter).':'.
1.1071 raeburn 3156: &escape($creationcontext).':'.$domcloner,
3157: $tryserver);
1.918 raeburn 3158: my @pairs=split(/\&/,$rep);
3159: foreach my $item (@pairs) {
3160: my ($key,$value)=split(/\=/,$item,2);
3161: $key = &unescape($key);
3162: next if ($key =~ /^error: 2 /);
3163: my $result = &thaw_unescape($value);
3164: if (ref($result) eq 'HASH') {
3165: $returnhash{$key}=$result;
3166: } else {
1.921 raeburn 3167: my @responses = split(/:/,$value);
3168: my @items = ('description','inst_code','owner','type');
1.918 raeburn 3169: for (my $i=0; $i<@responses; $i++) {
1.921 raeburn 3170: $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918 raeburn 3171: }
1.1008 raeburn 3172: }
1.353 www 3173: }
3174: }
3175: }
3176: }
3177: return %returnhash;
3178: }
3179:
1.1055 raeburn 3180: sub courselastaccess {
3181: my ($cdom,$cnum,$hostidref) = @_;
3182: my %returnhash;
3183: if ($cdom && $cnum) {
3184: my $chome = &homeserver($cnum,$cdom);
3185: if ($chome ne 'no_host') {
3186: my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
3187: &extract_lastaccess(\%returnhash,$rep);
3188: }
3189: } else {
3190: if (!$cdom) { $cdom=''; }
3191: my %libserv = &all_library();
3192: foreach my $tryserver (keys(%libserv)) {
3193: if (ref($hostidref) eq 'ARRAY') {
3194: next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
3195: }
3196: if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
3197: my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
3198: &extract_lastaccess(\%returnhash,$rep);
3199: }
3200: }
3201: }
3202: return %returnhash;
3203: }
3204:
3205: sub extract_lastaccess {
3206: my ($returnhash,$rep) = @_;
3207: if (ref($returnhash) eq 'HASH') {
3208: unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
3209: $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
3210: $rep eq '') {
3211: my @pairs=split(/\&/,$rep);
3212: foreach my $item (@pairs) {
3213: my ($key,$value)=split(/\=/,$item,2);
3214: $key = &unescape($key);
3215: next if ($key =~ /^error: 2 /);
3216: $returnhash->{$key} = &thaw_unescape($value);
3217: }
3218: }
3219: }
3220: return;
3221: }
3222:
1.658 raeburn 3223: # ---------------------------------------------------------- DC e-mail
1.662 raeburn 3224:
3225: sub dcmailput {
1.685 raeburn 3226: my ($domain,$msgid,$message,$server)=@_;
1.662 raeburn 3227: my $status = &Apache::lonnet::critical(
1.740 www 3228: 'dcmailput:'.$domain.':'.&escape($msgid).'='.
3229: &escape($message),$server);
1.662 raeburn 3230: return $status;
3231: }
3232:
1.658 raeburn 3233: sub dcmaildump {
3234: my ($dom,$startdate,$enddate,$senders) = @_;
1.685 raeburn 3235: my %returnhash=();
1.846 albertel 3236:
3237: if (defined(&domain($dom,'primary'))) {
1.685 raeburn 3238: my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
3239: &escape($enddate).':';
3240: my @esc_senders=map { &escape($_)} @$senders;
3241: $cmd.=&escape(join('&',@esc_senders));
1.846 albertel 3242: foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800 albertel 3243: my ($key,$value) = split(/\=/,$line,2);
1.685 raeburn 3244: if (($key) && ($value)) {
3245: $returnhash{&unescape($key)} = &unescape($value);
1.658 raeburn 3246: }
3247: }
3248: }
3249: return %returnhash;
3250: }
1.662 raeburn 3251: # ---------------------------------------------------------- Domain roles
3252:
3253: sub get_domain_roles {
3254: my ($dom,$roles,$startdate,$enddate)=@_;
1.1018 raeburn 3255: if ((!defined($startdate)) || ($startdate eq '')) {
1.662 raeburn 3256: $startdate = '.';
3257: }
1.1018 raeburn 3258: if ((!defined($enddate)) || ($enddate eq '')) {
1.662 raeburn 3259: $enddate = '.';
3260: }
1.922 raeburn 3261: my $rolelist;
3262: if (ref($roles) eq 'ARRAY') {
3263: $rolelist = join(':',@{$roles});
3264: }
1.662 raeburn 3265: my %personnel = ();
1.841 albertel 3266:
3267: my %servers = &get_servers($dom,'library');
3268: foreach my $tryserver (keys(%servers)) {
3269: %{$personnel{$tryserver}}=();
3270: foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
3271: &escape($startdate).':'.
3272: &escape($enddate).':'.
3273: &escape($rolelist), $tryserver))) {
3274: my ($key,$value) = split(/\=/,$line,2);
3275: if (($key) && ($value)) {
3276: $personnel{$tryserver}{&unescape($key)} = &unescape($value);
3277: }
3278: }
1.662 raeburn 3279: }
3280: return %personnel;
3281: }
1.658 raeburn 3282:
1.1057 www 3283: # ----------------------------------------------------------- Interval timing
1.149 www 3284:
1.504 albertel 3285: sub get_first_access {
3286: my ($type,$argsymb)=@_;
1.790 albertel 3287: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3288: if ($argsymb) { $symb=$argsymb; }
3289: my ($map,$id,$res)=&decode_symb($symb);
1.926 albertel 3290: if ($type eq 'course') {
3291: $res='course';
3292: } elsif ($type eq 'map') {
1.588 albertel 3293: $res=&symbread($map);
3294: } else {
3295: $res=$symb;
3296: }
3297: my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
3298: return $times{"$courseid\0$res"};
1.504 albertel 3299: }
3300:
3301: sub set_first_access {
3302: my ($type)=@_;
1.790 albertel 3303: my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504 albertel 3304: my ($map,$id,$res)=&decode_symb($symb);
1.928 albertel 3305: if ($type eq 'course') {
3306: $res='course';
3307: } elsif ($type eq 'map') {
1.588 albertel 3308: $res=&symbread($map);
3309: } else {
3310: $res=$symb;
3311: }
3312: my $firstaccess=&get_first_access($type,$symb);
1.505 albertel 3313: if (!$firstaccess) {
1.588 albertel 3314: return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505 albertel 3315: }
3316: return 'already_set';
1.504 albertel 3317: }
3318:
1.110 www 3319: # --------------------------------------------- Set Expire Date for Spreadsheet
3320:
3321: sub expirespread {
3322: my ($uname,$udom,$stype,$usymb)=@_;
1.620 albertel 3323: my $cid=$env{'request.course.id'};
1.110 www 3324: if ($cid) {
3325: my $now=time;
3326: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620 albertel 3327: return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
3328: $env{'course.'.$cid.'.num'}.
1.110 www 3329: ':nohist_expirationdates:'.
3330: &escape($key).'='.$now,
1.620 albertel 3331: $env{'course.'.$cid.'.home'})
1.110 www 3332: }
3333: return 'ok';
1.14 www 3334: }
3335:
1.109 www 3336: # ----------------------------------------------------- Devalidate Spreadsheets
3337:
3338: sub devalidate {
1.325 www 3339: my ($symb,$uname,$udom)=@_;
1.620 albertel 3340: my $cid=$env{'request.course.id'};
1.109 www 3341: if ($cid) {
1.391 matthew 3342: # delete the stored spreadsheets for
3343: # - the student level sheet of this user in course's homespace
3344: # - the assessment level sheet for this resource
3345: # for this user in user's homespace
1.553 albertel 3346: # - current conditional state info
1.325 www 3347: my $key=$uname.':'.$udom.':';
1.109 www 3348: my $status=
1.299 matthew 3349: &del('nohist_calculatedsheets',
1.391 matthew 3350: [$key.'studentcalc:'],
1.620 albertel 3351: $env{'course.'.$cid.'.domain'},
3352: $env{'course.'.$cid.'.num'})
1.133 albertel 3353: .' '.
3354: &del('nohist_calculatedsheets_'.$cid,
1.391 matthew 3355: [$key.'assesscalc:'.$symb],$udom,$uname);
1.109 www 3356: unless ($status eq 'ok ok') {
3357: &logthis('Could not devalidate spreadsheet '.
1.325 www 3358: $uname.' at '.$udom.' for '.
1.109 www 3359: $symb.': '.$status);
1.133 albertel 3360: }
1.553 albertel 3361: &delenv('user.state.'.$cid);
1.109 www 3362: }
3363: }
3364:
1.265 albertel 3365: sub get_scalar {
3366: my ($string,$end) = @_;
3367: my $value;
3368: if ($$string =~ s/^([^&]*?)($end)/$2/) {
3369: $value = $1;
3370: } elsif ($$string =~ s/^([^&]*?)&//) {
3371: $value = $1;
3372: }
3373: return &unescape($value);
3374: }
3375:
3376: sub array2str {
3377: my (@array) = @_;
3378: my $result=&arrayref2str(\@array);
3379: $result=~s/^__ARRAY_REF__//;
3380: $result=~s/__END_ARRAY_REF__$//;
3381: return $result;
3382: }
3383:
1.204 albertel 3384: sub arrayref2str {
3385: my ($arrayref) = @_;
1.265 albertel 3386: my $result='__ARRAY_REF__';
1.204 albertel 3387: foreach my $elem (@$arrayref) {
1.265 albertel 3388: if(ref($elem) eq 'ARRAY') {
3389: $result.=&arrayref2str($elem).'&';
3390: } elsif(ref($elem) eq 'HASH') {
3391: $result.=&hashref2str($elem).'&';
3392: } elsif(ref($elem)) {
3393: #print("Got a ref of ".(ref($elem))." skipping.");
1.204 albertel 3394: } else {
3395: $result.=&escape($elem).'&';
3396: }
3397: }
3398: $result=~s/\&$//;
1.265 albertel 3399: $result .= '__END_ARRAY_REF__';
1.204 albertel 3400: return $result;
3401: }
3402:
1.168 albertel 3403: sub hash2str {
1.204 albertel 3404: my (%hash) = @_;
3405: my $result=&hashref2str(\%hash);
1.265 albertel 3406: $result=~s/^__HASH_REF__//;
3407: $result=~s/__END_HASH_REF__$//;
1.204 albertel 3408: return $result;
3409: }
3410:
3411: sub hashref2str {
3412: my ($hashref)=@_;
1.265 albertel 3413: my $result='__HASH_REF__';
1.800 albertel 3414: foreach my $key (sort(keys(%$hashref))) {
3415: if (ref($key) eq 'ARRAY') {
3416: $result.=&arrayref2str($key).'=';
3417: } elsif (ref($key) eq 'HASH') {
3418: $result.=&hashref2str($key).'=';
3419: } elsif (ref($key)) {
1.265 albertel 3420: $result.='=';
1.800 albertel 3421: #print("Got a ref of ".(ref($key))." skipping.");
1.204 albertel 3422: } else {
1.800 albertel 3423: if ($key) {$result.=&escape($key).'=';} else { last; }
1.204 albertel 3424: }
3425:
1.800 albertel 3426: if(ref($hashref->{$key}) eq 'ARRAY') {
3427: $result.=&arrayref2str($hashref->{$key}).'&';
3428: } elsif(ref($hashref->{$key}) eq 'HASH') {
3429: $result.=&hashref2str($hashref->{$key}).'&';
3430: } elsif(ref($hashref->{$key})) {
1.265 albertel 3431: $result.='&';
1.800 albertel 3432: #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204 albertel 3433: } else {
1.800 albertel 3434: $result.=&escape($hashref->{$key}).'&';
1.204 albertel 3435: }
3436: }
1.168 albertel 3437: $result=~s/\&$//;
1.265 albertel 3438: $result .= '__END_HASH_REF__';
1.168 albertel 3439: return $result;
3440: }
3441:
3442: sub str2hash {
1.265 albertel 3443: my ($string)=@_;
3444: my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
3445: return %$hash;
3446: }
3447:
3448: sub str2hashref {
1.168 albertel 3449: my ($string) = @_;
1.265 albertel 3450:
3451: my %hash;
3452:
3453: if($string !~ /^__HASH_REF__/) {
3454: if (! ($string eq '' || !defined($string))) {
3455: $hash{'error'}='Not hash reference';
3456: }
3457: return (\%hash, $string);
3458: }
3459:
3460: $string =~ s/^__HASH_REF__//;
3461:
3462: while($string !~ /^__END_HASH_REF__/) {
3463: #key
3464: my $key='';
3465: if($string =~ /^__HASH_REF__/) {
3466: ($key, $string)=&str2hashref($string);
3467: if(defined($key->{'error'})) {
3468: $hash{'error'}='Bad data';
3469: return (\%hash, $string);
3470: }
3471: } elsif($string =~ /^__ARRAY_REF__/) {
3472: ($key, $string)=&str2arrayref($string);
3473: if($key->[0] eq 'Array reference error') {
3474: $hash{'error'}='Bad data';
3475: return (\%hash, $string);
3476: }
3477: } else {
3478: $string =~ s/^(.*?)=//;
1.267 albertel 3479: $key=&unescape($1);
1.265 albertel 3480: }
3481: $string =~ s/^=//;
3482:
3483: #value
3484: my $value='';
3485: if($string =~ /^__HASH_REF__/) {
3486: ($value, $string)=&str2hashref($string);
3487: if(defined($value->{'error'})) {
3488: $hash{'error'}='Bad data';
3489: return (\%hash, $string);
3490: }
3491: } elsif($string =~ /^__ARRAY_REF__/) {
3492: ($value, $string)=&str2arrayref($string);
3493: if($value->[0] eq 'Array reference error') {
3494: $hash{'error'}='Bad data';
3495: return (\%hash, $string);
3496: }
3497: } else {
3498: $value=&get_scalar(\$string,'__END_HASH_REF__');
3499: }
3500: $string =~ s/^&//;
3501:
3502: $hash{$key}=$value;
1.204 albertel 3503: }
1.265 albertel 3504:
3505: $string =~ s/^__END_HASH_REF__//;
3506:
3507: return (\%hash, $string);
1.204 albertel 3508: }
3509:
3510: sub str2array {
1.265 albertel 3511: my ($string)=@_;
3512: my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
3513: return @$array;
3514: }
3515:
3516: sub str2arrayref {
1.204 albertel 3517: my ($string) = @_;
1.265 albertel 3518: my @array;
3519:
3520: if($string !~ /^__ARRAY_REF__/) {
3521: if (! ($string eq '' || !defined($string))) {
3522: $array[0]='Array reference error';
3523: }
3524: return (\@array, $string);
3525: }
3526:
3527: $string =~ s/^__ARRAY_REF__//;
3528:
3529: while($string !~ /^__END_ARRAY_REF__/) {
3530: my $value='';
3531: if($string =~ /^__HASH_REF__/) {
3532: ($value, $string)=&str2hashref($string);
3533: if(defined($value->{'error'})) {
3534: $array[0] ='Array reference error';
3535: return (\@array, $string);
3536: }
3537: } elsif($string =~ /^__ARRAY_REF__/) {
3538: ($value, $string)=&str2arrayref($string);
3539: if($value->[0] eq 'Array reference error') {
3540: $array[0] ='Array reference error';
3541: return (\@array, $string);
3542: }
3543: } else {
3544: $value=&get_scalar(\$string,'__END_ARRAY_REF__');
3545: }
3546: $string =~ s/^&//;
3547:
3548: push(@array, $value);
1.191 harris41 3549: }
1.265 albertel 3550:
3551: $string =~ s/^__END_ARRAY_REF__//;
3552:
3553: return (\@array, $string);
1.168 albertel 3554: }
3555:
1.167 albertel 3556: # -------------------------------------------------------------------Temp Store
3557:
1.168 albertel 3558: sub tmpreset {
3559: my ($symb,$namespace,$domain,$stuname) = @_;
3560: if (!$symb) {
3561: $symb=&symbread();
1.620 albertel 3562: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3563: }
3564: $symb=escape($symb);
3565:
1.620 albertel 3566: if (!$namespace) { $namespace=$env{'request.state'}; }
1.168 albertel 3567: $namespace=~s/\//\_/g;
3568: $namespace=~s/\W//g;
3569:
1.620 albertel 3570: if (!$domain) { $domain=$env{'user.domain'}; }
3571: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3572: if ($domain eq 'public' && $stuname eq 'public') {
3573: $stuname=$ENV{'REMOTE_ADDR'};
3574: }
1.168 albertel 3575: my $path=$perlvar{'lonDaemons'}.'/tmp';
3576: my %hash;
3577: if (tie(%hash,'GDBM_File',
3578: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3579: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 3580: foreach my $key (keys(%hash)) {
1.180 albertel 3581: if ($key=~ /:$symb/) {
1.168 albertel 3582: delete($hash{$key});
3583: }
3584: }
3585: }
3586: }
3587:
1.167 albertel 3588: sub tmpstore {
1.168 albertel 3589: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3590:
3591: if (!$symb) {
3592: $symb=&symbread();
1.620 albertel 3593: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3594: }
3595: $symb=escape($symb);
3596:
3597: if (!$namespace) {
3598: # I don't think we would ever want to store this for a course.
3599: # it seems this will only be used if we don't have a course.
1.620 albertel 3600: #$namespace=$env{'request.course.id'};
1.168 albertel 3601: #if (!$namespace) {
1.620 albertel 3602: $namespace=$env{'request.state'};
1.168 albertel 3603: #}
3604: }
3605: $namespace=~s/\//\_/g;
3606: $namespace=~s/\W//g;
1.620 albertel 3607: if (!$domain) { $domain=$env{'user.domain'}; }
3608: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3609: if ($domain eq 'public' && $stuname eq 'public') {
3610: $stuname=$ENV{'REMOTE_ADDR'};
3611: }
1.168 albertel 3612: my $now=time;
3613: my %hash;
3614: my $path=$perlvar{'lonDaemons'}.'/tmp';
3615: if (tie(%hash,'GDBM_File',
3616: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3617: &GDBM_WRCREAT(),0640)) {
1.168 albertel 3618: $hash{"version:$symb"}++;
3619: my $version=$hash{"version:$symb"};
3620: my $allkeys='';
3621: foreach my $key (keys(%$storehash)) {
3622: $allkeys.=$key.':';
1.591 albertel 3623: $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168 albertel 3624: }
3625: $hash{"$version:$symb:timestamp"}=$now;
3626: $allkeys.='timestamp';
3627: $hash{"$version:keys:$symb"}=$allkeys;
3628: if (untie(%hash)) {
3629: return 'ok';
3630: } else {
3631: return "error:$!";
3632: }
3633: } else {
3634: return "error:$!";
3635: }
3636: }
1.167 albertel 3637:
1.168 albertel 3638: # -----------------------------------------------------------------Temp Restore
1.167 albertel 3639:
1.168 albertel 3640: sub tmprestore {
3641: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 3642:
1.168 albertel 3643: if (!$symb) {
3644: $symb=&symbread();
1.620 albertel 3645: if (!$symb) { $symb= $env{'request.url'}; }
1.168 albertel 3646: }
3647: $symb=escape($symb);
3648:
1.620 albertel 3649: if (!$namespace) { $namespace=$env{'request.state'}; }
1.591 albertel 3650:
1.620 albertel 3651: if (!$domain) { $domain=$env{'user.domain'}; }
3652: if (!$stuname) { $stuname=$env{'user.name'}; }
1.591 albertel 3653: if ($domain eq 'public' && $stuname eq 'public') {
3654: $stuname=$ENV{'REMOTE_ADDR'};
3655: }
1.168 albertel 3656: my %returnhash;
3657: $namespace=~s/\//\_/g;
3658: $namespace=~s/\W//g;
3659: my %hash;
3660: my $path=$perlvar{'lonDaemons'}.'/tmp';
3661: if (tie(%hash,'GDBM_File',
3662: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256 albertel 3663: &GDBM_READER(),0640)) {
1.168 albertel 3664: my $version=$hash{"version:$symb"};
3665: $returnhash{'version'}=$version;
3666: my $scope;
3667: for ($scope=1;$scope<=$version;$scope++) {
3668: my $vkeys=$hash{"$scope:keys:$symb"};
3669: my @keys=split(/:/,$vkeys);
3670: my $key;
3671: $returnhash{"$scope:keys"}=$vkeys;
3672: foreach $key (@keys) {
1.591 albertel 3673: $returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
3674: $returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167 albertel 3675: }
3676: }
1.168 albertel 3677: if (!(untie(%hash))) {
3678: return "error:$!";
3679: }
3680: } else {
3681: return "error:$!";
3682: }
3683: return %returnhash;
1.167 albertel 3684: }
3685:
1.9 www 3686: # ----------------------------------------------------------------------- Store
3687:
3688: sub store {
1.124 www 3689: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3690: my $home='';
3691:
1.168 albertel 3692: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3693:
1.213 www 3694: $symb=&symbclean($symb);
1.122 albertel 3695: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 3696:
1.620 albertel 3697: if (!$domain) { $domain=$env{'user.domain'}; }
3698: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 3699:
3700: &devalidate($symb,$stuname,$domain);
1.109 www 3701:
3702: $symb=escape($symb);
1.187 www 3703: if (!$namespace) {
1.620 albertel 3704: unless ($namespace=$env{'request.course.id'}) {
1.187 www 3705: return '';
3706: }
3707: }
1.620 albertel 3708: if (!$home) { $home=$env{'user.home'}; }
1.447 www 3709:
3710: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
3711: $$storehash{'host'}=$perlvar{'lonHostID'};
3712:
1.12 www 3713: my $namevalue='';
1.800 albertel 3714: foreach my $key (keys(%$storehash)) {
3715: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 3716: }
1.12 www 3717: $namevalue=~s/\&$//;
1.187 www 3718: &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124 www 3719: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 3720: }
3721:
1.47 www 3722: # -------------------------------------------------------------- Critical Store
3723:
3724: sub cstore {
1.124 www 3725: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
3726: my $home='';
3727:
1.168 albertel 3728: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3729:
1.213 www 3730: $symb=&symbclean($symb);
1.122 albertel 3731: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 3732:
1.620 albertel 3733: if (!$domain) { $domain=$env{'user.domain'}; }
3734: if (!$stuname) { $stuname=$env{'user.name'}; }
1.325 www 3735:
3736: &devalidate($symb,$stuname,$domain);
1.109 www 3737:
3738: $symb=escape($symb);
1.187 www 3739: if (!$namespace) {
1.620 albertel 3740: unless ($namespace=$env{'request.course.id'}) {
1.187 www 3741: return '';
3742: }
3743: }
1.620 albertel 3744: if (!$home) { $home=$env{'user.home'}; }
1.447 www 3745:
3746: $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
3747: $$storehash{'host'}=$perlvar{'lonHostID'};
1.122 albertel 3748:
1.47 www 3749: my $namevalue='';
1.800 albertel 3750: foreach my $key (keys(%$storehash)) {
3751: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191 harris41 3752: }
1.47 www 3753: $namevalue=~s/\&$//;
1.187 www 3754: &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188 www 3755: return critical
3756: ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 3757: }
3758:
1.9 www 3759: # --------------------------------------------------------------------- Restore
3760:
3761: sub restore {
1.124 www 3762: my ($symb,$namespace,$domain,$stuname) = @_;
3763: my $home='';
3764:
1.168 albertel 3765: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 3766:
1.122 albertel 3767: if (!$symb) {
3768: unless ($symb=escape(&symbread())) { return ''; }
3769: } else {
1.213 www 3770: $symb=&escape(&symbclean($symb));
1.122 albertel 3771: }
1.188 www 3772: if (!$namespace) {
1.620 albertel 3773: unless ($namespace=$env{'request.course.id'}) {
1.188 www 3774: return '';
3775: }
3776: }
1.620 albertel 3777: if (!$domain) { $domain=$env{'user.domain'}; }
3778: if (!$stuname) { $stuname=$env{'user.name'}; }
3779: if (!$home) { $home=$env{'user.home'}; }
1.122 albertel 3780: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
3781:
1.12 www 3782: my %returnhash=();
1.800 albertel 3783: foreach my $line (split(/\&/,$answer)) {
3784: my ($name,$value)=split(/\=/,$line);
1.591 albertel 3785: $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191 harris41 3786: }
1.75 www 3787: my $version;
3788: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800 albertel 3789: foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
3790: $returnhash{$item}=$returnhash{$version.':'.$item};
1.191 harris41 3791: }
1.75 www 3792: }
1.13 www 3793: return %returnhash;
1.34 www 3794: }
3795:
3796: # ---------------------------------------------------------- Course Description
3797:
3798: sub coursedescription {
1.731 albertel 3799: my ($courseid,$args)=@_;
1.34 www 3800: $courseid=~s/^\///;
1.49 www 3801: $courseid=~s/\_/\//g;
1.34 www 3802: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 3803: my $chome=&homeserver($cnum,$cdomain);
1.302 albertel 3804: my $normalid=$cdomain.'_'.$cnum;
3805: # need to always cache even if we get errors otherwise we keep
3806: # trying and trying and trying to get the course description.
3807: my %envhash=();
3808: my %returnhash=();
1.731 albertel 3809:
3810: my $expiretime=600;
3811: if ($env{'request.course.id'} eq $normalid) {
3812: $expiretime=120;
3813: }
3814:
3815: my $prefix='course.'.$cdomain.'_'.$cnum.'.';
3816: if (!$args->{'freshen_cache'}
3817: && ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
3818: foreach my $key (keys(%env)) {
3819: next if ($key !~ /^\Q$prefix\E(.*)/);
3820: my ($setting) = $1;
3821: $returnhash{$setting} = $env{$key};
3822: }
3823: return %returnhash;
3824: }
3825:
3826: # get the data agin
3827: if (!$args->{'one_time'}) {
3828: $envhash{'course.'.$normalid.'.last_cache'}=time;
3829: }
1.811 albertel 3830:
1.34 www 3831: if ($chome ne 'no_host') {
1.302 albertel 3832: %returnhash=&dump('environment',$cdomain,$cnum);
1.129 albertel 3833: if (!exists($returnhash{'con_lost'})) {
3834: $returnhash{'home'}= $chome;
3835: $returnhash{'domain'} = $cdomain;
3836: $returnhash{'num'} = $cnum;
1.741 raeburn 3837: if (!defined($returnhash{'type'})) {
3838: $returnhash{'type'} = 'Course';
3839: }
1.130 albertel 3840: while (my ($name,$value) = each %returnhash) {
1.53 www 3841: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 3842: }
1.270 www 3843: $returnhash{'url'}=&clutter($returnhash{'url'});
1.34 www 3844: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620 albertel 3845: $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60 www 3846: $envhash{'course.'.$normalid.'.home'}=$chome;
3847: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
3848: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34 www 3849: }
3850: }
1.731 albertel 3851: if (!$args->{'one_time'}) {
1.949 raeburn 3852: &appenv(\%envhash);
1.731 albertel 3853: }
1.302 albertel 3854: return %returnhash;
1.461 www 3855: }
3856:
3857: # -------------------------------------------------See if a user is privileged
3858:
3859: sub privileged {
3860: my ($username,$domain)=@_;
3861: my $rolesdump=&reply("dump:$domain:$username:roles",
3862: &homeserver($username,$domain));
1.1033 raeburn 3863: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
3864: ($rolesdump =~ /^error:/)) {
3865: return 0;
3866: }
1.461 www 3867: my $now=time;
3868: if ($rolesdump ne '') {
1.800 albertel 3869: foreach my $entry (split(/&/,$rolesdump)) {
3870: if ($entry!~/^rolesdef_/) {
3871: my ($area,$role)=split(/=/,$entry);
1.461 www 3872: $area=~s/\_\w\w$//;
3873: my ($trole,$tend,$tstart)=split(/_/,$role);
3874: if (($trole eq 'dc') || ($trole eq 'su')) {
3875: my $active=1;
3876: if ($tend) {
3877: if ($tend<$now) { $active=0; }
3878: }
3879: if ($tstart) {
3880: if ($tstart>$now) { $active=0; }
3881: }
3882: if ($active) { return 1; }
3883: }
3884: }
3885: }
3886: }
3887: return 0;
1.9 www 3888: }
1.1 albertel 3889:
1.103 harris41 3890: # -------------------------------------------------------- Get user privileges
1.11 www 3891:
3892: sub rolesinit {
3893: my ($domain,$username,$authhost)=@_;
1.1034 raeburn 3894: my $now=time;
3895: my %userroles = ('user.login.time' => $now);
1.11 www 3896: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.1033 raeburn 3897: if (($rolesdump eq 'con_lost') || ($rolesdump eq '') ||
3898: ($rolesdump =~ /^error:/)) {
3899: return \%userroles;
3900: }
1.11 www 3901: my %allroles=();
1.678 raeburn 3902: my %allgroups=();
3903: my $group_privs;
1.11 www 3904:
3905: if ($rolesdump ne '') {
1.800 albertel 3906: foreach my $entry (split(/&/,$rolesdump)) {
3907: if ($entry!~/^rolesdef_/) {
3908: my ($area,$role)=split(/=/,$entry);
1.587 albertel 3909: $area=~s/\_\w\w$//;
1.678 raeburn 3910: my ($trole,$tend,$tstart,$group_privs);
1.587 albertel 3911: if ($role=~/^cr/) {
1.807 albertel 3912: if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
3913: ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655 albertel 3914: ($tend,$tstart)=split('_',$trest);
3915: } else {
3916: $trole=$role;
3917: }
1.678 raeburn 3918: } elsif ($role =~ m|^gr/|) {
3919: ($trole,$tend,$tstart) = split(/_/,$role);
3920: ($trole,$group_privs) = split(/\//,$trole);
3921: $group_privs = &unescape($group_privs);
1.587 albertel 3922: } else {
3923: ($trole,$tend,$tstart)=split(/_/,$role);
3924: }
1.743 albertel 3925: my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
3926: $username);
3927: @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567 raeburn 3928: if (($tend!=0) && ($tend<$now)) { $trole=''; }
3929: if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11 www 3930: if (($area ne '') && ($trole ne '')) {
1.347 albertel 3931: my $spec=$trole.'.'.$area;
3932: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
3933: if ($trole =~ /^cr\//) {
1.567 raeburn 3934: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678 raeburn 3935: } elsif ($trole eq 'gr') {
3936: &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347 albertel 3937: } else {
1.567 raeburn 3938: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347 albertel 3939: }
1.12 www 3940: }
1.662 raeburn 3941: }
1.191 harris41 3942: }
1.743 albertel 3943: my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
3944: $userroles{'user.adv'} = $adv;
3945: $userroles{'user.author'} = $author;
1.620 albertel 3946: $env{'user.adv'}=$adv;
1.11 www 3947: }
1.743 albertel 3948: return \%userroles;
1.11 www 3949: }
3950:
1.567 raeburn 3951: sub set_arearole {
3952: my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
3953: # log the associated role with the area
3954: &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743 albertel 3955: return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567 raeburn 3956: }
3957:
3958: sub custom_roleprivs {
3959: my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
3960: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
3961: my $homsvr=homeserver($rauthor,$rdomain);
1.838 albertel 3962: if (&hostname($homsvr) ne '') {
1.567 raeburn 3963: my ($rdummy,$roledef)=
3964: &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
3965: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
3966: my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
3967: if (defined($syspriv)) {
1.1043 raeburn 3968: if ($trest =~ /^$match_community$/) {
3969: $syspriv =~ s/bre\&S//;
3970: }
1.567 raeburn 3971: $$allroles{'cm./'}.=':'.$syspriv;
3972: $$allroles{$spec.'./'}.=':'.$syspriv;
3973: }
3974: if ($tdomain ne '') {
3975: if (defined($dompriv)) {
3976: $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
3977: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
3978: }
3979: if (($trest ne '') && (defined($coursepriv))) {
3980: $$allroles{'cm.'.$area}.=':'.$coursepriv;
3981: $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
3982: }
3983: }
3984: }
3985: }
3986: }
3987:
1.678 raeburn 3988: sub group_roleprivs {
3989: my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
3990: my $access = 1;
3991: my $now = time;
3992: if (($tend!=0) && ($tend<$now)) { $access = 0; }
3993: if (($tstart!=0) && ($tstart>$now)) { $access=0; }
3994: if ($access) {
1.811 albertel 3995: my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678 raeburn 3996: $$allgroups{$course}{$group} .=':'.$group_privs;
3997: }
3998: }
1.567 raeburn 3999:
4000: sub standard_roleprivs {
4001: my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
4002: if (defined($pr{$trole.':s'})) {
4003: $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
4004: $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
4005: }
4006: if ($tdomain ne '') {
4007: if (defined($pr{$trole.':d'})) {
4008: $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4009: $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
4010: }
4011: if (($trest ne '') && (defined($pr{$trole.':c'}))) {
4012: $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
4013: $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
4014: }
4015: }
4016: }
4017:
4018: sub set_userprivs {
1.1064 raeburn 4019: my ($userroles,$allroles,$allgroups,$groups_roles) = @_;
1.567 raeburn 4020: my $author=0;
4021: my $adv=0;
1.678 raeburn 4022: my %grouproles = ();
4023: if (keys(%{$allgroups}) > 0) {
1.1064 raeburn 4024: my @groupkeys;
1.1000 raeburn 4025: foreach my $role (keys(%{$allroles})) {
1.1064 raeburn 4026: push(@groupkeys,$role);
4027: }
4028: if (ref($groups_roles) eq 'HASH') {
4029: foreach my $key (keys(%{$groups_roles})) {
4030: unless (grep(/^\Q$key\E$/,@groupkeys)) {
4031: push(@groupkeys,$key);
4032: }
4033: }
4034: }
4035: if (@groupkeys > 0) {
4036: foreach my $role (@groupkeys) {
4037: my ($trole,$area,$sec,$extendedarea);
4038: if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
4039: $trole = $1;
4040: $area = $2;
4041: $sec = $3;
4042: $extendedarea = $area.$sec;
4043: if (exists($$allgroups{$area})) {
4044: foreach my $group (keys(%{$$allgroups{$area}})) {
4045: my $spec = $trole.'.'.$extendedarea;
4046: $grouproles{$spec.'.'.$area.'/'.$group} =
1.681 raeburn 4047: $$allgroups{$area}{$group};
1.1064 raeburn 4048: }
1.678 raeburn 4049: }
4050: }
4051: }
4052: }
4053: }
1.800 albertel 4054: foreach my $group (keys(%grouproles)) {
4055: $$allroles{$group} = $grouproles{$group};
1.678 raeburn 4056: }
1.800 albertel 4057: foreach my $role (keys(%{$allroles})) {
4058: my %thesepriv;
1.941 raeburn 4059: if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800 albertel 4060: foreach my $item (split(/:/,$$allroles{$role})) {
4061: if ($item ne '') {
4062: my ($privilege,$restrictions)=split(/&/,$item);
1.567 raeburn 4063: if ($restrictions eq '') {
4064: $thesepriv{$privilege}='F';
4065: } elsif ($thesepriv{$privilege} ne 'F') {
4066: $thesepriv{$privilege}.=$restrictions;
4067: }
4068: if ($thesepriv{'adv'} eq 'F') { $adv=1; }
4069: }
4070: }
4071: my $thesestr='';
1.800 albertel 4072: foreach my $priv (keys(%thesepriv)) {
4073: $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
4074: }
4075: $userroles->{'user.priv.'.$role} = $thesestr;
1.567 raeburn 4076: }
4077: return ($author,$adv);
4078: }
4079:
1.994 raeburn 4080: sub role_status {
1.1002 raeburn 4081: my ($rolekey,$then,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994 raeburn 4082: my @pwhere = ();
4083: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
4084: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
4085: unless (!defined($$role) || $$role eq '') {
4086: $$where=join('.',@pwhere);
4087: $$trolecode=$$role.'.'.$$where;
4088: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
4089: $$tstatus='is';
4090: if ($$tstart && $$tstart>$then) {
4091: $$tstatus='future';
1.1034 raeburn 4092: if ($$tstart<$now) {
4093: if ($$tstart && $$tstart>$refresh) {
1.1002 raeburn 4094: if (($$where ne '') && ($$role ne '')) {
1.1064 raeburn 4095: my (%allroles,%allgroups,$group_privs,
4096: %groups_roles,@rolecodes);
1.1002 raeburn 4097: my %userroles = (
4098: 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
4099: );
1.1064 raeburn 4100: @rolecodes = ('cm');
1.1002 raeburn 4101: my $spec=$$role.'.'.$$where;
4102: my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
4103: if ($$role =~ /^cr\//) {
4104: &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064 raeburn 4105: push(@rolecodes,'cr');
1.1002 raeburn 4106: } elsif ($$role eq 'gr') {
1.1064 raeburn 4107: push(@rolecodes,$$role);
1.1002 raeburn 4108: my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
4109: $env{'user.name'});
1.1064 raeburn 4110: my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002 raeburn 4111: (undef,my $group_privs) = split(/\//,$trole);
4112: $group_privs = &unescape($group_privs);
4113: &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064 raeburn 4114: my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
4115: if (keys(%course_roles) > 0) {
4116: my ($tnum) = ($trest =~ /^($match_courseid)/);
4117: if ($tdomain ne '' && $tnum ne '') {
4118: foreach my $key (keys(%course_roles)) {
4119: if ($key =~ /^\Q$tnum\E:\Q$tdomain\E:([^:]+):?([^:]*)/) {
4120: my $crsrole = $1;
4121: my $crssec = $2;
4122: if ($crsrole =~ /^cr/) {
4123: unless (grep(/^cr$/,@rolecodes)) {
4124: push(@rolecodes,'cr');
4125: }
4126: } else {
4127: unless(grep(/^\Q$crsrole\E$/,@rolecodes)) {
4128: push(@rolecodes,$crsrole);
4129: }
4130: }
4131: my $rolekey = $crsrole.'./'.$tdomain.'/'.$tnum;
4132: if ($crssec ne '') {
4133: $rolekey .= '/'.$crssec;
4134: }
4135: $rolekey .= './';
4136: $groups_roles{$rolekey} = \@rolecodes;
4137: }
4138: }
4139: }
4140: }
1.1002 raeburn 4141: } else {
1.1064 raeburn 4142: push(@rolecodes,$$role);
1.1002 raeburn 4143: &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
4144: }
1.1064 raeburn 4145: my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
4146: &appenv(\%userroles,\@rolecodes);
1.1002 raeburn 4147: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
4148: }
4149: }
1.1034 raeburn 4150: $$tstatus = 'is';
1.1002 raeburn 4151: }
1.994 raeburn 4152: }
4153: if ($$tend) {
4154: if ($$tend<$then) {
4155: $$tstatus='expired';
4156: } elsif ($$tend<$now) {
4157: $$tstatus='will_not';
4158: }
4159: }
4160: }
4161: }
4162: }
4163:
4164: sub check_adhoc_privs {
1.1002 raeburn 4165: my ($cdom,$cnum,$then,$refresh,$now,$checkrole) = @_;
1.994 raeburn 4166: my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
4167: if ($env{$cckey}) {
4168: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1002 raeburn 4169: &role_status($cckey,$then,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994 raeburn 4170: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
4171: &set_adhoc_privileges($cdom,$cnum,$checkrole);
4172: }
4173: } else {
4174: &set_adhoc_privileges($cdom,$cnum,$checkrole);
4175: }
4176: }
4177:
4178: sub set_adhoc_privileges {
4179: # role can be cc or ca
4180: my ($dcdom,$pickedcourse,$role) = @_;
4181: my $area = '/'.$dcdom.'/'.$pickedcourse;
4182: my $spec = $role.'.'.$area;
4183: my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
4184: $env{'user.name'});
4185: my %ccrole = ();
4186: &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
4187: my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
4188: &appenv(\%userroles,[$role,'cm']);
4189: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
4190: &appenv( {'request.role' => $spec,
4191: 'request.role.domain' => $dcdom,
4192: 'request.course.sec' => ''
4193: }
4194: );
4195: my $tadv=0;
4196: if (&allowed('adv') eq 'F') { $tadv=1; }
4197: &appenv({'request.role.adv' => $tadv});
4198: }
4199:
1.12 www 4200: # --------------------------------------------------------------- get interface
4201:
4202: sub get {
1.131 albertel 4203: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4204: my $items='';
1.800 albertel 4205: foreach my $item (@$storearr) {
4206: $items.=&escape($item).'&';
1.191 harris41 4207: }
1.12 www 4208: $items=~s/\&$//;
1.620 albertel 4209: if (!$udomain) { $udomain=$env{'user.domain'}; }
4210: if (!$uname) { $uname=$env{'user.name'}; }
1.131 albertel 4211: my $uhome=&homeserver($uname,$udomain);
4212:
1.133 albertel 4213: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4214: my @pairs=split(/\&/,$rep);
1.273 albertel 4215: if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
4216: return @pairs;
4217: }
1.15 www 4218: my %returnhash=();
1.42 www 4219: my $i=0;
1.800 albertel 4220: foreach my $item (@$storearr) {
4221: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4222: $i++;
1.191 harris41 4223: }
1.15 www 4224: return %returnhash;
1.27 www 4225: }
4226:
4227: # --------------------------------------------------------------- del interface
4228:
4229: sub del {
1.133 albertel 4230: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 4231: my $items='';
1.800 albertel 4232: foreach my $item (@$storearr) {
4233: $items.=&escape($item).'&';
1.191 harris41 4234: }
1.984 neumanie 4235:
1.27 www 4236: $items=~s/\&$//;
1.620 albertel 4237: if (!$udomain) { $udomain=$env{'user.domain'}; }
4238: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4239: my $uhome=&homeserver($uname,$udomain);
4240: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 4241: }
4242:
4243: # -------------------------------------------------------------- dump interface
4244:
4245: sub dump {
1.755 albertel 4246: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
4247: if (!$udomain) { $udomain=$env{'user.domain'}; }
4248: if (!$uname) { $uname=$env{'user.name'}; }
4249: my $uhome=&homeserver($uname,$udomain);
4250: if ($regexp) {
4251: $regexp=&escape($regexp);
4252: } else {
4253: $regexp='.';
4254: }
4255: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
4256: my @pairs=split(/\&/,$rep);
4257: my %returnhash=();
4258: foreach my $item (@pairs) {
4259: my ($key,$value)=split(/=/,$item,2);
4260: $key = &unescape($key);
4261: next if ($key =~ /^error: 2 /);
4262: $returnhash{$key}=&thaw_unescape($value);
4263: }
4264: return %returnhash;
1.407 www 4265: }
4266:
1.717 albertel 4267: # --------------------------------------------------------- dumpstore interface
4268:
4269: sub dumpstore {
4270: my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822 albertel 4271: if (!$udomain) { $udomain=$env{'user.domain'}; }
4272: if (!$uname) { $uname=$env{'user.name'}; }
4273: my $uhome=&homeserver($uname,$udomain);
4274: if ($regexp) {
4275: $regexp=&escape($regexp);
4276: } else {
4277: $regexp='.';
4278: }
4279: my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
4280: my @pairs=split(/\&/,$rep);
4281: my %returnhash=();
4282: foreach my $item (@pairs) {
4283: my ($key,$value)=split(/=/,$item,2);
4284: next if ($key =~ /^error: 2 /);
4285: $returnhash{$key}=&thaw_unescape($value);
4286: }
4287: return %returnhash;
1.717 albertel 4288: }
4289:
1.407 www 4290: # -------------------------------------------------------------- keys interface
4291:
4292: sub getkeys {
4293: my ($namespace,$udomain,$uname)=@_;
1.620 albertel 4294: if (!$udomain) { $udomain=$env{'user.domain'}; }
4295: if (!$uname) { $uname=$env{'user.name'}; }
1.407 www 4296: my $uhome=&homeserver($uname,$udomain);
4297: my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
4298: my @keyarray=();
1.800 albertel 4299: foreach my $key (split(/\&/,$rep)) {
1.812 raeburn 4300: next if ($key =~ /^error: 2 /);
1.800 albertel 4301: push(@keyarray,&unescape($key));
1.407 www 4302: }
4303: return @keyarray;
1.318 matthew 4304: }
4305:
1.319 matthew 4306: # --------------------------------------------------------------- currentdump
4307: sub currentdump {
1.328 matthew 4308: my ($courseid,$sdom,$sname)=@_;
1.620 albertel 4309: $courseid = $env{'request.course.id'} if (! defined($courseid));
4310: $sdom = $env{'user.domain'} if (! defined($sdom));
4311: $sname = $env{'user.name'} if (! defined($sname));
1.326 matthew 4312: my $uhome = &homeserver($sname,$sdom);
4313: my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318 matthew 4314: return if ($rep =~ /^(error:|no_such_host)/);
1.319 matthew 4315: #
1.318 matthew 4316: my %returnhash=();
1.319 matthew 4317: #
4318: if ($rep eq "unknown_cmd") {
4319: # an old lond will not know currentdump
4320: # Do a dump and make it look like a currentdump
1.822 albertel 4321: my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319 matthew 4322: return if ($tmp[0] =~ /^(error:|no_such_host)/);
4323: my %hash = @tmp;
4324: @tmp=();
1.424 matthew 4325: %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319 matthew 4326: } else {
4327: my @pairs=split(/\&/,$rep);
1.800 albertel 4328: foreach my $pair (@pairs) {
4329: my ($key,$value)=split(/=/,$pair,2);
1.319 matthew 4330: my ($symb,$param) = split(/:/,$key);
4331: $returnhash{&unescape($symb)}->{&unescape($param)} =
1.557 albertel 4332: &thaw_unescape($value);
1.319 matthew 4333: }
1.191 harris41 4334: }
1.12 www 4335: return %returnhash;
1.424 matthew 4336: }
4337:
4338: sub convert_dump_to_currentdump{
4339: my %hash = %{shift()};
4340: my %returnhash;
4341: # Code ripped from lond, essentially. The only difference
4342: # here is the unescaping done by lonnet::dump(). Conceivably
4343: # we might run in to problems with parameter names =~ /^v\./
4344: while (my ($key,$value) = each(%hash)) {
4345: my ($v,$symb,$param) = split(/:/,$key);
1.822 albertel 4346: $symb = &unescape($symb);
4347: $param = &unescape($param);
1.424 matthew 4348: next if ($v eq 'version' || $symb eq 'keys');
4349: next if (exists($returnhash{$symb}) &&
4350: exists($returnhash{$symb}->{$param}) &&
4351: $returnhash{$symb}->{'v.'.$param} > $v);
4352: $returnhash{$symb}->{$param}=$value;
4353: $returnhash{$symb}->{'v.'.$param}=$v;
4354: }
4355: #
4356: # Remove all of the keys in the hashes which keep track of
4357: # the version of the parameter.
4358: while (my ($symb,$param_hash) = each(%returnhash)) {
4359: # use a foreach because we are going to delete from the hash.
4360: foreach my $key (keys(%$param_hash)) {
4361: delete($param_hash->{$key}) if ($key =~ /^v\./);
4362: }
4363: }
4364: return \%returnhash;
1.12 www 4365: }
4366:
1.627 albertel 4367: # ------------------------------------------------------ critical inc interface
4368:
4369: sub cinc {
4370: return &inc(@_,'critical');
4371: }
4372:
1.449 matthew 4373: # --------------------------------------------------------------- inc interface
4374:
4375: sub inc {
1.627 albertel 4376: my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620 albertel 4377: if (!$udomain) { $udomain=$env{'user.domain'}; }
4378: if (!$uname) { $uname=$env{'user.name'}; }
1.449 matthew 4379: my $uhome=&homeserver($uname,$udomain);
4380: my $items='';
4381: if (! ref($store)) {
4382: # got a single value, so use that instead
4383: $items = &escape($store).'=&';
4384: } elsif (ref($store) eq 'SCALAR') {
4385: $items = &escape($$store).'=&';
4386: } elsif (ref($store) eq 'ARRAY') {
4387: $items = join('=&',map {&escape($_);} @{$store});
4388: } elsif (ref($store) eq 'HASH') {
4389: while (my($key,$value) = each(%{$store})) {
4390: $items.= &escape($key).'='.&escape($value).'&';
4391: }
4392: }
4393: $items=~s/\&$//;
1.627 albertel 4394: if ($critical) {
4395: return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
4396: } else {
4397: return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
4398: }
1.449 matthew 4399: }
4400:
1.12 www 4401: # --------------------------------------------------------------- put interface
4402:
4403: sub put {
1.134 albertel 4404: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4405: if (!$udomain) { $udomain=$env{'user.domain'}; }
4406: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4407: my $uhome=&homeserver($uname,$udomain);
1.12 www 4408: my $items='';
1.800 albertel 4409: foreach my $item (keys(%$storehash)) {
4410: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4411: }
1.12 www 4412: $items=~s/\&$//;
1.134 albertel 4413: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 4414: }
4415:
1.631 albertel 4416: # ------------------------------------------------------------ newput interface
4417:
4418: sub newput {
4419: my ($namespace,$storehash,$udomain,$uname)=@_;
4420: if (!$udomain) { $udomain=$env{'user.domain'}; }
4421: if (!$uname) { $uname=$env{'user.name'}; }
4422: my $uhome=&homeserver($uname,$udomain);
4423: my $items='';
4424: foreach my $key (keys(%$storehash)) {
4425: $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
4426: }
4427: $items=~s/\&$//;
4428: return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
4429: }
4430:
4431: # --------------------------------------------------------- putstore interface
4432:
1.524 raeburn 4433: sub putstore {
1.715 albertel 4434: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620 albertel 4435: if (!$udomain) { $udomain=$env{'user.domain'}; }
4436: if (!$uname) { $uname=$env{'user.name'}; }
1.524 raeburn 4437: my $uhome=&homeserver($uname,$udomain);
4438: my $items='';
1.715 albertel 4439: foreach my $key (keys(%$storehash)) {
4440: $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524 raeburn 4441: }
1.715 albertel 4442: $items=~s/\&$//;
1.716 albertel 4443: my $esc_symb=&escape($symb);
4444: my $esc_v=&escape($version);
1.715 albertel 4445: my $reply =
1.716 albertel 4446: &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715 albertel 4447: $uhome);
4448: if ($reply eq 'unknown_cmd') {
1.716 albertel 4449: # gfall back to way things use to be done
1.715 albertel 4450: return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
4451: $uname);
1.524 raeburn 4452: }
1.715 albertel 4453: return $reply;
4454: }
4455:
4456: sub old_putstore {
1.716 albertel 4457: my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
4458: if (!$udomain) { $udomain=$env{'user.domain'}; }
4459: if (!$uname) { $uname=$env{'user.name'}; }
4460: my $uhome=&homeserver($uname,$udomain);
4461: my %newstorehash;
1.800 albertel 4462: foreach my $item (keys(%$storehash)) {
4463: my $key = $version.':'.&escape($symb).':'.$item;
4464: $newstorehash{$key} = $storehash->{$item};
1.716 albertel 4465: }
4466: my $items='';
4467: my %allitems = ();
1.800 albertel 4468: foreach my $item (keys(%newstorehash)) {
4469: if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716 albertel 4470: my $key = $1.':keys:'.$2;
4471: $allitems{$key} .= $3.':';
4472: }
1.800 albertel 4473: $items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716 albertel 4474: }
1.800 albertel 4475: foreach my $item (keys(%allitems)) {
4476: $allitems{$item} =~ s/\:$//;
4477: $items.= $item.'='.$allitems{$item}.'&';
1.716 albertel 4478: }
4479: $items=~s/\&$//;
4480: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524 raeburn 4481: }
4482:
1.47 www 4483: # ------------------------------------------------------ critical put interface
4484:
4485: sub cput {
1.134 albertel 4486: my ($namespace,$storehash,$udomain,$uname)=@_;
1.620 albertel 4487: if (!$udomain) { $udomain=$env{'user.domain'}; }
4488: if (!$uname) { $uname=$env{'user.name'}; }
1.134 albertel 4489: my $uhome=&homeserver($uname,$udomain);
1.47 www 4490: my $items='';
1.800 albertel 4491: foreach my $item (keys(%$storehash)) {
4492: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191 harris41 4493: }
1.47 www 4494: $items=~s/\&$//;
1.134 albertel 4495: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4496: }
4497:
4498: # -------------------------------------------------------------- eget interface
4499:
4500: sub eget {
1.133 albertel 4501: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 4502: my $items='';
1.800 albertel 4503: foreach my $item (@$storearr) {
4504: $items.=&escape($item).'&';
1.191 harris41 4505: }
1.12 www 4506: $items=~s/\&$//;
1.620 albertel 4507: if (!$udomain) { $udomain=$env{'user.domain'}; }
4508: if (!$uname) { $uname=$env{'user.name'}; }
1.133 albertel 4509: my $uhome=&homeserver($uname,$udomain);
4510: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 4511: my @pairs=split(/\&/,$rep);
4512: my %returnhash=();
1.42 www 4513: my $i=0;
1.800 albertel 4514: foreach my $item (@$storearr) {
4515: $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42 www 4516: $i++;
1.191 harris41 4517: }
1.12 www 4518: return %returnhash;
4519: }
4520:
1.667 albertel 4521: # ------------------------------------------------------------ tmpput interface
4522: sub tmpput {
1.802 raeburn 4523: my ($storehash,$server,$context)=@_;
1.667 albertel 4524: my $items='';
1.800 albertel 4525: foreach my $item (keys(%$storehash)) {
4526: $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667 albertel 4527: }
4528: $items=~s/\&$//;
1.802 raeburn 4529: if (defined($context)) {
4530: $items .= ':'.&escape($context);
4531: }
1.667 albertel 4532: return &reply("tmpput:$items",$server);
4533: }
4534:
4535: # ------------------------------------------------------------ tmpget interface
4536: sub tmpget {
1.688 albertel 4537: my ($token,$server)=@_;
4538: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4539: my $rep=&reply("tmpget:$token",$server);
1.667 albertel 4540: my %returnhash;
4541: foreach my $item (split(/\&/,$rep)) {
4542: my ($key,$value)=split(/=/,$item);
1.951 raeburn 4543: next if ($key =~ /^error: 2 /);
1.667 albertel 4544: $returnhash{&unescape($key)}=&thaw_unescape($value);
4545: }
4546: return %returnhash;
4547: }
4548:
1.688 albertel 4549: # ------------------------------------------------------------ tmpget interface
4550: sub tmpdel {
4551: my ($token,$server)=@_;
4552: if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
4553: return &reply("tmpdel:$token",$server);
4554: }
4555:
1.765 albertel 4556: # -------------------------------------------------- portfolio access checking
4557:
4558: sub portfolio_access {
1.766 albertel 4559: my ($requrl) = @_;
1.765 albertel 4560: my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
4561: my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814 raeburn 4562: if ($result) {
4563: my %setters;
4564: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4565: my ($startblock,$endblock) =
4566: &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
4567: if ($startblock && $endblock) {
4568: return 'B';
4569: }
4570: } else {
4571: my ($startblock,$endblock) =
4572: &Apache::loncommon::blockcheck(\%setters,'port');
4573: if ($startblock && $endblock) {
4574: return 'B';
4575: }
4576: }
4577: }
1.765 albertel 4578: if ($result eq 'ok') {
1.766 albertel 4579: return 'F';
1.765 albertel 4580: } elsif ($result =~ /^[^:]+:guest_/) {
1.766 albertel 4581: return 'A';
1.765 albertel 4582: }
1.766 albertel 4583: return '';
1.765 albertel 4584: }
4585:
4586: sub get_portfolio_access {
1.767 albertel 4587: my ($udom,$unum,$file_name,$group,$access_hash) = @_;
4588:
4589: if (!ref($access_hash)) {
4590: my $current_perms = &get_portfile_permissions($udom,$unum);
4591: my %access_controls = &get_access_controls($current_perms,$group,
4592: $file_name);
4593: $access_hash = $access_controls{$file_name};
4594: }
4595:
1.765 albertel 4596: my ($public,$guest,@domains,@users,@courses,@groups);
4597: my $now = time;
4598: if (ref($access_hash) eq 'HASH') {
4599: foreach my $key (keys(%{$access_hash})) {
4600: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
4601: if ($start > $now) {
4602: next;
4603: }
4604: if ($end && $end<$now) {
4605: next;
4606: }
4607: if ($scope eq 'public') {
4608: $public = $key;
4609: last;
4610: } elsif ($scope eq 'guest') {
4611: $guest = $key;
4612: } elsif ($scope eq 'domains') {
4613: push(@domains,$key);
4614: } elsif ($scope eq 'users') {
4615: push(@users,$key);
4616: } elsif ($scope eq 'course') {
4617: push(@courses,$key);
4618: } elsif ($scope eq 'group') {
4619: push(@groups,$key);
4620: }
4621: }
4622: if ($public) {
4623: return 'ok';
4624: }
4625: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
4626: if ($guest) {
4627: return $guest;
4628: }
4629: } else {
4630: if (@domains > 0) {
4631: foreach my $domkey (@domains) {
4632: if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
4633: if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
4634: return 'ok';
4635: }
4636: }
4637: }
4638: }
4639: if (@users > 0) {
4640: foreach my $userkey (@users) {
1.865 raeburn 4641: if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
4642: foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
4643: if (ref($item) eq 'HASH') {
4644: if (($item->{'uname'} eq $env{'user.name'}) &&
4645: ($item->{'udom'} eq $env{'user.domain'})) {
4646: return 'ok';
4647: }
4648: }
4649: }
4650: }
1.765 albertel 4651: }
4652: }
4653: my %roleshash;
4654: my @courses_and_groups = @courses;
4655: push(@courses_and_groups,@groups);
4656: if (@courses_and_groups > 0) {
4657: my (%allgroups,%allroles);
4658: my ($start,$end,$role,$sec,$group);
4659: foreach my $envkey (%env) {
1.1060 raeburn 4660: if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 4661: my $cid = $2.'_'.$3;
4662: if ($1 eq 'gr') {
4663: $group = $4;
4664: $allgroups{$cid}{$group} = $env{$envkey};
4665: } else {
4666: if ($4 eq '') {
4667: $sec = 'none';
4668: } else {
4669: $sec = $4;
4670: }
4671: $allroles{$cid}{$1}{$sec} = $env{$envkey};
4672: }
1.811 albertel 4673: } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765 albertel 4674: my $cid = $2.'_'.$3;
4675: if ($4 eq '') {
4676: $sec = 'none';
4677: } else {
4678: $sec = $4;
4679: }
4680: $allroles{$cid}{$1}{$sec} = $env{$envkey};
4681: }
4682: }
4683: if (keys(%allroles) == 0) {
4684: return;
4685: }
4686: foreach my $key (@courses_and_groups) {
4687: my %content = %{$$access_hash{$key}};
4688: my $cnum = $content{'number'};
4689: my $cdom = $content{'domain'};
4690: my $cid = $cdom.'_'.$cnum;
4691: if (!exists($allroles{$cid})) {
4692: next;
4693: }
4694: foreach my $role_id (keys(%{$content{'roles'}})) {
4695: my @sections = @{$content{'roles'}{$role_id}{'section'}};
4696: my @groups = @{$content{'roles'}{$role_id}{'group'}};
4697: my @status = @{$content{'roles'}{$role_id}{'access'}};
4698: my @roles = @{$content{'roles'}{$role_id}{'role'}};
4699: foreach my $role (keys(%{$allroles{$cid}})) {
4700: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
4701: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
4702: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
4703: if (grep/^all$/,@sections) {
4704: return 'ok';
4705: } else {
4706: if (grep/^$sec$/,@sections) {
4707: return 'ok';
4708: }
4709: }
4710: }
4711: }
4712: if (keys(%{$allgroups{$cid}}) == 0) {
4713: if (grep/^none$/,@groups) {
4714: return 'ok';
4715: }
4716: } else {
4717: if (grep/^all$/,@groups) {
4718: return 'ok';
4719: }
4720: foreach my $group (keys(%{$allgroups{$cid}})) {
4721: if (grep/^$group$/,@groups) {
4722: return 'ok';
4723: }
4724: }
4725: }
4726: }
4727: }
4728: }
4729: }
4730: }
4731: if ($guest) {
4732: return $guest;
4733: }
4734: }
4735: }
4736: return;
4737: }
4738:
4739: sub course_group_datechecker {
4740: my ($dates,$now,$status) = @_;
4741: my ($start,$end) = split(/\./,$dates);
4742: if (!$start && !$end) {
4743: return 'ok';
4744: }
4745: if (grep/^active$/,@{$status}) {
4746: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
4747: return 'ok';
4748: }
4749: }
4750: if (grep/^previous$/,@{$status}) {
4751: if ($end > $now ) {
4752: return 'ok';
4753: }
4754: }
4755: if (grep/^future$/,@{$status}) {
4756: if ($start > $now) {
4757: return 'ok';
4758: }
4759: }
4760: return;
4761: }
4762:
4763: sub parse_portfolio_url {
4764: my ($url) = @_;
4765:
4766: my ($type,$udom,$unum,$group,$file_name);
4767:
1.823 albertel 4768: if ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765 albertel 4769: $type = 1;
4770: $udom = $1;
4771: $unum = $2;
4772: $file_name = $3;
1.823 albertel 4773: } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765 albertel 4774: $type = 2;
4775: $udom = $1;
4776: $unum = $2;
4777: $group = $3;
4778: $file_name = $3.'/'.$4;
4779: }
4780: if (wantarray) {
4781: return ($type,$udom,$unum,$file_name,$group);
4782: }
4783: return $type;
4784: }
4785:
4786: sub is_portfolio_url {
4787: my ($url) = @_;
4788: return scalar(&parse_portfolio_url($url));
4789: }
4790:
1.798 raeburn 4791: sub is_portfolio_file {
4792: my ($file) = @_;
1.820 raeburn 4793: if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798 raeburn 4794: return 1;
4795: }
4796: return;
4797: }
4798:
1.976 raeburn 4799: sub usertools_access {
1.985 raeburn 4800: my ($uname,$udom,$tool,$action,$context) = @_;
4801: my ($access,%tools);
4802: if ($context eq '') {
4803: $context = 'tools';
4804: }
4805: if ($context eq 'requestcourses') {
4806: %tools = (
4807: official => 1,
4808: unofficial => 1,
1.1006 raeburn 4809: community => 1,
1.985 raeburn 4810: );
4811: } else {
4812: %tools = (
4813: aboutme => 1,
4814: blog => 1,
4815: portfolio => 1,
4816: );
4817: }
1.976 raeburn 4818: return if (!defined($tools{$tool}));
4819:
4820: if ((!defined($udom)) || (!defined($uname))) {
4821: $udom = $env{'user.domain'};
4822: $uname = $env{'user.name'};
4823: }
4824:
1.978 raeburn 4825: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
4826: if ($action ne 'reload') {
1.985 raeburn 4827: if ($context eq 'requestcourses') {
4828: return $env{'environment.canrequest.'.$tool};
4829: } else {
4830: return $env{'environment.availabletools.'.$tool};
4831: }
4832: }
1.976 raeburn 4833: }
4834:
4835: my ($toolstatus,$inststatus);
4836:
1.985 raeburn 4837: if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
4838: ($action ne 'reload')) {
4839: $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976 raeburn 4840: $inststatus = $env{'environment.inststatus'};
4841: } else {
1.1025 raeburn 4842: my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
1.985 raeburn 4843: $toolstatus = $userenv{$context.'.'.$tool};
1.976 raeburn 4844: $inststatus = $userenv{'inststatus'};
4845: }
4846:
4847: if ($toolstatus ne '') {
4848: if ($toolstatus) {
4849: $access = 1;
4850: } else {
4851: $access = 0;
4852: }
4853: return $access;
4854: }
4855:
4856: my $is_adv = &is_advanced_user($udom,$uname);
4857: my %domdef = &get_domain_defaults($udom);
4858: if (ref($domdef{$tool}) eq 'HASH') {
4859: if ($is_adv) {
4860: if ($domdef{$tool}{'_LC_adv'} ne '') {
4861: if ($domdef{$tool}{'_LC_adv'}) {
4862: $access = 1;
4863: } else {
4864: $access = 0;
4865: }
4866: return $access;
4867: }
4868: }
4869: if ($inststatus ne '') {
4870: my ($hasaccess,$hasnoaccess);
4871: foreach my $affiliation (split(/:/,$inststatus)) {
4872: if ($domdef{$tool}{$affiliation} ne '') {
4873: if ($domdef{$tool}{$affiliation}) {
4874: $hasaccess = 1;
4875: } else {
4876: $hasnoaccess = 1;
4877: }
4878: }
4879: }
4880: if ($hasaccess || $hasnoaccess) {
4881: if ($hasaccess) {
4882: $access = 1;
4883: } elsif ($hasnoaccess) {
4884: $access = 0;
4885: }
4886: return $access;
4887: }
4888: } else {
4889: if ($domdef{$tool}{'default'} ne '') {
4890: if ($domdef{$tool}{'default'}) {
4891: $access = 1;
4892: } elsif ($domdef{$tool}{'default'} == 0) {
4893: $access = 0;
4894: }
4895: return $access;
4896: }
4897: }
4898: } else {
1.985 raeburn 4899: if ($context eq 'tools') {
4900: $access = 1;
4901: } else {
4902: $access = 0;
4903: }
1.976 raeburn 4904: return $access;
4905: }
4906: }
4907:
1.1050 raeburn 4908: sub is_course_owner {
4909: my ($cdom,$cnum,$udom,$uname) = @_;
4910: if (($udom eq '') || ($uname eq '')) {
4911: $udom = $env{'user.domain'};
4912: $uname = $env{'user.name'};
4913: }
4914: unless (($udom eq '') || ($uname eq '')) {
4915: if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
4916: if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
4917: return 1;
4918: } else {
4919: my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
4920: if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
4921: return 1;
4922: }
4923: }
4924: }
4925: }
4926: return;
4927: }
4928:
1.976 raeburn 4929: sub is_advanced_user {
4930: my ($udom,$uname) = @_;
4931: my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
4932: my %allroles;
4933: my $is_adv;
4934: foreach my $role (keys(%roleshash)) {
4935: my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
4936: my $area = '/'.$tdomain.'/'.$trest;
4937: if ($sec ne '') {
4938: $area .= '/'.$sec;
4939: }
4940: if (($area ne '') && ($trole ne '')) {
4941: my $spec=$trole.'.'.$area;
4942: if ($trole =~ /^cr\//) {
4943: &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
4944: } elsif ($trole ne 'gr') {
4945: &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
4946: }
4947: }
4948: }
4949: foreach my $role (keys(%allroles)) {
4950: last if ($is_adv);
4951: foreach my $item (split(/:/,$allroles{$role})) {
4952: if ($item ne '') {
4953: my ($privilege,$restrictions)=split(/&/,$item);
4954: if ($privilege eq 'adv') {
4955: $is_adv = 1;
4956: last;
4957: }
4958: }
4959: }
4960: }
4961: return $is_adv;
4962: }
1.798 raeburn 4963:
1.1035 raeburn 4964: sub check_can_request {
1.1036 raeburn 4965: my ($dom,$can_request,$request_domains) = @_;
1.1035 raeburn 4966: my $canreq = 0;
4967: my ($types,$typename) = &Apache::loncommon::course_types();
4968: my @options = ('approval','validate','autolimit');
4969: my $optregex = join('|',@options);
4970: if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
4971: foreach my $type (@{$types}) {
4972: if (&usertools_access($env{'user.name'},
4973: $env{'user.domain'},
4974: $type,undef,'requestcourses')) {
4975: $canreq ++;
1.1036 raeburn 4976: if (ref($request_domains) eq 'HASH') {
4977: push(@{$request_domains->{$type}},$env{'user.domain'});
4978: }
1.1035 raeburn 4979: if ($dom eq $env{'user.domain'}) {
4980: $can_request->{$type} = 1;
4981: }
4982: }
4983: if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
4984: my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
4985: if (@curr > 0) {
1.1036 raeburn 4986: foreach my $item (@curr) {
4987: if (ref($request_domains) eq 'HASH') {
4988: my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
4989: if ($otherdom ne '') {
4990: if (ref($request_domains->{$type}) eq 'ARRAY') {
4991: unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
4992: push(@{$request_domains->{$type}},$otherdom);
4993: }
4994: } else {
4995: push(@{$request_domains->{$type}},$otherdom);
4996: }
4997: }
4998: }
4999: }
5000: unless($dom eq $env{'user.domain'}) {
5001: $canreq ++;
1.1035 raeburn 5002: if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
5003: $can_request->{$type} = 1;
5004: }
5005: }
5006: }
5007: }
5008: }
5009: }
5010: return $canreq;
5011: }
5012:
1.341 www 5013: # ---------------------------------------------- Custom access rule evaluation
5014:
5015: sub customaccess {
5016: my ($priv,$uri)=@_;
1.807 albertel 5017: my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819 www 5018: my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807 albertel 5019: $udom = &LONCAPA::clean_domain($udom);
5020: $ucrs = &LONCAPA::clean_username($ucrs);
1.341 www 5021: my $access=0;
1.800 albertel 5022: foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893 albertel 5023: my ($effect,$realm,$role,$type)=split(/\:/,$right);
5024: if ($type eq 'user') {
5025: foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896 albertel 5026: my ($tdom,$tuname)=split(m{/},$scope);
1.893 albertel 5027: if ($tdom) {
5028: if ($tdom ne $env{'user.domain'}) { next; }
5029: }
1.896 albertel 5030: if ($tuname) {
5031: if ($tuname ne $env{'user.name'}) { next; }
1.893 albertel 5032: }
5033: $access=($effect eq 'allow');
5034: last;
5035: }
5036: } else {
5037: if ($role) {
5038: if ($role ne $urole) { next; }
5039: }
5040: foreach my $scope (split(/\s*\,\s*/,$realm)) {
5041: my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
5042: if ($tdom) {
5043: if ($tdom ne $udom) { next; }
5044: }
5045: if ($tcrs) {
5046: if ($tcrs ne $ucrs) { next; }
5047: }
5048: if ($tsec) {
5049: if ($tsec ne $usec) { next; }
5050: }
5051: $access=($effect eq 'allow');
5052: last;
5053: }
5054: if ($realm eq '' && $role eq '') {
5055: $access=($effect eq 'allow');
5056: }
1.402 bowersj2 5057: }
1.341 www 5058: }
5059: return $access;
5060: }
5061:
1.103 harris41 5062: # ------------------------------------------------- Check for a user privilege
1.12 www 5063:
5064: sub allowed {
1.810 raeburn 5065: my ($priv,$uri,$symb,$role)=@_;
1.705 albertel 5066: my $ver_orguri=$uri;
1.439 www 5067: $uri=&deversion($uri);
1.152 www 5068: my $orguri=$uri;
1.52 www 5069: $uri=&declutter($uri);
1.809 raeburn 5070:
1.810 raeburn 5071: if ($priv eq 'evb') {
5072: # Evade communication block restrictions for specified role in a course
5073: if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
5074: return $1;
5075: } else {
5076: return;
5077: }
5078: }
5079:
1.620 albertel 5080: if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54 www 5081: # Free bre access to adm and meta resources
1.775 albertel 5082: if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$}))
1.769 albertel 5083: || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) ))
5084: && ($priv eq 'bre')) {
1.14 www 5085: return 'F';
1.159 www 5086: }
5087:
1.545 banghart 5088: # Free bre access to user's own portfolio contents
1.714 raeburn 5089: my ($space,$domain,$name,@dir)=split('/',$uri);
1.647 raeburn 5090: if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) &&
1.714 raeburn 5091: ($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814 raeburn 5092: my %setters;
5093: my ($startblock,$endblock) =
5094: &Apache::loncommon::blockcheck(\%setters,'port');
5095: if ($startblock && $endblock) {
5096: return 'B';
5097: } else {
5098: return 'F';
5099: }
1.545 banghart 5100: }
5101:
1.762 raeburn 5102: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714 raeburn 5103: if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups')
5104: && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
5105: if (exists($env{'request.course.id'})) {
5106: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
5107: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
5108: if (($domain eq $cdom) && ($name eq $cnum)) {
5109: my $courseprivid=$env{'request.course.id'};
5110: $courseprivid=~s/\_/\//;
5111: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
5112: .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
5113: return $1;
1.762 raeburn 5114: } else {
5115: if ($env{'request.course.sec'}) {
5116: $courseprivid.='/'.$env{'request.course.sec'};
5117: }
5118: if ($env{'user.priv.'.$env{'request.role'}.'./'.
5119: $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
5120: return $2;
5121: }
1.714 raeburn 5122: }
5123: }
5124: }
5125: }
5126:
1.159 www 5127: # Free bre to public access
5128:
5129: if ($priv eq 'bre') {
1.238 www 5130: my $copyright=&metadata($uri,'copyright');
1.620 albertel 5131: if (($copyright eq 'public') && (!$env{'request.course.id'})) {
1.301 www 5132: return 'F';
5133: }
1.238 www 5134: if ($copyright eq 'priv') {
5135: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5136: unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238 www 5137: return '';
5138: }
5139: }
5140: if ($copyright eq 'domain') {
5141: $uri=~/([^\/]+)\/([^\/]+)\//;
1.620 albertel 5142: unless (($env{'user.domain'} eq $1) ||
5143: ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238 www 5144: return '';
5145: }
1.262 matthew 5146: }
1.620 albertel 5147: if ($env{'request.role'}=~ /li\.\//) {
1.262 matthew 5148: # Library role, so allow browsing of resources in this domain.
5149: return 'F';
1.238 www 5150: }
1.341 www 5151: if ($copyright eq 'custom') {
5152: unless (&customaccess($priv,$uri)) { return ''; }
5153: }
1.14 www 5154: }
1.264 matthew 5155: # Domain coordinator is trying to create a course
1.620 albertel 5156: if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264 matthew 5157: # uri is the requested domain in this case.
5158: # comparison to 'request.role.domain' shows if the user has selected
1.678 raeburn 5159: # a role of dc for the domain in question.
1.620 albertel 5160: return 'F' if ($uri eq $env{'request.role.domain'});
1.264 matthew 5161: }
1.29 www 5162:
1.52 www 5163: my $thisallowed='';
5164: my $statecond=0;
5165: my $courseprivid='';
5166:
1.1039 raeburn 5167: my $ownaccess;
1.1043 raeburn 5168: # Community Coordinator or Assistant Co-author browsing resource space.
1.1039 raeburn 5169: if (($priv eq 'bro') && ($env{'user.author'})) {
5170: if ($uri eq '') {
5171: $ownaccess = 1;
5172: } else {
5173: if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
5174: my $udom = $env{'user.domain'};
5175: my $uname = $env{'user.name'};
5176: if ($uri =~ m{^\Q$udom\E/?$}) {
5177: $ownaccess = 1;
1.1040 raeburn 5178: } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039 raeburn 5179: unless ($uri =~ m{\.\./}) {
5180: $ownaccess = 1;
5181: }
5182: } elsif (($udom ne 'public') && ($uname ne 'public')) {
5183: my $now = time;
5184: if ($uri =~ m{^([^/]+)/?$}) {
5185: my $adom = $1;
5186: foreach my $key (keys(%env)) {
1.1042 raeburn 5187: if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039 raeburn 5188: my ($start,$end) = split('.',$env{$key});
5189: if (($now >= $start) && (!$end || $end < $now)) {
5190: $ownaccess = 1;
5191: last;
5192: }
5193: }
5194: }
5195: } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
5196: my $adom = $1;
5197: my $aname = $2;
1.1042 raeburn 5198: foreach my $role ('ca','aa') {
5199: if ($env{"user.role.$role./$adom/$aname"}) {
5200: my ($start,$end) =
5201: split('.',$env{"user.role.$role./$adom/$aname"});
5202: if (($now >= $start) && (!$end || $end < $now)) {
5203: $ownaccess = 1;
5204: last;
5205: }
1.1039 raeburn 5206: }
5207: }
5208: }
5209: }
5210: }
5211: }
5212: }
5213:
1.52 www 5214: # Course
5215:
1.620 albertel 5216: if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5217: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5218: $thisallowed.=$1;
5219: }
1.52 www 5220: }
1.29 www 5221:
1.52 www 5222: # Domain
5223:
1.620 albertel 5224: if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479 albertel 5225: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5226: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5227: $thisallowed.=$1;
5228: }
1.12 www 5229: }
1.52 www 5230:
5231: # Course: uri itself is a course
1.66 www 5232: my $courseuri=$uri;
5233: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 5234: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 5235:
1.620 albertel 5236: if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479 albertel 5237: =~/\Q$priv\E\&([^\:]*)/) {
1.1043 raeburn 5238: unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039 raeburn 5239: $thisallowed.=$1;
5240: }
1.12 www 5241: }
1.29 www 5242:
1.665 albertel 5243: # URI is an uploaded document for this course, default permissions don't matter
1.611 albertel 5244: # not allowing 'edit' access (editupload) to uploaded course docs
1.492 albertel 5245: if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665 albertel 5246: $thisallowed='';
1.671 raeburn 5247: my ($match)=&is_on_map($uri);
5248: if ($match) {
5249: if ($env{'user.priv.'.$env{'request.role'}.'./'}
5250: =~/\Q$priv\E\&([^\:]*)/) {
5251: $thisallowed.=$1;
5252: }
5253: } else {
1.705 albertel 5254: my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671 raeburn 5255: if ($refuri) {
5256: if ($refuri =~ m|^/adm/|) {
1.669 raeburn 5257: $thisallowed='F';
1.671 raeburn 5258: } else {
5259: $refuri=&declutter($refuri);
5260: my ($match) = &is_on_map($refuri);
5261: if ($match) {
5262: $thisallowed='F';
5263: }
1.669 raeburn 5264: }
1.671 raeburn 5265: }
5266: }
1.314 www 5267: }
1.492 albertel 5268:
1.766 albertel 5269: if ($priv eq 'bre'
5270: && $thisallowed ne 'F'
5271: && $thisallowed ne '2'
5272: && &is_portfolio_url($uri)) {
5273: $thisallowed = &portfolio_access($uri);
5274: }
5275:
1.52 www 5276: # Full access at system, domain or course-wide level? Exit.
1.29 www 5277: if ($thisallowed=~/F/) {
5278: return 'F';
5279: }
5280:
1.52 www 5281: # If this is generating or modifying users, exit with special codes
1.29 www 5282:
1.643 www 5283: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
5284: if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642 albertel 5285: my ($audom,$auname)=split('/',$uri);
1.643 www 5286: # no author name given, so this just checks on the general right to make a co-author in this domain
5287: unless ($auname) { return $thisallowed; }
5288: # an author name is given, so we are about to actually make a co-author for a certain account
1.642 albertel 5289: if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
5290: (($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
5291: ($audom ne $env{'request.role.domain'}))) { return ''; }
5292: }
1.52 www 5293: return $thisallowed;
5294: }
5295: #
1.103 harris41 5296: # Gathered so far: system, domain and course wide privileges
1.52 www 5297: #
5298: # Course: See if uri or referer is an individual resource that is part of
5299: # the course
5300:
1.620 albertel 5301: if ($env{'request.course.id'}) {
1.232 www 5302:
1.620 albertel 5303: $courseprivid=$env{'request.course.id'};
5304: if ($env{'request.course.sec'}) {
5305: $courseprivid.='/'.$env{'request.course.sec'};
1.52 www 5306: }
5307: $courseprivid=~s/\_/\//;
5308: my $checkreferer=1;
1.232 www 5309: my ($match,$cond)=&is_on_map($uri);
5310: if ($match) {
5311: $statecond=$cond;
1.620 albertel 5312: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5313: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5314: $thisallowed.=$1;
5315: $checkreferer=0;
5316: }
1.29 www 5317: }
1.83 www 5318:
1.148 www 5319: if ($checkreferer) {
1.620 albertel 5320: my $refuri=$env{'httpref.'.$orguri};
1.148 www 5321: unless ($refuri) {
1.800 albertel 5322: foreach my $key (keys(%env)) {
5323: if ($key=~/^httpref\..*\*/) {
5324: my $pattern=$key;
1.156 www 5325: $pattern=~s/^httpref\.\/res\///;
1.148 www 5326: $pattern=~s/\*/\[\^\/\]\+/g;
5327: $pattern=~s/\//\\\//g;
1.152 www 5328: if ($orguri=~/$pattern/) {
1.800 albertel 5329: $refuri=$env{$key};
1.148 www 5330: }
5331: }
1.191 harris41 5332: }
1.148 www 5333: }
1.232 www 5334:
1.148 www 5335: if ($refuri) {
1.152 www 5336: $refuri=&declutter($refuri);
1.232 www 5337: my ($match,$cond)=&is_on_map($refuri);
5338: if ($match) {
5339: my $refstatecond=$cond;
1.620 albertel 5340: if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479 albertel 5341: =~/\Q$priv\E\&([^\:]*)/) {
1.52 www 5342: $thisallowed.=$1;
1.53 www 5343: $uri=$refuri;
5344: $statecond=$refstatecond;
1.52 www 5345: }
5346: }
1.148 www 5347: }
1.29 www 5348: }
1.52 www 5349: }
1.29 www 5350:
1.52 www 5351: #
1.103 harris41 5352: # Gathered now: all privileges that could apply, and condition number
1.52 www 5353: #
5354: #
5355: # Full or no access?
5356: #
1.29 www 5357:
1.52 www 5358: if ($thisallowed=~/F/) {
5359: return 'F';
5360: }
1.29 www 5361:
1.52 www 5362: unless ($thisallowed) {
5363: return '';
5364: }
1.29 www 5365:
1.52 www 5366: # Restrictions exist, deal with them
5367: #
5368: # C:according to course preferences
5369: # R:according to resource settings
5370: # L:unless locked
5371: # X:according to user session state
5372: #
5373:
5374: # Possibly locked functionality, check all courses
1.54 www 5375: # Locks might take effect only after 10 minutes cache expiration for other
5376: # courses, and 2 minutes for current course
1.52 www 5377:
5378: my $envkey;
5379: if ($thisallowed=~/L/) {
1.1000 raeburn 5380: foreach $envkey (keys(%env)) {
1.54 www 5381: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
5382: my $courseid=$2;
5383: my $roleid=$1.'.'.$2;
1.92 www 5384: $courseid=~s/^\///;
1.54 www 5385: my $expiretime=600;
1.620 albertel 5386: if ($env{'request.role'} eq $roleid) {
1.54 www 5387: $expiretime=120;
5388: }
5389: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
5390: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620 albertel 5391: if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731 albertel 5392: &coursedescription($courseid,{'freshen_cache' => 1});
1.54 www 5393: }
1.620 albertel 5394: if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
5395: || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
5396: if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
5397: &log($env{'user.domain'},$env{'user.name'},
5398: $env{'user.home'},
1.57 www 5399: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 5400: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5401: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5402: return '';
5403: }
5404: }
1.620 albertel 5405: if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
5406: || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
5407: if ($env{'priv.'.$priv.'.lock.expire'}>time) {
5408: &log($env{'user.domain'},$env{'user.name'},
5409: $env{'user.home'},
1.57 www 5410: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 5411: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620 albertel 5412: $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 5413: return '';
5414: }
5415: }
5416: }
1.29 www 5417: }
1.52 www 5418: }
5419:
5420: #
5421: # Rest of the restrictions depend on selected course
5422: #
5423:
1.620 albertel 5424: unless ($env{'request.course.id'}) {
1.766 albertel 5425: if ($thisallowed eq 'A') {
5426: return 'A';
1.814 raeburn 5427: } elsif ($thisallowed eq 'B') {
5428: return 'B';
1.766 albertel 5429: } else {
5430: return '1';
5431: }
1.52 www 5432: }
1.29 www 5433:
1.52 www 5434: #
5435: # Now user is definitely in a course
5436: #
1.53 www 5437:
5438:
5439: # Course preferences
5440:
5441: if ($thisallowed=~/C/) {
1.620 albertel 5442: my $rolecode=(split(/\./,$env{'request.role'}))[0];
5443: my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
5444: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479 albertel 5445: =~/\Q$rolecode\E/) {
1.689 albertel 5446: if ($priv ne 'pch') {
5447: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5448: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
5449: $env{'request.course.id'});
5450: }
1.237 www 5451: return '';
5452: }
5453:
1.620 albertel 5454: if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479 albertel 5455: =~/\Q$unamedom\E/) {
1.689 albertel 5456: if ($priv ne 'pch') {
5457: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
5458: 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
5459: $env{'request.course.id'});
5460: }
1.54 www 5461: return '';
5462: }
1.53 www 5463: }
5464:
5465: # Resource preferences
5466:
5467: if ($thisallowed=~/R/) {
1.620 albertel 5468: my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479 albertel 5469: if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689 albertel 5470: if ($priv ne 'pch') {
5471: &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
5472: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
5473: }
5474: return '';
1.54 www 5475: }
1.53 www 5476: }
1.30 www 5477:
1.246 www 5478: # Restricted by state or randomout?
1.30 www 5479:
1.52 www 5480: if ($thisallowed=~/X/) {
1.620 albertel 5481: if ($env{'acc.randomout'}) {
1.579 albertel 5482: if (!$symb) { $symb=&symbread($uri,1); }
1.620 albertel 5483: if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) {
1.248 www 5484: return '';
5485: }
1.247 www 5486: }
5487: if (&condval($statecond)) {
1.52 www 5488: return '2';
5489: } else {
5490: return '';
5491: }
5492: }
1.30 www 5493:
1.766 albertel 5494: if ($thisallowed eq 'A') {
5495: return 'A';
1.814 raeburn 5496: } elsif ($thisallowed eq 'B') {
5497: return 'B';
1.766 albertel 5498: }
1.52 www 5499: return 'F';
1.232 www 5500: }
5501:
1.710 albertel 5502: sub split_uri_for_cond {
5503: my $uri=&deversion(&declutter(shift));
5504: my @uriparts=split(/\//,$uri);
5505: my $filename=pop(@uriparts);
5506: my $pathname=join('/',@uriparts);
5507: return ($pathname,$filename);
5508: }
1.232 www 5509: # --------------------------------------------------- Is a resource on the map?
5510:
5511: sub is_on_map {
1.710 albertel 5512: my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289 bowersj2 5513: #Trying to find the conditional for the file
1.620 albertel 5514: my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289 bowersj2 5515: /\&\Q$filename\E\:([\d\|]+)\&/);
1.232 www 5516: if ($match) {
1.289 bowersj2 5517: return (1,$1);
5518: } else {
1.434 www 5519: return (0,0);
1.289 bowersj2 5520: }
1.12 www 5521: }
5522:
1.427 www 5523: # --------------------------------------------------------- Get symb from alias
5524:
5525: sub get_symb_from_alias {
5526: my $symb=shift;
5527: my ($map,$resid,$url)=&decode_symb($symb);
5528: # Already is a symb
5529: if ($url) { return $symb; }
5530: # Must be an alias
5531: my $aliassymb='';
5532: my %bighash;
1.620 albertel 5533: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427 www 5534: &GDBM_READER(),0640)) {
5535: my $rid=$bighash{'mapalias_'.$symb};
5536: if ($rid) {
5537: my ($mapid,$resid)=split(/\./,$rid);
1.429 albertel 5538: $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
5539: $resid,$bighash{'src_'.$rid});
1.427 www 5540: }
5541: untie %bighash;
5542: }
5543: return $aliassymb;
5544: }
5545:
1.12 www 5546: # ----------------------------------------------------------------- Define Role
5547:
5548: sub definerole {
5549: if (allowed('mcr','/')) {
5550: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800 albertel 5551: foreach my $role (split(':',$sysrole)) {
5552: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5553: if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
5554: if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
5555: if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5556: return "refused:s:$crole&$cqual";
5557: }
5558: }
1.191 harris41 5559: }
1.800 albertel 5560: foreach my $role (split(':',$domrole)) {
5561: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5562: if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
5563: if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
5564: if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) {
1.21 www 5565: return "refused:d:$crole&$cqual";
5566: }
5567: }
1.191 harris41 5568: }
1.800 albertel 5569: foreach my $role (split(':',$courole)) {
5570: my ($crole,$cqual)=split(/\&/,$role);
1.479 albertel 5571: if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
5572: if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
5573: if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) {
1.21 www 5574: return "refused:c:$crole&$cqual";
5575: }
5576: }
1.191 harris41 5577: }
1.620 albertel 5578: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
5579: "$env{'user.domain'}:$env{'user.name'}:".
1.21 www 5580: "rolesdef_$rolename=".
5581: escape($sysrole.'_'.$domrole.'_'.$courole);
1.620 albertel 5582: return reply($command,$env{'user.home'});
1.12 www 5583: } else {
5584: return 'refused';
5585: }
1.105 harris41 5586: }
5587:
5588: # ---------------- Make a metadata query against the network of library servers
5589:
5590: sub metadata_query {
1.244 matthew 5591: my ($query,$custom,$customshow,$server_array)=@_;
1.120 harris41 5592: my %rhash;
1.845 albertel 5593: my %libserv = &all_library();
1.244 matthew 5594: my @server_list = (defined($server_array) ? @$server_array
5595: : keys(%libserv) );
5596: for my $server (@server_list) {
1.118 harris41 5597: unless ($custom or $customshow) {
5598: my $reply=&reply("querysend:".&escape($query),$server);
5599: $rhash{$server}=$reply;
5600: }
5601: else {
5602: my $reply=&reply("querysend:".&escape($query).':'.
5603: &escape($custom).':'.&escape($customshow),
5604: $server);
5605: $rhash{$server}=$reply;
5606: }
1.112 harris41 5607: }
1.118 harris41 5608: return \%rhash;
1.240 www 5609: }
5610:
5611: # ----------------------------------------- Send log queries and wait for reply
5612:
5613: sub log_query {
5614: my ($uname,$udom,$query,%filters)=@_;
5615: my $uhome=&homeserver($uname,$udom);
5616: if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838 albertel 5617: my $uhost=&hostname($uhome);
1.800 albertel 5618: my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240 www 5619: my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
5620: $uhome);
1.479 albertel 5621: unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242 www 5622: return get_query_reply($queryid);
5623: }
5624:
1.818 raeburn 5625: # -------------------------- Update MySQL table for portfolio file
5626:
5627: sub update_portfolio_table {
1.821 raeburn 5628: my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970 raeburn 5629: if ($group ne '') {
5630: $file_name =~s /^\Q$group\E//;
5631: }
1.818 raeburn 5632: my $homeserver = &homeserver($uname,$udom);
5633: my $queryid=
1.821 raeburn 5634: &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
5635: ':'.&escape($file_name).':'.$action,$homeserver);
1.818 raeburn 5636: my $reply = &get_query_reply($queryid);
5637: return $reply;
5638: }
5639:
1.899 raeburn 5640: # -------------------------- Update MySQL allusers table
5641:
5642: sub update_allusers_table {
5643: my ($uname,$udom,$names) = @_;
5644: my $homeserver = &homeserver($uname,$udom);
5645: my $queryid=
5646: &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
5647: 'lastname='.&escape($names->{'lastname'}).'%%'.
5648: 'firstname='.&escape($names->{'firstname'}).'%%'.
5649: 'middlename='.&escape($names->{'middlename'}).'%%'.
5650: 'generation='.&escape($names->{'generation'}).'%%'.
5651: 'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
5652: 'id='.&escape($names->{'id'}),$homeserver);
5653: my $reply = &get_query_reply($queryid);
5654: return $reply;
5655: }
5656:
1.508 raeburn 5657: # ------- Request retrieval of institutional classlists for course(s)
1.506 raeburn 5658:
5659: sub fetch_enrollment_query {
1.511 raeburn 5660: my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508 raeburn 5661: my $homeserver;
1.547 raeburn 5662: my $maxtries = 1;
1.508 raeburn 5663: if ($context eq 'automated') {
5664: $homeserver = $perlvar{'lonHostID'};
1.547 raeburn 5665: $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508 raeburn 5666: } else {
5667: $homeserver = &homeserver($cnum,$dom);
5668: }
1.838 albertel 5669: my $host=&hostname($homeserver);
1.506 raeburn 5670: my $cmd = '';
1.1000 raeburn 5671: foreach my $affiliate (keys(%{$affiliatesref})) {
1.800 albertel 5672: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506 raeburn 5673: }
5674: $cmd =~ s/%%$//;
5675: $cmd = &escape($cmd);
5676: my $query = 'fetchenrollment';
1.620 albertel 5677: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526 raeburn 5678: unless ($queryid=~/^\Q$host\E\_/) {
5679: &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum);
5680: return 'error: '.$queryid;
5681: }
1.506 raeburn 5682: my $reply = &get_query_reply($queryid);
1.547 raeburn 5683: my $tries = 1;
5684: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
5685: $reply = &get_query_reply($queryid);
5686: $tries ++;
5687: }
1.526 raeburn 5688: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620 albertel 5689: &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526 raeburn 5690: } else {
1.901 albertel 5691: my @responses = split(/:/,$reply);
1.515 raeburn 5692: if ($homeserver eq $perlvar{'lonHostID'}) {
1.800 albertel 5693: foreach my $line (@responses) {
5694: my ($key,$value) = split(/=/,$line,2);
1.515 raeburn 5695: $$replyref{$key} = $value;
5696: }
5697: } else {
1.506 raeburn 5698: my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800 albertel 5699: foreach my $line (@responses) {
5700: my ($key,$value) = split(/=/,$line);
1.506 raeburn 5701: $$replyref{$key} = $value;
5702: if ($value > 0) {
1.800 albertel 5703: foreach my $item (@{$$affiliatesref{$key}}) {
5704: my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506 raeburn 5705: my $destname = $pathname.'/'.$filename;
5706: my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526 raeburn 5707: if ($xml_classlist =~ /^error/) {
5708: &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
5709: } else {
1.506 raeburn 5710: if ( open(FILE,">$destname") ) {
5711: print FILE &unescape($xml_classlist);
5712: close(FILE);
1.526 raeburn 5713: } else {
5714: &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506 raeburn 5715: }
5716: }
5717: }
5718: }
5719: }
5720: }
5721: return 'ok';
5722: }
5723: return 'error';
5724: }
5725:
1.242 www 5726: sub get_query_reply {
5727: my $queryid=shift;
1.240 www 5728: my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
5729: my $reply='';
5730: for (1..100) {
5731: sleep 2;
5732: if (-e $replyfile.'.end') {
1.448 albertel 5733: if (open(my $fh,$replyfile)) {
1.904 albertel 5734: $reply = join('',<$fh>);
5735: close($fh);
1.240 www 5736: } else { return 'error: reply_file_error'; }
1.242 www 5737: return &unescape($reply);
5738: }
1.240 www 5739: }
1.242 www 5740: return 'timeout:'.$queryid;
1.240 www 5741: }
5742:
5743: sub courselog_query {
1.241 www 5744: #
5745: # possible filters:
5746: # url: url or symb
5747: # username
5748: # domain
5749: # action: view, submit, grade
5750: # start: timestamp
5751: # end: timestamp
5752: #
1.240 www 5753: my (%filters)=@_;
1.620 albertel 5754: unless ($env{'request.course.id'}) { return 'no_course'; }
1.241 www 5755: if ($filters{'url'}) {
5756: $filters{'url'}=&symbclean(&declutter($filters{'url'}));
5757: $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
5758: $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
5759: }
1.620 albertel 5760: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
5761: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240 www 5762: return &log_query($cname,$cdom,'courselog',%filters);
5763: }
5764:
5765: sub userlog_query {
1.858 raeburn 5766: #
5767: # possible filters:
5768: # action: log check role
5769: # start: timestamp
5770: # end: timestamp
5771: #
1.240 www 5772: my ($uname,$udom,%filters)=@_;
5773: return &log_query($uname,$udom,'userlog',%filters);
1.12 www 5774: }
5775:
1.506 raeburn 5776: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course
5777:
5778: sub auto_run {
1.508 raeburn 5779: my ($cnum,$cdom) = @_;
1.876 raeburn 5780: my $response = 0;
5781: my $settings;
5782: my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
5783: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
5784: $settings = $domconfig{'autoenroll'};
5785: if ($settings->{'run'} eq '1') {
5786: $response = 1;
5787: }
5788: } else {
1.934 raeburn 5789: my $homeserver;
5790: if (&is_course($cdom,$cnum)) {
5791: $homeserver = &homeserver($cnum,$cdom);
5792: } else {
5793: $homeserver = &domain($cdom,'primary');
5794: }
5795: if ($homeserver ne 'no_host') {
5796: $response = &reply('autorun:'.$cdom,$homeserver);
5797: }
1.876 raeburn 5798: }
1.506 raeburn 5799: return $response;
5800: }
1.776 albertel 5801:
1.506 raeburn 5802: sub auto_get_sections {
1.508 raeburn 5803: my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007 raeburn 5804: my $homeserver;
5805: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
5806: $homeserver = &homeserver($cnum,$cdom);
5807: }
5808: if (!defined($homeserver)) {
5809: if ($cdom =~ /^$match_domain$/) {
5810: $homeserver = &domain($cdom,'primary');
5811: }
5812: }
5813: my @secs;
5814: if (defined($homeserver)) {
5815: my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
5816: unless ($response eq 'refused') {
5817: @secs = split(/:/,$response);
5818: }
1.506 raeburn 5819: }
5820: return @secs;
5821: }
1.776 albertel 5822:
1.506 raeburn 5823: sub auto_new_course {
1.508 raeburn 5824: my ($cnum,$cdom,$inst_course_id,$owner) = @_;
5825: my $homeserver = &homeserver($cnum,$cdom);
1.515 raeburn 5826: my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506 raeburn 5827: return $response;
5828: }
1.776 albertel 5829:
1.506 raeburn 5830: sub auto_validate_courseID {
1.508 raeburn 5831: my ($cnum,$cdom,$inst_course_id) = @_;
5832: my $homeserver = &homeserver($cnum,$cdom);
1.511 raeburn 5833: my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506 raeburn 5834: return $response;
5835: }
1.776 albertel 5836:
1.1007 raeburn 5837: sub auto_validate_instcode {
1.1020 raeburn 5838: my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007 raeburn 5839: my ($homeserver,$response);
5840: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
5841: $homeserver = &homeserver($cnum,$cdom);
5842: }
5843: if (!defined($homeserver)) {
5844: if ($cdom =~ /^$match_domain$/) {
5845: $homeserver = &domain($cdom,'primary');
5846: }
5847: }
1.1065 raeburn 5848: $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
5849: &escape($instcode).':'.&escape($owner),$homeserver));
1.1027 raeburn 5850: my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
5851: return ($outcome,$description);
1.1007 raeburn 5852: }
5853:
1.506 raeburn 5854: sub auto_create_password {
1.873 raeburn 5855: my ($cnum,$cdom,$authparam,$udom) = @_;
5856: my ($homeserver,$response);
1.506 raeburn 5857: my $create_passwd = 0;
5858: my $authchk = '';
1.873 raeburn 5859: if ($udom =~ /^$match_domain$/) {
5860: $homeserver = &domain($udom,'primary');
5861: }
5862: if ($homeserver eq '') {
5863: if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
5864: $homeserver = &homeserver($cnum,$cdom);
5865: }
5866: }
5867: if ($homeserver eq '') {
5868: $authchk = 'nodomain';
1.506 raeburn 5869: } else {
1.873 raeburn 5870: $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
5871: if ($response eq 'refused') {
5872: $authchk = 'refused';
5873: } else {
1.901 albertel 5874: ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873 raeburn 5875: }
1.506 raeburn 5876: }
5877: return ($authparam,$create_passwd,$authchk);
5878: }
5879:
1.706 raeburn 5880: sub auto_photo_permission {
5881: my ($cnum,$cdom,$students) = @_;
5882: my $homeserver = &homeserver($cnum,$cdom);
1.707 albertel 5883: my ($outcome,$perm_reqd,$conditions) =
5884: split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709 albertel 5885: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5886: return (undef,undef);
5887: }
1.706 raeburn 5888: return ($outcome,$perm_reqd,$conditions);
5889: }
5890:
5891: sub auto_checkphotos {
5892: my ($uname,$udom,$pid) = @_;
5893: my $homeserver = &homeserver($uname,$udom);
5894: my ($result,$resulttype);
5895: my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707 albertel 5896: &escape($uname).':'.&escape($pid),
5897: $homeserver));
1.709 albertel 5898: if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5899: return (undef,undef);
5900: }
1.706 raeburn 5901: if ($outcome) {
5902: ($result,$resulttype) = split(/:/,$outcome);
5903: }
5904: return ($result,$resulttype);
5905: }
5906:
5907: sub auto_photochoice {
5908: my ($cnum,$cdom) = @_;
5909: my $homeserver = &homeserver($cnum,$cdom);
5910: my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707 albertel 5911: &escape($cdom),
5912: $homeserver)));
1.709 albertel 5913: if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
5914: return (undef,undef);
5915: }
1.706 raeburn 5916: return ($update,$comment);
5917: }
5918:
5919: sub auto_photoupdate {
5920: my ($affiliatesref,$dom,$cnum,$photo) = @_;
5921: my $homeserver = &homeserver($cnum,$dom);
1.838 albertel 5922: my $host=&hostname($homeserver);
1.706 raeburn 5923: my $cmd = '';
5924: my $maxtries = 1;
1.800 albertel 5925: foreach my $affiliate (keys(%{$affiliatesref})) {
5926: $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706 raeburn 5927: }
5928: $cmd =~ s/%%$//;
5929: $cmd = &escape($cmd);
5930: my $query = 'institutionalphotos';
5931: my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
5932: unless ($queryid=~/^\Q$host\E\_/) {
5933: &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
5934: return 'error: '.$queryid;
5935: }
5936: my $reply = &get_query_reply($queryid);
5937: my $tries = 1;
5938: while (($reply=~/^timeout/) && ($tries < $maxtries)) {
5939: $reply = &get_query_reply($queryid);
5940: $tries ++;
5941: }
5942: if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
5943: &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
5944: } else {
5945: my @responses = split(/:/,$reply);
5946: my $outcome = shift(@responses);
5947: foreach my $item (@responses) {
5948: my ($key,$value) = split(/=/,$item);
5949: $$photo{$key} = $value;
5950: }
5951: return $outcome;
5952: }
5953: return 'error';
5954: }
5955:
1.521 raeburn 5956: sub auto_instcode_format {
1.793 albertel 5957: my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
5958: $cat_order) = @_;
1.521 raeburn 5959: my $courses = '';
1.772 raeburn 5960: my @homeservers;
1.521 raeburn 5961: if ($caller eq 'global') {
1.841 albertel 5962: my %servers = &get_servers($codedom,'library');
5963: foreach my $tryserver (keys(%servers)) {
5964: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
5965: push(@homeservers,$tryserver);
5966: }
1.584 raeburn 5967: }
1.1022 raeburn 5968: } elsif ($caller eq 'requests') {
5969: if ($codedom =~ /^$match_domain$/) {
5970: my $chome = &domain($codedom,'primary');
5971: unless ($chome eq 'no_host') {
5972: push(@homeservers,$chome);
5973: }
5974: }
1.521 raeburn 5975: } else {
1.772 raeburn 5976: push(@homeservers,&homeserver($caller,$codedom));
1.521 raeburn 5977: }
1.793 albertel 5978: foreach my $code (keys(%{$instcodes})) {
5979: $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521 raeburn 5980: }
5981: chop($courses);
1.772 raeburn 5982: my $ok_response = 0;
5983: my $response;
5984: while (@homeservers > 0 && $ok_response == 0) {
5985: my $server = shift(@homeservers);
5986: $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
5987: if ($response !~ /(con_lost|error|no_such_host|refused)/) {
5988: my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) =
1.901 albertel 5989: split(/:/,$response);
1.772 raeburn 5990: %{$codes} = (%{$codes},&str2hash($codes_str));
5991: push(@{$codetitles},&str2array($codetitles_str));
5992: %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
5993: %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
5994: $ok_response = 1;
5995: }
5996: }
5997: if ($ok_response) {
1.521 raeburn 5998: return 'ok';
1.772 raeburn 5999: } else {
6000: return $response;
1.521 raeburn 6001: }
6002: }
6003:
1.792 raeburn 6004: sub auto_instcode_defaults {
6005: my ($domain,$returnhash,$code_order) = @_;
6006: my @homeservers;
1.841 albertel 6007:
6008: my %servers = &get_servers($domain,'library');
6009: foreach my $tryserver (keys(%servers)) {
6010: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6011: push(@homeservers,$tryserver);
6012: }
1.792 raeburn 6013: }
1.841 albertel 6014:
1.792 raeburn 6015: my $response;
1.841 albertel 6016: foreach my $server (@homeservers) {
1.792 raeburn 6017: $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841 albertel 6018: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
6019:
6020: foreach my $pair (split(/\&/,$response)) {
6021: my ($name,$value)=split(/\=/,$pair);
6022: if ($name eq 'code_order') {
6023: @{$code_order} = split(/\&/,&unescape($value));
6024: } else {
6025: $returnhash->{&unescape($name)}=&unescape($value);
6026: }
6027: }
6028: return 'ok';
1.792 raeburn 6029: }
1.841 albertel 6030:
6031: return $response;
1.1003 raeburn 6032: }
6033:
6034: sub auto_possible_instcodes {
1.1007 raeburn 6035: my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
6036: unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') &&
6037: (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
6038: return;
6039: }
1.1003 raeburn 6040: my (@homeservers,$uhome);
6041: if (defined(&domain($domain,'primary'))) {
6042: $uhome=&domain($domain,'primary');
6043: push(@homeservers,&domain($domain,'primary'));
6044: } else {
6045: my %servers = &get_servers($domain,'library');
6046: foreach my $tryserver (keys(%servers)) {
6047: if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
6048: push(@homeservers,$tryserver);
6049: }
6050: }
6051: }
6052: my $response;
6053: foreach my $server (@homeservers) {
6054: $response=&reply('autopossibleinstcodes:'.$domain,$server);
6055: next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007 raeburn 6056: my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) =
6057: split(':',$response);
6058: @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
6059: @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003 raeburn 6060: foreach my $item (split('&',$cat_title)) {
1.1005 raeburn 6061: my ($name,$value)=split('=',$item);
6062: $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6063: }
6064: foreach my $item (split('&',$cat_order)) {
1.1005 raeburn 6065: my ($name,$value)=split('=',$item);
6066: $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003 raeburn 6067: }
6068: return 'ok';
6069: }
6070: return $response;
6071: }
1.792 raeburn 6072:
1.1010 raeburn 6073: sub auto_courserequest_checks {
6074: my ($dom) = @_;
1.1020 raeburn 6075: my ($homeserver,%validations);
6076: if ($dom =~ /^$match_domain$/) {
6077: $homeserver = &domain($dom,'primary');
6078: }
6079: unless ($homeserver eq 'no_host') {
6080: my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
6081: unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
6082: my @items = split(/&/,$response);
6083: foreach my $item (@items) {
6084: my ($key,$value) = split('=',$item);
6085: $validations{&unescape($key)} = &thaw_unescape($value);
6086: }
6087: }
6088: }
1.1010 raeburn 6089: return %validations;
6090: }
6091:
1.1020 raeburn 6092: sub auto_courserequest_validation {
6093: my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
6094: my ($homeserver,$response);
6095: if ($dom =~ /^$match_domain$/) {
6096: $homeserver = &domain($dom,'primary');
6097: }
6098: unless ($homeserver eq 'no_host') {
1.1021 raeburn 6099:
1.1020 raeburn 6100: $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021 raeburn 6101: ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020 raeburn 6102: ':'.&escape($instcode).':'.&escape($instseclist),
6103: $homeserver));
6104: }
6105: return $response;
6106: }
6107:
1.777 albertel 6108: sub auto_validate_class_sec {
1.918 raeburn 6109: my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773 raeburn 6110: my $homeserver = &homeserver($cnum,$cdom);
1.918 raeburn 6111: my $ownerlist;
6112: if (ref($owners) eq 'ARRAY') {
6113: $ownerlist = join(',',@{$owners});
6114: } else {
6115: $ownerlist = $owners;
6116: }
1.773 raeburn 6117: my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918 raeburn 6118: &escape($ownerlist).':'.$cdom,$homeserver);
1.773 raeburn 6119: return $response;
6120: }
6121:
1.679 raeburn 6122: # ------------------------------------------------------- Course Group routines
6123:
6124: sub get_coursegroups {
1.809 raeburn 6125: my ($cdom,$cnum,$group,$namespace) = @_;
6126: return(&dump($namespace,$cdom,$cnum,$group));
1.805 raeburn 6127: }
6128:
1.679 raeburn 6129: sub modify_coursegroup {
6130: my ($cdom,$cnum,$groupsettings) = @_;
6131: return(&put('coursegroups',$groupsettings,$cdom,$cnum));
6132: }
6133:
1.809 raeburn 6134: sub toggle_coursegroup_status {
6135: my ($cdom,$cnum,$group,$action) = @_;
6136: my ($from_namespace,$to_namespace);
6137: if ($action eq 'delete') {
6138: $from_namespace = 'coursegroups';
6139: $to_namespace = 'deleted_groups';
6140: } else {
6141: $from_namespace = 'deleted_groups';
6142: $to_namespace = 'coursegroups';
6143: }
6144: my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805 raeburn 6145: if (my $tmp = &error(%curr_group)) {
6146: &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
6147: return ('read error',$tmp);
6148: } else {
6149: my %savedsettings = %curr_group;
1.809 raeburn 6150: my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805 raeburn 6151: my $deloutcome;
6152: if ($result eq 'ok') {
1.809 raeburn 6153: $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805 raeburn 6154: } else {
6155: return ('write error',$result);
6156: }
6157: if ($deloutcome eq 'ok') {
6158: return 'ok';
6159: } else {
6160: return ('delete error',$deloutcome);
6161: }
6162: }
6163: }
6164:
1.679 raeburn 6165: sub modify_group_roles {
1.957 raeburn 6166: my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679 raeburn 6167: my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
6168: my $role = 'gr/'.&escape($userprivs);
6169: my ($uname,$udom) = split(/:/,$user);
1.957 raeburn 6170: my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684 raeburn 6171: if ($result eq 'ok') {
6172: &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
6173: }
1.679 raeburn 6174: return $result;
6175: }
6176:
6177: sub modify_coursegroup_membership {
6178: my ($cdom,$cnum,$membership) = @_;
6179: my $result = &put('groupmembership',$membership,$cdom,$cnum);
6180: return $result;
6181: }
6182:
1.682 raeburn 6183: sub get_active_groups {
6184: my ($udom,$uname,$cdom,$cnum) = @_;
6185: my $now = time;
6186: my %groups = ();
6187: foreach my $key (keys(%env)) {
1.811 albertel 6188: if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682 raeburn 6189: my ($start,$end) = split(/\./,$env{$key});
6190: if (($end!=0) && ($end<$now)) { next; }
6191: if (($start!=0) && ($start>$now)) { next; }
6192: if ($1 eq $cdom && $2 eq $cnum) {
6193: $groups{$3} = $env{$key} ;
6194: }
6195: }
6196: }
6197: return %groups;
6198: }
6199:
1.683 raeburn 6200: sub get_group_membership {
6201: my ($cdom,$cnum,$group) = @_;
6202: return(&dump('groupmembership',$cdom,$cnum,$group));
6203: }
6204:
6205: sub get_users_groups {
6206: my ($udom,$uname,$courseid) = @_;
1.733 raeburn 6207: my @usersgroups;
1.683 raeburn 6208: my $cachetime=1800;
6209:
6210: my $hashid="$udom:$uname:$courseid";
1.733 raeburn 6211: my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
6212: if (defined($cached)) {
1.734 albertel 6213: @usersgroups = split(/:/,$grouplist);
1.733 raeburn 6214: } else {
6215: $grouplist = '';
1.816 raeburn 6216: my $courseurl = &courseid_to_courseurl($courseid);
6217: my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817 raeburn 6218: my $access_end = $env{'course.'.$courseid.
6219: '.default_enrollment_end_date'};
6220: my $now = time;
6221: foreach my $key (keys(%roleshash)) {
6222: if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
6223: my $group = $1;
6224: if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
6225: my $start = $2;
6226: my $end = $1;
6227: if ($start == -1) { next; } # deleted from group
6228: if (($start!=0) && ($start>$now)) { next; }
6229: if (($end!=0) && ($end<$now)) {
6230: if ($access_end && $access_end < $now) {
6231: if ($access_end - $end < 86400) {
6232: push(@usersgroups,$group);
1.733 raeburn 6233: }
6234: }
1.817 raeburn 6235: next;
1.733 raeburn 6236: }
1.817 raeburn 6237: push(@usersgroups,$group);
1.683 raeburn 6238: }
6239: }
6240: }
1.817 raeburn 6241: @usersgroups = &sort_course_groups($courseid,@usersgroups);
6242: $grouplist = join(':',@usersgroups);
6243: &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683 raeburn 6244: }
1.733 raeburn 6245: return @usersgroups;
1.683 raeburn 6246: }
6247:
6248: sub devalidate_getgroups_cache {
6249: my ($udom,$uname,$cdom,$cnum)=@_;
6250: my $courseid = $cdom.'_'.$cnum;
1.807 albertel 6251:
1.683 raeburn 6252: my $hashid="$udom:$uname:$courseid";
6253: &devalidate_cache_new('getgroups',$hashid);
6254: }
6255:
1.12 www 6256: # ------------------------------------------------------------------ Plain Text
6257:
6258: sub plaintext {
1.988 raeburn 6259: my ($short,$type,$cid,$forcedefault) = @_;
1.1046 raeburn 6260: if ($short =~ m{^cr/}) {
1.758 albertel 6261: return (split('/',$short))[-1];
6262: }
1.742 raeburn 6263: if (!defined($cid)) {
6264: $cid = $env{'request.course.id'};
6265: }
6266: my %rolenames = (
1.1008 raeburn 6267: Course => 'std',
6268: Community => 'alt1',
1.742 raeburn 6269: );
1.1037 raeburn 6270: if ($cid ne '') {
6271: if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
6272: unless ($forcedefault) {
6273: my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};
6274: &Apache::lonlocal::mt_escape(\$roletext);
6275: return &Apache::lonlocal::mt($roletext);
6276: }
6277: }
6278: }
6279: if ((defined($type)) && (defined($rolenames{$type})) &&
6280: (defined($rolenames{$type})) &&
6281: (defined($prp{$short}{$rolenames{$type}}))) {
1.742 raeburn 6282: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037 raeburn 6283: } elsif ($cid ne '') {
6284: my $crstype = $env{'course.'.$cid.'.type'};
6285: if (($crstype ne '') && (defined($rolenames{$crstype})) &&
6286: (defined($prp{$short}{$rolenames{$crstype}}))) {
6287: return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
6288: }
1.742 raeburn 6289: }
1.1037 raeburn 6290: return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12 www 6291: }
6292:
6293: # ----------------------------------------------------------------- Assign Role
6294:
6295: sub assignrole {
1.957 raeburn 6296: my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
6297: $context)=@_;
1.21 www 6298: my $mrole;
6299: if ($role =~ /^cr\//) {
1.393 www 6300: my $cwosec=$url;
1.811 albertel 6301: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393 www 6302: unless (&allowed('ccr',$cwosec)) {
1.1026 raeburn 6303: my $refused = 1;
6304: if ($context eq 'requestcourses') {
6305: if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
6306: if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
6307: if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
6308: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6309: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6310: if ($crsenv{'internal.courseowner'} eq
6311: $env{'user.name'}.':'.$env{'user.domain'}) {
6312: $refused = '';
6313: }
6314: }
6315: }
6316: }
6317: }
6318: if ($refused) {
6319: &logthis('Refused custom assignrole: '.
6320: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
6321: ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
6322: return 'refused';
6323: }
1.104 www 6324: }
1.21 www 6325: $mrole='cr';
1.678 raeburn 6326: } elsif ($role =~ /^gr\//) {
6327: my $cwogrp=$url;
1.811 albertel 6328: $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678 raeburn 6329: unless (&allowed('mdg',$cwogrp)) {
6330: &logthis('Refused group assignrole: '.
6331: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
6332: $env{'user.name'}.' at '.$env{'user.domain'});
6333: return 'refused';
6334: }
6335: $mrole='gr';
1.21 www 6336: } else {
1.82 www 6337: my $cwosec=$url;
1.811 albertel 6338: $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932 raeburn 6339: if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
6340: my $refused;
6341: if (($env{'request.course.sec'} ne '') && ($role eq 'st')) {
6342: if (!(&allowed('c'.$role,$url))) {
6343: $refused = 1;
6344: }
6345: } else {
6346: $refused = 1;
6347: }
1.947 raeburn 6348: if ($refused) {
1.1045 raeburn 6349: my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
6350: if (!$selfenroll && $context eq 'course') {
6351: my %crsenv;
6352: if ($role eq 'cc' || $role eq 'co') {
6353: %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6354: if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
6355: if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
6356: if ($crsenv{'internal.courseowner'} eq
6357: $env{'user.name'}.':'.$env{'user.domain'}) {
6358: $refused = '';
6359: }
6360: }
6361: } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) {
6362: if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
6363: if ($crsenv{'internal.courseowner'} eq
6364: $env{'user.name'}.':'.$env{'user.domain'}) {
6365: $refused = '';
6366: }
6367: }
6368: }
6369: }
6370: } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947 raeburn 6371: $refused = '';
1.1017 raeburn 6372: } elsif ($context eq 'requestcourses') {
1.1041 raeburn 6373: my @possroles = ('st','ta','ep','in','cc','co');
1.1026 raeburn 6374: if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041 raeburn 6375: my $wrongcc;
6376: if ($cnum =~ /^$match_community$/) {
6377: $wrongcc = 1 if ($role eq 'cc');
6378: } else {
6379: $wrongcc = 1 if ($role eq 'co');
6380: }
6381: unless ($wrongcc) {
6382: my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
6383: if ($crsenv{'internal.courseowner'} eq
6384: $env{'user.name'}.':'.$env{'user.domain'}) {
6385: $refused = '';
6386: }
1.1017 raeburn 6387: }
6388: }
6389: }
6390: if ($refused) {
1.947 raeburn 6391: &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
6392: ' '.$role.' '.$end.' '.$start.' by '.
6393: $env{'user.name'}.' at '.$env{'user.domain'});
6394: return 'refused';
6395: }
1.932 raeburn 6396: }
1.104 www 6397: }
1.21 www 6398: $mrole=$role;
6399: }
1.620 albertel 6400: my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21 www 6401: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 6402: if ($end) { $command.='_'.$end; }
1.21 www 6403: if ($start) {
6404: if ($end) {
1.81 www 6405: $command.='_'.$start;
1.21 www 6406: } else {
1.81 www 6407: $command.='_0_'.$start;
1.21 www 6408: }
6409: }
1.739 raeburn 6410: my $origstart = $start;
6411: my $origend = $end;
1.957 raeburn 6412: my $delflag;
1.357 www 6413: # actually delete
6414: if ($deleteflag) {
1.373 www 6415: if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357 www 6416: # modify command to delete the role
1.620 albertel 6417: $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357 www 6418: "$udom:$uname:$url".'_'."$mrole";
1.620 albertel 6419: &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom");
1.357 www 6420: # set start and finish to negative values for userrolelog
6421: $start=-1;
6422: $end=-1;
1.957 raeburn 6423: $delflag = 1;
1.357 www 6424: }
6425: }
6426: # send command
1.349 www 6427: my $answer=&reply($command,&homeserver($uname,$udom));
1.357 www 6428: # log new user role if status is ok
1.349 www 6429: if ($answer eq 'ok') {
1.663 raeburn 6430: &userrolelog($role,$uname,$udom,$url,$start,$end);
1.739 raeburn 6431: # for course roles, perform group memberships changes triggered by role change.
1.957 raeburn 6432: &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739 raeburn 6433: unless ($role =~ /^gr/) {
6434: &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957 raeburn 6435: $origstart,$selfenroll,$context);
1.739 raeburn 6436: }
1.1053 raeburn 6437: if ($role eq 'cc') {
6438: &autoupdate_coowners($url,$end,$start,$uname,$udom);
6439: }
1.349 www 6440: }
6441: return $answer;
1.169 harris41 6442: }
6443:
1.1053 raeburn 6444: sub autoupdate_coowners {
6445: my ($url,$end,$start,$uname,$udom) = @_;
6446: my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
6447: if (($cdom ne '') && ($cnum ne '')) {
6448: my $now = time;
6449: my %domdesign = &Apache::loncommon::get_domainconf($cdom);
6450: if ($domdesign{$cdom.'.autoassign.co-owners'}) {
6451: my %coursehash = &coursedescription($cdom.'_'.$cnum);
6452: my $instcode = $coursehash{'internal.coursecode'};
6453: if ($instcode ne '') {
1.1056 raeburn 6454: if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
6455: unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053 raeburn 6456: my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056 raeburn 6457: my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
6458: if ($result eq 'valid') {
6459: if ($coursehash{'internal.co-owners'}) {
1.1053 raeburn 6460: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6461: push(@newcoowners,$coowner);
6462: }
6463: unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
6464: push(@newcoowners,$uname.':'.$udom);
6465: }
6466: @newcoowners = sort(@newcoowners);
6467: } else {
6468: push(@newcoowners,$uname.':'.$udom);
6469: }
6470: } else {
6471: if ($coursehash{'internal.co-owners'}) {
6472: foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
6473: unless ($coowner eq $uname.':'.$udom) {
6474: push(@newcoowners,$coowner);
6475: }
6476: }
6477: unless (@newcoowners > 0) {
6478: $delcoowners = 1;
6479: $coowners = '';
6480: }
6481: }
6482: }
6483: if (@newcoowners || $delcoowners) {
6484: &store_coowners($cdom,$cnum,$coursehash{'home'},
6485: $delcoowners,@newcoowners);
6486: }
6487: }
6488: }
6489: }
6490: }
6491: }
6492: }
6493:
6494: sub store_coowners {
6495: my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
6496: my $cid = $cdom.'_'.$cnum;
6497: my ($coowners,$delresult,$putresult);
6498: if (@newcoowners) {
6499: $coowners = join(',',@newcoowners);
6500: my %coownershash = (
6501: 'internal.co-owners' => $coowners,
6502: );
6503: $putresult = &put('environment',\%coownershash,$cdom,$cnum);
6504: if ($putresult eq 'ok') {
6505: if ($env{'course.'.$cid.'.num'} eq $cnum) {
6506: &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
6507: }
6508: }
6509: }
6510: if ($delcoowners) {
6511: $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
6512: if ($delresult eq 'ok') {
6513: if ($env{'course.'.$cid.'.internal.co-owners'}) {
6514: &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
6515: }
6516: }
6517: }
6518: if (($putresult eq 'ok') || ($delresult eq 'ok')) {
6519: my %crsinfo =
6520: &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
6521: if (ref($crsinfo{$cid}) eq 'HASH') {
6522: $crsinfo{$cid}{'co-owners'} = \@newcoowners;
6523: my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
6524: }
6525: }
6526: }
6527:
1.169 harris41 6528: # -------------------------------------------------- Modify user authentication
1.197 www 6529: # Overrides without validation
6530:
1.169 harris41 6531: sub modifyuserauth {
6532: my ($udom,$uname,$umode,$upass)=@_;
6533: my $uhome=&homeserver($uname,$udom);
1.197 www 6534: unless (&allowed('mau',$udom)) { return 'refused'; }
6535: &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620 albertel 6536: $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6537: ' in domain '.$env{'request.role.domain'});
1.169 harris41 6538: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
6539: &escape($upass),$uhome);
1.620 albertel 6540: &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197 www 6541: 'Authentication changed for '.$udom.', '.$uname.', '.$umode.
6542: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
6543: &log($udom,,$uname,$uhome,
1.620 albertel 6544: 'Authentication changed by '.$env{'user.domain'}.', '.
6545: $env{'user.name'}.', '.$umode.
1.197 www 6546: '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169 harris41 6547: unless ($reply eq 'ok') {
1.197 www 6548: &logthis('Authentication mode error: '.$reply);
1.169 harris41 6549: return 'error: '.$reply;
6550: }
1.170 harris41 6551: return 'ok';
1.80 www 6552: }
6553:
1.81 www 6554: # --------------------------------------------------------------- Modify a user
1.80 www 6555:
1.81 www 6556: sub modifyuser {
1.206 matthew 6557: my ($udom, $uname, $uid,
6558: $umode, $upass, $first,
6559: $middle, $last, $gene,
1.1058 raeburn 6560: $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807 albertel 6561: $udom= &LONCAPA::clean_domain($udom);
6562: $uname=&LONCAPA::clean_username($uname);
1.1059 raeburn 6563: my $showcandelete = 'none';
6564: if (ref($candelete) eq 'ARRAY') {
6565: if (@{$candelete} > 0) {
6566: $showcandelete = join(', ',@{$candelete});
6567: }
6568: }
1.81 www 6569: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 6570: $umode.', '.$first.', '.$middle.', '.
1.1059 raeburn 6571: $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206 matthew 6572: (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
6573: ' desiredhome not specified').
1.620 albertel 6574: ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
6575: ' in domain '.$env{'request.role.domain'});
1.230 stredwic 6576: my $uhome=&homeserver($uname,$udom,'true');
1.80 www 6577: # ----------------------------------------------------------------- Create User
1.406 albertel 6578: if (($uhome eq 'no_host') &&
6579: (($umode && $upass) || ($umode eq 'localauth'))) {
1.80 www 6580: my $unhome='';
1.844 albertel 6581: if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) {
1.209 matthew 6582: $unhome = $desiredhome;
1.620 albertel 6583: } elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
6584: $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209 matthew 6585: } else { # load balancing routine for determining $unhome
1.81 www 6586: my $loadm=10000000;
1.841 albertel 6587: my %servers = &get_servers($udom,'library');
6588: foreach my $tryserver (keys(%servers)) {
6589: my $answer=reply('load',$tryserver);
6590: if (($answer=~/\d+/) && ($answer<$loadm)) {
6591: $loadm=$answer;
6592: $unhome=$tryserver;
6593: }
1.80 www 6594: }
6595: }
6596: if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206 matthew 6597: return 'error: unable to find a home server for '.$uname.
6598: ' in domain '.$udom;
1.80 www 6599: }
6600: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
6601: &escape($upass),$unhome);
6602: unless ($reply eq 'ok') {
6603: return 'error: '.$reply;
6604: }
1.230 stredwic 6605: $uhome=&homeserver($uname,$udom,'true');
1.80 www 6606: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386 matthew 6607: return 'error: unable verify users home machine.';
1.80 www 6608: }
1.209 matthew 6609: } # End of creation of new user
1.80 www 6610: # ---------------------------------------------------------------------- Add ID
6611: if ($uid) {
6612: $uid=~tr/A-Z/a-z/;
6613: my %uidhash=&idrget($udom,$uname);
1.196 www 6614: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
6615: && (!$forceid)) {
1.80 www 6616: unless ($uid eq $uidhash{$uname}) {
1.386 matthew 6617: return 'error: user id "'.$uid.'" does not match '.
6618: 'current user id "'.$uidhash{$uname}.'".';
1.80 www 6619: }
6620: } else {
6621: &idput($udom,($uname => $uid));
6622: }
6623: }
6624: # -------------------------------------------------------------- Add names, etc
1.313 matthew 6625: my @tmp=&get('environment',
1.899 raeburn 6626: ['firstname','middlename','lastname','generation','id',
1.963 raeburn 6627: 'permanentemail','inststatus'],
1.134 albertel 6628: $udom,$uname);
1.313 matthew 6629: my %names;
6630: if ($tmp[0] =~ m/^error:.*/) {
6631: %names=();
6632: } else {
6633: %names = @tmp;
6634: }
1.388 www 6635: #
1.1058 raeburn 6636: # If name, email and/or uid are blank (e.g., because an uploaded file
6637: # of users did not contain them), do not overwrite existing values
6638: # unless field is in $candelete array ref.
6639: #
6640:
6641: my @fields = ('firstname','middlename','lastname','generation',
6642: 'permanentemail','id');
6643: my %newvalues;
6644: if (ref($candelete) eq 'ARRAY') {
6645: foreach my $field (@fields) {
6646: if (grep(/^\Q$field\E$/,@{$candelete})) {
6647: if ($field eq 'firstname') {
6648: $names{$field} = $first;
6649: } elsif ($field eq 'middlename') {
6650: $names{$field} = $middle;
6651: } elsif ($field eq 'lastname') {
6652: $names{$field} = $last;
6653: } elsif ($field eq 'generation') {
6654: $names{$field} = $gene;
6655: } elsif ($field eq 'permanentemail') {
6656: $names{$field} = $email;
6657: } elsif ($field eq 'id') {
6658: $names{$field} = $uid;
6659: }
6660: }
6661: }
6662: }
1.388 www 6663: if ($first) { $names{'firstname'} = $first; }
1.385 matthew 6664: if (defined($middle)) { $names{'middlename'} = $middle; }
1.388 www 6665: if ($last) { $names{'lastname'} = $last; }
1.385 matthew 6666: if (defined($gene)) { $names{'generation'} = $gene; }
1.592 www 6667: if ($email) {
6668: $email=~s/[^\w\@\.\-\,]//gs;
1.963 raeburn 6669: if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592 www 6670: }
1.899 raeburn 6671: if ($uid) { $names{'id'} = $uid; }
1.989 raeburn 6672: if (defined($inststatus)) {
6673: $names{'inststatus'} = '';
6674: my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
6675: if (ref($usertypes) eq 'HASH') {
6676: my @okstatuses;
6677: foreach my $item (split(/:/,$inststatus)) {
6678: if (defined($usertypes->{$item})) {
6679: push(@okstatuses,$item);
6680: }
6681: }
6682: if (@okstatuses) {
6683: $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
6684: }
6685: }
6686: }
1.134 albertel 6687: my $reply = &put('environment', \%names, $udom,$uname);
6688: if ($reply ne 'ok') { return 'error: '.$reply; }
1.899 raeburn 6689: my $sqlresult = &update_allusers_table($uname,$udom,\%names);
1.680 www 6690: &devalidate_cache_new('namescache',$uname.':'.$udom);
1.963 raeburn 6691: my $logmsg = 'Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
6692: $umode.', '.$first.', '.$middle.', '.
6693: $last.', '.$gene.', '.$email.', '.$inststatus;
6694: if ($env{'user.name'} ne '' && $env{'user.domain'}) {
6695: $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
6696: } else {
6697: $logmsg .= ' during self creation';
6698: }
6699: &logthis($logmsg);
1.134 albertel 6700: return 'ok';
1.80 www 6701: }
6702:
1.81 www 6703: # -------------------------------------------------------------- Modify student
1.80 www 6704:
1.81 www 6705: sub modifystudent {
6706: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957 raeburn 6707: $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990 raeburn 6708: $selfenroll,$context,$inststatus)=@_;
1.455 albertel 6709: if (!$cid) {
1.620 albertel 6710: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 6711: return 'not_in_class';
6712: }
1.80 www 6713: }
6714: # --------------------------------------------------------------- Make the user
1.81 www 6715: my $reply=&modifyuser
1.209 matthew 6716: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990 raeburn 6717: $desiredhome,$email,$inststatus);
1.80 www 6718: unless ($reply eq 'ok') { return $reply; }
1.297 matthew 6719: # This will cause &modify_student_enrollment to get the uid from the
6720: # students environment
6721: $uid = undef if (!$forceid);
1.455 albertel 6722: $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957 raeburn 6723: $gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297 matthew 6724: return $reply;
6725: }
6726:
6727: sub modify_student_enrollment {
1.957 raeburn 6728: my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455 albertel 6729: my ($cdom,$cnum,$chome);
6730: if (!$cid) {
1.620 albertel 6731: unless ($cid=$env{'request.course.id'}) {
1.455 albertel 6732: return 'not_in_class';
6733: }
1.620 albertel 6734: $cdom=$env{'course.'.$cid.'.domain'};
6735: $cnum=$env{'course.'.$cid.'.num'};
1.455 albertel 6736: } else {
6737: ($cdom,$cnum)=split(/_/,$cid);
6738: }
1.620 albertel 6739: $chome=$env{'course.'.$cid.'.home'};
1.455 albertel 6740: if (!$chome) {
1.457 raeburn 6741: $chome=&homeserver($cnum,$cdom);
1.297 matthew 6742: }
1.455 albertel 6743: if (!$chome) { return 'unknown_course'; }
1.297 matthew 6744: # Make sure the user exists
1.81 www 6745: my $uhome=&homeserver($uname,$udom);
6746: if (($uhome eq '') || ($uhome eq 'no_host')) {
6747: return 'error: no such user';
6748: }
1.297 matthew 6749: # Get student data if we were not given enough information
6750: if (!defined($first) || $first eq '' ||
6751: !defined($last) || $last eq '' ||
6752: !defined($uid) || $uid eq '' ||
6753: !defined($middle) || $middle eq '' ||
6754: !defined($gene) || $gene eq '') {
1.294 matthew 6755: # They did not supply us with enough data to enroll the student, so
6756: # we need to pick up more information.
1.297 matthew 6757: my %tmp = &get('environment',
1.294 matthew 6758: ['firstname','middlename','lastname', 'generation','id']
1.297 matthew 6759: ,$udom,$uname);
6760:
1.800 albertel 6761: #foreach my $key (keys(%tmp)) {
6762: # &logthis("key $key = ".$tmp{$key});
1.455 albertel 6763: #}
1.294 matthew 6764: $first = $tmp{'firstname'} if (!defined($first) || $first eq '');
6765: $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
6766: $last = $tmp{'lastname'} if (!defined($last) || $last eq '');
1.297 matthew 6767: $gene = $tmp{'generation'} if (!defined($gene) || $gene eq '');
1.294 matthew 6768: $uid = $tmp{'id'} if (!defined($uid) || $uid eq '');
6769: }
1.556 albertel 6770: my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487 albertel 6771: my $reply=cput('classlist',
6772: {"$uname:$udom" =>
1.515 raeburn 6773: join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487 albertel 6774: $cdom,$cnum);
1.81 www 6775: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
6776: return 'error: '.$reply;
1.652 albertel 6777: } else {
6778: &devalidate_getsection_cache($udom,$uname,$cid);
1.81 www 6779: }
1.297 matthew 6780: # Add student role to user
1.83 www 6781: my $uurl='/'.$cid;
1.81 www 6782: $uurl=~s/\_/\//g;
6783: if ($usec) {
6784: $uurl.='/'.$usec;
6785: }
1.957 raeburn 6786: return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21 www 6787: }
6788:
1.556 albertel 6789: sub format_name {
6790: my ($firstname,$middlename,$lastname,$generation,$first)=@_;
6791: my $name;
6792: if ($first ne 'lastname') {
6793: $name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
6794: } else {
6795: if ($lastname=~/\S/) {
6796: $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
6797: $name=~s/\s+,/,/;
6798: } else {
6799: $name.= $firstname.' '.$middlename.' '.$generation;
6800: }
6801: }
6802: $name=~s/^\s+//;
6803: $name=~s/\s+$//;
6804: $name=~s/\s+/ /g;
6805: return $name;
6806: }
6807:
1.84 www 6808: # ------------------------------------------------- Write to course preferences
6809:
6810: sub writecoursepref {
6811: my ($courseid,%prefs)=@_;
6812: $courseid=~s/^\///;
6813: $courseid=~s/\_/\//g;
6814: my ($cdomain,$cnum)=split(/\//,$courseid);
6815: my $chome=homeserver($cnum,$cdomain);
6816: if (($chome eq '') || ($chome eq 'no_host')) {
6817: return 'error: no such course';
6818: }
6819: my $cstring='';
1.800 albertel 6820: foreach my $pref (keys(%prefs)) {
6821: $cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191 harris41 6822: }
1.84 www 6823: $cstring=~s/\&$//;
6824: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
6825: }
6826:
6827: # ---------------------------------------------------------- Make/modify course
6828:
6829: sub createcourse {
1.741 raeburn 6830: my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017 raeburn 6831: $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84 www 6832: $url=&declutter($url);
6833: my $cid='';
1.1028 raeburn 6834: if ($context eq 'requestcourses') {
6835: my $can_create = 0;
6836: my ($ownername,$ownerdom) = split(':',$course_owner);
6837: if ($udom eq $ownerdom) {
6838: if (&usertools_access($ownername,$ownerdom,$category,undef,
6839: $context)) {
6840: $can_create = 1;
6841: }
6842: } else {
6843: my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
6844: $category);
6845: if ($userenv{'reqcrsotherdom.'.$category} ne '') {
6846: my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
6847: if (@curr > 0) {
6848: my @options = qw(approval validate autolimit);
6849: my $optregex = join('|',@options);
6850: if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
6851: $can_create = 1;
6852: }
6853: }
6854: }
6855: }
6856: if ($can_create) {
6857: unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
6858: unless (&allowed('ccc',$udom)) {
6859: return 'refused';
6860: }
1.1017 raeburn 6861: }
6862: } else {
6863: return 'refused';
6864: }
1.1028 raeburn 6865: } elsif (!&allowed('ccc',$udom)) {
6866: return 'refused';
1.84 www 6867: }
1.1011 raeburn 6868: # --------------------------------------------------------------- Get Unique ID
6869: my $uname;
6870: if ($cnum =~ /^$match_courseid$/) {
6871: my $chome=&homeserver($cnum,$udom,'true');
6872: if (($chome eq '') || ($chome eq 'no_host')) {
6873: $uname = $cnum;
6874: } else {
1.1038 raeburn 6875: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 6876: }
6877: } else {
1.1038 raeburn 6878: $uname = &generate_coursenum($udom,$crstype);
1.1011 raeburn 6879: }
6880: return $uname if ($uname =~ /^error/);
6881: # -------------------------------------------------- Check supplied server name
1.1052 raeburn 6882: if (!defined($course_server)) {
6883: if (defined(&domain($udom,'primary'))) {
6884: $course_server = &domain($udom,'primary');
6885: } else {
6886: $course_server = $env{'user.home'};
6887: }
6888: }
6889: my %host_servers =
6890: &Apache::lonnet::get_servers($udom,'library');
6891: unless ($host_servers{$course_server}) {
6892: return 'error: invalid home server for course: '.$course_server;
1.264 matthew 6893: }
1.84 www 6894: # ------------------------------------------------------------- Make the course
6895: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264 matthew 6896: $course_server);
1.84 www 6897: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011 raeburn 6898: my $uhome=&homeserver($uname,$udom,'true');
1.84 www 6899: if (($uhome eq '') || ($uhome eq 'no_host')) {
6900: return 'error: no such course';
6901: }
1.271 www 6902: # ----------------------------------------------------------------- Course made
1.516 raeburn 6903: # log existence
1.1029 raeburn 6904: my $now = time;
1.918 raeburn 6905: my $newcourse = {
6906: $udom.'_'.$uname => {
1.921 raeburn 6907: description => $description,
6908: inst_code => $inst_code,
6909: owner => $course_owner,
6910: type => $crstype,
1.1029 raeburn 6911: creator => $env{'user.name'}.':'.
6912: $env{'user.domain'},
6913: created => $now,
6914: context => $context,
1.918 raeburn 6915: },
6916: };
1.921 raeburn 6917: &courseidput($udom,$newcourse,$uhome,'notime');
1.358 www 6918: # set toplevel url
1.271 www 6919: my $topurl=$url;
6920: unless ($nonstandard) {
6921: # ------------------------------------------ For standard courses, make top url
6922: my $mapurl=&clutter($url);
1.278 www 6923: if ($mapurl eq '/res/') { $mapurl=''; }
1.620 albertel 6924: $env{'form.initmap'}=(<<ENDINITMAP);
1.271 www 6925: <map>
6926: <resource id="1" type="start"></resource>
6927: <resource id="2" src="$mapurl"></resource>
6928: <resource id="3" type="finish"></resource>
6929: <link index="1" from="1" to="2"></link>
6930: <link index="2" from="2" to="3"></link>
6931: </map>
6932: ENDINITMAP
6933: $topurl=&declutter(
1.638 albertel 6934: &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271 www 6935: );
6936: }
6937: # ----------------------------------------------------------- Write preferences
1.84 www 6938: &writecoursepref($udom.'_'.$uname,
1.1056 raeburn 6939: ('description' => $description,
6940: 'url' => $topurl,
6941: 'internal.creator' => $env{'user.name'}.':'.
6942: $env{'user.domain'},
6943: 'internal.created' => $now,
6944: 'internal.creationcontext' => $context)
6945: );
1.84 www 6946: return '/'.$udom.'/'.$uname;
6947: }
6948:
1.1011 raeburn 6949: # ------------------------------------------------------------------- Create ID
6950: sub generate_coursenum {
1.1038 raeburn 6951: my ($udom,$crstype) = @_;
1.1011 raeburn 6952: my $domdesc = &domain($udom);
6953: return 'error: invalid domain' if ($domdesc eq '');
1.1038 raeburn 6954: my $first;
6955: if ($crstype eq 'Community') {
6956: $first = '0';
6957: } else {
6958: $first = int(1+rand(9));
6959: }
6960: my $uname=$first.
1.1011 raeburn 6961: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
6962: substr($$.time,0,5).unpack("H8",pack("I32",time)).
6963: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
6964: # ----------------------------------------------- Make sure that does not exist
6965: my $uhome=&homeserver($uname,$udom,'true');
6966: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038 raeburn 6967: if ($crstype eq 'Community') {
6968: $first = '0';
6969: } else {
6970: $first = int(1+rand(9));
6971: }
6972: $uname=$first.
1.1011 raeburn 6973: ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
6974: substr($$.time,0,5).unpack("H8",pack("I32",time)).
6975: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
6976: $uhome=&homeserver($uname,$udom,'true');
6977: unless (($uhome eq '') || ($uhome eq 'no_host')) {
6978: return 'error: unable to generate unique course-ID';
6979: }
6980: }
6981: return $uname;
6982: }
6983:
1.813 albertel 6984: sub is_course {
6985: my ($cdom,$cnum) = @_;
6986: my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946 raeburn 6987: undef,'.');
1.813 albertel 6988: if (exists($courses{$cdom.'_'.$cnum})) {
6989: return 1;
6990: }
6991: return 0;
6992: }
6993:
1.1015 raeburn 6994: sub store_userdata {
6995: my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013 raeburn 6996: my $result;
1.1016 raeburn 6997: if ($datakey ne '') {
1.1013 raeburn 6998: if (ref($storehash) eq 'HASH') {
1.1017 raeburn 6999: if ($udom eq '' || $uname eq '') {
7000: $udom = $env{'user.domain'};
7001: $uname = $env{'user.name'};
7002: }
7003: my $uhome=&homeserver($uname,$udom);
1.1013 raeburn 7004: if (($uhome eq '') || ($uhome eq 'no_host')) {
7005: $result = 'error: no_host';
7006: } else {
7007: $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
7008: $storehash->{'host'} = $perlvar{'lonHostID'};
7009:
7010: my $namevalue='';
7011: foreach my $key (keys(%{$storehash})) {
7012: $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
7013: }
7014: $namevalue=~s/\&$//;
7015: $result = &reply("store:$env{'user.domain'}:$env{'user.name'}:".
1.1015 raeburn 7016: "$namespace:$datakey:$namevalue",$uhome);
1.1013 raeburn 7017: }
7018: } else {
7019: $result = 'error: data to store was not a hash reference';
7020: }
7021: } else {
7022: $result= 'error: invalid requestkey';
7023: }
7024: return $result;
7025: }
7026:
1.21 www 7027: # ---------------------------------------------------------- Assign Custom Role
7028:
7029: sub assigncustomrole {
1.957 raeburn 7030: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7031: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957 raeburn 7032: $end,$start,$deleteflag,$selfenroll,$context);
1.21 www 7033: }
7034:
7035: # ----------------------------------------------------------------- Revoke Role
7036:
7037: sub revokerole {
1.957 raeburn 7038: my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7039: my $now=time;
1.965 raeburn 7040: return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21 www 7041: }
7042:
7043: # ---------------------------------------------------------- Revoke Custom Role
7044:
7045: sub revokecustomrole {
1.957 raeburn 7046: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21 www 7047: my $now=time;
1.357 www 7048: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957 raeburn 7049: $deleteflag,$selfenroll,$context);
1.17 www 7050: }
7051:
1.533 banghart 7052: # ------------------------------------------------------------ Disk usage
1.535 albertel 7053: sub diskusage {
1.955 raeburn 7054: my ($udom,$uname,$directorypath,$getpropath)=@_;
7055: $directorypath =~ s/\/$//;
7056: my $listing=&reply('du2:'.&escape($directorypath).':'
7057: .&escape($getpropath).':'.&escape($uname).':'
7058: .&escape($udom),homeserver($uname,$udom));
7059: if ($listing eq 'unknown_cmd') {
7060: if ($getpropath) {
7061: $directorypath = &propath($udom,$uname).'/'.$directorypath;
7062: }
7063: $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
7064: }
1.514 albertel 7065: return $listing;
1.512 banghart 7066: }
7067:
1.566 banghart 7068: sub is_locked {
7069: my ($file_name, $domain, $user) = @_;
7070: my @check;
7071: my $is_locked;
7072: push @check, $file_name;
1.613 albertel 7073: my %locked = &get('file_permissions',\@check,
1.620 albertel 7074: $env{'user.domain'},$env{'user.name'});
1.615 albertel 7075: my ($tmp)=keys(%locked);
7076: if ($tmp=~/^error:/) { undef(%locked); }
1.745 raeburn 7077:
1.566 banghart 7078: if (ref($locked{$file_name}) eq 'ARRAY') {
1.745 raeburn 7079: $is_locked = 'false';
7080: foreach my $entry (@{$locked{$file_name}}) {
7081: if (ref($entry) eq 'ARRAY') {
1.746 raeburn 7082: $is_locked = 'true';
7083: last;
1.745 raeburn 7084: }
7085: }
1.566 banghart 7086: } else {
7087: $is_locked = 'false';
7088: }
7089: }
7090:
1.759 albertel 7091: sub declutter_portfile {
7092: my ($file) = @_;
1.833 albertel 7093: $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759 albertel 7094: return $file;
7095: }
7096:
1.559 banghart 7097: # ------------------------------------------------------------- Mark as Read Only
7098:
7099: sub mark_as_readonly {
7100: my ($domain,$user,$files,$what) = @_;
1.613 albertel 7101: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7102: my ($tmp)=keys(%current_permissions);
7103: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560 banghart 7104: foreach my $file (@{$files}) {
1.759 albertel 7105: $file = &declutter_portfile($file);
1.561 banghart 7106: push(@{$current_permissions{$file}},$what);
1.559 banghart 7107: }
1.613 albertel 7108: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7109: return;
7110: }
7111:
1.572 banghart 7112: # ------------------------------------------------------------Save Selected Files
7113:
7114: sub save_selected_files {
7115: my ($user, $path, @files) = @_;
7116: my $filename = $user."savedfiles";
1.573 banghart 7117: my @other_files = &files_not_in_path($user, $path);
1.871 albertel 7118: open (OUT, '>'.$tmpdir.$filename);
1.573 banghart 7119: foreach my $file (@files) {
1.620 albertel 7120: print (OUT $env{'form.currentpath'}.$file."\n");
1.573 banghart 7121: }
7122: foreach my $file (@other_files) {
1.574 banghart 7123: print (OUT $file."\n");
1.572 banghart 7124: }
1.574 banghart 7125: close (OUT);
1.572 banghart 7126: return 'ok';
7127: }
7128:
1.574 banghart 7129: sub clear_selected_files {
7130: my ($user) = @_;
7131: my $filename = $user."savedfiles";
7132: open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7133: print (OUT undef);
7134: close (OUT);
7135: return ("ok");
7136: }
7137:
1.572 banghart 7138: sub files_in_path {
7139: my ($user, $path) = @_;
7140: my $filename = $user."savedfiles";
7141: my %return_files;
1.574 banghart 7142: open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573 banghart 7143: while (my $line_in = <IN>) {
1.574 banghart 7144: chomp ($line_in);
7145: my @paths_and_file = split (m!/!, $line_in);
7146: my $file_part = pop (@paths_and_file);
7147: my $path_part = join ('/', @paths_and_file);
1.573 banghart 7148: $path_part.='/';
7149: my $path_and_file = $path_part.$file_part;
7150: if ($path_part eq $path) {
7151: $return_files{$file_part}= 'selected';
7152: }
7153: }
1.574 banghart 7154: close (IN);
7155: return (\%return_files);
1.572 banghart 7156: }
7157:
7158: # called in portfolio select mode, to show files selected NOT in current directory
7159: sub files_not_in_path {
7160: my ($user, $path) = @_;
7161: my $filename = $user."savedfiles";
7162: my @return_files;
7163: my $path_part;
1.800 albertel 7164: open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
7165: while (my $line = <IN>) {
1.572 banghart 7166: #ok, I know it's clunky, but I want it to work
1.800 albertel 7167: my @paths_and_file = split(m|/|, $line);
7168: my $file_part = pop(@paths_and_file);
7169: chomp($file_part);
7170: my $path_part = join('/', @paths_and_file);
1.572 banghart 7171: $path_part .= '/';
7172: my $path_and_file = $path_part.$file_part;
7173: if ($path_part ne $path) {
1.800 albertel 7174: push(@return_files, ($path_and_file));
1.572 banghart 7175: }
7176: }
1.800 albertel 7177: close(OUT);
1.574 banghart 7178: return (@return_files);
1.572 banghart 7179: }
7180:
1.745 raeburn 7181: #----------------------------------------------Get portfolio file permissions
1.629 banghart 7182:
1.745 raeburn 7183: sub get_portfile_permissions {
7184: my ($domain,$user) = @_;
1.613 albertel 7185: my %current_permissions = &dump('file_permissions',$domain,$user);
1.615 albertel 7186: my ($tmp)=keys(%current_permissions);
7187: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7188: return \%current_permissions;
7189: }
7190:
7191: #---------------------------------------------Get portfolio file access controls
7192:
1.749 raeburn 7193: sub get_access_controls {
1.745 raeburn 7194: my ($current_permissions,$group,$file) = @_;
1.769 albertel 7195: my %access;
7196: my $real_file = $file;
7197: $file =~ s/\.meta$//;
1.745 raeburn 7198: if (defined($file)) {
1.749 raeburn 7199: if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
7200: foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769 albertel 7201: $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749 raeburn 7202: }
7203: }
1.745 raeburn 7204: } else {
1.749 raeburn 7205: foreach my $key (keys(%{$current_permissions})) {
7206: if ($key =~ /\0accesscontrol$/) {
7207: if (defined($group)) {
7208: if ($key !~ m-^\Q$group\E/-) {
7209: next;
7210: }
7211: }
7212: my ($fullpath) = split(/\0/,$key);
7213: if (ref($$current_permissions{$key}) eq 'HASH') {
7214: foreach my $control (keys(%{$$current_permissions{$key}})) {
7215: $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
7216: }
7217: }
7218: }
7219: }
7220: }
7221: return %access;
7222: }
7223:
7224: sub modify_access_controls {
7225: my ($file_name,$changes,$domain,$user)=@_;
7226: my ($outcome,$deloutcome);
7227: my %store_permissions;
7228: my %new_values;
7229: my %new_control;
7230: my %translation;
7231: my @deletions = ();
7232: my $now = time;
7233: if (exists($$changes{'activate'})) {
7234: if (ref($$changes{'activate'}) eq 'HASH') {
7235: my @newitems = sort(keys(%{$$changes{'activate'}}));
7236: my $numnew = scalar(@newitems);
7237: for (my $i=0; $i<$numnew; $i++) {
7238: my $newkey = $newitems[$i];
7239: my $newid = &Apache::loncommon::get_cgi_id();
1.797 raeburn 7240: if ($newkey =~ /^\d+:/) {
7241: $newkey =~ s/^(\d+)/$newid/;
7242: $translation{$1} = $newid;
7243: } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
7244: $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
7245: $translation{$1} = $newid;
7246: }
1.749 raeburn 7247: $new_values{$file_name."\0".$newkey} =
7248: $$changes{'activate'}{$newitems[$i]};
7249: $new_control{$newkey} = $now;
7250: }
7251: }
7252: }
7253: my %todelete;
7254: my %changed_items;
7255: foreach my $action ('delete','update') {
7256: if (exists($$changes{$action})) {
7257: if (ref($$changes{$action}) eq 'HASH') {
7258: foreach my $key (keys(%{$$changes{$action}})) {
7259: my ($itemnum) = ($key =~ /^([^:]+):/);
7260: if ($action eq 'delete') {
7261: $todelete{$itemnum} = 1;
7262: } else {
7263: $changed_items{$itemnum} = $key;
7264: }
7265: }
1.745 raeburn 7266: }
7267: }
1.749 raeburn 7268: }
7269: # get lock on access controls for file.
7270: my $lockhash = {
7271: $file_name."\0".'locked_access_records' => $env{'user.name'}.
7272: ':'.$env{'user.domain'},
7273: };
7274: my $tries = 0;
7275: my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7276:
7277: while (($gotlock ne 'ok') && $tries <3) {
7278: $tries ++;
7279: sleep 1;
7280: $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
7281: }
7282: if ($gotlock eq 'ok') {
7283: my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
7284: my ($tmp)=keys(%curr_permissions);
7285: if ($tmp=~/^error:/) { undef(%curr_permissions); }
7286: if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
7287: my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
7288: if (ref($curr_controls) eq 'HASH') {
7289: foreach my $control_item (keys(%{$curr_controls})) {
7290: my ($itemnum) = ($control_item =~ /^([^:]+):/);
7291: if (defined($todelete{$itemnum})) {
7292: push(@deletions,$file_name."\0".$control_item);
7293: } else {
7294: if (defined($changed_items{$itemnum})) {
7295: $new_control{$changed_items{$itemnum}} = $now;
7296: push(@deletions,$file_name."\0".$control_item);
7297: $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
7298: } else {
7299: $new_control{$control_item} = $$curr_controls{$control_item};
7300: }
7301: }
1.745 raeburn 7302: }
7303: }
7304: }
1.970 raeburn 7305: my ($group);
7306: if (&is_course($domain,$user)) {
7307: ($group,my $file) = split(/\//,$file_name,2);
7308: }
1.749 raeburn 7309: $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
7310: $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
7311: $outcome = &put('file_permissions',\%new_values,$domain,$user);
7312: # remove lock
7313: my @del_lock = ($file_name."\0".'locked_access_records');
7314: my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818 raeburn 7315: my $sqlresult =
1.970 raeburn 7316: &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818 raeburn 7317: $group);
1.749 raeburn 7318: } else {
7319: $outcome = "error: could not obtain lockfile\n";
1.745 raeburn 7320: }
1.749 raeburn 7321: return ($outcome,$deloutcome,\%new_values,\%translation);
1.745 raeburn 7322: }
7323:
1.827 raeburn 7324: sub make_public_indefinitely {
7325: my ($requrl) = @_;
7326: my $now = time;
7327: my $action = 'activate';
7328: my $aclnum = 0;
7329: if (&is_portfolio_url($requrl)) {
7330: my (undef,$udom,$unum,$file_name,$group) =
7331: &parse_portfolio_url($requrl);
7332: my $current_perms = &get_portfile_permissions($udom,$unum);
7333: my %access_controls = &get_access_controls($current_perms,
7334: $group,$file_name);
7335: foreach my $key (keys(%{$access_controls{$file_name}})) {
7336: my ($num,$scope,$end,$start) =
7337: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
7338: if ($scope eq 'public') {
7339: if ($start <= $now && $end == 0) {
7340: $action = 'none';
7341: } else {
7342: $action = 'update';
7343: $aclnum = $num;
7344: }
7345: last;
7346: }
7347: }
7348: if ($action eq 'none') {
7349: return 'ok';
7350: } else {
7351: my %changes;
7352: my $newend = 0;
7353: my $newstart = $now;
7354: my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
7355: $changes{$action}{$newkey} = {
7356: type => 'public',
7357: time => {
7358: start => $newstart,
7359: end => $newend,
7360: },
7361: };
7362: my ($outcome,$deloutcome,$new_values,$translation) =
7363: &modify_access_controls($file_name,\%changes,$udom,$unum);
7364: return $outcome;
7365: }
7366: } else {
7367: return 'invalid';
7368: }
7369: }
7370:
1.745 raeburn 7371: #------------------------------------------------------Get Marked as Read Only
7372:
7373: sub get_marked_as_readonly {
7374: my ($domain,$user,$what,$group) = @_;
7375: my $current_permissions = &get_portfile_permissions($domain,$user);
1.563 banghart 7376: my @readonly_files;
1.629 banghart 7377: my $cmp1=$what;
7378: if (ref($what)) { $cmp1=join('',@{$what}) };
1.745 raeburn 7379: while (my ($file_name,$value) = each(%{$current_permissions})) {
7380: if (defined($group)) {
7381: if ($file_name !~ m-^\Q$group\E/-) {
7382: next;
7383: }
7384: }
1.561 banghart 7385: if (ref($value) eq "ARRAY"){
7386: foreach my $stored_what (@{$value}) {
1.629 banghart 7387: my $cmp2=$stored_what;
1.759 albertel 7388: if (ref($stored_what) eq 'ARRAY') {
1.746 raeburn 7389: $cmp2=join('',@{$stored_what});
1.745 raeburn 7390: }
1.629 banghart 7391: if ($cmp1 eq $cmp2) {
1.561 banghart 7392: push(@readonly_files, $file_name);
1.745 raeburn 7393: last;
1.563 banghart 7394: } elsif (!defined($what)) {
7395: push(@readonly_files, $file_name);
1.745 raeburn 7396: last;
1.561 banghart 7397: }
7398: }
1.745 raeburn 7399: }
1.561 banghart 7400: }
7401: return @readonly_files;
7402: }
1.577 banghart 7403: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561 banghart 7404:
1.577 banghart 7405: sub get_marked_as_readonly_hash {
1.745 raeburn 7406: my ($current_permissions,$group,$what) = @_;
1.577 banghart 7407: my %readonly_files;
1.745 raeburn 7408: while (my ($file_name,$value) = each(%{$current_permissions})) {
7409: if (defined($group)) {
7410: if ($file_name !~ m-^\Q$group\E/-) {
7411: next;
7412: }
7413: }
1.577 banghart 7414: if (ref($value) eq "ARRAY"){
7415: foreach my $stored_what (@{$value}) {
1.745 raeburn 7416: if (ref($stored_what) eq 'ARRAY') {
1.750 banghart 7417: foreach my $lock_descriptor(@{$stored_what}) {
7418: if ($lock_descriptor eq 'graded') {
7419: $readonly_files{$file_name} = 'graded';
7420: } elsif ($lock_descriptor eq 'handback') {
7421: $readonly_files{$file_name} = 'handback';
7422: } else {
7423: if (!exists($readonly_files{$file_name})) {
7424: $readonly_files{$file_name} = 'locked';
7425: }
7426: }
1.745 raeburn 7427: }
1.750 banghart 7428: }
1.577 banghart 7429: }
7430: }
7431: }
7432: return %readonly_files;
7433: }
1.559 banghart 7434: # ------------------------------------------------------------ Unmark as Read Only
7435:
7436: sub unmark_as_readonly {
1.629 banghart 7437: # unmarks $file_name (if $file_name is defined), or all files locked by $what
7438: # for portfolio submissions, $what contains [$symb,$crsid]
1.745 raeburn 7439: my ($domain,$user,$what,$file_name,$group) = @_;
1.759 albertel 7440: $file_name = &declutter_portfile($file_name);
1.634 albertel 7441: my $symb_crs = $what;
7442: if (ref($what)) { $symb_crs=join('',@$what); }
1.745 raeburn 7443: my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615 albertel 7444: my ($tmp)=keys(%current_permissions);
7445: if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745 raeburn 7446: my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650 albertel 7447: foreach my $file (@readonly_files) {
1.759 albertel 7448: my $clean_file = &declutter_portfile($file);
7449: if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650 albertel 7450: my $current_locks = $current_permissions{$file};
1.563 banghart 7451: my @new_locks;
7452: my @del_keys;
7453: if (ref($current_locks) eq "ARRAY"){
7454: foreach my $locker (@{$current_locks}) {
1.632 albertel 7455: my $compare=$locker;
1.749 raeburn 7456: if (ref($locker) eq 'ARRAY') {
1.745 raeburn 7457: $compare=join('',@{$locker});
1.746 raeburn 7458: if ($compare ne $symb_crs) {
7459: push(@new_locks, $locker);
7460: }
1.563 banghart 7461: }
7462: }
1.650 albertel 7463: if (scalar(@new_locks) > 0) {
1.563 banghart 7464: $current_permissions{$file} = \@new_locks;
7465: } else {
7466: push(@del_keys, $file);
1.613 albertel 7467: &del('file_permissions',\@del_keys, $domain, $user);
1.650 albertel 7468: delete($current_permissions{$file});
1.563 banghart 7469: }
7470: }
1.561 banghart 7471: }
1.613 albertel 7472: &put('file_permissions',\%current_permissions,$domain,$user);
1.559 banghart 7473: return;
7474: }
1.512 banghart 7475:
1.17 www 7476: # ------------------------------------------------------------ Directory lister
7477:
7478: sub dirlist {
1.955 raeburn 7479: my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18 www 7480: $uri=~s/^\///;
7481: $uri=~s/\/$//;
1.253 stredwic 7482: my ($udom, $uname);
1.955 raeburn 7483: if ($getuserdir) {
1.253 stredwic 7484: $udom = $userdomain;
7485: $uname = $username;
1.955 raeburn 7486: } else {
7487: (undef,$udom,$uname)=split(/\//,$uri);
7488: if(defined($userdomain)) {
7489: $udom = $userdomain;
7490: }
7491: if(defined($username)) {
7492: $uname = $username;
7493: }
1.253 stredwic 7494: }
1.955 raeburn 7495: my ($dirRoot,$listing,@listing_results);
1.253 stredwic 7496:
1.955 raeburn 7497: $dirRoot = $perlvar{'lonDocRoot'};
7498: if (defined($getpropath)) {
7499: $dirRoot = &propath($udom,$uname);
1.253 stredwic 7500: $dirRoot =~ s/\/$//;
1.955 raeburn 7501: } elsif (defined($getuserdir)) {
7502: my $subdir=$uname.'__';
7503: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
7504: $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
7505: ."/$udom/$subdir/$uname";
7506: } elsif (defined($alternateRoot)) {
7507: $dirRoot = $alternateRoot;
1.751 banghart 7508: }
1.253 stredwic 7509:
7510: if($udom) {
7511: if($uname) {
1.955 raeburn 7512: $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956 raeburn 7513: .$getuserdir.':'.&escape($dirRoot)
1.955 raeburn 7514: .':'.&escape($uname).':'.&escape($udom),
7515: &homeserver($uname,$udom));
7516: if ($listing eq 'unknown_cmd') {
7517: $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
7518: &homeserver($uname,$udom));
7519: } else {
7520: @listing_results = map { &unescape($_); } split(/:/,$listing);
7521: }
1.605 matthew 7522: if ($listing eq 'unknown_cmd') {
1.800 albertel 7523: $listing = &reply('ls:'.$dirRoot.'/'.$uri,
7524: &homeserver($uname,$udom));
1.605 matthew 7525: @listing_results = split(/:/,$listing);
7526: } else {
7527: @listing_results = map { &unescape($_); } split(/:/,$listing);
7528: }
7529: return @listing_results;
1.955 raeburn 7530: } elsif(!$alternateRoot) {
1.800 albertel 7531: my %allusers;
1.841 albertel 7532: my %servers = &get_servers($udom,'library');
1.955 raeburn 7533: foreach my $tryserver (keys(%servers)) {
7534: $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
7535: &escape($udom),$tryserver);
7536: if ($listing eq 'unknown_cmd') {
7537: $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
7538: $udom, $tryserver);
7539: } else {
7540: @listing_results = map { &unescape($_); } split(/:/,$listing);
7541: }
1.841 albertel 7542: if ($listing eq 'unknown_cmd') {
7543: $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
7544: $udom, $tryserver);
7545: @listing_results = split(/:/,$listing);
7546: } else {
7547: @listing_results =
7548: map { &unescape($_); } split(/:/,$listing);
7549: }
7550: if ($listing_results[0] ne 'no_such_dir' &&
7551: $listing_results[0] ne 'empty' &&
7552: $listing_results[0] ne 'con_lost') {
7553: foreach my $line (@listing_results) {
7554: my ($entry) = split(/&/,$line,2);
7555: $allusers{$entry} = 1;
7556: }
7557: }
1.253 stredwic 7558: }
7559: my $alluserstr='';
1.800 albertel 7560: foreach my $user (sort(keys(%allusers))) {
7561: $alluserstr.=$user.'&user:';
1.253 stredwic 7562: }
7563: $alluserstr=~s/:$//;
7564: return split(/:/,$alluserstr);
7565: } else {
1.800 albertel 7566: return ('missing user name');
1.253 stredwic 7567: }
1.955 raeburn 7568: } elsif(!defined($getpropath)) {
1.841 albertel 7569: my @all_domains = sort(&all_domains());
1.955 raeburn 7570: foreach my $domain (@all_domains) {
7571: $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
7572: }
7573: return @all_domains;
7574: } else {
1.800 albertel 7575: return ('missing domain');
1.275 stredwic 7576: }
7577: }
7578:
7579: # --------------------------------------------- GetFileTimestamp
7580: # This function utilizes dirlist and returns the date stamp for
7581: # when it was last modified. It will also return an error of -1
7582: # if an error occurs
7583:
7584: sub GetFileTimestamp {
1.955 raeburn 7585: my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807 albertel 7586: $studentDomain = &LONCAPA::clean_domain($studentDomain);
7587: $studentName = &LONCAPA::clean_username($studentName);
1.955 raeburn 7588: my ($fileStat) =
7589: &Apache::lonnet::dirlist($filename,$studentDomain,$studentName,
7590: undef,$getuserdir);
1.275 stredwic 7591: my @stats = split('&', $fileStat);
7592: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375 matthew 7593: # @stats contains first the filename, then the stat output
7594: return $stats[10]; # so this is 10 instead of 9.
1.275 stredwic 7595: } else {
7596: return -1;
1.253 stredwic 7597: }
1.26 www 7598: }
7599:
1.712 albertel 7600: sub stat_file {
7601: my ($uri) = @_;
1.787 albertel 7602: $uri = &clutter_with_no_wrapper($uri);
1.722 albertel 7603:
1.955 raeburn 7604: my ($udom,$uname,$file);
1.712 albertel 7605: if ($uri =~ m-^/(uploaded|editupload)/-) {
7606: ($udom,$uname,$file) =
1.811 albertel 7607: ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712 albertel 7608: $file = 'userfiles/'.$file;
7609: }
7610: if ($uri =~ m-^/res/-) {
7611: ($udom,$uname) =
1.807 albertel 7612: ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712 albertel 7613: $file = $uri;
7614: }
7615:
7616: if (!$udom || !$uname || !$file) {
7617: # unable to handle the uri
7618: return ();
7619: }
1.956 raeburn 7620: my $getpropath;
7621: if ($file =~ /^userfiles\//) {
7622: $getpropath = 1;
7623: }
1.955 raeburn 7624: my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712 albertel 7625: my @stats = split('&', $result);
1.721 banghart 7626:
1.712 albertel 7627: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
7628: shift(@stats); #filename is first
7629: return @stats;
7630: }
7631: return ();
7632: }
7633:
1.26 www 7634: # -------------------------------------------------------- Value of a Condition
7635:
1.713 albertel 7636: # gets the value of a specific preevaluated condition
7637: # stored in the string $env{user.state.<cid>}
7638: # or looks up a condition reference in the bighash and if if hasn't
7639: # already been evaluated recurses into docondval to get the value of
7640: # the condition, then memoizing it to
7641: # $env{user.state.<cid>.<condition>}
1.40 www 7642: sub directcondval {
7643: my $number=shift;
1.620 albertel 7644: if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555 albertel 7645: &Apache::lonuserstate::evalstate();
7646: }
1.713 albertel 7647: if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
7648: return $env{'user.state.'.$env{'request.course.id'}.".$number"};
7649: } elsif ($number =~ /^_/) {
7650: my $sub_condition;
7651: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
7652: &GDBM_READER(),0640)) {
7653: $sub_condition=$bighash{'conditions'.$number};
7654: untie(%bighash);
7655: }
7656: my $value = &docondval($sub_condition);
1.949 raeburn 7657: &appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713 albertel 7658: return $value;
7659: }
1.620 albertel 7660: if ($env{'user.state.'.$env{'request.course.id'}}) {
7661: return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40 www 7662: } else {
7663: return 2;
7664: }
7665: }
7666:
1.713 albertel 7667: # get the collection of conditions for this resource
1.26 www 7668: sub condval {
7669: my $condidx=shift;
1.54 www 7670: my $allpathcond='';
1.713 albertel 7671: foreach my $cond (split(/\|/,$condidx)) {
7672: if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
7673: $allpathcond.=
7674: '('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
7675: }
1.191 harris41 7676: }
1.54 www 7677: $allpathcond=~s/\|$//;
1.713 albertel 7678: return &docondval($allpathcond);
7679: }
7680:
7681: #evaluates an expression of conditions
7682: sub docondval {
7683: my ($allpathcond) = @_;
7684: my $result=0;
7685: if ($env{'request.course.id'}
7686: && defined($allpathcond)) {
7687: my $operand='|';
7688: my @stack;
7689: foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
7690: if ($chunk eq '(') {
7691: push @stack,($operand,$result);
7692: } elsif ($chunk eq ')') {
7693: my $before=pop @stack;
7694: if (pop @stack eq '&') {
7695: $result=$result>$before?$before:$result;
7696: } else {
7697: $result=$result>$before?$result:$before;
7698: }
7699: } elsif (($chunk eq '&') || ($chunk eq '|')) {
7700: $operand=$chunk;
7701: } else {
7702: my $new=directcondval($chunk);
7703: if ($operand eq '&') {
7704: $result=$result>$new?$new:$result;
7705: } else {
7706: $result=$result>$new?$result:$new;
7707: }
7708: }
7709: }
1.26 www 7710: }
7711: return $result;
1.421 albertel 7712: }
7713:
7714: # ---------------------------------------------------- Devalidate courseresdata
7715:
7716: sub devalidatecourseresdata {
7717: my ($coursenum,$coursedomain)=@_;
7718: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 7719: &devalidate_cache_new('courseres',$hashid);
1.28 www 7720: }
7721:
1.763 www 7722:
1.200 www 7723: # --------------------------------------------------- Course Resourcedata Query
1.878 foxr 7724: #
7725: # Parameters:
7726: # $coursenum - Number of the course.
7727: # $coursedomain - Domain at which the course was created.
7728: # Returns:
7729: # A hash of the course parameters along (I think) with timestamps
7730: # and version info.
1.877 foxr 7731:
1.624 albertel 7732: sub get_courseresdata {
7733: my ($coursenum,$coursedomain)=@_;
1.200 www 7734: my $coursehom=&homeserver($coursenum,$coursedomain);
7735: my $hashid=$coursenum.':'.$coursedomain;
1.599 albertel 7736: my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624 albertel 7737: my %dumpreply;
1.417 albertel 7738: unless (defined($cached)) {
1.624 albertel 7739: %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417 albertel 7740: $result=\%dumpreply;
1.251 albertel 7741: my ($tmp) = keys(%dumpreply);
7742: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599 albertel 7743: &do_cache_new('courseres',$hashid,$result,600);
1.306 albertel 7744: } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
7745: return $tmp;
1.416 albertel 7746: } elsif ($tmp =~ /^(error)/) {
1.417 albertel 7747: $result=undef;
1.599 albertel 7748: &do_cache_new('courseres',$hashid,$result,600);
1.250 albertel 7749: }
7750: }
1.624 albertel 7751: return $result;
7752: }
7753:
1.633 albertel 7754: sub devalidateuserresdata {
7755: my ($uname,$udom)=@_;
7756: my $hashid="$udom:$uname";
7757: &devalidate_cache_new('userres',$hashid);
7758: }
7759:
1.624 albertel 7760: sub get_userresdata {
7761: my ($uname,$udom)=@_;
7762: #most student don\'t have any data set, check if there is some data
7763: if (&EXT_cache_status($udom,$uname)) { return undef; }
7764:
7765: my $hashid="$udom:$uname";
7766: my ($result,$cached)=&is_cached_new('userres',$hashid);
7767: if (!defined($cached)) {
7768: my %resourcedata=&dump('resourcedata',$udom,$uname);
7769: $result=\%resourcedata;
7770: &do_cache_new('userres',$hashid,$result,600);
7771: }
7772: my ($tmp)=keys(%$result);
7773: if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
7774: return $result;
7775: }
7776: #error 2 occurs when the .db doesn't exist
7777: if ($tmp!~/error: 2 /) {
1.672 albertel 7778: &logthis("<font color=\"blue\">WARNING:".
1.624 albertel 7779: " Trying to get resource data for ".
7780: $uname." at ".$udom.": ".
7781: $tmp."</font>");
7782: } elsif ($tmp=~/error: 2 /) {
1.633 albertel 7783: #&EXT_cache_set($udom,$uname);
7784: &do_cache_new('userres',$hashid,undef,600);
1.636 albertel 7785: undef($tmp); # not really an error so don't send it back
1.624 albertel 7786: }
7787: return $tmp;
7788: }
1.879 foxr 7789: #----------------------------------------------- resdata - return resource data
7790: # Purpose:
7791: # Return resource data for either users or for a course.
7792: # Parameters:
7793: # $name - Course/user name.
7794: # $domain - Name of the domain the user/course is registered on.
7795: # $type - Type of thing $name is (must be 'course' or 'user'
7796: # @which - Array of names of resources desired.
7797: # Returns:
7798: # The value of the first reasource in @which that is found in the
7799: # resource hash.
7800: # Exceptional Conditions:
7801: # If the $type passed in is not valid (not the string 'course' or
7802: # 'user', an undefined reference is returned.
7803: # If none of the resources are found, an undef is returned
1.624 albertel 7804: sub resdata {
7805: my ($name,$domain,$type,@which)=@_;
7806: my $result;
7807: if ($type eq 'course') {
7808: $result=&get_courseresdata($name,$domain);
7809: } elsif ($type eq 'user') {
7810: $result=&get_userresdata($name,$domain);
7811: }
7812: if (!ref($result)) { return $result; }
1.251 albertel 7813: foreach my $item (@which) {
1.927 albertel 7814: if (defined($result->{$item->[0]})) {
7815: return [$result->{$item->[0]},$item->[1]];
1.251 albertel 7816: }
1.250 albertel 7817: }
1.291 albertel 7818: return undef;
1.200 www 7819: }
7820:
1.379 matthew 7821: #
7822: # EXT resource caching routines
7823: #
7824:
7825: sub clear_EXT_cache_status {
1.383 albertel 7826: &delenv('cache.EXT.');
1.379 matthew 7827: }
7828:
7829: sub EXT_cache_status {
7830: my ($target_domain,$target_user) = @_;
1.383 albertel 7831: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620 albertel 7832: if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379 matthew 7833: # We know already the user has no data
7834: return 1;
7835: } else {
7836: return 0;
7837: }
7838: }
7839:
7840: sub EXT_cache_set {
7841: my ($target_domain,$target_user) = @_;
1.383 albertel 7842: my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949 raeburn 7843: #&appenv({$cachename => time});
1.379 matthew 7844: }
7845:
1.28 www 7846: # --------------------------------------------------------- Value of a Variable
1.58 www 7847: sub EXT {
1.715 albertel 7848:
1.395 albertel 7849: my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68 www 7850: unless ($varname) { return ''; }
1.218 albertel 7851: #get real user name/domain, courseid and symb
7852: my $courseid;
1.359 albertel 7853: my $publicuser;
1.427 www 7854: if ($symbparm) {
7855: $symbparm=&get_symb_from_alias($symbparm);
7856: }
1.218 albertel 7857: if (!($uname && $udom)) {
1.790 albertel 7858: (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218 albertel 7859: if (!$symbparm) { $symbparm=$cursymb; }
7860: } else {
1.620 albertel 7861: $courseid=$env{'request.course.id'};
1.218 albertel 7862: }
1.48 www 7863: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
7864: my $rest;
1.320 albertel 7865: if (defined($therest[0])) {
1.48 www 7866: $rest=join('.',@therest);
7867: } else {
7868: $rest='';
7869: }
1.320 albertel 7870:
1.57 www 7871: my $qualifierrest=$qualifier;
7872: if ($rest) { $qualifierrest.='.'.$rest; }
7873: my $spacequalifierrest=$space;
7874: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 7875: if ($realm eq 'user') {
1.48 www 7876: # --------------------------------------------------------------- user.resource
7877: if ($space eq 'resource') {
1.651 albertel 7878: if ( (defined($Apache::lonhomework::parsing_a_problem)
7879: || defined($Apache::lonhomework::parsing_a_task))
7880: &&
1.744 albertel 7881: ($symbparm eq &symbread()) ) {
7882: # if we are in the middle of processing the resource the
7883: # get the value we are planning on committing
7884: if (defined($Apache::lonhomework::results{$qualifierrest})) {
7885: return $Apache::lonhomework::results{$qualifierrest};
7886: } else {
7887: return $Apache::lonhomework::history{$qualifierrest};
7888: }
1.335 albertel 7889: } else {
1.359 albertel 7890: my %restored;
1.620 albertel 7891: if ($publicuser || $env{'request.state'} eq 'construct') {
1.359 albertel 7892: %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
7893: } else {
7894: %restored=&restore($symbparm,$courseid,$udom,$uname);
7895: }
1.335 albertel 7896: return $restored{$qualifierrest};
7897: }
1.48 www 7898: # ----------------------------------------------------------------- user.access
7899: } elsif ($space eq 'access') {
1.218 albertel 7900: # FIXME - not supporting calls for a specific user
1.48 www 7901: return &allowed($qualifier,$rest);
7902: # ------------------------------------------ user.preferences, user.environment
7903: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620 albertel 7904: if (($uname eq $env{'user.name'}) &&
7905: ($udom eq $env{'user.domain'})) {
7906: return $env{join('.',('environment',$qualifierrest))};
1.218 albertel 7907: } else {
1.359 albertel 7908: my %returnhash;
7909: if (!$publicuser) {
7910: %returnhash=&userenvironment($udom,$uname,
7911: $qualifierrest);
7912: }
1.218 albertel 7913: return $returnhash{$qualifierrest};
7914: }
1.48 www 7915: # ----------------------------------------------------------------- user.course
7916: } elsif ($space eq 'course') {
1.218 albertel 7917: # FIXME - not supporting calls for a specific user
1.620 albertel 7918: return $env{join('.',('request.course',$qualifier))};
1.48 www 7919: # ------------------------------------------------------------------- user.role
7920: } elsif ($space eq 'role') {
1.218 albertel 7921: # FIXME - not supporting calls for a specific user
1.620 albertel 7922: my ($role,$where)=split(/\./,$env{'request.role'});
1.48 www 7923: if ($qualifier eq 'value') {
7924: return $role;
7925: } elsif ($qualifier eq 'extent') {
7926: return $where;
7927: }
7928: # ----------------------------------------------------------------- user.domain
7929: } elsif ($space eq 'domain') {
1.218 albertel 7930: return $udom;
1.48 www 7931: # ------------------------------------------------------------------- user.name
7932: } elsif ($space eq 'name') {
1.218 albertel 7933: return $uname;
1.48 www 7934: # ---------------------------------------------------- Any other user namespace
1.29 www 7935: } else {
1.359 albertel 7936: my %reply;
7937: if (!$publicuser) {
7938: %reply=&get($space,[$qualifierrest],$udom,$uname);
7939: }
7940: return $reply{$qualifierrest};
1.48 www 7941: }
1.236 www 7942: } elsif ($realm eq 'query') {
7943: # ---------------------------------------------- pull stuff out of query string
1.384 albertel 7944: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
7945: [$spacequalifierrest]);
1.620 albertel 7946: return $env{'form.'.$spacequalifierrest};
1.236 www 7947: } elsif ($realm eq 'request') {
1.48 www 7948: # ------------------------------------------------------------- request.browser
7949: if ($space eq 'browser') {
1.430 www 7950: if ($qualifier eq 'textremote') {
1.676 albertel 7951: if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430 www 7952: return 1;
7953: } else {
7954: return 0;
7955: }
7956: } else {
1.620 albertel 7957: return $env{'browser.'.$qualifier};
1.430 www 7958: }
1.57 www 7959: # ------------------------------------------------------------ request.filename
7960: } else {
1.620 albertel 7961: return $env{'request.'.$spacequalifierrest};
1.29 www 7962: }
1.28 www 7963: } elsif ($realm eq 'course') {
1.48 www 7964: # ---------------------------------------------------------- course.description
1.620 albertel 7965: return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57 www 7966: } elsif ($realm eq 'resource') {
1.165 www 7967:
1.620 albertel 7968: if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539 albertel 7969: if (!$symbparm) { $symbparm=&symbread(); }
7970: }
1.693 albertel 7971:
7972: if ($space eq 'title') {
7973: if (!$symbparm) { $symbparm = $env{'request.filename'}; }
7974: return &gettitle($symbparm);
7975: }
7976:
7977: if ($space eq 'map') {
7978: my ($map) = &decode_symb($symbparm);
7979: return &symbread($map);
7980: }
1.905 albertel 7981: if ($space eq 'filename') {
7982: if ($symbparm) {
7983: return &clutter((&decode_symb($symbparm))[2]);
7984: }
7985: return &hreflocation('',$env{'request.filename'});
7986: }
1.693 albertel 7987:
7988: my ($section, $group, @groups);
1.593 albertel 7989: my ($courselevelm,$courselevel);
1.539 albertel 7990: if ($symbparm && defined($courseid) &&
1.620 albertel 7991: $courseid eq $env{'request.course.id'}) {
1.165 www 7992:
1.218 albertel 7993: #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165 www 7994:
1.60 www 7995: # ----------------------------------------------------- Cascading lookup scheme
1.218 albertel 7996: my $symbp=$symbparm;
1.735 albertel 7997: my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218 albertel 7998:
7999: my $symbparm=$symbp.'.'.$spacequalifierrest;
8000: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
8001:
1.620 albertel 8002: if (($env{'user.name'} eq $uname) &&
8003: ($env{'user.domain'} eq $udom)) {
8004: $section=$env{'request.course.sec'};
1.733 raeburn 8005: @groups = split(/:/,$env{'request.course.groups'});
8006: @groups=&sort_course_groups($courseid,@groups);
1.218 albertel 8007: } else {
1.539 albertel 8008: if (! defined($usection)) {
1.551 albertel 8009: $section=&getsection($udom,$uname,$courseid);
1.539 albertel 8010: } else {
8011: $section = $usection;
8012: }
1.733 raeburn 8013: @groups = &get_users_groups($udom,$uname,$courseid);
1.218 albertel 8014: }
8015:
8016: my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
8017: my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
8018: my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
8019:
1.593 albertel 8020: $courselevel=$courseid.'.'.$spacequalifierrest;
1.218 albertel 8021: my $courselevelr=$courseid.'.'.$symbparm;
1.593 albertel 8022: $courselevelm=$courseid.'.'.$mapparm;
1.69 www 8023:
1.60 www 8024: # ----------------------------------------------------------- first, check user
1.624 albertel 8025:
8026: my $userreply=&resdata($uname,$udom,'user',
1.927 albertel 8027: ([$courselevelr,'resource'],
8028: [$courselevelm,'map' ],
8029: [$courselevel, 'course' ]));
1.931 albertel 8030: if (defined($userreply)) { return &get_reply($userreply); }
1.95 www 8031:
1.594 albertel 8032: # ------------------------------------------------ second, check some of course
1.684 raeburn 8033: my $coursereply;
1.691 raeburn 8034: if (@groups > 0) {
8035: $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
8036: $mapparm,$spacequalifierrest);
1.927 albertel 8037: if (defined($coursereply)) { return &get_reply($coursereply); }
1.684 raeburn 8038: }
1.96 www 8039:
1.684 raeburn 8040: $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927 albertel 8041: $env{'course.'.$courseid.'.domain'},
8042: 'course',
8043: ([$seclevelr, 'resource'],
8044: [$seclevelm, 'map' ],
8045: [$seclevel, 'course' ],
8046: [$courselevelr,'resource']));
8047: if (defined($coursereply)) { return &get_reply($coursereply); }
1.200 www 8048:
1.60 www 8049: # ------------------------------------------------------ third, check map parms
1.218 albertel 8050: my %parmhash=();
8051: my $thisparm='';
8052: if (tie(%parmhash,'GDBM_File',
1.620 albertel 8053: $env{'request.course.fn'}.'_parms.db',
1.256 albertel 8054: &GDBM_READER(),0640)) {
1.218 albertel 8055: $thisparm=$parmhash{$symbparm};
8056: untie(%parmhash);
8057: }
1.927 albertel 8058: if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218 albertel 8059: }
1.594 albertel 8060: # ------------------------------------------ fourth, look in resource metadata
1.71 www 8061:
1.218 albertel 8062: $spacequalifierrest=~s/\./\_/;
1.282 albertel 8063: my $filename;
8064: if (!$symbparm) { $symbparm=&symbread(); }
8065: if ($symbparm) {
1.409 www 8066: $filename=(&decode_symb($symbparm))[2];
1.282 albertel 8067: } else {
1.620 albertel 8068: $filename=$env{'request.filename'};
1.282 albertel 8069: }
8070: my $metadata=&metadata($filename,$spacequalifierrest);
1.927 albertel 8071: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282 albertel 8072: $metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927 albertel 8073: if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142 www 8074:
1.927 albertel 8075: # ---------------------------------------------- fourth, look in rest of course
1.593 albertel 8076: if ($symbparm && defined($courseid) &&
1.620 albertel 8077: $courseid eq $env{'request.course.id'}) {
1.624 albertel 8078: my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
8079: $env{'course.'.$courseid.'.domain'},
8080: 'course',
1.927 albertel 8081: ([$courselevelm,'map' ],
8082: [$courselevel, 'course']));
8083: if (defined($coursereply)) { return &get_reply($coursereply); }
1.593 albertel 8084: }
1.145 www 8085: # ------------------------------------------------------------------ Cascade up
1.218 albertel 8086: unless ($space eq '0') {
1.336 albertel 8087: my @parts=split(/_/,$space);
8088: my $id=pop(@parts);
8089: my $part=join('_',@parts);
8090: if ($part eq '') { $part='0'; }
1.927 albertel 8091: my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395 albertel 8092: $symbparm,$udom,$uname,$section,1);
1.938 raeburn 8093: if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218 albertel 8094: }
1.395 albertel 8095: if ($recurse) { return undef; }
8096: my $pack_def=&packages_tab_default($filename,$varname);
1.927 albertel 8097: if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48 www 8098: # ---------------------------------------------------- Any other user namespace
8099: } elsif ($realm eq 'environment') {
8100: # ----------------------------------------------------------------- environment
1.620 albertel 8101: if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
8102: return $env{'environment.'.$spacequalifierrest};
1.219 albertel 8103: } else {
1.770 albertel 8104: if ($uname eq 'anonymous' && $udom eq '') {
8105: return '';
8106: }
1.219 albertel 8107: my %returnhash=&userenvironment($udom,$uname,
8108: $spacequalifierrest);
8109: return $returnhash{$spacequalifierrest};
8110: }
1.28 www 8111: } elsif ($realm eq 'system') {
1.48 www 8112: # ----------------------------------------------------------------- system.time
8113: if ($space eq 'time') {
8114: return time;
8115: }
1.696 albertel 8116: } elsif ($realm eq 'server') {
8117: # ----------------------------------------------------------------- system.time
8118: if ($space eq 'name') {
8119: return $ENV{'SERVER_NAME'};
8120: }
1.28 www 8121: }
1.48 www 8122: return '';
1.61 www 8123: }
8124:
1.927 albertel 8125: sub get_reply {
8126: my ($reply_value) = @_;
1.940 raeburn 8127: if (ref($reply_value) eq 'ARRAY') {
8128: if (wantarray) {
8129: return @$reply_value;
8130: }
8131: return $reply_value->[0];
8132: } else {
8133: return $reply_value;
1.927 albertel 8134: }
8135: }
8136:
1.691 raeburn 8137: sub check_group_parms {
8138: my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
8139: my @groupitems = ();
8140: my $resultitem;
1.927 albertel 8141: my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691 raeburn 8142: foreach my $group (@{$groups}) {
8143: foreach my $level (@levels) {
1.927 albertel 8144: my $item = $courseid.'.['.$group.'].'.$level->[0];
8145: push(@groupitems,[$item,$level->[1]]);
1.691 raeburn 8146: }
8147: }
8148: my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
8149: $env{'course.'.$courseid.'.domain'},
8150: 'course',@groupitems);
8151: return $coursereply;
8152: }
8153:
8154: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733 raeburn 8155: my ($courseid,@groups) = @_;
8156: @groups = sort(@groups);
1.691 raeburn 8157: return @groups;
8158: }
8159:
1.395 albertel 8160: sub packages_tab_default {
8161: my ($uri,$varname)=@_;
8162: my (undef,$part,$name)=split(/\./,$varname);
1.738 albertel 8163:
8164: my (@extension,@specifics,$do_default);
8165: foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395 albertel 8166: my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738 albertel 8167: if ($pack_type eq 'default') {
8168: $do_default=1;
8169: } elsif ($pack_type eq 'extension') {
8170: push(@extension,[$package,$pack_type,$pack_part]);
1.885 albertel 8171: } elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848 albertel 8172: # only look at packages defaults for packages that this id is
1.738 albertel 8173: push(@specifics,[$package,$pack_type,$pack_part]);
8174: }
8175: }
8176: # first look for a package that matches the requested part id
8177: foreach my $package (@specifics) {
8178: my (undef,$pack_type,$pack_part)=@{$package};
8179: next if ($pack_part ne $part);
8180: if (defined($packagetab{"$pack_type&$name&default"})) {
8181: return $packagetab{"$pack_type&$name&default"};
8182: }
8183: }
8184: # look for any possible matching non extension_ package
8185: foreach my $package (@specifics) {
8186: my (undef,$pack_type,$pack_part)=@{$package};
1.468 albertel 8187: if (defined($packagetab{"$pack_type&$name&default"})) {
8188: return $packagetab{"$pack_type&$name&default"};
8189: }
1.585 albertel 8190: if ($pack_type eq 'part') { $pack_part='0'; }
1.468 albertel 8191: if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
8192: return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395 albertel 8193: }
8194: }
1.738 albertel 8195: # look for any posible extension_ match
8196: foreach my $package (@extension) {
8197: my ($package,$pack_type)=@{$package};
8198: if (defined($packagetab{"$pack_type&$name&default"})) {
8199: return $packagetab{"$pack_type&$name&default"};
8200: }
8201: if (defined($packagetab{$package."&$name&default"})) {
8202: return $packagetab{$package."&$name&default"};
8203: }
8204: }
8205: # look for a global default setting
8206: if ($do_default && defined($packagetab{"default&$name&default"})) {
8207: return $packagetab{"default&$name&default"};
8208: }
1.395 albertel 8209: return undef;
8210: }
8211:
1.334 albertel 8212: sub add_prefix_and_part {
8213: my ($prefix,$part)=@_;
8214: my $keyroot;
8215: if (defined($prefix) && $prefix !~ /^__/) {
8216: # prefix that has a part already
8217: $keyroot=$prefix;
8218: } elsif (defined($prefix)) {
8219: # prefix that is missing a part
8220: if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
8221: } else {
8222: # no prefix at all
8223: if (defined($part)) { $keyroot='_'.$part; }
8224: }
8225: return $keyroot;
8226: }
8227:
1.71 www 8228: # ---------------------------------------------------------------- Get metadata
8229:
1.599 albertel 8230: my %metaentry;
1.1070 www 8231: my %importedpartids;
1.71 www 8232: sub metadata {
1.176 www 8233: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71 www 8234: $uri=&declutter($uri);
1.288 albertel 8235: # if it is a non metadata possible uri return quickly
1.529 albertel 8236: if (($uri eq '') ||
8237: (($uri =~ m|^/*adm/|) &&
1.698 albertel 8238: ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.924 albertel 8239: ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) ) {
8240: return undef;
8241: }
8242: if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/})
8243: && &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468 albertel 8244: return undef;
1.288 albertel 8245: }
1.73 www 8246: my $filename=$uri;
8247: $uri=~s/\.meta$//;
1.172 www 8248: #
8249: # Is the metadata already cached?
1.177 www 8250: # Look at timestamp of caching
1.172 www 8251: # Everything is cached by the main uri, libraries are never directly cached
8252: #
1.428 albertel 8253: if (!defined($liburi)) {
1.599 albertel 8254: my ($result,$cached)=&is_cached_new('meta',$uri);
1.428 albertel 8255: if (defined($cached)) { return $result->{':'.$what}; }
8256: }
8257: {
1.1069 www 8258: # Imported parts would go here
1.1070 www 8259: my %importedids=();
8260: my @origfileimportpartids=();
1.1069 www 8261: my $importedparts=0;
1.172 www 8262: #
8263: # Is this a recursive call for a library?
8264: #
1.599 albertel 8265: # if (! exists($metacache{$uri})) {
8266: # $metacache{$uri}={};
8267: # }
1.924 albertel 8268: my $cachetime = 60*60;
1.171 www 8269: if ($liburi) {
8270: $liburi=&declutter($liburi);
8271: $filename=$liburi;
1.401 bowersj2 8272: } else {
1.599 albertel 8273: &devalidate_cache_new('meta',$uri);
8274: undef(%metaentry);
1.401 bowersj2 8275: }
1.140 www 8276: my %metathesekeys=();
1.73 www 8277: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489 albertel 8278: my $metastring;
1.924 albertel 8279: if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929 albertel 8280: my $which = &hreflocation('','/'.($liburi || $uri));
1.924 albertel 8281: $metastring =
1.929 albertel 8282: &Apache::lonnet::ssi_body($which,
1.924 albertel 8283: ('grade_target' => 'meta'));
8284: $cachetime = 1; # only want this cached in the child not long term
8285: } elsif ($uri !~ m -^(editupload)/-) {
1.543 albertel 8286: my $file=&filelocation('',&clutter($filename));
1.599 albertel 8287: #push(@{$metaentry{$uri.'.file'}},$file);
1.543 albertel 8288: $metastring=&getfile($file);
1.489 albertel 8289: }
1.208 albertel 8290: my $parser=HTML::LCParser->new(\$metastring);
1.71 www 8291: my $token;
1.140 www 8292: undef %metathesekeys;
1.71 www 8293: while ($token=$parser->get_token) {
1.339 albertel 8294: if ($token->[0] eq 'S') {
8295: if (defined($token->[2]->{'package'})) {
1.172 www 8296: #
8297: # This is a package - get package info
8298: #
1.339 albertel 8299: my $package=$token->[2]->{'package'};
8300: my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8301: if (defined($token->[2]->{'id'})) {
8302: $keyroot.='_'.$token->[2]->{'id'};
8303: }
1.599 albertel 8304: if ($metaentry{':packages'}) {
8305: $metaentry{':packages'}.=','.$package.$keyroot;
1.339 albertel 8306: } else {
1.599 albertel 8307: $metaentry{':packages'}=$package.$keyroot;
1.339 albertel 8308: }
1.736 albertel 8309: foreach my $pack_entry (keys(%packagetab)) {
1.432 albertel 8310: my $part=$keyroot;
8311: $part=~s/^\_//;
1.736 albertel 8312: if ($pack_entry=~/^\Q$package\E\&/ ||
8313: $pack_entry=~/^\Q$package\E_0\&/) {
8314: my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395 albertel 8315: # ignore package.tab specified default values
8316: # here &package_tab_default() will fetch those
8317: if ($subp eq 'default') { next; }
1.736 albertel 8318: my $value=$packagetab{$pack_entry};
1.432 albertel 8319: my $unikey;
8320: if ($pack =~ /_0$/) {
8321: $unikey='parameter_0_'.$name;
8322: $part=0;
8323: } else {
8324: $unikey='parameter'.$keyroot.'_'.$name;
8325: }
1.339 albertel 8326: if ($subp eq 'display') {
8327: $value.=' [Part: '.$part.']';
8328: }
1.599 albertel 8329: $metaentry{':'.$unikey.'.part'}=$part;
1.395 albertel 8330: $metathesekeys{$unikey}=1;
1.599 albertel 8331: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8332: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.339 albertel 8333: }
1.599 albertel 8334: if (defined($metaentry{':'.$unikey.'.default'})) {
8335: $metaentry{':'.$unikey}=
8336: $metaentry{':'.$unikey.'.default'};
1.356 albertel 8337: }
1.339 albertel 8338: }
8339: }
8340: } else {
1.172 www 8341: #
8342: # This is not a package - some other kind of start tag
1.339 albertel 8343: #
8344: my $entry=$token->[1];
1.1068 www 8345: my $unikey='';
1.175 www 8346:
1.339 albertel 8347: if ($entry eq 'import') {
1.175 www 8348: #
8349: # Importing a library here
1.339 albertel 8350: #
1.1067 www 8351: my $location=$parser->get_text('/import');
8352: my $dir=$filename;
8353: $dir=~s|[^/]*$||;
8354: $location=&filelocation($dir,$location);
1.1069 www 8355:
1.1068 www 8356: my $importmode=$token->[2]->{'importmode'};
8357: if ($importmode eq 'problem') {
1.1069 www 8358: # Import as problem/response
1.1068 www 8359: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8360: } elsif ($importmode eq 'part') {
8361: # Import as part(s)
1.1069 www 8362: $importedparts=1;
8363: # We need to get the original file and the imported file to get the part order correct
8364: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
8365: # Load and inspect original file
1.1070 www 8366: if ($#origfileimportpartids<0) {
8367: undef(%importedpartids);
8368: my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
8369: my $origfile=&getfile($origfilelocation);
8370: @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
8371: }
8372:
1.1069 www 8373: # Load and inspect imported file
8374: my $impfile=&getfile($location);
8375: my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
8376: if ($#impfilepartids>=0) {
8377: # This problem had parts
1.1070 www 8378: $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069 www 8379: } else {
8380: # Importing by turning a single problem into a problem part
8381: # It gets the import-tags ID as part-ID
8382: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070 www 8383: $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069 www 8384: }
1.1068 www 8385: } else {
8386: # Normal import
8387: $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
8388: if (defined($token->[2]->{'id'})) {
8389: $unikey.='_'.$token->[2]->{'id'};
8390: }
1.1067 www 8391: }
8392:
1.339 albertel 8393: if ($depthcount<20) {
1.736 albertel 8394: my $metadata =
8395: &metadata($uri,'keys', $location,$unikey,
8396: $depthcount+1);
8397: foreach my $meta (split(',',$metadata)) {
8398: $metaentry{':'.$meta}=$metaentry{':'.$meta};
8399: $metathesekeys{$meta}=1;
1.339 albertel 8400: }
1.1068 www 8401:
8402: }
1.1067 www 8403: } else {
8404: #
8405: # Not importing, some other kind of non-package, non-library start tag
8406: #
8407: $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
8408: if (defined($token->[2]->{'id'})) {
8409: $unikey.='_'.$token->[2]->{'id'};
8410: }
1.339 albertel 8411: if (defined($token->[2]->{'name'})) {
8412: $unikey.='_'.$token->[2]->{'name'};
8413: }
8414: $metathesekeys{$unikey}=1;
1.736 albertel 8415: foreach my $param (@{$token->[3]}) {
8416: $metaentry{':'.$unikey.'.'.$param} =
8417: $token->[2]->{$param};
1.339 albertel 8418: }
8419: my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599 albertel 8420: my $default=$metaentry{':'.$unikey.'.default'};
1.339 albertel 8421: if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
8422: # only ws inside the tag, and not in default, so use default
8423: # as value
1.599 albertel 8424: $metaentry{':'.$unikey}=$default;
1.908 albertel 8425: } elsif ( $internaltext =~ /\S/ ) {
8426: # something interesting inside the tag
8427: $metaentry{':'.$unikey}=$internaltext;
1.339 albertel 8428: } else {
1.908 albertel 8429: # no interesting values, don't set a default
1.339 albertel 8430: }
1.172 www 8431: # end of not-a-package not-a-library import
1.339 albertel 8432: }
1.172 www 8433: # end of not-a-package start tag
1.339 albertel 8434: }
1.172 www 8435: # the next is the end of "start tag"
1.339 albertel 8436: }
8437: }
1.483 albertel 8438: my ($extension) = ($uri =~ /\.(\w+)$/);
1.883 albertel 8439: $extension = lc($extension);
8440: if ($extension eq 'htm') { $extension='html'; }
8441:
1.737 albertel 8442: foreach my $key (keys(%packagetab)) {
1.483 albertel 8443: #no specific packages #how's our extension
8444: if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488 albertel 8445: &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483 albertel 8446: \%metathesekeys);
8447: }
1.883 albertel 8448:
8449: if (!exists($metaentry{':packages'})
8450: || $packagetab{"import_defaults&extension_$extension"}) {
1.737 albertel 8451: foreach my $key (keys(%packagetab)) {
1.483 albertel 8452: #no specific packages well let's get default then
8453: if ($key!~/^default&/) { next; }
1.488 albertel 8454: &metadata_create_package_def($uri,$key,'default',
1.483 albertel 8455: \%metathesekeys);
8456: }
8457: }
1.338 www 8458: # are there custom rights to evaluate
1.599 albertel 8459: if ($metaentry{':copyright'} eq 'custom') {
1.339 albertel 8460:
1.338 www 8461: #
8462: # Importing a rights file here
1.339 albertel 8463: #
8464: unless ($depthcount) {
1.599 albertel 8465: my $location=$metaentry{':customdistributionfile'};
1.339 albertel 8466: my $dir=$filename;
8467: $dir=~s|[^/]*$||;
8468: $location=&filelocation($dir,$location);
1.736 albertel 8469: my $rights_metadata =
8470: &metadata($uri,'keys',$location,'_rights',
8471: $depthcount+1);
8472: foreach my $rights (split(',',$rights_metadata)) {
8473: #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
8474: $metathesekeys{$rights}=1;
1.339 albertel 8475: }
8476: }
8477: }
1.737 albertel 8478: # uniqifiy package listing
8479: my %seen;
8480: my @uniq_packages =
8481: grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
8482: $metaentry{':packages'} = join(',',@uniq_packages);
8483:
1.1070 www 8484: if ($importedparts) {
8485: # We had imported parts and need to rebuild partorder
8486: $metaentry{':partorder'}='';
8487: $metathesekeys{'partorder'}=1;
8488: for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
8489: if ($origfileimportpartids[$index] eq 'part') {
8490: # original part, part of the problem
8491: $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
8492: } else {
8493: # we have imported parts at this position
8494: $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
8495: }
8496: }
8497: $metaentry{':partorder'}=~s/^\,//;
8498: }
8499:
1.737 albertel 8500: $metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599 albertel 8501: &metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
8502: $metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924 albertel 8503: &do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177 www 8504: # this is the end of "was not already recently cached
1.71 www 8505: }
1.599 albertel 8506: return $metaentry{':'.$what};
1.261 albertel 8507: }
8508:
1.488 albertel 8509: sub metadata_create_package_def {
1.483 albertel 8510: my ($uri,$key,$package,$metathesekeys)=@_;
8511: my ($pack,$name,$subp)=split(/\&/,$key);
8512: if ($subp eq 'default') { next; }
8513:
1.599 albertel 8514: if (defined($metaentry{':packages'})) {
8515: $metaentry{':packages'}.=','.$package;
1.483 albertel 8516: } else {
1.599 albertel 8517: $metaentry{':packages'}=$package;
1.483 albertel 8518: }
8519: my $value=$packagetab{$key};
8520: my $unikey;
8521: $unikey='parameter_0_'.$name;
1.599 albertel 8522: $metaentry{':'.$unikey.'.part'}=0;
1.483 albertel 8523: $$metathesekeys{$unikey}=1;
1.599 albertel 8524: unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
8525: $metaentry{':'.$unikey.'.'.$subp}=$value;
1.483 albertel 8526: }
1.599 albertel 8527: if (defined($metaentry{':'.$unikey.'.default'})) {
8528: $metaentry{':'.$unikey}=
8529: $metaentry{':'.$unikey.'.default'};
1.483 albertel 8530: }
8531: }
8532:
1.261 albertel 8533: sub metadata_generate_part0 {
8534: my ($metadata,$metacache,$uri) = @_;
8535: my %allnames;
1.737 albertel 8536: foreach my $metakey (keys(%$metadata)) {
1.261 albertel 8537: if ($metakey=~/^parameter\_(.*)/) {
1.428 albertel 8538: my $part=$$metacache{':'.$metakey.'.part'};
8539: my $name=$$metacache{':'.$metakey.'.name'};
1.356 albertel 8540: if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261 albertel 8541: $allnames{$name}=$part;
8542: }
8543: }
8544: }
8545: foreach my $name (keys(%allnames)) {
8546: $$metadata{"parameter_0_$name"}=1;
1.428 albertel 8547: my $key=":parameter_0_$name";
1.261 albertel 8548: $$metacache{"$key.part"}='0';
8549: $$metacache{"$key.name"}=$name;
1.428 albertel 8550: $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261 albertel 8551: $allnames{$name}.'_'.$name.
8552: '.type'};
1.428 albertel 8553: my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261 albertel 8554: '.display'};
1.644 www 8555: my $expr='[Part: '.$allnames{$name}.']';
1.479 albertel 8556: $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261 albertel 8557: $$metacache{"$key.display"}=$olddis;
8558: }
1.71 www 8559: }
8560:
1.764 albertel 8561: # ------------------------------------------------------ Devalidate title cache
8562:
8563: sub devalidate_title_cache {
8564: my ($url)=@_;
8565: if (!$env{'request.course.id'}) { return; }
8566: my $symb=&symbread($url);
8567: if (!$symb) { return; }
8568: my $key=$env{'request.course.id'}."\0".$symb;
8569: &devalidate_cache_new('title',$key);
8570: }
8571:
1.1014 droeschl 8572: # ------------------------------------------------- Get the title of a course
8573:
8574: sub current_course_title {
8575: return $env{ 'course.' . $env{'request.course.id'} . '.description' };
8576: }
1.301 www 8577: # ------------------------------------------------- Get the title of a resource
8578:
8579: sub gettitle {
8580: my $urlsymb=shift;
8581: my $symb=&symbread($urlsymb);
1.534 albertel 8582: if ($symb) {
1.620 albertel 8583: my $key=$env{'request.course.id'}."\0".$symb;
1.599 albertel 8584: my ($result,$cached)=&is_cached_new('title',$key);
1.575 albertel 8585: if (defined($cached)) {
8586: return $result;
8587: }
1.534 albertel 8588: my ($map,$resid,$url)=&decode_symb($symb);
8589: my $title='';
1.907 albertel 8590: if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
8591: $title = $env{'course.'.$env{'request.course.id'}.'.description'};
8592: } else {
8593: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
8594: &GDBM_READER(),0640)) {
8595: my $mapid=$bighash{'map_pc_'.&clutter($map)};
8596: $title=$bighash{'title_'.$mapid.'.'.$resid};
8597: untie(%bighash);
8598: }
1.534 albertel 8599: }
8600: $title=~s/\&colon\;/\:/gs;
8601: if ($title) {
1.599 albertel 8602: return &do_cache_new('title',$key,$title,600);
1.534 albertel 8603: }
8604: $urlsymb=$url;
8605: }
8606: my $title=&metadata($urlsymb,'title');
8607: if (!$title) { $title=(split('/',$urlsymb))[-1]; }
8608: return $title;
1.301 www 8609: }
1.613 albertel 8610:
1.614 albertel 8611: sub get_slot {
8612: my ($which,$cnum,$cdom)=@_;
8613: if (!$cnum || !$cdom) {
1.790 albertel 8614: (undef,my $courseid)=&whichuser();
1.620 albertel 8615: $cdom=$env{'course.'.$courseid.'.domain'};
8616: $cnum=$env{'course.'.$courseid.'.num'};
1.614 albertel 8617: }
1.703 albertel 8618: my $key=join("\0",'slots',$cdom,$cnum,$which);
8619: my %slotinfo;
8620: if (exists($remembered{$key})) {
8621: $slotinfo{$which} = $remembered{$key};
8622: } else {
8623: %slotinfo=&get('slots',[$which],$cdom,$cnum);
8624: &Apache::lonhomework::showhash(%slotinfo);
8625: my ($tmp)=keys(%slotinfo);
8626: if ($tmp=~/^error:/) { return (); }
8627: $remembered{$key} = $slotinfo{$which};
8628: }
1.616 albertel 8629: if (ref($slotinfo{$which}) eq 'HASH') {
8630: return %{$slotinfo{$which}};
8631: }
8632: return $slotinfo{$which};
1.614 albertel 8633: }
1.31 www 8634: # ------------------------------------------------- Update symbolic store links
8635:
8636: sub symblist {
8637: my ($mapname,%newhash)=@_;
1.438 www 8638: $mapname=&deversion(&declutter($mapname));
1.31 www 8639: my %hash;
1.620 albertel 8640: if (($env{'request.course.fn'}) && (%newhash)) {
8641: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 8642: &GDBM_WRCREAT(),0640)) {
1.1000 raeburn 8643: foreach my $url (keys(%newhash)) {
1.711 albertel 8644: next if ($url eq 'last_known'
8645: && $env{'form.no_update_last_known'});
8646: $hash{declutter($url)}=&encode_symb($mapname,
8647: $newhash{$url}->[1],
8648: $newhash{$url}->[0]);
1.191 harris41 8649: }
1.31 www 8650: if (untie(%hash)) {
8651: return 'ok';
8652: }
8653: }
8654: }
8655: return 'error';
1.212 www 8656: }
8657:
8658: # --------------------------------------------------------------- Verify a symb
8659:
8660: sub symbverify {
1.510 www 8661: my ($symb,$thisurl)=@_;
8662: my $thisfn=$thisurl;
1.439 www 8663: $thisfn=&declutter($thisfn);
1.215 www 8664: # direct jump to resource in page or to a sequence - will construct own symbs
8665: if ($thisfn=~/\.(page|sequence)$/) { return 1; }
8666: # check URL part
1.409 www 8667: my ($map,$resid,$url)=&decode_symb($symb);
1.439 www 8668:
1.431 www 8669: unless ($url eq $thisfn) { return 0; }
1.213 www 8670:
1.216 www 8671: $symb=&symbclean($symb);
1.510 www 8672: $thisurl=&deversion($thisurl);
1.439 www 8673: $thisfn=&deversion($thisfn);
1.213 www 8674:
8675: my %bighash;
8676: my $okay=0;
1.431 www 8677:
1.620 albertel 8678: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 8679: &GDBM_READER(),0640)) {
1.1032 raeburn 8680: if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
8681: $thisurl =~ s/\?.+$//;
8682: }
1.510 www 8683: my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216 www 8684: unless ($ids) {
1.510 www 8685: $ids=$bighash{'ids_/'.$thisurl};
1.216 www 8686: }
8687: if ($ids) {
8688: # ------------------------------------------------------------------- Has ID(s)
1.800 albertel 8689: foreach my $id (split(/\,/,$ids)) {
8690: my ($mapid,$resid)=split(/\./,$id);
1.1032 raeburn 8691: if ($thisfn =~ m{^/adm/wrapper/ext/}) {
8692: $symb =~ s/\?.+$//;
8693: }
1.216 www 8694: if (
8695: &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
8696: eq $symb) {
1.620 albertel 8697: if (($env{'request.role.adv'}) ||
1.800 albertel 8698: $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582 albertel 8699: $okay=1;
8700: }
8701: }
1.216 www 8702: }
8703: }
1.213 www 8704: untie(%bighash);
8705: }
8706: return $okay;
1.31 www 8707: }
8708:
1.210 www 8709: # --------------------------------------------------------------- Clean-up symb
8710:
8711: sub symbclean {
8712: my $symb=shift;
1.568 albertel 8713: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210 www 8714: # remove version from map
8715: $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215 www 8716:
1.210 www 8717: # remove version from URL
8718: $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213 www 8719:
1.507 www 8720: # remove wrapper
8721:
1.510 www 8722: $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694 albertel 8723: $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210 www 8724: return $symb;
1.409 www 8725: }
8726:
8727: # ---------------------------------------------- Split symb to find map and url
1.429 albertel 8728:
8729: sub encode_symb {
8730: my ($map,$resid,$url)=@_;
8731: return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
8732: }
1.409 www 8733:
8734: sub decode_symb {
1.568 albertel 8735: my $symb=shift;
8736: if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
8737: my ($map,$resid,$url)=split(/___/,$symb);
1.413 www 8738: return (&fixversion($map),$resid,&fixversion($url));
8739: }
8740:
8741: sub fixversion {
8742: my $fn=shift;
1.609 banghart 8743: if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435 www 8744: my %bighash;
8745: my $uri=&clutter($fn);
1.620 albertel 8746: my $key=$env{'request.course.id'}.'_'.$uri;
1.440 www 8747: # is this cached?
1.599 albertel 8748: my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440 www 8749: if (defined($cached)) { return $result; }
8750: # unfortunately not cached, or expired
1.620 albertel 8751: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440 www 8752: &GDBM_READER(),0640)) {
8753: if ($bighash{'version_'.$uri}) {
8754: my $version=$bighash{'version_'.$uri};
1.444 www 8755: unless (($version eq 'mostrecent') ||
8756: ($version==&getversion($uri))) {
1.440 www 8757: $uri=~s/\.(\w+)$/\.$version\.$1/;
8758: }
8759: }
8760: untie %bighash;
1.413 www 8761: }
1.599 albertel 8762: return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438 www 8763: }
8764:
8765: sub deversion {
8766: my $url=shift;
8767: $url=~s/\.\d+\.(\w+)$/\.$1/;
8768: return $url;
1.210 www 8769: }
8770:
1.31 www 8771: # ------------------------------------------------------ Return symb list entry
8772:
8773: sub symbread {
1.249 www 8774: my ($thisfn,$donotrecurse)=@_;
1.542 albertel 8775: my $cache_str='request.symbread.cached.'.$thisfn;
1.620 albertel 8776: if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242 www 8777: # no filename provided? try from environment
1.44 www 8778: unless ($thisfn) {
1.620 albertel 8779: if ($env{'request.symb'}) {
8780: return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539 albertel 8781: }
1.620 albertel 8782: $thisfn=$env{'request.filename'};
1.44 www 8783: }
1.569 albertel 8784: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242 www 8785: # is that filename actually a symb? Verify, clean, and return
8786: if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539 albertel 8787: if (&symbverify($thisfn,$1)) {
1.620 albertel 8788: return $env{$cache_str}=&symbclean($thisfn);
1.539 albertel 8789: }
1.242 www 8790: }
1.44 www 8791: $thisfn=declutter($thisfn);
1.31 www 8792: my %hash;
1.37 www 8793: my %bighash;
8794: my $syval='';
1.620 albertel 8795: if (($env{'request.course.fn'}) && ($thisfn)) {
1.481 raeburn 8796: my $targetfn = $thisfn;
1.609 banghart 8797: if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481 raeburn 8798: $targetfn = 'adm/wrapper/'.$thisfn;
8799: }
1.687 albertel 8800: if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
8801: $targetfn=$1;
8802: }
1.620 albertel 8803: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256 albertel 8804: &GDBM_READER(),0640)) {
1.481 raeburn 8805: $syval=$hash{$targetfn};
1.37 www 8806: untie(%hash);
8807: }
8808: # ---------------------------------------------------------- There was an entry
8809: if ($syval) {
1.601 albertel 8810: #unless ($syval=~/\_\d+$/) {
1.620 albertel 8811: #unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949 raeburn 8812: #&appenv({'request.ambiguous' => $thisfn});
1.620 albertel 8813: #return $env{$cache_str}='';
1.601 albertel 8814: #}
8815: #$syval.=$1;
8816: #}
1.37 www 8817: } else {
8818: # ------------------------------------------------------- Was not in symb table
1.620 albertel 8819: if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256 albertel 8820: &GDBM_READER(),0640)) {
1.37 www 8821: # ---------------------------------------------- Get ID(s) for current resource
1.280 www 8822: my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65 www 8823: unless ($ids) {
8824: $ids=$bighash{'ids_/'.$thisfn};
1.242 www 8825: }
8826: unless ($ids) {
8827: # alias?
8828: $ids=$bighash{'mapalias_'.$thisfn};
1.65 www 8829: }
1.37 www 8830: if ($ids) {
8831: # ------------------------------------------------------------------- Has ID(s)
8832: my @possibilities=split(/\,/,$ids);
1.39 www 8833: if ($#possibilities==0) {
8834: # ----------------------------------------------- There is only one possibility
1.37 www 8835: my ($mapid,$resid)=split(/\./,$ids);
1.626 albertel 8836: $syval=&encode_symb($bighash{'map_id_'.$mapid},
8837: $resid,$thisfn);
1.249 www 8838: } elsif (!$donotrecurse) {
1.39 www 8839: # ------------------------------------------ There is more than one possibility
8840: my $realpossible=0;
1.800 albertel 8841: foreach my $id (@possibilities) {
8842: my $file=$bighash{'src_'.$id};
1.39 www 8843: if (&allowed('bre',$file)) {
1.800 albertel 8844: my ($mapid,$resid)=split(/\./,$id);
1.39 www 8845: if ($bighash{'map_type_'.$mapid} ne 'page') {
8846: $realpossible++;
1.626 albertel 8847: $syval=&encode_symb($bighash{'map_id_'.$mapid},
8848: $resid,$thisfn);
1.39 www 8849: }
8850: }
1.191 harris41 8851: }
1.39 www 8852: if ($realpossible!=1) { $syval=''; }
1.249 www 8853: } else {
8854: $syval='';
1.37 www 8855: }
8856: }
8857: untie(%bighash)
1.481 raeburn 8858: }
1.31 www 8859: }
1.62 www 8860: if ($syval) {
1.620 albertel 8861: return $env{$cache_str}=$syval;
1.62 www 8862: }
1.31 www 8863: }
1.949 raeburn 8864: &appenv({'request.ambiguous' => $thisfn});
1.620 albertel 8865: return $env{$cache_str}='';
1.31 www 8866: }
8867:
8868: # ---------------------------------------------------------- Return random seed
8869:
1.32 www 8870: sub numval {
8871: my $txt=shift;
8872: $txt=~tr/A-J/0-9/;
8873: $txt=~tr/a-j/0-9/;
8874: $txt=~tr/K-T/0-9/;
8875: $txt=~tr/k-t/0-9/;
8876: $txt=~tr/U-Z/0-5/;
8877: $txt=~tr/u-z/0-5/;
8878: $txt=~s/\D//g;
1.564 albertel 8879: if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32 www 8880: return int($txt);
1.368 albertel 8881: }
8882:
1.484 albertel 8883: sub numval2 {
8884: my $txt=shift;
8885: $txt=~tr/A-J/0-9/;
8886: $txt=~tr/a-j/0-9/;
8887: $txt=~tr/K-T/0-9/;
8888: $txt=~tr/k-t/0-9/;
8889: $txt=~tr/U-Z/0-5/;
8890: $txt=~tr/u-z/0-5/;
8891: $txt=~s/\D//g;
8892: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
8893: my $total;
8894: foreach my $val (@txts) { $total+=$val; }
1.564 albertel 8895: if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484 albertel 8896: return int($total);
8897: }
8898:
1.575 albertel 8899: sub numval3 {
8900: use integer;
8901: my $txt=shift;
8902: $txt=~tr/A-J/0-9/;
8903: $txt=~tr/a-j/0-9/;
8904: $txt=~tr/K-T/0-9/;
8905: $txt=~tr/k-t/0-9/;
8906: $txt=~tr/U-Z/0-5/;
8907: $txt=~tr/u-z/0-5/;
8908: $txt=~s/\D//g;
8909: my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
8910: my $total;
8911: foreach my $val (@txts) { $total+=$val; }
8912: if ($_64bit) { $total=(($total<<32)>>32); }
8913: return $total;
8914: }
8915:
1.675 albertel 8916: sub digest {
8917: my ($data)=@_;
8918: my $digest=&Digest::MD5::md5($data);
8919: my ($a,$b,$c,$d)=unpack("iiii",$digest);
8920: my ($e,$f);
8921: {
8922: use integer;
8923: $e=($a+$b);
8924: $f=($c+$d);
8925: if ($_64bit) {
8926: $e=(($e<<32)>>32);
8927: $f=(($f<<32)>>32);
8928: }
8929: }
8930: if (wantarray) {
8931: return ($e,$f);
8932: } else {
8933: my $g;
8934: {
8935: use integer;
8936: $g=($e+$f);
8937: if ($_64bit) {
8938: $g=(($g<<32)>>32);
8939: }
8940: }
8941: return $g;
8942: }
8943: }
8944:
1.368 albertel 8945: sub latest_rnd_algorithm_id {
1.675 albertel 8946: return '64bit5';
1.366 albertel 8947: }
1.32 www 8948:
1.503 albertel 8949: sub get_rand_alg {
8950: my ($courseid)=@_;
1.790 albertel 8951: if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503 albertel 8952: if ($courseid) {
1.620 albertel 8953: return $env{"course.$courseid.rndseed"};
1.503 albertel 8954: }
8955: return &latest_rnd_algorithm_id();
8956: }
8957:
1.562 albertel 8958: sub validCODE {
8959: my ($CODE)=@_;
8960: if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
8961: return 0;
8962: }
8963:
1.491 albertel 8964: sub getCODE {
1.620 albertel 8965: if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618 albertel 8966: if ( (defined($Apache::lonhomework::parsing_a_problem) ||
8967: defined($Apache::lonhomework::parsing_a_task) ) &&
8968: &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491 albertel 8969: return $Apache::lonhomework::history{'resource.CODE'};
8970: }
8971: return undef;
8972: }
8973:
1.31 www 8974: sub rndseed {
1.155 albertel 8975: my ($symb,$courseid,$domain,$username)=@_;
1.790 albertel 8976: my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896 albertel 8977: if (!defined($symb)) {
1.366 albertel 8978: unless ($symb=$wsymb) { return time; }
8979: }
8980: if (!$courseid) { $courseid=$wcourseid; }
8981: if (!$domain) { $domain=$wdomain; }
8982: if (!$username) { $username=$wusername }
1.503 albertel 8983: my $which=&get_rand_alg();
1.803 albertel 8984:
1.491 albertel 8985: if (defined(&getCODE())) {
1.675 albertel 8986: if ($which eq '64bit5') {
8987: return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
8988: } elsif ($which eq '64bit4') {
1.575 albertel 8989: return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
8990: } else {
8991: return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
8992: }
1.675 albertel 8993: } elsif ($which eq '64bit5') {
8994: return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575 albertel 8995: } elsif ($which eq '64bit4') {
8996: return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501 albertel 8997: } elsif ($which eq '64bit3') {
8998: return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443 albertel 8999: } elsif ($which eq '64bit2') {
9000: return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366 albertel 9001: } elsif ($which eq '64bit') {
9002: return &rndseed_64bit($symb,$courseid,$domain,$username);
9003: }
9004: return &rndseed_32bit($symb,$courseid,$domain,$username);
9005: }
9006:
9007: sub rndseed_32bit {
9008: my ($symb,$courseid,$domain,$username)=@_;
9009: {
9010: use integer;
9011: my $symbchck=unpack("%32C*",$symb) << 27;
9012: my $symbseed=numval($symb) << 22;
9013: my $namechck=unpack("%32C*",$username) << 17;
9014: my $nameseed=numval($username) << 12;
9015: my $domainseed=unpack("%32C*",$domain) << 7;
9016: my $courseseed=unpack("%32C*",$courseid);
9017: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790 albertel 9018: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9019: #&logthis("rndseed :$num:$symb");
1.564 albertel 9020: if ($_64bit) { $num=(($num<<32)>>32); }
1.366 albertel 9021: return $num;
9022: }
9023: }
9024:
9025: sub rndseed_64bit {
9026: my ($symb,$courseid,$domain,$username)=@_;
9027: {
9028: use integer;
9029: my $symbchck=unpack("%32S*",$symb) << 21;
9030: my $symbseed=numval($symb) << 10;
9031: my $namechck=unpack("%32S*",$username);
9032:
9033: my $nameseed=numval($username) << 21;
9034: my $domainseed=unpack("%32S*",$domain) << 10;
9035: my $courseseed=unpack("%32S*",$courseid);
9036:
9037: my $num1=$symbchck+$symbseed+$namechck;
9038: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9039: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9040: #&logthis("rndseed :$num:$symb");
1.564 albertel 9041: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366 albertel 9042: return "$num1,$num2";
1.155 albertel 9043: }
1.366 albertel 9044: }
9045:
1.443 albertel 9046: sub rndseed_64bit2 {
9047: my ($symb,$courseid,$domain,$username)=@_;
9048: {
9049: use integer;
9050: # strings need to be an even # of cahracters long, it it is odd the
9051: # last characters gets thrown away
9052: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9053: my $symbseed=numval($symb) << 10;
9054: my $namechck=unpack("%32S*",$username.' ');
9055:
9056: my $nameseed=numval($username) << 21;
1.501 albertel 9057: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9058: my $courseseed=unpack("%32S*",$courseid.' ');
9059:
9060: my $num1=$symbchck+$symbseed+$namechck;
9061: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9062: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9063: #&logthis("rndseed :$num:$symb");
1.803 albertel 9064: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501 albertel 9065: return "$num1,$num2";
9066: }
9067: }
9068:
9069: sub rndseed_64bit3 {
9070: my ($symb,$courseid,$domain,$username)=@_;
9071: {
9072: use integer;
9073: # strings need to be an even # of cahracters long, it it is odd the
9074: # last characters gets thrown away
9075: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9076: my $symbseed=numval2($symb) << 10;
9077: my $namechck=unpack("%32S*",$username.' ');
9078:
9079: my $nameseed=numval2($username) << 21;
1.443 albertel 9080: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9081: my $courseseed=unpack("%32S*",$courseid.' ');
9082:
9083: my $num1=$symbchck+$symbseed+$namechck;
9084: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9085: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9086: #&logthis("rndseed :$num1:$num2:$_64bit");
1.564 albertel 9087: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9088:
1.503 albertel 9089: return "$num1:$num2";
1.443 albertel 9090: }
9091: }
9092:
1.575 albertel 9093: sub rndseed_64bit4 {
9094: my ($symb,$courseid,$domain,$username)=@_;
9095: {
9096: use integer;
9097: # strings need to be an even # of cahracters long, it it is odd the
9098: # last characters gets thrown away
9099: my $symbchck=unpack("%32S*",$symb.' ') << 21;
9100: my $symbseed=numval3($symb) << 10;
9101: my $namechck=unpack("%32S*",$username.' ');
9102:
9103: my $nameseed=numval3($username) << 21;
9104: my $domainseed=unpack("%32S*",$domain.' ') << 10;
9105: my $courseseed=unpack("%32S*",$courseid.' ');
9106:
9107: my $num1=$symbchck+$symbseed+$namechck;
9108: my $num2=$nameseed+$domainseed+$courseseed;
1.790 albertel 9109: #&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
9110: #&logthis("rndseed :$num1:$num2:$_64bit");
1.575 albertel 9111: if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
9112:
9113: return "$num1:$num2";
9114: }
9115: }
9116:
1.675 albertel 9117: sub rndseed_64bit5 {
9118: my ($symb,$courseid,$domain,$username)=@_;
9119: my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
9120: return "$num1:$num2";
9121: }
9122:
1.366 albertel 9123: sub rndseed_CODE_64bit {
9124: my ($symb,$courseid,$domain,$username)=@_;
1.155 albertel 9125: {
1.366 albertel 9126: use integer;
1.443 albertel 9127: my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484 albertel 9128: my $symbseed=numval2($symb);
1.491 albertel 9129: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9130: my $CODEseed=numval(&getCODE());
1.443 albertel 9131: my $courseseed=unpack("%32S*",$courseid.' ');
1.484 albertel 9132: my $num1=$symbseed+$CODEchck;
9133: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9134: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9135: #&logthis("rndseed :$num1:$num2:$symb");
1.564 albertel 9136: if ($_64bit) { $num1=(($num1<<32)>>32); }
9137: if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503 albertel 9138: return "$num1:$num2";
1.366 albertel 9139: }
9140: }
9141:
1.575 albertel 9142: sub rndseed_CODE_64bit4 {
9143: my ($symb,$courseid,$domain,$username)=@_;
9144: {
9145: use integer;
9146: my $symbchck=unpack("%32S*",$symb.' ') << 16;
9147: my $symbseed=numval3($symb);
9148: my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
9149: my $CODEseed=numval3(&getCODE());
9150: my $courseseed=unpack("%32S*",$courseid.' ');
9151: my $num1=$symbseed+$CODEchck;
9152: my $num2=$CODEseed+$courseseed+$symbchck;
1.790 albertel 9153: #&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
9154: #&logthis("rndseed :$num1:$num2:$symb");
1.575 albertel 9155: if ($_64bit) { $num1=(($num1<<32)>>32); }
9156: if ($_64bit) { $num2=(($num2<<32)>>32); }
9157: return "$num1:$num2";
9158: }
9159: }
9160:
1.675 albertel 9161: sub rndseed_CODE_64bit5 {
9162: my ($symb,$courseid,$domain,$username)=@_;
9163: my $code = &getCODE();
9164: my ($num1,$num2)=&digest("$symb,$courseid,$code");
9165: return "$num1:$num2";
9166: }
9167:
1.366 albertel 9168: sub setup_random_from_rndseed {
9169: my ($rndseed)=@_;
1.503 albertel 9170: if ($rndseed =~/([,:])/) {
9171: my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366 albertel 9172: &Math::Random::random_set_seed(abs($num1),abs($num2));
9173: } else {
9174: &Math::Random::random_set_seed_from_phrase($rndseed);
1.98 albertel 9175: }
1.36 albertel 9176: }
9177:
1.474 albertel 9178: sub latest_receipt_algorithm_id {
1.835 albertel 9179: return 'receipt3';
1.474 albertel 9180: }
9181:
1.480 www 9182: sub recunique {
9183: my $fucourseid=shift;
9184: my $unique;
1.835 albertel 9185: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
9186: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9187: $unique=$env{"course.$fucourseid.internal.encseed"};
1.480 www 9188: } else {
9189: $unique=$perlvar{'lonReceipt'};
9190: }
9191: return unpack("%32C*",$unique);
9192: }
9193:
9194: sub recprefix {
9195: my $fucourseid=shift;
9196: my $prefix;
1.835 albertel 9197: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
9198: $env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620 albertel 9199: $prefix=$env{"course.$fucourseid.internal.encpref"};
1.480 www 9200: } else {
9201: $prefix=$perlvar{'lonHostID'};
9202: }
9203: return unpack("%32C*",$prefix);
9204: }
9205:
1.76 www 9206: sub ireceipt {
1.474 albertel 9207: my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835 albertel 9208:
9209: my $return =&recprefix($fucourseid).'-';
9210:
9211: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
9212: $env{'request.state'} eq 'construct') {
9213: $return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
9214: return $return;
9215: }
9216:
1.76 www 9217: my $cuname=unpack("%32C*",$funame);
9218: my $cudom=unpack("%32C*",$fudom);
9219: my $cucourseid=unpack("%32C*",$fucourseid);
9220: my $cusymb=unpack("%32C*",$fusymb);
1.480 www 9221: my $cunique=&recunique($fucourseid);
1.474 albertel 9222: my $cpart=unpack("%32S*",$part);
1.835 albertel 9223: if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
9224:
1.790 albertel 9225: #&logthis("doing receipt2 using parts $cpart, uname $cuname and udom $cudom gets ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474 albertel 9226:
9227: $return.= ($cunique%$cuname+
9228: $cunique%$cudom+
9229: $cusymb%$cuname+
9230: $cusymb%$cudom+
9231: $cucourseid%$cuname+
9232: $cucourseid%$cudom+
9233: $cpart%$cuname+
9234: $cpart%$cudom);
9235: } else {
9236: $return.= ($cunique%$cuname+
9237: $cunique%$cudom+
9238: $cusymb%$cuname+
9239: $cusymb%$cudom+
9240: $cucourseid%$cuname+
9241: $cucourseid%$cudom);
9242: }
9243: return $return;
1.76 www 9244: }
9245:
9246: sub receipt {
1.474 albertel 9247: my ($part)=@_;
1.790 albertel 9248: my ($symb,$courseid,$domain,$name) = &whichuser();
1.474 albertel 9249: return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76 www 9250: }
1.260 ng 9251:
1.790 albertel 9252: sub whichuser {
9253: my ($passedsymb)=@_;
9254: my ($symb,$courseid,$domain,$name,$publicuser);
9255: if (defined($env{'form.grade_symb'})) {
9256: my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
9257: my $allowed=&allowed('vgr',$tmp_courseid);
9258: if (!$allowed &&
9259: exists($env{'request.course.sec'}) &&
9260: $env{'request.course.sec'} !~ /^\s*$/) {
9261: $allowed=&allowed('vgr',$tmp_courseid.
9262: '/'.$env{'request.course.sec'});
9263: }
9264: if ($allowed) {
9265: ($symb)=&get_env_multiple('form.grade_symb');
9266: $courseid=$tmp_courseid;
9267: ($domain)=&get_env_multiple('form.grade_domain');
9268: ($name)=&get_env_multiple('form.grade_username');
9269: return ($symb,$courseid,$domain,$name,$publicuser);
9270: }
9271: }
9272: if (!$passedsymb) {
9273: $symb=&symbread();
9274: } else {
9275: $symb=$passedsymb;
9276: }
9277: $courseid=$env{'request.course.id'};
9278: $domain=$env{'user.domain'};
9279: $name=$env{'user.name'};
9280: if ($name eq 'public' && $domain eq 'public') {
9281: if (!defined($env{'form.username'})) {
9282: $env{'form.username'}.=time.rand(10000000);
9283: }
9284: $name.=$env{'form.username'};
9285: }
9286: return ($symb,$courseid,$domain,$name,$publicuser);
9287:
9288: }
9289:
1.36 albertel 9290: # ------------------------------------------------------------ Serves up a file
1.472 albertel 9291: # returns either the contents of the file or
9292: # -1 if the file doesn't exist
1.481 raeburn 9293: #
9294: # if the target is a file that was uploaded via DOCS,
9295: # a check will be made to see if a current copy exists on the local server,
9296: # if it does this will be served, otherwise a copy will be retrieved from
9297: # the home server for the course and stored in /home/httpd/html/userfiles on
9298: # the local server.
1.472 albertel 9299:
1.36 albertel 9300: sub getfile {
1.538 albertel 9301: my ($file) = @_;
1.609 banghart 9302: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538 albertel 9303: &repcopy($file);
9304: return &readfile($file);
9305: }
9306:
9307: sub repcopy_userfile {
9308: my ($file)=@_;
1.609 banghart 9309: if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610 albertel 9310: if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538 albertel 9311: my ($cdom,$cnum,$filename) =
1.811 albertel 9312: ($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538 albertel 9313: my $uri="/uploaded/$cdom/$cnum/$filename";
9314: if (-e "$file") {
1.828 www 9315: # we already have a local copy, check it out
1.538 albertel 9316: my @fileinfo = stat($file);
1.828 www 9317: my $rtncode;
9318: my $info;
1.538 albertel 9319: my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482 albertel 9320: if ($lwpresp ne 'ok') {
1.828 www 9321: # there is no such file anymore, even though we had a local copy
1.482 albertel 9322: if ($rtncode eq '404') {
1.538 albertel 9323: unlink($file);
1.482 albertel 9324: }
9325: return -1;
9326: }
9327: if ($info < $fileinfo[9]) {
1.828 www 9328: # nice, the file we have is up-to-date, just say okay
1.607 raeburn 9329: return 'ok';
1.828 www 9330: } else {
9331: # the file is outdated, get rid of it
9332: unlink($file);
1.482 albertel 9333: }
1.828 www 9334: }
9335: # one way or the other, at this point, we don't have the file
9336: # construct the correct path for the file
9337: my @parts = ($cdom,$cnum);
9338: if ($filename =~ m|^(.+)/[^/]+$|) {
9339: push @parts, split(/\//,$1);
9340: }
9341: my $path = $perlvar{'lonDocRoot'}.'/userfiles';
9342: foreach my $part (@parts) {
9343: $path .= '/'.$part;
9344: if (!-e $path) {
9345: mkdir($path,0770);
1.482 albertel 9346: }
9347: }
1.828 www 9348: # now the path exists for sure
9349: # get a user agent
9350: my $ua=new LWP::UserAgent;
9351: my $transferfile=$file.'.in.transfer';
9352: # FIXME: this should flock
9353: if (-e $transferfile) { return 'ok'; }
9354: my $request;
9355: $uri=~s/^\///;
1.980 raeburn 9356: my $homeserver = &homeserver($cnum,$cdom);
9357: my $protocol = $protocol{$homeserver};
9358: $protocol = 'http' if ($protocol ne 'https');
9359: $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828 www 9360: my $response=$ua->request($request,$transferfile);
9361: # did it work?
9362: if ($response->is_error()) {
9363: unlink($transferfile);
9364: &logthis("Userfile repcopy failed for $uri");
9365: return -1;
9366: }
9367: # worked, rename the transfer file
9368: rename($transferfile,$file);
1.607 raeburn 9369: return 'ok';
1.481 raeburn 9370: }
9371:
1.517 albertel 9372: sub tokenwrapper {
9373: my $uri=shift;
1.980 raeburn 9374: $uri=~s|^https?\://([^/]+)||;
1.552 albertel 9375: $uri=~s|^/||;
1.620 albertel 9376: $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517 albertel 9377: my $token=$1;
1.552 albertel 9378: my (undef,$udom,$uname,$file)=split('/',$uri,4);
9379: if ($udom && $uname && $file) {
9380: $file=~s|(\?\.*)*$||;
1.949 raeburn 9381: &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980 raeburn 9382: my $homeserver = &homeserver($uname,$udom);
9383: my $protocol = $protocol{$homeserver};
9384: $protocol = 'http' if ($protocol ne 'https');
9385: return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517 albertel 9386: (($uri=~/\?/)?'&':'?').'token='.$token.
9387: '&tokenissued='.$perlvar{'lonHostID'};
9388: } else {
9389: return '/adm/notfound.html';
9390: }
9391: }
9392:
1.828 www 9393: # call with reqtype HEAD: get last modification time
9394: # call with reqtype GET: get the file contents
9395: # Do not call this with reqtype GET for large files! It loads everything into memory
9396: #
1.481 raeburn 9397: sub getuploaded {
9398: my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
9399: $uri=~s/^\///;
1.980 raeburn 9400: my $homeserver = &homeserver($cnum,$cdom);
9401: my $protocol = $protocol{$homeserver};
9402: $protocol = 'http' if ($protocol ne 'https');
9403: $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481 raeburn 9404: my $ua=new LWP::UserAgent;
9405: my $request=new HTTP::Request($reqtype,$uri);
9406: my $response=$ua->request($request);
9407: $$rtncode = $response->code;
1.482 albertel 9408: if (! $response->is_success()) {
9409: return 'failed';
9410: }
9411: if ($reqtype eq 'HEAD') {
1.486 www 9412: $$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482 albertel 9413: } elsif ($reqtype eq 'GET') {
9414: $$info = $response->content;
1.472 albertel 9415: }
1.482 albertel 9416: return 'ok';
1.36 albertel 9417: }
9418:
1.481 raeburn 9419: sub readfile {
9420: my $file = shift;
9421: if ( (! -e $file ) || ($file eq '') ) { return -1; };
9422: my $fh;
9423: open($fh,"<$file");
9424: my $a='';
1.800 albertel 9425: while (my $line = <$fh>) { $a .= $line; }
1.481 raeburn 9426: return $a;
9427: }
9428:
1.36 albertel 9429: sub filelocation {
1.590 banghart 9430: my ($dir,$file) = @_;
9431: my $location;
9432: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700 albertel 9433:
9434: if ($file =~ m-^/adm/-) {
9435: $file=~s-^/adm/wrapper/-/-;
9436: $file=~s-^/adm/coursedocs/showdoc/-/-;
9437: }
1.882 albertel 9438:
1.590 banghart 9439: if ($file=~m:^/~:) { # is a contruction space reference
9440: $location = $file;
9441: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807 albertel 9442: } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649 albertel 9443: # is a correct contruction space reference
9444: $location = $file;
1.956 raeburn 9445: } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
9446: $location = $file;
1.609 banghart 9447: } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590 banghart 9448: my ($udom,$uname,$filename)=
1.811 albertel 9449: ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590 banghart 9450: my $home=&homeserver($uname,$udom);
9451: my $is_me=0;
9452: my @ids=¤t_machine_ids();
9453: foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
9454: if ($is_me) {
1.955 raeburn 9455: $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590 banghart 9456: } else {
9457: $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
9458: $udom.'/'.$uname.'/'.$filename;
9459: }
1.882 albertel 9460: } elsif ($file =~ m-^/adm/-) {
9461: $location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590 banghart 9462: } else {
9463: $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
9464: $file=~s:^/res/:/:;
9465: if ( !( $file =~ m:^/:) ) {
9466: $location = $dir. '/'.$file;
9467: } else {
9468: $location = '/home/httpd/html/res'.$file;
9469: }
1.59 albertel 9470: }
1.590 banghart 9471: $location=~s://+:/:g; # remove duplicate /
1.930 albertel 9472: while ($location=~m{/\.\./}) {
9473: if ($location =~ m{/[^/]+/\.\./}) {
9474: $location=~ s{/[^/]+/\.\./}{/}g;
9475: } else {
9476: $location=~ s{/\.\./}{/}g;
9477: }
9478: } #remove dir/..
1.590 banghart 9479: while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
9480: return $location;
1.46 www 9481: }
1.36 albertel 9482:
1.46 www 9483: sub hreflocation {
9484: my ($dir,$file)=@_;
1.980 raeburn 9485: unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666 albertel 9486: $file=filelocation($dir,$file);
1.700 albertel 9487: } elsif ($file=~m-^/adm/-) {
9488: $file=~s-^/adm/wrapper/-/-;
9489: $file=~s-^/adm/coursedocs/showdoc/-/-;
1.666 albertel 9490: }
9491: if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
9492: $file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807 albertel 9493: } elsif ($file=~m-/home/($match_username)/public_html/-) {
9494: $file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666 albertel 9495: } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811 albertel 9496: $file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666 albertel 9497: -/uploaded/$1/$2/-x;
1.46 www 9498: }
1.913 albertel 9499: if ($file=~ m{^/userfiles/}) {
9500: $file =~ s{^/userfiles/}{/uploaded/};
9501: }
1.462 albertel 9502: return $file;
1.465 albertel 9503: }
9504:
9505: sub current_machine_domains {
1.853 albertel 9506: return &machine_domains(&hostname($perlvar{'lonHostID'}));
9507: }
9508:
9509: sub machine_domains {
9510: my ($hostname) = @_;
1.465 albertel 9511: my @domains;
1.838 albertel 9512: my %hostname = &all_hostnames();
1.465 albertel 9513: while( my($id, $name) = each(%hostname)) {
1.467 matthew 9514: # &logthis("-$id-$name-$hostname-");
1.465 albertel 9515: if ($hostname eq $name) {
1.844 albertel 9516: push(@domains,&host_domain($id));
1.465 albertel 9517: }
9518: }
9519: return @domains;
9520: }
9521:
9522: sub current_machine_ids {
1.853 albertel 9523: return &machine_ids(&hostname($perlvar{'lonHostID'}));
9524: }
9525:
9526: sub machine_ids {
9527: my ($hostname) = @_;
9528: $hostname ||= &hostname($perlvar{'lonHostID'});
1.465 albertel 9529: my @ids;
1.888 albertel 9530: my %name_to_host = &all_names();
1.889 albertel 9531: if (ref($name_to_host{$hostname}) eq 'ARRAY') {
9532: return @{ $name_to_host{$hostname} };
9533: }
9534: return;
1.31 www 9535: }
9536:
1.824 raeburn 9537: sub additional_machine_domains {
9538: my @domains;
9539: open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
9540: while( my $line = <$fh>) {
9541: $line =~ s/\s//g;
9542: push(@domains,$line);
9543: }
9544: return @domains;
9545: }
9546:
9547: sub default_login_domain {
9548: my $domain = $perlvar{'lonDefDomain'};
9549: my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
9550: foreach my $posdom (¤t_machine_domains(),
9551: &additional_machine_domains()) {
9552: if (lc($posdom) eq lc($testdomain)) {
9553: $domain=$posdom;
9554: last;
9555: }
9556: }
9557: return $domain;
9558: }
9559:
1.31 www 9560: # ------------------------------------------------------------- Declutters URLs
9561:
9562: sub declutter {
9563: my $thisfn=shift;
1.569 albertel 9564: if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479 albertel 9565: $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31 www 9566: $thisfn=~s/^\///;
1.697 albertel 9567: $thisfn=~s|^adm/wrapper/||;
9568: $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31 www 9569: $thisfn=~s/^res\///;
1.1032 raeburn 9570: unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
9571: $thisfn=~s/\?.+$//;
9572: }
1.268 www 9573: return $thisfn;
9574: }
9575:
9576: # ------------------------------------------------------------- Clutter up URLs
9577:
9578: sub clutter {
9579: my $thisfn='/'.&declutter(shift);
1.887 albertel 9580: if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884 albertel 9581: || $thisfn =~ m{^/adm/(includes|pages)} ) {
1.270 www 9582: $thisfn='/res'.$thisfn;
9583: }
1.1031 raeburn 9584: if ($thisfn !~m|^/adm|) {
9585: if ($thisfn =~ m|^/ext/|) {
1.694 albertel 9586: $thisfn='/adm/wrapper'.$thisfn;
1.695 albertel 9587: } else {
9588: my ($ext) = ($thisfn =~ /\.(\w+)$/);
9589: my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698 albertel 9590: if ($embstyle eq 'ssi'
9591: || ($embstyle eq 'hdn')
9592: || ($embstyle eq 'rat')
9593: || ($embstyle eq 'prv')
9594: || ($embstyle eq 'ign')) {
9595: #do nothing with these
9596: } elsif (($embstyle eq 'img')
1.695 albertel 9597: || ($embstyle eq 'emb')
9598: || ($embstyle eq 'wrp')) {
9599: $thisfn='/adm/wrapper'.$thisfn;
1.698 albertel 9600: } elsif ($embstyle eq 'unk'
9601: && $thisfn!~/\.(sequence|page)$/) {
1.695 albertel 9602: $thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698 albertel 9603: } else {
1.718 www 9604: # &logthis("Got a blank emb style");
1.695 albertel 9605: }
1.694 albertel 9606: }
9607: }
1.31 www 9608: return $thisfn;
1.12 www 9609: }
9610:
1.787 albertel 9611: sub clutter_with_no_wrapper {
9612: my $uri = &clutter(shift);
9613: if ($uri =~ m-^/adm/-) {
9614: $uri =~ s-^/adm/wrapper/-/-;
9615: $uri =~ s-^/adm/coursedocs/showdoc/-/-;
9616: }
9617: return $uri;
9618: }
9619:
1.557 albertel 9620: sub freeze_escape {
9621: my ($value)=@_;
9622: if (ref($value)) {
9623: $value=&nfreeze($value);
9624: return '__FROZEN__'.&escape($value);
9625: }
9626: return &escape($value);
9627: }
9628:
1.11 www 9629:
1.557 albertel 9630: sub thaw_unescape {
9631: my ($value)=@_;
9632: if ($value =~ /^__FROZEN__/) {
9633: substr($value,0,10,undef);
9634: $value=&unescape($value);
9635: return &thaw($value);
9636: }
9637: return &unescape($value);
9638: }
9639:
1.436 albertel 9640: sub correct_line_ends {
9641: my ($result)=@_;
9642: $$result =~s/\r\n/\n/mg;
9643: $$result =~s/\r/\n/mg;
1.415 albertel 9644: }
1.1 albertel 9645: # ================================================================ Main Program
9646:
1.184 www 9647: sub goodbye {
1.204 albertel 9648: &logthis("Starting Shut down");
1.443 albertel 9649: #not converted to using infrastruture and probably shouldn't be
1.870 albertel 9650: &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443 albertel 9651: #converted
1.599 albertel 9652: # &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870 albertel 9653: &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
9654: # &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
9655: # &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425 albertel 9656: #1.1 only
1.870 albertel 9657: # &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
9658: # &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
9659: # &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
9660: # &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
9661: &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599 albertel 9662: &logthis(sprintf("%-20s is %s",'kicks',$kicks));
9663: &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184 www 9664: &flushcourselogs();
9665: &logthis("Shutting down");
9666: }
9667:
1.852 albertel 9668: sub get_dns {
1.869 albertel 9669: my ($url,$func,$ignore_cache) = @_;
9670: if (!$ignore_cache) {
9671: my ($content,$cached)=
9672: &Apache::lonnet::is_cached_new('dns',$url);
9673: if ($cached) {
9674: &$func($content);
9675: return;
9676: }
9677: }
9678:
9679: my %alldns;
1.852 albertel 9680: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
9681: foreach my $dns (<$config>) {
9682: next if ($dns !~ /^\^(\S*)/x);
1.979 raeburn 9683: my $line = $1;
9684: my ($host,$protocol) = split(/:/,$line);
9685: if ($protocol ne 'https') {
9686: $protocol = 'http';
9687: }
9688: $alldns{$host} = $protocol;
1.869 albertel 9689: }
9690: while (%alldns) {
9691: my ($dns) = keys(%alldns);
1.852 albertel 9692: my $ua=new LWP::UserAgent;
1.979 raeburn 9693: my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852 albertel 9694: my $response=$ua->request($request);
1.979 raeburn 9695: delete($alldns{$dns});
1.852 albertel 9696: next if ($response->is_error());
9697: my @content = split("\n",$response->content);
1.869 albertel 9698: &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852 albertel 9699: &$func(\@content);
1.869 albertel 9700: return;
1.852 albertel 9701: }
9702: close($config);
1.871 albertel 9703: my $which = (split('/',$url))[3];
9704: &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
9705: open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869 albertel 9706: my @content = <$config>;
9707: &$func(\@content);
9708: return;
1.852 albertel 9709: }
1.327 albertel 9710: # ------------------------------------------------------------ Read domain file
9711: {
1.852 albertel 9712: my $loaded;
1.846 albertel 9713: my %domain;
9714:
1.852 albertel 9715: sub parse_domain_tab {
9716: my ($lines) = @_;
9717: foreach my $line (@$lines) {
9718: next if ($line =~ /^(\#|\s*$ )/x);
1.403 www 9719:
1.846 albertel 9720: chomp($line);
1.852 albertel 9721: my ($name,@elements) = split(/:/,$line,9);
1.846 albertel 9722: my %this_domain;
9723: foreach my $field ('description', 'auth_def', 'auth_arg_def',
9724: 'lang_def', 'city', 'longi', 'lati',
9725: 'primary') {
9726: $this_domain{$field} = shift(@elements);
9727: }
9728: $domain{$name} = \%this_domain;
1.852 albertel 9729: }
9730: }
1.864 albertel 9731:
9732: sub reset_domain_info {
9733: undef($loaded);
9734: undef(%domain);
9735: }
9736:
1.852 albertel 9737: sub load_domain_tab {
1.869 albertel 9738: my ($ignore_cache) = @_;
9739: &get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852 albertel 9740: my $fh;
9741: if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
9742: my @lines = <$fh>;
9743: &parse_domain_tab(\@lines);
1.448 albertel 9744: }
1.852 albertel 9745: close($fh);
9746: $loaded = 1;
1.327 albertel 9747: }
1.846 albertel 9748:
9749: sub domain {
1.852 albertel 9750: &load_domain_tab() if (!$loaded);
9751:
1.846 albertel 9752: my ($name,$what) = @_;
9753: return if ( !exists($domain{$name}) );
9754:
9755: if (!$what) {
9756: return $domain{$name}{'description'};
9757: }
9758: return $domain{$name}{$what};
9759: }
1.974 raeburn 9760:
9761: sub domain_info {
9762: &load_domain_tab() if (!$loaded);
9763: return %domain;
9764: }
9765:
1.327 albertel 9766: }
9767:
9768:
1.1 albertel 9769: # ------------------------------------------------------------- Read hosts file
9770: {
1.838 albertel 9771: my %hostname;
1.844 albertel 9772: my %hostdom;
1.845 albertel 9773: my %libserv;
1.852 albertel 9774: my $loaded;
1.888 albertel 9775: my %name_to_host;
1.852 albertel 9776:
9777: sub parse_hosts_tab {
9778: my ($file) = @_;
9779: foreach my $configline (@$file) {
9780: next if ($configline =~ /^(\#|\s*$ )/x);
9781: next if ($configline =~ /^\^/);
9782: chomp($configline);
1.968 raeburn 9783: my ($id,$domain,$role,$name,$protocol)=split(/:/,$configline);
1.852 albertel 9784: $name=~s/\s//g;
9785: if ($id && $domain && $role && $name) {
9786: $hostname{$id}=$name;
1.888 albertel 9787: push(@{$name_to_host{$name}}, $id);
1.852 albertel 9788: $hostdom{$id}=$domain;
9789: if ($role eq 'library') { $libserv{$id}=$name; }
1.969 raeburn 9790: if (defined($protocol)) {
9791: if ($protocol eq 'https') {
9792: $protocol{$id} = $protocol;
9793: } else {
9794: $protocol{$id} = 'http';
9795: }
1.968 raeburn 9796: } else {
1.969 raeburn 9797: $protocol{$id} = 'http';
1.968 raeburn 9798: }
1.852 albertel 9799: }
9800: }
9801: }
1.864 albertel 9802:
9803: sub reset_hosts_info {
1.897 albertel 9804: &purge_remembered();
1.864 albertel 9805: &reset_domain_info();
9806: &reset_hosts_ip_info();
1.892 albertel 9807: undef(%name_to_host);
1.864 albertel 9808: undef(%hostname);
9809: undef(%hostdom);
9810: undef(%libserv);
9811: undef($loaded);
9812: }
1.1 albertel 9813:
1.852 albertel 9814: sub load_hosts_tab {
1.869 albertel 9815: my ($ignore_cache) = @_;
9816: &get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852 albertel 9817: open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
9818: my @config = <$config>;
9819: &parse_hosts_tab(\@config);
9820: close($config);
9821: $loaded=1;
1.1 albertel 9822: }
1.852 albertel 9823:
1.838 albertel 9824: sub hostname {
1.852 albertel 9825: &load_hosts_tab() if (!$loaded);
9826:
1.838 albertel 9827: my ($lonid) = @_;
9828: return $hostname{$lonid};
9829: }
1.845 albertel 9830:
1.838 albertel 9831: sub all_hostnames {
1.852 albertel 9832: &load_hosts_tab() if (!$loaded);
9833:
1.838 albertel 9834: return %hostname;
9835: }
1.845 albertel 9836:
1.888 albertel 9837: sub all_names {
9838: &load_hosts_tab() if (!$loaded);
9839:
9840: return %name_to_host;
9841: }
9842:
1.974 raeburn 9843: sub all_host_domain {
9844: &load_hosts_tab() if (!$loaded);
9845: return %hostdom;
9846: }
9847:
1.845 albertel 9848: sub is_library {
1.852 albertel 9849: &load_hosts_tab() if (!$loaded);
9850:
1.845 albertel 9851: return exists($libserv{$_[0]});
9852: }
9853:
9854: sub all_library {
1.852 albertel 9855: &load_hosts_tab() if (!$loaded);
9856:
1.845 albertel 9857: return %libserv;
9858: }
9859:
1.1062 droeschl 9860: sub unique_library {
9861: #2x reverse removes all hostnames that appear more than once
9862: my %unique = reverse &all_library();
9863: return reverse %unique;
9864: }
9865:
1.841 albertel 9866: sub get_servers {
1.852 albertel 9867: &load_hosts_tab() if (!$loaded);
9868:
1.841 albertel 9869: my ($domain,$type) = @_;
9870: my %possible_hosts = ($type eq 'library') ? %libserv
9871: : %hostname;
9872: my %result;
1.842 albertel 9873: if (ref($domain) eq 'ARRAY') {
9874: while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843 albertel 9875: if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842 albertel 9876: $result{$host} = $hostname;
9877: }
9878: }
9879: } else {
9880: while ( my ($host,$hostname) = each(%possible_hosts)) {
9881: if ($hostdom{$host} eq $domain) {
9882: $result{$host} = $hostname;
9883: }
1.841 albertel 9884: }
9885: }
9886: return %result;
9887: }
1.845 albertel 9888:
1.1062 droeschl 9889: sub get_unique_servers {
9890: my %unique = reverse &get_servers(@_);
9891: return reverse %unique;
9892: }
9893:
1.844 albertel 9894: sub host_domain {
1.852 albertel 9895: &load_hosts_tab() if (!$loaded);
9896:
1.844 albertel 9897: my ($lonid) = @_;
9898: return $hostdom{$lonid};
9899: }
9900:
1.841 albertel 9901: sub all_domains {
1.852 albertel 9902: &load_hosts_tab() if (!$loaded);
9903:
1.841 albertel 9904: my %seen;
9905: my @uniq = grep(!$seen{$_}++, values(%hostdom));
9906: return @uniq;
9907: }
1.1 albertel 9908: }
9909:
1.847 albertel 9910: {
9911: my %iphost;
1.856 albertel 9912: my %name_to_ip;
9913: my %lonid_to_ip;
1.869 albertel 9914:
1.847 albertel 9915: sub get_hosts_from_ip {
9916: my ($ip) = @_;
9917: my %iphosts = &get_iphost();
9918: if (ref($iphosts{$ip})) {
9919: return @{$iphosts{$ip}};
9920: }
9921: return;
1.839 albertel 9922: }
1.864 albertel 9923:
9924: sub reset_hosts_ip_info {
9925: undef(%iphost);
9926: undef(%name_to_ip);
9927: undef(%lonid_to_ip);
9928: }
1.856 albertel 9929:
9930: sub get_host_ip {
9931: my ($lonid) = @_;
9932: if (exists($lonid_to_ip{$lonid})) {
9933: return $lonid_to_ip{$lonid};
9934: }
9935: my $name=&hostname($lonid);
9936: my $ip = gethostbyname($name);
9937: return if (!$ip || length($ip) ne 4);
9938: $ip=inet_ntoa($ip);
9939: $name_to_ip{$name} = $ip;
9940: $lonid_to_ip{$lonid} = $ip;
9941: return $ip;
9942: }
1.847 albertel 9943:
9944: sub get_iphost {
1.869 albertel 9945: my ($ignore_cache) = @_;
1.894 albertel 9946:
1.869 albertel 9947: if (!$ignore_cache) {
9948: if (%iphost) {
9949: return %iphost;
9950: }
9951: my ($ip_info,$cached)=
9952: &Apache::lonnet::is_cached_new('iphost','iphost');
9953: if ($cached) {
9954: %iphost = %{$ip_info->[0]};
9955: %name_to_ip = %{$ip_info->[1]};
9956: %lonid_to_ip = %{$ip_info->[2]};
9957: return %iphost;
9958: }
9959: }
1.894 albertel 9960:
9961: # get yesterday's info for fallback
9962: my %old_name_to_ip;
9963: my ($ip_info,$cached)=
9964: &Apache::lonnet::is_cached_new('iphost','iphost');
9965: if ($cached) {
9966: %old_name_to_ip = %{$ip_info->[1]};
9967: }
9968:
1.888 albertel 9969: my %name_to_host = &all_names();
9970: foreach my $name (keys(%name_to_host)) {
1.847 albertel 9971: my $ip;
9972: if (!exists($name_to_ip{$name})) {
9973: $ip = gethostbyname($name);
9974: if (!$ip || length($ip) ne 4) {
1.894 albertel 9975: if (defined($old_name_to_ip{$name})) {
9976: $ip = $old_name_to_ip{$name};
9977: &logthis("Can't find $name defaulting to old $ip");
9978: } else {
9979: &logthis("Name $name no IP found");
9980: next;
9981: }
9982: } else {
9983: $ip=inet_ntoa($ip);
1.847 albertel 9984: }
9985: $name_to_ip{$name} = $ip;
9986: } else {
9987: $ip = $name_to_ip{$name};
1.653 albertel 9988: }
1.888 albertel 9989: foreach my $id (@{ $name_to_host{$name} }) {
9990: $lonid_to_ip{$id} = $ip;
9991: }
9992: push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598 albertel 9993: }
1.869 albertel 9994: &Apache::lonnet::do_cache_new('iphost','iphost',
9995: [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894 albertel 9996: 48*60*60);
1.869 albertel 9997:
1.847 albertel 9998: return %iphost;
1.598 albertel 9999: }
10000:
1.992 raeburn 10001: #
10002: # Given a DNS returns the loncapa host name for that DNS
10003: #
10004: sub host_from_dns {
10005: my ($dns) = @_;
10006: my @hosts;
10007: my $ip;
10008:
1.993 raeburn 10009: if (exists($name_to_ip{$dns})) {
1.992 raeburn 10010: $ip = $name_to_ip{$dns};
10011: }
10012: if (!$ip) {
10013: $ip = gethostbyname($dns); # Initial translation to IP is in net order.
10014: if (length($ip) == 4) {
10015: $ip = &IO::Socket::inet_ntoa($ip);
10016: }
10017: }
10018: if ($ip) {
10019: @hosts = get_hosts_from_ip($ip);
10020: return $hosts[0];
10021: }
10022: return undef;
1.986 foxr 10023: }
1.992 raeburn 10024:
1.986 foxr 10025: }
10026:
1.862 albertel 10027: BEGIN {
10028:
10029: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
10030: unless ($readit) {
10031: {
10032: my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
10033: %perlvar = (%perlvar,%{$configvars});
10034: }
10035:
10036:
1.1 albertel 10037: # ------------------------------------------------------ Read spare server file
10038: {
1.448 albertel 10039: open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1 albertel 10040:
10041: while (my $configline=<$config>) {
10042: chomp($configline);
1.284 matthew 10043: if ($configline) {
1.784 albertel 10044: my ($host,$type) = split(':',$configline,2);
1.785 albertel 10045: if (!defined($type) || $type eq '') { $type = 'default' };
1.784 albertel 10046: push(@{ $spareid{$type} }, $host);
1.1 albertel 10047: }
10048: }
1.448 albertel 10049: close($config);
1.1 albertel 10050: }
1.11 www 10051: # ------------------------------------------------------------ Read permissions
10052: {
1.448 albertel 10053: open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11 www 10054:
10055: while (my $configline=<$config>) {
1.448 albertel 10056: chomp($configline);
10057: if ($configline) {
10058: my ($role,$perm)=split(/ /,$configline);
10059: if ($perm ne '') { $pr{$role}=$perm; }
10060: }
1.11 www 10061: }
1.448 albertel 10062: close($config);
1.11 www 10063: }
10064:
10065: # -------------------------------------------- Read plain texts for permissions
10066: {
1.448 albertel 10067: open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11 www 10068:
10069: while (my $configline=<$config>) {
1.448 albertel 10070: chomp($configline);
10071: if ($configline) {
1.742 raeburn 10072: my ($short,@plain)=split(/:/,$configline);
10073: %{$prp{$short}} = ();
10074: if (@plain > 0) {
10075: $prp{$short}{'std'} = $plain[0];
10076: for (my $i=1; $i<@plain; $i++) {
10077: $prp{$short}{'alt'.$i} = $plain[$i];
10078: }
10079: }
1.448 albertel 10080: }
1.135 www 10081: }
1.448 albertel 10082: close($config);
1.135 www 10083: }
10084:
10085: # ---------------------------------------------------------- Read package table
10086: {
1.448 albertel 10087: open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135 www 10088:
10089: while (my $configline=<$config>) {
1.483 albertel 10090: if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448 albertel 10091: chomp($configline);
10092: my ($short,$plain)=split(/:/,$configline);
10093: my ($pack,$name)=split(/\&/,$short);
10094: if ($plain ne '') {
10095: $packagetab{$pack.'&'.$name.'&name'}=$name;
10096: $packagetab{$short}=$plain;
10097: }
1.11 www 10098: }
1.448 albertel 10099: close($config);
1.329 matthew 10100: }
10101:
1.1073 ! raeburn 10102: # ---------------------------------------------------------- Read loncaparev table
! 10103: {
! 10104: if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
! 10105: if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
! 10106: while (my $configline=<$config>) {
! 10107: chomp($configline);
! 10108: my ($hostid,$loncaparev)=split(/:/,$configline);
! 10109: $loncaparevs{$hostid}=$loncaparev;
! 10110: }
! 10111: close($config);
! 10112: }
! 10113: }
! 10114: }
! 10115:
! 10116: sub all_loncaparevs {
! 10117: 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);
! 10118: }
! 10119:
1.329 matthew 10120: # ------------- set up temporary directory
10121: {
10122: $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
10123:
1.11 www 10124: }
10125:
1.794 albertel 10126: $memcache=new Cache::Memcached({'servers' => ['127.0.0.1:11211'],
10127: 'compress_threshold'=> 20_000,
10128: });
1.185 www 10129:
1.281 www 10130: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186 www 10131: $dumpcount=0;
1.958 www 10132: $locknum=0;
1.22 www 10133:
1.163 harris41 10134: &logtouch();
1.672 albertel 10135: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195 www 10136: $readit=1;
1.564 albertel 10137: {
10138: use integer;
10139: my $test=(2**32)+1;
1.568 albertel 10140: if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564 albertel 10141: &logthis(" Detected 64bit platform ($_64bit)");
10142: }
1.195 www 10143: }
1.1 albertel 10144: }
1.179 www 10145:
1.1 albertel 10146: 1;
1.191 harris41 10147: __END__
10148:
1.243 albertel 10149: =pod
10150:
1.191 harris41 10151: =head1 NAME
10152:
1.243 albertel 10153: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191 harris41 10154:
10155: =head1 SYNOPSIS
10156:
1.243 albertel 10157: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191 harris41 10158:
10159: &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
10160:
1.243 albertel 10161: Common parameters:
10162:
10163: =over 4
10164:
10165: =item *
10166:
10167: $uname : an internal username (if $cname expecting a course Id specifically)
10168:
10169: =item *
10170:
10171: $udom : a domain (if $cdom expecting a course's domain specifically)
10172:
10173: =item *
10174:
10175: $symb : a resource instance identifier
10176:
10177: =item *
10178:
10179: $namespace : the name of a .db file that contains the data needed or
10180: being set.
10181:
10182: =back
10183:
1.394 bowersj2 10184: =head1 OVERVIEW
1.191 harris41 10185:
1.394 bowersj2 10186: lonnet provides subroutines which interact with the
10187: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
10188: about classes, users, and resources.
1.243 albertel 10189:
10190: For many of these objects you can also use this to store data about
10191: them or modify them in various ways.
1.191 harris41 10192:
1.394 bowersj2 10193: =head2 Symbs
1.191 harris41 10194:
1.394 bowersj2 10195: To identify a specific instance of a resource, LON-CAPA uses symbols
10196: or "symbs"X<symb>. These identifiers are built from the URL of the
10197: map, the resource number of the resource in the map, and the URL of
10198: the resource itself. The latter is somewhat redundant, but might help
10199: if maps change.
10200:
10201: An example is
10202:
10203: msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
10204:
10205: The respective map entry is
10206:
10207: <resource id="19" src="/res/msu/korte/tests/part12.problem"
10208: title="Problem 2">
10209: </resource>
10210:
10211: Symbs are used by the random number generator, as well as to store and
10212: restore data specific to a certain instance of for example a problem.
10213:
10214: =head2 Storing And Retrieving Data
10215:
10216: X<store()>X<cstore()>X<restore()>Three of the most important functions
10217: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
10218: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
10219: is is the non-critical message twin of cstore. These functions are for
10220: handlers to store a perl hash to a user's permanent data space in an
10221: easy manner, and to retrieve it again on another call. It is expected
10222: that a handler would use this once at the beginning to retrieve data,
10223: and then again once at the end to send only the new data back.
10224:
10225: The data is stored in the user's data directory on the user's
10226: homeserver under the ID of the course.
10227:
10228: The hash that is returned by restore will have all of the previous
10229: value for all of the elements of the hash.
10230:
10231: Example:
10232:
10233: #creating a hash
10234: my %hash;
10235: $hash{'foo'}='bar';
10236:
10237: #storing it
10238: &Apache::lonnet::cstore(\%hash);
10239:
10240: #changing a value
10241: $hash{'foo'}='notbar';
10242:
10243: #adding a new value
10244: $hash{'bar'}='foo';
10245: &Apache::lonnet::cstore(\%hash);
10246:
10247: #retrieving the hash
10248: my %history=&Apache::lonnet::restore();
10249:
10250: #print the hash
10251: foreach my $key (sort(keys(%history))) {
10252: print("\%history{$key} = $history{$key}");
10253: }
10254:
10255: Will print out:
1.191 harris41 10256:
1.394 bowersj2 10257: %history{1:foo} = bar
10258: %history{1:keys} = foo:timestamp
10259: %history{1:timestamp} = 990455579
10260: %history{2:bar} = foo
10261: %history{2:foo} = notbar
10262: %history{2:keys} = foo:bar:timestamp
10263: %history{2:timestamp} = 990455580
10264: %history{bar} = foo
10265: %history{foo} = notbar
10266: %history{timestamp} = 990455580
10267: %history{version} = 2
10268:
10269: Note that the special hash entries C<keys>, C<version> and
10270: C<timestamp> were added to the hash. C<version> will be equal to the
10271: total number of versions of the data that have been stored. The
10272: C<timestamp> attribute will be the UNIX time the hash was
10273: stored. C<keys> is available in every historical section to list which
10274: keys were added or changed at a specific historical revision of a
10275: hash.
10276:
10277: B<Warning>: do not store the hash that restore returns directly. This
10278: will cause a mess since it will restore the historical keys as if the
10279: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191 harris41 10280:
1.394 bowersj2 10281: Calling convention:
1.191 harris41 10282:
1.394 bowersj2 10283: my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
10284: &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191 harris41 10285:
1.394 bowersj2 10286: For more detailed information, see lonnet specific documentation.
1.191 harris41 10287:
1.394 bowersj2 10288: =head1 RETURN MESSAGES
1.191 harris41 10289:
1.394 bowersj2 10290: =over 4
1.191 harris41 10291:
1.394 bowersj2 10292: =item * B<con_lost>: unable to contact remote host
1.191 harris41 10293:
1.394 bowersj2 10294: =item * B<con_delayed>: unable to contact remote host, message will be delivered
10295: when the connection is brought back up
1.191 harris41 10296:
1.394 bowersj2 10297: =item * B<con_failed>: unable to contact remote host and unable to save message
10298: for later delivery
1.191 harris41 10299:
1.967 bisitz 10300: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191 harris41 10301:
1.394 bowersj2 10302: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243 albertel 10303: that was requested
1.191 harris41 10304:
1.243 albertel 10305: =back
1.191 harris41 10306:
1.243 albertel 10307: =head1 PUBLIC SUBROUTINES
1.191 harris41 10308:
1.243 albertel 10309: =head2 Session Environment Functions
1.191 harris41 10310:
1.243 albertel 10311: =over 4
1.191 harris41 10312:
1.394 bowersj2 10313: =item *
10314: X<appenv()>
1.949 raeburn 10315: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394 bowersj2 10316: the user envirnoment file, and will be restored for each access this
1.620 albertel 10317: user makes during this session, also modifies the %env for the current
1.949 raeburn 10318: process. Optional rolesarrayref - if defined contains a reference to an array
10319: of roles which are exempt from the restriction on modifying user.role entries
10320: in the user's environment.db and in %env.
1.191 harris41 10321:
10322: =item *
1.394 bowersj2 10323: X<delenv()>
1.987 raeburn 10324: B<delenv($delthis,$regexp)>: removes all items from the session
10325: environment file that begin with $delthis. If the
10326: optional second arg - $regexp - is true, $delthis is treated as a
10327: regular expression, otherwise \Q$delthis\E is used.
10328: The values are also deleted from the current processes %env.
1.191 harris41 10329:
1.795 albertel 10330: =item * get_env_multiple($name)
10331:
10332: gets $name from the %env hash, it seemlessly handles the cases where multiple
10333: values may be defined and end up as an array ref.
10334:
10335: returns an array of values
10336:
1.243 albertel 10337: =back
10338:
10339: =head2 User Information
1.191 harris41 10340:
1.243 albertel 10341: =over 4
1.191 harris41 10342:
10343: =item *
1.394 bowersj2 10344: X<queryauthenticate()>
10345: B<queryauthenticate($uname,$udom)>: try to determine user's current
1.191 harris41 10346: authentication scheme
10347:
10348: =item *
1.394 bowersj2 10349: X<authenticate()>
1.1073 ! raeburn 10350: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394 bowersj2 10351: authenticate user from domain's lib servers (first use the current
10352: one). C<$upass> should be the users password.
1.1073 ! raeburn 10353: $checkdefauth is optional (value is 1 if a check should be made to
! 10354: authenticate user using default authentication method, and allow
! 10355: account creation if username does not have account in the domain).
! 10356: $clientcancheckhost is optional (value is 1 if checking whether the
! 10357: server can host will occur on the client side in lonauth.pm).
1.191 harris41 10358:
10359: =item *
1.394 bowersj2 10360: X<homeserver()>
10361: B<homeserver($uname,$udom)>: find the server which has
10362: the user's directory and files (there must be only one), this caches
10363: the answer, and also caches if there is a borken connection.
1.191 harris41 10364:
10365: =item *
1.394 bowersj2 10366: X<idget()>
10367: B<idget($udom,@ids)>: find the usernames behind a list of IDs
10368: (IDs are a unique resource in a domain, there must be only 1 ID per
10369: username, and only 1 username per ID in a specific domain) (returns
10370: hash: id=>name,id=>name)
1.191 harris41 10371:
10372: =item *
1.394 bowersj2 10373: X<idrget()>
10374: B<idrget($udom,@unames)>: find the IDs behind a list of
10375: usernames (returns hash: name=>id,name=>id)
1.191 harris41 10376:
10377: =item *
1.394 bowersj2 10378: X<idput()>
10379: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191 harris41 10380:
10381: =item *
1.394 bowersj2 10382: X<rolesinit()>
10383: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243 albertel 10384:
10385: =item *
1.551 albertel 10386: X<getsection()>
10387: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243 albertel 10388: course $cname, return section name/number or '' for "not in course"
10389: and '-1' for "no section"
10390:
10391: =item *
1.394 bowersj2 10392: X<userenvironment()>
10393: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243 albertel 10394: passed in @what from the requested user's environment, returns a hash
10395:
1.858 raeburn 10396: =item *
10397: X<userlog_query()>
1.859 albertel 10398: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
10399: activity.log file. %filters defines filters applied when parsing the
10400: log file. These can be start or end timestamps, or the type of action
10401: - log to look for Login or Logout events, check for Checkin or
10402: Checkout, role for role selection. The response is in the form
10403: timestamp1:hostid1:event1×tamp2:hostid2:event2 where events are
10404: escaped strings of the action recorded in the activity.log file.
1.858 raeburn 10405:
1.243 albertel 10406: =back
10407:
10408: =head2 User Roles
10409:
10410: =over 4
10411:
10412: =item *
10413:
1.810 raeburn 10414: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243 albertel 10415: F: full access
10416: U,I,K: authentication modes (cxx only)
10417: '': forbidden
10418: 1: user needs to choose course
10419: 2: browse allowed
1.766 albertel 10420: A: passphrase authentication needed
1.243 albertel 10421:
10422: =item *
10423:
10424: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
10425: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
10426: and course level
10427:
10428: =item *
10429:
1.988 raeburn 10430: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash
10431: (rolesplain.tab); plain text explanation of a user role term.
1.1008 raeburn 10432: $type is Course (default) or Community.
1.988 raeburn 10433: If $forcedefault evaluates to true, text returned will be default
10434: text for $type. Otherwise, if this is a course, the text returned
10435: will be a custom name for the role (if defined in the course's
10436: environment). If no custom name is defined the default is returned.
10437:
1.832 raeburn 10438: =item *
10439:
1.935 raeburn 10440: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858 raeburn 10441: All arguments are optional. Returns a hash of a roles, either for
10442: co-author/assistant author roles for a user's Construction Space
1.906 albertel 10443: (default), or if $context is 'userroles', roles for the user himself,
1.933 raeburn 10444: In the hash, keys are set to colon-separated $uname,$udom,$role, and
10445: (optionally) if $withsec is true, a fourth colon-separated item - $section.
10446: For each key, value is set to colon-separated start and end times for
10447: the role. If no username and domain are specified, will default to
1.934 raeburn 10448: current user/domain. Types, roles, and roledoms are references to arrays
1.858 raeburn 10449: of role statuses (active, future or previous), roles
10450: (e.g., cc,in, st etc.) and domains of the roles which can be used
10451: to restrict the list of roles reported. If no array ref is
10452: provided for types, will default to return only active roles.
1.834 albertel 10453:
1.243 albertel 10454: =back
10455:
10456: =head2 User Modification
10457:
10458: =over 4
10459:
10460: =item *
10461:
1.957 raeburn 10462: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243 albertel 10463: user for the level given by URL. Optional start and end dates (leave empty
10464: string or zero for "no date")
1.191 harris41 10465:
10466: =item *
10467:
1.243 albertel 10468: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
10469: change a users, password, possible return values are: ok,
10470: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
10471: refused
1.191 harris41 10472:
10473: =item *
10474:
1.243 albertel 10475: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191 harris41 10476:
10477: =item *
10478:
1.1058 raeburn 10479: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
10480: $forceid,$desiredhome,$email,$inststatus,$candelete) :
10481:
10482: will update user information (firstname,middlename,lastname,generation,
10483: permanentemail), and if forceid is true, student/employee ID also.
10484: A user's institutional affiliation(s) can also be updated.
10485: User information fields will not be overwritten with empty entries
10486: unless the field is included in the $candelete array reference.
10487: This array is included when a single user is modified via "Manage Users",
10488: or when Autoupdate.pl is run by cron in a domain.
1.191 harris41 10489:
10490: =item *
10491:
1.286 matthew 10492: modifystudent
10493:
1.957 raeburn 10494: modify a student's enrollment and identification information.
1.286 matthew 10495: The course id is resolved based on the current users environment.
10496: This means the envoking user must be a course coordinator or otherwise
10497: associated with a course.
10498:
1.297 matthew 10499: This call is essentially a wrapper for lonnet::modifyuser and
10500: lonnet::modify_student_enrollment
1.286 matthew 10501:
10502: Inputs:
10503:
10504: =over 4
10505:
1.957 raeburn 10506: =item B<$udom> Student's loncapa domain
1.286 matthew 10507:
1.957 raeburn 10508: =item B<$uname> Student's loncapa login name
1.286 matthew 10509:
1.964 bisitz 10510: =item B<$uid> Student/Employee ID
1.286 matthew 10511:
1.957 raeburn 10512: =item B<$umode> Student's authentication mode
1.286 matthew 10513:
1.957 raeburn 10514: =item B<$upass> Student's password
1.286 matthew 10515:
1.957 raeburn 10516: =item B<$first> Student's first name
1.286 matthew 10517:
1.957 raeburn 10518: =item B<$middle> Student's middle name
1.286 matthew 10519:
1.957 raeburn 10520: =item B<$last> Student's last name
1.286 matthew 10521:
1.957 raeburn 10522: =item B<$gene> Student's generation
1.286 matthew 10523:
1.957 raeburn 10524: =item B<$usec> Student's section in course
1.286 matthew 10525:
10526: =item B<$end> Unix time of the roles expiration
10527:
10528: =item B<$start> Unix time of the roles start date
10529:
10530: =item B<$forceid> If defined, allow $uid to be changed
10531:
10532: =item B<$desiredhome> server to use as home server for student
10533:
1.957 raeburn 10534: =item B<$email> Student's permanent e-mail address
10535:
10536: =item B<$type> Type of enrollment (auto or manual)
10537:
1.963 raeburn 10538: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto
10539:
10540: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957 raeburn 10541:
1.963 raeburn 10542: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957 raeburn 10543:
1.963 raeburn 10544: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957 raeburn 10545:
1.963 raeburn 10546: =item B<$inststatus> institutional status of user - : separated string of escaped status types
1.957 raeburn 10547:
1.286 matthew 10548: =back
1.297 matthew 10549:
10550: =item *
10551:
10552: modify_student_enrollment
10553:
10554: Change a students enrollment status in a class. The environment variable
10555: 'role.request.course' must be defined for this function to proceed.
10556:
10557: Inputs:
10558:
10559: =over 4
10560:
10561: =item $udom, students domain
10562:
10563: =item $uname, students name
10564:
10565: =item $uid, students user id
10566:
10567: =item $first, students first name
10568:
10569: =item $middle
10570:
10571: =item $last
10572:
10573: =item $gene
10574:
10575: =item $usec
10576:
10577: =item $end
10578:
10579: =item $start
10580:
1.957 raeburn 10581: =item $type
10582:
10583: =item $locktype
10584:
10585: =item $cid
10586:
10587: =item $selfenroll
10588:
10589: =item $context
10590:
1.297 matthew 10591: =back
10592:
1.191 harris41 10593:
10594: =item *
10595:
1.243 albertel 10596: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
10597: custom role; give a custom role to a user for the level given by URL. Specify
10598: name and domain of role author, and role name
1.191 harris41 10599:
10600: =item *
10601:
1.243 albertel 10602: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191 harris41 10603:
10604: =item *
10605:
1.243 albertel 10606: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
10607:
10608: =back
10609:
10610: =head2 Course Infomation
10611:
10612: =over 4
1.191 harris41 10613:
10614: =item *
10615:
1.631 albertel 10616: coursedescription($courseid) : returns a hash of information about the
10617: specified course id, including all environment settings for the
10618: course, the description of the course will be in the hash under the
10619: key 'description'
1.191 harris41 10620:
10621: =item *
10622:
1.624 albertel 10623: resdata($name,$domain,$type,@which) : request for current parameter
10624: setting for a specific $type, where $type is either 'course' or 'user',
10625: @what should be a list of parameters to ask about. This routine caches
10626: answers for 5 minutes.
1.243 albertel 10627:
1.877 foxr 10628: =item *
10629:
10630: get_courseresdata($courseid, $domain) : dump the entire course resource
10631: data base, returning a hash that is keyed by the resource name and has
10632: values that are the resource value. I believe that the timestamps and
10633: versions are also returned.
10634:
10635:
1.243 albertel 10636: =back
10637:
10638: =head2 Course Modification
10639:
10640: =over 4
1.191 harris41 10641:
10642: =item *
10643:
1.243 albertel 10644: writecoursepref($courseid,%prefs) : write preferences (environment
10645: database) for a course
1.191 harris41 10646:
10647: =item *
10648:
1.1011 raeburn 10649: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
10650:
10651: =item *
10652:
1.1038 raeburn 10653: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243 albertel 10654:
10655: =back
10656:
10657: =head2 Resource Subroutines
10658:
10659: =over 4
1.191 harris41 10660:
10661: =item *
10662:
1.243 albertel 10663: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191 harris41 10664:
10665: =item *
10666:
1.243 albertel 10667: repcopy($filename) : subscribes to the requested file, and attempts to
10668: replicate from the owning library server, Might return
1.607 raeburn 10669: 'unavailable', 'not_found', 'forbidden', 'ok', or
10670: 'bad_request', also attempts to grab the metadata for the
1.243 albertel 10671: resource. Expects the local filesystem pathname
10672: (/home/httpd/html/res/....)
10673:
10674: =back
10675:
10676: =head2 Resource Information
10677:
10678: =over 4
1.191 harris41 10679:
10680: =item *
10681:
1.243 albertel 10682: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
10683: a vairety of different possible values, $varname should be a request
10684: string, and the other parameters can be used to specify who and what
10685: one is asking about.
10686:
10687: Possible values for $varname are environment.lastname (or other item
10688: from the envirnment hash), user.name (or someother aspect about the
10689: user), resource.0.maxtries (or some other part and parameter of a
10690: resource)
1.204 albertel 10691:
10692: =item *
10693:
1.243 albertel 10694: directcondval($number) : get current value of a condition; reads from a state
10695: string
1.204 albertel 10696:
10697: =item *
10698:
1.243 albertel 10699: condval($condidx) : value of condition index based on state
1.204 albertel 10700:
10701: =item *
10702:
1.243 albertel 10703: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
10704: resource's metadata, $what should be either a specific key, or either
10705: 'keys' (to get a list of possible keys) or 'packages' to get a list of
10706: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
10707:
10708: this function automatically caches all requests
1.191 harris41 10709:
10710: =item *
10711:
1.243 albertel 10712: metadata_query($query,$custom,$customshow) : make a metadata query against the
10713: network of library servers; returns file handle of where SQL and regex results
10714: will be stored for query
1.191 harris41 10715:
10716: =item *
10717:
1.243 albertel 10718: symbread($filename) : return symbolic list entry (filename argument optional);
10719: returns the data handle
1.191 harris41 10720:
10721: =item *
10722:
1.243 albertel 10723: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582 albertel 10724: a possible symb for the URL in $thisfn, and if is an encryypted
10725: resource that the user accessed using /enc/ returns a 1 on success, 0
10726: on failure, user must be in a course, as it assumes the existance of
1.620 albertel 10727: the course initial hash, and uses $env('request.course.id'}
1.243 albertel 10728:
1.191 harris41 10729:
10730: =item *
10731:
1.243 albertel 10732: symbclean($symb) : removes versions numbers from a symb, returns the
10733: cleaned symb
1.191 harris41 10734:
10735: =item *
10736:
1.243 albertel 10737: is_on_map($uri) : checks if the $uri is somewhere on the current
10738: course map, user must be in a course for it to work.
1.191 harris41 10739:
10740: =item *
10741:
1.243 albertel 10742: numval($salt) : return random seed value (addend for rndseed)
1.191 harris41 10743:
10744: =item *
10745:
1.243 albertel 10746: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
10747: a random seed, all arguments are optional, if they aren't sent it uses the
10748: environment to derive them. Note: if symb isn't sent and it can't get one
10749: from &symbread it will use the current time as its return value
1.191 harris41 10750:
10751: =item *
10752:
1.243 albertel 10753: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
10754: unfakeable, receipt
1.191 harris41 10755:
10756: =item *
10757:
1.620 albertel 10758: receipt() : API to ireceipt working off of env values; given out to users
1.191 harris41 10759:
10760: =item *
10761:
1.243 albertel 10762: countacc($url) : count the number of accesses to a given URL
1.191 harris41 10763:
10764: =item *
10765:
1.243 albertel 10766: 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 10767:
10768: =item *
10769:
1.243 albertel 10770: 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 10771:
10772: =item *
10773:
1.243 albertel 10774: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191 harris41 10775:
10776: =item *
10777:
1.243 albertel 10778: devalidate($symb) : devalidate temporary spreadsheet calculations,
10779: forcing spreadsheet to reevaluate the resource scores next time.
10780:
10781: =back
10782:
10783: =head2 Storing/Retreiving Data
10784:
10785: =over 4
1.191 harris41 10786:
10787: =item *
10788:
1.243 albertel 10789: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
10790: for this url; hashref needs to be given and should be a \%hashname; the
10791: remaining args aren't required and if they aren't passed or are '' they will
1.620 albertel 10792: be derived from the env
1.191 harris41 10793:
10794: =item *
10795:
1.243 albertel 10796: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
10797: uses critical subroutine
1.191 harris41 10798:
10799: =item *
10800:
1.243 albertel 10801: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
10802: all args are optional
1.191 harris41 10803:
10804: =item *
10805:
1.717 albertel 10806: dumpstore($namespace,$udom,$uname,$regexp,$range) :
10807: dumps the complete (or key matching regexp) namespace into a hash
10808: ($udom, $uname, $regexp, $range are optional) for a namespace that is
10809: normally &store()ed into
10810:
10811: $range should be either an integer '100' (give me the first 100
10812: matching records)
10813: or be two integers sperated by a - with no spaces
10814: '30-50' (give me the 30th through the 50th matching
10815: records)
10816:
10817:
10818: =item *
10819:
10820: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
10821: replaces a &store() version of data with a replacement set of data
10822: for a particular resource in a namespace passed in the $storehash hash
10823: reference
10824:
10825: =item *
10826:
1.243 albertel 10827: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
10828: works very similar to store/cstore, but all data is stored in a
10829: temporary location and can be reset using tmpreset, $storehash should
10830: be a hash reference, returns nothing on success
1.191 harris41 10831:
10832: =item *
10833:
1.243 albertel 10834: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
10835: similar to restore, but all data is stored in a temporary location and
10836: can be reset using tmpreset. Returns a hash of values on success,
10837: error string otherwise.
1.191 harris41 10838:
10839: =item *
10840:
1.243 albertel 10841: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
10842: deltes all keys for $symb form the temporary storage hash.
1.191 harris41 10843:
10844: =item *
10845:
1.243 albertel 10846: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
10847: reference filled in from namesp ($udom and $uname are optional)
1.191 harris41 10848:
10849: =item *
10850:
1.243 albertel 10851: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
10852: namesp ($udom and $uname are optional)
1.191 harris41 10853:
10854: =item *
10855:
1.702 albertel 10856: dump($namespace,$udom,$uname,$regexp,$range) :
1.243 albertel 10857: dumps the complete (or key matching regexp) namespace into a hash
1.702 albertel 10858: ($udom, $uname, $regexp, $range are optional)
1.449 matthew 10859:
1.702 albertel 10860: $range should be either an integer '100' (give me the first 100
10861: matching records)
10862: or be two integers sperated by a - with no spaces
10863: '30-50' (give me the 30th through the 50th matching
10864: records)
1.449 matthew 10865: =item *
10866:
10867: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
10868: $store can be a scalar, an array reference, or if the amount to be
10869: incremented is > 1, a hash reference.
10870:
10871: ($udom and $uname are optional)
1.191 harris41 10872:
10873: =item *
10874:
1.243 albertel 10875: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
10876: ($udom and $uname are optional)
1.191 harris41 10877:
10878: =item *
10879:
1.243 albertel 10880: cput($namespace,$storehash,$udom,$uname) : critical put
10881: ($udom and $uname are optional)
1.191 harris41 10882:
10883: =item *
10884:
1.748 albertel 10885: newput($namespace,$storehash,$udom,$uname) :
10886:
10887: Attempts to store the items in the $storehash, but only if they don't
10888: currently exist, if this succeeds you can be certain that you have
10889: successfully created a new key value pair in the $namespace db.
10890:
10891:
10892: Args:
10893: $namespace: name of database to store values to
10894: $storehash: hashref to store to the db
10895: $udom: (optional) domain of user containing the db
10896: $uname: (optional) name of user caontaining the db
10897:
10898: Returns:
10899: 'ok' -> succeeded in storing all keys of $storehash
10900: 'key_exists: <key>' -> failed to anything out of $storehash, as at
10901: least <key> already existed in the db (other
10902: requested keys may also already exist)
1.967 bisitz 10903: 'error: <msg>' -> unable to tie the DB or other error occurred
1.748 albertel 10904: 'con_lost' -> unable to contact request server
10905: 'refused' -> action was not allowed by remote machine
10906:
10907:
10908: =item *
10909:
1.243 albertel 10910: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
10911: reference filled in from namesp (encrypts the return communication)
10912: ($udom and $uname are optional)
1.191 harris41 10913:
10914: =item *
10915:
1.243 albertel 10916: log($udom,$name,$home,$message) : write to permanent log for user; use
10917: critical subroutine
10918:
1.806 raeburn 10919: =item *
10920:
1.860 raeburn 10921: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
10922: array reference filled in from namespace found in domain level on either
10923: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806 raeburn 10924:
10925: =item *
10926:
1.860 raeburn 10927: put_dom($namespace,$storehash,$udom,$uhome) : stores hash in namespace at
10928: domain level either on specified domain server ($uhome) or primary domain
10929: server ($udom and $uhome are optional)
1.806 raeburn 10930:
1.943 raeburn 10931: =item *
10932:
10933: get_domain_defaults($target_domain) : returns hash with defaults for
10934: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
10935: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
10936: or localauth), initial password or a kerberos realm, language (e.g., en-us).
10937: Values are retrieved from cache (if current), or from domain's configuration.db
10938: (if available), or lastly from values in lonTabs/dns_domain,tab,
10939: or lonTabs/domain.tab.
10940:
10941: %domdefaults = &get_auth_defaults($target_domain);
10942:
1.243 albertel 10943: =back
10944:
10945: =head2 Network Status Functions
10946:
10947: =over 4
1.191 harris41 10948:
10949: =item *
10950:
10951: dirlist($uri) : return directory list based on URI
10952:
10953: =item *
10954:
1.243 albertel 10955: spareserver() : find server with least workload from spare.tab
10956:
1.986 foxr 10957:
10958: =item *
10959:
10960: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
10961: if there is no corresponding loncapa host.
10962:
1.243 albertel 10963: =back
10964:
1.986 foxr 10965:
1.243 albertel 10966: =head2 Apache Request
10967:
10968: =over 4
1.191 harris41 10969:
10970: =item *
10971:
1.243 albertel 10972: ssi($url,%hash) : server side include, does a complete request cycle on url to
10973: localhost, posts hash
10974:
10975: =back
10976:
10977: =head2 Data to String to Data
10978:
10979: =over 4
1.191 harris41 10980:
10981: =item *
10982:
1.243 albertel 10983: hash2str(%hash) : convert a hash into a string complete with escaping and '='
10984: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191 harris41 10985:
10986: =item *
10987:
1.243 albertel 10988: hashref2str($hashref) : convert a hashref into a string complete with
10989: escaping and '=' and '&' separators, supports elements that are
10990: arrayrefs and hashrefs
1.191 harris41 10991:
10992: =item *
10993:
1.243 albertel 10994: arrayref2str($arrayref) : convert an arrayref into a string complete
10995: with escaping and '&' separators, supports elements that are arrayrefs
10996: and hashrefs
1.191 harris41 10997:
10998: =item *
10999:
1.243 albertel 11000: str2hash($string) : convert string to hash using unescaping and
11001: splitting on '=' and '&', supports elements that are arrayrefs and
11002: hashrefs
1.191 harris41 11003:
11004: =item *
11005:
1.243 albertel 11006: str2array($string) : convert string to hash using unescaping and
11007: splitting on '&', supports elements that are arrayrefs and hashrefs
11008:
11009: =back
11010:
11011: =head2 Logging Routines
11012:
11013: =over 4
11014:
11015: These routines allow one to make log messages in the lonnet.log and
11016: lonnet.perm logfiles.
1.191 harris41 11017:
11018: =item *
11019:
1.243 albertel 11020: logtouch() : make sure the logfile, lonnet.log, exists
1.191 harris41 11021:
11022: =item *
11023:
1.243 albertel 11024: logthis() : append message to the normal lonnet.log file, it gets
11025: preiodically rolled over and deleted.
1.191 harris41 11026:
11027: =item *
11028:
1.243 albertel 11029: logperm() : append a permanent message to lonnet.perm.log, this log
11030: file never gets deleted by any automated portion of the system, only
11031: messages of critical importance should go in here.
11032:
11033: =back
11034:
11035: =head2 General File Helper Routines
11036:
11037: =over 4
1.191 harris41 11038:
11039: =item *
11040:
1.481 raeburn 11041: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
11042: (a) files in /uploaded
11043: (i) If a local copy of the file exists -
11044: compares modification date of local copy with last-modified date for
11045: definitive version stored on home server for course. If local copy is
11046: stale, requests a new version from the home server and stores it.
11047: If the original has been removed from the home server, then local copy
11048: is unlinked.
11049: (ii) If local copy does not exist -
11050: requests the file from the home server and stores it.
11051:
11052: If $caller is 'uploadrep':
11053: This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
11054: for request for files originally uploaded via DOCS.
11055: - returns 'ok' if fresh local copy now available, -1 otherwise.
11056:
11057: Otherwise:
11058: This indicates a call from the content generation phase of the request.
11059: - returns the entire contents of the file or -1.
11060:
11061: (b) files in /res
11062: - returns the entire contents of a file or -1;
11063: it properly subscribes to and replicates the file if neccessary.
1.191 harris41 11064:
1.712 albertel 11065:
11066: =item *
11067:
11068: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
11069: reference
11070:
11071: returns either a stat() list of data about the file or an empty list
11072: if the file doesn't exist or couldn't find out about it (connection
11073: problems or user unknown)
11074:
1.191 harris41 11075: =item *
11076:
1.243 albertel 11077: filelocation($dir,$file) : returns file system location of a file
11078: based on URI; meant to be "fairly clean" absolute reference, $dir is a
11079: directory that relative $file lookups are to looked in ($dir of /a/dir
11080: and a file of ../bob will become /a/bob)
1.191 harris41 11081:
11082: =item *
11083:
11084: hreflocation($dir,$file) : returns file system location or a URL; same as
11085: filelocation except for hrefs
11086:
11087: =item *
11088:
11089: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
11090:
1.243 albertel 11091: =back
11092:
1.608 albertel 11093: =head2 Usererfile file routines (/uploaded*)
11094:
11095: =over 4
11096:
11097: =item *
11098:
11099: userfileupload(): main rotine for putting a file in a user or course's
11100: filespace, arguments are,
11101:
1.620 albertel 11102: formname - required - this is the name of the element in $env where the
1.608 albertel 11103: filename, and the contents of the file to create/modifed exist
1.620 albertel 11104: the filename is in $env{'form.'.$formname.'.filename'} and the
11105: contents of the file is located in $env{'form.'.$formname}
1.608 albertel 11106: coursedoc - if true, store the file in the course of the active role
11107: of the current user
11108: subdir - required - subdirectory to put the file in under ../userfiles/
11109: if undefined, it will be placed in "unknown"
11110:
11111: (This routine calls clean_filename() to remove any dangerous
11112: characters from the filename, and then calls finuserfileupload() to
11113: complete the transaction)
11114:
11115: returns either the url of the uploaded file (/uploaded/....) if successful
11116: and /adm/notfound.html if unsuccessful
11117:
11118: =item *
11119:
11120: clean_filename(): routine for cleaing a filename up for storage in
11121: userfile space, argument is:
11122:
11123: filename - proposed filename
11124:
11125: returns: the new clean filename
11126:
11127: =item *
11128:
11129: finishuserfileupload(): routine that creaes and sends the file to
11130: userspace, probably shouldn't be called directly
11131:
11132: docuname: username or courseid of destination for the file
11133: docudom: domain of user/course of destination for the file
11134: formname: same as for userfileupload()
11135: fname: filename (inculding subdirectories) for the file
11136:
11137: returns either the url of the uploaded file (/uploaded/....) if successful
11138: and /adm/notfound.html if unsuccessful
11139:
11140: =item *
11141:
11142: renameuserfile(): renames an existing userfile to a new name
11143:
11144: Args:
11145: docuname: username or courseid of destination for the file
11146: docudom: domain of user/course of destination for the file
11147: old: current file name (including any subdirs under userfiles)
11148: new: desired file name (including any subdirs under userfiles)
11149:
11150: =item *
11151:
11152: mkdiruserfile(): creates a directory is a userfiles dir
11153:
11154: Args:
11155: docuname: username or courseid of destination for the file
11156: docudom: domain of user/course of destination for the file
11157: dir: dir to create (including any subdirs under userfiles)
11158:
11159: =item *
11160:
11161: removeuserfile(): removes a file that exists in userfiles
11162:
11163: Args:
11164: docuname: username or courseid of destination for the file
11165: docudom: domain of user/course of destination for the file
11166: fname: filname to delete (including any subdirs under userfiles)
11167:
11168: =item *
11169:
11170: removeuploadedurl(): convience function for removeuserfile()
11171:
11172: Args:
11173: url: a full /uploaded/... url to delete
11174:
1.747 albertel 11175: =item *
11176:
11177: get_portfile_permissions():
11178: Args:
11179: domain: domain of user or course contain the portfolio files
11180: user: name of user or num of course contain the portfolio files
11181: Returns:
11182: hashref of a dump of the proper file_permissions.db
11183:
11184:
11185: =item *
11186:
11187: get_access_controls():
11188:
11189: Args:
11190: current_permissions: the hash ref returned from get_portfile_permissions()
11191: group: (optional) the group you want the files associated with
11192: file: (optional) the file you want access info on
11193:
11194: Returns:
1.749 raeburn 11195: a hash (keys are file names) of hashes containing
11196: keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
11197: values are XML containing access control settings (see below)
1.747 albertel 11198:
11199: Internal notes:
11200:
1.749 raeburn 11201: access controls are stored in file_permissions.db as key=value pairs.
11202: key -> path to file/file_name\0uniqueID:scope_end_start
11203: where scope -> public,guest,course,group,domains or users.
11204: end -> UNIX time for end of access (0 -> no end date)
11205: start -> UNIX time for start of access
11206:
11207: value -> XML description of access control
11208: <scope type=""> (type =1 of: public,guest,course,group,domains,users">
11209: <start></start>
11210: <end></end>
11211:
11212: <password></password> for scope type = guest
11213:
11214: <domain></domain> for scope type = course or group
11215: <number></number>
11216: <roles id="">
11217: <role></role>
11218: <access></access>
11219: <section></section>
11220: <group></group>
11221: </roles>
11222:
11223: <dom></dom> for scope type = domains
11224:
11225: <users> for scope type = users
11226: <user>
11227: <uname></uname>
11228: <udom></udom>
11229: </user>
11230: </users>
11231: </scope>
11232:
11233: Access data is also aggregated for each file in an additional key=value pair:
11234: key -> path to file/file_name\0accesscontrol
11235: value -> reference to hash
11236: hash contains key = value pairs
11237: where key = uniqueID:scope_end_start
11238: value = UNIX time record was last updated
11239:
11240: Used to improve speed of look-ups of access controls for each file.
11241:
11242: Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
11243:
11244: modify_access_controls():
11245:
11246: Modifies access controls for a portfolio file
11247: Args
11248: 1. file name
11249: 2. reference to hash of required changes,
11250: 3. domain
11251: 4. username
11252: where domain,username are the domain of the portfolio owner
11253: (either a user or a course)
11254:
11255: Returns:
11256: 1. result of additions or updates ('ok' or 'error', with error message).
11257: 2. result of deletions ('ok' or 'error', with error message).
11258: 3. reference to hash of any new or updated access controls.
11259: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
11260: key = integer (inbound ID)
11261: value = uniqueID
1.747 albertel 11262:
1.608 albertel 11263: =back
11264:
1.243 albertel 11265: =head2 HTTP Helper Routines
11266:
11267: =over 4
11268:
1.191 harris41 11269: =item *
11270:
11271: escape() : unpack non-word characters into CGI-compatible hex codes
11272:
11273: =item *
11274:
11275: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
11276:
1.243 albertel 11277: =back
11278:
11279: =head1 PRIVATE SUBROUTINES
11280:
11281: =head2 Underlying communication routines (Shouldn't call)
11282:
11283: =over 4
11284:
11285: =item *
11286:
11287: subreply() : tries to pass a message to lonc, returns con_lost if incapable
11288:
11289: =item *
11290:
11291: reply() : uses subreply to send a message to remote machine, logs all failures
11292:
11293: =item *
11294:
11295: critical() : passes a critical message to another server; if cannot
11296: get through then place message in connection buffer directory and
11297: returns con_delayed, if incapable of saving message, returns
11298: con_failed
11299:
11300: =item *
11301:
11302: reconlonc() : tries to reconnect lonc client processes.
11303:
11304: =back
11305:
11306: =head2 Resource Access Logging
11307:
11308: =over 4
11309:
11310: =item *
11311:
11312: flushcourselogs() : flush (save) buffer logs and access logs
11313:
11314: =item *
11315:
11316: courselog($what) : save message for course in hash
11317:
11318: =item *
11319:
11320: courseacclog($what) : save message for course using &courselog(). Perform
11321: special processing for specific resource types (problems, exams, quizzes, etc).
11322:
1.191 harris41 11323: =item *
11324:
11325: goodbye() : flush course logs and log shutting down; it is called in srm.conf
11326: as a PerlChildExitHandler
1.243 albertel 11327:
11328: =back
11329:
11330: =head2 Other
11331:
11332: =over 4
11333:
11334: =item *
11335:
11336: symblist($mapname,%newhash) : update symbolic storage links
1.191 harris41 11337:
11338: =back
11339:
11340: =cut
1.877 foxr 11341:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>