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

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

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


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