| 1 | package Crop::HTTP::Session; | ||
| 2 | use base qw/ Crop::Object::Simple /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::HTTP::Session | ||
| 6 | HTTP Session. | ||
| 7 | |||
| 8 | A Session remembers a pair of cookies. If last request fails, previous cookie | ||
| 9 | is used to look up the session. | ||
| 10 | =cut | ||
| 11 | |||
| 12 | use v5.14; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | use Digest::MD5 qw/ md5_hex /; | ||
| 16 | |||
| 17 | use Crop::DateTime; | ||
| 18 | use Crop::HTTP::Session::Thru; | ||
| 19 | |||
| 20 | use Crop::Error; | ||
| 21 | use Crop::Debug; | ||
| 22 | |||
| 23 | =begin nd | ||
| 24 | Variable: our %Attributes | ||
| 25 | Class attributes: | ||
| 26 | |||
| 27 | cookie - last cookie | ||
| 28 | cookie2 - pre last cookie in case of setting new cookie failure | ||
| 29 | ctime - creation time | ||
| 30 | id_client - Client ID; always exists | ||
| 31 | mtime - modified | ||
| 32 | notes - 'fake', supposed users | ||
| 33 | referer - URL to redirect; | ||
| 34 | error - error to client | ||
| 35 | =cut | ||
| 36 | our %Attributes = ( | ||
| 37 | cookie => {mode => 'read'}, | ||
| 38 | cookie2 => undef, | ||
| 39 | ctime => undef, | ||
| 40 | id_client => {mode => 'read/write'}, | ||
| 41 | mtime => {mode => 'write'}, | ||
| 42 | notes => undef, | ||
| 43 | # referer => undef, | ||
| 44 | error => undef, | ||
| 45 | ); | ||
| 46 | |||
| 47 | =begin nd | ||
| 48 | Method: generate_cookie ( ) | ||
| 49 | Set cookie string to the 'cookie' attribute. | ||
| 50 | |||
| 51 | All requests set fresh cookie to a client. | ||
| 52 | |||
| 53 | Copies existing cookie to cookie2. | ||
| 54 | |||
| 55 | This method can be called with server stopped. | ||
| 56 | |||
| 57 | Returns: | ||
| 58 | $self | ||
| 59 | =cut | ||
| 60 | sub generate_cookie { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | if ($self->{cookie}) { | ||
| 64 | $self->{cookie2} = $self->{cookie} if $self->{cookie} eq $self->S->request->cookie_in; | ||
| 65 | } elsif ($self->{cookie2}) { | ||
| 66 | undef $self->{cookie2}; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $tik = $self->S->can('tik') ? $self->S->tik : Crop::DateTime->new; # if the Server is not running | ||
| 70 | |||
| 71 | $self->{cookie} = sprintf "%s-%s%06s", | ||
| 72 | md5_hex(rand), | ||
| 73 | $tik->sec, | ||
| 74 | $tik->usec; | ||
| 75 | |||
| 76 | $self->Modified; | ||
| 77 | } | ||
| 78 | |||
| 79 | =begin nd | ||
| 80 | Method: pass_thru ( ) | ||
| 81 | Save input for next request. | ||
| 82 | |||
| 83 | Form handler pass thru the current request input to the next request for use original user's data by the same form | ||
| 84 | highlighting errors and filling form fields. | ||
| 85 | |||
| 86 | To restore early saved input and errors, use <restore_param ( )> | ||
| 87 | =cut | ||
| 88 | sub pass_thru { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | my $thru = Crop::Object::Collection->new('Crop::HTTP::Session::Thru'); | ||
| 92 | |||
| 93 | # my $rp = $self->S->I; | ||
| 94 | my $param = $self->S->request->param; | ||
| 95 | # debug 'CROPHTTPSESSION_PASSTHRU_REQUEST=', $param; | ||
| 96 | while (my ($p, $v) = each %$param) { | ||
| 97 | $thru->Push({ | ||
| 98 | id_session => $self->{id}, | ||
| 99 | param => $p, | ||
| 100 | value => $v, | ||
| 101 | }); | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | # debug 'CROPHTTPSESSION_PASSTHRU_ATWORK'; | ||
| 107 | my $error; | ||
| 108 | Crop::Error::bind $error; | ||
| 109 | # debug 'CROPHTTPSESSION_PASSTHRU_ERR=', $error; | ||
| 110 | |||
| 111 | # my $thru = Crop::Object::Collection->new('Crop::HTTP::Session::Thru'); | ||
| 112 | while (my ($err, $val) = each %$error) { | ||
| 113 | $thru->Push({ | ||
| 114 | id_session => $self->{id}, | ||
| 115 | param => "ERROR.$err", | ||
| 116 | value => $val, | ||
| 117 | }); | ||
| 118 | } | ||
| 119 | |||
| 120 | # debug 'CROPHTTPSESSION_PASSTHRU_THRU=', $thru; | ||
| 121 | } | ||
| 122 | |||
| 123 | =begin nd | ||
| 124 | Method: restore_thru ( ) | ||
| 125 | |||
| 126 | Restore previously saved parameters to use in form where a handler has failed. | ||
| 127 | =cut | ||
| 128 | sub restore_thru { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | my $thru = Crop::HTTP::Session::Thru->All(id_session => $self->{id}); | ||
| 132 | for ($thru->List) { | ||
| 133 | # debug 'CROPHTTPSESSION_RESTORETHRU_PARAM=', $_; | ||
| 134 | my @param = split '\.', $_->param; | ||
| 135 | # debug 'CROPHTTPSESSION_RESTORETHRU_PARAMS=', \@param; | ||
| 136 | my $last_name = pop @param; | ||
| 137 | |||
| 138 | my $cur_node = $self->S->O; | ||
| 139 | while (my $next = shift @param) { | ||
| 140 | # debug 'CROPHTTPSESSION_RESTORETHRU_NEXT=', $next; | ||
| 141 | $cur_node->{$next} = {} unless exists $cur_node->{$next}; | ||
| 142 | $cur_node = $cur_node->{$next}; | ||
| 143 | } | ||
| 144 | $cur_node->{$last_name} = $_->value; | ||
| 145 | |||
| 146 | $_->Remove; | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->S->request->param; | ||
| 150 | |||
| 151 | # debug 'CROPHTTPSESSION_RESTORETHRU_O=', $self->S->O; | ||
| 152 | |||
| 153 | # for | ||
| 154 | # if (not $param->Is_empty) { | ||
| 155 | # ; # delete $param's | ||
| 156 | # } | ||
| 157 | } | ||
| 158 | |||
| 159 | =begin nd | ||
| 160 | Method: Table ( ) | ||
| 161 | Table in Warehouse. | ||
| 162 | |||
| 163 | Returns: | ||
| 164 | 'session' string | ||
| 165 | =cut | ||
| 166 | sub Table { 'session' } | ||
| 167 | |||
| 168 | 1; |