Annotation of loncom/cgi/listdomconfig.pl, revision 1.2

1.1       raeburn     1: #!/usr/bin/perl
                      2: $|=1;
                      3: # Domain Configuration Dump
1.2     ! raeburn     4: # $Id: listdomconfig.pl,v 1.1 2011/10/21 20:23:36 raeburn Exp $
1.1       raeburn     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: #
                     28: #############################################
                     29: #############################################
                     30: 
                     31: =pod
                     32: 
                     33: =head1 NAME
                     34: 
                     35: listdomconfig.pl
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: CGI script to display domain configuration as plain text.
                     40: 
                     41: =head1 Subroutines
                     42: 
                     43: =over 4
                     44: 
                     45: =cut
                     46: 
                     47: #############################################
                     48: #############################################
                     49: 
                     50: use strict;
                     51: 
                     52: use lib '/home/httpd/lib/perl/';
                     53: use LONCAPA::loncgi;
                     54: use LONCAPA::lonauthcgi;
                     55: use Apache::lonnet();
                     56: use Apache::lonlocal;
                     57: use LONCAPA;
                     58: use GDBM_File;
                     59: use Data::Dumper;
                     60: use Storable qw(thaw);
                     61: use GDBM_File;
                     62: 
                     63: print &LONCAPA::loncgi::cgi_header('text/plain',1);
                     64: 
                     65: &main();
                     66: exit 0;
                     67: 
                     68: #############################################
                     69: #############################################
                     70: 
                     71: =pod
                     72: 
                     73: =item main()
                     74: 
                     75: Inputs: None
                     76: 
                     77: Returns: Nothing
                     78: 
                     79: Description: Main program. Determines if requesting IP is allowed 
                     80:              to view domain configuration(s) for domains for
                     81:              which this server is the primary library server.
                     82: 
                     83: =cut
                     84: 
                     85: #############################################
                     86: #############################################
                     87: 
                     88: sub main {
                     89:     my $remote_ip = $ENV{'REMOTE_ADDR'};
                     90:     my $allowed;
                     91:     if (&LONCAPA::lonauthcgi::check_ipbased_access('domconf',$remote_ip)) {
                     92:         $allowed = 1;
                     93:     } elsif (&LONCAPA::loncgi::check_cookie_and_load_env()) {
                     94:         $allowed = &LONCAPA::lonauthcgi::can_view('domconf');
                     95:     }
                     96:     &LONCAPA::loncgi::check_cookie_and_load_env();
                     97:     &Apache::lonlocal::get_language_handle();
1.2     ! raeburn    98:     my (%gets,$format);
        !            99:     &LONCAPA::loncgi::cgi_getitems($ENV{'QUERY_STRING'},\%gets,['format','primary']);
        !           100:     if (ref($gets{'format'}) eq 'ARRAY') {
        !           101:         $format = $gets{'format'}->[0];
        !           102:     }
1.1       raeburn   103:     if ($allowed ne '') {
                    104:         my @okdoms;
                    105:         unless ($allowed == 1) {
                    106:             @okdoms = split(/\&/,$allowed);
                    107:         }
1.2     ! raeburn   108:         my @hosts;
        !           109:         if (ref($gets{'primary'}) eq 'ARRAY') {
        !           110:             my @posshosts = &Apache::lonnet::current_machine_ids();
        !           111:             foreach my $prim (@{$gets{'primary'}}) {
        !           112:                 if (grep(/^\Q$prim\E$/,@posshosts)) {
        !           113:                     unless (grep(/^\Q$prim\E$/,@hosts)) {
        !           114:                         push(@hosts,$prim);
        !           115:                     }
        !           116:                 }
        !           117:             }
        !           118:         } else {
        !           119:             @hosts = &Apache::lonnet::current_machine_ids();
        !           120:         }
1.1       raeburn   121:         my $numshown = 0;
                    122:         my $numnonprim = 0;
                    123:         foreach my $lonhost (@hosts) {
                    124:             my $dom = &Apache::lonnet::host_domain($lonhost);
                    125:             unless ($allowed == 1) {
                    126:                 next unless (grep(/^\Q$dom\E$/,@okdoms));
                    127:             }
                    128:             my $prim_id = &Apache::lonnet::domain($dom,'primary');
                    129:             if (($prim_id ne '') && (grep(/^\Q$prim_id\E$/,@hosts))) {
1.2     ! raeburn   130:                 unless ($format eq 'raw') {
        !           131:                     my $domdesc = &Apache::lonnet::domain($dom);
        !           132:                     print &mt('Domain configuration for [_1]',"$domdesc ($dom)")."\n\n";
        !           133:                 }
        !           134:                 &show_config($dom,$format);
1.1       raeburn   135:                 print "\n";
                    136:                 $numshown ++;
                    137:             } else {
                    138:                 $numnonprim ++;
                    139:             }
                    140:         }
                    141:         if (!$numshown) {
                    142:             if ($numnonprim) {
1.2     ! raeburn   143:                 unless ($format eq 'raw') {
        !           144:                     print &mt('This server is not a primary library server')."\n";
        !           145:                 }
1.1       raeburn   146:             } else {
1.2     ! raeburn   147:                 unless ($format eq 'raw') { 
        !           148:                     print &mt("You do not have access rights to view domain configuration for domain(s) hosted on this server.")."\n";
        !           149:                 }
1.1       raeburn   150:             }
                    151:         }
                    152:     } else {
1.2     ! raeburn   153:         unless ($format eq 'raw') {
        !           154:             &LONCAPA::lonauthcgi::unauthorized_msg('domconf');
        !           155:         }
1.1       raeburn   156:     }
                    157: }
                    158: 
                    159: #############################################
                    160: #############################################
                    161: 
                    162: =pod
                    163: 
                    164: =item show_config
                    165: 
                    166: Inputs: $domain - domain for which domain configuration is to be shown 
                    167: 
                    168: Returns: Nothing
                    169: 
                    170: Description: Displays plain text of domain configuration by dumping
                    171:              contents of configuration.db
                    172: 
                    173: =cut
                    174: 
                    175: #############################################
                    176: #############################################
                    177: 
                    178: sub show_config {
1.2     ! raeburn   179:     my ($dom,$format) = @_;
1.1       raeburn   180:     my $lonusersdir = $Apache::lonnet::perlvar{'lonUsersDir'};
                    181:     my $fname = $lonusersdir.'/'.$dom.'/configuration.db';
                    182:     my $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
                    183:     if (ref($dbref) eq 'HASH') {
1.2     ! raeburn   184:         my $output; 
1.1       raeburn   185:         foreach my $key (sort(keys(%{$dbref}))) {
                    186:             my $value = $dbref->{$key};  
1.2     ! raeburn   187:             if ($format eq 'raw') {
        !           188:                 $output .= "$key=$value".'&';    
        !           189:             } else {
        !           190:                 if ($value =~ s/^__FROZEN__//) {
        !           191:                     $value = thaw(&unescape($value));
        !           192:                 }
        !           193:                 $key = &unescape($key);
        !           194:                 $value = &unescape($value) if (!ref($value));
        !           195:                 print "$key = ".(ref($value)?Dumper($value):$value)."\n";
1.1       raeburn   196:             }
                    197:         }
                    198:         &LONCAPA::locking_hash_untie($dbref);
1.2     ! raeburn   199:         if ($format eq 'raw') {
        !           200:             $output .=~ s/\&$//;
        !           201:             print $output;
        !           202:         } 
1.1       raeburn   203:     }
                    204:     return;
                    205: }
                    206: 
                    207: =pod
                    208: 
                    209: =back
                    210: 
                    211: =cut
                    212: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>