File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.21: download - view: text, annotated - select for diffs
Tue Apr 17 14:02:56 2018 UTC (6 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LON-CAPA as LTI Provider
  - LTI launch can be for a published .sequence or .page
  - LTI launch can be for a composite page.

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

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