File:  [LON-CAPA] / loncom / LONCAPA.pm
Revision 1.28: download - view: text, annotated - select for diffs
Wed May 13 14:01:10 2009 UTC (14 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_8_99_0, bz5969, bz2851, HEAD, GCI_2, BZ5971-printing-apage, BZ5434-fox
- Accumulate POD at end of file.

    1: # The LearningOnline Network
    2: # Base routines
    3: #
    4: # $Id: LONCAPA.pm,v 1.28 2009/05/13 14:01:10 raeburn 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: 
   30: 
   31: 
   32: package LONCAPA;
   33: 
   34: use strict;
   35: use lib '/home/httpd/lib/perl/';
   36: use LONCAPA::Configuration;
   37: use Fcntl qw(:flock);
   38: use GDBM_File;
   39: use POSIX;
   40: 
   41: my $loncapa_max_wait_time = 13;
   42: 
   43: 
   44: use vars qw($match_domain   $match_not_domain
   45: 	    $match_username $match_not_username
   46: 	    $match_courseid $match_not_courseid
   47: 	    $match_name
   48:             $match_lonid
   49: 	    $match_handle   $match_not_handle);
   50: 
   51: require Exporter;
   52: our @ISA = qw (Exporter);
   53: our @EXPORT = qw(&add_get_param    &escape            &unescape       
   54: 		 &tie_domain_hash  &untie_domain_hash &tie_user_hash
   55: 		 &untie_user_hash  &propath);
   56: our @EXPORT_OK = qw($match_domain   $match_not_domain
   57: 		    $match_username $match_not_username
   58: 		    $match_courseid $match_not_courseid
   59: 		    $match_name
   60: 		    $match_lonid
   61: 		    $match_handle   $match_not_handle);
   62: our %EXPORT_TAGS = ( 'match' =>[qw($match_domain   $match_not_domain
   63: 				   $match_username $match_not_username
   64: 				   $match_courseid $match_not_courseid
   65: 				   $match_name
   66: 				   $match_lonid
   67: 				   $match_handle   $match_not_handle)],);
   68: my %perlvar;
   69: 
   70: 
   71: sub add_get_param {
   72:     my ($url,$form_data) = @_;
   73:     my $needs_question_mark = ($url !~ /\?/);
   74: 
   75:     while (my ($name,$value) = each(%$form_data)) {
   76: 	if ($needs_question_mark) {
   77: 	    $url.='?';
   78: 	    $needs_question_mark = 0;
   79: 	} else { 
   80: 	    $url.='&';
   81: 	}
   82: 	$url.=$name.'='.&escape($form_data->{$name});
   83:     }
   84:     return $url;
   85: }
   86: 
   87: # -------------------------------------------------------- Escape Special Chars
   88: 
   89: sub escape {
   90:     my $str=shift;
   91:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
   92:     return $str;
   93: }
   94: 
   95: # ----------------------------------------------------- Un-Escape Special Chars
   96: 
   97: sub unescape {
   98:     my $str=shift;
   99:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  100:     return $str;
  101: }
  102: 
  103: $match_domain     = $LONCAPA::domain_re     = qr{[\w\-.]+};
  104: $match_not_domain = $LONCAPA::not_domain_re = qr{[^\w\-.]+};
  105: sub clean_domain {
  106:     my ($domain) = @_;
  107:     $domain =~ s/$match_not_domain//g;
  108:     return $domain;
  109: }
  110: 
  111: $match_username     = $LONCAPA::username_re     = qr{\w[\w\-.@]+};
  112: $match_not_username = $LONCAPA::not_username_re = qr{[^\w\-.@]+};
  113: sub clean_username {
  114:     my ($username) = @_;
  115:     $username =~ s/^\W+//;
  116:     $username =~ s/$match_not_username//g;
  117:     return $username;
  118: }
  119: 
  120: 
  121: $match_courseid     = $LONCAPA::courseid_re     = qr{\d[\w\-.]+};
  122: $match_not_courseid = $LONCAPA::not_courseid_re = qr{[^\w\-.]+};
  123: sub clean_courseid {
  124:     my ($courseid) = @_;
  125:     $courseid =~ s/^\D+//;
  126:     $courseid =~ s/$match_not_courseid//g;
  127:     return $courseid;
  128: }
  129: 
  130: $match_name         = $LONCAPA::name_re = qr{$match_username|$match_courseid};
  131: sub clean_name {
  132:     my ($name) = @_;
  133:     $name =~ s/$match_not_username//g;
  134:     return $name;
  135: }
  136: 
  137: $match_lonid     = $LONCAPA::lonid_re     = qr{[\w\-.]+};
  138: 
  139: sub split_courseid {
  140:     my ($courseid) = @_;
  141:     my  ($domain,$coursenum) = 
  142: 	($courseid=~m{^/($match_domain)/($match_courseid)});
  143:     return ($domain,$coursenum);
  144: }
  145: 
  146: $match_handle     = $LONCAPA::handle_re     = qr{[\w\-.@]+};
  147: $match_not_handle = $LONCAPA::not_handle_re = qr{[^\w\-.@]+};
  148: sub clean_handle {
  149:     my ($handle) = @_;
  150:     $handle =~ s/$match_not_handle//g;
  151:     return $handle;
  152: }
  153: 
  154: # -------------------------------------------- Return path to profile directory
  155: 
  156: sub propath {
  157:     my ($udom,$uname)=@_;
  158:     $udom = &clean_domain($udom);
  159:     $uname= &clean_name($uname);
  160:     my $subdir=$uname.'__';
  161:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  162:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
  163:     return $proname;
  164: } 
  165: 
  166: 
  167: sub tie_domain_hash {
  168:     my ($domain,$namespace,$how,$loghead,$logtail) = @_;
  169:     
  170:     # Filter out any whitespace in the domain name:
  171:     
  172:     $domain = &clean_domain($domain);
  173:     
  174:     # We have enough to go on to tie the hash:
  175:     
  176:     my $user_top_dir   = $perlvar{'lonUsersDir'};
  177:     my $domain_dir     = $user_top_dir."/$domain";
  178:     my $resource_file  = $domain_dir."/$namespace";
  179:     return &_locking_hash_tie($resource_file,$namespace,$how,$loghead,$logtail);
  180: }
  181: 
  182: sub untie_domain_hash {
  183:     return &_locking_hash_untie(@_);
  184: }
  185: 
  186: 
  187: sub tie_user_hash {
  188:     my ($domain,$user,$namespace,$how,$loghead,$what) = @_;
  189: 
  190:     $namespace=~s{/}{_}g;	# / -> _
  191:     $namespace     = &clean_username($namespace);
  192:     my $proname    = &propath($domain, $user);
  193:     my $file_prefix="$proname/$namespace";
  194:     return &_locking_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
  195: }
  196: 
  197: sub untie_user_hash {
  198:     return &_locking_hash_untie(@_);
  199: }
  200: 
  201: 
  202: sub locking_hash_tie {
  203:     my ($filename,$how)=@_;
  204:     my ($file_prefix,$namespace)=&db_filename_parts($filename);
  205:     if ($namespace eq '') { return undef; }
  206:     return &_locking_hash_tie($file_prefix,$namespace,$how);
  207: }
  208: 
  209: sub locking_hash_untie {
  210:     return &_locking_hash_untie(@_);
  211: }
  212: 
  213: sub db_filename_parts {
  214:     my ($filename)=@_;
  215:     my ($file_path,$namespace)=($filename=~/^(.*)\/([^\/]+)\.db$/);
  216:     if ($namespace eq '') { return undef; }
  217:     return ($file_path.'/'.$namespace,$namespace);
  218: }
  219: 
  220: # internal routines that handle the actual tieing and untieing process
  221: 
  222: sub _do_hash_tie {
  223:     my ($file_prefix,$namespace,$how,$loghead,$what) = @_;
  224:     my %hash;
  225:     if(tie(%hash, 'GDBM_File', "$file_prefix.db", $how, 0640)) {
  226: 	# If this is a namespace for which a history is kept,
  227: 	# make the history log entry:    
  228: 	if (($namespace !~/^nohist\_/) && (defined($loghead))) {
  229: 	    my $hfh = IO::File->new(">>$file_prefix.hist"); 
  230: 	    if($hfh) {
  231: 		my $now = time();
  232: 		print $hfh ("$loghead:$now:$what\n");
  233: 	    }
  234: 	    $hfh->close;
  235: 	}
  236: 	return \%hash;
  237:     } else {
  238: 	return undef;
  239:     }
  240: }
  241: 
  242: sub _do_hash_untie {
  243:     my ($hashref) = @_;
  244:     my $result = untie(%$hashref);
  245:     return $result;
  246: }
  247: 
  248: {
  249:     my $sym;
  250:     my @pushed_syms;
  251: 
  252:     sub clean_sym {
  253: 	undef($sym);
  254:     }
  255:     sub push_locking_hash_tie {
  256: 	if (!defined($sym)) {
  257: 	    die("Invalid used of push_locking_hash_tie, should only be called after a lock has occurred and before and unlock.");
  258: 	}
  259: 	push(@pushed_syms,$sym);
  260: 	undef($sym);
  261:     }
  262: 
  263:     sub pop_locking_hash_tie {
  264: 	if (defined($sym)) {
  265: 	    die("Invalid nested used of pop_locking_hash_tie, should only be called after a unlock has occurred.");
  266: 	}
  267: 	$sym = pop(@pushed_syms);
  268:     }
  269: 
  270:     sub _locking_hash_tie {
  271: 	my ($file_prefix,$namespace,$how,$loghead,$what) = @_;
  272: 	if (defined($sym)) {
  273: 	    die('Nested locking attempted without proper use of push_locking_hash_tie, this is unsupported');
  274: 	}
  275: 
  276:         my $lock_type=LOCK_SH;
  277: # Are we reading or writing?
  278:         if ($how eq &GDBM_READER()) {
  279: # We are reading
  280:            if (!open($sym,"$file_prefix.db.lock")) {
  281: # We don't have a lock file. This could mean
  282: # - that there is no such db-file
  283: # - that it does not have a lock file yet
  284:                if ((! -e "$file_prefix.db") && (! -e "$file_prefix.db.gz")) {
  285: # No such file. Forget it.                
  286:                    $! = 2;
  287: 		   &clean_sym();
  288:                    return undef;
  289:                }
  290: # Apparently just no lock file yet. Make one
  291:                open($sym,">>$file_prefix.db.lock");
  292:            }
  293: # Do a shared lock
  294:            if (!&flock_sym(LOCK_SH)) { 
  295: 	       &clean_sym();
  296: 	       return undef; 
  297: 	   } 
  298: # If this is compressed, we will actually need an exclusive lock
  299: 	   if (-e "$file_prefix.db.gz") {
  300: 	       if (!&flock_sym(LOCK_EX)) {
  301: 		   &clean_sym();
  302: 		   return undef;
  303: 	       }
  304: 	   }
  305:         } elsif ($how eq &GDBM_WRCREAT()) {
  306: # We are writing
  307:            open($sym,">>$file_prefix.db.lock");
  308: # Writing needs exclusive lock
  309:            if (!&flock_sym(LOCK_EX)) {
  310: 	       &clean_sym();
  311: 	       return undef;
  312: 	   }
  313:         } else {
  314:            die("Unknown method $how for $file_prefix");
  315:         }
  316: # The file is ours!
  317: # If it is archived, un-archive it now
  318:        if (-e "$file_prefix.db.gz") {
  319:            system("gunzip $file_prefix.db.gz");
  320: 	   if (-e "$file_prefix.hist.gz") {
  321: 	       system("gunzip $file_prefix.hist.gz");
  322: 	   }
  323:        }
  324: # Change access mode to non-blocking
  325:        $how=$how|&GDBM_NOLOCK();
  326: # Go ahead and tie the hash
  327:       	my $result = 
  328: 	    &_do_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
  329: 	if (!$result) {
  330: 	    &clean_sym();
  331: 	}
  332: 	return $result;
  333:     }
  334: 
  335:     sub flock_sym {
  336:         my ($lock_type)=@_;
  337: 	my $failed=0;
  338: 	eval {
  339: 	    local $SIG{__DIE__}='DEFAULT';
  340: 	    local $SIG{ALRM}=sub {
  341: 		$failed=1;
  342: 		die("failed lock");
  343: 	    };
  344: 	    alarm($loncapa_max_wait_time);
  345: 	    flock($sym,$lock_type);
  346: 	    alarm(0);
  347: 	};
  348: 	if ($failed) {
  349: 	    $! = 100; # throwing error # 100
  350: 	    return undef;
  351: 	} else {
  352: 	    return 1;
  353: 	}
  354:     }
  355: 
  356:     sub _locking_hash_untie {
  357: 	my ($hashref) = @_;
  358: 	my $result = untie(%$hashref);
  359: 	flock($sym,LOCK_UN);
  360: 	close($sym);
  361: 	&clean_sym();
  362: 	return $result;
  363:     }
  364: }
  365: 
  366: BEGIN {
  367:     %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
  368: }
  369: 
  370: 1;
  371: 
  372: __END__
  373: 
  374: =pod
  375: 
  376: =head1 NAME
  377: 
  378: Apache::LONCAPA
  379: 
  380: LONCAPA - Basic routines
  381: 
  382: =head1 SYNOPSIS
  383: 
  384: Generally useful routines
  385: 
  386: =head1 EXPORTED SUBROUTINES
  387: 
  388: =over
  389: 
  390: =item escape()
  391: 
  392: unpack non-word characters into CGI-compatible hex codes
  393: 
  394: =item unescape()
  395: 
  396:  pack CGI-compatible hex codes into actual non-word ASCII character
  397: 
  398: =item  add_get_param()
  399: 
  400: Append escaped form elements (name=value etc.) to a url.
  401:  
  402:  Inputs:  url (with or without exit GET from parameters), hash ref of
  403:               form name => value pairs
  404: 
  405:  Return: url with form name elements and values appended to the 
  406:          the url, doing proper escaping of the values and joining with ? or &
  407:          as needed
  408: 
  409: =item clean_handle()
  410: 
  411: =item propath()
  412: 
  413: =item untie_domain_hash()
  414: 
  415: =item tie_domain_hash()
  416: 
  417: Manipulation of hash based databases (factoring out common code
  418: for later use as we refactor.
  419: 
  420:  Ties a domain level resource file to a hash.
  421:  If requested a history entry is created in the associated hist file.
  422: 
  423:  Parameters:
  424:     domain    - Name of the domain in which the resource file lives.
  425:     namespace - Name of the hash within that domain.
  426:     how       - How to tie the hash (e.g. GDBM_WRCREAT()).
  427:     loghead   - Optional parameter, if present a log entry is created
  428:                 in the associated history file and this is the first part
  429:                  of that entry.
  430:     logtail   - Goes along with loghead,  The actual logentry is of the
  431:                 form $loghead:<timestamp>:logtail.
  432: Returns:
  433:    Reference to a hash bound to the db file or alternatively undef
  434:    if the tie failed.
  435: 
  436: =item tie_user_hash()
  437: 
  438:   Ties a user's resource file to a hash.  
  439:   If necessary, an appropriate history
  440:   log file entry is made as well.
  441:   This sub factors out common code from the subs that manipulate
  442:   the various gdbm files that keep keyword value pairs.
  443: Parameters:
  444:   domain       - Name of the domain the user is in.
  445:   user         - Name of the 'current user'.
  446:   namespace    - Namespace representing the file to tie.
  447:   how          - What the tie is done to (e.g. GDBM_WRCREAT().
  448:   loghead      - Optional first part of log entry if there may be a
  449:                  history file.
  450:   what         - Optional tail of log entry if there may be a history
  451:                  file.
  452: Returns:
  453:   hash to which the database is tied.  It's up to the caller to untie.
  454:   undef if the has could not be tied.
  455: 
  456: =item locking_hash_tie()
  457: 
  458: routines if you just have a filename return tied hashref or undef
  459: 
  460: =item locking_hash_untie()
  461: 
  462: =item db_filename_parts()
  463: 
  464: =head1 INTERNAL SUBROUTINES
  465: 
  466: =item _do_hash_tie()
  467: 
  468: =item _do_hash_untie()
  469: 
  470: =back
  471: 
  472: =cut
  473: 

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