Annotation of loncom/lontrans.pm, revision 1.21
1.1 www 1: # The LearningOnline Network
2: # URL translation for User Files
3: #
1.21 ! raeburn 4: # $Id: lontrans.pm,v 1.20 2018/04/14 02:29:51 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+___.+$}) {
1.20 raeburn 47: if ($user{'ltiuri'} ne $realuri) {
48: &Apache::lonnet::appenv({'request.lti.uri' => $realuri});
49: }
1.17 raeburn 50: my ($map,$resid,$url) = split(/___/,$realuri);
51: $realuri = &Apache::lonnet::clutter($url).'?symb='.$realuri;
1.21 ! raeburn 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;
1.18 raeburn 58: } elsif ($realuri =~ m{^tiny/$match_domain/\w+$}) {
1.20 raeburn 59: $realuri = '/'.$realuri;
60: if ($user{'ltiuri'} ne $realuri) {
61: &Apache::lonnet::appenv({'request.lti.uri' => $realuri});
62: }
1.17 raeburn 63: } elsif ($realuri =~ m{($match_domain)/($match_courseid)$}) {
64: $realuri = '/adm/navmaps';
1.20 raeburn 65: if ($user{'ltiuri'} ne '') {
66: &Apache::lonnet::delenv('request.lti.uri');
67: }
1.17 raeburn 68: } else {
69: $realuri = '/'.$realuri;
70: if ($realuri =~ m{/default_\d+\.sequence$}) {
1.20 raeburn 71: if ($user{'ltiuri'} ne $realuri) {
72: &Apache::lonnet::appenv({'request.lti.uri' => $realuri});
73: }
1.17 raeburn 74: $realuri .= (($realuri =~/\?/)?'&':'?').'navmap=1';
1.21 ! raeburn 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: }
1.17 raeburn 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: }
1.18 raeburn 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'})) {
1.19 raeburn 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: }
1.18 raeburn 140: } else {
1.19 raeburn 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: }
1.18 raeburn 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: }
1.17 raeburn 166: } elsif ($r->uri=~m{^/raw/}) {
1.15 raeburn 167: my $host = $r->headers_in->get('Host');
168: if ($host) {
169: unless ($host =~ /^internal\-/) {
1.16 raeburn 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;
1.15 raeburn 176: }
177: }
178: }
179: }
1.7 raeburn 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);
1.10 albertel 184: if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7 raeburn 185: my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9 albertel 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) {
1.13 albertel 190: $r->filename(&propath($udom,$uname).
191: '/userfiles/'.(join('/',@ufile)));
1.7 raeburn 192: }
1.15 raeburn 193: return OK;
1.18 raeburn 194: } else {
1.15 raeburn 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;
1.1 www 246: }
247:
248: 1;
249: __END__
250:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>