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

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


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