Annotation of loncom/lonenc.pm, revision 1.16

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

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