Annotation of loncom/lonenc.pm, revision 1.14

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for encrypted filenames
                      3: #
1.14    ! albertel    4: # $Id: lonenc.pm,v 1.13 2006/03/22 19:55:39 albertel 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::lonenc;
                     30: 
                     31: use strict;
1.11      albertel   32: use Apache::lonnet;
1.1       www        33: use Crypt::IDEA;
1.9       albertel   34: use Time::HiRes qw(gettimeofday);
1.1       www        35: 
1.2       www        36: sub encryptseed {
1.11      albertel   37:     my $seed=$env{'course.'.$env{'request.course.id'}.'.internal.encseed'};
1.2       www        38:     $seed=~s/[^0-9a-f]/0/g;
                     39:     $seed.='0123456789abcdef';
                     40:     $seed=substr($seed.$seed,0,32);
                     41:     return pack("H32",$seed);
                     42: }
                     43: 
1.1       www        44: sub unencrypted {
                     45:     my $uri=shift;
                     46:     $uri=~s/^\/enc\/(\d+)\///;
                     47:     my $cmdlength=$1;
1.2       www        48:     my $seed=&encryptseed();
                     49:     unless ($seed) {
1.1       www        50: 	return '/'.$uri;
                     51:     }
                     52:     $uri=&Apache::lonnet::unescape($uri);
1.2       www        53:     my $cipher=new IDEA $seed;
1.1       www        54:     my $decuri='';
                     55:     for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
                     56: 	$decuri.=$cipher->decrypt(
                     57: 				  pack("H16",substr($uri,$encidx,16))
                     58: 				  );
                     59:     }
1.11      albertel   60:     $env{'request.enc'}=1;
1.9       albertel   61:     $decuri=&remove_noise($decuri);
1.1       www        62:     return substr($decuri,0,$cmdlength);
                     63: }
                     64: 
1.9       albertel   65: # add a randomish character after every 4th caharacter
                     66: sub add_noise {
                     67:     my ($uri)=@_;
                     68:     my @noise=split(/(.)/,(&gettimeofday())[1]);
                     69:     my $noisy;
                     70:     my $i;
                     71:     foreach my $chunk (split(/(....)/,$uri)) {
                     72: 	$noisy.=$chunk;
                     73: 	$noisy.=$noise[($i++)%(scalar@noise)];
                     74:     }
                     75:     return $noisy;
                     76: }
                     77: 
                     78: # remove every fifth character
                     79: sub remove_noise {
                     80:     my ($uri)=@_;
                     81:     my $clean;
                     82:     foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
                     83:     return $clean;
                     84: }
                     85: 
1.1       www        86: sub encrypted {
1.12      albertel   87:     my ($uri,$force_enc) = @_;
                     88:     if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
1.2       www        89:     my $seed=&encryptseed();
                     90:     unless ($seed) {
                     91: 	return $uri;
                     92:     }
1.1       www        93:     my $cmdlength=length($uri);
1.9       albertel   94:     # add noise before enc so that that same url's look different
                     95:     $uri=&add_noise($uri);
                     96:     my $noiselength=length($uri);
                     97:     $uri.=time;
1.1       www        98:     my $encuri='';
1.2       www        99:     my $cipher=new IDEA $seed;
1.9       albertel  100:     for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
1.1       www       101: 	$encuri.=unpack("H16",
                    102: 			$cipher->encrypt(substr($uri,$encidx,8)));
                    103:     }
                    104:     return '/enc/'.$cmdlength.'/'.&Apache::lonnet::escape($encuri);
                    105: }
                    106: 
1.5       albertel  107: sub check_encrypt {
                    108:     my $str=shift;
1.11      albertel  109:     if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
1.5       albertel  110:     return $str;
                    111: }
                    112: 
1.6       albertel  113: sub check_decrypt {
                    114:     my ($str)=@_;
1.7       albertel  115:     if (ref($str)) {
                    116: 	if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
                    117: 	return;
                    118:     }
                    119:     if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
                    120:     return $str;
1.6       albertel  121: }
                    122: 
1.10      albertel  123: sub encrypt_ref {
1.12      albertel  124:     my ($token,$elements,$force_enc)=@_;
1.10      albertel  125:     my $html;
1.12      albertel  126:     if ($force_enc || $env{'request.enc'}) {
1.10      albertel  127: 	while (my ($name,$value)= each(%{ $elements })) {
                    128: 	    if (!$value) { next; }
                    129: 	    my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
1.12      albertel  130: 	    if ($href !~ /^http:/) {
                    131: 		$href = &Apache::lonenc::encrypted($href,$force_enc);
                    132: 	    }
1.10      albertel  133: 	    $token->[2]->{$name}=$href;
                    134: 	}
1.12      albertel  135: 	delete($token->[2]->{'encrypturl'});
1.10      albertel  136: 	$html = &Apache::edit::rebuild_tag($token);
                    137:     } else {
                    138: 	$html = $token->[4];
                    139:     }
                    140:     return $html;
                    141: }
1.1       www       142: 1;
                    143: __END__
                    144: 
                    145: 
                    146: 
                    147: 
                    148: 
                    149: 
                    150: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>