File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.18: download - view: text, annotated - select for diffs
Fri Jan 12 13:34:08 2018 UTC (6 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6400 Tiny URL for deep-linking.
  Requires Short::URL and String::CRC32 modules from CPAN. Dependency on
  perl-Short-URL needs to be added to LONCAPA-prerequisites.

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

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