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

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


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