Diff for /loncom/auth/lonlogin.pm between versions 1.125 and 1.180

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

Removed from v.1.125  
changed lines
  Added in v.1.180


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