File:  [LON-CAPA] / loncom / cgi / takeoffline.pl
Revision 1.7: download - view: text, annotated - select for diffs
Mon Mar 22 20:11:15 2010 UTC (14 years, 1 month ago) by droeschl
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
In process of removing remote control, remote navmap and different icon modes code
- removed no_inline_link argument from loncommon::start_page, loncommon::bodytag
- removed subs
    o loncommon::head: it wasn't used
    o loncommon::endheadtag: functionality now covered by loncommon::headtag
    o lonmenu::registerurl: functionality moved to loncommon::headtag, because it was the only function that called registerurl
- removed remote control related translations in de.pm
- refactoring and cleanup

    1: #!/usr/bin/perl
    2: $|=1;
    3: # Take machine offline, reroute traffic
    4: #
    5: # $Id: takeoffline.pl,v 1.7 2010/03/22 20:11:15 droeschl Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: use strict;
   31: use lib '/home/httpd/lib/perl/';
   32: use Apache::lonlocal;
   33: use Apache::loncommon;
   34: use LONCAPA::loncgi;
   35: use LONCAPA::lonauthcgi;
   36: 
   37: print "Content-type: text/html\n\n";
   38: 
   39: &main();
   40: 
   41: sub main {
   42:     if (!&LONCAPA::lonauthcgi::check_ipbased_access('takeoffline')) {
   43:         if (!&LONCAPA::loncgi::check_cookie_and_load_env()) {
   44:             &Apache::lonlocal::get_language_handle();
   45:             print(&LONCAPA::loncgi::missing_cookie_msg());
   46:             return;
   47:         }
   48: 
   49:         if (!&LONCAPA::lonauthcgi::can_view('takeoffline')) {
   50:             &Apache::lonlocal::get_language_handle();
   51:             print(&LONCAPA::lonauthcgi::unauthorized_msg('takeoffline'));
   52:             return;
   53:         }
   54:     }
   55:     &Apache::lonlocal::get_language_handle();
   56: 
   57:     if ($ENV{'QUERY_STRING'} =~ /^phase\=two\&?(.*)$/) {
   58:         my $reroute = $1;
   59:         print &Apache::loncommon::start_page('Take Offline');
   60:         &phasetwo($reroute);
   61:     } else {
   62:         my $js = <<ENDJS;
   63: <script type="text/javascript">
   64: 
   65: function setphase(form) {
   66:     var subaction = '/cgi-bin/takeoffline.pl?phase=two';
   67:     form.action = subaction+'&'+form.rerouteto.options[form.rerouteto.selectedIndex].value;
   68:     form.submit();
   69: }
   70: 
   71: </script>
   72: ENDJS
   73:         print &Apache::loncommon::start_page('Take Offline',$js);
   74:         &phaseone();
   75:     }
   76:     print &Apache::loncommon::end_page();
   77:     return;
   78: }
   79: 
   80: sub phaseone {
   81: #    print '<html><body bgcolor="#FFFFFF">
   82:     print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
   83:     my ($is_dc,@okmachines,%servers);
   84:     my @poss_domains = &Apache::lonnet::current_machine_domains();
   85:     foreach my $dom (@poss_domains) {
   86:         if ($Apache::lonnet::env{'request.role'} eq "dc./$dom/") {
   87:             $is_dc = 1;
   88:         }
   89:         my %domconfig = &Apache::lonnet::get_dom('configuration',['serverstatuses'],$dom);
   90:         if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
   91:             if (ref($domconfig{'serverstatuses'}{'takeonline'}) eq 'HASH') {
   92:                 if ($domconfig{'serverstatuses'}{'takeonline'}{'machines'} ne '') {
   93:                     @okmachines = split(/,/,$domconfig{'serverstatuses'}{'takeonline'}{'machines'});
   94:                 }
   95:             }
   96:         }
   97:         %{$servers{$dom}} = &Apache::lonnet::get_servers($dom);
   98:     }
   99:     my $ip = $ENV{'REMOTE_ADDR'};
  100:     my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
  101:     print '<p>'.&mt("When a LON-CAPA server is 'taken offline' using this utility, the standard log-in page will be replaced with one of the following:").
  102:           '<ul><li>'.&mt('A page which automatically performs a redirect to another server in your domain - the server must be specified below.').'</li>'.
  103:               '<li>'.&mt('A page which announces that this LON-CAPA server is offline.').'</li></ul></p>';
  104:     print '<p>'.&mt("Once a server is offline, the [_1]'take online'[_2] utility can be used to reverse this, but you should verify that you will still be able to access that script once the server is in an offline state.",'<a href="/cgi-bin/takeonline.pl">','</a>').'</p>';
  105:     print '<p>'.&mt("In common with other Domain Status pages the 'take online' script is accessible from certain IP addresses:").'<ul>'.
  106:           '<li>'.&mt('The loopback device - 127.0.0.1 (localhost) - for times when you are web browsing from the server itself.').'</li>'.
  107:           '<li>'.&mt("Specified IP addresses set via the configuration for [_1]Access to Server Status Pages[_2] for domain(s) hosted on the server.",'<a href="/adm/domainprefs?phase=display&actions=serverstatuses">','</a>').'</li></ul><p>'.
  108:           &mt("Once the server is offline you will not be able to log-in directly to select a Domain Coordinator role to use the 'Take online' script, unless IP-based controls provide your access.").'<br />'.&mt("However, you will be able to log-in to a different server in the LON-CAPA network, select a Domain Coordinator role in this server's domain, and then use: [_1] to migrate you session to this machine.","<br /><tt>/adm/switchserver?otherserver=$lonhost</tt>").'<br /></p><p><hr /></p>';
  109:     if ($ip eq '127.0.0.1') {
  110:         print '<div class="LC_info">'.&mt('You are accessing this page from the loopback device.').'</div>';
  111:     } elsif (!@okmachines) {
  112:         print '<div class="LC_warning">'.&mt("IP-based access to the 'take online' page is currently unavailable, as no IP addresses have been specified.").'</div>';
  113:     } elsif (!grep(/^\Q$ip\E$/,@okmachines)) {
  114:         print '<div class="LC_warning">'.&mt("Your current IP [_1] is not among those configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
  115:     } else {
  116:         print '<div class="LC_info">'.&mt("Your current IP [_1] is configured to have IP-based access to the 'take online' utility.",$ip).'</div>';
  117:     }
  118:     my (%allhosts,$otherserver,$domain);
  119:     if ($ENV{'QUERY_STRING'}) {
  120:         ($otherserver,$domain)=split(/\&/,$ENV{'QUERY_STRING'});
  121:     }
  122:     print '<p><hr /></p><p><form method="post" name="takeoffline" onSubmit="javascript:setphase(this);">';
  123: 
  124:     my $options;
  125:     if ($domain) {
  126:         if (grep(/^\Q$domain\E$/,keys(%servers))) {
  127:             if (ref($servers{$domain}) eq 'HASH') {
  128:                 foreach my $hostid (sort(keys(%{$servers{$domain}}))) {
  129:                     next if ($hostid eq $lonhost);
  130:                     my $selchk = '';
  131:                     if ($otherserver eq $hostid) {
  132:                         $selchk = 'selected="selected"';
  133:                     }
  134:                     $options .=
  135:    "<option value=\"$hostid&$domain\" $selchk>"."\n".
  136:    "$hostid ".&mt('Domain: [_1], Hostname: [_2]',$domain,$servers{$domain}{$hostid}).'</option>'."\n";
  137:                 }
  138:             }
  139:         }
  140:     }
  141:     foreach my $dom (sort(keys(%servers))) {
  142:         next if (($domain) && ($dom eq $domain));
  143:         if (ref($servers{$dom}) eq 'HASH') {
  144:             foreach my $hostid (sort(keys(%{$servers{$dom}}))) {
  145:                 next if ($hostid eq $lonhost);
  146:                 next if(defined($allhosts{$hostid}));
  147:                 $allhosts{$hostid} = 1;
  148:                 my $selchk = '';
  149:                 if ($otherserver eq $hostid) {
  150:                     $selchk = 'selected="selected"';
  151:                 }
  152:                 $options .=  
  153:                "<option value=\"$hostid&$dom\" $selchk>".
  154:                $hostid.'</option>';
  155:             }
  156:         }
  157:     }
  158:     if ($options) {
  159:         print &mt('Optional: choose another server in your domain, to which to redirect log-ins: ').
  160:               '<select name="rerouteto">'."\n".
  161:               ' <option value="">'.&mt('Please select').'</option>'."\n".
  162:               $options.'</select>'."\n";
  163:     } else {
  164:         print &mt('There are no other servers in your domain to which to redirect logins.');
  165:     }
  166:     print '<br /><br /><input type="submit" name="offline" value="Take Offline" />'."\n".
  167:           '</form>'."\n";
  168: }
  169: 
  170: sub phasetwo {
  171:     my ($reroute) = @_;
  172:     print '<h2>'.&Apache::lonlocal::mt('Take Offline').'</h2>';
  173:     my $statusmsg;
  174:     if ($reroute) {
  175:         my ($otherserver,$domain)=split(/\&/,$reroute);
  176:         if (&reroute($otherserver,$domain)) {
  177:             print &Apache::lonlocal::mt('Rerouting to [_1]',$otherserver);
  178:             $statusmsg = "status=rerouting&server=$otherserver&domain=$domain&time=".time.
  179:                          "by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}";
  180:         }
  181:     } else {
  182:         if (&dead()) {
  183:             print &Apache::lonlocal::mt('No reroute server given, taking completely offline.');
  184:             $statusmsg = 'status=offline&time='.time."by=$Apache::lonnet::env{'user.name'}:$Apache::lonnet::env{'user.domain'}"
  185:         }
  186:     }
  187:     if ($statusmsg) {
  188:         if (open (STATUS,'>/home/httpd/html/lon-status/reroute.txt')) {
  189:             print STATUS "$statusmsg\n";
  190:             close(STATUS);
  191:         } else {
  192:             print &Apache::lonlocal::mt('Logging of status change to [_1] failed.','<tt>/home/httpd/html/lon-status/reroute.txt</tt>'); 
  193:         }
  194:     }
  195: }
  196: 
  197: sub dead {
  198:     if (open (OUT,'>/home/httpd/html/index.html')) {
  199:         print OUT &Apache::lonlocal::mt('This LON-CAPA server is currently offline.');
  200:         close(OUT);
  201:         return 'ok';
  202:     }
  203:     return;
  204: }
  205: 
  206: sub reroute {
  207:     my ($otherserver,$domain) = @_;
  208:     if (open (OUT,'>/home/httpd/html/index.html')) {
  209:         print OUT (<<ENDNEWINDEX);
  210: <html>
  211:  <head>
  212:   <title>Welcome to the LearningOnline Network with CAPA</title>
  213:   <meta HTTP-EQUIV="Refresh" CONTENT="0.5; url=http://$otherserver/adm/login?domain=$domain">
  214:  </head>
  215:  <body style="background-color:"#004400"; margin: 0px;">
  216:   <img src="/adm/lonIcons/header.gif" />
  217: <p><font color="#FFFFFF">Connecting to
  218: <a href="http://$otherserver/adm/login?domain=$domain"><font color="#FFFFFF">http://$otherserver/</font></a></font></p>
  219: </body>
  220: </html>
  221: ENDNEWINDEX
  222:        close(OUT);
  223:        return 'ok';
  224:     }
  225:     return;
  226: }

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