File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.158.2.8: download - view: text, annotated - select for diffs
Thu Aug 1 14:21:37 2019 UTC (4 years, 10 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.158: preferred, unified
- For 2.11
  Backport 1.174, 1.175, 1.176, 1.178

    1: # The LearningOnline Network
    2: # Login Screen
    3: #
    4: # $Id: lonlogin.pm,v 1.158.2.8 2019/08/01 14:21:37 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 CGI::Cookie();
   42:  
   43: sub handler {
   44:     my $r = shift;
   45: 
   46:     &Apache::loncommon::get_unprocessed_cgi
   47: 	(join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},
   48: 	      $ENV{'REDIRECT_QUERY_STRING'}),
   49: 	 ['interface','username','domain','firsturl','localpath','localres',
   50: 	  'token','role','symb','iptoken','btoken']);
   51:     if (!defined($env{'form.firsturl'})) {
   52:         &Apache::lonacc::get_posted_cgi($r,['firsturl']);
   53:     }
   54: 
   55: # -- check if they are a migrating user
   56:     if (defined($env{'form.token'})) {
   57: 	return &Apache::migrateuser::handler($r);
   58:     }
   59: 
   60: # For "public user" - remove any exising "public" cookie, as user really wants to log-in
   61:     my ($handle,$lonidsdir,$expirepub,$userdom);
   62:     $lonidsdir=$r->dir_config('lonIDsDir');
   63:     unless ($r->header_only) {
   64:         $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);
   65:         if ($handle ne '') {
   66:             if ($handle=~/^publicuser\_/) {
   67:                 unlink($r->dir_config('lonIDsDir')."/$handle.id");
   68:                 undef($handle);
   69:                 undef($userdom);
   70:                 $expirepub = 1;
   71:             }
   72:         }
   73:     }
   74: 
   75:     &Apache::loncommon::no_cache($r);
   76:     &Apache::lonlocal::get_language_handle($r);
   77:     &Apache::loncommon::content_type($r,'text/html');
   78:     if ($expirepub) {
   79:         my $c = new CGI::Cookie(-name    => 'lonID',
   80:                                 -value   => '',
   81:                                 -expires => '-10y',);
   82:         $r->header_out('Set-cookie' => $c);
   83:     } elsif (($handle eq '') && ($userdom ne '')) {
   84:         my $c = new CGI::Cookie(-name    => 'lonID',
   85:                                 -value   => '',
   86:                                 -expires => '-10y',);
   87:         $r->headers_out->add('Set-cookie' => $c);
   88:     }
   89:     $r->send_http_header;
   90:     return OK if $r->header_only;
   91: 
   92: 
   93: # Are we re-routing?
   94:     my $londocroot = $r->dir_config('lonDocRoot'); 
   95:     if (-e "$londocroot/lon-status/reroute.txt") {
   96: 	&Apache::lonauth::reroute($r);
   97: 	return OK;
   98:     }
   99: 
  100:     my $lonhost = $r->dir_config('lonHostID');
  101:     $env{'form.firsturl'} =~ s/(`)/'/g;
  102: 
  103: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  104: 
  105:     my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
  106:     if ($found_server) {
  107:         my $hostname = &Apache::lonnet::hostname($found_server);
  108:         if ($hostname ne '') {
  109:             my $protocol = $Apache::lonnet::protocol{$found_server};
  110:             $protocol = 'http' if ($protocol ne 'https');
  111:             my $dest = '/adm/roles';
  112:             if ($env{'form.firsturl'} ne '') {
  113:                 $dest = $env{'form.firsturl'};
  114:             }
  115:             my %info = (
  116:                          balcookie => $lonhost.':'.$balancer_cookie,
  117:                        );
  118:             my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server);
  119:             if ($balancer_token) {
  120:                 $dest .=  (($dest=~/\?/)?'&;':'?') . 'btoken='.$balancer_token;
  121:             }
  122:             my $url = $protocol.'://'.$hostname.$dest;
  123:             my $start_page =
  124:                 &Apache::loncommon::start_page('Switching Server ...',undef,
  125:                                                {'redirect'       => [0,$url],});
  126:             my $end_page   = &Apache::loncommon::end_page();
  127:             $r->print($start_page.$end_page);
  128:             return OK;
  129:         }
  130:     }
  131: 
  132: #
  133: # Check if a LON-CAPA load balancer sent user here because user's browser sent
  134: # it a balancer cookie for an active session on this server.
  135: #
  136: 
  137:     my $balcookie;
  138:     if ($env{'form.btoken'}) {
  139:         my %info = &Apache::lonnet::tmpget($env{'form.btoken'});
  140:         $balcookie = $info{'balcookie'};
  141:         &Apache::lonnet::tmpdel($env{'form.btoken'});
  142:         delete($env{'form.btoken'});
  143:     }
  144: 
  145: #
  146: # If browser sent an old cookie for which the session file had been removed
  147: # check if configuration for user's domain has a portal URL set.  If so
  148: # switch user's log-in to the portal.
  149: #
  150: 
  151:     if (($handle eq '') && ($userdom ne '')) {
  152:         my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);
  153:         if ($domdefaults{'portal_def'} =~ /^https?\:/) {
  154:             my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  155:                                           {'redirect' => [0,$domdefaults{'portal_def'}],});
  156:             my $end_page   = &Apache::loncommon::end_page();
  157:             $r->print($start_page.$end_page);
  158:             return OK;
  159:         }
  160:     }
  161: 
  162:     $env{'form.firsturl'} =~ s/(`)/'/g;
  163: 
  164: # -------------------------------- Prevent users from attempting to login twice
  165:     if ($handle ne '') {
  166:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  167: 	my $start_page = 
  168: 	    &Apache::loncommon::start_page('Already logged in');
  169: 	my $end_page = 
  170: 	    &Apache::loncommon::end_page();
  171:         my $dest = '/adm/roles';
  172:         if ($env{'form.firsturl'} ne '') {
  173:             $dest = $env{'form.firsturl'}; 
  174:         }
  175: 	$r->print(
  176:               $start_page
  177:              .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  178:              .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
  179:               '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'
  180:              .$end_page
  181:              );
  182:         return OK;
  183:     }
  184: 
  185: # ---------------------------------------------------- No valid token, continue
  186: 
  187: # ---------------------------- Not possible to really login to domain "public"
  188:     if ($env{'form.domain'} eq 'public') {
  189: 	$env{'form.domain'}='';
  190: 	$env{'form.username'}='';
  191:     }
  192: 
  193: # ------ Is this page requested because /adm/migrateuser detected an IP change?
  194:     my %sessiondata;
  195:     if ($env{'form.iptoken'}) {
  196:         %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});
  197:         unless ($sessiondata{'sessionserver'}) {
  198:             my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});
  199:             delete($env{'form.iptoken'});
  200:         }
  201:     }
  202: # ----------------------------------------------------------- Process Interface
  203:     $env{'form.interface'}=~s/\W//g;
  204: 
  205:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
  206:         &Apache::loncommon::decode_user_agent();
  207: 
  208:     my $iconpath= 
  209: 	&Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));
  210: 
  211:     my $domain = &Apache::lonnet::default_login_domain();
  212:     my $defdom = $domain;
  213:     if ($lonhost ne '') {
  214:         unless ($sessiondata{'sessionserver'}) {
  215:             my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie);
  216:             if ($redirect) {
  217:                 $r->print($redirect);
  218:                 return OK;
  219:             }
  220:         }
  221:     }
  222: 
  223:     if (($sessiondata{'domain'}) &&
  224:         (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) {
  225:         $domain=$sessiondata{'domain'};
  226:     } elsif (($env{'form.domain'}) && 
  227: 	(&Apache::lonnet::domain($env{'form.domain'},'description'))) {
  228: 	$domain=$env{'form.domain'};
  229:     }
  230: 
  231:     my $role    = $r->dir_config('lonRole');
  232:     my $loadlim = $r->dir_config('lonLoadLim');
  233:     my $uloadlim= $r->dir_config('lonUserLoadLim');
  234:     my $servadm = $r->dir_config('lonAdmEMail');
  235:     my $tabdir  = $r->dir_config('lonTabDir');
  236:     my $include = $r->dir_config('lonIncludes');
  237:     my $expire  = $r->dir_config('lonExpire');
  238:     my $version = $r->dir_config('lonVersion');
  239:     my $host_name = &Apache::lonnet::hostname($lonhost);
  240: 
  241: # --------------------------------------------- Default values for login fields
  242:     
  243:     my ($authusername,$authdomain);
  244:     if ($sessiondata{'username'}) {
  245:         $authusername=$sessiondata{'username'};
  246:     } else {
  247:         $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});
  248:         $authusername=($env{'form.username'}?$env{'form.username'}:'');
  249:     }
  250:     if ($sessiondata{'domain'}) {
  251:         $authdomain=$sessiondata{'domain'};
  252:     } else {
  253:         $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});
  254:         $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);
  255:     }
  256: 
  257: # ---------------------------------------------------------- Determine own load
  258:     my $loadavg;
  259:     {
  260: 	my $loadfile=Apache::File->new('/proc/loadavg');
  261: 	$loadavg=<$loadfile>;
  262:     }
  263:     $loadavg =~ s/\s.*//g;
  264: 
  265:     my ($loadpercent,$userloadpercent);
  266:     if ($loadlim) {
  267:         $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  268:     }
  269:     if ($uloadlim) {
  270:         $userloadpercent=&Apache::lonnet::userload();
  271:     }
  272: 
  273:     my $firsturl=
  274:     ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});
  275: 
  276: # ----------------------------------------------------------- Get announcements
  277:     my $announcements=&Apache::lonnet::getannounce();
  278: # -------------------------------------------------------- Set login parameters
  279: 
  280:     my @hexstr=('0','1','2','3','4','5','6','7',
  281:                 '8','9','a','b','c','d','e','f');
  282:     my $lkey='';
  283:     for (0..7) {
  284:         $lkey.=$hexstr[rand(15)];
  285:     }
  286: 
  287:     my $ukey='';
  288:     for (0..7) {
  289:         $ukey.=$hexstr[rand(15)];
  290:     }
  291: 
  292:     my $lextkey=hex($lkey);
  293:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
  294: 
  295:     my $uextkey=hex($ukey);
  296:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
  297: 
  298: # -------------------------------------------------------- Store away log token
  299:     my $tokenextras;
  300:     if ($env{'form.role'}) {
  301:         $tokenextras = '&role='.&escape($env{'form.role'});
  302:     }
  303:     if ($env{'form.symb'}) {
  304:         if (!$tokenextras) {
  305:             $tokenextras = '&';
  306:         }
  307:         $tokenextras .= '&symb='.&escape($env{'form.symb'});
  308:     }
  309:     if ($env{'form.iptoken'}) {
  310:         if (!$tokenextras) {
  311:             $tokenextras = '&&';
  312:         }
  313:         $tokenextras .= '&iptoken='.&escape($env{'form.iptoken'});
  314:     }
  315:     my $logtoken=Apache::lonnet::reply(
  316:        'tmpput:'.$ukey.$lkey.'&'.$firsturl.$tokenextras,
  317:        $lonhost);
  318: 
  319: # -- If we cannot talk to ourselves, or hostID does not map to a hostname
  320: #    we are in serious trouble
  321: 
  322:     if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  323:         if ($logtoken eq 'no_such_host') {
  324:             &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');
  325:         }
  326:         my $spares='';
  327: 	my $last;
  328:         foreach my $hostid (sort
  329: 			    {
  330: 				&Apache::lonnet::hostname($a) cmp
  331: 				    &Apache::lonnet::hostname($b);
  332: 			    }
  333: 			    keys(%Apache::lonnet::spareid)) {
  334:             next if ($hostid eq $lonhost);
  335: 	    my $hostname = &Apache::lonnet::hostname($hostid);
  336: 	    next if (($last eq $hostname) || ($hostname eq ''));
  337:             $spares.='<br /><font size="+1"><a href="http://'.
  338:                 $hostname.
  339:                 '/adm/login?domain='.$authdomain.'">'.
  340:                 $hostname.'</a>'.
  341:                 ' '.&mt('(preferred)').'</font>'.$/;
  342: 	    $last=$hostname;
  343:         }
  344:         if ($spares) {
  345:             $spares.= '<br />';
  346:         }
  347:         my %all_hostnames = &Apache::lonnet::all_hostnames();
  348:         foreach my $hostid (sort
  349: 		    {
  350: 			&Apache::lonnet::hostname($a) cmp
  351: 			    &Apache::lonnet::hostname($b);
  352: 		    }
  353: 		    keys(%all_hostnames)) {
  354:             next if ($hostid eq $lonhost || $Apache::lonnet::spareid{$hostid});
  355:             my $hostname = &Apache::lonnet::hostname($hostid);
  356:             next if (($last eq $hostname) || ($hostname eq ''));
  357:             $spares.='<br /><a href="http://'.
  358: 	             $hostname.
  359: 	             '/adm/login?domain='.$authdomain.'">'.
  360: 	             $hostname.'</a>';
  361:             $last=$hostname;
  362:          }
  363:          $r->print(
  364:    '<html>'
  365:   .'<head><title>'
  366:   .&mt('The LearningOnline Network with CAPA')
  367:   .'</title></head>'
  368:   .'<body bgcolor="#FFFFFF">'
  369:   .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
  370:   .'<img src="/adm/lonKaputt/lonlogo_broken.gif" align="right" />'
  371:   .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
  372:         if ($spares) {
  373:             $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
  374:                      .'</p>'
  375:                      .$spares);
  376:         }
  377:         $r->print('</body>'
  378:                  .'</html>'
  379:         );
  380:         return OK;
  381:     }
  382: 
  383: # ----------------------------------------------- Apparently we are in business
  384:     $servadm=~s/\,/\<br \/\>/g;
  385: 
  386: # ----------------------------------------------------------- Front page design
  387:     my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
  388:     my $font=&Apache::loncommon::designparm('login.font',$domain);
  389:     my $link=&Apache::loncommon::designparm('login.link',$domain);
  390:     my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
  391:     my $alink=&Apache::loncommon::designparm('login.alink',$domain);
  392:     my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
  393:     my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
  394:     my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
  395:     my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
  396:     my $logo=&Apache::loncommon::designparm('login.logo',$domain);
  397:     my $img=&Apache::loncommon::designparm('login.img',$domain);
  398:     my $domainlogo=&Apache::loncommon::domainlogo($domain);
  399:     my $showbanner = 1;
  400:     my $showmainlogo = 1;
  401:     if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
  402:         $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
  403:     }
  404:     if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
  405:         $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
  406:     }
  407:     my $showadminmail;
  408:     my @possdoms = &Apache::lonnet::current_machine_domains();
  409:     if (grep(/^\Q$domain\E$/,@possdoms)) {
  410:         $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
  411:     }
  412:     my $showcoursecat =
  413:         &Apache::loncommon::designparm('login.coursecatalog',$domain);
  414:     my $shownewuserlink = 
  415:         &Apache::loncommon::designparm('login.newuser',$domain);
  416:     my $showhelpdesk =
  417:         &Apache::loncommon::designparm('login.helpdesk',$domain);
  418:     my $now=time;
  419:     my $js = (<<ENDSCRIPT);
  420: 
  421: <script type="text/javascript" language="JavaScript">
  422: // <![CDATA[
  423: function send()
  424: {
  425: this.document.server.elements.uname.value
  426: =this.document.client.elements.uname.value;
  427: 
  428: this.document.server.elements.udom.value
  429: =this.document.client.elements.udom.value;
  430: 
  431: uextkey=this.document.client.elements.uextkey.value;
  432: lextkey=this.document.client.elements.lextkey.value;
  433: initkeys();
  434: 
  435: this.document.server.elements.upass0.value
  436:     =getCrypted(this.document.client.elements.upass$now.value);
  437: 
  438: this.document.client.elements.uname.value='';
  439: this.document.client.elements.upass$now.value='';
  440: 
  441: this.document.server.submit();
  442: return false;
  443: }
  444: 
  445: function enableInput() {
  446:     this.document.client.elements.upass$now.removeAttribute("readOnly");
  447:     this.document.client.elements.uname.removeAttribute("readOnly");
  448:     this.document.client.elements.udom.removeAttribute("readOnly");
  449:     return;
  450: }
  451: 
  452: // ]]>
  453: </script>
  454: 
  455: ENDSCRIPT
  456: 
  457: # --------------------------------------------------- Print login screen header
  458: 
  459:     my %add_entries = (
  460: 	       bgcolor      => "$mainbg",
  461: 	       text         => "$font",
  462: 	       link         => "$link",
  463: 	       vlink        => "$vlink",
  464: 	       alink        => "$alink",
  465:                onload       => 'javascript:enableInput();',);
  466: 
  467:     my ($lonhost_in_use,$headextra,$headextra_exempt,@hosts,%defaultdomconf);
  468:     @hosts = &Apache::lonnet::current_machine_ids();
  469:     $lonhost_in_use = $lonhost;
  470:     if (@hosts > 1) {
  471:         foreach my $hostid (@hosts) {
  472:             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
  473:                 $lonhost_in_use = $hostid;
  474:                 last;
  475:             }
  476:         }
  477:     }
  478:     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
  479:     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
  480:     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
  481:     if ($headextra) {
  482:         my $omitextra;
  483:         if ($headextra_exempt ne '') {
  484:             my @exempt = split(',',$headextra_exempt);
  485:             my $ip = $ENV{'REMOTE_ADDR'};
  486:             if (grep(/^\Q$ip\E$/,@exempt)) {
  487:                 $omitextra = 1;
  488:             }
  489:         }
  490:         unless ($omitextra) {
  491:             my $confname = $defdom.'-domainconfig';
  492:             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
  493:                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
  494:                 unless ($extra eq '-1') {
  495:                     $js .= "\n".$extra."\n";
  496:                 }
  497:             }
  498:         }
  499:     }
  500: 
  501:     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
  502: 			       { 'redirect'       => [$expire,'/adm/roles'], 
  503: 				 'add_entries' => \%add_entries,
  504: 				 'only_body'   => 1,}));
  505: 
  506: # ----------------------------------------------------------------------- Texts
  507: 
  508:     my %lt=&Apache::lonlocal::texthash(
  509:           'un'       => 'Username',
  510:           'pw'       => 'Password',
  511:           'dom'      => 'Domain',
  512:           'perc'     => 'percent',
  513:           'load'     => 'Server Load',
  514:           'userload' => 'User Load',
  515:           'catalog'  => 'Course/Community Catalog',
  516:           'log'      => 'Log in',
  517:           'help'     => 'Log-in Help',
  518:           'serv'     => 'Server',
  519:           'servadm'  => 'Server Administration',
  520:           'helpdesk' => 'Contact Helpdesk',
  521:           'forgotpw' => 'Forgot password?',
  522:           'newuser'  => 'New User?',
  523:        );
  524: # -------------------------------------------------- Change password field name
  525: 
  526:     my $forgotpw = &forgotpwdisplay(%lt);
  527:     $forgotpw .= '<br />' if $forgotpw;
  528:     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
  529:     if ($loginhelp) {
  530:         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
  531:     }
  532: 
  533: # ---------------------------------------------------- Serve out DES JavaScript
  534:     {
  535:     my $jsh=Apache::File->new($include."/londes.js");
  536:     $r->print(<$jsh>);
  537:     }
  538: # ---------------------------------------------------------- Serve rest of page
  539: 
  540:     $r->print(
  541:     '<div class="LC_Box"'
  542:    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
  543: );
  544: 
  545:     $r->print(<<ENDSERVERFORM);
  546: <form name="server" action="/adm/authenticate" method="post" target="_top">
  547:    <input type="hidden" name="logtoken" value="$logtoken" />
  548:    <input type="hidden" name="serverid" value="$lonhost" />
  549:    <input type="hidden" name="uname" value="" />
  550:    <input type="hidden" name="upass0" value="" />
  551:    <input type="hidden" name="udom" value="" />
  552:    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
  553:    <input type="hidden" name="localres" value="$env{'form.localres'}" />
  554:   </form>
  555: ENDSERVERFORM
  556:     my $coursecatalog;
  557:     if (($showcoursecat eq '') || ($showcoursecat)) {
  558:         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
  559:     }
  560:     my $newuserlink;
  561:     if ($shownewuserlink) {
  562:         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  563:     }
  564:     my $logintitle =
  565:         '<h2 class="LC_hcell"'
  566:        .' style="background:'.$loginbox_header_bgcol.';'
  567:        .' color:'.$loginbox_header_textcol.'">'
  568:        .$lt{'log'}
  569:        .'</h2>';
  570: 
  571:     my $noscript_warning='<noscript><span class="LC_warning"><b>'
  572:                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
  573:                         .'</b></span></noscript>';
  574:     my $helpdeskscript;
  575:     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
  576:                                        $authdomain,\$helpdeskscript,
  577:                                        $showhelpdesk,\@possdoms);
  578: 
  579:     my $mobileargs;
  580:     if ($clientmobile) {
  581:         $mobileargs = 'autocapitalize="off" autocorrect="off"'; 
  582:     }
  583:     my $loginform=(<<LFORM);
  584: <form name="client" action="" onsubmit="return(send())">
  585:   <input type="hidden" name="lextkey" value="$lextkey" />
  586:   <input type="hidden" name="uextkey" value="$uextkey" />
  587:   <b><label for="uname">$lt{'un'}</label>:</b><br />
  588:   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
  589:   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
  590:   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
  591:   <b><label for="udom">$lt{'dom'}</label>:</b><br />
  592:   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
  593:   <input type="submit" value="$lt{'log'}" />
  594: </form>
  595: LFORM
  596: 
  597:     if ($showbanner) {
  598:         $r->print(<<HEADER);
  599: <!-- The LON-CAPA Header -->
  600: <div style="background:$pgbg;margin:0;width:100%;">
  601:   <img src="$img" border="0" alt="The Learning Online Network with CAPA" class="LC_maxwidth" />
  602: </div>
  603: HEADER
  604:     }
  605:     $r->print(<<ENDTOP);
  606: <div style="float:left;margin-top:0;">
  607: <div class="LC_Box" style="background:$loginbox_bg;">
  608:   $logintitle
  609:   $loginform
  610:   $noscript_warning
  611: </div>
  612:   
  613: <div class="LC_Box" style="padding-top: 10px;">
  614:   $loginhelp
  615:   $forgotpw
  616:   $contactblock
  617:   $newuserlink
  618:   $coursecatalog
  619: </div>
  620: </div>
  621: 
  622: <div>
  623: ENDTOP
  624:     if ($showmainlogo) {
  625:         $r->print(' <img src="'.$logo.'" alt="" class="LC_maxwidth" />'."\n");
  626:     }
  627: $r->print(<<ENDTOP);
  628: $announcements
  629: </div>
  630: <hr style="clear:both;" />
  631: ENDTOP
  632:     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
  633:     $domainrow = <<"END";
  634:       <tr>
  635:        <td  align="left" valign="top">
  636:         <small><b>$lt{'dom'}:&nbsp;</b></small>
  637:        </td>
  638:        <td  align="left" valign="top">
  639:         <small><tt>&nbsp;$domain</tt></small>
  640:        </td>
  641:       </tr>
  642: END
  643:     $serverrow = <<"END";
  644:       <tr>
  645:        <td  align="left" valign="top">
  646:         <small><b>$lt{'serv'}:&nbsp;</b></small>
  647:        </td>
  648:        <td align="left" valign="top">
  649:         <small><tt>&nbsp;$lonhost ($role)</tt></small>
  650:        </td>
  651:       </tr>
  652: END
  653:     if ($loadlim) {
  654:         $loadrow = <<"END";
  655:       <tr>
  656:        <td align="left" valign="top">
  657:         <small><b>$lt{'load'}:&nbsp;</b></small>
  658:        </td>
  659:        <td align="left" valign="top">
  660:         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
  661:        </td>
  662:       </tr>
  663: END
  664:     }
  665:     if ($uloadlim) {
  666:         $userloadrow = <<"END";
  667:       <tr>
  668:        <td align="left" valign="top">
  669:         <small><b>$lt{'userload'}:&nbsp;</b></small>
  670:        </td>
  671:        <td align="left" valign="top">
  672:         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
  673:        </td>
  674:       </tr>
  675: END
  676:     }
  677:     if (($version ne '') && ($version ne '<!-- VERSION -->')) {
  678:         $versionrow = <<"END";
  679:       <tr>
  680:        <td colspan="2" align="left">
  681:         <small>$version</small>
  682:        </td>
  683:       </tr>
  684: END
  685:     }
  686: 
  687:     $r->print(<<ENDDOCUMENT);
  688:     <div style="float: left;">
  689:      <table border="0" cellspacing="0" cellpadding="0">
  690: $domainrow
  691: $serverrow
  692: $loadrow    
  693: $userloadrow
  694: $versionrow
  695:      </table>
  696:     </div>
  697:     <div style="float: right;">
  698:     $domainlogo
  699:     </div>
  700:     <br style="clear:both;" />
  701:  </div>
  702: 
  703: <script type="text/javascript">
  704: // <![CDATA[
  705: // the if prevents the script error if the browser can not handle this
  706: if ( document.client.uname ) { document.client.uname.focus(); }
  707: // ]]>
  708: </script>
  709: $helpdeskscript
  710: 
  711: ENDDOCUMENT
  712:     my %endargs = ( 'noredirectlink' => 1, );
  713:     $r->print(&Apache::loncommon::end_page(\%endargs));
  714:     return OK;
  715: }
  716: 
  717: sub check_loginvia {
  718:     my ($domain,$lonhost,$lonidsdir,$balcookie) = @_;
  719:     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {
  720:         return;
  721:     }
  722:     my %domconfhash = &Apache::loncommon::get_domainconf($domain);
  723:     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
  724:     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
  725:     my $output;
  726:     if ($loginvia ne '') {
  727:         my $noredirect;
  728:         my $ip = $ENV{'REMOTE_ADDR'};
  729:         if ($ip eq '127.0.0.1') {
  730:             $noredirect = 1;
  731:         } else {
  732:             if ($loginvia_exempt ne '') {
  733:                 my @exempt = split(',',$loginvia_exempt);
  734:                 if (grep(/^\Q$ip\E$/,@exempt)) {
  735:                     $noredirect = 1;
  736:                 }
  737:             }
  738:         }
  739:         unless ($noredirect) {
  740:             my ($newhost,$path);
  741:             if ($loginvia =~ /:/) {
  742:                 ($newhost,$path) = split(':',$loginvia);
  743:             } else {
  744:                 $newhost = $loginvia;
  745:             }
  746:             if ($newhost ne $lonhost) {
  747:                 if (&Apache::lonnet::hostname($newhost) ne '') {
  748:                     if ($balcookie) {
  749:                         my ($balancer,$cookie) = split(/:/,$balcookie);
  750:                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {
  751:                             my ($udom,$uname,$cookieid) = ($1,$2,$3);
  752:                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {
  753:                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {
  754:                                     while (my $filename=readdir($dh)) {
  755:                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {
  756:                                             my $handle = $1;
  757:                                             my %hash =
  758:                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,
  759:                                                                                      ['request.balancercookie',
  760:                                                                                       'user.linkedenv']);
  761:                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {
  762:                                                 if (unlink("$lonidsdir/$filename")) {
  763:                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
  764:                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&
  765:                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {
  766:                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");
  767:                                                     }
  768:                                                 }
  769:                                             }
  770:                                             last;
  771:                                         }
  772:                                     }
  773:                                     closedir($dh);
  774:                                 }
  775:                             }
  776:                         }
  777:                     }
  778:                     $output = &redirect_page($newhost,$path);
  779:                 }
  780:             }
  781:         }
  782:     }
  783:     return $output;
  784: }
  785: 
  786: sub redirect_page {
  787:     my ($desthost,$path) = @_;
  788:     my $hostname = &Apache::lonnet::hostname($desthost);
  789:     my $protocol = $Apache::lonnet::protocol{$desthost};
  790:     $protocol = 'http' if ($protocol ne 'https');
  791:     unless ($path =~ m{^/}) {
  792:         $path = '/'.$path;
  793:     }
  794:     my $url = $protocol.'://'.$hostname.$path;
  795:     if ($env{'form.firsturl'} ne '') {
  796:         $url .='?firsturl='.$env{'form.firsturl'};
  797:     }
  798:     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  799:                                                     {'redirect' => [0,$url],});
  800:     my $end_page   = &Apache::loncommon::end_page();
  801:     return $start_page.$end_page;
  802: }
  803: 
  804: sub contactdisplay {
  805:     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
  806:         $possdoms) = @_;
  807:     my $contactblock;
  808:     my $origmail;
  809:     if (ref($possdoms) eq 'ARRAY') {
  810:         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) { 
  811:             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  812:         }
  813:     }
  814:     my $requestmail = 
  815:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
  816:                                                  $authdomain,$origmail);
  817:     unless ($showhelpdesk eq '0') {
  818:         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
  819:             $showhelpdesk = 1;
  820:         } else {
  821:             $showhelpdesk = 0;
  822:         }
  823:     }
  824:     if ($servadm && $showadminmail) {
  825:         $contactblock .= $$lt{'servadm'}.':<br />'.
  826:                          '<tt>'.$servadm.'</tt><br />';
  827:     }
  828:     if ($showhelpdesk) {
  829:         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
  830:         my $thisurl = &escape('/adm/login');
  831:         $$helpdeskscript = <<"ENDSCRIPT";
  832: <script type="text/javascript">
  833: // <![CDATA[
  834: function helpdesk() {
  835:     var possdom = document.client.udom.value;
  836:     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
  837:     if (codedom == '') {
  838:         codedom = "$authdomain";
  839:     }
  840:     var querystr = "origurl=$thisurl&codedom="+codedom;
  841:     document.location.href = "/adm/helpdesk?"+querystr;
  842:     return;
  843: }
  844: // ]]>
  845: </script>
  846: ENDSCRIPT
  847:     }
  848:     return $contactblock;
  849: }
  850: 
  851: sub forgotpwdisplay {
  852:     my (%lt) = @_;
  853:     my $prompt_for_resetpw = 1; 
  854:     if ($prompt_for_resetpw) {
  855:         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
  856:     }
  857:     return;
  858: }
  859: 
  860: sub coursecatalog_link {
  861:     my ($linkname) = @_;
  862:     return <<"END";
  863:       <a href="/adm/coursecatalog">$linkname</a>
  864: END
  865: }
  866: 
  867: sub newuser_link {
  868:     my ($linkname) = @_;
  869:     return '<a href="/adm/createaccount">'.$linkname.'</a>';
  870: }
  871: 
  872: 1;
  873: __END__

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