Diff for /loncom/auth/lonlogin.pm between versions 1.140 and 1.176

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


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