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

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

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


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