Annotation of loncom/lonenc.pm, revision 1.18

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

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