File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.20: download - view: text, annotated - select for diffs
Sat Apr 14 02:29:51 2018 UTC (6 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LON-CAPA as LTI Provider
  - Original LTI launch of LON-CAPA is for a resource, a map, or an entire
    course, and display is for iframe, tab or window; retain this in %env.

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

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