Diff for /loncom/auth/lonauth.pm between versions 1.121.2.24.2.2 and 1.158

version 1.121.2.24.2.2, 2022/02/24 14:42:30 version 1.158, 2019/06/16 22:32:04
Line 1 Line 1
 # The LearningOnline Network  # The LearningOnline Network
 # User Authentication Module  # User Authentication Module
 #  #
 # $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::lonauth;  package Apache::lonauth;
   
 use strict;  use strict;
 use LONCAPA qw(:DEFAULT :match);  use LONCAPA qw(:DEFAULT :match);
 use Apache::Constants qw(:common);  use Apache::Constants qw(:common);
 use CGI qw(:standard);  use CGI qw(:standard);
 use Apache::loncommon();  use Apache::loncommon();
 use Apache::lonnet;  use Apache::lonnet;
 use Apache::lonmenu();  use Apache::lonmenu();
 use Apache::createaccount;  use Apache::createaccount;
 use Fcntl qw(:flock);  use Apache::ltiauth;
 use Apache::lonlocal;  use Fcntl qw(:flock);
 use Apache::File();  use Apache::lonlocal;
 use HTML::Entities;  use Apache::File();
 use Digest::MD5;  use HTML::Entities;
 use CGI::Cookie();  use Digest::MD5;
     
 # ------------------------------------------------------------ Successful login  # ------------------------------------------------------------ Successful login
 sub success {  sub success {
     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,      my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
  $form,$cid,$expirepub) = @_;   $form,$skipcritical) = @_;
   
 # ------------------------------------------------------------ Get cookie ready  # ------------------------------------------------------------ Get cookie ready
     my $cookie =      my $cookie =
  &Apache::loncommon::init_user_environment($r, $username, $domain,   &Apache::loncommon::init_user_environment($r, $username, $domain,
   $authhost, $form,    $authhost, $form,
   {'extra_env' => $extra_env,});    {'extra_env' => $extra_env,});
   
     my $public=($username eq 'public' && $domain eq 'public');      my $public=($username eq 'public' && $domain eq 'public');
   
     if ($public or $lowerurl eq 'noredirect') { return $cookie; }      if ($public or $lowerurl eq 'noredirect') { return $cookie; }
   
 # -------------------------------------------------------------------- Log this  # -------------------------------------------------------------------- Log this
   
     my $ip = &Apache::lonnet::get_requestor_ip();      &Apache::lonnet::log($domain,$username,$authhost,
     &Apache::lonnet::log($domain,$username,$authhost,                           "Login $ENV{'REMOTE_ADDR'}");
                          "Login $ip");  
   # ------------------------------------------------- Check for critical messages
 # ------------------------------------------------- Check for critical messages  
       unless ($skipcritical) {
     my @what=&Apache::lonnet::dump('critical',$domain,$username);          my @what=&Apache::lonnet::dump('critical',$domain,$username);
     if ($what[0]) {          if ($what[0]) {
  if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {      if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
     $lowerurl='/adm/email?critical=display';          $lowerurl='/adm/email?critical=display';
         }              }
     }          }
       }
 # ------------------------------------------------------------ Get cookies ready  
     my ($securecookie,$defaultcookie);  # ----------------------------------------------------------- Get cookies ready
     my $ssl = $r->subprocess_env('https');      my ($securecookie,$defaultcookie);
     if ($ssl) {      my $ssl = $r->subprocess_env('https');
         $securecookie="lonSID=$cookie; path=/; HttpOnly; secure";      if ($ssl) {
         my $lonidsdir=$r->dir_config('lonIDsDir');          $securecookie="lonSID=$cookie; path=/; HttpOnly; secure";
         if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {          my $lonidsdir=$r->dir_config('lonIDsDir');
             my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';          if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {
             if (-e "$lonidsdir/$linkname.id") {              my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';
                 unlink("$lonidsdir/$linkname.id");              if (-e "$lonidsdir/$linkname.id") {
             }                  unlink("$lonidsdir/$linkname.id");
             my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",              }
                                               "$lonidsdir/$linkname.id"); 1 };              my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",
             if ($made_symlink) {                                                "$lonidsdir/$linkname.id"); 1 };
                 $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";              if ($made_symlink) {
                 &Apache::lonnet::appenv({'user.linkedenv' => $linkname});                  $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";
             }                  &Apache::lonnet::appenv({'user.linkedenv' => $linkname});
         }              }
     } else {          }
         $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";      } else {
     }          $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";
 # -------------------------------------------------------- Menu script and info      }
     my $destination = $lowerurl;  # -------------------------------------------------------- Menu script and info
       my $destination = $lowerurl;
     if (defined($form->{role})) {      if ($env{'request.lti.login'}) {
         my $envkey = 'user.role.'.$form->{role};          if (($env{'request.lti.reqcrs'}) && ($env{'request.lti.reqrole'} eq 'cc')) {
         my $now=time;              &Apache::loncommon::content_type($r,'text/html');
         my $then=$env{'user.login.time'};              if ($securecookie) {
         my $refresh=$env{'user.refresh.time'};                  $r->headers_out->add('Set-cookie' => $securecookie);
         my $update=$env{'user.update.time'};              }
         if (!$update) {              if ($defaultcookie) {
             $update = $then;                  $r->headers_out->add('Set-cookie' => $defaultcookie);
         }              }
         if (exists($env{$envkey})) {              $r->send_http_header;
             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);              if (ref($form) eq 'HASH') {
             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,                  $form->{'lti.login'} = $env{'request.lti.login'};
                                          \$trolecode,\$tstatus,\$tstart,\$tend);                  $form->{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
             if ($tstatus eq 'is') {                  $form->{'lti.reqrole'} = $env{'request.lti.reqrole'};
                 $destination  .= ($destination =~ /\?/) ? '&' : '?';                  $form->{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');              }
                 $destination .= 'selectrole=1&'.$newrole.'=1';              &Apache::ltiauth::lti_reqcrs($r,$domain,$form,$username,$domain);
             }              return;
         }          }
     }          if ($env{'request.lti.selfenrollrole'}) {
     if (defined($form->{symb})) {              if (&Apache::ltiauth::lti_enroll($username,$domain,
         my $destsymb = $form->{symb};                                               $env{'request.lti.selfenrollrole'}) eq 'ok') {
         my $encrypted;                  $form->{'role'} = $env{'request.lti.selfenrollrole'};
         if ($destsymb =~ m{^/enc/}) {                  &Apache::lonnet::delenv('request.lti.selfenrollrole');
             $encrypted = 1;              } else {
             if ($cid) {                  &Apache::ltiauth::invalid_request($r,24);
                 $destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);              }
             }          }
         }      }
         $destination  .= ($destination =~ /\?/) ? '&' : '?';      if (defined($form->{role})) {
         if ($destsymb =~ /___/) {          my $envkey = 'user.role.'.$form->{role};
             my ($map,$resid,$desturl)=split(/___/,$destsymb);          my $now=time;
             $desturl = &Apache::lonnet::clutter($desturl);          my $then=$env{'user.login.time'};
             if ($encrypted) {          my $refresh=$env{'user.refresh.time'};
                 $desturl = &Apache::lonenc::encrypted($desturl,1,$cid);          my $update=$env{'user.update.time'};
                 $destsymb = $form->{symb};          if (!$update) {
             }              $update = $then;
             $desturl = &HTML::Entities::encode($desturl,'"<>&');          }
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');          if (exists($env{$envkey})) {
             $destination .= 'destinationurl='.$desturl.              my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
                             '&destsymb='.$destsymb;              &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
         } elsif (!$encrypted) {                                           \$trolecode,\$tstatus,\$tstart,\$tend);
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');              if ($tstatus eq 'is') {
             $destination .= 'destinationurl='.$destsymb;                  $destination  .= ($destination =~ /\?/) ? '&' : '?';
         }                  my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
     }                  $destination .= 'selectrole=1&'.$newrole.'=1';
     if ($destination =~ m{^/adm/roles}) {              }
         $destination  .= ($destination =~ /\?/) ? '&' : '?';          }
         $destination .= 'source=login';      }
     }      if (defined($form->{symb})) {
           my $destsymb = $form->{symb};
     if (($env{'request.deeplink.login'} eq $lowerurl) &&          $destination  .= ($destination =~ /\?/) ? '&' : '?';
         (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {          if ($destsymb =~ /___/) {
         my %info;              # FIXME Need to deal with encrypted symbs and urls as needed.
         if ($env{'request.linkprot'}) {              my ($map,$resid,$desturl)=split(/___/,$destsymb);
             $info{'linkprot'} = $env{'request.linkprot'};              $desturl = &Apache::lonnet::clutter($desturl);
         } elsif ($env{'request.linkkey'} ne '') {              $desturl = &HTML::Entities::encode($desturl,'"<>&');
             $info{'linkkey'} = $env{'request.linkkey'};              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
         }              $destination .= 'destinationurl='.$desturl.
         $info{'origurl'} = $lowerurl;                              '&destsymb='.$destsymb;
         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');          } else {
         unless (($token eq 'con_lost') || ($token eq 'refused') ||              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {              $destination .= 'destinationurl='.$destsymb;
             $destination .= (($destination =~ /\?/) ? '&' : '?') . 'ttoken='.$token;          }
         }      }
     }      if ($destination =~ m{^/adm/roles}) {
     if ($env{'request.deeplink.login'}) {          $destination  .= ($destination =~ /\?/) ? '&' : '?';
         if ($env{'environment.remote'} eq 'on') {          $destination .= 'source=login';
             &Apache::lonnet::appenv({'environment.remote' => 'off'});      }
         }  
     }      my $windowname = 'loncapaclient';
       if ($env{'request.lti.login'}) {
     my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});          $windowname .= 'lti';
     my $startupremote=&Apache::lonmenu::startupremote($destination);      }
     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);      my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";');
     my $setflags=&Apache::lonmenu::setflags();      my $brcrum = [{'href' => '',
     my $maincall=&Apache::lonmenu::maincall();                     'text' => 'Successful Login'},];
     my $brcrum = [{'href' => '',      my $args = {'bread_crumbs' => $brcrum,};
                    'text' => 'Successful Login'},];      unless ((defined($form->{role})) || (defined($form->{symb}))) {
     my $start_page=&Apache::loncommon::start_page('Successful Login',          my $update=$env{'user.update.time'};
                                                   $startupremote,          if (!$update) {
                                                   {'no_inline_link' => 1,              $update = $env{'user.login.time'};
                                                    'bread_crumbs' => $brcrum,});          }
     my $end_page  =&Apache::loncommon::end_page();          my %roles_in_env;
           my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
     my $continuelink;          if ($showcount == 1) {
     if ($env{'environment.remote'} eq 'off') {              foreach my $rolecode (keys(%roles_in_env)) {
  $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';                  my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
     }                  if ($cid) {
 # ------------------------------------------------- Output for successful login                      my %coursedescription =
                           &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
     &Apache::loncommon::content_type($r,'text/html');                      if ($coursedescription{'type'} eq 'Placement') {
     if ($securecookie) {                          $args->{'crstype'} = 'Placement';
         $r->headers_out->add('Set-cookie' => $securecookie);                      }
     }                      last;
     if ($defaultcookie) {                  }
         $r->headers_out->add('Set-cookie' => $defaultcookie);              }
     }          }
     if ($expirepub) {      }
         my $c = new CGI::Cookie(-name    => 'lonPubID',  
                                 -value   => '',  # ------------------------------------------------- Output for successful login
                                 -expires => '-10y',);  
         $r->headers_out->add('Set-cookie' => $c);      &Apache::loncommon::content_type($r,'text/html');
     }      if ($securecookie) {
     $r->send_http_header;          $r->headers_out->add('Set-cookie' => $securecookie);
       }
     my %lt=&Apache::lonlocal::texthash(      if ($defaultcookie) {
        'wel' => 'Welcome',          $r->headers_out->add('Set-cookie' => $defaultcookie);
        'pro' => 'Login problems?',      }
        );      $r->send_http_header;
     my $loginhelp = &loginhelpdisplay($domain);  
     if ($loginhelp) {      my ($start_page,$js,$pagebody,$end_page);
         $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';      if ($env{'request.lti.login'}) {
     }          $args = {'only_body' => 1};
           if ($env{'request.lti.target'} eq '') {
     my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');               my $ltitarget = (($destination =~ /\?/) ? '&' : '?').
     $r->print(<<ENDSUCCESS);                              'ltitarget=iframe';
 $start_page              $js = <<"ENDJS";
 $setflags  
 $windowinfo  <script type="text/javascript">
 <h1>$lt{'wel'}</h1>  // <![CDATA[
 $welcome  function setLTItarget() {
 $loginhelp      var newloc = '$destination';
 $remoteinfo      if (parent !== window) {
 $maincall          newloc += '$ltitarget';
 $continuelink      }
 $end_page      window.location.href=newloc;
 ENDSUCCESS  }
     return;  // ]]>
 }  </script>
   
 # --------------------------------------------------------------- Failed login!  ENDJS
               $args->{'add_entries'} = {'onload' => "javascript:setLTItarget();"};
 sub failed {              $pagebody =  '<noscript><span class="LC_warning">'
     my ($r,$message,$form,$authhost) = @_;                          .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
     (undef,undef,undef,my $clientmathml,my $clientunicode) =                          .'</span></noscript>';
         &Apache::loncommon::decode_user_agent();          } else {
     my $args = {};              $args->{'redirect'} = [0,$destination,1];
     if ($clientunicode && !$clientmathml) {          }
         $args = {'browser.unicode' => 1};          $start_page=&Apache::loncommon::start_page('',$js,$args);
     }      } else {
           $args->{'redirect'} = [0,$destination];
     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);          $start_page=&Apache::loncommon::start_page('Successful Login',
     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});                                                     $js,$args);
     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});  
     if (&Apache::lonnet::domain($udom,'description') eq '') {          my %lt=&Apache::lonlocal::texthash(
         undef($udom);             'wel' => 'Welcome',
     }             'pro' => 'Login problems?',
     my $retry = '/adm/login';            );
     if ($uname eq $form->{'uname'}) {          $pagebody = "<h1>$lt{'wel'}</h1>\n".
         $retry .= '?username='.$uname;                      &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
     }          my $loginhelp = &loginhelpdisplay($domain);
     if ($udom) {          if ($loginhelp) {
         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;              $pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
     }          }
     my $lonhost = $r->dir_config('lonHostID');      }
     my $querystr;      $end_page = &Apache::loncommon::end_page();
     my $result = &set_retry_token($form,$lonhost,\$querystr);      $r->print(<<ENDSUCCESS);
     if ($result eq 'fail') {  $start_page
         if (exists($form->{role})) {  $windowinfo
             my $role = &Apache::loncommon::cleanup_html($form->{role});  $pagebody
             if ($role ne '') {  $end_page
                 $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;  ENDSUCCESS
             }      return;
         }  }
         if (exists($form->{symb})) {  
             my $symb = &Apache::loncommon::cleanup_html($form->{symb});  # --------------------------------------------------------------- Failed login!
             if ($symb ne '') {  
                 $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;  sub failed {
             }      my ($r,$message,$form) = @_;
         }      (undef,undef,undef,my $clientmathml,my $clientunicode) =
         if (exists($form->{firsturl})) {          &Apache::loncommon::decode_user_agent();
             my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});      my $args = {};
             if ($firsturl ne '') {      if ($clientunicode && !$clientmathml) {
                 $retry .= (($retry=~/\?/)?'&amp;':'?').'firsturl='.$firsturl;          $args = {'browser.unicode' => 1};
                 if ($form->{firsturl} =~ m{^/tiny/$match_domain/\w+$}) {      }
                     unless (exists($form->{linkprot})) {  
                         if (exists($form->{linkkey})) {      my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
                             $retry .= 'linkkey='.$form->{linkkey};      my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
                         }      my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
                     }      if (&Apache::lonnet::domain($udom,'description') eq '') {
                 }          undef($udom);
             }      }
         }      my $retry = '/adm/login';
         if (exists($form->{linkprot})) {      if ($uname eq $form->{'uname'}) {
             my $ltoken = &Apache::lonnet::tmpput({linkprot => $form->{'linkprot'}},          $retry .= '?username='.$uname;
                                                  $r->dir_config('lonHostID'),'retry');      }
             if ($ltoken) {      if ($udom) {
                 $retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;          $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
             }      }
         }      if (exists($form->{role})) {
     } elsif ($querystr ne '') {          my $role = &Apache::loncommon::cleanup_html($form->{role});
         $retry .= (($retry=~/\?/)?'&amp;':'?').$querystr;          if ($role ne '') {
     }              $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
     my $end_page = &Apache::loncommon::end_page();          }
     &Apache::loncommon::content_type($r,'text/html');      }
     $r->send_http_header;      if (exists($form->{symb})) {
     my @actions =          my $symb = &Apache::loncommon::cleanup_html($form->{symb});
          (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));          if ($symb ne '') {
     my $loginhelp = &loginhelpdisplay($udom);              $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
     if ($loginhelp) {          }
         push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');      }
     }      if (exists($form->{firsturl})) {
     #FIXME: link to helpdesk might be added here          my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});
           if ($firsturl ne '') {
     $r->print(              $retry .= (($retry=~/\?/)?'&amp;':'?').'firsturl='.$firsturl;
        $start_page              if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
       .'<h2>'.&mt('Sorry ...').'</h2>'                  unless (exists($form->{linkprot})) {
       .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'                      if (exists($form->{linkkey})) {
       .&Apache::lonhtmlcommon::actionbox(\@actions)                          $retry .= 'linkkey='.$form->{linkkey};
       .$end_page                      }
     );                  }
  }              }
           }
 # ------------------------------------------------------------------ Rerouting!      }
       if (exists($form->{linkprot})) {
 sub reroute {          my $ltoken = &Apache::lonnet::tmpput({linkprot => $form->{'linkprot'}},
     my ($r) = @_;                                               $r->dir_config('lonHostID'));
     &Apache::loncommon::content_type($r,'text/html');          if ($ltoken) {
     $r->send_http_header;              $retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
     my $msg='<b>'.&mt('Sorry ...').'</b><br />'          }
            .&mt('Please [_1]log in again[_2].');      }
     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});      my $end_page = &Apache::loncommon::end_page();
 }      &Apache::loncommon::content_type($r,'text/html');
       $r->send_http_header;
 # ---------------------------------------------------------------- Main handler      my @actions =
            (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
 sub handler {      my $loginhelp = &loginhelpdisplay($udom);
     my $r = shift;      if ($loginhelp) {
     my $londocroot = $r->dir_config('lonDocRoot');          push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
 # Are we re-routing?      }
     if (-e "$londocroot/lon-status/reroute.txt") {      #FIXME: link to helpdesk might be added here
  &reroute($r);  
  return OK;      $r->print(
     }         $start_page
         .'<h2>'.&mt('Sorry ...').'</h2>'
     &Apache::lonlocal::get_language_handle($r);        .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
         .&Apache::lonhtmlcommon::actionbox(\@actions)
 # -------------------------------- Prevent users from attempting to login twice        .$end_page
     my $handle = &Apache::lonnet::check_for_valid_session($r);      );
     if ($handle ne '') {   }
         my $lonidsdir=$r->dir_config('lonIDsDir');  
         if ($handle=~/^publicuser\_/) {  # ------------------------------------------------------------------ Rerouting!
 # For "public user" - remove it, we apparently really want to login  
             unlink($r->dir_config('lonIDsDir')."/$handle.id");  sub reroute {
         } else {      my ($r) = @_;
 # Indeed, a valid token is found      &Apache::loncommon::content_type($r,'text/html');
             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);      $r->send_http_header;
     &Apache::loncommon::content_type($r,'text/html');      my $msg='<b>'.&mt('Sorry ...').'</b><br />'
     $r->send_http_header;             .&mt('Please [_1]log in again[_2].');
     my $start_page =       &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
         &Apache::loncommon::start_page('Already logged in');  }
     my $end_page =   
         &Apache::loncommon::end_page();  # ---------------------------------------------------------------- Main handler
             my $dest = '/adm/roles';  
             my %form = &get_form_items($r);  sub handler {
             if ($form{'logtoken'}) {      my $r = shift;
                 my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},      my $londocroot = $r->dir_config('lonDocRoot');
                                                      $form{'serverid'});  # Are we re-routing?
                 unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||      if (-e "$londocroot/lon-status/reroute.txt") {
                         ($tmpinfo eq 'no_such_host')) {   &reroute($r);
                     my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);   return OK;
                     $firsturl = &unescape($firsturl);      }
                     my %info;  
                     foreach my $item (@rest) {      &Apache::lonlocal::get_language_handle($r);
                         my ($key,$value) = split(/=/,$item);  
                         $info{$key} = &unescape($value);  # -------------------------------- Prevent users from attempting to login twice
                     }      my $handle = &Apache::lonnet::check_for_valid_session($r);
                     if ($firsturl ne '') {      if ($handle ne '') {
                         $info{'firsturl'} = $firsturl;          my $lonidsdir=$r->dir_config('lonIDsDir');
                         $dest = $firsturl;          if ($handle=~/^publicuser\_/) {
                         my $relogin;  # For "public user" - remove it, we apparently really want to login
                         if ($dest =~ m{^/tiny/$match_domain/\w+$}) {              unlink($r->dir_config('lonIDsDir')."/$handle.id");
                             if ($env{'request.course.id'}) {          } else {
                                 my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};  # Indeed, a valid token is found
                                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};              &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                                 my $symb = &Apache::loncommon::symb_from_tinyurl($dest,$cnum,$cdom);      &Apache::loncommon::content_type($r,'text/html');
                                 if ($symb) {      $r->send_http_header;
                                     unless (&set_deeplink_login(%info) eq 'ok') {      my $start_page =
                                         $relogin = 1;          &Apache::loncommon::start_page('Already logged in');
                                     }      my $end_page =
                                 }          &Apache::loncommon::end_page();
                             }              my $dest = '/adm/roles';
                             if ($relogin) {              if ($env{'form.firsturl'} ne '') {
                                 $r->print(                  $dest = $env{'form.firsturl'};
                                       $start_page                  if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
                                      .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'                      &Apache::lonnet::appenv({'request.deeplink.login' => $env{'form.firsturl'}});
                                      .'<p>'.&mt('Please [_1]log out[_2] first, and then try your access again',                  }
                                                 '<a href="/adm/logout">','</a>')              }
                                      .'</p>'              $r->print(
                                      .$end_page);                 $start_page
                             } else {                .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
                                 if (($info{'linkprot'}) || ($info{'linkkey'} ne '')) {                .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
                                     my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');                      ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
                                     unless (($token eq 'con_lost') || ($token eq 'refused') ||                .'</p>'
                                             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {                .$end_page
                                         $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;              );
                                     }              return OK;
                                 }          }
                                 $r->print(      }
                                       $start_page  
                                      .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'  # ---------------------------------------------------- No valid token, continue
                                      .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4] first, and then try your access again',  
                                                 '<a href="'.$dest.'">','</a>',  
                                                 '<a href="/adm/logout">','</a>')      my $buffer;
                                      .'</p>'      if ($r->header_in('Content-length') > 0) {
                                      .$end_page);   $r->read($buffer,$r->header_in('Content-length'),0);
                             }      }
                             return OK;      my %form;
                         }      foreach my $pair (split(/&/,$buffer)) {
                     }         my ($name,$value) = split(/=/,$pair);
                 }         $value =~ tr/+/ /;
             }         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
             $r->print(         $form{$name}=$value;
                $start_page      }
               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'  
               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'      if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')   &failed($r,'Username, password and domain need to be specified.',
               .'</p>'   \%form);
               .$end_page          return OK;
             );      }
             return OK;  
         }  # split user logging in and "su"-user
     }  
       ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
 # ---------------------------------------------------- No valid token, continue      $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
       $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
     my %form = &get_form_items($r);      $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});
     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {      $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
  &failed($r,'Username, password and domain need to be specified.',  
  \%form);      my $role   = $r->dir_config('lonRole');
         return OK;      my $domain = $r->dir_config('lonDefDomain');
     }      my $prodir = $r->dir_config('lonUsersDir');
       my $contact_name = &mt('LON-CAPA helpdesk');
 # split user logging in and "su"-user  
   # ---------------------------------------- Get the information from login token
     ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});  
     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});      my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});                                        $form{'serverid'});
     $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});  
     $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});      if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
           ($tmpinfo eq 'no_such_host')) {
     my $role   = $r->dir_config('lonRole');   &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
     my $domain = $r->dir_config('lonDefDomain');          return OK;
     my $prodir = $r->dir_config('lonUsersDir');      } else {
     my $contact_name = &mt('LON-CAPA helpdesk');   my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
      $form{'serverid'});
 # ---------------------------------------- Get the information from login token          if ( $reply ne 'ok' ) {
               &failed($r,'Session could not be opened.',\%form);
     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},      &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
                                       $form{'serverid'});      return OK;
    }
     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||       }
         ($tmpinfo eq 'no_such_host')) {  
  &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);      if (!&Apache::lonnet::domain($form{'udom'})) {
         return OK;          &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
     } else {          return OK;
  my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},      }
    $form{'serverid'});  
         if ( $reply ne 'ok' ) {      my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr,$linkstr)=split(/&/,$tmpinfo);
             &failed($r,'Session could not be opened.',\%form);      if ($rolestr) {
     &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");          $rolestr = &unescape($rolestr);
     return OK;      }
  }      if ($symbstr) {
     }          $symbstr= &unescape($symbstr);
       }
     if (!&Apache::lonnet::domain($form{'udom'})) {      if ($iptokenstr) {
         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);          $iptokenstr = &unescape($iptokenstr);
         return OK;      }
     }      if ($linkstr) {
           $linkstr = &unescape($linkstr);
     my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);      }
     $firsturl = &unescape($firsturl);      if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
     foreach my $item (@rest) {          $form{'firsturl'} = $firsturl;
         my ($key,$value) = split(/=/,$item);      }
         $form{$key} = &unescape($value);      if ($rolestr =~ /^role=/) {
     }          (undef,$form{'role'}) = split('=',$rolestr);
     if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {      }
         $form{'firsturl'} = $firsturl;      if ($symbstr =~ /^symb=/) {
     }          (undef,$form{'symb'}) = split('=',$symbstr);
     my $upass = &Apache::loncommon::des_decrypt($des_key,$form{'upass0'});      }
       if ($iptokenstr =~ /^iptoken=/) {
 # ---------------------------------------------------------------- Authenticate          (undef,$form{'iptoken'}) = split('=',$iptokenstr);
       }
     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});      if ($linkstr =~ /^linkprot=/) {
     my ($cancreate,$statustocreate) =          (undef,$form{'linkprot'}) = split('=',$linkstr);
         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});      } elsif ($linkstr =~ /^linkkey=/) {
     my $defaultauth;          (undef,$form{'linkkey'}) = split('=',$linkstr);
     if (ref($cancreate) eq 'ARRAY') {      }
         if (grep(/^login$/,@{$cancreate})) {  
             $defaultauth = 1;      my $upass = $ENV{HTTPS} ? $form{'upass0'}
         }          : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
     }  
     my $clientcancheckhost = 1;  # ---------------------------------------------------------------- Authenticate
     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,  
                                               $form{'udom'},$defaultauth,      my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
                                               $clientcancheckhost);      my ($cancreate,$statustocreate) =
               &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
 # --------------------------------------------------------------------- Failed?      my $defaultauth;
       if (ref($cancreate) eq 'ARRAY') {
     if ($authhost eq 'no_host') {          if (grep(/^login$/,@{$cancreate})) {
  &failed($r,'Username and/or password could not be authenticated.',              $defaultauth = 1;
  \%form);          }
         return OK;      }
     } elsif ($authhost eq 'no_account_on_host') {      my $clientcancheckhost = 1;
         if ($defaultauth) {      my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');                                                $form{'udom'},$defaultauth,
             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {                                                $clientcancheckhost);
                 return OK;  
             }  # --------------------------------------------------------------------- Failed?
             my $start_page =   
                 &Apache::loncommon::start_page('Create a user account in LON-CAPA',      if ($authhost eq 'no_host') {
                                                '',{'no_inline_link'   => 1,});   &failed($r,'Username and/or password could not be authenticated.',
             my $lonhost = $r->dir_config('lonHostID');   \%form);
             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};          return OK;
             my $contacts =       } elsif ($authhost eq 'no_account_on_host') {
                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',          if ($defaultauth) {
                                                         $form{'udom'},$origmail);              my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
             my ($contact_email) = split(',',$contacts);               unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
             my $output =                   return OK;
                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},              }
                                                        $domdesc,'',$lonhost,              my $start_page =
                                                        $contact_email,$contact_name,                  &Apache::loncommon::start_page('Create a user account in LON-CAPA');
                                                        undef,$statustocreate);              my $lonhost = $r->dir_config('lonHostID');
             &Apache::loncommon::content_type($r,'text/html');              my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
             $r->send_http_header;              my $contacts =
             &Apache::createaccount::print_header($r,$start_page);                  &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
             $r->print('<h3>'.&mt('Account creation').'</h3>'.                                                          $form{'udom'},$origmail);
                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.              my ($contact_email) = split(',',$contacts);
                       $output.&Apache::loncommon::end_page());              my $output =
             return OK;                  &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
         } else {                                                         $domdesc,'',$lonhost,
             &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);                                                         $contact_email,$contact_name,
             return OK;                                                         undef,$statustocreate);
         }              &Apache::loncommon::content_type($r,'text/html');
     }              $r->send_http_header;
               &Apache::createaccount::print_header($r,$start_page);
     if (($firsturl eq '') ||               $r->print('<h3>'.&mt('Account creation').'</h3>'.
  ($firsturl=~/^\/adm\/(logout|remote)/)) {                        &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
  $firsturl='/adm/roles';                        $output.&Apache::loncommon::end_page());
     }              return OK;
           } else {
     my ($hosthere,%sessiondata);              &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
     if ($form{'iptoken'}) {              return OK;
         %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});          }
         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});      }
         if (($sessiondata{'domain'} eq $form{'udom'}) &&  
             ($sessiondata{'username'} eq $form{'uname'})) {      if (($firsturl eq '') ||
             $hosthere = 1;   ($firsturl=~/^\/adm\/(logout|remote)/)) {
         }   $firsturl='/adm/roles';
     }      }
   
 # --------------------------------- Are we attempting to login as somebody else?      my $hosthere;
     if ($form{'suname'}) {      if ($form{'iptoken'}) {
         my ($suname,$sudom,$sudomref);          my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
         $suname = $form{'suname'};          my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
         $sudom = $form{'udom'};          if (($sessiondata{'domain'} eq $form{'udom'}) &&
         if ($form{'sudom'}) {              ($sessiondata{'username'} eq $form{'uname'})) {
             unless ($sudom eq $form{'sudom'}) {              $hosthere = 1;
                 if (&Apache::lonnet::domain($form{'sudom'})) {          }
                     $sudomref = [$form{'sudom'}];      }
                     $sudom = $form{'sudom'};  
                 }  # --------------------------------- Are we attempting to login as somebody else?
             }      if ($form{'suname'}) {
         }          my ($suname,$sudom,$sudomref);
 # ------------ see if the original user has enough privileges to pull this stunt          $suname = $form{'suname'};
  if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {          $sudom = $form{'udom'};
 # ---------------------------------------------------- see if the su-user exists          if ($form{'sudom'}) {
     unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {              unless ($sudom eq $form{'sudom'}) {
 # ------------------------------ see if the su-user is not too highly privileged                  if (&Apache::lonnet::domain($form{'sudom'})) {
  if (&Apache::lonnet::privileged($suname,$sudom)) {                      $sudomref = [$form{'sudom'}];
                     &Apache::lonnet::logthis('Attempted switch user to privileged user');                      $sudom = $form{'sudom'};
                 } else {                  }
                     my $noprivswitch;              }
 #          }
 # su-user's home server and user's home server must have one of:  # ------------ see if the original user has enough privileges to pull this stunt
 # (a) same domain   if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
 # (b) same primary library server for the two domains  # ---------------------------------------------------- see if the su-user exists
 # (c) same "internet domain" for primary library server(s) for home servers' domains      unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
 #  # ------------------------------ see if the su-user is not too highly privileged
                     my $suprim = &Apache::lonnet::domain($sudom,'primary');   if (&Apache::lonnet::privileged($suname,$sudom)) {
                     my $suintdom = &Apache::lonnet::internet_dom($suprim);                      &Apache::lonnet::logthis('Attempted switch user to privileged user');
                     unless ($sudom eq $form{'udom'}) {                  } else {
                         my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');                      my $noprivswitch;
                         my $uintdom = &Apache::lonnet::internet_dom($uprim);  #
                         unless ($suprim eq $uprim) {  # su-user's home server and user's home server must have one of:
                             unless ($suintdom eq $uintdom) {  # (a) same domain
                                 &Apache::lonnet::logthis('Attempted switch user '  # (b) same primary library server for the two domains
                                    .'to user with different "internet domain".');  # (c) same "internet domain" for primary library server(s) for home servers' domains
                                 $noprivswitch = 1;  #
                             }                      my $suprim = &Apache::lonnet::domain($sudom,'primary');
                         }                      my $suintdom = &Apache::lonnet::internet_dom($suprim);
                     }                      unless ($sudom eq $form{'udom'}) {
                           my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
                     unless ($noprivswitch) {                          my $uintdom = &Apache::lonnet::internet_dom($uprim);
 #                          unless ($suprim eq $uprim) {
 # server where log-in occurs must have same "internet domain" as su-user's home                              unless ($suintdom eq $uintdom) {
 # server                                  &Apache::lonnet::logthis('Attempted switch user '
 #                                     .'to user with different "internet domain".');                        
                         my $lonhost = $r->dir_config('lonHostID');                                  $noprivswitch = 1;
                         my $hostintdom = &Apache::lonnet::internet_dom($lonhost);                              }
                         if ($hostintdom ne $suintdom) {                          }
                             &Apache::lonnet::logthis('Attempted switch user on a '                      }
                                 .'server with a different "internet domain".');  
                         } else {                      unless ($noprivswitch) {
   #
 # -------------------------------------------------------- actually switch users  # server where log-in occurs must have same "internet domain" as su-user's home
   # server
     &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.  #
  $form{'udom'}.' logging in as '.$suname.':'.$sudom);                          my $lonhost = $r->dir_config('lonHostID');
     $form{'uname'}=$suname;                          my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
                             if ($form{'udom'} ne $sudom) {                          if ($hostintdom ne $suintdom) {
                                 $form{'udom'}=$sudom;                              &Apache::lonnet::logthis('Attempted switch user on a '
                             }                                  .'server with a different "internet domain".');
                         }                          } else {
                     }  
  }  # -------------------------------------------------------- actually switch users
     }  
  } else {              &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
     &Apache::lonnet::logthis('Non-privileged user attempting switch user');                                $form{'udom'}.' logging in as '.$suname.':'.$sudom);
  }              $form{'uname'}=$suname;
     }                              if ($form{'udom'} ne $sudom) {
                                   $form{'udom'}=$sudom;
     my ($is_balancer,$otherserver);                              }
                           }
     unless ($hosthere) {                      }
         ($is_balancer,$otherserver) =   }
             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');      }
         if ($is_balancer) {   } else {
             # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)      &Apache::lonnet::logthis('Non-privileged user attempting switch user');
             my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);   }
             if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {      }
                 $otherserver = $found_server;  
             }      my ($is_balancer,$otherserver);
             if ($otherserver eq '') {  
                 my $lowest_load;      unless ($hosthere) {
                 ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});          ($is_balancer,$otherserver) =
                 if ($lowest_load > 100) {              &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
                     $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});          if ($is_balancer) {
                 }              if ($otherserver eq '') {
             }                  my $lowest_load;
             if ($otherserver ne '') {                  ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
                 my @hosts = &Apache::lonnet::current_machine_ids();                  if ($lowest_load > 100) {
                 if (grep(/^\Q$otherserver\E$/,@hosts)) {                      $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});
                     $hosthere = $otherserver;                  }
                 }              }
             }              if ($otherserver ne '') {
         }                  my @hosts = &Apache::lonnet::current_machine_ids();
     }                  if (grep(/^\Q$otherserver\E$/,@hosts)) {
                       $hosthere = $otherserver;
     if (($is_balancer) && (!$hosthere)) {                  }
         if ($otherserver) {              }
             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,          }
                      \%form);      }
             my $switchto = '/adm/switchserver?otherserver='.$otherserver;  
             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {      if (($is_balancer) && (!$hosthere)) {
                 $switchto .= '&origurl='.$firsturl;          if ($otherserver) {
             }              &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
             if ($form{'role'}) {                       \%form);
                 $switchto .= '&role='.$form{'role'};              my $switchto = '/adm/switchserver?otherserver='.$otherserver;
             }              if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
             if ($form{'symb'}) {                  $switchto .= '&origurl='.$firsturl; #should escape
                 $switchto .= '&symb='.$form{'symb'};              }
             }              if ($form{'role'}) {
             if ($form{'linkprot'}) {                  $switchto .= '&role='.$form{'role'};
                 $env{'request.linkprot'} = $form{'linkprot'};              }
             } elsif ($form{'linkkey'} ne '') {              if ($form{'symb'}) {
                 $env{'request.linkkey'} = $form{'linkkey'};                  $switchto .= '&symb='.$form{'symb'};
             }              }
             if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {              if ($form{'linkprot'}) {
                 &set_deeplink_login(%form);                  $env{'request.linkprot'} = $form{'linkprot'};
             }              } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
             $r->internal_redirect($switchto);                  if ($form{'linkkey'}) {
         } else {                      $env{'request.linkkey'} = $form{'linkkey'};
             &Apache::loncommon::content_type($r,'text/html');                  }
             $r->send_http_header;                  $env{'request.deeplink.login'} = $form{'firsturl'};
             $r->print(&noswitch());              }
         }              $r->internal_redirect($switchto);
         return OK;          } else {
     } else {              $r->print(&noswitch());
         if (!&check_can_host($r,\%form,$authhost)) {          }
             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});          return OK;
             if ($otherserver) {      } else {
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,          if (!&check_can_host($r,\%form,$authhost)) {
                          \%form);              my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;              if ($otherserver) {
                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                     $switchto .= '&origurl='.$firsturl;                           \%form);
                 }                  my $switchto = '/adm/switchserver?otherserver='.$otherserver;
                 if ($form{'role'}) {                  if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
                     $switchto .= '&role='.$form{'role'};                      $switchto .= '&origurl='.$firsturl; #should escape
                 }                  }
                 if ($form{'symb'}) {                  if ($form{'role'}) {
                     $switchto .= '&symb='.$form{'symb'};                      $switchto .= '&role='.$form{'role'};
                 }                  }
                 if ($form{'linkprot'}) {                  if ($form{'symb'}) {
                     $env{'request.linkprot'} = $form{'linkprot'};                      $switchto .= '&symb='.$form{'symb'};
                 } elsif ($form{'linkkey'} ne '') {                  }
                     $env{'request.linkkey'} = $form{'linkkey'};                  if ($form{'linkprot'}) {
                 }                      $env{'request.linkprot'} = $form{'linkprot'};
                 if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {                  } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
                     &set_deeplink_login(%form);                      if ($form{'linkkey'}) {
                 }                          $env{'request.linkkey'} = $form{'linkkey'};
                 $r->internal_redirect($switchto);                      }
             } else {                      $env{'request.deeplink.login'} = $form{'firsturl'};
                 &Apache::loncommon::content_type($r,'text/html');  
                 $r->send_http_header;                  }
                 $r->print(&noswitch());                  $r->internal_redirect($switchto);
             }              } else {
             return OK;                  $r->print(&noswitch());
         }              }
               return OK;
 # ------------------------------------------------------- Do the load balancing          }
   
 # ---------------------------------------------------------- Determine own load  # ------------------------------------------------------- Do the load balancing
         my $loadlim = $r->dir_config('lonLoadLim');  
         my $loadavg;  # ---------------------------------------------------------- Determine own load
         {          my $loadlim = $r->dir_config('lonLoadLim');
             my $loadfile=Apache::File->new('/proc/loadavg');          my $loadavg;
             $loadavg=<$loadfile>;          {
         }              my $loadfile=Apache::File->new('/proc/loadavg');
         $loadavg =~ s/\s.*//g;              $loadavg=<$loadfile>;
         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);          }
         my $userloadpercent=&Apache::lonnet::userload();          $loadavg =~ s/\s.*//g;
           my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
 # ---------------------------------------------------------- Are we overloaded?          my $userloadpercent=&Apache::lonnet::userload();
         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {  
             my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});  # ---------------------------------------------------------- Are we overloaded?
             if (!$unloaded) {          if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});              my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
             }              if (!$unloaded) {
             if ($unloaded) {                  ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',              }
                          undef,\%form);              if ($unloaded) {
                 if ($form{'linkprot'}) {                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
                     $env{'request.linkprot'} = $form{'linkprot'};                           undef,\%form);
                 } elsif ($form{'linkkey'} ne '') {                  if ($form{'linkprot'}) {
                     $env{'request.linkkey'} = $form{'linkkey'};                      $env{'request.linkprot'} = $form{'linkprot'};
                 }                  } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
                 if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {                      if ($form{'linkkey'}) {
                     &set_deeplink_login(%form);                          $env{'request.linkkey'} = $form{'linkkey'};
                 }                      }
                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);                      $env{'request.deeplink.login'} = $form{'firsturl'};
                 return OK;                  }
             }                  $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
         }                  return OK;
         if (($is_balancer) && ($hosthere)) {              }
             $form{'noloadbalance'} = $hosthere;          }
         }          if (($is_balancer) && ($hosthere)) {
         my $extra_env;              $form{'noloadbalance'} = $hosthere;
         if (($hosthere) && ($sessiondata{'sessionserver'} ne '')) {          }
             if ($sessiondata{'origurl'} ne '') {          my $extra_env;
                 $firsturl = $sessiondata{'origurl'};          if ($form{'linkprot'}) {
                 $form{'firsturl'} = $sessiondata{'origurl'};              my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
                 my @names = ('role','symb','linkprot','linkkey');              if ($linkprotector) {
                 foreach my $item (@names) {                  $extra_env = {'user.linkprotector' => $linkprotector,
                     if ($sessiondata{$item} ne '') {                                'user.linkproturi'   => $uri,};
                         $form{$item} = $sessiondata{$item};              }
                     }          } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
                 }              if ($form{'linkkey'}) {
             }                  $extra_env = {'user.deeplinkkey' => $form{'linkkey'},
         }                                'user.keyedlinkuri' => $form{'firsturl'},
         if ($form{'linkprot'}) {                                'request.deeplink.login' => $form{'firsturl'}};
             my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);              } else {
             if ($linkprotector) {                  $extra_env = {'request.deeplink.login' => $form{'firsturl'}};
                 $extra_env = {'user.linkprotector' => $linkprotector,              }
                               'user.linkproturi'   => $uri};          }
             }          &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
         } elsif ($form{'linkkey'} ne '') {                   \%form);
             $extra_env = {'user.deeplinkkey'  => $form{'linkkey'},          return OK;
                           'user.keyedlinkuri' => $form{'firsturl'}};      }
         }  }
         if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {  
             &set_deeplink_login(%form);  sub check_can_host {
             if ($form{'linkprot'}) {      my ($r,$form,$authhost,$domdesc) = @_;
                 if (ref($extra_env) eq 'HASH') {      return unless (ref($form) eq 'HASH');
                     %{$extra_env} = ( %{$extra_env}, 'request.linkprot' => $form{'linkprot'} );      my $canhost = 1;
                 } else {      my $lonhost = $r->dir_config('lonHostID');
                     $extra_env = {'request.linkprot' => $form{'linkprot'}};      my $udom = $form->{'udom'};
                 }      my @intdoms;
             } elsif ($form{'linkkey'} ne '') {      my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
                 if (ref($extra_env) eq 'HASH') {      if (ref($internet_names) eq 'ARRAY') {
                     %{$extra_env} = ( %{$extra_env}, 'request.linkkey' => $form{'linkkey'} );          @intdoms = @{$internet_names};
                 } else {      }
                     $extra_env = {'request.linkkey' => $form{'linkkey'}};      my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                 }      my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
             }      unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
             if ($env{'request.deeplink.login'}) {          my $machine_dom = &Apache::lonnet::host_domain($lonhost);
                 if (ref($extra_env) eq 'HASH') {          my $hostname = &Apache::lonnet::hostname($lonhost);
                     %{$extra_env} = ( %{$extra_env}, 'request.deeplink.login' => $form{'firsturl'} );          my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
                 } else {          my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                     $extra_env = {'request.deeplink.login' => $form{'firsturl'}};          my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                 }          my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
             }          my $loncaparev;
         }          if ($authhost eq 'no_account_on_host') {
         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
                  \%form);          } else {
         return OK;              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
     }          }
 }          $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
                                                        $udomdefaults{'remotesessions'},
 sub get_form_items {                                                       $defdomdefaults{'hostedsessions'});
     my ($r) = @_;      }
     my $buffer;      unless ($canhost) {
     if ($r->header_in('Content-length') > 0) {          if ($authhost eq 'no_account_on_host') {
         $r->read($buffer,$r->header_in('Content-length'),0);              my $checkloginvia = 1;
     }              my ($login_host,$hostname) =
     my %form;                  &Apache::lonnet::choose_server($udom,$checkloginvia);
     foreach my $pair (split(/&/,$buffer)) {              &Apache::loncommon::content_type($r,'text/html');
        my ($name,$value) = split(/=/,$pair);              $r->send_http_header;
        $value =~ tr/+/ /;              if ($login_host ne '') {
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;                  my $protocol = $Apache::lonnet::protocol{$login_host};
        $form{$name}=$value;                  $protocol = 'http' if ($protocol ne 'https');
     }                  my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
     return %form;  #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
 }                  $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
                             '<h3>'.&mt('Account creation').'</h3>'.
 sub set_deeplink_login {                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
     my (%form) = @_;                            '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
     my $disallow;                            '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
     if ($form{'firsturl'} =~ m{^/tiny/($match_domain)/\w+$}) {                            &Apache::loncommon::end_page());
         my $cdom = $1;              } else {
         my ($cnum,$symb) = &Apache::loncommon::symb_from_tinyurl($form{'firsturl'},'',$cdom);                  $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
         if ($symb) {                            '<h3>'.&mt('Account creation unavailable').'</h3>'.
             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
                 my $deeplink;                            '<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'.
                 if ($symb =~ /\.(page|sequence)$/) {                            &Apache::loncommon::end_page());
                     my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($symb))[2]);              }
                     my $navmap = Apache::lonnavmaps::navmap->new();          } else {
                     if (ref($navmap)) {              &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
                         $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');                       $form);
                     }              if ($form->{'linkprot'}) {
                 } else {                  $env{'request.linkprot'} = $form->{'linkprot'};
                     $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);              } elsif ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
                 }                  if ($form->{'linkkey'}) {
                 if ($deeplink ne '') {                      $env{'request.linkkey'} = $form->{'linkkey'};
                     my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);                  }
                     if (($protect ne 'none') && ($protect ne '')) {                  $env{'request.deeplink.login'} = $form->{'firsturl'};
                         my ($acctype,$item) = split(/:/,$protect);              }
                         if ($acctype =~ /lti(c|d)$/) {              my ($otherserver) = &Apache::lonnet::choose_server($udom);
                             unless ($form{'linkprot'} eq $item.$1.':'.$env{'request.deeplink.login'}) {              $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
                                 $disallow = 1;          }
                             }      }
                         } elsif ($acctype eq 'key') {      return $canhost;
                             unless ($form{'linkkey'} eq $item) {  }
                                 $disallow = 1;  
                             }  sub noswitch {
                         }      my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
                     }                   '<h3>'.&mt('Session unavailable').'</h3>'.
                 }                   &mt('This LON-CAPA server is unable to host your session.').'<br />'.
                 unless ($disallow) {                   '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
                     $env{'request.deeplink.login'} = $form{'firsturl'};                   &Apache::loncommon::end_page();
                 }      return $result;
             } else {  }
                 $env{'request.deeplink.login'} = $form{'firsturl'};  
             }  sub loginhelpdisplay {
         }      my ($authdomain) = @_;
     }      my $login_help = 1;
     if ($disallow) {      my $lang = &Apache::lonlocal::current_language();
         return;      if ($login_help) {
     }          my $dom = $authdomain;
     return 'ok';          if ($dom eq '') {
 }              $dom = &Apache::lonnet::default_login_domain();
           }
 sub set_retry_token {          my %domconfhash = &Apache::loncommon::get_domainconf($dom);
     my ($form,$lonhost,$querystr) = @_;          my $loginhelp_url;
     if (ref($form) eq 'HASH') {          if ($lang) {
         my ($firsturl,$token,$extras,@names);              $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
         @names = ('role','symb','linkprot','linkkey','iptoken');              if ($loginhelp_url ne '') {
         foreach my $name (@names) {                  return $loginhelp_url;
             if ($form->{$name} ne '') {              }
                 $extras .= '&'.$name.'='.&escape($form->{$name});          }
                 last if ($name eq 'linkprot');          $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
             }          if ($loginhelp_url ne '') {
         }              return $loginhelp_url;
         my $firsturl = $form->{'firsturl'};          } else {
         if (($firsturl ne '') || ($extras ne '')) {              return '/adm/loginproblems.html';
             $extras .= ':retry';          }
             $token = &Apache::lonnet::reply('tmpput:'.&escape($firsturl).      }
                                             $extras,$lonhost);      return;
             if (($token eq 'con_lost') || ($token eq 'no_such_host')) {  }
                 return 'fail';  
             } else {  1;
                 if (ref($querystr)) {  __END__
                     $$querystr = 'retry='.$token;  
                 }  
                 return 'ok';  
             }  
         }  
     }  
     return;  
 }  
   
 sub check_can_host {  
     my ($r,$form,$authhost,$domdesc) = @_;  
     return unless (ref($form) eq 'HASH');  
     my $canhost = 1;  
     my $lonhost = $r->dir_config('lonHostID');  
     my $udom = $form->{'udom'};  
     my @intdoms;  
     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);  
     if (ref($internet_names) eq 'ARRAY') {  
         @intdoms = @{$internet_names};  
     }  
     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');  
     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);  
     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {  
         my $machine_dom = &Apache::lonnet::host_domain($lonhost);  
         my $hostname = &Apache::lonnet::hostname($lonhost);  
         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);  
         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);  
         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);  
         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);  
         my $loncaparev;  
         if ($authhost eq 'no_account_on_host') {  
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);  
         } else {  
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);  
         }  
         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,  
                                                      $udomdefaults{'remotesessions'},  
                                                      $defdomdefaults{'hostedsessions'});  
     }  
     unless ($canhost) {  
         if ($authhost eq 'no_account_on_host') {  
             my $checkloginvia = 1;  
             my ($login_host,$hostname) =   
                 &Apache::lonnet::choose_server($udom,$checkloginvia);  
             &Apache::loncommon::content_type($r,'text/html');  
             $r->send_http_header;  
             if ($login_host ne '') {  
                 my $protocol = $Apache::lonnet::protocol{$login_host};  
                 $protocol = 'http' if ($protocol ne 'https');  
                 my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);  
                 $hostname = $alias if ($alias ne '');  
                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';  
 #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host  
                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').  
                           '<h3>'.&mt('Account creation').'</h3>'.  
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.  
                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.  
                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').  
                           &Apache::loncommon::end_page());  
             } else {  
                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').  
                           '<h3>'.&mt('Account creation unavailable').'</h3>'.  
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.  
                           '<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'.  
                           &Apache::loncommon::end_page());  
             }  
         } else {  
             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,  
                      $form);  
             if ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {  
                 $env{'request.deeplink.login'} = $form->{'firsturl'};  
             }  
             if ($form->{'linkprot'}) {  
                 $env{'request.linkprot'} = $form->{'linkprot'};  
             } elsif ($form->{'linkkey'} ne '') {  
                 $env{'request.linkkey'} = $form->{'linkkey'};  
             }  
             my ($otherserver) = &Apache::lonnet::choose_server($udom);  
             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);  
         }  
     }  
     return $canhost;  
 }  
   
 sub noswitch {  
     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').  
                  '<h3>'.&mt('Session unavailable').'</h3>'.  
                  &mt('This LON-CAPA server is unable to host your session.').'<br />'.  
                  '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.  
                  &Apache::loncommon::end_page();  
     return $result;  
 }  
   
 sub loginhelpdisplay {  
     my ($authdomain) = @_;  
     my $login_help = 1;  
     my $lang = &Apache::lonlocal::current_language();  
     if ($login_help) {  
         my $dom = $authdomain;  
         if ($dom eq '') {  
             $dom = &Apache::lonnet::default_login_domain();  
         }  
         my %domconfhash = &Apache::loncommon::get_domainconf($dom);  
         my $loginhelp_url;  
         if ($lang) {  
             $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};  
             if ($loginhelp_url ne '') {  
                 return $loginhelp_url;  
             }  
         }  
         $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};  
         if ($loginhelp_url ne '') {  
             return $loginhelp_url;  
         } else {  
             return '/adm/loginproblems.html';  
         }  
     }  
     return;  
 }  
   
 1;  
 __END__  
   
   

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


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