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

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


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