File:
[LON-CAPA] /
loncom /
lonenc.pm
Revision
1.19:
download - view:
text,
annotated -
select for diffs
Sun Dec 24 21:22:33 2006 UTC (17 years, 9 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
Allow &encryptseed() to take an optional argument - a courseID, and to retrieve the course's internal.encseed when it is not currently available in %env. Used to perform decryption on encrypted URLs included in the baseurl of feedback messages, if the message is read when the corresponding course role is unselected.
1: # The LearningOnline Network
2: # URL translation for encrypted filenames
3: #
4: # $Id: lonenc.pm,v 1.19 2006/12/24 21:22:33 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::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: my %seedhash =
51: &Apache::lonnet::get('environment',['internal.encseed'],
52: $course{'domain'},$course{'num'});
53: $seed = $seedhash{'internal.encseed'};
54: }
55: }
56: if (defined($seed)) {
57: $seed=~s/[^0-9a-f]/0/g;
58: $seed.='0123456789abcdef';
59: $seed=substr($seed.$seed,0,32);
60: return pack("H32",$seed);
61: } else {
62: return;
63: }
64: }
65:
66: sub unencrypted {
67: my ($uri,$cid) = @_;
68: $uri=~s/^\/enc\/(\d+)\///;
69: my $cmdlength=$1;
70: # strip any added extension
71: $uri=~s/\.[^.]*//;
72: my $seed=&encryptseed($cid);
73: unless ($seed) {
74: return '/'.$uri;
75: }
76: $uri=&unescape($uri);
77: my $cipher=new IDEA $seed;
78: my $decuri='';
79: for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
80: $decuri.=$cipher->decrypt(
81: pack("H16",substr($uri,$encidx,16))
82: );
83: }
84: $env{'request.enc'}=1;
85: $decuri=&remove_noise($decuri);
86: return substr($decuri,0,$cmdlength);
87: }
88:
89: # add a randomish character after every 4th caharacter
90: sub add_noise {
91: my ($uri)=@_;
92: my @noise=split(/(.)/,(&gettimeofday())[1]);
93: my $noisy;
94: my $i;
95: foreach my $chunk (split(/(....)/,$uri)) {
96: $noisy.=$chunk;
97: $noisy.=$noise[($i++)%(scalar@noise)];
98: }
99: return $noisy;
100: }
101:
102: # remove every fifth character
103: sub remove_noise {
104: my ($uri)=@_;
105: my $clean;
106: foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
107: return $clean;
108: }
109:
110: sub encrypted {
111: my ($uri,$force_enc) = @_;
112: if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
113: my $seed=&encryptseed();
114: unless ($seed) {
115: return $uri;
116: }
117: my $cmdlength=length($uri);
118: # add noise before enc so that that same url's look different
119: $uri=&add_noise($uri);
120: my $noiselength=length($uri);
121: $uri.=time;
122: my $encuri='';
123: my $cipher=new IDEA $seed;
124: for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
125: $encuri.=unpack("H16",
126: $cipher->encrypt(substr($uri,$encidx,8)));
127: }
128: return '/enc/'.$cmdlength.'/'.&escape($encuri);
129: }
130:
131: sub check_encrypt {
132: my $str=shift;
133: if (ref($str)) {
134: if ($env{'request.enc'}) { $$str = &Apache::lonenc::encrypted($$str); }
135: return;
136: } else {
137: if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
138: }
139: return $str;
140: }
141:
142: sub check_decrypt {
143: my ($str)=@_;
144: if (ref($str)) {
145: if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
146: return;
147: }
148: if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
149: return $str;
150: }
151:
152: sub encrypt_ref {
153: my ($token,$elements,$force_enc)=@_;
154: my $html;
155: if ($force_enc || $env{'request.enc'}) {
156: while (my ($name,$value)= each(%{ $elements })) {
157: if (!$value) { next; }
158: my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
159: if ($href !~ /^http:/) {
160: # IE really wants an extension
161: my ($extension) = ($href =~ m/(\.[^.]*)$/);
162: $href = &Apache::lonenc::encrypted($href,$force_enc);
163: $href .= $extension;
164: }
165: $token->[2]->{$name}=$href;
166: }
167: delete($token->[2]->{'encrypturl'});
168: $html = &Apache::edit::rebuild_tag($token);
169: } else {
170: $html = $token->[4];
171: }
172: return $html;
173: }
174: 1;
175: __END__
176:
177:
178:
179:
180:
181:
182:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>