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

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

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


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