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

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


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