Diff for /loncom/auth/lonlogin.pm between versions 1.127 and 1.177

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


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