version 1.2, 2005/04/05 20:43:27
|
version 1.16, 2018/07/04 16:58:26
|
Line 36 loncgi
|
Line 36 loncgi
|
|
|
=head1 SYNOPSIS |
=head1 SYNOPSIS |
|
|
Provides subroutines for checking a LON-CAPA cookie and loading the users |
Provides subroutines for checking a LON-CAPA cookie, loading the user's |
environment. |
environment, and retrieving arguments passed in via a CGI's Query String. |
|
|
=head1 Subroutines |
=head1 Subroutines |
|
|
Line 52 package LONCAPA::loncgi;
|
Line 52 package LONCAPA::loncgi;
|
use strict; |
use strict; |
use warnings FATAL=>'all'; |
use warnings FATAL=>'all'; |
no warnings 'uninitialized'; |
no warnings 'uninitialized'; |
use vars qw(%env); |
|
|
|
use CGI(); |
use lib '/home/httpd/lib/perl/'; |
|
use CGI qw(:standard); |
use CGI::Cookie(); |
use CGI::Cookie(); |
|
use MIME::Types(); |
use Fcntl qw(:flock); |
use Fcntl qw(:flock); |
|
use LONCAPA; |
use LONCAPA::Configuration(); |
use LONCAPA::Configuration(); |
require Exporter; |
use GDBM_File; |
|
use Apache::lonlocal; |
our @ISA = qw (Exporter); |
|
our @EXPORT = qw(%env); |
|
|
|
my $lonidsdir; |
my $lonidsdir; |
|
|
Line 71 BEGIN {
|
Line 71 BEGIN {
|
$lonidsdir = $perlvar->{'lonIDsDir'}; |
$lonidsdir = $perlvar->{'lonIDsDir'}; |
} |
} |
|
|
|
|
############################################# |
############################################# |
############################################# |
############################################# |
|
|
=pod |
=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. |
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. |
Side effect: Loads the user's environment into the %env hash |
|
if the cookie is correct. |
|
|
=cut |
=cut |
|
|
############################################# |
############################################# |
############################################# |
############################################# |
sub check_cookie_and_load_env { |
sub check_cookie_and_load_env { |
my %cookies=fetch CGI::Cookie; |
my ($r) = @_; |
if (exists($cookies{'lonID'}) && |
my ($hascookie,$handle) = &check_cookie($r); |
-e "$lonidsdir/".$cookies{'lonID'}->value.".id") { |
if (($hascookie) && ($handle)) { |
# cookie found |
&transfer_profile_to_env($handle); |
&transfer_profile_to_env($cookies{'lonID'}->value); |
|
return 1; |
|
} else { |
|
# No cookie found |
|
return 0; |
|
} |
} |
|
return $hascookie; |
} |
} |
|
|
############################################# |
############################################# |
Line 105 sub check_cookie_and_load_env {
|
Line 104 sub check_cookie_and_load_env {
|
|
|
=pod |
=pod |
|
|
=item check_cookie |
=item check_cookie() |
|
|
Inputs: none |
Inputs: none |
|
|
|
Array context: |
|
Returns: (1,$handle) if the user has a LON-CAPA cookie; |
|
(0) if user does not have a LON-CAPA cookie. |
|
|
|
Scalar context: |
Returns: 1 if the user has a LON-CAPA cookie and 0 if not. |
Returns: 1 if the user has a LON-CAPA cookie and 0 if not. |
|
|
=cut |
=cut |
Line 116 Returns: 1 if the user has a LON-CAPA co
|
Line 120 Returns: 1 if the user has a LON-CAPA co
|
############################################# |
############################################# |
############################################# |
############################################# |
sub check_cookie { |
sub check_cookie { |
my %cookies=fetch CGI::Cookie; |
my ($r) = @_; |
if (exists($cookies{'lonID'}) && |
my %cookies; |
-e "$lonidsdir/".$cookies{'lonID'}->value.".id") { |
if (ref($r)) { |
# cookie found |
%cookies = CGI::Cookie->fetch($r); |
return 1; |
} else { |
|
%cookies = CGI::Cookie->fetch(); |
|
} |
|
if (keys(%cookies)) { |
|
my $name = 'lonID'; |
|
my $secure = 'lonSID'; |
|
my $linkname = 'lonLinkID'; |
|
my $pubname = 'lonPubID'; |
|
my $lonid; |
|
if (exists($cookies{$secure})) { |
|
$lonid=$cookies{$secure}; |
|
} elsif (exists($cookies{$name})) { |
|
$lonid=$cookies{$name}; |
|
} elsif (exists($cookies{$linkname})) { |
|
$lonid=$cookies{$linkname}; |
|
} elsif (exists($cookies{$pubname})) { |
|
$lonid=$cookies{$pubname}; |
|
} |
|
if ($lonid) { |
|
my $handle=&LONCAPA::clean_handle($lonid->value); |
|
if ($handle) { |
|
if (-l "$lonidsdir/$handle.id") { |
|
my $link = readlink("$lonidsdir/$handle.id"); |
|
if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) { |
|
$handle = $1; |
|
} |
|
} |
|
if (-e "$lonidsdir/".$handle.".id") { |
|
# valid cookie found |
|
if (wantarray) { |
|
return (1,$handle); |
|
} else { |
|
return 1; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
# No valid cookie found |
|
if (wantarray) { |
|
return (0); |
} else { |
} else { |
# No cookie found |
|
return 0; |
return 0; |
} |
} |
} |
} |
Line 132 sub check_cookie {
|
Line 175 sub check_cookie {
|
|
|
=pod |
=pod |
|
|
=item transfer_profile_to_env |
=item transfer_profile_to_env() |
|
|
Load the users environment into the %ENV hash. |
Load the users environment into the %env hash. |
|
|
Inputs: $handle, the name of the users LON-CAPA cookie. |
Inputs: $handle, the name of the users LON-CAPA cookie. |
|
|
Line 146 Returns: undef
|
Line 189 Returns: undef
|
############################################# |
############################################# |
sub transfer_profile_to_env { |
sub transfer_profile_to_env { |
my ($handle)=@_; |
my ($handle)=@_; |
my @profile; |
if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_READER(), |
{ |
0640)) { |
open(IDFILE, "<$lonidsdir/$handle.id"); |
%Apache::lonnet::env = %disk_env; |
flock(IDFILE,LOCK_SH); |
untie(%disk_env); |
@profile=<IDFILE>; |
|
close(IDFILE); |
|
} |
|
foreach my $envrow (@profile) { |
|
chomp($envrow); |
|
my ($envname,$envvalue)=split(/=/,$envrow); |
|
$ENV{$envname} = $envvalue; |
|
$env{$envname} = $envvalue; |
|
} |
} |
$ENV{'user.environment'} = "$lonidsdir/$handle.id"; |
$Apache::lonnet::env{'user.environment'} = "$lonidsdir/$handle.id"; |
$env{'user.environment'} = "$lonidsdir/$handle.id"; |
|
return undef; |
return undef; |
} |
} |
|
|
Line 169 sub transfer_profile_to_env {
|
Line 203 sub transfer_profile_to_env {
|
|
|
=pod |
=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 <<END; |
|
<html> |
|
<head><title>$lt{'cook'}</title></head> |
|
<body> |
|
$lt{'your'} |
|
</body> |
|
</html> |
|
END |
|
|
|
} |
|
|
|
############################################# |
|
############################################# |
|
|
|
=pod |
|
|
|
=cgi_getitems() |
|
|
|
Inputs: $query - the CGI query string (required) |
|
$getitems - reference to a hash (required) |
|
$possname - permitted names of keys (optional) |
|
|
|
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,$possnames)= @_; |
|
foreach (split(/&/,$query)) { |
|
my ($name, $value) = split(/=/,$_); |
|
$name = &unescape($name); |
|
if (ref($possnames) eq 'ARRAY') { |
|
next unless (grep(/^\Q$name\E$/,@{$possnames})); |
|
} |
|
$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 ($contenttype =~ m{^text/}) { |
|
$headers{'-charset'} = 'utf-8'; |
|
} |
|
} |
|
} |
|
if ($nocache) { |
|
$headers{'-expires'} = 'now'; |
|
} |
|
if (%headers) { |
|
return CGI::header(%headers); |
|
} |
|
return; |
|
} |
|
|
|
=pod |
|
|
=back |
=back |
|
|
=cut |
=cut |