Diff for /loncom/auth/lonlogin.pm between versions 1.158.2.6 and 1.180

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

Removed from v.1.158.2.6  
changed lines
  Added in v.1.180


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