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

version 1.143, 2010/08/26 08:10:47 version 1.179, 2019/01/27 16:02:43
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 $last;
     if ($showbanner) {          foreach my $hostid (sort
         $r->print(<<HEADER);      {
 <!-- The LON-CAPA Header -->   &Apache::lonnet::hostname($a) cmp
 <div style="background:$pgbg;margin:0;width:100%;">      &Apache::lonnet::hostname($b);
   <img src="$img" border="0" alt="The Learning Online Network with CAPA" />      }
 </div>      keys(%Apache::lonnet::spareid)) {
 HEADER              next if ($hostid eq $lonhost);
     }      my $hostname = &Apache::lonnet::hostname($hostid);
     $r->print(<<ENDTOP);      next if (($last eq $hostname) || ($hostname eq ''));
 <div style="float:left;">              $spares.='<br /><font size="+1"><a href="http://'.
 <div class="LC_Box" style="background:$loginbox_bg;">                  $hostname.
   $logintitle                  '/adm/login?domain='.$authdomain.'">'.
   $loginform                  $hostname.'</a>'.
   $noscript_warning                  ' '.&mt('(preferred)').'</font>'.$/;
 </div>      $last=$hostname;
             }
 <div class="LC_Box" style="padding-top: 10px;">          if ($spares) {
   $loginhelp              $spares.= '<br />';
   $forgotpw          }
   $contactblock          my %all_hostnames = &Apache::lonnet::all_hostnames();
   $newuserlink          foreach my $hostid (sort
   $coursecatalog      {
 </div>   &Apache::lonnet::hostname($a) cmp
 </div>      &Apache::lonnet::hostname($b);
       }
 <div>      keys(%all_hostnames)) {
 ENDTOP              next if ($hostid eq $lonhost || $Apache::lonnet::spareid{$hostid});
     if ($showmainlogo) {              my $hostname = &Apache::lonnet::hostname($hostid);
         $r->print(' <img src="'.$logo.'" alt="" />'."\n");              next if (($last eq $hostname) || ($hostname eq ''));
     }              $spares.='<br /><a href="http://'.
 $r->print(<<ENDTOP);               $hostname.
 $announcements               '/adm/login?domain='.$authdomain.'">'.
 </div>               $hostname.'</a>';
 <hr style="clear:both;" />              $last=$hostname;
 ENDTOP           }
            $r->print(
 $r->print(<<ENDDOCUMENT);     '<html>'
     <div style="float: left;">    .'<head><title>'
      <table border="0" cellspacing="0" cellpadding="0">    .&mt('The LearningOnline Network with CAPA')
       <tr>    .'</title></head>'
        <td  align="left" valign="top">    .'<body bgcolor="#FFFFFF">'
         <small><b>$lt{'dom'}:&nbsp;</b></small>    .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
        </td>    .'<img src="/adm/lonKaputt/lonlogo_broken.gif" align="right" />'
        <td  align="left" valign="top">    .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
         <small><tt>&nbsp;$domain</tt></small>          if ($spares) {
        </td>              $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
       </tr>                       .'</p>'
       <tr>                       .$spares);
        <td  align="left" valign="top">          }
         <small><b>$lt{'serv'}:&nbsp;</b></small>          $r->print('</body>'
        </td>                   .'</html>'
        <td align="left" valign="top">          );
         <small><tt>&nbsp;$lonhost ($role)</tt></small>          return OK;
        </td>      }
       </tr>  
       <tr>  # ----------------------------------------------- Apparently we are in business
        <td align="left" valign="top">      $servadm=~s/\,/\<br \/\>/g;
         <small><b>$lt{'load'}:&nbsp;</b></small>  
        </td>  # ----------------------------------------------------------- Front page design
        <td align="left" valign="top">      my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>      my $font=&Apache::loncommon::designparm('login.font',$domain);
        </td>      my $link=&Apache::loncommon::designparm('login.link',$domain);
       </tr>      my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
       <tr>      my $alink=&Apache::loncommon::designparm('login.alink',$domain);
        <td align="left" valign="top">      my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
         <small><b>$lt{'userload'}:&nbsp;</b></small>      my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
        </td>      my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
        <td align="left" valign="top">      my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>      my $logo=&Apache::loncommon::designparm('login.logo',$domain);
        </td>      my $img=&Apache::loncommon::designparm('login.img',$domain);
       </tr>      my $domainlogo=&Apache::loncommon::domainlogo($domain);
       <tr>      my $showbanner = 1;
        <td colspan="2" align="left">      my $showmainlogo = 1;
         <small>$version</small>      if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
        </td>          $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
       </tr>      }
      </table>      if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
     </div>          $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
     <div style="float: right;">      }
     $domainlogo      my $showadminmail;
     </div>      my @possdoms = &Apache::lonnet::current_machine_domains();
     <br style="clear:both;" />      if (grep(/^\Q$domain\E$/,@possdoms)) {
  </div>          $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
       }
 <script type="text/javascript">      my $showcoursecat =
 // <![CDATA[          &Apache::loncommon::designparm('login.coursecatalog',$domain);
 // the if prevents the script error if the browser can not handle this      my $shownewuserlink =
 if ( document.client.uname ) { document.client.uname.focus(); }          &Apache::loncommon::designparm('login.newuser',$domain);
 // ]]>      my $showhelpdesk =
 </script>          &Apache::loncommon::designparm('login.helpdesk',$domain);
 $helpdeskscript      my $now=time;
       my $js = (<<ENDSCRIPT);
 ENDDOCUMENT  
     my %endargs = ( 'noredirectlink' => 1, );  <script type="text/javascript" language="JavaScript">
     $r->print(&Apache::loncommon::end_page(\%endargs));  // <![CDATA[
     return OK;  function send()
 }  {
   this.document.server.elements.uname.value
 sub check_loginvia {  =this.document.client.elements.uname.value;
     my ($domain,$lonhost) = @_;  
     if ($domain eq '' || $lonhost eq '') {  this.document.server.elements.udom.value
         return;  =this.document.client.elements.udom.value;
     }  
     my %domconfhash = &Apache::loncommon::get_domainconf($domain);  uextkey=this.document.client.elements.uextkey.value;
     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};  lextkey=this.document.client.elements.lextkey.value;
     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};  initkeys();
     my $output;  
     if ($loginvia ne '') {  if(this.document.server.action.substr(0,5) === 'http:'){
         my $noredirect;      this.document.server.elements.upass0.value
         my $ip = $ENV{'REMOTE_ADDR'};          =getCrypted(this.document.client.elements.upass$now.value);
         if ($ip eq '127.0.0.1') {  } else {
             $noredirect = 1;      this.document.server.elements.upass0.value
         } else {          =this.document.client.elements.upass$now.value;
             if ($loginvia_exempt ne '') {  }
                 my @exempt = split(',',$loginvia_exempt);  
                 if (grep(/^\Q$ip\E$/,@exempt)) {  this.document.client.elements.uname.value='';
                     $noredirect = 1;  this.document.client.elements.upass$now.value='';
                 }  
             }  this.document.server.submit();
         }  return false;
         unless ($noredirect) {  }
             my ($newhost,$path);  
             if ($loginvia =~ /:/) {  function enableInput() {
                 ($newhost,$path) = split(':',$loginvia);      this.document.client.elements.upass$now.removeAttribute("readOnly");
             } else {      this.document.client.elements.uname.removeAttribute("readOnly");
                 $newhost = $loginvia;      this.document.client.elements.udom.removeAttribute("readOnly");
             }      return;
             if ($newhost ne $lonhost) {  }
                 if (&Apache::lonnet::hostname($newhost) ne '') {  
                     $output = &redirect_page($newhost,$path);  // ]]>
                 }  </script>
             }  
         }  ENDSCRIPT
     }  
     return $output;  # --------------------------------------------------- Print login screen header
 }  
       my %add_entries = (
 sub redirect_page {         bgcolor      => "$mainbg",
     my ($desthost,$path) = @_;         text         => "$font",
     my $protocol = $Apache::lonnet::protocol{$desthost};         link         => "$link",
     $protocol = 'http' if ($protocol ne 'https');         vlink        => "$vlink",
     unless ($path =~ m{^/}) {         alink        => "$alink",
         $path = '/'.$path;                 onload       => 'javascript:enableInput();',);
     }  
     my $url = $protocol.'://'.&Apache::lonnet::hostname($desthost).$path;      my ($lonhost_in_use,$headextra,$headextra_exempt,@hosts,%defaultdomconf);
     if ($env{'form.firsturl'} ne '') {      @hosts = &Apache::lonnet::current_machine_ids();
         $url .='?firsturl='.$env{'form.firsturl'};      $lonhost_in_use = $lonhost;
     }      if (@hosts > 1) {
     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,          foreach my $hostid (@hosts) {
                                                     {'redirect' => [0,$url],});              if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
     my $end_page   = &Apache::loncommon::end_page();                  $lonhost_in_use = $hostid;
     return $start_page.$end_page;                  last;
 }              }
           }
 sub contactdisplay {      }
     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript) = @_;      %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
     my $contactblock;      $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
     my $showhelpdesk = 0;      $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
     my $requestmail = $Apache::lonnet::perlvar{'lonSupportEMail'};      if ($headextra) {
     if ($requestmail =~ m/^[^\@]+\@[^\@]+$/) {          my $omitextra;
         $showhelpdesk = 1;          if ($headextra_exempt ne '') {
     }              my @exempt = split(',',$headextra_exempt);
     if ($servadm && $showadminmail) {              my $ip = $ENV{'REMOTE_ADDR'};
         $contactblock .= $$lt{'servadm'}.':<br />'.              if (grep(/^\Q$ip\E$/,@exempt)) {
                          '<tt>'.$servadm.'</tt><br />';                  $omitextra = 1;
     }              }
     if ($showhelpdesk) {          }
         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';          unless ($omitextra) {
         my $thisurl = &escape('/adm/login');              my $confname = $defdom.'-domainconfig';
         $$helpdeskscript = <<"ENDSCRIPT";              if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
 <script type="text/javascript">                  my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
 // <![CDATA[                  unless ($extra eq '-1') {
 function helpdesk() {                      $js .= "\n".$extra."\n";
     var codedom = document.client.udom.value;                  }
     if (codedom == '') {              }
         codedom = "$authdomain";          }
     }      }
     var querystr = "origurl=$thisurl&codedom="+codedom;  
     document.location.href = "/adm/helpdesk?"+querystr;      $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
     return;         { 'redirect'       => [$expire,'/adm/roles'],
 }   'add_entries' => \%add_entries,
 // ]]>   'only_body'   => 1,}));
 </script>  
 ENDSCRIPT  # ----------------------------------------------------------------------- Texts
     }  
     return $contactblock;      my %lt=&Apache::lonlocal::texthash(
 }            'un'       => 'Username',
             'pw'       => 'Password',
 sub forgotpwdisplay {            'dom'      => 'Domain',
     my (%lt) = @_;            'perc'     => 'percent',
     my $prompt_for_resetpw = 1;             'load'     => 'Server Load',
     if ($prompt_for_resetpw) {            'userload' => 'User Load',
         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';            'catalog'  => 'Course/Community Catalog',
     }            'log'      => 'Log in',
     return;            'help'     => 'Log-in Help',
 }            'serv'     => 'Server',
             'servadm'  => 'Server Administration',
 sub loginhelpdisplay {            'helpdesk' => 'Contact Helpdesk',
     my ($authdomain,%lt) = @_;            'forgotpw' => 'Forgot password?',
     my $login_help = 1;            'newuser'  => 'New User?',
     if ($login_help) {         );
         my $dom = $authdomain;  # -------------------------------------------------- Change password field name
         if ($dom eq '') {  
             $dom = &Apache::lonnet::default_login_domain();      my $forgotpw = &forgotpwdisplay(%lt);
         }      $forgotpw .= '<br />' if $forgotpw;
         my %helpconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$dom);      my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
         my $loginhelp_url = $helpconfig{'helpsettings'}{'loginhelpurl'};      if ($loginhelp) {
         if ($loginhelp_url ne '') {          $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
             return '<a href="'.$loginhelp_url.'">'.$lt{'help'}.'</a>';      }
         } else {  
             return '<a href="/adm/loginproblems.html">'.$lt{'help'}.'</a>';  # ---------------------------------------------------- Serve out DES JavaScript
         }      {
     }      my $jsh=Apache::File->new($include."/londes.js");
     return;      $r->print(<$jsh>);
 }      }
   # ---------------------------------------------------------- Serve rest of page
 sub coursecatalog_link {  
     my ($linkname) = @_;      $r->print(
     return <<"END";      '<div class="LC_Box"'
       <a href="/adm/coursecatalog">$linkname</a>     .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
 END  );
 }  
       $r->print(<<ENDSERVERFORM);
 sub newuser_link {  <form name="server" action="/adm/authenticate" method="post" target="_top">
     my ($linkname) = @_;     <input type="hidden" name="logtoken" value="$logtoken" />
     return '<a href="/adm/createaccount">'.$linkname.'</a>';     <input type="hidden" name="serverid" value="$lonhost" />
 }     <input type="hidden" name="uname" value="" />
      <input type="hidden" name="upass0" value="" />
 1;     <input type="hidden" name="udom" value="" />
 __END__     <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.179


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