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

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

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


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