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

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


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