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