| 1 | package CGI::kSession; | ||
| 2 | $CGI::kSession::VERSION = '0.5.3'; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | sub new { | ||
| 6 | my ($c, %args) = @_; | ||
| 7 | my $class = ref($c) || $c; | ||
| 8 | $args{SID} = $args{id}; | ||
| 9 | bless \%args, $class; | ||
| 10 | } | ||
| 11 | |||
| 12 | sub start { | ||
| 13 | my $cl = shift; | ||
| 14 | if (!exists($cl->{lifetime})) { $cl->{lifetime} = 600; } | ||
| 15 | if (!exists($cl->{path})) { $cl->{path} = "/var/tmp/"; } | ||
| 16 | # ustawia nowy ID jesli nie jest zaden podany juz | ||
| 17 | if ((!exists($cl->{SID})) || ((length($cl->{SID}) == 0))) { $cl->id($cl->newID()); } | ||
| 18 | |||
| 19 | if (-e $cl->getfile()) { return 0; } | ||
| 20 | # nowy plik sesji jesli takowy juz nie istnieje | ||
| 21 | open(SF,">".$cl->getfile()); close(SF); | ||
| 22 | $cl->check_sessions(); | ||
| 23 | return 1; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub check_sessions { | ||
| 27 | my $cl = shift; | ||
| 28 | opendir(SD,$cl->{path}); | ||
| 29 | my @files = readdir(SD); | ||
| 30 | shift @files; | ||
| 31 | shift @files; | ||
| 32 | foreach my $f (@files) { | ||
| 33 | if (((stat($cl->{path}.$f))[9] + $cl->{lifetime}) < time()) { | ||
| 34 | unlink($cl->{path}.$f); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | closedir(SD); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub destroy { | ||
| 41 | my $cl = shift; | ||
| 42 | if (!$cl->have_id()) { return -1; } | ||
| 43 | if (-e $cl->getfile()) { unlink($cl->getfile()); } | ||
| 44 | undef $cl->{SID}; | ||
| 45 | if (defined($cl->{id})) { undef $cl->{id}; } | ||
| 46 | return 1; | ||
| 47 | } | ||
| 48 | |||
| 49 | #czy sesja o podanym id istnieje | ||
| 50 | sub exists { | ||
| 51 | my ($cl,$id) = @_; | ||
| 52 | if (!defined($id)) { return 0; } | ||
| 53 | my $file = $cl->{path}.$cl->{$id}; | ||
| 54 | if (-e $file) { return 1; } | ||
| 55 | return 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub have_id { | ||
| 59 | my $cl = shift; | ||
| 60 | if (!exists($cl->{SID})) { return 0; } | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub save_path { | ||
| 65 | my ($cl, $path) = @_; | ||
| 66 | if (defined($path)) { $cl->{path} = $path } | ||
| 67 | return $cl->{path}; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub id { | ||
| 71 | my ($cl, $newid) = @_; | ||
| 72 | if (!$cl->have_id()) { return -1; } | ||
| 73 | if (defined($newid)) { $cl->{SID} = $newid; } | ||
| 74 | return $cl->{SID}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub getfile { | ||
| 78 | my $cl = shift; | ||
| 79 | return $cl->{path}.$cl->{SID}; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub is_registered { | ||
| 83 | my ($cl,$name) = @_; | ||
| 84 | if (!$cl->have_id()) { return -1; } | ||
| 85 | if (-e $cl->getfile()) { | ||
| 86 | open(SF,$cl->getfile); | ||
| 87 | while (my $l = <SF>) { | ||
| 88 | my @line = split (/=/,$l); | ||
| 89 | if ($line[0] eq $name) { | ||
| 90 | close(SF); | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | close(SF); | ||
| 95 | } | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub register { | ||
| 101 | my ($cl,$name) = @_; | ||
| 102 | if (!$cl->have_id()) { return -1; } | ||
| 103 | if ($cl->is_registered($name)) { return 0; } | ||
| 104 | if (-e $cl->getfile()) { | ||
| 105 | open(SF,">>".$cl->getfile()); | ||
| 106 | print SF $name."=\n"; | ||
| 107 | close(SF); | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub unregister { | ||
| 114 | my ($cl,$name) = @_; | ||
| 115 | my $content = ""; | ||
| 116 | if (!$cl->have_id()) { return -1; } | ||
| 117 | if (!$cl->is_registered($name)) { return 0; } | ||
| 118 | open(SF,$cl->getfile()); | ||
| 119 | while (my $l = <SF>) { | ||
| 120 | $l =~ s/^$name=(.*?)\n//i; | ||
| 121 | $content .= $l; | ||
| 122 | } | ||
| 123 | close(SF); | ||
| 124 | open(SF,">".$cl->getfile()); | ||
| 125 | # $content =~ s/$name=(.*?)\n//gis; | ||
| 126 | print SF $content; | ||
| 127 | close(SF); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub unset { | ||
| 131 | my $cl = shift; | ||
| 132 | open(SF,">".$cl->getfile()); | ||
| 133 | close(SF); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub get { | ||
| 137 | my ($cl,$name) = @_; | ||
| 138 | if (!$cl->have_id()) { return -1; } | ||
| 139 | if ($cl->is_registered($name)) { | ||
| 140 | if (-e $cl->getfile()) { | ||
| 141 | open(SF,$cl->getfile()); | ||
| 142 | while (my $l = <SF>) { | ||
| 143 | if ($l =~ /^$name=(.*?)\n/i) { | ||
| 144 | close(SF); | ||
| 145 | return $1; | ||
| 146 | } | ||
| 147 | # my @line = split (/=/,$l); | ||
| 148 | # if ($line[0] eq $name) { | ||
| 149 | # close(SF); | ||
| 150 | # return $line[1]; | ||
| 151 | # } | ||
| 152 | } | ||
| 153 | close(SF); | ||
| 154 | } | ||
| 155 | } else { | ||
| 156 | return -1; | ||
| 157 | }; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub set { | ||
| 161 | my ($cl,$name,$value) = @_; | ||
| 162 | if (!$cl->have_id()) { return -1; } | ||
| 163 | if ($cl->is_registered($name)) { | ||
| 164 | my $content = ""; | ||
| 165 | |||
| 166 | open(SF,$cl->getfile()); | ||
| 167 | while (my $l = <SF>) { | ||
| 168 | $l =~ s/^$name=(.*?)\n/$name=$value\n/gis; | ||
| 169 | $content .= $l; | ||
| 170 | } | ||
| 171 | close(SF); | ||
| 172 | # $content =~ s/$name=(.*?)\n/$name=$value\n/gis; | ||
| 173 | open(SF,">".$cl->getfile()); | ||
| 174 | print SF $content; | ||
| 175 | close(SF); | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub newID { | ||
| 183 | my $cl = shift; | ||
| 184 | my $ary = "0123456789abcdefghijABCDEFGH"; # replace with the set of characters | ||
| 185 | $cl->{SID} = ""; | ||
| 186 | my $arylen = length($ary); | ||
| 187 | for my $i (0 .. 23) { | ||
| 188 | my $idx = int(rand(time) % $arylen); | ||
| 189 | my $dig = substr($ary, $idx, 1); | ||
| 190 | # if ($i > 0) { | ||
| 191 | # if ($i % 8 == 0) { $cl->{SID} .= "-"; } | ||
| 192 | # elsif ($i % 4 == 0) {$cl->{SID} .= "_"; } | ||
| 193 | # } | ||
| 194 | $cl->{SID} .= $dig; | ||
| 195 | } $cl->{SID}; | ||
| 196 | } | ||
| 197 | |||
| 198 | ############################# | ||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 | __END__ |