Diff for /loncom/auth/lonlogin.pm between versions 1.158.2.6.2.2 and 1.178

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


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