Diff for /loncom/auth/lonlogin.pm between versions 1.158.2.9.2.3 and 1.172

version 1.158.2.9.2.3, 2021/01/04 12:50:06 version 1.172, 2018/01/12 15:26:39
Line 1 Line 1
 # The LearningOnline Network  # The LearningOnline Network
 # Login Screen  # Login Screen
 #  #
 # $Id$  # $Id$
 #  #
 # Copyright Michigan State University Board of Trustees  # Copyright Michigan State University Board of Trustees
 #  #
 # This file is part of the LearningOnline Network with CAPA (LON-CAPA).  # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
 #  #
 # LON-CAPA is free software; you can redistribute it and/or modify  # LON-CAPA is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by  # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or  # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.  # (at your option) any later version.
 #  #
 # LON-CAPA is distributed in the hope that it will be useful,  # LON-CAPA is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of  # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.  # GNU General Public License for more details.
 #  #
 # You should have received a copy of the GNU General Public License  # You should have received a copy of the GNU General Public License
 # along with LON-CAPA; if not, write to the Free Software  # along with LON-CAPA; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #  #
 # /home/httpd/html/adm/gpl.txt  # /home/httpd/html/adm/gpl.txt
 #  #
 # http://www.lon-capa.org/  # http://www.lon-capa.org/
 #  #
   
 package Apache::lonlogin;  package Apache::lonlogin;
   
 use strict;  use strict;
 use Apache::Constants qw(:common);  use Apache::Constants qw(:common);
 use Apache::File ();  use Apache::File ();
 use Apache::lonnet;  use Apache::lonnet;
 use Apache::loncommon();  use Apache::loncommon();
 use Apache::lonauth();  use Apache::lonauth();
 use Apache::lonlocal;  use Apache::lonlocal;
 use Apache::migrateuser();  use Apache::migrateuser();
 use lib '/home/httpd/lib/perl/';  use lib '/home/httpd/lib/perl/';
 use LONCAPA qw(:DEFAULT :match);  use LONCAPA;
 use CGI::Cookie();  use CGI::Cookie();
     
 sub handler {  sub handler {
     my $r = shift;      my $r = shift;
   
     &Apache::loncommon::get_unprocessed_cgi      &Apache::loncommon::get_unprocessed_cgi
  (join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},   (join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},
       $ENV{'REDIRECT_QUERY_STRING'}),        $ENV{'REDIRECT_QUERY_STRING'}),
  ['interface','username','domain','firsturl','localpath','localres',   ['interface','username','domain','firsturl','localpath','localres',
   'token','role','symb','iptoken','btoken']);    'token','role','symb','iptoken']);
     if (!defined($env{'form.firsturl'})) {      if (!defined($env{'form.firsturl'})) {
         &Apache::lonacc::get_posted_cgi($r,['firsturl']);          &Apache::lonacc::get_posted_cgi($r,['firsturl']);
     }      }
     if (!defined($env{'form.firsturl'})) {      if (!defined($env{'form.firsturl'})) {
         if ($ENV{'REDIRECT_URL'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) {          if ($ENV{'REDIRECT_URL'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) {
             $env{'form.firsturl'} = $ENV{'REDIRECT_URL'};              $env{'form.firsturl'} = $ENV{'REDIRECT_URL'};
         }          }
     }      }
   
 # -- check if they are a migrating user  # -- check if they are a migrating user
     if (defined($env{'form.token'})) {      if (defined($env{'form.token'})) {
  return &Apache::migrateuser::handler($r);   return &Apache::migrateuser::handler($r);
     }      }
   
 # For "public user" - remove any exising "public" cookie, as user really wants to log-in  # For "public user" - remove any exising "public" cookie, as user really wants to log-in
     my ($handle,$lonidsdir,$expirepub,$userdom);      my ($handle,$lonidsdir,$expirepub,$userdom);
     $lonidsdir=$r->dir_config('lonIDsDir');      unless ($r->header_only) {
     unless ($r->header_only) {          $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);
         $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);          if ($handle ne '') {
         if ($handle ne '') {              $lonidsdir=$r->dir_config('lonIDsDir');
             if ($handle=~/^publicuser\_/) {              if ($handle=~/^publicuser\_/) {
                 unlink($r->dir_config('lonIDsDir')."/$handle.id");                  unlink($r->dir_config('lonIDsDir')."/$handle.id");
                 undef($handle);                  undef($handle);
                 undef($userdom);                  undef($userdom);
                 $expirepub = 1;                  $expirepub = 1;
             }              }
         }          }
     }      }
   
     &Apache::loncommon::no_cache($r);      &Apache::loncommon::no_cache($r);
     &Apache::lonlocal::get_language_handle($r);      &Apache::lonlocal::get_language_handle($r);
     &Apache::loncommon::content_type($r,'text/html');      &Apache::loncommon::content_type($r,'text/html');
     if ($expirepub) {      if ($expirepub) {
         my $c = new CGI::Cookie(-name    => 'lonPubID',          my $c = new CGI::Cookie(-name    => 'lonPubID',
                                 -value   => '',                                  -value   => '',
                                 -expires => '-10y',);                                  -expires => '-10y',);
         $r->header_out('Set-cookie' => $c);          $r->header_out('Set-cookie' => $c);
     } elsif (($handle eq '') && ($userdom ne '')) {      } elsif (($handle eq '') && ($userdom ne '')) {
         my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));          my $c = new CGI::Cookie(-name    => 'lonID',
         foreach my $name (keys(%cookies)) {                                  -value   => '',
             next unless ($name =~ /^lon(|S|Link|Pub)ID$/);                                  -expires => '-10y',);
             my $c = new CGI::Cookie(-name    => $name,          $r->headers_out->add('Set-cookie' => $c);
                                     -value   => '',      }
                                     -expires => '-10y',);      $r->send_http_header;
             $r->headers_out->add('Set-cookie' => $c);      return OK if $r->header_only;
         }  
     }  
     $r->send_http_header;  # Are we re-routing?
     return OK if $r->header_only;      my $londocroot = $r->dir_config('lonDocRoot');
       if (-e "$londocroot/lon-status/reroute.txt") {
    &Apache::lonauth::reroute($r);
 # Are we re-routing?   return OK;
     my $londocroot = $r->dir_config('lonDocRoot');       }
     if (-e "$londocroot/lon-status/reroute.txt") {  
  &Apache::lonauth::reroute($r);  #
  return OK;  # If browser sent an old cookie for which the session file had been removed
     }  # check if configuration for user's domain has a portal URL set.  If so
   # switch user's log-in to the portal.
     my $lonhost = $r->dir_config('lonHostID');  #
     $env{'form.firsturl'} =~ s/(`)/'/g;  
       if (($handle eq '') && ($userdom ne '')) {
 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)          my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);
           if ($domdefaults{'portal_def'} =~ /^https?\:/) {
     my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);              my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
     if ($found_server) {                                            {'redirect' => [0,$domdefaults{'portal_def'}],});
         my $hostname = &Apache::lonnet::hostname($found_server);              my $end_page   = &Apache::loncommon::end_page();
         if ($hostname ne '') {              $r->print($start_page.$end_page);
             my $protocol = $Apache::lonnet::protocol{$found_server};              return OK;
             $protocol = 'http' if ($protocol ne 'https');          }
             my $dest = '/adm/roles';      }
             if ($env{'form.firsturl'} ne '') {  
                 $dest = $env{'form.firsturl'};      $env{'form.firsturl'} =~ s/(`)/'/g;
             }  
             my %info = (  # -------------------------------- Prevent users from attempting to login twice
                          balcookie => $lonhost.':'.$balancer_cookie,      if ($handle ne '') {
                        );          &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
             my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server);   my $start_page =
             if ($balancer_token) {      &Apache::loncommon::start_page('Already logged in');
                 $dest .=  (($dest=~/\?/)?'&;':'?') . 'btoken='.$balancer_token;   my $end_page =
             }      &Apache::loncommon::end_page();
             my $url = $protocol.'://'.$hostname.$dest;          my $dest = '/adm/roles';
             my $start_page =          if ($env{'form.firsturl'} ne '') {
                 &Apache::loncommon::start_page('Switching Server ...',undef,              $dest = $env{'form.firsturl'};
                                                {'redirect'       => [0,$url],});          }
             my $end_page   = &Apache::loncommon::end_page();   $r->print(
             $r->print($start_page.$end_page);                    $start_page
             return OK;                   .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
         }                   .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
     }                    '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'
                    .$end_page
 #                   );
 # Check if a LON-CAPA load balancer sent user here because user's browser sent          return OK;
 # it a balancer cookie for an active session on this server.      }
 #  
   # ---------------------------------------------------- No valid token, continue
     my $balcookie;  
     if ($env{'form.btoken'}) {  # ---------------------------- Not possible to really login to domain "public"
         my %info = &Apache::lonnet::tmpget($env{'form.btoken'});      if ($env{'form.domain'} eq 'public') {
         $balcookie = $info{'balcookie'};   $env{'form.domain'}='';
         &Apache::lonnet::tmpdel($env{'form.btoken'});   $env{'form.username'}='';
         delete($env{'form.btoken'});      }
     }  
   # ------ Is this page requested because /adm/migrateuser detected an IP change?
 #      my %sessiondata;
 # If browser sent an old cookie for which the session file had been removed      if ($env{'form.iptoken'}) {
 # check if configuration for user's domain has a portal URL set.  If so          %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});
 # switch user's log-in to the portal.          unless ($sessiondata{'sessionserver'}) {
 #              my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});
               delete($env{'form.iptoken'});
     if (($handle eq '') && ($userdom ne '')) {          }
         my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);      }
         if ($domdefaults{'portal_def'} =~ /^https?\:/) {  # ----------------------------------------------------------- Process Interface
             my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,      $env{'form.interface'}=~s/\W//g;
                                           {'redirect' => [0,$domdefaults{'portal_def'}],});  
             my $end_page   = &Apache::loncommon::end_page();      (undef,undef,undef,undef,undef,undef,my $clientmobile) =
             $r->print($start_page.$end_page);          &Apache::loncommon::decode_user_agent();
             return OK;  
         }      my $iconpath=
     }   &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));
   
     $env{'form.firsturl'} =~ s/(`)/'/g;      my $lonhost = $r->dir_config('lonHostID');
       my $domain = &Apache::lonnet::default_login_domain();
 # -------------------------------- Prevent users from attempting to login twice      my $defdom = $domain;
     if ($handle ne '') {      if ($lonhost ne '') {
         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);          unless ($sessiondata{'sessionserver'}) {
  my $start_page =               my $redirect = &check_loginvia($domain,$lonhost);
     &Apache::loncommon::start_page('Already logged in');              if ($redirect) {
  my $end_page =                   $r->print($redirect);
     &Apache::loncommon::end_page();                  return OK;
         my $dest = '/adm/roles';              }
         if ($env{'form.firsturl'} ne '') {          }
             $dest = $env{'form.firsturl'};       }
         }  
  $r->print(      if (($sessiondata{'domain'}) &&
               $start_page          (&Apache::lonnet::domain($env{'form.domain'},'description'))) {
              .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'          $domain=$sessiondata{'domain'};
              .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',      } elsif (($env{'form.domain'}) &&
               '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'   (&Apache::lonnet::domain($env{'form.domain'},'description'))) {
              .$end_page   $domain=$env{'form.domain'};
              );      }
         return OK;  
     }      my $role    = $r->dir_config('lonRole');
       my $loadlim = $r->dir_config('lonLoadLim');
 # ---------------------------------------------------- No valid token, continue      my $uloadlim= $r->dir_config('lonUserLoadLim');
       my $servadm = $r->dir_config('lonAdmEMail');
 # ---------------------------- Not possible to really login to domain "public"      my $tabdir  = $r->dir_config('lonTabDir');
     if ($env{'form.domain'} eq 'public') {      my $include = $r->dir_config('lonIncludes');
  $env{'form.domain'}='';      my $expire  = $r->dir_config('lonExpire');
  $env{'form.username'}='';      my $version = $r->dir_config('lonVersion');
     }      my $host_name = &Apache::lonnet::hostname($lonhost);
   
 # ------ Is this page requested because /adm/migrateuser detected an IP change?  # --------------------------------------------- Default values for login fields
     my %sessiondata;     
     if ($env{'form.iptoken'}) {      my ($authusername,$authdomain);
         %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});      if ($sessiondata{'username'}) {
         unless ($sessiondata{'sessionserver'}) {          $authusername=$sessiondata{'username'};
             my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});      } else {
             delete($env{'form.iptoken'});          $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});
         }          $authusername=($env{'form.username'}?$env{'form.username'}:'');
     }      }
 # ----------------------------------------------------------- Process Interface      if ($sessiondata{'domain'}) {
     $env{'form.interface'}=~s/\W//g;          $authdomain=$sessiondata{'domain'};
       } else {
     (undef,undef,undef,undef,undef,undef,my $clientmobile) =          $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});
         &Apache::loncommon::decode_user_agent();          $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);
       }
     my $iconpath=   
  &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));  # ---------------------------------------------------------- Determine own load
       my $loadavg;
     my $domain = &Apache::lonnet::default_login_domain();      {
     my $defdom = $domain;   my $loadfile=Apache::File->new('/proc/loadavg');
     if ($lonhost ne '') {   $loadavg=<$loadfile>;
         unless ($sessiondata{'sessionserver'}) {      }
             my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie);      $loadavg =~ s/\s.*//g;
             if ($redirect) {  
                 $r->print($redirect);      my ($loadpercent,$userloadpercent);
                 return OK;      if ($loadlim) {
             }          $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
         }      }
     }      if ($uloadlim) {
           $userloadpercent=&Apache::lonnet::userload();
     if (($sessiondata{'domain'}) &&      }
         (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) {  
         $domain=$sessiondata{'domain'};      my $firsturl=
     } elsif (($env{'form.domain'}) &&       ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});
  (&Apache::lonnet::domain($env{'form.domain'},'description'))) {  
  $domain=$env{'form.domain'};  # ----------------------------------------------------------- Get announcements
     }      my $announcements=&Apache::lonnet::getannounce();
   # -------------------------------------------------------- Set login parameters
     my $role    = $r->dir_config('lonRole');  
     my $loadlim = $r->dir_config('lonLoadLim');      my @hexstr=('0','1','2','3','4','5','6','7',
     my $uloadlim= $r->dir_config('lonUserLoadLim');                  '8','9','a','b','c','d','e','f');
     my $servadm = $r->dir_config('lonAdmEMail');      my $lkey='';
     my $tabdir  = $r->dir_config('lonTabDir');      for (0..7) {
     my $include = $r->dir_config('lonIncludes');          $lkey.=$hexstr[rand(15)];
     my $expire  = $r->dir_config('lonExpire');      }
     my $version = $r->dir_config('lonVersion');  
     my $host_name = &Apache::lonnet::hostname($lonhost);      my $ukey='';
       for (0..7) {
 # --------------------------------------------- Default values for login fields          $ukey.=$hexstr[rand(15)];
           }
     my ($authusername,$authdomain);  
     if ($sessiondata{'username'}) {      my $lextkey=hex($lkey);
         $authusername=$sessiondata{'username'};      if ($lextkey>2147483647) { $lextkey-=4294967296; }
     } else {  
         $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});      my $uextkey=hex($ukey);
         $authusername=($env{'form.username'}?$env{'form.username'}:'');      if ($uextkey>2147483647) { $uextkey-=4294967296; }
     }  
     if ($sessiondata{'domain'}) {  # -------------------------------------------------------- Store away log token
         $authdomain=$sessiondata{'domain'};      my $tokenextras;
     } else {      if ($env{'form.role'}) {
         $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});          $tokenextras = '&role='.&escape($env{'form.role'});
         $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);      }
     }      if ($env{'form.symb'}) {
           if (!$tokenextras) {
 # ---------------------------------------------------------- Determine own load              $tokenextras = '&';
     my $loadavg;          }
     {          $tokenextras .= '&symb='.&escape($env{'form.symb'});
  my $loadfile=Apache::File->new('/proc/loadavg');      }
  $loadavg=<$loadfile>;      if ($env{'form.iptoken'}) {
     }          if (!$tokenextras) {
     $loadavg =~ s/\s.*//g;              $tokenextras = '&&';
           }
     my ($loadpercent,$userloadpercent);          $tokenextras .= '&iptoken='.&escape($env{'form.iptoken'});
     if ($loadlim) {      }
         $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);      my $logtoken=Apache::lonnet::reply(
     }         'tmpput:'.$ukey.$lkey.'&'.$firsturl.$tokenextras,
     if ($uloadlim) {         $lonhost);
         $userloadpercent=&Apache::lonnet::userload();  
     }  # -- If we cannot talk to ourselves, or hostID does not map to a hostname
   #    we are in serious trouble
     my $firsturl=  
     ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});      if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
           if ($logtoken eq 'no_such_host') {
 # ----------------------------------------------------------- Get announcements              &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');
     my $announcements=&Apache::lonnet::getannounce();          }
 # -------------------------------------------------------- Set login parameters          my $spares='';
    my $last;
     my @hexstr=('0','1','2','3','4','5','6','7',          foreach my $hostid (sort
                 '8','9','a','b','c','d','e','f');      {
     my $lkey='';   &Apache::lonnet::hostname($a) cmp
     for (0..7) {      &Apache::lonnet::hostname($b);
         $lkey.=$hexstr[rand(15)];      }
     }      keys(%Apache::lonnet::spareid)) {
               next if ($hostid eq $lonhost);
     my $ukey='';      my $hostname = &Apache::lonnet::hostname($hostid);
     for (0..7) {      next if (($last eq $hostname) || ($hostname eq ''));
         $ukey.=$hexstr[rand(15)];              $spares.='<br /><font size="+1"><a href="http://'.
     }                  $hostname.
                   '/adm/login?domain='.$authdomain.'">'.
     my $lextkey=hex($lkey);                  $hostname.'</a>'.
     if ($lextkey>2147483647) { $lextkey-=4294967296; }                  ' '.&mt('(preferred)').'</font>'.$/;
       $last=$hostname;
     my $uextkey=hex($ukey);          }
     if ($uextkey>2147483647) { $uextkey-=4294967296; }          if ($spares) {
               $spares.= '<br />';
 # -------------------------------------------------------- Store away log token          }
     my $tokenextras;          my %all_hostnames = &Apache::lonnet::all_hostnames();
     if ($env{'form.role'}) {          foreach my $hostid (sort
         $tokenextras = '&role='.&escape($env{'form.role'});      {
     }   &Apache::lonnet::hostname($a) cmp
     if ($env{'form.symb'}) {      &Apache::lonnet::hostname($b);
         if (!$tokenextras) {      }
             $tokenextras = '&';      keys(%all_hostnames)) {
         }              next if ($hostid eq $lonhost || $Apache::lonnet::spareid{$hostid});
         $tokenextras .= '&symb='.&escape($env{'form.symb'});              my $hostname = &Apache::lonnet::hostname($hostid);
     }              next if (($last eq $hostname) || ($hostname eq ''));
     if ($env{'form.iptoken'}) {              $spares.='<br /><a href="http://'.
         if (!$tokenextras) {               $hostname.
             $tokenextras = '&&';               '/adm/login?domain='.$authdomain.'">'.
         }               $hostname.'</a>';
         $tokenextras .= '&iptoken='.&escape($env{'form.iptoken'});              $last=$hostname;
     }           }
     my $logtoken=Apache::lonnet::reply(           $r->print(
        'tmpput:'.$ukey.$lkey.'&'.$firsturl.$tokenextras,     '<html>'
        $lonhost);    .'<head><title>'
     .&mt('The LearningOnline Network with CAPA')
 # -- If we cannot talk to ourselves, or hostID does not map to a hostname    .'</title></head>'
 #    we are in serious trouble    .'<body bgcolor="#FFFFFF">'
     .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
     if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {    .'<img src="/adm/lonKaputt/lonlogo_broken.gif" align="right" />'
         if ($logtoken eq 'no_such_host') {    .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
             &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');          if ($spares) {
         }              $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
         my $spares='';                       .'</p>'
         my (@sparehosts,%spareservers);                       .$spares);
         my $sparesref = &Apache::lonnet::this_host_spares($defdom);          }
         if (ref($sparesref) eq 'HASH') {          $r->print('</body>'
             foreach my $key (keys(%{$sparesref})) {                   .'</html>'
                 if (ref($sparesref->{$key}) eq 'ARRAY') {          );
                     my @sorted = sort { &Apache::lonnet::hostname($a) cmp          return OK;
                                         &Apache::lonnet::hostname($b);      }
                                       } @{$sparesref->{$key}};  
                     if (@sorted) {  # ----------------------------------------------- Apparently we are in business
                         if ($key eq 'primary') {      $servadm=~s/\,/\<br \/\>/g;
                             unshift(@sparehosts,@sorted);  
                         } elsif ($key eq 'default') {  # ----------------------------------------------------------- Front page design
                             push(@sparehosts,@sorted);      my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
                         }      my $font=&Apache::loncommon::designparm('login.font',$domain);
                     }      my $link=&Apache::loncommon::designparm('login.link',$domain);
                 }      my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
             }      my $alink=&Apache::loncommon::designparm('login.alink',$domain);
         }      my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
         foreach my $hostid (@sparehosts) {      my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
             next if ($hostid eq $lonhost);      my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
     my $hostname = &Apache::lonnet::hostname($hostid);      my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
     next if (($hostname eq '') || ($spareservers{$hostname}));      my $logo=&Apache::loncommon::designparm('login.logo',$domain);
             $spareservers{$hostname} = 1;      my $img=&Apache::loncommon::designparm('login.img',$domain);
             my $protocol = $Apache::lonnet::protocol{$hostid};      my $domainlogo=&Apache::loncommon::domainlogo($domain);
             $protocol = 'http' if ($protocol ne 'https');      my $showbanner = 1;
             $spares.='<br /><span style="font-size: larger;"><a href="'.$protocol.'://'.      my $showmainlogo = 1;
                 $hostname.      if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
                 '/adm/login?domain='.$authdomain.'">'.          $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
                 $hostname.'</a>'.      }
                 ' '.&mt('(preferred)').'</span>'.$/;      if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
         }          $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
         if ($spares) {      }
             $spares.= '<br />';      my $showadminmail;
         }      my @possdoms = &Apache::lonnet::current_machine_domains();
         my %all_hostnames = &Apache::lonnet::all_hostnames();      if (grep(/^\Q$domain\E$/,@possdoms)) {
         foreach my $hostid (sort          $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
     {      }
  &Apache::lonnet::hostname($a) cmp      my $showcoursecat =
     &Apache::lonnet::hostname($b);          &Apache::loncommon::designparm('login.coursecatalog',$domain);
     }      my $shownewuserlink =
     keys(%all_hostnames)) {          &Apache::loncommon::designparm('login.newuser',$domain);
             next if ($hostid eq $lonhost);      my $showhelpdesk =
             my $hostname = &Apache::lonnet::hostname($hostid);          &Apache::loncommon::designparm('login.helpdesk',$domain);
             next if (($hostname eq '') || ($spareservers{$hostname}));      my $now=time;
             $spareservers{$hostname} = 1;      my $js = (<<ENDSCRIPT);
             my $protocol = $Apache::lonnet::protocol{$hostid};  
             $protocol = 'http' if ($protocol ne 'https');  <script type="text/javascript" language="JavaScript">
             $spares.='<br /><a href="'.$protocol.'://'.  // <![CDATA[
              $hostname.  function send()
              '/adm/login?domain='.$authdomain.'">'.  {
              $hostname.'</a>';  this.document.server.elements.uname.value
          }  =this.document.client.elements.uname.value;
          $r->print(  
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'  this.document.server.elements.udom.value
   .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'  =this.document.client.elements.udom.value;
   .'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'  
   .&mt('The LearningOnline Network with CAPA')  uextkey=this.document.client.elements.uextkey.value;
   .'</title></head>'  lextkey=this.document.client.elements.lextkey.value;
   .'<body bgcolor="#FFFFFF">'  initkeys();
   .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'  
   .'<img src="/adm/lonKaputt/lonlogo_broken.gif" alt="broken icon" align="right" />'  if(this.document.server.action.substr(0,5) === 'http:'){
   .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');      this.document.server.elements.upass0.value
         if ($spares) {          =getCrypted(this.document.client.elements.upass$now.value);
             $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')  } else {
                      .'</p>'      this.document.server.elements.upass0.value
                      .$spares);          =this.document.client.elements.upass$now.value;
         }  }
         $r->print('</body>'  
                  .'</html>'  this.document.client.elements.uname.value='';
         );  this.document.client.elements.upass$now.value='';
         return OK;  
     }  this.document.server.submit();
   return false;
 # ----------------------------------------------- Apparently we are in business  }
     $servadm=~s/\,/\<br \/\>/g;  
   function enableInput() {
 # ----------------------------------------------------------- Front page design      this.document.client.elements.upass$now.removeAttribute("readOnly");
     my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);      this.document.client.elements.uname.removeAttribute("readOnly");
     my $font=&Apache::loncommon::designparm('login.font',$domain);      this.document.client.elements.udom.removeAttribute("readOnly");
     my $link=&Apache::loncommon::designparm('login.link',$domain);      return;
     my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);  }
     my $alink=&Apache::loncommon::designparm('login.alink',$domain);  
     my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);  // ]]>
     my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);  </script>
     my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);  
     my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);  ENDSCRIPT
     my $logo=&Apache::loncommon::designparm('login.logo',$domain);  
     my $img=&Apache::loncommon::designparm('login.img',$domain);  # --------------------------------------------------- Print login screen header
     my $domainlogo=&Apache::loncommon::domainlogo($domain);  
     my $showbanner = 1;      my %add_entries = (
     my $showmainlogo = 1;         bgcolor      => "$mainbg",
     if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {         text         => "$font",
         $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);         link         => "$link",
     }         vlink        => "$vlink",
     if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {         alink        => "$alink",
         $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);                 onload       => 'javascript:enableInput();',);
     }  
     my $showadminmail;      my ($lonhost_in_use,$headextra,$headextra_exempt,@hosts,%defaultdomconf);
     my @possdoms = &Apache::lonnet::current_machine_domains();      @hosts = &Apache::lonnet::current_machine_ids();
     if (grep(/^\Q$domain\E$/,@possdoms)) {      $lonhost_in_use = $lonhost;
         $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);      if (@hosts > 1) {
     }          foreach my $hostid (@hosts) {
     my $showcoursecat =              if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
         &Apache::loncommon::designparm('login.coursecatalog',$domain);                  $lonhost_in_use = $hostid;
     my $shownewuserlink =                   last;
         &Apache::loncommon::designparm('login.newuser',$domain);              }
     my $showhelpdesk =          }
         &Apache::loncommon::designparm('login.helpdesk',$domain);      }
     my $now=time;      %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
     my $js = (<<ENDSCRIPT);      $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
       $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
 <script type="text/javascript" language="JavaScript">      if ($headextra) {
 // <![CDATA[          my $omitextra;
 function send()          if ($headextra_exempt ne '') {
 {              my @exempt = split(',',$headextra_exempt);
 this.document.server.elements.uname.value              my $ip = $ENV{'REMOTE_ADDR'};
 =this.document.client.elements.uname.value;              if (grep(/^\Q$ip\E$/,@exempt)) {
                   $omitextra = 1;
 this.document.server.elements.udom.value              }
 =this.document.client.elements.udom.value;          }
           unless ($omitextra) {
 uextkey=this.document.client.elements.uextkey.value;              my $confname = $defdom.'-domainconfig';
 lextkey=this.document.client.elements.lextkey.value;              if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
 initkeys();                  my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
                   unless ($extra eq '-1') {
 this.document.server.elements.upass0.value                      $js .= "\n".$extra."\n";
     =getCrypted(this.document.client.elements.upass$now.value);                  }
               }
 this.document.client.elements.uname.value='';          }
 this.document.client.elements.upass$now.value='';      }
   
 this.document.server.submit();      $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
 return false;         { 'redirect'       => [$expire,'/adm/roles'],
 }   'add_entries' => \%add_entries,
    'only_body'   => 1,}));
 function enableInput() {  
     this.document.client.elements.upass$now.removeAttribute("readOnly");  # ----------------------------------------------------------------------- Texts
     this.document.client.elements.uname.removeAttribute("readOnly");  
     this.document.client.elements.udom.removeAttribute("readOnly");      my %lt=&Apache::lonlocal::texthash(
     return;            'un'       => 'Username',
 }            'pw'       => 'Password',
             'dom'      => 'Domain',
 // ]]>            'perc'     => 'percent',
 </script>            'load'     => 'Server Load',
             'userload' => 'User Load',
 ENDSCRIPT            'catalog'  => 'Course/Community Catalog',
             'log'      => 'Log in',
 # --------------------------------------------------- Print login screen header            'help'     => 'Log-in Help',
             'serv'     => 'Server',
     my %add_entries = (            'servadm'  => 'Server Administration',
        bgcolor      => "$mainbg",            'helpdesk' => 'Contact Helpdesk',
        text         => "$font",            'forgotpw' => 'Forgot password?',
        link         => "$link",            'newuser'  => 'New User?',
        vlink        => "$vlink",         );
        alink        => "$alink",  # -------------------------------------------------- Change password field name
                onload       => 'javascript:enableInput();',);  
       my $forgotpw = &forgotpwdisplay(%lt);
     my ($lonhost_in_use,$headextra,$headextra_exempt,@hosts,%defaultdomconf);      $forgotpw .= '<br />' if $forgotpw;
     @hosts = &Apache::lonnet::current_machine_ids();      my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
     $lonhost_in_use = $lonhost;      if ($loginhelp) {
     if (@hosts > 1) {          $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
         foreach my $hostid (@hosts) {      }
             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {  
                 $lonhost_in_use = $hostid;  # ---------------------------------------------------- Serve out DES JavaScript
                 last;      {
             }      my $jsh=Apache::File->new($include."/londes.js");
         }      $r->print(<$jsh>);
     }      }
     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);  # ---------------------------------------------------------- Serve rest of page
     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};  
     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};      $r->print(
     if ($headextra) {      '<div class="LC_Box"'
         my $omitextra;     .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
         if ($headextra_exempt ne '') {  );
             my @exempt = split(',',$headextra_exempt);  
             my $ip = &Apache::lonnet::get_requestor_ip();      $r->print(<<ENDSERVERFORM);
             if (grep(/^\Q$ip\E$/,@exempt)) {  <form name="server" action="/adm/authenticate" method="post" target="_top">
                 $omitextra = 1;     <input type="hidden" name="logtoken" value="$logtoken" />
             }     <input type="hidden" name="serverid" value="$lonhost" />
         }     <input type="hidden" name="uname" value="" />
         unless ($omitextra) {     <input type="hidden" name="upass0" value="" />
             my $confname = $defdom.'-domainconfig';     <input type="hidden" name="udom" value="" />
             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {     <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));     <input type="hidden" name="localres" value="$env{'form.localres'}" />
                 unless ($extra eq '-1') {    </form>
                     $js .= "\n".$extra."\n";  ENDSERVERFORM
                 }      my $coursecatalog;
             }      if (($showcoursecat eq '') || ($showcoursecat)) {
         }          $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
     }      }
       my $newuserlink;
     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,      if ($shownewuserlink) {
        { 'redirect'       => [$expire,'/adm/roles'],           $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  'add_entries' => \%add_entries,      }
  'only_body'   => 1,}));      my $logintitle =
           '<h2 class="LC_hcell"'
 # ----------------------------------------------------------------------- Texts         .' style="background:'.$loginbox_header_bgcol.';'
          .' color:'.$loginbox_header_textcol.'">'
     my %lt=&Apache::lonlocal::texthash(         .$lt{'log'}
           'un'       => 'Username',         .'</h2>';
           'pw'       => 'Password',  
           'dom'      => 'Domain',      my $noscript_warning='<noscript><span class="LC_warning"><b>'
           'perc'     => 'percent',                          .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
           'load'     => 'Server Load',                          .'</b></span></noscript>';
           'userload' => 'User Load',      my $helpdeskscript;
           'catalog'  => 'Course/Community Catalog',      my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
           'log'      => 'Log in',                                         $authdomain,\$helpdeskscript,
           'help'     => 'Log-in Help',                                         $showhelpdesk,\@possdoms);
           'serv'     => 'Server',  
           'servadm'  => 'Server Administration',      my $mobileargs;
           'helpdesk' => 'Contact Helpdesk',      if ($clientmobile) {
           'forgotpw' => 'Forgot password?',          $mobileargs = 'autocapitalize="off" autocorrect="off"';
           'newuser'  => 'New User?',      }
        );      my $loginform=(<<LFORM);
 # -------------------------------------------------- Change password field name  <form name="client" action="" onsubmit="return(send())">
     <input type="hidden" name="lextkey" value="$lextkey" />
     my $forgotpw = &forgotpwdisplay(%lt);    <input type="hidden" name="uextkey" value="$uextkey" />
     $forgotpw .= '<br />' if $forgotpw;    <b><label for="uname">$lt{'un'}</label>:</b><br />
     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);    <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
     if ($loginhelp) {    <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';    <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
     }    <b><label for="udom">$lt{'dom'}</label>:</b><br />
     <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
 # ---------------------------------------------------- Serve out DES JavaScript    <input type="submit" value="$lt{'log'}" />
     {  </form>
     my $jsh=Apache::File->new($include."/londes.js");  LFORM
     $r->print(<$jsh>);  
     }      if ($showbanner) {
 # ---------------------------------------------------------- Serve rest of page          $r->print(<<HEADER);
   <!-- The LON-CAPA Header -->
     $r->print(  <div style="background:$pgbg;margin:0;width:100%;">
     '<div class="LC_Box"'    <img src="$img" border="0" alt="The Learning Online Network with CAPA" class="LC_maxwidth" />
    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'  </div>
 );  HEADER
       }
     $r->print(<<ENDSERVERFORM);      $r->print(<<ENDTOP);
 <form name="server" action="/adm/authenticate" method="post" target="_top">  <div style="float:left;margin-top:0;">
    <input type="hidden" name="logtoken" value="$logtoken" />  <div class="LC_Box" style="background:$loginbox_bg;">
    <input type="hidden" name="serverid" value="$lonhost" />    $logintitle
    <input type="hidden" name="uname" value="" />    $loginform
    <input type="hidden" name="upass0" value="" />    $noscript_warning
    <input type="hidden" name="udom" value="" />  </div>
    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />   
    <input type="hidden" name="localres" value="$env{'form.localres'}" />  <div class="LC_Box" style="padding-top: 10px;">
   </form>    $loginhelp
 ENDSERVERFORM    $forgotpw
     my $coursecatalog;    $contactblock
     if (($showcoursecat eq '') || ($showcoursecat)) {    $newuserlink
         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';    $coursecatalog
     }  </div>
     my $newuserlink;  </div>
     if ($shownewuserlink) {  
         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';  <div>
     }  ENDTOP
     my $logintitle =      if ($showmainlogo) {
         '<h2 class="LC_hcell"'          $r->print(' <img src="'.$logo.'" alt="" class="LC_maxwidth" />'."\n");
        .' style="background:'.$loginbox_header_bgcol.';'      }
        .' color:'.$loginbox_header_textcol.'">'  $r->print(<<ENDTOP);
        .$lt{'log'}  $announcements
        .'</h2>';  </div>
   <hr style="clear:both;" />
     my $noscript_warning='<noscript><span class="LC_warning"><b>'  ENDTOP
                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')      my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
                         .'</b></span></noscript>';      $domainrow = <<"END";
     my $helpdeskscript;        <tr>
     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,         <td  align="left" valign="top">
                                        $authdomain,\$helpdeskscript,          <small><b>$lt{'dom'}:&nbsp;</b></small>
                                        $showhelpdesk,\@possdoms);         </td>
          <td  align="left" valign="top">
     my $mobileargs;          <small><tt>&nbsp;$domain</tt></small>
     if ($clientmobile) {         </td>
         $mobileargs = 'autocapitalize="off" autocorrect="off"';         </tr>
     }  END
     my $loginform=(<<LFORM);      $serverrow = <<"END";
 <form name="client" action="" onsubmit="return(send())">        <tr>
   <input type="hidden" name="lextkey" value="$lextkey" />         <td  align="left" valign="top">
   <input type="hidden" name="uextkey" value="$uextkey" />          <small><b>$lt{'serv'}:&nbsp;</b></small>
   <b><label for="uname">$lt{'un'}</label>:</b><br />         </td>
   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />         <td align="left" valign="top">
   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />          <small><tt>&nbsp;$lonhost ($role)</tt></small>
   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />         </td>
   <b><label for="udom">$lt{'dom'}</label>:</b><br />        </tr>
   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />  END
   <input type="submit" value="$lt{'log'}" />      if ($loadlim) {
 </form>          $loadrow = <<"END";
 LFORM        <tr>
          <td align="left" valign="top">
     if ($showbanner) {          <small><b>$lt{'load'}:&nbsp;</b></small>
         $r->print(<<HEADER);         </td>
 <!-- The LON-CAPA Header -->         <td align="left" valign="top">
 <div style="background:$pgbg;margin:0;width:100%;">          <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
   <img src="$img" border="0" alt="The Learning Online Network with CAPA" class="LC_maxwidth" />         </td>
 </div>        </tr>
 HEADER  END
     }      }
     $r->print(<<ENDTOP);      if ($uloadlim) {
 <div style="float:left;margin-top:0;">          $userloadrow = <<"END";
 <div class="LC_Box" style="background:$loginbox_bg;">        <tr>
   $logintitle         <td align="left" valign="top">
   $loginform          <small><b>$lt{'userload'}:&nbsp;</b></small>
   $noscript_warning         </td>
 </div>         <td align="left" valign="top">
             <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
 <div class="LC_Box" style="padding-top: 10px;">         </td>
   $loginhelp        </tr>
   $forgotpw  END
   $contactblock      }
   $newuserlink      if (($version ne '') && ($version ne '<!-- VERSION -->')) {
   $coursecatalog          $versionrow = <<"END";
 </div>        <tr>
 </div>         <td colspan="2" align="left">
           <small>$version</small>
 <div>         </td>
 ENDTOP        </tr>
     if ($showmainlogo) {  END
         $r->print(' <img src="'.$logo.'" alt="" class="LC_maxwidth" />'."\n");      }
     }  
 $r->print(<<ENDTOP);      $r->print(<<ENDDOCUMENT);
 $announcements      <div style="float: left;">
 </div>       <table border="0" cellspacing="0" cellpadding="0">
 <hr style="clear:both;" />  $domainrow
 ENDTOP  $serverrow
     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);  $loadrow    
     $domainrow = <<"END";  $userloadrow
       <tr>  $versionrow
        <td  align="left" valign="top">       </table>
         <small><b>$lt{'dom'}:&nbsp;</b></small>      </div>
        </td>      <div style="float: right;">
        <td  align="left" valign="top">      $domainlogo
         <small><tt>&nbsp;$domain</tt></small>      </div>
        </td>      <br style="clear:both;" />
       </tr>   </div>
 END  
     $serverrow = <<"END";  <script type="text/javascript">
       <tr>  // <![CDATA[
        <td  align="left" valign="top">  // the if prevents the script error if the browser can not handle this
         <small><b>$lt{'serv'}:&nbsp;</b></small>  if ( document.client.uname ) { document.client.uname.focus(); }
        </td>  // ]]>
        <td align="left" valign="top">  </script>
         <small><tt>&nbsp;$lonhost ($role)</tt></small>  $helpdeskscript
        </td>  
       </tr>  ENDDOCUMENT
 END      my %endargs = ( 'noredirectlink' => 1, );
     if ($loadlim) {      $r->print(&Apache::loncommon::end_page(\%endargs));
         $loadrow = <<"END";      return OK;
       <tr>  }
        <td align="left" valign="top">  
         <small><b>$lt{'load'}:&nbsp;</b></small>  sub check_loginvia {
        </td>      my ($domain,$lonhost) = @_;
        <td align="left" valign="top">      if ($domain eq '' || $lonhost eq '') {
         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>          return;
        </td>      }
       </tr>      my %domconfhash = &Apache::loncommon::get_domainconf($domain);
 END      my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
     }      my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
     if ($uloadlim) {      my $output;
         $userloadrow = <<"END";      if ($loginvia ne '') {
       <tr>          my $noredirect;
        <td align="left" valign="top">          my $ip = $ENV{'REMOTE_ADDR'};
         <small><b>$lt{'userload'}:&nbsp;</b></small>          if ($ip eq '127.0.0.1') {
        </td>              $noredirect = 1;
        <td align="left" valign="top">          } else {
         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>              if ($loginvia_exempt ne '') {
        </td>                  my @exempt = split(',',$loginvia_exempt);
       </tr>                  if (grep(/^\Q$ip\E$/,@exempt)) {
 END                      $noredirect = 1;
     }                  }
     if (($version ne '') && ($version ne '<!-- VERSION -->')) {              }
         $versionrow = <<"END";          }
       <tr>          unless ($noredirect) {
        <td colspan="2" align="left">              my ($newhost,$path);
         <small>$version</small>              if ($loginvia =~ /:/) {
        </td>                  ($newhost,$path) = split(':',$loginvia);
       </tr>              } else {
 END                  $newhost = $loginvia;
     }              }
               if ($newhost ne $lonhost) {
     $r->print(<<ENDDOCUMENT);                  if (&Apache::lonnet::hostname($newhost) ne '') {
     <div style="float: left;">                      $output = &redirect_page($newhost,$path);
      <table border="0" cellspacing="0" cellpadding="0">                  }
 $domainrow              }
 $serverrow          }
 $loadrow          }
 $userloadrow      return $output;
 $versionrow  }
      </table>  
     </div>  sub redirect_page {
     <div style="float: right;">      my ($desthost,$path) = @_;
     $domainlogo      my $protocol = $Apache::lonnet::protocol{$desthost};
     </div>      $protocol = 'http' if ($protocol ne 'https');
     <br style="clear:both;" />      unless ($path =~ m{^/}) {
  </div>          $path = '/'.$path;
       }
 <script type="text/javascript">      my $url = $protocol.'://'.&Apache::lonnet::hostname($desthost).$path;
 // <![CDATA[      if ($env{'form.firsturl'} ne '') {
 // the if prevents the script error if the browser can not handle this          $url .='?firsturl='.$env{'form.firsturl'};
 if ( document.client.uname ) { document.client.uname.focus(); }      }
 // ]]>      my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
 </script>                                                      {'redirect' => [0,$url],});
 $helpdeskscript      my $end_page   = &Apache::loncommon::end_page();
       return $start_page.$end_page;
 ENDDOCUMENT  }
     my %endargs = ( 'noredirectlink' => 1, );  
     $r->print(&Apache::loncommon::end_page(\%endargs));  sub contactdisplay {
     return OK;      my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
 }          $possdoms) = @_;
       my $contactblock;
 sub check_loginvia {      my $origmail;
     my ($domain,$lonhost,$lonidsdir,$balcookie) = @_;      if (ref($possdoms) eq 'ARRAY') {
     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {          if (grep(/^\Q$authdomain\E$/,@{$possdoms})) {
         return;              $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
     }          }
     my %domconfhash = &Apache::loncommon::get_domainconf($domain);      }
     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};      my $requestmail =
     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};          &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
     my $output;                                                   $authdomain,$origmail);
     if ($loginvia ne '') {      unless ($showhelpdesk eq '0') {
         my $noredirect;          if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
         my $ip = &Apache::lonnet::get_requestor_ip();              $showhelpdesk = 1;
         if ($ip eq '127.0.0.1') {          } else {
             $noredirect = 1;              $showhelpdesk = 0;
         } else {          }
             if ($loginvia_exempt ne '') {      }
                 my @exempt = split(',',$loginvia_exempt);      if ($servadm && $showadminmail) {
                 if (grep(/^\Q$ip\E$/,@exempt)) {          $contactblock .= $$lt{'servadm'}.':<br />'.
                     $noredirect = 1;                           '<tt>'.$servadm.'</tt><br />';
                 }      }
             }      if ($showhelpdesk) {
         }          $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
         unless ($noredirect) {          my $thisurl = &escape('/adm/login');
             my ($newhost,$path);          $$helpdeskscript = <<"ENDSCRIPT";
             if ($loginvia =~ /:/) {  <script type="text/javascript">
                 ($newhost,$path) = split(':',$loginvia);  // <![CDATA[
             } else {  function helpdesk() {
                 $newhost = $loginvia;      var possdom = document.client.udom.value;
             }      var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
             if ($newhost ne $lonhost) {      if (codedom == '') {
                 if (&Apache::lonnet::hostname($newhost) ne '') {          codedom = "$authdomain";
                     if ($balcookie) {      }
                         my ($balancer,$cookie) = split(/:/,$balcookie);      var querystr = "origurl=$thisurl&codedom="+codedom;
                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {      document.location.href = "/adm/helpdesk?"+querystr;
                             my ($udom,$uname,$cookieid) = ($1,$2,$3);      return;
                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {  }
                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {  // ]]>
                                     while (my $filename=readdir($dh)) {  </script>
                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {  ENDSCRIPT
                                             my $handle = $1;      }
                                             my %hash =      return $contactblock;
                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,  }
                                                                                      ['request.balancercookie',  
                                                                                       'user.linkedenv']);  sub forgotpwdisplay {
                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {      my (%lt) = @_;
                                                 if (unlink("$lonidsdir/$filename")) {      my $prompt_for_resetpw = 1;
                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&      if ($prompt_for_resetpw) {
                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&          return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {      }
                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");      return;
                                                     }  }
                                                 }  
                                             }  sub coursecatalog_link {
                                             last;      my ($linkname) = @_;
                                         }      return <<"END";
                                     }        <a href="/adm/coursecatalog">$linkname</a>
                                     closedir($dh);  END
                                 }  }
                             }  
                         }  sub newuser_link {
                     }      my ($linkname) = @_;
                     $output = &redirect_page($newhost,$path);      return '<a href="/adm/createaccount">'.$linkname.'</a>';
                 }  }
             }  
         }  1;
     }  __END__
     return $output;  
 }  
   
 sub redirect_page {  
     my ($desthost,$path) = @_;  
     my $hostname = &Apache::lonnet::hostname($desthost);  
     my $protocol = $Apache::lonnet::protocol{$desthost};  
     $protocol = 'http' if ($protocol ne 'https');  
     unless ($path =~ m{^/}) {  
         $path = '/'.$path;  
     }  
     my $url = $protocol.'://'.$hostname.$path;  
     if ($env{'form.firsturl'} ne '') {  
         $url .='?firsturl='.$env{'form.firsturl'};  
     }  
     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,  
                                                     {'redirect' => [0,$url],});  
     my $end_page   = &Apache::loncommon::end_page();  
     return $start_page.$end_page;  
 }  
   
 sub contactdisplay {  
     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,  
         $possdoms) = @_;  
     my $contactblock;  
     my $origmail;  
     if (ref($possdoms) eq 'ARRAY') {  
         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) {   
             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};  
         }  
     }  
     my $requestmail =   
         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',  
                                                  $authdomain,$origmail);  
     unless ($showhelpdesk eq '0') {  
         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {  
             $showhelpdesk = 1;  
         } else {  
             $showhelpdesk = 0;  
         }  
     }  
     if ($servadm && $showadminmail) {  
         $contactblock .= $$lt{'servadm'}.':<br />'.  
                          '<tt>'.$servadm.'</tt><br />';  
     }  
     if ($showhelpdesk) {  
         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';  
         my $thisurl = &escape('/adm/login');  
         $$helpdeskscript = <<"ENDSCRIPT";  
 <script type="text/javascript">  
 // <![CDATA[  
 function helpdesk() {  
     var possdom = document.client.udom.value;  
     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');  
     if (codedom == '') {  
         codedom = "$authdomain";  
     }  
     var querystr = "origurl=$thisurl&codedom="+codedom;  
     document.location.href = "/adm/helpdesk?"+querystr;  
     return;  
 }  
 // ]]>  
 </script>  
 ENDSCRIPT  
     }  
     return $contactblock;  
 }  
   
 sub forgotpwdisplay {  
     my (%lt) = @_;  
     my $prompt_for_resetpw = 1;   
     if ($prompt_for_resetpw) {  
         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';  
     }  
     return;  
 }  
   
 sub coursecatalog_link {  
     my ($linkname) = @_;  
     return <<"END";  
       <a href="/adm/coursecatalog">$linkname</a>  
 END  
 }  
   
 sub newuser_link {  
     my ($linkname) = @_;  
     return '<a href="/adm/createaccount">'.$linkname.'</a>';  
 }  
   
 1;  
 __END__  

Removed from v.1.158.2.9.2.3  
changed lines
  Added in v.1.172


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