Annotation of loncom/LONCAPA.pm, revision 1.5

1.1       albertel    1: # The LearningOnline Network
                      2: # Base routines
                      3: #
1.5     ! albertel    4: # $Id: LONCAPA.pm,v 1.4 2006/05/30 19:26:34 albertel Exp $
1.1       albertel    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: 
                     30: package LONCAPA;
                     31: 
                     32: use strict;
1.2       www        33: use lib '/home/httpd/lib/perl/';
                     34: use LONCAPA::Configuration;
                     35: use Fcntl qw(:flock);
                     36: use GDBM_File;
                     37: use POSIX;
                     38: 
                     39: my $loncapa_max_wait_time = 13;
                     40: 
1.1       albertel   41: require Exporter;
                     42: our @ISA = qw (Exporter);
1.2       www        43: our @EXPORT = qw(&add_get_param &escape &unescape &tie_domain_hash &untie_domain_hash &tie_user_hash &untie_user_hash &propath);
                     44: my %perlvar;
1.1       albertel   45: 
1.2       www        46: # Inputs are a url, and a hash ref of
1.1       albertel   47: # form name => value pairs
                     48: # takes care of properly adding the form name elements and values to the 
                     49: # the url doing proper escaping of the values and joining with ? or & as 
                     50: # needed
                     51: 
                     52: sub add_get_param {
                     53:     my ($url,$form_data) = @_;
                     54:     my $needs_question_mark = ($url !~ /\?/);
                     55: 
                     56:     while (my ($name,$value) = each(%$form_data)) {
                     57: 	if ($needs_question_mark) {
                     58: 	    $url.='?';
                     59: 	    $needs_question_mark = 0;
                     60: 	} else { 
                     61: 	    $url.='&';
                     62: 	}
                     63: 	$url.=$name.'='.&escape($form_data->{$name});
                     64:     }
                     65:     return $url;
                     66: }
                     67: 
                     68: # -------------------------------------------------------- Escape Special Chars
                     69: 
                     70: sub escape {
                     71:     my $str=shift;
                     72:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
                     73:     return $str;
                     74: }
                     75: 
                     76: # ----------------------------------------------------- Un-Escape Special Chars
                     77: 
                     78: sub unescape {
                     79:     my $str=shift;
                     80:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     81:     return $str;
                     82: }
                     83: 
