6 package Gruta
::Data
::BASE
;
10 sub fields
{ return (); }
11 sub vfields
{ return (); }
12 sub afields
{ return ($_[0]->fields(), $_[0]->vfields()); }
13 sub filter_field
{ return $_[2]; }
19 $self->{_source
} = shift;
22 return $self->{_source
};
29 my $id = $self->get('id') || '';
30 $id =~ /^[\d\w_-]+$/ or croak
"Bad id [$id]";
39 my $self = bless({ }, $class);
41 foreach my $k ($self->afields()) {
43 $self->set($k, $args{$k});
53 croak
'get ' . ref($self) . " field '$field'?" unless exists $self->{$field};
55 return $self->{$field};
63 croak
'set ' . ref($self) . " field '$field'?" unless exists $self->{$field};
65 $self->{$field} = $self->filter_field($field, $value);
67 return $self->{$field};
71 package Gruta
::Data
::Topic
;
73 use base
'Gruta::Data::BASE';
75 sub fields
{ return qw(id name editors max_stories internal); }
82 # ensure empty numeric values are 0
83 if ($field =~ /^(max_stories|internal)$/ && !$value) {
92 my $user = shift; # Gruta::Data::User
94 return $user && ($user->get('is_admin') ||
95 $self->get('editors') =~ m{\b$user->get('id')\b}) ?
1 : 0;
98 package Gruta
::Data
::Story
;
100 use base
'Gruta::Data::BASE';
104 sub fields
{ return qw(id topic_id title date date2 userid format hits ctime content); }
105 sub vfields
{ return qw(abstract body); }
112 # ensure empty numeric values are 0
113 if ($field =~ /^(hits|ctime)$/ && !$value) {
123 $self->SUPER::_assert
();
125 my $topic_id = $self->get('topic_id') || '';
126 $topic_id =~ /^[\d\w_-]+$/ or croak
"Bad topic_id";
131 sub date
{ return Gruta
::Data
::format_date
($_[0]->get('date'), $_[1]); }
132 sub date2
{ return Gruta
::Data
::format_date
($_[0]->get('date2'), $_[1]); }
134 sub touch
{ return $_[0]; }
141 $self->set('tags', [ @_ ]);
144 @ret = @
{ $self->get('tags') };
153 return sprintf ('%08x', int(rand(0xffffffff)));
158 my $user = shift; # Gruta::Data::User
160 return !$user && $self->get('date') > Gruta
::Data
::today
() ?
0 : 1;
164 package Gruta
::Data
::User
;
166 use base
'Gruta::Data::BASE';
168 sub fields
{ return qw(id username email password can_upload is_admin xdate); }
169 sub vfields
{ return qw(sid); }
176 # ensure empty numeric values are 0
177 if ($field =~ /^(can_upload|is_admin)$/ && !$value) {
184 sub xdate
{ return Gruta
::Data
::format_date
($_[0]->get('xdate'), $_[1]); }
190 $self->set('password', Gruta
::Data
::crypt($passwd));
196 package Gruta
::Data
::Session
;
198 use base
'Gruta::Data::BASE';
200 sub fields
{ return qw(id time user_id ip); }
205 my $sid = time() . $$;
207 return $class->SUPER::new
( id
=> $sid, time => time(), @_);
218 my ($y, $m, $d, $H, $M, $S) = ($date =~
219 /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/);
221 $format =~ s/%Y/$y/g;
222 $format =~ s/%y/$y/g;
223 $format =~ s/%m/$m/g;
224 $format =~ s/%d/$d/g;
225 $format =~ s/%H/$H/g;
226 $format =~ s/%M/$M/g;
227 $format =~ s/%S/$S/g;
229 if ($format =~ /%w/) {
232 my $t = POSIX
::mktime
(0, 0, 0, $d, $m - 1, $y - 1900);
233 my $w = (localtime($t))[6];
235 $format =~ s/%w/$w/g;
249 my ($S,$M,$H,$d,$m,$y) = (localtime)[0..5];
251 my $date = sprintf('%04d%02d%02d%02d%02d%02d',
252 1900 + $y, $m + 1, $d, $H, $M, $S);
254 return Gruta
::Data
::format_date
($date, $format);
262 # no salt? pick one at random
264 $salt = sprintf('%02d', rand(100));
267 return crypt($key, $salt);