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

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

Removed from v.1.158.2.4  
changed lines
  Added in v.1.179


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