Diff for /loncom/auth/lonlogin.pm between versions 1.158 and 1.176

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

Removed from v.1.158  
changed lines
  Added in v.1.176


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