File:
[LON-CAPA] /
loncom /
lonenc.pm
Revision
1.20:
download - view:
text,
annotated -
select for diffs
Wed Dec 27 17:11:08 2006 UTC (17 years, 9 months ago) by
albertel
Branches:
MAIN
CVS tags:
version_2_4_X,
version_2_4_2,
version_2_4_1,
version_2_4_0,
version_2_3_X,
version_2_3_99_0,
version_2_3_2,
version_2_3_1,
version_2_3_0,
HEAD
- no need to another get on the environment coursedescription already got it
1: # The LearningOnline Network
2: # URL translation for encrypted filenames
3: #
4: # $Id: lonenc.pm,v 1.20 2006/12/27 17:11:08 albertel 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::lonenc;
30:
31: use strict;
32: use Apache::lonnet;
33: use Crypt::IDEA;
34: use Time::HiRes qw(gettimeofday);
35: use LONCAPA;
36:
37: sub encryptseed {
38: my ($cid) = @_;
39: if (!defined($cid)) {
40: $cid = $env{'request.course.id'};
41: }
42: my $seed;
43: if (defined($cid)) {
44: if (defined$env{'course.'.$cid.'.internal.encseed'}) {
45: $seed = $env{'course.'.$cid.'.internal.encseed'};
46: } else {
47: my %descargs = ( 'one_time' => 1);
48: my %course =
49: &Apache::lonnet::coursedescription($cid,\%descargs);
50: $seed = $course{'internal.encseed'};
51: }
52: }
53: if (defined($seed)) {
54: $seed=~s/[^0-9a-f]/0/g;
55: $seed.='0123456789abcdef';
56: $seed=substr($seed.$seed,0,32);
57: return pack("H32",$seed);
58: } else {
59: return;
60: }
61: }
62:
63: sub unencrypted {
64: my ($uri,$cid) = @_;
65: $uri=~s/^\/enc\/(\d+)\///;
66: my $cmdlength=$1;
67: # strip any added extension
68: $uri=~s/\.[^.]*//;
69: my $seed=&encryptseed($cid);
70: unless ($seed) {
71: return '/'.$uri;
72: }
73: $uri=&unescape($uri);
74: my $cipher=new IDEA $seed;
75: my $decuri='';
76: for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
77: $decuri.=$cipher->decrypt(
78: pack("H16",substr($uri,$encidx,16))
79: );
80: }
81: $env{'request.enc'}=1;
82: $decuri=&remove_noise($decuri);
83: return substr($decuri,0,$cmdlength);
84: }
85:
86: # add a randomish character after every 4th caharacter
87: sub add_noise {
88: my ($uri)=@_;
89: my @noise=split(/(.)/,(&gettimeofday())[1]);
90: my $noisy;
91: my $i;
92: foreach my $chunk (split(/(....)/,$uri)) {
93: $noisy.=$chunk;
94: $noisy.=$noise[($i++)%(scalar@noise)];
95: }
96: return $noisy;
97: }
98:
99: # remove every fifth character
100: sub remove_noise {
101: my ($uri)=@_;
102: my $clean;
103: foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
104: return $clean;
105: }
106:
107: sub encrypted {
108: my ($uri,$force_enc) = @_;
109: if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
110: my $seed=&encryptseed();
111: unless ($seed) {
112: return $uri;
113: }
114: my $cmdlength=length($uri);
115: # add noise before enc so that that same url's look different
116: $uri=&add_noise($uri);
117: my $noiselength=length($uri);
118: $uri.=time;
119: my $encuri='';
120: my $cipher=new IDEA $seed;
121: for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
122: $encuri.=unpack("H16",
123: $cipher->encrypt(substr($uri,$encidx,8)));
124: }
125: return '/enc/'.$cmdlength.'/'.&escape($encuri);
126: }
127:
128: sub check_encrypt {
129: my $str=shift;
130: if (ref($str)) {
131: if ($env{'request.enc'}) { $$str = &Apache::lonenc::encrypted($$str); }
132: return;
133: } else {
134: if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
135: }
136: return $str;
137: }
138:
139: sub check_decrypt {
140: my ($str)=@_;
141: if (ref($str)) {
142: if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
143: return;
144: }
145: if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
146: return $str;
147: }
148:
149: sub encrypt_ref {
150: my ($token,$elements,$force_enc)=@_;
151: my $html;
152: if ($force_enc || $env{'request.enc'}) {
153: while (my ($name,$value)= each(%{ $elements })) {
154: if (!$value) { next; }
155: my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
156: if ($href !~ /^http:/) {
157: # IE really wants an extension
158: my ($extension) = ($href =~ m/(\.[^.]*)$/);
159: $href = &Apache::lonenc::encrypted($href,$force_enc);
160: $href .= $extension;
161: }
162: $token->[2]->{$name}=$href;
163: }
164: delete($token->[2]->{'encrypturl'});
165: $html = &Apache::edit::rebuild_tag($token);
166: } else {
167: $html = $token->[4];
168: }
169: return $html;
170: }
171: 1;
172: __END__
173:
174:
175:
176:
177:
178:
179:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>