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

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


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