File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue May 8 15:31:22 2018 UTC (5 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Use internal_redirect to redirect to resource pointed to by a tiny URL, except
  when authenticated via LTI.

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.23 2018/05/08 15:31:22 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 :http);
   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{^/+tiny/+($match_domain)/+(\w+)$}) {
   42:         my ($cdom,$key) = ($1,$2);
   43:         if (&Apache::lonnet::domain($cdom) ne '') {
   44:             my %user;
   45:             my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
   46:             if ($handle ne '') {
   47:                 my $lonidsdir=$r->dir_config('lonIDsDir');
   48:                 &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   49:                 if ($env{'request.course.id'}) {
   50:                     my $tinyurl;
   51:                     my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
   52:                     if (defined($cached)) {
   53:                         $tinyurl = $result;
   54:                     } else {
   55:                         my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
   56:                         my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
   57:                         if ($currtiny{$key} ne '') {
   58:                             $tinyurl = $currtiny{$key};
   59:                             &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
   60:                         }
   61:                     }
   62:                     if ($tinyurl ne '') {
   63:                         my ($cnum,$symb) = split(/\&/,$tinyurl);
   64:                         if (($cnum =~ /^$match_courseid$/) &&
   65:                             (&Apache::lonnet::homeserver($cnum,$cdom) ne 'no_host')) {
   66:                             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
   67:                                 my ($map,$resid,$url) = &Apache::lonnet::decode_symb($symb);
   68:                                 if (&Apache::lonnet::is_on_map($url)) {
   69:                                     my $realuri;
   70:                                     if ((&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) &&
   71:                                         (!$env{'request.role.adv'})) {
   72:                                         $env{'user.error.msg'}=$r->uri.':bre:1:1:Access to resource denied';
   73:                                         return HTTP_NOT_ACCEPTABLE;
   74:                                     }
   75:                                     if ((&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) &&
   76:                                         (!$env{'request.role.adv'})) {
   77:                                         $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
   78:                                         if (($url =~ /\.sequence$/) &&
   79:                                             ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
   80:                                             $realuri .= '?navmap=1';
   81:                                         } else {
   82:                                             $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
   83:                                         }
   84:                                     } else {
   85:                                         $realuri = &Apache::lonnet::clutter($url);
   86:                                         if (($url =~ /\.sequence$/) &&
   87:                                             ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
   88:                                             $realuri .= '?navmap=1'; 
   89:                                         } else {
   90:                                             $realuri .= '?symb='.$symb;
   91:                                         }
   92:                                     }
   93:                                     my $host = $r->headers_in->get('Host');
   94:                                     if ($r->is_initial_req() || !$host) {
   95:                                         $r->internal_redirect($realuri);
   96:                                         return OK;
   97:                                     } else {
   98:                                         my $protocol = 'http';
   99:                                         if ($r->get_server_port == 443) {
  100:                                             $protocol = 'https';
  101:                                         }
  102:                                         my $location = $protocol.'://'.$host.$realuri;
  103:                                         $r->headers_out->set(Location => $location);
  104:                                         return REDIRECT;
  105:                                     }
  106:                                 }
  107:                             }
  108:                         }
  109:                     }
  110:                 }
  111:             }
  112:         }
  113:     } elsif ($r->uri=~m{^/raw/}) {
  114:         my $host = $r->headers_in->get('Host');
  115:         if ($host) {
  116:             unless ($host =~ /^internal\-/) {
  117:                 my $remote_ip = $r->get_remote_host();
  118:                 my $lonhost = $r->dir_config('lonHostID');
  119:                 if (&redirect_raw($remote_ip,$lonhost)) {
  120:                     my $location = 'https://internal-'.$host.$r->uri;
  121:                     $r->headers_out->set(Location => $location);
  122:                     return REDIRECT;
  123:                 }
  124:             }
  125:         }
  126:     }
  127:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
  128:         my $fn = $r->uri();
  129:         $fn=~s/^\/raw//;
  130:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
  131: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  132:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  133: 	my $allowed=0;
  134: 	my @ids=&Apache::lonnet::current_machine_ids();
  135: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
  136: 	if ($allowed) {
  137: 	    $r->filename(&propath($udom,$uname).
  138: 			 '/userfiles/'.(join('/',@ufile)));
  139:         }
  140:         return OK;
  141:     } else {
  142:         return DECLINED;
  143:     }
  144: }
  145: 
  146: sub redirect_raw {
  147:     my ($remote_ip,$lonhost) = @_;
  148:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
  149:     my $redirect;
  150:     while (@remhostids) {
  151:         my $try_server = pop(@remhostids);
  152:         my $remhostname = &Apache::lonnet::hostname($try_server);
  153:         if ($remhostname) {
  154:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
  155:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
  156:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
  157:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
  158:                 if (ref($internet_names) eq 'ARRAY') {
  159:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
  160:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
  161:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
  162:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
  163:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  164:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  165:                         my $replication = $domdefaults{'replication'};
  166:                         if (ref($replication) eq 'HASH') {
  167:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
  168:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
  169:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
  170:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
  171:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
  172:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
  173:                                     $redirect = 1;
  174:                                 } else {
  175:                                     $redirect = 0;
  176:                                 }
  177:                             }
  178:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  179:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  180:                                     $redirect = 0;
  181:                                 } else {
  182:                                     $redirect = 1;
  183:                                 }
  184:                             }
  185:                         }
  186:                     }
  187:                 }
  188:             }
  189:             last;
  190:         }
  191:     }
  192:     return $redirect;  
  193: }
  194: 
  195: 1;
  196: __END__
  197: 

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