File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.16: download - view: text, annotated - select for diffs
Tue Aug 16 20:17:54 2016 UTC (7 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support Apache 2.4 using $r->get_remote_host() from Apache2::compat().

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.16 2016/08/16 20:17:54 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::lontrans;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :remotehost REDIRECT);
   33: use Apache::lonnet();
   34: use Apache::File();
   35: use LONCAPA qw(:DEFAULT :match);
   36: 
   37: sub handler {
   38:     my $r = shift;
   39:     # FIXME line remove when mod_perl fixes BUG#4948 
   40:     $r->notes->set('error-notes' => '');
   41:     if ($r->uri=~m{^/raw/}) {
   42:         my $host = $r->headers_in->get('Host');
   43:         if ($host) {
   44:             unless ($host =~ /^internal\-/) {
   45:                 my $remote_ip = $r->get_remote_host();
   46:                 my $lonhost = $r->dir_config('lonHostID');
   47:                 if (&redirect_raw($remote_ip,$lonhost)) {
   48:                     my $location = 'https://internal-'.$host.$r->uri;
   49:                     $r->headers_out->set(Location => $location);
   50:                     return REDIRECT;
   51:                 }
   52:             }
   53:         }
   54:     }
   55:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
   56:         my $fn = $r->uri();
   57:         $fn=~s/^\/raw//;
   58:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
   59: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
   60:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
   61: 	my $allowed=0;
   62: 	my @ids=&Apache::lonnet::current_machine_ids();
   63: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
   64: 	if ($allowed) {
   65: 	    $r->filename(&propath($udom,$uname).
   66: 			 '/userfiles/'.(join('/',@ufile)));
   67:         }
   68:         return OK;
   69:     } else { 
   70:         return DECLINED;
   71:     }
   72: }
   73: 
   74: sub redirect_raw {
   75:     my ($remote_ip,$lonhost) = @_;
   76:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
   77:     my $redirect;
   78:     while (@remhostids) {
   79:         my $try_server = pop(@remhostids);
   80:         my $remhostname = &Apache::lonnet::hostname($try_server);
   81:         if ($remhostname) {
   82:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
   83:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
   84:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
   85:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
   86:                 if (ref($internet_names) eq 'ARRAY') {
   87:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
   88:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
   89:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
   90:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
   91:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
   92:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
   93:                         my $replication = $domdefaults{'replication'};
   94:                         if (ref($replication) eq 'HASH') {
   95:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
   96:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
   97:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
   98:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
   99:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
  100:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
  101:                                     $redirect = 1;
  102:                                 } else {
  103:                                     $redirect = 0;
  104:                                 }
  105:                             }
  106:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  107:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  108:                                     $redirect = 0;
  109:                                 } else {
  110:                                     $redirect = 1;
  111:                                 }
  112:                             }
  113:                         }
  114:                     }
  115:                 }
  116:             }
  117:             last;
  118:         }
  119:     }
  120:     return $redirect;  
  121: }
  122: 
  123: 1;
  124: __END__
  125: 
  126: 
  127: 
  128: 
  129: 
  130: 
  131: 

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