File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.158.2.13.2.7: download - view: text, annotated - select for diffs
Tue Aug 30 12:28:08 2022 UTC (21 months ago) by raeburn
Branches: version_2_11_4_msu
- For 2.11.4 (modified)
  Include changes in 1.202, 1.203

    1: # The LearningOnline Network
    2: # Login Screen
    3: #
    4: # $Id: lonlogin.pm,v 1.158.2.13.2.7 2022/08/30 12:28:08 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonlogin;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::File ();
   34: use Apache::lonnet;
   35: use Apache::loncommon();
   36: use Apache::lonauth();
   37: use Apache::lonlocal;
   38: use Apache::migrateuser();
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA qw(:DEFAULT :match);
   41: use URI::Escape;
   42: use HTML::Entities();
   43: use CGI::Cookie();
   44:  
   45: sub handler {
   46:     my $r = shift;
   47: 
   48:     &Apache::loncommon::get_unprocessed_cgi
   49: 	(join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},
   50: 	      $ENV{'REDIRECT_QUERY_STRING'}),
   51: 	 ['interface','username','domain','firsturl','localpath','localres',
   52: 	  'token','role','symb','iptoken','btoken','ltoken','ttoken','linkkey',
   53:           'saml','sso','retry']);
   54: 
   55: # -- check if they are a migrating user
   56:     if (defined($env{'form.token'})) {
   57: 	return &Apache::migrateuser::handler($r);
   58:     }
   59: 
   60:     my $lonhost = $r->dir_config('lonHostID');
   61:     if ($env{'form.ttoken'}) {
   62:         my %info = &Apache::lonnet::tmpget($env{'form.ttoken'});
   63:         &Apache::lonnet::tmpdel($env{'form.ttoken'});
   64:         if ($info{'origurl'}) {
   65:             $env{'form.firsturl'} = $info{'origurl'};
   66:         }
   67:         if ($info{'ltoken'}) {
   68:             $env{'form.ltoken'} = $info{'ltoken'};
   69:         } elsif ($info{'linkprot'}) {
   70:             $env{'form.linkprot'} = $info{'linkprot'};
   71:             foreach my $item ('linkprotuser','linkprotexit') {
   72:                 if ($info{$item} ne '') {
   73:                     $env{'form.'.$item} = $info{$item};
   74:                 }
   75:             }
   76:         } elsif ($info{'linkkey'} ne '') {
   77:             $env{'form.linkkey'} = $info{'linkkey'};
   78:         }
   79:     } elsif (($env{'form.sso'}) || ($env{'form.retry'})) {
   80:         my $infotoken;
   81:         if ($env{'form.sso'}) {
   82:             $infotoken = $env{'form.sso'};
   83:         } else {
   84:             $infotoken = $env{'form.retry'};
   85:         }
   86:         my $data = &Apache::lonnet::reply('tmpget:'.$infotoken,$lonhost);
   87:         unless (($data=~/^error/) || ($data eq 'con_lost') ||
   88:                 ($data eq 'no_such_host')) {
   89:             my %info = &decode_token($data);
   90:             foreach my $item (keys(%info)) {
   91:                 $env{'form.'.$item} = $info{$item};
   92:             }
   93:             &Apache::lonnet::tmpdel($infotoken);
   94:         }
   95:     } else {
   96:         if (!defined($env{'form.firsturl'})) {
   97:             &Apache::lonacc::get_posted_cgi($r,['firsturl']);
   98:         }
   99:         if (!defined($env{'form.firsturl'})) {
  100:             if ($ENV{'REDIRECT_URL'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) {
  101:                 $env{'form.firsturl'} = $ENV{'REDIRECT_URL'};
  102:             }
  103:         }
  104:         if (($env{'form.firsturl'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) &&
  105:             (!$env{'form.ltoken'}) && (!$env{'form.linkprot'}) && (!$env{'form.linkkey'})) {
  106:             &Apache::lonacc::get_posted_cgi($r,['linkkey']);
  107:         }
  108:         if ($env{'form.firsturl'} eq '/adm/logout') {
  109:             delete($env{'form.firsturl'});
  110:         }
  111:     }
  112: 
  113: # For "public user" - remove any exising "public" cookie, as user really wants to log-in
  114:     my ($handle,$lonidsdir,$expirepub,$userdom);
  115:     $lonidsdir=$r->dir_config('lonIDsDir');
  116:     unless ($r->header_only) {
  117:         $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);
  118:         if ($handle ne '') {
  119:             if ($handle=~/^publicuser\_/) {
  120:                 unlink($r->dir_config('lonIDsDir')."/$handle.id");
  121:                 undef($handle);
  122:                 undef($userdom);
  123:                 $expirepub = 1;
  124:             }
  125:         }
  126:     }
  127: 
  128:     &Apache::loncommon::no_cache($r);
  129:     &Apache::lonlocal::get_language_handle($r);
  130:     &Apache::loncommon::content_type($r,'text/html');
  131:     if ($expirepub) {
  132:         my $c = new CGI::Cookie(-name    => 'lonPubID',
  133:                                 -value   => '',
  134:                                 -expires => '-10y',);
  135:         $r->header_out('Set-cookie' => $c);
  136:     } elsif (($handle eq '') && ($userdom ne '')) {
  137:         my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  138:         foreach my $name (keys(%cookies)) {
  139:             next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
  140:             my $c = new CGI::Cookie(-name    => $name,
  141:                                     -value   => '',
  142:                                     -expires => '-10y',);
  143:             $r->headers_out->add('Set-cookie' => $c);
  144:         }
  145:     }
  146:     $r->send_http_header;
  147:     return OK if $r->header_only;
  148: 
  149: 
  150: # Are we re-routing?
  151:     my $londocroot = $r->dir_config('lonDocRoot'); 
  152:     if (-e "$londocroot/lon-status/reroute.txt") {
  153: 	&Apache::lonauth::reroute($r);
  154: 	return OK;
  155:     }
  156: 
  157: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  158: 
  159:     my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
  160:     if ($found_server) {
  161:         my $hostname = &Apache::lonnet::hostname($found_server);
  162:         if ($hostname ne '') {
  163:             my $protocol = $Apache::lonnet::protocol{$found_server};
  164:             $protocol = 'http' if ($protocol ne 'https');
  165:             my $dest = '/adm/roles';
  166:             if ($env{'form.firsturl'} ne '') {
  167:                 $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&');
  168:             }
  169:             my %info = (
  170:                          balcookie => $lonhost.':'.$balancer_cookie,
  171:                        );
  172:             if ($env{'form.role'}) {
  173:                 $info{'role'} = $env{'form.role'};
  174:             }
  175:             if ($env{'form.symb'}) {
  176:                 $info{'symb'} = $env{'form.symb'};
  177:             }
  178:             my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server);
  179:             unless (($balancer_token eq 'con_lost') || ($balancer_token eq 'refused') ||
  180:                     ($balancer_token eq 'unknown_cmd') || ($balancer_token eq 'no_such_host')) {
  181:                 $dest .=  (($dest=~/\?/)?'&amp;':'?') . 'btoken='.$balancer_token;
  182:             }
  183:             if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  184:                 my %link_info;
  185:                 if ($env{'form.ltoken'}) {
  186:                     $link_info{'ltoken'} = $env{'form.ltoken'};
  187:                 } elsif ($env{'form.linkprot'}) {
  188:                     $link_info{'linkprot'} = $env{'form.linkprot'};
  189:                     foreach my $item ('linkprotuser','linkprotexit') {
  190:                         if ($env{'form.'.$item} ne '') {
  191:                             $link_info{$item} = $env{'form.'.$item};
  192:                         }
  193:                     }
  194:                 } elsif ($env{'form.linkkey'} ne '') {
  195:                     $link_info{'linkkey'} = $env{'form.linkkey'};
  196:                 }
  197:                 if (keys(%link_info)) {
  198:                     $link_info{'origurl'} = $env{'form.firsturl'};
  199:                     my $token = &Apache::lonnet::tmpput(\%link_info,$found_server,'link');
  200:                     unless (($token eq 'con_lost') || ($token eq 'refused') ||
  201:                             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  202:                         $dest .=  (($dest=~/\?/)?'&amp;':'?') . 'ttoken='.$token;
  203:                     }
  204:                 }
  205:             }
  206:             unless ($found_server eq $lonhost) {
  207:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$found_server);
  208:                 $hostname = $alias if ($alias ne '');
  209:             }
  210:             my $url = $protocol.'://'.$hostname.$dest;
  211:             my $start_page =
  212:                 &Apache::loncommon::start_page('Switching Server ...',undef,
  213:                                                {'redirect'       => [0,$url],});
  214:             my $end_page   = &Apache::loncommon::end_page();
  215:             $r->print($start_page.$end_page);
  216:             return OK;
  217:         }
  218:     }
  219: 
  220: #
  221: # Check if a LON-CAPA load balancer sent user here because user's browser sent
  222: # it a balancer cookie for an active session on this server.
  223: #
  224: 
  225:     my $balcookie;
  226:     if ($env{'form.btoken'}) {
  227:         my %info = &Apache::lonnet::tmpget($env{'form.btoken'});
  228:         $balcookie = $info{'balcookie'};
  229:         &Apache::lonnet::tmpdel($env{'form.btoken'});
  230:         delete($env{'form.btoken'});
  231:     }
  232: 
  233: #
  234: # If browser sent an old cookie for which the session file had been removed
  235: # check if configuration for user's domain has a portal URL set.  If so
  236: # switch user's log-in to the portal.
  237: #
  238: 
  239:     if (($handle eq '') && ($userdom ne '')) {
  240:         my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);
  241:         if ($domdefaults{'portal_def'} =~ /^https?\:/) {
  242:             my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  243:                                           {'redirect' => [0,$domdefaults{'portal_def'}],});
  244:             my $end_page   = &Apache::loncommon::end_page();
  245:             $r->print($start_page.$end_page);
  246:             return OK;
  247:         }
  248:     }
  249: 
  250: # -------------------------------- Prevent users from attempting to login twice
  251:     if ($handle ne '') {
  252:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  253: 	my $start_page = 
  254: 	    &Apache::loncommon::start_page('Already logged in');
  255: 	my $end_page = 
  256: 	    &Apache::loncommon::end_page();
  257:         my $dest = '/adm/roles';
  258:         if ($env{'form.firsturl'} ne '') {
  259:             $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&');
  260:         }
  261:         if (($env{'form.ltoken'}) || ($env{'form.linkprot'})) {
  262:             my ($linkprot,$linkprotuser,$linkprotexit);
  263:             if ($env{'form.ltoken'}) {
  264:                 my %info = &Apache::lonnet::tmpget($env{'form.ltoken'});
  265:                 $linkprot = $info{'linkprot'};
  266:                 if ($info{'linkprotuser'} ne '') {
  267:                     $linkprotuser = $info{'linkprotuser'};
  268:                 }
  269:                 if ($info{'linkprotexit'} ne '') {
  270:                     $linkprotexit = $info{'linkprotexit'};
  271:                 }
  272:             } else {
  273:                 $linkprot = $env{'form.linkprot'};
  274:                 $linkprotuser = $env{'form.linkprotuser'};
  275:                 $linkprotexit = $env{'form.linkprotexit'};
  276:             }
  277:             if ($linkprot) {
  278:                 my ($linkprotector,$deeplink) = split(/:/,$linkprot,2);
  279:                 if (($deeplink =~ m{^/tiny/$match_domain/\w+$}) &&
  280:                     ($linkprotuser ne '') && ($linkprotuser ne $env{'user.name'}.':'.$env{'user.domain'})) {
  281:                     my $ip = &Apache::lonnet::get_requestor_ip();
  282:                     my %linkprotinfo = (
  283:                                           origurl => $deeplink,
  284:                                           linkprot => $linkprot,
  285:                                           linkprotuser => $linkprotuser,
  286:                                           linkprotexit => $linkprotexit,
  287:                                        );
  288:                     if ($env{'form.ltoken'}) {
  289:                         my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
  290:                     }
  291:                     &Apache::migrateuser::logout($r,$ip,$handle,undef,undef,\%linkprotinfo);
  292:                     return OK;
  293:                 }
  294:                 if ($env{'user.linkprotector'}) {
  295:                     my @protectors = split(/,/,$env{'user.linkprotector'});
  296:                     unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
  297:                         push(@protectors,$linkprotector);
  298:                         @protectors = sort { $a <=> $b } @protectors;
  299:                         &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
  300:                     }
  301:                 } else {
  302:                     &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
  303:                 }
  304:                 if ($env{'user.linkproturi'}) {
  305:                     my @proturis = split(/,/,$env{'user.linkproturi'});
  306:                     unless (grep(/^\Q$deeplink\E$/,@proturis)) {
  307:                         push(@proturis,$deeplink);
  308:                         @proturis = sort @proturis;
  309:                         &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
  310:                     }
  311:                 } else {
  312:                     &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
  313:                 }
  314:             }
  315:         } elsif ($env{'form.linkkey'} ne '') {
  316:             if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  317:                 my $linkkey = $env{'form.linkkey'};
  318:                 if ($env{'user.deeplinkkey'}) {
  319:                     my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
  320:                     unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
  321:                         push(@linkkeys,$linkkey);
  322:                         &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
  323:                     }
  324:                 } else {
  325:                     &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
  326:                 }
  327:                 my $deeplink = $env{'form.firsturl'};
  328:                 if ($env{'user.keyedlinkuri'}) {
  329:                     my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
  330:                     unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
  331:                         push(@keyeduris,$deeplink);
  332:                         &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
  333:                     }
  334:                 } else {
  335:                     &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
  336:                 }
  337:             }
  338:         }
  339:         if ($env{'form.ltoken'}) {
  340:             my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
  341:         }
  342: 	$r->print(
  343:               $start_page
  344:              .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  345:              .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
  346:               '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'
  347:              .$end_page
  348:              );
  349:         return OK;
  350:     }
  351: 
  352: # ---------------------------------------------------- No valid token, continue
  353: 
  354: # ---------------------------- Not possible to really login to domain "public"
  355:     if ($env{'form.domain'} eq 'public') {
  356: 	$env{'form.domain'}='';
  357: 	$env{'form.username'}='';
  358:     }
  359: 
  360: # ------ Is this page requested because /adm/migrateuser detected an IP change?
  361:     my %sessiondata;
  362:     if ($env{'form.iptoken'}) {
  363:         %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});
  364:         unless ($sessiondata{'sessionserver'}) {
  365:             my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});
  366:             delete($env{'form.iptoken'});
  367:         }
  368:     }
  369: # ----------------------------------------------------------- Process Interface
  370:     $env{'form.interface'}=~s/\W//g;
  371: 
  372:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
  373:         &Apache::loncommon::decode_user_agent($r);
  374: 
  375:     my $iconpath= 
  376: 	&Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));
  377: 
  378:     my $domain = &Apache::lonnet::default_login_domain();
  379:     my $defdom = $domain;
  380:     if ($lonhost ne '') {
  381:         unless ($sessiondata{'sessionserver'}) {
  382:             my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie);
  383:             if ($redirect) {
  384:                 $r->print($redirect);
  385:                 return OK;
  386:             }
  387:         }
  388:     }
  389: 
  390:     if (($sessiondata{'domain'}) &&
  391:         (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) {
  392:         $domain=$sessiondata{'domain'};
  393:     } elsif (($env{'form.domain'}) && 
  394: 	(&Apache::lonnet::domain($env{'form.domain'},'description'))) {
  395: 	$domain=$env{'form.domain'};
  396:     }
  397: 
  398:     my $role    = $r->dir_config('lonRole');
  399:     my $loadlim = $r->dir_config('lonLoadLim');
  400:     my $uloadlim= $r->dir_config('lonUserLoadLim');
  401:     my $servadm = $r->dir_config('lonAdmEMail');
  402:     my $tabdir  = $r->dir_config('lonTabDir');
  403:     my $include = $r->dir_config('lonIncludes');
  404:     my $expire  = $r->dir_config('lonExpire');
  405:     my $version = $r->dir_config('lonVersion');
  406:     my $host_name = &Apache::lonnet::hostname($lonhost);
  407: 
  408: # --------------------------------------------- Default values for login fields
  409:     
  410:     my ($authusername,$authdomain);
  411:     if ($sessiondata{'username'}) {
  412:         $authusername=$sessiondata{'username'};
  413:     } else {
  414:         $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});
  415:         $authusername=($env{'form.username'}?$env{'form.username'}:'');
  416:     }
  417:     if ($sessiondata{'domain'}) {
  418:         $authdomain=$sessiondata{'domain'};
  419:     } else {
  420:         $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});
  421:         $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);
  422:     }
  423: 
  424: # ---------------------------------------------------------- Determine own load
  425:     my $loadavg;
  426:     {
  427: 	my $loadfile=Apache::File->new('/proc/loadavg');
  428: 	$loadavg=<$loadfile>;
  429:     }
  430:     $loadavg =~ s/\s.*//g;
  431: 
  432:     my ($loadpercent,$userloadpercent);
  433:     if ($loadlim) {
  434:         $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  435:     }
  436:     if ($uloadlim) {
  437:         $userloadpercent=&Apache::lonnet::userload();
  438:     }
  439: 
  440:     my $firsturl=
  441:     ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});
  442: 
  443: # ----------------------------------------------------------- Get announcements
  444:     my $announcements=&Apache::lonnet::getannounce();
  445: # -------------------------------------------------------- Set login parameters
  446: 
  447:     my @hexstr=('0','1','2','3','4','5','6','7',
  448:                 '8','9','a','b','c','d','e','f');
  449:     my $lkey='';
  450:     for (0..7) {
  451:         $lkey.=$hexstr[rand(15)];
  452:     }
  453: 
  454:     my $ukey='';
  455:     for (0..7) {
  456:         $ukey.=$hexstr[rand(15)];
  457:     }
  458: 
  459:     my $lextkey=hex($lkey);
  460:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
  461: 
  462:     my $uextkey=hex($ukey);
  463:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
  464: 
  465: # -------------------------------------------------------- Store away log token
  466:     my ($tokenextras,$tokentype,$linkprot_for_login);
  467:     my @names = ('role','symb','iptoken','ltoken','linkprotuser','linkprotexit','linkprot','linkkey');
  468:     foreach my $name (@names) {
  469:         if ($env{'form.'.$name} ne '') {
  470:             if ($name eq 'ltoken') {
  471:                 my %info = &Apache::lonnet::tmpget($env{'form.'.$name});
  472:                 if ($info{'linkprot'}) {
  473:                     $linkprot_for_login = $info{'linkprot'};
  474:                     $tokenextras .= '&linkprot='.&escape($info{'linkprot'});
  475:                     foreach my $item ('linkprotuser','linkprotexit') {
  476:                         if ($info{$item}) {
  477:                             $tokenextras .= '&'.$item.'='.&escape($info{$item});
  478:                         }
  479:                     }
  480:                     $tokentype = 'link';
  481:                     last;
  482:                 }
  483:             } else {
  484:                 $tokenextras .= '&'.$name.'='.&escape($env{'form.'.$name});
  485:                 if (($name eq 'linkkey') || ($name eq 'linkprot')) {
  486:                     if ((($env{'form.retry'}) || ($env{'form.sso'})) &&
  487:                         (!$env{'form.ltoken'}) && ($name eq 'linkprot')) {
  488:                         $linkprot_for_login = $env{'form.linkprot'};
  489:                     }
  490:                     $tokentype = 'link';
  491:                 }
  492:             }
  493:         }
  494:     }
  495:     if ($tokentype) {
  496:         $tokenextras .= ":$tokentype";
  497:     }
  498:     my $logtoken=Apache::lonnet::reply(
  499:        'tmpput:'.$ukey.$lkey.'&'.&escape($firsturl).$tokenextras,
  500:        $lonhost);
  501: 
  502: # -- If we cannot talk to ourselves, or hostID does not map to a hostname
  503: #    we are in serious trouble
  504: 
  505:     if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  506:         if ($logtoken eq 'no_such_host') {
  507:             &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');
  508:         }
  509:         if ($env{'form.ltoken'}) {
  510:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
  511:             delete($env{'form.ltoken'});
  512:         }
  513:         my $spares='';
  514:         my (@sparehosts,%spareservers);
  515:         my $sparesref = &Apache::lonnet::this_host_spares($defdom);
  516:         if (ref($sparesref) eq 'HASH') {
  517:             foreach my $key (keys(%{$sparesref})) {
  518:                 if (ref($sparesref->{$key}) eq 'ARRAY') {
  519:                     my @sorted = sort { &Apache::lonnet::hostname($a) cmp
  520:                                         &Apache::lonnet::hostname($b);
  521:                                       } @{$sparesref->{$key}};
  522:                     if (@sorted) {
  523:                         if ($key eq 'primary') {
  524:                             unshift(@sparehosts,@sorted);
  525:                         } elsif ($key eq 'default') {
  526:                             push(@sparehosts,@sorted);
  527:                         }
  528:                     }
  529:                 }
  530:             }
  531:         }
  532:         foreach my $hostid (@sparehosts) {
  533:             next if ($hostid eq $lonhost);
  534: 	    my $hostname = &Apache::lonnet::hostname($hostid);
  535: 	    next if (($hostname eq '') || ($spareservers{$hostname}));
  536:             $spareservers{$hostname} = 1;
  537:             my $protocol = $Apache::lonnet::protocol{$hostid};
  538:             $protocol = 'http' if ($protocol ne 'https');
  539:             $spares.='<br /><span style="font-size: larger;"><a href="'.$protocol.'://'.
  540:                 $hostname.
  541:                 '/adm/login?domain='.$authdomain.'">'.
  542:                 $hostname.'</a>'.
  543:                 ' '.&mt('(preferred)').'</span>'.$/;
  544:         }
  545:         if ($spares) {
  546:             $spares.= '<br />';
  547:         }
  548:         my %all_hostnames = &Apache::lonnet::all_hostnames();
  549:         foreach my $hostid (sort
  550: 		    {
  551: 			&Apache::lonnet::hostname($a) cmp
  552: 			    &Apache::lonnet::hostname($b);
  553: 		    }
  554: 		    keys(%all_hostnames)) {
  555:             next if ($hostid eq $lonhost);
  556:             my $hostname = &Apache::lonnet::hostname($hostid);
  557:             next if (($hostname eq '') || ($spareservers{$hostname}));
  558:             $spareservers{$hostname} = 1;
  559:             my $protocol = $Apache::lonnet::protocol{$hostid};
  560:             $protocol = 'http' if ($protocol ne 'https');
  561:             $spares.='<br /><a href="'.$protocol.'://'.
  562: 	             $hostname.
  563: 	             '/adm/login?domain='.$authdomain.'">'.
  564: 	             $hostname.'</a>';
  565:          }
  566:          $r->print(
  567:    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  568:   .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
  569:   .'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'
  570:   .&mt('The LearningOnline Network with CAPA')
  571:   .'</title></head>'
  572:   .'<body bgcolor="#FFFFFF">'
  573:   .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
  574:   .'<img src="/adm/lonKaputt/lonlogo_broken.gif" alt="broken icon" align="right" />'
  575:   .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
  576:         if ($spares) {
  577:             $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
  578:                      .'</p>'
  579:                      .$spares);
  580:         }
  581:         $r->print('</body>'
  582:                  .'</html>'
  583:         );
  584:         return OK;
  585:     }
  586: 
  587: # ----------------------------------------------- Apparently we are in business
  588:     $servadm=~s/\,/\<br \/\>/g;
  589: 
  590: # ----------------------------------------------------------- Front page design
  591:     my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
  592:     my $font=&Apache::loncommon::designparm('login.font',$domain);
  593:     my $link=&Apache::loncommon::designparm('login.link',$domain);
  594:     my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
  595:     my $alink=&Apache::loncommon::designparm('login.alink',$domain);
  596:     my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
  597:     my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
  598:     my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
  599:     my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
  600:     my $logo=&Apache::loncommon::designparm('login.logo',$domain);
  601:     my $img=&Apache::loncommon::designparm('login.img',$domain);
  602:     my $domainlogo=&Apache::loncommon::domainlogo($domain);
  603:     my $showbanner = 1;
  604:     my $showmainlogo = 1;
  605:     if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
  606:         $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
  607:     }
  608:     if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
  609:         $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
  610:     }
  611:     my $showadminmail;
  612:     my @possdoms = &Apache::lonnet::current_machine_domains();
  613:     if (grep(/^\Q$domain\E$/,@possdoms)) {
  614:         $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
  615:     }
  616:     my $showcoursecat =
  617:         &Apache::loncommon::designparm('login.coursecatalog',$domain);
  618:     my $shownewuserlink = 
  619:         &Apache::loncommon::designparm('login.newuser',$domain);
  620:     my $showhelpdesk =
  621:         &Apache::loncommon::designparm('login.helpdesk',$domain);
  622:     my $now=time;
  623:     my $js = (<<ENDSCRIPT);
  624: 
  625: <script type="text/javascript" language="JavaScript">
  626: // <![CDATA[
  627: function send()
  628: {
  629: this.document.server.elements.uname.value
  630: =this.document.client.elements.uname.value;
  631: 
  632: this.document.server.elements.udom.value
  633: =this.document.client.elements.udom.value;
  634: 
  635: uextkey=this.document.client.elements.uextkey.value;
  636: lextkey=this.document.client.elements.lextkey.value;
  637: initkeys();
  638: 
  639: this.document.server.elements.upass0.value
  640:     =getCrypted(this.document.client.elements.upass$now.value);
  641: 
  642: this.document.client.elements.uname.value='';
  643: this.document.client.elements.upass$now.value='';
  644: 
  645: this.document.server.submit();
  646: return false;
  647: }
  648: 
  649: function enableInput() {
  650:     this.document.client.elements.upass$now.removeAttribute("readOnly");
  651:     this.document.client.elements.uname.removeAttribute("readOnly");
  652:     this.document.client.elements.udom.removeAttribute("readOnly");
  653:     return;
  654: }
  655: 
  656: // ]]>
  657: </script>
  658: 
  659: ENDSCRIPT
  660: 
  661:     my ($lonhost_in_use,@hosts,%defaultdomconf,$saml_prefix,$saml_landing,
  662:         $samlssotext,$samlnonsso,$samlssoimg,$samlssoalt,$samlssourl,$samltooltip,
  663:         $samlwindow);
  664:     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
  665:     @hosts = &Apache::lonnet::current_machine_ids();
  666:     $lonhost_in_use = $lonhost;
  667:     if (@hosts > 1) {
  668:         foreach my $hostid (@hosts) {
  669:             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
  670:                 $lonhost_in_use = $hostid;
  671:                 last;
  672:             }
  673:         }
  674:     }
  675:     $saml_prefix = $defdom.'.login.saml_';
  676:     if ($defaultdomconf{$saml_prefix.$lonhost_in_use}) {
  677:         $saml_landing = 1;
  678:         $samlssotext = $defaultdomconf{$saml_prefix.'text_'.$lonhost_in_use};
  679:         $samlnonsso = $defaultdomconf{$saml_prefix.'notsso_'.$lonhost_in_use};
  680:         $samlssoimg = $defaultdomconf{$saml_prefix.'img_'.$lonhost_in_use};
  681:         $samlssoalt = $defaultdomconf{$saml_prefix.'alt_'.$lonhost_in_use};
  682:         $samlssourl = $defaultdomconf{$saml_prefix.'url_'.$lonhost_in_use};
  683:         $samltooltip = $defaultdomconf{$saml_prefix.'title_'.$lonhost_in_use};
  684:         $samlwindow = $defaultdomconf{$saml_prefix.'window_'.$lonhost_in_use};
  685:     }
  686:     if ($saml_landing) {
  687:        if ($samlssotext eq '') {
  688:            $samlssotext = 'SSO Login';
  689:        }
  690:        if ($samlnonsso eq '') {
  691:            $samlnonsso = 'Non-SSO Login';
  692:        }
  693:        $js .= <<"ENDSAMLJS";
  694: 
  695: <script type="text/javascript">
  696: // <![CDATA[
  697: function toggleLClogin() {
  698:     if (document.getElementById('LC_standard_login')) {
  699:         if (document.getElementById('LC_standard_login').style.display == 'none') {
  700:             document.getElementById('LC_standard_login').style.display = 'inline-block';
  701:             if (document.getElementById('LC_login_text')) {
  702:                 document.getElementById('LC_login_text').innerHTML = '$samlnonsso';
  703:             }
  704:             if ( document.client.uname ) { document.client.uname.focus(); }
  705:             if (document.getElementById('LC_SSO_login')) {
  706:                 document.getElementById('LC_SSO_login').style.display = 'none';
  707:             }
  708:         } else {
  709:             document.getElementById('LC_standard_login').style.display = 'none';
  710:             if (document.getElementById('LC_login_text')) {
  711:                 document.getElementById('LC_login_text').innerHTML = '$samlssotext';
  712:             }
  713:             if (document.getElementById('LC_SSO_login')) {
  714:                 document.getElementById('LC_SSO_login').style.display = 'inline-block';
  715:             }
  716:         }
  717:     }
  718:     return;
  719: }
  720: 
  721: // ]]>
  722: </script>
  723: 
  724: ENDSAMLJS
  725:     }
  726: 
  727: # --------------------------------------------------- Print login screen header
  728: 
  729:     my %add_entries = (
  730: 	       bgcolor      => "$mainbg",
  731: 	       text         => "$font",
  732: 	       link         => "$link",
  733: 	       vlink        => "$vlink",
  734: 	       alink        => "$alink",
  735:                onload       => 'javascript:enableInput();',);
  736: 
  737:     my ($headextra,$headextra_exempt);
  738:     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
  739:     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
  740:     if ($headextra) {
  741:         my $omitextra;
  742:         if ($headextra_exempt ne '') {
  743:             my @exempt = split(',',$headextra_exempt);
  744:             my $ip = &Apache::lonnet::get_requestor_ip();
  745:             if (grep(/^\Q$ip\E$/,@exempt)) {
  746:                 $omitextra = 1;
  747:             }
  748:         }
  749:         unless ($omitextra) {
  750:             my $confname = $defdom.'-domainconfig';
  751:             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
  752:                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
  753:                 unless ($extra eq '-1') {
  754:                     $js .= "\n".$extra."\n";
  755:                 }
  756:             }
  757:         }
  758:     }
  759: 
  760:     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
  761: 			       { 'redirect'       => [$expire,'/adm/roles'], 
  762: 				 'add_entries' => \%add_entries,
  763: 				 'only_body'   => 1,}));
  764: 
  765: # ----------------------------------------------------------------------- Texts
  766: 
  767:     my %lt=&Apache::lonlocal::texthash(
  768:           'un'       => 'Username',
  769:           'pw'       => 'Password',
  770:           'dom'      => 'Domain',
  771:           'perc'     => 'percent',
  772:           'load'     => 'Server Load',
  773:           'userload' => 'User Load',
  774:           'catalog'  => 'Course/Community Catalog',
  775:           'log'      => 'Log in',
  776:           'help'     => 'Log-in Help',
  777:           'serv'     => 'Server',
  778:           'servadm'  => 'Server Administration',
  779:           'helpdesk' => 'Contact Helpdesk',
  780:           'forgotpw' => 'Forgot password?',
  781:           'newuser'  => 'New User?',
  782:           'change'   => 'Change?',
  783:        );
  784: # -------------------------------------------------- Change password field name
  785: 
  786:     my $forgotpw = &forgotpwdisplay(%lt);
  787:     $forgotpw .= '<br />' if $forgotpw;
  788:     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
  789:     if ($loginhelp) {
  790:         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
  791:     }
  792: 
  793: # ---------------------------------------------------- Serve out DES JavaScript
  794:     {
  795:     my $jsh=Apache::File->new($include."/londes.js");
  796:     $r->print(<$jsh>);
  797:     }
  798: # ---------------------------------------------------------- Serve rest of page
  799: 
  800:     $r->print(
  801:     '<div class="LC_Box"'
  802:    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
  803: );
  804: 
  805:     $r->print(<<ENDSERVERFORM);
  806: <form name="server" action="/adm/authenticate" method="post" target="_top">
  807:    <input type="hidden" name="logtoken" value="$logtoken" />
  808:    <input type="hidden" name="serverid" value="$lonhost" />
  809:    <input type="hidden" name="uname" value="" />
  810:    <input type="hidden" name="upass0" value="" />
  811:    <input type="hidden" name="udom" value="" />
  812:    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
  813:    <input type="hidden" name="localres" value="$env{'form.localres'}" />
  814:   </form>
  815: ENDSERVERFORM
  816:     my $coursecatalog;
  817:     if (($showcoursecat eq '') || ($showcoursecat)) {
  818:         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
  819:     }
  820:     my $newuserlink;
  821:     if ($shownewuserlink) {
  822:         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  823:     }
  824:     my $logintitle =
  825:         '<h2 class="LC_hcell"'
  826:        .' style="background:'.$loginbox_header_bgcol.';'
  827:        .' color:'.$loginbox_header_textcol.'">'
  828:        .$lt{'log'}
  829:        .'</h2>';
  830: 
  831:     my $noscript_warning='<noscript><span class="LC_warning"><b>'
  832:                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
  833:                         .'</b></span></noscript>';
  834:     my $helpdeskscript;
  835:     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
  836:                                        $authdomain,\$helpdeskscript,
  837:                                        $showhelpdesk,\@possdoms);
  838: 
  839:     my $mobileargs;
  840:     if ($clientmobile) {
  841:         $mobileargs = 'autocapitalize="off" autocorrect="off"'; 
  842:     }
  843:     my $loginform=(<<LFORM);
  844: <form name="client" action="" onsubmit="return(send())" id="lclogin">
  845:   <input type="hidden" name="lextkey" value="$lextkey" />
  846:   <input type="hidden" name="uextkey" value="$uextkey" />
  847:   <b><label for="uname">$lt{'un'}</label>:</b><br />
  848:   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
  849:   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
  850:   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
  851:   <b><label for="udom">$lt{'dom'}</label>:</b><br />
  852:   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
  853:   <input type="submit" value="$lt{'log'}" />
  854: </form>
  855: LFORM
  856: 
  857:     if ($showbanner) {
  858:         my $alttext = &Apache::loncommon::designparm('login.alttext_img',$domain);
  859:         if ($alttext eq '') {
  860:             $alttext = 'The Learning Online Network with CAPA';
  861:         }
  862:         $r->print(<<HEADER);
  863: <!-- The LON-CAPA Header -->
  864: <div style="background:$pgbg;margin:0;width:100%;">
  865:   <img src="$img" border="0" alt="$alttext" class="LC_maxwidth" id="lcloginbanner" />
  866: </div>
  867: HEADER
  868:     }
  869: 
  870:     my $stdauthformstyle = 'inline-block';
  871:     my $ssoauthstyle = 'none';
  872:     my $sso_onclick;
  873:     my $logintype;
  874:     $r->print('<div style="float:left;margin-top:0;">');
  875:     if ($saml_landing) {
  876:         $ssoauthstyle = 'inline-block';
  877:         $stdauthformstyle = 'none';
  878:         $logintype = $samlssotext;
  879:         my $ssologin = '/adm/sso';
  880:         if ($samlssourl  ne '') {
  881:             $ssologin = $samlssourl;
  882:         }
  883:         my $ssologin_for_js = &js_escape($ssologin);
  884:         my $querystr_for_js;
  885:         if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  886:             my $querystring;
  887:             if ($env{'form.firsturl'} ne '') {
  888:                 $querystring = 'origurl=';
  889:                 if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
  890:                     $querystring .= &uri_escape_utf8($env{'form.firsturl'});
  891:                 } else {
  892:                     $querystring .= &uri_escape($env{'form.firsturl'});
  893:                 }
  894:                 $querystring = &HTML::Entities::encode($querystring,"'");
  895:             }
  896:             if ($env{'form.ltoken'} ne '') {
  897:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'ltoken='.
  898:                                   &HTML::Entities::encode(&uri_escape($env{'form.ltoken'}));
  899:             } elsif ($env{'form.linkkey'}) {
  900:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'linkkey='.
  901:                                   &HTML::Entities::encode(&uri_escape($env{'form.linkkey'}));
  902:             }
  903:             if ($querystring ne '') {
  904:                 $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . $querystring;
  905:                 $querystr_for_js = &js_escape($querystring);
  906:             }
  907:         } elsif ($logtoken ne '') {
  908:             $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . 'logtoken='.$logtoken;
  909:             $querystr_for_js = &js_escape('logtoken='.$logtoken);
  910:         }
  911:         my $ssohref;
  912:         if ($samlwindow) {
  913:             $sso_onclick = <<"ENDJS";
  914: if (document.getElementById('LC_sso_login_link')) {
  915:     var ssoelem = document.getElementById('LC_sso_login_link')
  916:     ssoelem.addEventListener('click',samlWinFunction,false);
  917:     var windows = {};
  918:     function samlWinFunction(evt) {
  919:         evt.preventDefault();
  920:         var url = '$ssologin_for_js';
  921:         var name = 'lcssowin';
  922:         var querystr = '$querystr_for_js';
  923:         if (querystr) {
  924:             url += '?'+querystr+'&lcssowin=1';
  925:         } else {
  926:             url += '?lcssowin=1';
  927:         }
  928:         if ((typeof windows[name] !== 'undefined') && (!windows[name].closed)) {
  929:             windows[name].close();
  930:         }
  931:         windows[name]=window.open(url,name,'width=350,height=600');
  932:         windows[name].focus();
  933:         return false;
  934:     }
  935: }
  936: ENDJS
  937:         }
  938:         if ($samlssoimg ne '') {
  939:             $ssohref = '<a href="'.$ssologin.'" title="'.$samltooltip.'" id="LC_sso_login_link">'.
  940:                        '<img src="'.$samlssoimg.'" alt="'.$samlssoalt.'" id="lcssobutton" /></a>';
  941:         } else {
  942:             $ssohref = '<a href="'.$ssologin.'" id="LC_sso_login_link">'.$samlssotext.'</a>';
  943:         }
  944:         if (($env{'form.saml'} eq 'no') ||
  945:             (($env{'form.username'} ne '') && ($env{'form.domain'} ne ''))) {
  946:             $ssoauthstyle = 'none';
  947:             $stdauthformstyle = 'inline-block';
  948:             $logintype = $samlnonsso;
  949:         }
  950:         $r->print(<<ENDSAML);
  951: <p>
  952: Log-in type:
  953: <span style="font-weight:bold" id="LC_login_text">$logintype</span><br />
  954: <span><a href="javascript:toggleLClogin();" style="color:#000000">$lt{'change'}</a></span>
  955: </p>
  956: <div style="display:$ssoauthstyle" id="LC_SSO_login">
  957: <div class="LC_Box" style="padding-top: 10px;">
  958: $ssohref
  959: $noscript_warning
  960: </div>
  961: <div class="LC_Box" style="padding-top: 10px;">
  962: $loginhelp
  963: $contactblock
  964: $coursecatalog
  965: </div>
  966: </div>
  967: ENDSAML
  968:     } else {
  969:         if ($env{'form.ltoken'}) {
  970:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
  971:             delete($env{'form.ltoken'});
  972:         }
  973:     }
  974:     my $in_frame_js;
  975:     if ($linkprot_for_login) {
  976:         my ($linkprotector,$linkproturi) = split(/:/,$linkprot_for_login,2);
  977:         if (($linkprotector =~ /^\d+(c|d)$/) && ($linkproturi =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$})) {
  978:             my $set_target;
  979:             if (($env{'form.retry'}) || ($env{'form.sso'})) {
  980:                 if ($linkproturi eq $env{'form.firsturl'}) {
  981:                     $set_target = "    document.server.target = '_self';";
  982:                 }
  983:             } else {
  984:                 $set_target = <<ENDTARG;
  985:     var linkproturi = '$linkproturi';
  986:     var path = document.location.pathname.replace( new RegExp('^/adm/launch'),'');
  987:     if (linkproturi == path) {
  988:         document.server.target = '_self';
  989:     }
  990: ENDTARG
  991:             }
  992:             $in_frame_js = <<ENDJS;
  993: <script type="text/javascript">
  994: // <![CDATA[
  995: if ((window.self !== window.top) && (document.server.target != '_self')) {
  996:     $set_target
  997:     $sso_onclick
  998: }
  999: // ]]>
 1000: </script>
 1001: ENDJS
 1002:         }
 1003:     } elsif ($samlwindow) {
 1004:         $in_frame_js = <<ENDJS;
 1005: <script type="text/javascript">
 1006: // <![CDATA[
 1007: if ((window.self !== window.top) && (document.server.target != '_self')) {
 1008:     $sso_onclick
 1009: }
 1010: // ]]>
 1011: </script>
 1012: ENDJS
 1013:     }
 1014: 
 1015:     $r->print(<<ENDLOGIN);
 1016: <div style="display:$stdauthformstyle;" id="LC_standard_login">
 1017: <div class="LC_Box" style="background:$loginbox_bg;">
 1018:   $logintitle
 1019:   $loginform
 1020:   $noscript_warning
 1021: </div>
 1022:   
 1023: <div class="LC_Box" style="padding-top: 10px;">
 1024:   $loginhelp
 1025:   $forgotpw
 1026:   $contactblock
 1027:   $newuserlink
 1028:   $coursecatalog
 1029: </div>
 1030: </div>
 1031: 
 1032: ENDLOGIN
 1033:     $r->print('</div><div>'."\n");
 1034:     if ($showmainlogo) {
 1035:         my $alttext = &Apache::loncommon::designparm('login.alttext_logo',$domain);
 1036:         $r->print(' <img src="'.$logo.'" alt="'.$alttext.'" class="LC_maxwidth" id="lcloginmainlogo" />'."\n");
 1037:     }
 1038: $r->print(<<ENDTOP);
 1039: $announcements
 1040: </div>
 1041: <hr style="clear:both;" />
 1042: ENDTOP
 1043:     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
 1044:     $domainrow = <<"END";
 1045:       <tr>
 1046:        <td  align="left" valign="top">
 1047:         <small><b>$lt{'dom'}:&nbsp;</b></small>
 1048:        </td>
 1049:        <td  align="left" valign="top">
 1050:         <small><tt>&nbsp;$domain</tt></small>
 1051:        </td>
 1052:       </tr>
 1053: END
 1054:     $serverrow = <<"END";
 1055:       <tr>
 1056:        <td  align="left" valign="top">
 1057:         <small><b>$lt{'serv'}:&nbsp;</b></small>
 1058:        </td>
 1059:        <td align="left" valign="top">
 1060:         <small><tt>&nbsp;$lonhost ($role)</tt></small>
 1061:        </td>
 1062:       </tr>
 1063: END
 1064:     if ($loadlim) {
 1065:         $loadrow = <<"END";
 1066:       <tr>
 1067:        <td align="left" valign="top">
 1068:         <small><b>$lt{'load'}:&nbsp;</b></small>
 1069:        </td>
 1070:        <td align="left" valign="top">
 1071:         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
 1072:        </td>
 1073:       </tr>
 1074: END
 1075:     }
 1076:     if ($uloadlim) {
 1077:         $userloadrow = <<"END";
 1078:       <tr>
 1079:        <td align="left" valign="top">
 1080:         <small><b>$lt{'userload'}:&nbsp;</b></small>
 1081:        </td>
 1082:        <td align="left" valign="top">
 1083:         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
 1084:        </td>
 1085:       </tr>
 1086: END
 1087:     }
 1088:     if (($version ne '') && ($version ne '<!-- VERSION -->')) {
 1089:         $versionrow = <<"END";
 1090:       <tr>
 1091:        <td colspan="2" align="left">
 1092:         <small>$version</small>
 1093:        </td>
 1094:       </tr>
 1095: END
 1096:     }
 1097: 
 1098:     $r->print(<<ENDDOCUMENT);
 1099:     <div style="float: left;">
 1100:      <table border="0" cellspacing="0" cellpadding="0">
 1101: $domainrow
 1102: $serverrow
 1103: $loadrow    
 1104: $userloadrow
 1105: $versionrow
 1106:      </table>
 1107:     </div>
 1108:     <div style="float: right;">
 1109:     $domainlogo
 1110:     </div>
 1111:     <br style="clear:both;" />
 1112:  </div>
 1113: 
 1114: $in_frame_js
 1115: <script type="text/javascript">
 1116: // <![CDATA[
 1117: // the if prevents the script error if the browser can not handle this
 1118: if ( document.client.uname ) { document.client.uname.focus(); }
 1119: // ]]>
 1120: </script>
 1121: $helpdeskscript
 1122: 
 1123: ENDDOCUMENT
 1124:     my %endargs = ( 'noredirectlink' => 1, );
 1125:     $r->print(&Apache::loncommon::end_page(\%endargs));
 1126:     return OK;
 1127: }
 1128: 
 1129: sub check_loginvia {
 1130:     my ($domain,$lonhost,$lonidsdir,$balcookie) = @_;
 1131:     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {
 1132:         return;
 1133:     }
 1134:     my %domconfhash = &Apache::loncommon::get_domainconf($domain);
 1135:     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
 1136:     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
 1137:     my $output;
 1138:     if ($loginvia ne '') {
 1139:         my $noredirect;
 1140:         my $ip = &Apache::lonnet::get_requestor_ip();
 1141:         if ($ip eq '127.0.0.1') {
 1142:             $noredirect = 1;
 1143:         } else {
 1144:             if ($loginvia_exempt ne '') {
 1145:                 my @exempt = split(',',$loginvia_exempt);
 1146:                 if (grep(/^\Q$ip\E$/,@exempt)) {
 1147:                     $noredirect = 1;
 1148:                 }
 1149:             }
 1150:         }
 1151:         unless ($noredirect) {
 1152:             my ($newhost,$path);
 1153:             if ($loginvia =~ /:/) {
 1154:                 ($newhost,$path) = split(':',$loginvia);
 1155:             } else {
 1156:                 $newhost = $loginvia;
 1157:             }
 1158:             if ($newhost ne $lonhost) {
 1159:                 if (&Apache::lonnet::hostname($newhost) ne '') {
 1160:                     if ($balcookie) {
 1161:                         my ($balancer,$cookie) = split(/:/,$balcookie);
 1162:                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {
 1163:                             my ($udom,$uname,$cookieid) = ($1,$2,$3);
 1164:                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {
 1165:                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {
 1166:                                     while (my $filename=readdir($dh)) {
 1167:                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {
 1168:                                             my $handle = $1;
 1169:                                             my %hash =
 1170:                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,
 1171:                                                                                      ['request.balancercookie',
 1172:                                                                                       'user.linkedenv']);
 1173:                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {
 1174:                                                 if (unlink("$lonidsdir/$filename")) {
 1175:                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
 1176:                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&
 1177:                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {
 1178:                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");
 1179:                                                     }
 1180:                                                 }
 1181:                                             }
 1182:                                             last;
 1183:                                         }
 1184:                                     }
 1185:                                     closedir($dh);
 1186:                                 }
 1187:                             }
 1188:                         }
 1189:                     }
 1190:                     $output = &redirect_page($newhost,$path);
 1191:                 }
 1192:             }
 1193:         }
 1194:     }
 1195:     return $output;
 1196: }
 1197: 
 1198: sub redirect_page {
 1199:     my ($desthost,$path) = @_;
 1200:     my $hostname = &Apache::lonnet::hostname($desthost);
 1201:     my $protocol = $Apache::lonnet::protocol{$desthost};
 1202:     $protocol = 'http' if ($protocol ne 'https');
 1203:     unless ($path =~ m{^/}) {
 1204:         $path = '/'.$path;
 1205:     }
 1206:     my $url = $protocol.'://'.$hostname.$path;
 1207:     my $args = {};
 1208:     if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
 1209:         $url = $protocol.'://'.$hostname.$env{'form.firsturl'};
 1210:         if (($env{'form.ltoken'}) || ($env{'form.linkprot'} ne '') ||
 1211:             ($env{'form.linkkey'} ne '')) {
 1212:             my %link_info;
 1213:             if ($env{'form.ltoken'}) {
 1214:                 %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1215:                 &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1216:                 $args->{'only_body'} = 1;
 1217:             } elsif ($env{'form.linkprot'}) {
 1218:                 $link_info{'linkprot'} = $env{'form.linkprot'};
 1219:                 foreach my $item ('linkprotuser','linkprotexit') {
 1220:                     if ($env{'form.'.$item}) {
 1221:                         $link_info{$item} = $env{'form.'.$item};
 1222:                     }
 1223:                 }
 1224:                 $args->{'only_body'} = 1;
 1225:             } elsif ($env{'form.linkkey'} ne '') {
 1226:                 $link_info{'linkkey'} = $env{'form.linkkey'};
 1227:             }
 1228:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1229:             unless (($token eq 'con_lost') || ($token eq 'refused') ||
 1230:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1231:                 $url .= '?ltoken='.$token;
 1232:             }
 1233:         }
 1234:     } else {
 1235:         my $querystring;
 1236:         if ($env{'form.firsturl'} ne '') {
 1237:             if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
 1238:                 $querystring = &uri_escape_utf8($env{'form.firsturl'});
 1239:             } else {
 1240:                 $querystring = &uri_escape($env{'form.firsturl'});
 1241:             }
 1242:             $querystring = &HTML::Entities::encode($querystring,"'");
 1243:             $querystring = '?firsturl='.$querystring;
 1244:         }
 1245:         if ($env{'form.ltoken'}) {
 1246:             my %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1247:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1248:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1249:             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
 1250:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1251:                 unless (($path eq '/adm/roles') || ($path eq '/adm/login')) {
 1252:                     $url = $protocol.'://'.$hostname.'/adm/roles';
 1253:                 }
 1254:                 $querystring .= (($querystring =~/^\?/)?'&amp;':'?') . 'ttoken='.$token;
 1255:             }
 1256:         }
 1257:         $url .= $querystring;
 1258:     }
 1259:     $args->{'redirect'} = [0,$url];
 1260:     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,$args);
 1261:     my $end_page   = &Apache::loncommon::end_page();
 1262:     return $start_page.$end_page;
 1263: }
 1264: 
 1265: sub contactdisplay {
 1266:     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
 1267:         $possdoms) = @_;
 1268:     my $contactblock;
 1269:     my $origmail;
 1270:     if (ref($possdoms) eq 'ARRAY') {
 1271:         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) { 
 1272:             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
 1273:         }
 1274:     }
 1275:     my $requestmail = 
 1276:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
 1277:                                                  $authdomain,$origmail);
 1278:     unless ($showhelpdesk eq '0') {
 1279:         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
 1280:             $showhelpdesk = 1;
 1281:         } else {
 1282:             $showhelpdesk = 0;
 1283:         }
 1284:     }
 1285:     if ($servadm && $showadminmail) {
 1286:         $contactblock .= $$lt{'servadm'}.':<br />'.
 1287:                          '<tt>'.$servadm.'</tt><br />';
 1288:     }
 1289:     if ($showhelpdesk) {
 1290:         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
 1291:         my $thisurl = &escape('/adm/login');
 1292:         $$helpdeskscript = <<"ENDSCRIPT";
 1293: <script type="text/javascript">
 1294: // <![CDATA[
 1295: function helpdesk() {
 1296:     var possdom = document.client.udom.value;
 1297:     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
 1298:     if (codedom == '') {
 1299:         codedom = "$authdomain";
 1300:     }
 1301:     var querystr = "origurl=$thisurl&codedom="+codedom;
 1302:     document.location.href = "/adm/helpdesk?"+querystr;
 1303:     return;
 1304: }
 1305: // ]]>
 1306: </script>
 1307: ENDSCRIPT
 1308:     }
 1309:     return $contactblock;
 1310: }
 1311: 
 1312: sub forgotpwdisplay {
 1313:     my (%lt) = @_;
 1314:     my $prompt_for_resetpw = 1; 
 1315:     if ($prompt_for_resetpw) {
 1316:         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
 1317:     }
 1318:     return;
 1319: }
 1320: 
 1321: sub coursecatalog_link {
 1322:     my ($linkname) = @_;
 1323:     return <<"END";
 1324:       <a href="/adm/coursecatalog">$linkname</a>
 1325: END
 1326: }
 1327: 
 1328: sub newuser_link {
 1329:     my ($linkname) = @_;
 1330:     return '<a href="/adm/createaccount">'.$linkname.'</a>';
 1331: }
 1332: 
 1333: sub decode_token {
 1334:     my ($info) = @_;
 1335:     my ($firsturl,@rest)=split(/\&/,$info);
 1336:     my %form;
 1337:     if ($firsturl ne '') {
 1338:         $form{'firsturl'} = &unescape($firsturl);
 1339:     }
 1340:     foreach my $item (@rest) {
 1341:         my ($key,$value) = split(/=/,$item);
 1342:         $form{$key} = &unescape($value);
 1343:     }
 1344:     return %form;
 1345: }
 1346: 
 1347: 1;
 1348: __END__

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