1.2       www        84: # -------------------------------------------- Return path to profile directory
                     85: 
                     86: sub propath {
                     87:     my ($udom,$uname)=@_;
                     88:     $udom=~s/\W//g;
                     89:     $uname=~s/\W//g;
                     90:     my $subdir=$uname.'__';
                     91:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                     92:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
                     93:     return $proname;
                     94: } 
                     95: 
                     96: 
                     97: #---------------------------------------------------------------
                     98: #
                     99: # Manipulation of hash based databases (factoring out common code
                    100: # for later use as we refactor.
                    101: #
                    102: #  Ties a domain level resource file to a hash.
                    103: #  If requested a history entry is created in the associated hist file.
                    104: #
                    105: #  Parameters:
                    106: #     domain    - Name of the domain in which the resource file lives.
                    107: #     namespace - Name of the hash within that domain.
                    108: #     how       - How to tie the hash (e.g. GDBM_WRCREAT()).
                    109: #     loghead   - Optional parameter, if present a log entry is created
                    110: #                 in the associated history file and this is the first part
                    111: #                  of that entry.
                    112: #     logtail   - Goes along with loghead,  The actual logentry is of the
                    113: #                 form $loghead:<timestamp>:logtail.
                    114: # Returns:
                    115: #    Reference to a hash bound to the db file or alternatively undef
                    116: #    if the tie failed.
                    117: #
                    118: sub tie_domain_hash {
                    119:     my ($domain,$namespace,$how,$loghead,$logtail) = @_;
                    120:     
                    121:     # Filter out any whitespace in the domain name:
                    122:     
                    123:     $domain =~ s/\W//g;
                    124:     
                    125:     # We have enough to go on to tie the hash:
                    126:     
                    127:     my $user_top_dir   = $perlvar{'lonUsersDir'};
                    128:     my $domain_dir     = $user_top_dir."/$domain";
                    129:     my $resource_file  = $domain_dir."/$namespace";
                    130:     return &_locking_hash_tie($resource_file,$namespace,$how,$loghead,$logtail);
                    131: }
                    132: 
                    133: sub untie_domain_hash {
                    134:     return &_locking_hash_untie(@_);
                    135: }
                    136: #
                    137: #   Ties a user's resource file to a hash.  
                    138: #   If necessary, an appropriate history
                    139: #   log file entry is made as well.
                    140: #   This sub factors out common code from the subs that manipulate
                    141: #   the various gdbm files that keep keyword value pairs.
                    142: # Parameters:
                    143: #   domain       - Name of the domain the user is in.
                    144: #   user         - Name of the 'current user'.
                    145: #   namespace    - Namespace representing the file to tie.
                    146: #   how          - What the tie is done to (e.g. GDBM_WRCREAT().
                    147: #   loghead      - Optional first part of log entry if there may be a
                    148: #                  history file.
                    149: #   what         - Optional tail of log entry if there may be a history
                    150: #                  file.
                    151: # Returns:
                    152: #   hash to which the database is tied.  It's up to the caller to untie.
                    153: #   undef if the has could not be tied.
                    154: #
                    155: sub tie_user_hash {
                    156:     my ($domain,$user,$namespace,$how,$loghead,$what) = @_;
                    157: 
                    158:     $namespace=~s/\//\_/g;	# / -> _
                    159:     $namespace=~s/\W//g;		# whitespace eliminated.
                    160:     my $proname     = &propath($domain, $user);
                    161: 
                    162:     my $file_prefix="$proname/$namespace";
                    163:     return &_locking_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
                    164: }
                    165: 
                    166: sub untie_user_hash {
                    167:     return &_locking_hash_untie(@_);
                    168: }
                    169: 
                    170: # internal routines that handle the actual tieing and untieing process
                    171: 
                    172: sub _do_hash_tie {
                    173:     my ($file_prefix,$namespace,$how,$loghead,$what) = @_;
                    174:     my %hash;
                    175:     if(tie(%hash, 'GDBM_File', "$file_prefix.db", $how, 0640)) {
                    176: 	# If this is a namespace for which a history is kept,
                    177: 	# make the history log entry:    
                    178: 	if (($namespace !~/^nohist\_/) && (defined($loghead))) {
                    179: 	    my $hfh = IO::File->new(">>$file_prefix.hist"); 
                    180: 	    if($hfh) {
1.5     ! albertel  181: 		my $now = time();
        !           182: 		print $hfh ("$loghead:$now:$what\n");
1.2       www       183: 	    }
                    184: 	    $hfh->close;
                    185: 	}
                    186: 	return \%hash;
                    187:     } else {
                    188: 	return undef;
                    189:     }
                    190: }
                    191: 
                    192: sub _do_hash_untie {
                    193:     my ($hashref) = @_;
                    194:     my $result = untie(%$hashref);
                    195:     return $result;
                    196: }
                    197: 
                    198: {
                    199:     my $sym;
                    200: 
                    201:     sub _locking_hash_tie {
                    202: 	my ($file_prefix,$namespace,$how,$loghead,$what) = @_;
                    203:         my $lock_type=LOCK_SH;
                    204: # Are we reading or writing?
                    205:         if ($how eq &GDBM_READER()) {
                    206: # We are reading
                    207:            if (!open($sym,"$file_prefix.db.lock")) {
                    208: # We don't have a lock file. This could mean
                    209: # - that there is no such db-file
                    210: # - that it does not have a lock file yet
                    211:                if ((! -e "$file_prefix.db") && (! -e "$file_prefix.db.gz")) {
                    212: # No such file. Forget it.                
                    213:                    $! = 2;
                    214:                    return undef;
                    215:                }
                    216: # Apparently just no lock file yet. Make one
                    217:                open($sym,">>$file_prefix.db.lock");
                    218:            }
                    219: # Do a shared lock
                    220:            if (!&flock_sym(LOCK_SH)) { return undef; } 
                    221: # If this is compressed, we will actually need an exclusive lock
                    222: 	   if (-e "$file_prefix.db.gz") {
                    223: 	       if (!&flock_sym(LOCK_EX)) { return undef; }
                    224: 	   }
                    225:         } elsif ($how eq &GDBM_WRCREAT()) {
                    226: # We are writing
                    227:            open($sym,">>$file_prefix.db.lock");
                    228: # Writing needs exclusive lock
                    229:            if (!&flock_sym(LOCK_EX)) { return undef; }
                    230:         } else {
1.5     ! albertel  231:            die("Unknown method $how for $file_prefix");
1.2       www       232:         }
                    233: # The file is ours!
                    234: # If it is archived, un-archive it now
                    235:        if (-e "$file_prefix.db.gz") {
                    236:            system("gunzip $file_prefix.db.gz");
                    237: 	   if (-e "$file_prefix.hist.gz") {
                    238: 	       system("gunzip $file_prefix.hist.gz");
                    239: 	   }
                    240:        }
                    241: # Change access mode to non-blocking
                    242:        $how=$how|&GDBM_NOLOCK();
                    243: # Go ahead and tie the hash
                    244:        return &_do_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
                    245:     }
                    246: 
                    247:     sub flock_sym {
                    248:         my ($lock_type)=@_;
                    249: 	my $failed=0;
                    250: 	eval {
                    251: 	    local $SIG{__DIE__}='DEFAULT';
                    252: 	    local $SIG{ALRM}=sub {
                    253: 		$failed=1;
                    254: 		die("failed lock");
                    255: 	    };
                    256: 	    alarm($loncapa_max_wait_time);
                    257: 	    flock($sym,$lock_type);
                    258: 	    alarm(0);
                    259: 	};
                    260: 	if ($failed) {
                    261: 	    $! = 100; # throwing error # 100
                    262: 	    return undef;
                    263: 	} else {
                    264: 	    return 1;
                    265: 	}
                    266:     }
                    267: 
                    268:     sub _locking_hash_untie {
                    269: 	my ($hashref) = @_;
                    270: 	my $result = untie(%$hashref);
                    271: 	flock($sym,LOCK_UN);
                    272: 	close($sym);
                    273: 	undef($sym);
                    274: 	return $result;
                    275:     }
                    276: }
                    277: 
                    278: BEGIN {
1.4       albertel  279:     %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
1.2       www       280: }
                    281: 
1.1       albertel  282: 1;
                    283: 
                    284: __END__
                    285: 
                    286: =pod
                    287: 
                    288: =head1 NAME
                    289: 
                    290: LONCAPA - Basic routines
                    291: 
                    292: =head1 SYNOPSIS
                    293: 
                    294: Generally useful routines
                    295: 
                    296: =head1 EXPORTED SUBROUTINES
                    297: 
                    298: =over 4
                    299: 
                    300: =item *
                    301: 
                    302: escape() : unpack non-word characters into CGI-compatible hex codes
                    303: 
                    304: =item *
                    305: 
                    306: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                    307: 
                    308: =item *
                    309: 
                    310: add_get_param() :
                    311:  Inputs:  url (with or without exit GET from parameters), hash ref of
                    312:               form name => value pairs
                    313: 
                    314:  Return: url with properly added the form name elements and values to the 
                    315:          the url doing proper escaping of the values and joining with ? or &
                    316:          as needed
                    317: 
                    318: =back

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