Annotation of loncom/LONCAPA.pm, revision 1.3
1.1 albertel 1: # The LearningOnline Network
2: # Base routines
3: #
1.3 ! www 4: # $Id: LONCAPA.pm,v 1.2 2006/05/30 12:45:12 www 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 $args = scalar @_;
180: my $hfh = IO::File->new(">>$file_prefix.hist");
181: if($hfh) {
182: my $now = time;
183: print $hfh "$loghead:$now:$what\n";
184: }
185: $hfh->close;
186: }
187: return \%hash;
188: } else {
189: return undef;
190: }
191: }
192:
193: sub _do_hash_untie {
194: my ($hashref) = @_;
195: my $result = untie(%$hashref);
196: return $result;
197: }
198:
199: {
200: my $sym;
201:
202: sub _locking_hash_tie {
203: my ($file_prefix,$namespace,$how,$loghead,$what) = @_;
204: my $lock_type=LOCK_SH;
205: # Are we reading or writing?
206: if ($how eq &GDBM_READER()) {
207: # We are reading
208: if (!open($sym,"$file_prefix.db.lock")) {
209: # We don't have a lock file. This could mean
210: # - that there is no such db-file
211: # - that it does not have a lock file yet
212: if ((! -e "$file_prefix.db") && (! -e "$file_prefix.db.gz")) {
213: # No such file. Forget it.
214: $! = 2;
215: return undef;
216: }
217: # Apparently just no lock file yet. Make one
218: open($sym,">>$file_prefix.db.lock");
219: }
220: # Do a shared lock
221: if (!&flock_sym(LOCK_SH)) { return undef; }
222: # If this is compressed, we will actually need an exclusive lock
223: if (-e "$file_prefix.db.gz") {
224: if (!&flock_sym(LOCK_EX)) { return undef; }
225: }
226: } elsif ($how eq &GDBM_WRCREAT()) {
227: # We are writing
228: open($sym,">>$file_prefix.db.lock");
229: # Writing needs exclusive lock
230: if (!&flock_sym(LOCK_EX)) { return undef; }
231: } else {
232: &logthis("Unknown method $how for $file_prefix");
233: die();
234: }
235: # The file is ours!
236: # If it is archived, un-archive it now
237: if (-e "$file_prefix.db.gz") {
238: system("gunzip $file_prefix.db.gz");
239: if (-e "$file_prefix.hist.gz") {
240: system("gunzip $file_prefix.hist.gz");
241: }
242: }
243: # Change access mode to non-blocking
244: $how=$how|&GDBM_NOLOCK();
245: # Go ahead and tie the hash
246: return &_do_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
247: }
248:
249: sub flock_sym {
250: my ($lock_type)=@_;
251: my $failed=0;
252: eval {
253: local $SIG{__DIE__}='DEFAULT';
254: local $SIG{ALRM}=sub {
255: $failed=1;
256: die("failed lock");
257: };
258: alarm($loncapa_max_wait_time);
259: flock($sym,$lock_type);
260: alarm(0);
261: };
262: if ($failed) {
263: $! = 100; # throwing error # 100
264: return undef;
265: } else {
266: return 1;
267: }
268: }
269:
270: sub _locking_hash_untie {
271: my ($hashref) = @_;
272: my $result = untie(%$hashref);
273: flock($sym,LOCK_UN);
274: close($sym);
275: undef($sym);
276: return $result;
277: }
278: }
279:
280: BEGIN {
281: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
282: %perlvar=%{$perlvarref};
283: undef $perlvarref;
284: }
285:
1.1 albertel 286: 1;
287:
288: __END__
289:
290: =pod
291:
292: =head1 NAME
293:
294: LONCAPA - Basic routines
295:
296: =head1 SYNOPSIS
297:
298: Generally useful routines
299:
300: =head1 EXPORTED SUBROUTINES
301:
302: =over 4
303:
304: =item *
305:
306: escape() : unpack non-word characters into CGI-compatible hex codes
307:
308: =item *
309:
310: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
311:
312: =item *
313:
314: add_get_param() :
315: Inputs: url (with or without exit GET from parameters), hash ref of
316: form name => value pairs
317:
318: Return: url with properly added the form name elements and values to the
319: the url doing proper escaping of the values and joining with ? or &
320: as needed
321:
322: =back
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>