File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Nov 30 15:14:58 2017 UTC (6 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LTI Integration: LON-CAPA as LTI Provider
  - Work in progress

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.17 2017/11/30 15:14:58 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{^/adm/lti/(.+)$}) {
   42:         my $realuri = $1;
   43:         my %user;
   44:         my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
   45:         if (($handle ne '') && ($user{'lti'})) {
   46:             if ($realuri =~ m{^uploaded/$match_domain/$match_courseid/(default|supplemental)(|_\d+)\.(?:sequence|page)___\d+___.+$}) {
   47:                 my ($map,$resid,$url) = split(/___/,$realuri);
   48:                 $realuri = &Apache::lonnet::clutter($url).'?symb='.$realuri;
   49:             } elsif ($realuri =~ m{($match_domain)/($match_courseid)$}) {
   50:                 $realuri = '/adm/navmaps';
   51:             } else {
   52:                 $realuri = '/'.$realuri;
   53:                 if ($realuri =~ m{/default_\d+\.sequence$}) {
   54:                     $realuri .= (($realuri =~/\?/)?'&':'?').'navmap=1';
   55:                 }
   56:             }
   57:             my $host = $r->headers_in->get('Host');
   58:             if ($host) {
   59:                 my $protocol = 'http';
   60:                 if ($r->get_server_port == 443) {
   61:                     $protocol = 'https';
   62:                 }
   63:                 my $location = $protocol.'://'.$host.$realuri;
   64:                 $r->headers_out->set(Location => $location);
   65:                 return REDIRECT;
   66:             }
   67:         }
   68:     } elsif ($r->uri=~m{^/raw/}) {
   69:         my $host = $r->headers_in->get('Host');
   70:         if ($host) {
   71:             unless ($host =~ /^internal\-/) {
   72:                 my $remote_ip = $r->get_remote_host();
   73:                 my $lonhost = $r->dir_config('lonHostID');
   74:                 if (&redirect_raw($remote_ip,$lonhost)) {
   75:                     my $location = 'https://internal-'.$host.$r->uri;
   76:                     $r->headers_out->set(Location => $location);
   77:                     return REDIRECT;
   78:                 }
   79:             }
   80:         }
   81:     }
   82:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
   83:         my $fn = $r->uri();
   84:         $fn=~s/^\/raw//;
   85:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
   86: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
   87:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
   88: 	my $allowed=0;
   89: 	my @ids=&Apache::lonnet::current_machine_ids();
   90: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
   91: 	if ($allowed) {
   92: 	    $r->filename(&propath($udom,$uname).
   93: 			 '/userfiles/'.(join('/',@ufile)));
   94:         }
   95:         return OK;
   96:     } else { 
   97:         return DECLINED;
   98:     }
   99: }
  100: 
  101: sub redirect_raw {
  102:     my ($remote_ip,$lonhost) = @_;
  103:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
  104:     my $redirect;
  105:     while (@remhostids) {
  106:         my $try_server = pop(@remhostids);
  107:         my $remhostname = &Apache::lonnet::hostname($try_server);
  108:         if ($remhostname) {
  109:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
  110:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
  111:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
  112:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
  113:                 if (ref($internet_names) eq 'ARRAY') {
  114:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
  115:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
  116:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
  117:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
  118:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  119:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  120:                         my $replication = $domdefaults{'replication'};
  121:                         if (ref($replication) eq 'HASH') {
  122:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
  123:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
  124:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
  125:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
  126:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
  127:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
  128:                                     $redirect = 1;
  129:                                 } else {
  130:                                     $redirect = 0;
  131:                                 }
  132:                             }
  133:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  134:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  135:                                     $redirect = 0;
  136:                                 } else {
  137:                                     $redirect = 1;
  138:                                 }
  139:                             }
  140:                         }
  141:                     }
  142:                 }
  143:             }
  144:             last;
  145:         }
  146:     }
  147:     return $redirect;  
  148: }
  149: 
  150: 1;
  151: __END__
  152: 
  153: 
  154: 
  155: 
  156: 
  157: 
  158: 

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