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

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

Removed from v.1.158  
changed lines
  Added in v.1.179


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