Diff for /loncom/auth/lonlogin.pm between versions 1.143 and 1.181

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


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.