Annotation of loncom/lontrans.pm, revision 1.19
1.1 www 1: # The LearningOnline Network
2: # URL translation for User Files
3: #
1.19 ! raeburn 4: # $Id: lontrans.pm,v 1.18 2018/01/12 13:34:08 raeburn Exp $
1.1 www 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;
1.18 raeburn 32: use Apache::Constants qw(:common :remotehost REDIRECT :http);
33: use Apache::lonnet;
1.1 www 34: use Apache::File();
1.15 raeburn 35: use LONCAPA qw(:DEFAULT :match);
1.2 www 36:
1.1 www 37: sub handler {
38: my $r = shift;
1.12 albertel 39: # FIXME line remove when mod_perl fixes BUG#4948
40: $r->notes->set('error-notes' => '');
1.17 raeburn 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;
1.18 raeburn 49: } elsif ($realuri =~ m{^tiny/$match_domain/\w+$}) {
50: $realuri = '/'.$realuri;
1.17 raeburn 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: }
1.18 raeburn 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'})) {
1.19 ! raeburn 106: $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
! 107: if (($url =~ /\.sequence$/) &&
! 108: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
! 109: $realuri .= '?navmap=1';
! 110: } else {
! 111: $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
! 112: }
1.18 raeburn 113: } else {
1.19 ! raeburn 114: $realuri = &Apache::lonnet::clutter($url);
! 115: if (($url =~ /\.sequence$/) &&
! 116: ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
! 117: $realuri .= '?navmap=1';
! 118: } else {
! 119: $realuri .= '?symb='.$symb;
! 120: }
1.18 raeburn 121: }
122: my $host = $r->headers_in->get('Host');
123: if ($host) {
124: my $protocol = 'http';
125: if ($r->get_server_port == 443) {
126: $protocol = 'https';
127: }
128: my $location = $protocol.'://'.$host.$realuri;
129: $r->headers_out->set(Location => $location);
130: return REDIRECT;
131: }
132: }
133: }
134: }
135: }
136: }
137: }
138: }
1.17 raeburn 139: } elsif ($r->uri=~m{^/raw/}) {
1.15 raeburn 140: my $host = $r->headers_in->get('Host');
141: if ($host) {
142: unless ($host =~ /^internal\-/) {
1.16 raeburn 143: my $remote_ip = $r->get_remote_host();
144: my $lonhost = $r->dir_config('lonHostID');
145: if (&redirect_raw($remote_ip,$lonhost)) {
146: my $location = 'https://internal-'.$host.$r->uri;
147: $r->headers_out->set(Location => $location);
148: return REDIRECT;
1.15 raeburn 149: }
150: }
151: }
152: }
1.7 raeburn 153: if ($r->uri=~m|^(/raw)?/uploaded/|) {
154: my $fn = $r->uri();
155: $fn=~s/^\/raw//;
156: my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
1.10 albertel 157: if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7 raeburn 158: my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9 albertel 159: my $allowed=0;
160: my @ids=&Apache::lonnet::current_machine_ids();
161: foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
162: if ($allowed) {
1.13 albertel 163: $r->filename(&propath($udom,$uname).
164: '/userfiles/'.(join('/',@ufile)));
1.7 raeburn 165: }
1.15 raeburn 166: return OK;
1.18 raeburn 167: } else {
1.15 raeburn 168: return DECLINED;
169: }
170: }
171:
172: sub redirect_raw {
173: my ($remote_ip,$lonhost) = @_;
174: my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
175: my $redirect;
176: while (@remhostids) {
177: my $try_server = pop(@remhostids);
178: my $remhostname = &Apache::lonnet::hostname($try_server);
179: if ($remhostname) {
180: my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
181: my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
182: if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
183: my $internet_names = &Apache::lonnet::get_internet_names($try_server);
184: if (ref($internet_names) eq 'ARRAY') {
185: my $intdom = &Apache::lonnet::internet_dom($lonhost);
186: unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
187: my $lonhostname = &Apache::lonnet::hostname($lonhost);
188: my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
189: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
190: my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
191: my $replication = $domdefaults{'replication'};
192: if (ref($replication) eq 'HASH') {
193: my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
194: my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
195: my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
196: my $remintdom = &Apache::lonnet::internet_dom($remprimary);
197: if (ref($replication->{'certreq'}) eq 'ARRAY') {
198: if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
199: $redirect = 1;
200: } else {
201: $redirect = 0;
202: }
203: }
204: if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
205: if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
206: $redirect = 0;
207: } else {
208: $redirect = 1;
209: }
210: }
211: }
212: }
213: }
214: }
215: last;
216: }
217: }
218: return $redirect;
1.1 www 219: }
220:
221: 1;
222: __END__
223:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>