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

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

Removed from v.1.158.2.1  
changed lines
  Added in v.1.178


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