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

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

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


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