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

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


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