Diff for /loncom/auth/lonlogin.pm between versions 1.158.2.6.2.3 and 1.188

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


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