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

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


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