Diff for /loncom/auth/lonauth.pm between versions 1.121.2.17 and 1.142

version 1.121.2.17, 2019/08/01 00:42:34 version 1.142, 2017/02/17 14:34:21
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;  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 DynaLoader; # for Crypt::DES version
 use Apache::lonnet;  use Crypt::DES;
 use Apache::lonmenu();  use Apache::loncommon();
 use Apache::createaccount;  use Apache::lonnet;
 use Fcntl qw(:flock);  use Apache::lonmenu();
 use Apache::lonlocal;  use Apache::createaccount;
 use Apache::File();  use Fcntl qw(:flock);
 use HTML::Entities;  use Apache::lonlocal;
    use Apache::File();
 # ------------------------------------------------------------ Successful login  use HTML::Entities;
 sub success {   
     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,  # ------------------------------------------------------------ Successful login
  $form) = @_;  sub success {
       my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
 # ------------------------------------------------------------ Get cookie ready   $form) = @_;
     my $cookie =  
  &Apache::loncommon::init_user_environment($r, $username, $domain,  # ------------------------------------------------------------ Get cookie ready
   $authhost, $form,      my $cookie =
   {'extra_env' => $extra_env,});   &Apache::loncommon::init_user_environment($r, $username, $domain,
     $authhost, $form,
     my $public=($username eq 'public' && $domain eq 'public');    {'extra_env' => $extra_env,});
   
     if ($public or $lowerurl eq 'noredirect') { return $cookie; }      my $public=($username eq 'public' && $domain eq 'public');
   
 # -------------------------------------------------------------------- Log this      if ($public or $lowerurl eq 'noredirect') { return $cookie; }
   
     &Apache::lonnet::log($domain,$username,$authhost,  # -------------------------------------------------------------------- Log this
                          "Login $ENV{'REMOTE_ADDR'}");  
       &Apache::lonnet::log($domain,$username,$authhost,
 # ------------------------------------------------- Check for critical messages                           "Login $ENV{'REMOTE_ADDR'}");
   
     my @what=&Apache::lonnet::dump('critical',$domain,$username);  # ------------------------------------------------- Check for critical messages
     if ($what[0]) {  
  if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {      my @what=&Apache::lonnet::dump('critical',$domain,$username);
     $lowerurl='/adm/email?critical=display';      if ($what[0]) {
         }   if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
     }      $lowerurl='/adm/email?critical=display';
           }
 # ------------------------------------------------------------ Get cookie ready      }
     $cookie="lonID=$cookie; path=/; HttpOnly";  
 # -------------------------------------------------------- Menu script and info  # ------------------------------------------------------------ Get cookie ready
     my $destination = $lowerurl;      $cookie="lonID=$cookie; path=/; HttpOnly";
   # -------------------------------------------------------- Menu script and info
     if (defined($form->{role})) {      my $destination = $lowerurl;
         my $envkey = 'user.role.'.$form->{role};  
         my $now=time;      if (defined($form->{role})) {
         my $then=$env{'user.login.time'};          my $envkey = 'user.role.'.$form->{role};
         my $refresh=$env{'user.refresh.time'};          my $now=time;
         my $update=$env{'user.update.time'};          my $then=$env{'user.login.time'};
         if (!$update) {          my $refresh=$env{'user.refresh.time'};
             $update = $then;          my $update=$env{'user.update.time'};
         }          if (!$update) {
         if (exists($env{$envkey})) {              $update = $then;
             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);          }
             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,          if (exists($env{$envkey})) {
                                          \$trolecode,\$tstatus,\$tstart,\$tend);              my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
             if ($tstatus eq 'is') {              &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
                 $destination  .= ($destination =~ /\?/) ? '&' : '?';                                           \$trolecode,\$tstatus,\$tstart,\$tend);
                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');              if ($tstatus eq 'is') {
                 $destination .= 'selectrole=1&'.$newrole.'=1';                  $destination  .= ($destination =~ /\?/) ? '&' : '?';
             }                  my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
         }                  $destination .= 'selectrole=1&'.$newrole.'=1';
     }              }
     if (defined($form->{symb})) {          }
         my $destsymb = $form->{symb};      }
         $destination  .= ($destination =~ /\?/) ? '&' : '?';      if (defined($form->{symb})) {
         if ($destsymb =~ /___/) {          my $destsymb = $form->{symb};
             # FIXME Need to deal with encrypted symbs and urls as needed.          $destination  .= ($destination =~ /\?/) ? '&' : '?';
             my ($map,$resid,$desturl)=split(/___/,$destsymb);          if ($destsymb =~ /___/) {
             $desturl = &Apache::lonnet::clutter($desturl);              # FIXME Need to deal with encrypted symbs and urls as needed.
             $desturl = &HTML::Entities::encode($desturl,'"<>&');              my ($map,$resid,$desturl)=split(/___/,$destsymb);
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');              $desturl = &Apache::lonnet::clutter($desturl);
             $destination .= 'destinationurl='.$desturl.              $desturl = &HTML::Entities::encode($desturl,'"<>&');
                             '&destsymb='.$destsymb;              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
         } else {              $destination .= 'destinationurl='.$desturl.
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');                              '&destsymb='.$destsymb;
             $destination .= 'destinationurl='.$destsymb;          } else {
         }              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
     }              $destination .= 'destinationurl='.$destsymb;
     if ($destination =~ m{^/adm/roles}) {          }
         $destination  .= ($destination =~ /\?/) ? '&' : '?';      }
         $destination .= 'source=login';      if ($destination =~ m{^/adm/roles}) {
     }          $destination  .= ($destination =~ /\?/) ? '&' : '?';
           $destination .= 'source=login';
     my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});      }
     my $startupremote=&Apache::lonmenu::startupremote($destination);  
     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);      my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="loncapaclient";');
     my $setflags=&Apache::lonmenu::setflags();      my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
     my $maincall=&Apache::lonmenu::maincall();      my $brcrum = [{'href' => '',
     my $brcrum = [{'href' => '',                     'text' => 'Successful Login'},];
                    'text' => 'Successful Login'},];      my $args = {'bread_crumbs' => $brcrum,};
     my $start_page=&Apache::loncommon::start_page('Successful Login',      unless ((defined($form->{role})) || (defined($form->{symb}))) {
                                                   $startupremote,          my $update=$env{'user.update.time'};
                                                   {'no_inline_link' => 1,          if (!$update) {
                                                    'bread_crumbs' => $brcrum,});              $update = $env{'user.login.time'};
     my $end_page  =&Apache::loncommon::end_page();          }
           my %roles_in_env;
     my $continuelink;          my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
     if ($env{'environment.remote'} eq 'off') {          if ($showcount == 1) {
  $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';              foreach my $rolecode (keys(%roles_in_env)) {
     }                  my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
 # ------------------------------------------------- Output for successful login                  if ($cid) {
                       my %coursedescription =
     &Apache::loncommon::content_type($r,'text/html');                          &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
     $r->header_out('Set-cookie' => $cookie);                      if ($coursedescription{'type'} eq 'Placement') {
     $r->send_http_header;                          $args->{'crstype'} = 'Placement';
                       }
     my %lt=&Apache::lonlocal::texthash(                      last;
        'wel' => 'Welcome',                  }
        'pro' => 'Login problems?',              }
        );          }
     my $loginhelp = &loginhelpdisplay($domain);      }
     if ($loginhelp) {      my $start_page=&Apache::loncommon::start_page('Successful Login',
         $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';                                                    $header,$args);
     }      my $end_page  =&Apache::loncommon::end_page();
   
     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 $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
     $r->print(<<ENDSUCCESS);  # ------------------------------------------------- Output for successful login
 $start_page  
 $setflags      &Apache::loncommon::content_type($r,'text/html');
 $windowinfo      $r->header_out('Set-cookie' => $cookie);
 <h1>$lt{'wel'}</h1>      $r->send_http_header;
 $welcome  
 $loginhelp      my %lt=&Apache::lonlocal::texthash(
 $remoteinfo         'wel' => 'Welcome',
 $maincall         'pro' => 'Login problems?',
 $continuelink         );
 $end_page      my $loginhelp = &loginhelpdisplay($domain);
 ENDSUCCESS      if ($loginhelp) {
     return;          $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
 }      }
   
 # --------------------------------------------------------------- Failed login!      my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
       $r->print(<<ENDSUCCESS);
 sub failed {  $start_page
     my ($r,$message,$form) = @_;  $windowinfo
     (undef,undef,undef,my $clientmathml,my $clientunicode) =  <h1>$lt{'wel'}</h1>
         &Apache::loncommon::decode_user_agent();  $welcome
     my $args = {};  $loginhelp
     if ($clientunicode && !$clientmathml) {  $continuelink
         $args = {'browser.unicode' => 1};  $end_page
     }  ENDSUCCESS
       return;
     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);  }
     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});  
     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});  # --------------------------------------------------------------- Failed login!
     if (&Apache::lonnet::domain($udom,'description') eq '') {  
         undef($udom);  sub failed {
     }      my ($r,$message,$form) = @_;
     my $retry = '/adm/login';      (undef,undef,undef,my $clientmathml,my $clientunicode) =
     if ($uname eq $form->{'uname'}) {          &Apache::loncommon::decode_user_agent();
         $retry .= '?username='.$uname;      my $args = {};
     }      if ($clientunicode && !$clientmathml) {
     if ($udom) {          $args = {'browser.unicode' => 1};
         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;      }
     }  
     if (exists($form->{role})) {      my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
         my $role = &Apache::loncommon::cleanup_html($form->{role});      my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
         if ($role ne '') {      my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
             $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;      if (&Apache::lonnet::domain($udom,'description') eq '') {
         }          undef($udom);
     }      }
     if (exists($form->{symb})) {      my $retry = '/adm/login';
         my $symb = &Apache::loncommon::cleanup_html($form->{symb});      if ($uname eq $form->{'uname'}) {
         if ($symb ne '') {          $retry .= '?username='.$uname;
             $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;      }
         }      if ($udom) {
     }          $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
     my $end_page = &Apache::loncommon::end_page();      }
     &Apache::loncommon::content_type($r,'text/html');      if (exists($form->{role})) {
     $r->send_http_header;          my $role = &Apache::loncommon::cleanup_html($form->{role});
     my @actions =          if ($role ne '') {
          (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));              $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
     my $loginhelp = &loginhelpdisplay($udom);          }
     if ($loginhelp) {      }
         push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');      if (exists($form->{symb})) {
     }          my $symb = &Apache::loncommon::cleanup_html($form->{symb});
     #FIXME: link to helpdesk might be added here          if ($symb ne '') {
               $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
     $r->print(          }
        $start_page      }
       .'<h2>'.&mt('Sorry ...').'</h2>'      my $end_page = &Apache::loncommon::end_page();
       .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'      &Apache::loncommon::content_type($r,'text/html');
       .&Apache::lonhtmlcommon::actionbox(\@actions)      $r->send_http_header;
       .$end_page      my @actions =
     );           (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
  }      my $loginhelp = &loginhelpdisplay($udom);
       if ($loginhelp) {
 # ------------------------------------------------------------------ Rerouting!          push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
       }
 sub reroute {      #FIXME: link to helpdesk might be added here
     my ($r) = @_;  
     &Apache::loncommon::content_type($r,'text/html');      $r->print(
     $r->send_http_header;         $start_page
     my $msg='<b>'.&mt('Sorry ...').'</b><br />'        .'<h2>'.&mt('Sorry ...').'</h2>'
            .&mt('Please [_1]log in again[_2].');        .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});        .&Apache::lonhtmlcommon::actionbox(\@actions)
 }        .$end_page
       );
 # ---------------------------------------------------------------- Main handler   }
   
 sub handler {  # ------------------------------------------------------------------ Rerouting!
     my $r = shift;  
     my $londocroot = $r->dir_config('lonDocRoot');  sub reroute {
 # Are we re-routing?      my ($r) = @_;
     if (-e "$londocroot/lon-status/reroute.txt") {      &Apache::loncommon::content_type($r,'text/html');
  &reroute($r);      $r->send_http_header;
  return OK;      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});
     &Apache::lonlocal::get_language_handle($r);  }
   
 # -------------------------------- Prevent users from attempting to login twice  # ---------------------------------------------------------------- Main handler
     my $handle = &Apache::lonnet::check_for_valid_session($r);  
     if ($handle ne '') {  sub handler {
         my $lonidsdir=$r->dir_config('lonIDsDir');      my $r = shift;
         if ($handle=~/^publicuser\_/) {      my $londocroot = $r->dir_config('lonDocRoot');
 # For "public user" - remove it, we apparently really want to login  # Are we re-routing?
             unlink($r->dir_config('lonIDsDir')."/$handle.id");      if (-e "$londocroot/lon-status/reroute.txt") {
         } else {   &reroute($r);
 # Indeed, a valid token is found   return OK;
             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);      }
     &Apache::loncommon::content_type($r,'text/html');  
     $r->send_http_header;      &Apache::lonlocal::get_language_handle($r);
     my $start_page =   
         &Apache::loncommon::start_page('Already logged in');  # -------------------------------- Prevent users from attempting to login twice
     my $end_page =       my $handle = &Apache::lonnet::check_for_valid_session($r);
         &Apache::loncommon::end_page();      if ($handle ne '') {
             my $dest = '/adm/roles';          my $lonidsdir=$r->dir_config('lonIDsDir');
             if ($env{'form.firsturl'} ne '') {          if ($handle=~/^publicuser\_/) {
                 $dest = $env{'form.firsturl'};  # For "public user" - remove it, we apparently really want to login
             }              unlink($r->dir_config('lonIDsDir')."/$handle.id");
             $r->print(          } else {
                $start_page  # Indeed, a valid token is found
               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'              &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'      &Apache::loncommon::content_type($r,'text/html');
                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')      $r->send_http_header;
               .'</p>'      my $start_page =
               .$end_page          &Apache::loncommon::start_page('Already logged in');
             );      my $end_page =
             return OK;          &Apache::loncommon::end_page();
         }              my $dest = '/adm/roles';
     }              if ($env{'form.firsturl'} ne '') {
                   $dest = $env{'form.firsturl'};
 # ---------------------------------------------------- No valid token, continue              }
               $r->print(
                  $start_page
     my $buffer;                .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
     if ($r->header_in('Content-length') > 0) {                .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
  $r->read($buffer,$r->header_in('Content-length'),0);                      ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
     }                .'</p>'
     my %form;                .$end_page
     foreach my $pair (split(/&/,$buffer)) {              );
        my ($name,$value) = split(/=/,$pair);              return OK;
        $value =~ tr/+/ /;          }
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;      }
        $form{$name}=$value;  
     }  # ---------------------------------------------------- No valid token, continue
   
     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {  
  &failed($r,'Username, password and domain need to be specified.',      my $buffer;
  \%form);      if ($r->header_in('Content-length') > 0) {
         return OK;   $r->read($buffer,$r->header_in('Content-length'),0);
     }      }
       my %form;
 # split user logging in and "su"-user      foreach my $pair (split(/&/,$buffer)) {
          my ($name,$value) = split(/=/,$pair);
     ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});         $value =~ tr/+/ /;
     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});         $form{$name}=$value;
     $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});      }
     $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});  
       if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
     my $role   = $r->dir_config('lonRole');   &failed($r,'Username, password and domain need to be specified.',
     my $domain = $r->dir_config('lonDefDomain');   \%form);
     my $prodir = $r->dir_config('lonUsersDir');          return OK;
     my $contact_name = &mt('LON-CAPA helpdesk');      }
   
 # ---------------------------------------- Get the information from login token  # split user logging in and "su"-user
   
     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},      ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
                                       $form{'serverid'});      $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
       $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||       $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});
         ($tmpinfo eq 'no_such_host')) {  
  &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);      my $role   = $r->dir_config('lonRole');
         return OK;      my $domain = $r->dir_config('lonDefDomain');
     } else {      my $prodir = $r->dir_config('lonUsersDir');
  my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},      my $contact_name = &mt('LON-CAPA helpdesk');
    $form{'serverid'});  
         if ( $reply ne 'ok' ) {  # ---------------------------------------- Get the information from login token
             &failed($r,'Session could not be opened.',\%form);  
     &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");      my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
     return OK;                                        $form{'serverid'});
  }  
     }      if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
           ($tmpinfo eq 'no_such_host')) {
     if (!&Apache::lonnet::domain($form{'udom'})) {   &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);          return OK;
         return OK;      } else {
     }   my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
      $form{'serverid'});
     my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);          if ( $reply ne 'ok' ) {
     if ($rolestr) {              &failed($r,'Session could not be opened.',\%form);
         $rolestr = &unescape($rolestr);      &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
     }      return OK;
     if ($symbstr) {   }
         $symbstr= &unescape($symbstr);      }
     }  
     if ($iptokenstr) {      if (!&Apache::lonnet::domain($form{'udom'})) {
         $iptokenstr = &unescape($iptokenstr);          &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
     }          return OK;
     if ($rolestr =~ /^role=/) {      }
         (undef,$form{'role'}) = split('=',$rolestr);  
     }      my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);
     if ($symbstr =~ /^symb=/) {       if ($rolestr) {
         (undef,$form{'symb'}) = split('=',$symbstr);          $rolestr = &unescape($rolestr);
     }      }
     if ($iptokenstr =~ /^iptoken=/) {      if ($symbstr) {
         (undef,$form{'iptoken'}) = split('=',$iptokenstr);          $symbstr= &unescape($symbstr);
     }      }
       if ($iptokenstr) {
     my $upass = &Apache::loncommon::des_decrypt($key,$form{'upass0'});          $iptokenstr = &unescape($iptokenstr);
       }
 # ---------------------------------------------------------------- Authenticate      if ($rolestr =~ /^role=/) {
           (undef,$form{'role'}) = split('=',$rolestr);
     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});      }
     my ($cancreate,$statustocreate) =      if ($symbstr =~ /^symb=/) {
         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});          (undef,$form{'symb'}) = split('=',$symbstr);
     my $defaultauth;      }
     if (ref($cancreate) eq 'ARRAY') {      if ($iptokenstr =~ /^iptoken=/) {
         if (grep(/^login$/,@{$cancreate})) {          (undef,$form{'iptoken'}) = split('=',$iptokenstr);
             $defaultauth = 1;      }
         }  
     }      my $upass = $ENV{HTTPS} ? $form{'upass0'}
     my $clientcancheckhost = 1;          : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,  
                                               $form{'udom'},$defaultauth,  # ---------------------------------------------------------------- Authenticate
                                               $clientcancheckhost);  
           my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
 # --------------------------------------------------------------------- Failed?      my ($cancreate,$statustocreate) =
           &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
     if ($authhost eq 'no_host') {      my $defaultauth;
  &failed($r,'Username and/or password could not be authenticated.',      if (ref($cancreate) eq 'ARRAY') {
  \%form);          if (grep(/^login$/,@{$cancreate})) {
         return OK;              $defaultauth = 1;
     } elsif ($authhost eq 'no_account_on_host') {          }
         if ($defaultauth) {      }
             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');      my $clientcancheckhost = 1;
             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {      my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
                 return OK;                                                $form{'udom'},$defaultauth,
             }                                                $clientcancheckhost);
             my $start_page =      
                 &Apache::loncommon::start_page('Create a user account in LON-CAPA',  # --------------------------------------------------------------------- Failed?
                                                '',{'no_inline_link'   => 1,});  
             my $lonhost = $r->dir_config('lonHostID');      if ($authhost eq 'no_host') {
             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};   &failed($r,'Username and/or password could not be authenticated.',
             my $contacts =    \%form);
                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',          return OK;
                                                         $form{'udom'},$origmail);      } elsif ($authhost eq 'no_account_on_host') {
             my ($contact_email) = split(',',$contacts);           if ($defaultauth) {
             my $output =               my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},              unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
                                                        $domdesc,'',$lonhost,                  return OK;
                                                        $contact_email,$contact_name,              }
                                                        undef,$statustocreate);              my $start_page =
             &Apache::loncommon::content_type($r,'text/html');                  &Apache::loncommon::start_page('Create a user account in LON-CAPA');
             $r->send_http_header;              my $lonhost = $r->dir_config('lonHostID');
             &Apache::createaccount::print_header($r,$start_page);              my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
             $r->print('<h3>'.&mt('Account creation').'</h3>'.              my $contacts =
                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.                  &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                       $output.&Apache::loncommon::end_page());                                                          $form{'udom'},$origmail);
             return OK;              my ($contact_email) = split(',',$contacts);
         } else {              my $output =
             &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);                  &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
             return OK;                                                         $domdesc,'',$lonhost,
         }                                                         $contact_email,$contact_name,
     }                                                         undef,$statustocreate);
               &Apache::loncommon::content_type($r,'text/html');
     if (($firsturl eq '') ||               $r->send_http_header;
  ($firsturl=~/^\/adm\/(logout|remote)/)) {              &Apache::createaccount::print_header($r,$start_page);
  $firsturl='/adm/roles';              $r->print('<h3>'.&mt('Account creation').'</h3>'.
     }                        &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
                         $output.&Apache::loncommon::end_page());
     my $hosthere;              return OK;
     if ($form{'iptoken'}) {          } else {
         my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});              &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);
         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});              return OK;
         if (($sessiondata{'domain'} eq $form{'udom'}) &&          }
             ($sessiondata{'username'} eq $form{'uname'})) {      }
             $hosthere = 1;  
         }      if (($firsturl eq '') ||
     }   ($firsturl=~/^\/adm\/(logout|remote)/)) {
    $firsturl='/adm/roles';
 # --------------------------------- Are we attempting to login as somebody else?      }
     if ($form{'suname'}) {  
         my ($suname,$sudom,$sudomref);      my $hosthere;
         $suname = $form{'suname'};      if ($form{'iptoken'}) {
         $sudom = $form{'udom'};          my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
         if ($form{'sudom'}) {          my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
             unless ($sudom eq $form{'sudom'}) {          if (($sessiondata{'domain'} eq $form{'udom'}) &&
                 if (&Apache::lonnet::domain($form{'sudom'})) {              ($sessiondata{'username'} eq $form{'uname'})) {
                     $sudomref = [$form{'sudom'}];              $hosthere = 1;
                     $sudom = $form{'sudom'};          }
                 }      }
             }  
         }  # --------------------------------- Are we attempting to login as somebody else?
 # ------------ see if the original user has enough privileges to pull this stunt      if ($form{'suname'}) {
  if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {  # ------------ see if the original user has enough privileges to pull this stunt
 # ---------------------------------------------------- see if the su-user exists   if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
     unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {  # ---------------------------------------------------- see if the su-user exists
 # ------------------------------ see if the su-user is not too highly privileged      unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
  if (&Apache::lonnet::privileged($suname,$sudom)) {   eq 'no_host') {
                     &Apache::lonnet::logthis('Attempted switch user to privileged user');   &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
                 } else {  # ------------------------------ see if the su-user is not too highly privileged
                     my $noprivswitch;   unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
 #  # -------------------------------------------------------- actually switch users
 # su-user's home server and user's home server must have one of:      &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
 # (a) same domain   ' logging in as '.$form{'suname'});
 # (b) same primary library server for the two domains      $form{'uname'}=$form{'suname'};
 # (c) same "internet domain" for primary library server(s) for home servers' domains   } else {
 #      &Apache::lonnet::logthis('Attempted switch user to privileged user');
                     my $suprim = &Apache::lonnet::domain($sudom,'primary');   }
                     my $suintdom = &Apache::lonnet::internet_dom($suprim);      }
                     unless ($sudom eq $form{'udom'}) {   } else {
                         my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');      &Apache::lonnet::logthis('Non-privileged user attempting switch user');
                         my $uintdom = &Apache::lonnet::internet_dom($uprim);   }
                         unless ($suprim eq $uprim) {      }
                             unless ($suintdom eq $uintdom) {  
                                 &Apache::lonnet::logthis('Attempted switch user '      my ($is_balancer,$otherserver);
                                    .'to user with different "internet domain".');  
                                 $noprivswitch = 1;      unless ($hosthere) {
                             }          ($is_balancer,$otherserver) =
                         }              &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
                     }          if ($is_balancer) {
               if ($otherserver eq '') {
                     unless ($noprivswitch) {                  my $lowest_load;
 #                  ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
 # server where log-in occurs must have same "internet domain" as su-user's home                  if ($lowest_load > 100) {
 # server                      $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});
 #                  }
                         my $lonhost = $r->dir_config('lonHostID');              }
                         my $hostintdom = &Apache::lonnet::internet_dom($lonhost);              if ($otherserver ne '') {
                         if ($hostintdom ne $suintdom) {                  my @hosts = &Apache::lonnet::current_machine_ids();
                             &Apache::lonnet::logthis('Attempted switch user on a '                  if (grep(/^\Q$otherserver\E$/,@hosts)) {
                                 .'server with a different "internet domain".');                      $hosthere = $otherserver;
                         } else {                  }
               }
 # -------------------------------------------------------- actually switch users          }
       }
     &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.  
  $form{'udom'}.' logging in as '.$suname.':'.$sudom);      if (($is_balancer) && (!$hosthere)) {
     $form{'uname'}=$suname;          if ($otherserver) {
                             if ($form{'udom'} ne $sudom) {              &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                                 $form{'udom'}=$sudom;                       \%form);
                             }              my $switchto = '/adm/switchserver?otherserver='.$otherserver;
                         }              if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
                     }                  $switchto .= '&origurl='.$firsturl;
  }              }
     }              if ($form{'role'}) {
  } else {                  $switchto .= '&role='.$form{'role'};
     &Apache::lonnet::logthis('Non-privileged user attempting switch user');              }
  }              if ($form{'symb'}) {
     }                  $switchto .= '&symb='.$form{'symb'};
               }
     my ($is_balancer,$otherserver);              $r->internal_redirect($switchto);
           } else {
     unless ($hosthere) {              $r->print(&noswitch());
         ($is_balancer,$otherserver) =          }
             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');          return OK;
         if ($is_balancer) {      } else {
             if ($otherserver eq '') {          if (!&check_can_host($r,\%form,$authhost)) {
                 my $lowest_load;              my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
                 ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});              if ($otherserver) {
                 if ($lowest_load > 100) {                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                     $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});                           \%form);
                 }                  my $switchto = '/adm/switchserver?otherserver='.$otherserver;
             }                  if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
             if ($otherserver ne '') {                      $switchto .= '&origurl='.$firsturl;
                 my @hosts = &Apache::lonnet::current_machine_ids();                  }
                 if (grep(/^\Q$otherserver\E$/,@hosts)) {                  if ($form{'role'}) {
                     $hosthere = $otherserver;                      $switchto .= '&role='.$form{'role'};
                 }                  }
             }                  if ($form{'symb'}) {
         }                      $switchto .= '&symb='.$form{'symb'};
     }                  }
                   $r->internal_redirect($switchto);
     if (($is_balancer) && (!$hosthere)) {              } else {
         if ($otherserver) {                  $r->print(&noswitch());
             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,              }
                      \%form);              return OK;
             my $switchto = '/adm/switchserver?otherserver='.$otherserver;          }
             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {  
                 $switchto .= '&origurl='.$firsturl;  # ------------------------------------------------------- Do the load balancing
             }  
             if ($form{'role'}) {  # ---------------------------------------------------------- Determine own load
                 $switchto .= '&role='.$form{'role'};          my $loadlim = $r->dir_config('lonLoadLim');
             }          my $loadavg;
             if ($form{'symb'}) {          {
                 $switchto .= '&symb='.$form{'symb'};              my $loadfile=Apache::File->new('/proc/loadavg');
             }              $loadavg=<$loadfile>;
             $r->internal_redirect($switchto);          }
         } else {          $loadavg =~ s/\s.*//g;
             $r->print(&noswitch());          my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
         }          my $userloadpercent=&Apache::lonnet::userload();
         return OK;  
     } else {  # ---------------------------------------------------------- Are we overloaded?
         if (!&check_can_host($r,\%form,$authhost)) {          if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});              my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
             if ($otherserver) {              if (!$unloaded) {
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,                  ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
                          \%form);              }
                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;              if ($unloaded) {
                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
                     $switchto .= '&origurl='.$firsturl;                           undef,\%form);
                 }                  $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
                 if ($form{'role'}) {                  return OK;
                     $switchto .= '&role='.$form{'role'};              }
                 }          }
                 if ($form{'symb'}) {          if (($is_balancer) && ($hosthere)) {
                     $switchto .= '&symb='.$form{'symb'};              $form{'noloadbalance'} = $hosthere;
                 }          }
                 $r->internal_redirect($switchto);          &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
             } else {                   \%form);
                 $r->print(&noswitch());          return OK;
             }      }
             return OK;  }
         }  
   sub check_can_host {
 # ------------------------------------------------------- Do the load balancing      my ($r,$form,$authhost,$domdesc) = @_;
       return unless (ref($form) eq 'HASH');
 # ---------------------------------------------------------- Determine own load      my $canhost = 1;
         my $loadlim = $r->dir_config('lonLoadLim');      my $lonhost = $r->dir_config('lonHostID');
         my $loadavg;      my $udom = $form->{'udom'};
         {      my @intdoms;
             my $loadfile=Apache::File->new('/proc/loadavg');      my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
             $loadavg=<$loadfile>;      if (ref($internet_names) eq 'ARRAY') {
         }          @intdoms = @{$internet_names};
         $loadavg =~ s/\s.*//g;      }
         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);      my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
         my $userloadpercent=&Apache::lonnet::userload();      my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
       unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
 # ---------------------------------------------------------- Are we overloaded?          my $machine_dom = &Apache::lonnet::host_domain($lonhost);
         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {          my $hostname = &Apache::lonnet::hostname($lonhost);
             my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});          my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
             if (!$unloaded) {          my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});          my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
             }          my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
             if ($unloaded) {          my $loncaparev;
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',          if ($authhost eq 'no_account_on_host') {
                          undef,\%form);              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);          } else {
                 return OK;              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
             }          }
         }          $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
         if (($is_balancer) && ($hosthere)) {                                                       $udomdefaults{'remotesessions'},
             $form{'noloadbalance'} = $hosthere;                                                       $defdomdefaults{'hostedsessions'});
         }      }
         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,      unless ($canhost) {
                  \%form);          if ($authhost eq 'no_account_on_host') {
         return OK;              my $checkloginvia = 1;
     }              my ($login_host,$hostname) =
 }                  &Apache::lonnet::choose_server($udom,$checkloginvia);
               &Apache::loncommon::content_type($r,'text/html');
 sub check_can_host {              $r->send_http_header;
     my ($r,$form,$authhost,$domdesc) = @_;              if ($login_host ne '') {
     return unless (ref($form) eq 'HASH');                  my $protocol = $Apache::lonnet::protocol{$login_host};
     my $canhost = 1;                  $protocol = 'http' if ($protocol ne 'https');
     my $lonhost = $r->dir_config('lonHostID');                  my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
     my $udom = $form->{'udom'};                  $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
     my @intdoms;                            '<h3>'.&mt('Account creation').'</h3>'.
     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
     if (ref($internet_names) eq 'ARRAY') {                            '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
         @intdoms = @{$internet_names};                            '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
     }                            &Apache::loncommon::end_page());
     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');              } else {
     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);                  $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {                            '<h3>'.&mt('Account creation unavailable').'</h3>'.
         my $machine_dom = &Apache::lonnet::host_domain($lonhost);                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
         my $hostname = &Apache::lonnet::hostname($lonhost);                            '<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>'.
         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);                            &Apache::loncommon::end_page());
         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);              }
         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);          } else {
         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);              &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
         my $loncaparev;                       $form);
         if ($authhost eq 'no_account_on_host') {              my ($otherserver) = &Apache::lonnet::choose_server($udom);
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);              $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
         } else {          }
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);      }
         }      return $canhost;
         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,  }
                                                      $udomdefaults{'remotesessions'},  
                                                      $defdomdefaults{'hostedsessions'});  sub noswitch {
     }      my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
     unless ($canhost) {                   '<h3>'.&mt('Session unavailable').'</h3>'.
         if ($authhost eq 'no_account_on_host') {                   &mt('This LON-CAPA server is unable to host your session.').'<br />'.
             my $checkloginvia = 1;                   '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
             my ($login_host,$hostname) =                    &Apache::loncommon::end_page();
                 &Apache::lonnet::choose_server($udom,$checkloginvia);      return $result;
             &Apache::loncommon::content_type($r,'text/html');  }
             $r->send_http_header;  
             if ($login_host ne '') {  sub loginhelpdisplay {
                 my $protocol = $Apache::lonnet::protocol{$login_host};      my ($authdomain) = @_;
                 $protocol = 'http' if ($protocol ne 'https');      my $login_help = 1;
                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';      my $lang = &Apache::lonlocal::current_language();
                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').      if ($login_help) {
                           '<h3>'.&mt('Account creation').'</h3>'.          my $dom = $authdomain;
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.          if ($dom eq '') {
                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.              $dom = &Apache::lonnet::default_login_domain();
                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').          }
                           &Apache::loncommon::end_page());          my %domconfhash = &Apache::loncommon::get_domainconf($dom);
             } else {          my $loginhelp_url;
                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').          if ($lang) {
                           '<h3>'.&mt('Account creation unavailable').'</h3>'.              $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.              if ($loginhelp_url ne '') {
                           '<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>'.                  return $loginhelp_url;
                           &Apache::loncommon::end_page());              }
             }          }
         } else {          $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,          if ($loginhelp_url ne '') {
                      $form);              return $loginhelp_url;
             my ($otherserver) = &Apache::lonnet::choose_server($udom);          } else {
             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);              return '/adm/loginproblems.html';
         }          }
     }      }
     return $canhost;      return;
 }  }
   
 sub noswitch {  1;
     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').  __END__
                  '<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.17  
changed lines
  Added in v.1.142


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