File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.199: download - view: text, annotated - select for diffs
Sat Jun 18 02:10:18 2022 UTC (23 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  For LTI-protected deep links in which username is included in launch payload
  compare username in payload with username for any existing LON-CAPA session
  in current web browser and expire old session, if different user.

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

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