File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.158.2.13.2.6: download - view: text, annotated - select for diffs
Fri Jul 8 15:43:51 2022 UTC (22 months, 1 week ago) by raeburn
Branches: version_2_11_4_msu
- For 2.11.4 (modified)
  Include changes in 1.199, 1.200, 1.201

    1: # The LearningOnline Network
    2: # Login Screen
    3: #
    4: # $Id: lonlogin.pm,v 1.158.2.13.2.6 2022/07/08 15:43:51 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:     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
  664:     @hosts = &Apache::lonnet::current_machine_ids();
  665:     $lonhost_in_use = $lonhost;
  666:     if (@hosts > 1) {
  667:         foreach my $hostid (@hosts) {
  668:             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
  669:                 $lonhost_in_use = $hostid;
  670:                 last;
  671:             }
  672:         }
  673:     }
  674:     $saml_prefix = $defdom.'.login.saml_';
  675:     if ($defaultdomconf{$saml_prefix.$lonhost_in_use}) {
  676:         $saml_landing = 1;
  677:         $samlssotext = $defaultdomconf{$saml_prefix.'text_'.$lonhost_in_use};
  678:         $samlnonsso = $defaultdomconf{$saml_prefix.'notsso_'.$lonhost_in_use};
  679:         $samlssoimg = $defaultdomconf{$saml_prefix.'img_'.$lonhost_in_use};
  680:         $samlssoalt = $defaultdomconf{$saml_prefix.'alt_'.$lonhost_in_use};
  681:         $samlssourl = $defaultdomconf{$saml_prefix.'url_'.$lonhost_in_use};
  682:         $samltooltip = $defaultdomconf{$saml_prefix.'title_'.$lonhost_in_use};
  683:     }
  684:     if ($saml_landing) {
  685:        if ($samlssotext eq '') {
  686:            $samlssotext = 'SSO Login';
  687:        }
  688:        if ($samlnonsso eq '') {
  689:            $samlnonsso = 'Non-SSO Login';
  690:        }
  691:        $js .= <<"ENDSAMLJS";
  692: 
  693: <script type="text/javascript">
  694: // <![CDATA[
  695: function toggleLClogin() {
  696:     if (document.getElementById('LC_standard_login')) {
  697:         if (document.getElementById('LC_standard_login').style.display == 'none') {
  698:             document.getElementById('LC_standard_login').style.display = 'inline-block';
  699:             if (document.getElementById('LC_login_text')) {
  700:                 document.getElementById('LC_login_text').innerHTML = '$samlnonsso';
  701:             }
  702:             if ( document.client.uname ) { document.client.uname.focus(); }
  703:             if (document.getElementById('LC_SSO_login')) {
  704:                 document.getElementById('LC_SSO_login').style.display = 'none';
  705:             }
  706:         } else {
  707:             document.getElementById('LC_standard_login').style.display = 'none';
  708:             if (document.getElementById('LC_login_text')) {
  709:                 document.getElementById('LC_login_text').innerHTML = '$samlssotext';
  710:             }
  711:             if (document.getElementById('LC_SSO_login')) {
  712:                 document.getElementById('LC_SSO_login').style.display = 'inline-block';
  713:             }
  714:         }
  715:     }
  716:     return;
  717: }
  718: 
  719: // ]]>
  720: </script>
  721: 
  722: ENDSAMLJS
  723:     }
  724: 
  725: # --------------------------------------------------- Print login screen header
  726: 
  727:     my %add_entries = (
  728: 	       bgcolor      => "$mainbg",
  729: 	       text         => "$font",
  730: 	       link         => "$link",
  731: 	       vlink        => "$vlink",
  732: 	       alink        => "$alink",
  733:                onload       => 'javascript:enableInput();',);
  734: 
  735:     my ($headextra,$headextra_exempt);
  736:     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
  737:     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
  738:     if ($headextra) {
  739:         my $omitextra;
  740:         if ($headextra_exempt ne '') {
  741:             my @exempt = split(',',$headextra_exempt);
  742:             my $ip = &Apache::lonnet::get_requestor_ip();
  743:             if (grep(/^\Q$ip\E$/,@exempt)) {
  744:                 $omitextra = 1;
  745:             }
  746:         }
  747:         unless ($omitextra) {
  748:             my $confname = $defdom.'-domainconfig';
  749:             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
  750:                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
  751:                 unless ($extra eq '-1') {
  752:                     $js .= "\n".$extra."\n";
  753:                 }
  754:             }
  755:         }
  756:     }
  757: 
  758:     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
  759: 			       { 'redirect'       => [$expire,'/adm/roles'], 
  760: 				 'add_entries' => \%add_entries,
  761: 				 'only_body'   => 1,}));
  762: 
  763: # ----------------------------------------------------------------------- Texts
  764: 
  765:     my %lt=&Apache::lonlocal::texthash(
  766:           'un'       => 'Username',
  767:           'pw'       => 'Password',
  768:           'dom'      => 'Domain',
  769:           'perc'     => 'percent',
  770:           'load'     => 'Server Load',
  771:           'userload' => 'User Load',
  772:           'catalog'  => 'Course/Community Catalog',
  773:           'log'      => 'Log in',
  774:           'help'     => 'Log-in Help',
  775:           'serv'     => 'Server',
  776:           'servadm'  => 'Server Administration',
  777:           'helpdesk' => 'Contact Helpdesk',
  778:           'forgotpw' => 'Forgot password?',
  779:           'newuser'  => 'New User?',
  780:           'change'   => 'Change?',
  781:        );
  782: # -------------------------------------------------- Change password field name
  783: 
  784:     my $forgotpw = &forgotpwdisplay(%lt);
  785:     $forgotpw .= '<br />' if $forgotpw;
  786:     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
  787:     if ($loginhelp) {
  788:         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
  789:     }
  790: 
  791: # ---------------------------------------------------- Serve out DES JavaScript
  792:     {
  793:     my $jsh=Apache::File->new($include."/londes.js");
  794:     $r->print(<$jsh>);
  795:     }
  796: # ---------------------------------------------------------- Serve rest of page
  797: 
  798:     $r->print(
  799:     '<div class="LC_Box"'
  800:    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
  801: );
  802: 
  803:     $r->print(<<ENDSERVERFORM);
  804: <form name="server" action="/adm/authenticate" method="post" target="_top">
  805:    <input type="hidden" name="logtoken" value="$logtoken" />
  806:    <input type="hidden" name="serverid" value="$lonhost" />
  807:    <input type="hidden" name="uname" value="" />
  808:    <input type="hidden" name="upass0" value="" />
  809:    <input type="hidden" name="udom" value="" />
  810:    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
  811:    <input type="hidden" name="localres" value="$env{'form.localres'}" />
  812:   </form>
  813: ENDSERVERFORM
  814:     my $coursecatalog;
  815:     if (($showcoursecat eq '') || ($showcoursecat)) {
  816:         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
  817:     }
  818:     my $newuserlink;
  819:     if ($shownewuserlink) {
  820:         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  821:     }
  822:     my $logintitle =
  823:         '<h2 class="LC_hcell"'
  824:        .' style="background:'.$loginbox_header_bgcol.';'
  825:        .' color:'.$loginbox_header_textcol.'">'
  826:        .$lt{'log'}
  827:        .'</h2>';
  828: 
  829:     my $noscript_warning='<noscript><span class="LC_warning"><b>'
  830:                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
  831:                         .'</b></span></noscript>';
  832:     my $helpdeskscript;
  833:     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
  834:                                        $authdomain,\$helpdeskscript,
  835:                                        $showhelpdesk,\@possdoms);
  836: 
  837:     my $mobileargs;
  838:     if ($clientmobile) {
  839:         $mobileargs = 'autocapitalize="off" autocorrect="off"'; 
  840:     }
  841:     my $loginform=(<<LFORM);
  842: <form name="client" action="" onsubmit="return(send())" id="lclogin">
  843:   <input type="hidden" name="lextkey" value="$lextkey" />
  844:   <input type="hidden" name="uextkey" value="$uextkey" />
  845:   <b><label for="uname">$lt{'un'}</label>:</b><br />
  846:   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
  847:   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
  848:   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
  849:   <b><label for="udom">$lt{'dom'}</label>:</b><br />
  850:   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
  851:   <input type="submit" value="$lt{'log'}" />
  852: </form>
  853: LFORM
  854: 
  855:     if ($showbanner) {
  856:         my $alttext = &Apache::loncommon::designparm('login.alttext_img',$domain);
  857:         if ($alttext eq '') {
  858:             $alttext = 'The Learning Online Network with CAPA';
  859:         }
  860:         $r->print(<<HEADER);
  861: <!-- The LON-CAPA Header -->
  862: <div style="background:$pgbg;margin:0;width:100%;">
  863:   <img src="$img" border="0" alt="$alttext" class="LC_maxwidth" id="lcloginbanner" />
  864: </div>
  865: HEADER
  866:     }
  867: 
  868:     my $stdauthformstyle = 'inline-block';
  869:     my $ssoauthstyle = 'none';
  870:     my $logintype;
  871:     $r->print('<div style="float:left;margin-top:0;">');
  872:     if ($saml_landing) {
  873:         $ssoauthstyle = 'inline-block';
  874:         $stdauthformstyle = 'none';
  875:         $logintype = $samlssotext;
  876:         my $ssologin = '/adm/sso';
  877:         if ($samlssourl  ne '') {
  878:             $ssologin = $samlssourl;
  879:         }
  880:         if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  881:             my $querystring;
  882:             if ($env{'form.firsturl'} ne '') {
  883:                 $querystring = 'origurl=';
  884:                 if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
  885:                     $querystring .= &uri_escape_utf8($env{'form.firsturl'});
  886:                 } else {
  887:                     $querystring .= &uri_escape($env{'form.firsturl'});
  888:                 }
  889:                 $querystring = &HTML::Entities::encode($querystring,"'");
  890:             }
  891:             if ($env{'form.ltoken'} ne '') {
  892:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'ltoken='.
  893:                                   &HTML::Entities::encode(&uri_escape($env{'form.ltoken'}));
  894:             } elsif ($env{'form.linkkey'}) {
  895:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'linkkey='.
  896:                                   &HTML::Entities::encode(&uri_escape($env{'form.linkkey'}));
  897:             }
  898:             if ($querystring ne '') {
  899:                 $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . $querystring;
  900:             }
  901:         } elsif ($logtoken ne '') {
  902:             $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . 'logtoken='.$logtoken;
  903:         }
  904:         my $ssohref;
  905:         if ($samlssoimg ne '') {
  906:             $ssohref = '<a href="'.$ssologin.'" title="'.$samltooltip.'">'.
  907:                        '<img src="'.$samlssoimg.'" alt="'.$samlssoalt.'" id="lcssobutton" /></a>';
  908:         } else {
  909:             $ssohref = '<a href="'.$ssologin.'">'.$samlssotext.'</a>';
  910:         }
  911:         if (($env{'form.saml'} eq 'no') ||
  912:             (($env{'form.username'} ne '') && ($env{'form.domain'} ne ''))) {
  913:             $ssoauthstyle = 'none';
  914:             $stdauthformstyle = 'inline-block';
  915:             $logintype = $samlnonsso;
  916:         }
  917:         $r->print(<<ENDSAML);
  918: <p>
  919: Log-in type:
  920: <span style="font-weight:bold" id="LC_login_text">$logintype</span><br />
  921: <span><a href="javascript:toggleLClogin();" style="color:#000000">$lt{'change'}</a></span>
  922: </p>
  923: <div style="display:$ssoauthstyle" id="LC_SSO_login">
  924: <div class="LC_Box" style="padding-top: 10px;">
  925: $ssohref
  926: $noscript_warning
  927: </div>
  928: <div class="LC_Box" style="padding-top: 10px;">
  929: $loginhelp
  930: $contactblock
  931: $coursecatalog
  932: </div>
  933: </div>
  934: ENDSAML
  935:     } else {
  936:         if ($env{'form.ltoken'}) {
  937:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
  938:             delete($env{'form.ltoken'});
  939:         }
  940:     }
  941:     my $in_frame_js;
  942:     if ($linkprot_for_login) {
  943:         my ($linkprotector,$linkproturi) = split(/:/,$linkprot_for_login,2);
  944:         if (($linkprotector =~ /^\d+(c|d)$/) && ($linkproturi =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$})) {
  945:             my $set_target;
  946:             if (($env{'form.retry'}) || ($env{'form.sso'})) {
  947:                 if ($linkproturi eq $env{'form.firsturl'}) {
  948:                     $set_target = "    document.server.target = '_self';";
  949:                 }
  950:             } else {
  951:                 $set_target = <<ENDTARG;
  952:     var linkproturi = '$linkproturi';
  953:     var path = document.location.pathname.replace( new RegExp('^/adm/launch'),'');
  954:     if (linkproturi == path) {
  955:         document.server.target = '_self';
  956:     }
  957: ENDTARG
  958:             }
  959:             $in_frame_js = <<ENDJS;
  960: <script type="text/javascript">
  961: // <![CDATA[
  962: if ((window.self !== window.top) && (document.server.target != '_self')) {
  963:     $set_target
  964: }
  965: // ]]>
  966: </script>
  967: ENDJS
  968:         }
  969:     }
  970: 
  971:     $r->print(<<ENDLOGIN);
  972: <div style="display:$stdauthformstyle;" id="LC_standard_login">
  973: <div class="LC_Box" style="background:$loginbox_bg;">
  974:   $logintitle
  975:   $loginform
  976:   $noscript_warning
  977: </div>
  978:   
  979: <div class="LC_Box" style="padding-top: 10px;">
  980:   $loginhelp
  981:   $forgotpw
  982:   $contactblock
  983:   $newuserlink
  984:   $coursecatalog
  985: </div>
  986: </div>
  987: 
  988: ENDLOGIN
  989:     $r->print('</div><div>'."\n");
  990:     if ($showmainlogo) {
  991:         my $alttext = &Apache::loncommon::designparm('login.alttext_logo',$domain);
  992:         $r->print(' <img src="'.$logo.'" alt="'.$alttext.'" class="LC_maxwidth" id="lcloginmainlogo" />'."\n");
  993:     }
  994: $r->print(<<ENDTOP);
  995: $announcements
  996: </div>
  997: <hr style="clear:both;" />
  998: ENDTOP
  999:     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
 1000:     $domainrow = <<"END";
 1001:       <tr>
 1002:        <td  align="left" valign="top">
 1003:         <small><b>$lt{'dom'}:&nbsp;</b></small>
 1004:        </td>
 1005:        <td  align="left" valign="top">
 1006:         <small><tt>&nbsp;$domain</tt></small>
 1007:        </td>
 1008:       </tr>
 1009: END
 1010:     $serverrow = <<"END";
 1011:       <tr>
 1012:        <td  align="left" valign="top">
 1013:         <small><b>$lt{'serv'}:&nbsp;</b></small>
 1014:        </td>
 1015:        <td align="left" valign="top">
 1016:         <small><tt>&nbsp;$lonhost ($role)</tt></small>
 1017:        </td>
 1018:       </tr>
 1019: END
 1020:     if ($loadlim) {
 1021:         $loadrow = <<"END";
 1022:       <tr>
 1023:        <td align="left" valign="top">
 1024:         <small><b>$lt{'load'}:&nbsp;</b></small>
 1025:        </td>
 1026:        <td align="left" valign="top">
 1027:         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
 1028:        </td>
 1029:       </tr>
 1030: END
 1031:     }
 1032:     if ($uloadlim) {
 1033:         $userloadrow = <<"END";
 1034:       <tr>
 1035:        <td align="left" valign="top">
 1036:         <small><b>$lt{'userload'}:&nbsp;</b></small>
 1037:        </td>
 1038:        <td align="left" valign="top">
 1039:         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
 1040:        </td>
 1041:       </tr>
 1042: END
 1043:     }
 1044:     if (($version ne '') && ($version ne '<!-- VERSION -->')) {
 1045:         $versionrow = <<"END";
 1046:       <tr>
 1047:        <td colspan="2" align="left">
 1048:         <small>$version</small>
 1049:        </td>
 1050:       </tr>
 1051: END
 1052:     }
 1053: 
 1054:     $r->print(<<ENDDOCUMENT);
 1055:     <div style="float: left;">
 1056:      <table border="0" cellspacing="0" cellpadding="0">
 1057: $domainrow
 1058: $serverrow
 1059: $loadrow    
 1060: $userloadrow
 1061: $versionrow
 1062:      </table>
 1063:     </div>
 1064:     <div style="float: right;">
 1065:     $domainlogo
 1066:     </div>
 1067:     <br style="clear:both;" />
 1068:  </div>
 1069: 
 1070: $in_frame_js
 1071: <script type="text/javascript">
 1072: // <![CDATA[
 1073: // the if prevents the script error if the browser can not handle this
 1074: if ( document.client.uname ) { document.client.uname.focus(); }
 1075: // ]]>
 1076: </script>
 1077: $helpdeskscript
 1078: 
 1079: ENDDOCUMENT
 1080:     my %endargs = ( 'noredirectlink' => 1, );
 1081:     $r->print(&Apache::loncommon::end_page(\%endargs));
 1082:     return OK;
 1083: }
 1084: 
 1085: sub check_loginvia {
 1086:     my ($domain,$lonhost,$lonidsdir,$balcookie) = @_;
 1087:     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {
 1088:         return;
 1089:     }
 1090:     my %domconfhash = &Apache::loncommon::get_domainconf($domain);
 1091:     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
 1092:     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
 1093:     my $output;
 1094:     if ($loginvia ne '') {
 1095:         my $noredirect;
 1096:         my $ip = &Apache::lonnet::get_requestor_ip();
 1097:         if ($ip eq '127.0.0.1') {
 1098:             $noredirect = 1;
 1099:         } else {
 1100:             if ($loginvia_exempt ne '') {
 1101:                 my @exempt = split(',',$loginvia_exempt);
 1102:                 if (grep(/^\Q$ip\E$/,@exempt)) {
 1103:                     $noredirect = 1;
 1104:                 }
 1105:             }
 1106:         }
 1107:         unless ($noredirect) {
 1108:             my ($newhost,$path);
 1109:             if ($loginvia =~ /:/) {
 1110:                 ($newhost,$path) = split(':',$loginvia);
 1111:             } else {
 1112:                 $newhost = $loginvia;
 1113:             }
 1114:             if ($newhost ne $lonhost) {
 1115:                 if (&Apache::lonnet::hostname($newhost) ne '') {
 1116:                     if ($balcookie) {
 1117:                         my ($balancer,$cookie) = split(/:/,$balcookie);
 1118:                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {
 1119:                             my ($udom,$uname,$cookieid) = ($1,$2,$3);
 1120:                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {
 1121:                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {
 1122:                                     while (my $filename=readdir($dh)) {
 1123:                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {
 1124:                                             my $handle = $1;
 1125:                                             my %hash =
 1126:                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,
 1127:                                                                                      ['request.balancercookie',
 1128:                                                                                       'user.linkedenv']);
 1129:                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {
 1130:                                                 if (unlink("$lonidsdir/$filename")) {
 1131:                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
 1132:                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&
 1133:                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {
 1134:                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");
 1135:                                                     }
 1136:                                                 }
 1137:                                             }
 1138:                                             last;
 1139:                                         }
 1140:                                     }
 1141:                                     closedir($dh);
 1142:                                 }
 1143:                             }
 1144:                         }
 1145:                     }
 1146:                     $output = &redirect_page($newhost,$path);
 1147:                 }
 1148:             }
 1149:         }
 1150:     }
 1151:     return $output;
 1152: }
 1153: 
 1154: sub redirect_page {
 1155:     my ($desthost,$path) = @_;
 1156:     my $hostname = &Apache::lonnet::hostname($desthost);
 1157:     my $protocol = $Apache::lonnet::protocol{$desthost};
 1158:     $protocol = 'http' if ($protocol ne 'https');
 1159:     unless ($path =~ m{^/}) {
 1160:         $path = '/'.$path;
 1161:     }
 1162:     my $url = $protocol.'://'.$hostname.$path;
 1163:     my $args = {};
 1164:     if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
 1165:         $url = $protocol.'://'.$hostname.$env{'form.firsturl'};
 1166:         if (($env{'form.ltoken'}) || ($env{'form.linkprot'} ne '') ||
 1167:             ($env{'form.linkkey'} ne '')) {
 1168:             my %link_info;
 1169:             if ($env{'form.ltoken'}) {
 1170:                 %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1171:                 &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1172:                 $args->{'only_body'} = 1;
 1173:             } elsif ($env{'form.linkprot'}) {
 1174:                 $link_info{'linkprot'} = $env{'form.linkprot'};
 1175:                 foreach my $item ('linkprotuser','linkprotexit') {
 1176:                     if ($env{'form.'.$item}) {
 1177:                         $link_info{$item} = $env{'form.'.$item};
 1178:                     }
 1179:                 }
 1180:                 $args->{'only_body'} = 1;
 1181:             } elsif ($env{'form.linkkey'} ne '') {
 1182:                 $link_info{'linkkey'} = $env{'form.linkkey'};
 1183:             }
 1184:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1185:             unless (($token eq 'con_lost') || ($token eq 'refused') ||
 1186:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1187:                 $url .= '?ltoken='.$token;
 1188:             }
 1189:         }
 1190:     } else {
 1191:         my $querystring;
 1192:         if ($env{'form.firsturl'} ne '') {
 1193:             if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
 1194:                 $querystring = &uri_escape_utf8($env{'form.firsturl'});
 1195:             } else {
 1196:                 $querystring = &uri_escape($env{'form.firsturl'});
 1197:             }
 1198:             $querystring = &HTML::Entities::encode($querystring,"'");
 1199:             $querystring = '?firsturl='.$querystring;
 1200:         }
 1201:         if ($env{'form.ltoken'}) {
 1202:             my %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1203:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1204:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1205:             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
 1206:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1207:                 unless (($path eq '/adm/roles') || ($path eq '/adm/login')) {
 1208:                     $url = $protocol.'://'.$hostname.'/adm/roles';
 1209:                 }
 1210:                 $querystring .= (($querystring =~/^\?/)?'&amp;':'?') . 'ttoken='.$token;
 1211:             }
 1212:         }
 1213:         $url .= $querystring;
 1214:     }
 1215:     $args->{'redirect'} = [0,$url];
 1216:     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,$args);
 1217:     my $end_page   = &Apache::loncommon::end_page();
 1218:     return $start_page.$end_page;
 1219: }
 1220: 
 1221: sub contactdisplay {
 1222:     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
 1223:         $possdoms) = @_;
 1224:     my $contactblock;
 1225:     my $origmail;
 1226:     if (ref($possdoms) eq 'ARRAY') {
 1227:         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) { 
 1228:             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
 1229:         }
 1230:     }
 1231:     my $requestmail = 
 1232:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
 1233:                                                  $authdomain,$origmail);
 1234:     unless ($showhelpdesk eq '0') {
 1235:         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
 1236:             $showhelpdesk = 1;
 1237:         } else {
 1238:             $showhelpdesk = 0;
 1239:         }
 1240:     }
 1241:     if ($servadm && $showadminmail) {
 1242:         $contactblock .= $$lt{'servadm'}.':<br />'.
 1243:                          '<tt>'.$servadm.'</tt><br />';
 1244:     }
 1245:     if ($showhelpdesk) {
 1246:         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
 1247:         my $thisurl = &escape('/adm/login');
 1248:         $$helpdeskscript = <<"ENDSCRIPT";
 1249: <script type="text/javascript">
 1250: // <![CDATA[
 1251: function helpdesk() {
 1252:     var possdom = document.client.udom.value;
 1253:     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
 1254:     if (codedom == '') {
 1255:         codedom = "$authdomain";
 1256:     }
 1257:     var querystr = "origurl=$thisurl&codedom="+codedom;
 1258:     document.location.href = "/adm/helpdesk?"+querystr;
 1259:     return;
 1260: }
 1261: // ]]>
 1262: </script>
 1263: ENDSCRIPT
 1264:     }
 1265:     return $contactblock;
 1266: }
 1267: 
 1268: sub forgotpwdisplay {
 1269:     my (%lt) = @_;
 1270:     my $prompt_for_resetpw = 1; 
 1271:     if ($prompt_for_resetpw) {
 1272:         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
 1273:     }
 1274:     return;
 1275: }
 1276: 
 1277: sub coursecatalog_link {
 1278:     my ($linkname) = @_;
 1279:     return <<"END";
 1280:       <a href="/adm/coursecatalog">$linkname</a>
 1281: END
 1282: }
 1283: 
 1284: sub newuser_link {
 1285:     my ($linkname) = @_;
 1286:     return '<a href="/adm/createaccount">'.$linkname.'</a>';
 1287: }
 1288: 
 1289: sub decode_token {
 1290:     my ($info) = @_;
 1291:     my ($firsturl,@rest)=split(/\&/,$info);
 1292:     my %form;
 1293:     if ($firsturl ne '') {
 1294:         $form{'firsturl'} = &unescape($firsturl);
 1295:     }
 1296:     foreach my $item (@rest) {
 1297:         my ($key,$value) = split(/=/,$item);
 1298:         $form{$key} = &unescape($value);
 1299:     }
 1300:     return %form;
 1301: }
 1302: 
 1303: 1;
 1304: __END__

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