File:  [LON-CAPA] / loncom / lonenc.pm
Revision 1.15: download - view: text, annotated - select for diffs
Tue May 30 12:45:12 2006 UTC (17 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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