--- loncom/cgi/loncgi.pm 2006/04/10 17:46:04 1.6 +++ loncom/cgi/loncgi.pm 2011/10/21 20:00:30 1.12 @@ -1,7 +1,7 @@ # # LON-CAPA helpers for cgi-bin scripts # -# $Id: loncgi.pm,v 1.6 2006/04/10 17:46:04 albertel Exp $ +# $Id: loncgi.pm,v 1.12 2011/10/21 20:00:30 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -36,8 +36,8 @@ loncgi =head1 SYNOPSIS -Provides subroutines for checking a LON-CAPA cookie and loading the users -environment. +Provides subroutines for checking a LON-CAPA cookie, loading the user's +environment, and retrieving arguments passed in via a CGI's Query String. =head1 Subroutines @@ -53,10 +53,15 @@ use strict; use warnings FATAL=>'all'; no warnings 'uninitialized'; -use CGI(); +use lib '/home/httpd/lib/perl/'; +use CGI qw(:standard); use CGI::Cookie(); +use MIME::Types(); use Fcntl qw(:flock); +use LONCAPA; use LONCAPA::Configuration(); +use GDBM_File; +use Apache::lonlocal; my $lonidsdir; @@ -66,14 +71,16 @@ BEGIN { $lonidsdir = $perlvar->{'lonIDsDir'}; } + ############################################# ############################################# =pod -=item check_cookie_and_load_env +=item check_cookie_and_load_env() -Inputs: none +Inputs: 1 ( optional). When called from a handler in mod_perl, + pass in the request object. Returns: 1 if the user has a LON-CAPA cookie 0 if not. Loads the users environment into the %env hash if the cookie is correct. @@ -83,7 +90,13 @@ Loads the users environment into the %en ############################################# ############################################# sub check_cookie_and_load_env { - my %cookies=fetch CGI::Cookie; + my ($r) = @_; + my %cookies; + if (ref($r)) { + %cookies = CGI::Cookie->fetch($r); + } else { + %cookies = CGI::Cookie->fetch(); + } if (exists($cookies{'lonID'}) && -e "$lonidsdir/".$cookies{'lonID'}->value.".id") { # cookie found @@ -100,7 +113,7 @@ sub check_cookie_and_load_env { =pod -=item check_cookie +=item check_cookie() Inputs: none @@ -127,7 +140,7 @@ sub check_cookie { =pod -=item transfer_profile_to_env +=item transfer_profile_to_env() Load the users environment into the %env hash. @@ -141,19 +154,10 @@ Returns: undef ############################################# sub transfer_profile_to_env { my ($handle)=@_; - my @profile; - { - open(IDFILE, "<$lonidsdir/$handle.id"); - flock(IDFILE,LOCK_SH); - @profile=; - close(IDFILE); - } - foreach my $envrow (@profile) { - chomp($envrow); - my ($envname,$envvalue)=split(/=/,$envrow,2); - $envname = &unescape($envname); - $envvalue = &unescape($envvalue); - $Apache::lonnet::env{$envname} = $envvalue; + if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_READER(), + 0640)) { + %Apache::lonnet::env = %disk_env; + untie(%disk_env); } $Apache::lonnet::env{'user.environment'} = "$lonidsdir/$handle.id"; return undef; @@ -162,20 +166,96 @@ sub transfer_profile_to_env { ############################################# ############################################# -sub escape { - my $str=shift; - $str =~ s/(\W)/"%".unpack('H2',$1)/eg; - return $str; +=pod + +=item missing_cookie_msg() + +Inputs: none +Returns: HTML for a page indicating cookie information absent. + +=cut + +############################################# +############################################# +sub missing_cookie_msg { + my %lt = &Apache::lonlocal::texthash ( + cook => 'Bad Cookie', + your => 'Your cookie information is incorrect.', + ); + return < +$lt{'cook'} + +$lt{'your'} + + +END + } -# ----------------------------------------------------- Un-Escape Special Chars - -sub unescape { - my $str=shift; - $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; - return $str; +############################################# +############################################# + +=pod + +=cgi_getitems() + +Inputs: $query (the CGI query string), and $getitems, a reference to a hash + +Returns: nothing + +Side Effects: populates $getitems hash ref with key => value + where each key is the name of the form item in the query string + and value is an array of corresponding values. + +=cut + +############################################# +############################################# +sub cgi_getitems { + my ($query,$getitems)= @_; + foreach (split(/&/,$query)) { + my ($name, $value) = split(/=/,$_); + $name = &unescape($name); + $value =~ tr/+/ /; + $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; + push(@{$$getitems{$name}},$value); + } + return; } +############################################# +############################################# + +=pod + +=cgi_header() + +Inputs: $contenttype - Content Type (e.g., text/html or text/plain) + $nocache - Boolean 1 = nocache +Returns: HTTP Response headers constructed using CGI.pm + +=cut + +############################################# +############################################# +sub cgi_header { + my ($contenttype,$nocache) = @_; + my $mimetypes = MIME::Types->new; + my %headers; + if ($contenttype ne '') { + if ($mimetypes->type($contenttype) ne '') { + $headers{'-type'} = $contenttype; + } + } + if ($nocache) { + $headers{'-expires'} = 'now'; + } + if (%headers) { + return CGI::header(%headers); + } + return; +} =pod