Diff for /loncom/auth/lonlogin.pm between versions 1.134 and 1.177

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


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