File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.200: download - view: text, annotated - select for diffs
Sun Jun 26 04:03:47 2022 UTC (23 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  - Set request.linkprotuser in %env for students accessing deep-linked items
    with LTI link protection which includes username from other LMS.
  - Set "only_body" for intermediate pages shown while session is being set up
    for LTI link-protected access.
  - Provide appropriate feedback when a deep link is followed but the user
    only has a future and/or expired role in the target course.

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

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