| 1 | package QWizard::Storage::CGICookie; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use QWizard::Storage::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our @ISA = qw(QWizard::Storage::Base); | ||
| 6 | |||
| 7 | our %cached_cookies = (); | ||
| 8 | |||
| 9 | our $VERSION = '3.15'; | ||
| 10 | 0 | ▶ | use CGI qw(escapeHTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $qw = shift; | ||
| 15 | bless {wiz => $qw}, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub get { | ||
| 19 | my ($self, $it) = @_; | ||
| 20 | return $cached_cookies{$it} if (exists($cached_cookies{$it})); | ||
| 21 | # XXX: optimize this | ||
| 22 | my %cookies = fetch CGI::Cookie; | ||
| 23 | return if (!exists($cookies{$it})); | ||
| 24 | return $cookies{$it}->value; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub get_all { | ||
| 28 | my %cookies = fetch CGI::Cookie; | ||
| 29 | return \%cookies; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub set { | ||
| 33 | my ($self, $it, $val) = @_; | ||
| 34 | # security problems with passed in values. escape them. | ||
| 35 | my $str = "\n<script> document.cookie = \"" . escapeHTML($it) . "=" . | ||
| 36 | escapeHTML($val) . | ||
| 37 | "; path=/; expires=Mon, 16-Sep-2013 22:00:00 GMT\"</script>"; | ||
| 38 | $cached_cookies{$it} = $val; | ||
| 39 | if ($self->{'started'}) { | ||
| 40 | if ($#{$self->{'immediate_out'}}) { | ||
| 41 | print @{$self->{'immediate_out'}}; | ||
| 42 | delete $self->{'immediate_out'}; | ||
| 43 | } | ||
| 44 | print $str; | ||
| 45 | } else { | ||
| 46 | push @{$self->{'immediate_out'}}, $str; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub reset { | ||
| 51 | # XXX: reset real cookies | ||
| 52 | %cached_cookies = (); | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | |||
| 57 | =pod | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | QWizard::Storage::CGICookie - Stores data in web cookies. Requires javascript. | ||
| 62 | |||
| 63 | =head1 SYNOPSIS | ||
| 64 | |||
| 65 | my $st = new QWizard::Storage::CGICookie(); | ||
| 66 | $st->set('var', 'value'); | ||
| 67 | $st->get('var'); | ||
| 68 | |||
| 69 | =head1 DESCRIPTION | ||
| 70 | |||
| 71 | Stores data passed to it inside of web cookies. It requires | ||
| 72 | javascript so that the cookies can be set from anywhere including | ||
| 73 | after the HTTP headers have already been sent. | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Wes Hardaker, hardaker@users.sourceforge.net | ||
| 78 | |||
| 79 | =head1 SEE ALSO | ||
| 80 | |||
| 81 | perl(1) | ||
| 82 | |||
| 83 | Net-Policy: http://net-policy.sourceforge.net/ | ||
| 84 | |||
| 85 | =cut | ||