Diff for /loncom/auth/lonlogin.pm between versions 1.158.2.9 and 1.181

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


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