Annotation of loncom/lonenc.pm, revision 1.2

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for encrypted filenames
                      3: #
1.2     ! www         4: # $Id: lonenc.pm,v 1.1 2004/03/31 04:38:06 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;
                     32: use Apache::Constants qw(:common :remotehost);
                     33: use Apache::lonnet();
                     34: use Apache::File();
                     35: use Apache::loncommon;
                     36: use Crypt::IDEA;
                     37: 
                     38: sub handler {
                     39:     my $r = shift;
                     40:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     41:     my $lonid=$cookies{'lonID'};
                     42:     my $cookie;
                     43:     if ($lonid) {
                     44: 	my $handle=$lonid->value;
                     45:         $handle=~s/\W//g;
                     46:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     47:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
                     48: # Initialize Environment
                     49:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     50: # Decrypt URL and redirect
                     51: 	    $r->internal_redirect(&unencrypted($r->uri));
                     52: 	    return OK;
                     53: 	} 
                     54:     }
                     55:     return FORBIDDEN;
                     56: }
                     57: 
1.2     ! www        58: sub encryptseed {
        !            59:     my $seed=$ENV{'course.'.$ENV{'request.course.id'}.'.internal.encseed'};
        !            60:     $seed=~s/[^0-9a-f]/0/g;
        !            61:     $seed.='0123456789abcdef';
        !            62:     $seed=substr($seed.$seed,0,32);
        !            63:     return pack("H32",$seed);
        !            64: }
        !            65: 
1.1       www        66: sub unencrypted {
                     67:     my $uri=shift;
                     68:     $uri=~s/^\/enc\/(\d+)\///;
                     69:     my $cmdlength=$1;
1.2     ! www        70:     my $seed=&encryptseed();
        !            71:     unless ($seed) {
1.1       www        72: 	return '/'.$uri;
                     73:     }
                     74:     $uri=&Apache::lonnet::unescape($uri);
1.2     ! www        75:     my $cipher=new IDEA $seed;
1.1       www        76:     my $decuri='';
                     77:     for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
                     78: 	$decuri.=$cipher->decrypt(
                     79: 				  pack("H16",substr($uri,$encidx,16))
                     80: 				  );
                     81:     }
                     82:     return substr($decuri,0,$cmdlength);
                     83: }
                     84: 
                     85: sub encrypted {
                     86:     my $uri=shift;
1.2     ! www        87:     my $seed=&encryptseed();
        !            88:     unless ($seed) {
        !            89: 	return $uri;
        !            90:     }
1.1       www        91:     my $cmdlength=length($uri);
1.2     ! www        92:     $uri.='00000000';
1.1       www        93:     my $encuri='';
1.2     ! www        94:     my $cipher=new IDEA $seed;
1.1       www        95:     for (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
                     96: 	$encuri.=unpack("H16",
                     97: 			$cipher->encrypt(substr($uri,$encidx,8)));
                     98:     }
                     99:     return '/enc/'.$cmdlength.'/'.&Apache::lonnet::escape($encuri);
                    100: }
                    101: 
                    102: 1;
                    103: __END__
                    104: 
                    105: 
                    106: 
                    107: 
                    108: 
                    109: 
                    110: 

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