File:  [LON-CAPA] / loncom / auth / switchserver.pm
Revision 1.6: download - view: text, annotated - select for diffs
Wed Dec 28 16:46:47 2005 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, HEAD
- BUG #4524 swtchserver works in a backward to 2.0 and before manner

    1: # The LearningOnline Network
    2: # Switch Servers Handler
    3: #
    4: # $Id: switchserver.pm,v 1.6 2005/12/28 16:46:47 albertel 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::switchserver;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::lonnet;
   34: use Apache::lonmenu;
   35: use CGI::Cookie();
   36: use Apache::lonlocal;
   37: 
   38: sub init_env {
   39:     my ($r) = @_;
   40:     
   41:     if (-e $env{'user.environment'}) {
   42: 	return  $env{'user.environment'};
   43:     }
   44:     my $requrl=$r->uri;
   45:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   46:     my $lonid=$cookies{'lonID'};
   47:     my $cookie;
   48:     if (!$lonid) { return undef; }
   49: 
   50:     my $handle=$lonid->value;
   51:     $handle=~s/\W//g;
   52:     my $lonidsdir=$r->dir_config('lonIDsDir');
   53:     if ((!-e "$lonidsdir/$handle.id") || ($handle eq '')) {
   54: 	$r->log_reason("Cookie $handle not valid", $r->filename); 
   55: 	return undef;
   56:     }
   57: 
   58:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   59:     
   60:     return $r->dir_config('lonIDsDir')."/$handle.id";
   61: }
   62: 
   63: sub handler {
   64:     my ($r) = @_;
   65:     
   66:     my $handle=&init_env($r);
   67:     if (!defined($handle)) { return FORBIDDEN; }
   68: 
   69:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   70: 					    ['otherserver','role']);
   71: 
   72:     my $switch_to=$Apache::lonnet::hostname{$env{'form.otherserver'}};
   73:     if (!$env{'form.otherserver'}) {
   74: 	$env{'form.otherserver'}=&Apache::lonnet::spareserver(30000,undef,1);
   75: 	$switch_to=$Apache::lonnet::hostname{$env{'form.otherserver'}};
   76:     }
   77: 
   78:     if (!defined($switch_to)) { return FORBIDDEN; }
   79: 
   80:     if ($env{'form.role'} && 
   81: 	!exists($env{'user.role.'.$env{'form.role'}})) { return FORBIDDEN; }
   82: 
   83:     #remove session env, and log event
   84:     unlink($handle);
   85:     my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
   86: 	      $env{'form.role'});
   87:     &Apache::lonnet::put('email_status',\%temp);
   88:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
   89: 			 $env{'user.home'},
   90: 			 "Switch Server to $env{'form.otherserver'} with role $env{'form.role'} $ENV{'REMOTE_ADDR'}");
   91: 
   92:     &Apache::loncommon::content_type($r,'text/html');
   93:     $r->send_http_header;
   94:     return OK if $r->header_only;
   95: # -------------------------------------------------------- Menu script and info
   96: 	   
   97:     my $windowinfo=&Apache::lonmenu::close();
   98:     $windowinfo.=&Apache::lonnavmaps::close();
   99: # ---------------------------------------------------------------- Get handover
  100: 
  101:     my %info=('ip'       => $ENV{'REMOTE_ADDR'},
  102: 	      'domain'   => $env{'user.domain'},
  103: 	      'username' => $env{'user.name'},
  104: 	      'role'     => $env{'form.role'},
  105: 	      'server'   => $r->dir_config('lonHostID'));
  106:     my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
  107:     my $switch='<meta HTTP-EQUIV="Refresh" CONTENT="0.5; url=http://'.$switch_to.'/adm/login?domain='.$env{'user.domain'}.'&username='.$env{'user.name'}.'&token='.$token.'">';
  108:     my $bodytag=&Apache::loncommon::bodytag('Switching Server ...');
  109: # --------------------------------------------------------------- Screen Output
  110:     $r->print(<<ENDDOCUMENT);
  111: 	   <html>
  112: 	       <head><title>The LearningOnline Network with CAPA Logout</title>
  113: 	       $switch
  114: 	       </head>
  115: 	       $bodytag
  116: 	       $windowinfo
  117: 	       </body>
  118: 	       </html>
  119: ENDDOCUMENT
  120: 
  121:     &Apache::lonnet::flushcourselogs();
  122:     return OK;
  123: }
  124: 
  125: 1;
  126: __END__
  127: 
  128: 
  129: 
  130: 
  131: 
  132: 
  133: 
  134: 

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