6 package Gruta
::Data
::BASE
;
10 sub fields
{ return (); }
11 sub vfields
{ return (); }
12 sub afields
{ return ($_[0]->fields(), $_[0]->vfields()); }
18 $self->{_source
} = shift;
21 return $self->{_source
};
28 my $id = $self->get('id') || '';
29 $id =~ /^[\d\w_-]+$/ or croak
"Bad id [$id]";
38 my $self = bless({ }, $class);
40 foreach my $k ($self->afields()) {
41 $self->{$k} = $args{$k};
51 croak
'get ' . ref($self) . " field '$field'?" unless exists $self->{$field};
53 return $self->{$field};
60 croak
'set ' . ref($self) . " field '$field'?" unless exists $self->{$field};
62 $self->{$field} = shift;
64 return $self->{$field};
68 package Gruta
::Data
::Topic
;
70 use base
'Gruta::Data::BASE';
72 sub fields
{ return qw(id name editors max_stories internal); }
76 my $user = shift; # Gruta::Data::User
78 return $user && ($user->get('is_admin') ||
79 $self->get('editors') =~ m{\b$user->get('id')\b}) ?
1 : 0;
82 package Gruta
::Data
::Story
;
84 use base
'Gruta::Data::BASE';
88 sub fields
{ return qw(id topic_id title date date2 userid format hits ctime content); }
89 sub vfields
{ return qw(abstract body); }
94 $self->SUPER::_assert
();
96 my $topic_id = $self->get('topic_id') || '';
97 $topic_id =~ /^[\d\w_-]+$/ or croak
"Bad topic_id";
102 sub date
{ return Gruta
::Data
::format_date
($_[0]->get('date'), $_[1]); }
103 sub date2
{ return Gruta
::Data
::format_date
($_[0]->get('date2'), $_[1]); }
105 sub touch
{ return $_[0]; }
112 $self->set('tags', [ @_ ]);
115 @ret = @
{ $self->get('tags') };
124 return sprintf ('%08x', int(rand(0xffffffff)));
129 my $user = shift; # Gruta::Data::User
131 return !$user && $self->get('date') > Gruta
::Data
::today
() ?
0 : 1;
135 package Gruta
::Data
::User
;
137 use base
'Gruta::Data::BASE';
139 sub fields
{ return qw(id username email password can_upload is_admin xdate); }
140 sub vfields
{ return qw(sid); }
142 sub xdate
{ return Gruta
::Data
::format_date
($_[0]->get('xdate'), $_[1]); }
148 $self->set('password', Gruta
::Data
::crypt($passwd));
154 package Gruta
::Data
::Session
;
156 use base
'Gruta::Data::BASE';
158 sub fields
{ return qw(id time user_id ip); }
163 my $sid = time() . $$;
165 return $class->SUPER::new
( id
=> $sid, time => time(), @_);
176 my ($y, $m, $d, $H, $M, $S) = ($date =~
177 /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/);
179 $format =~ s/%Y/$y/g;
180 $format =~ s/%y/$y/g;
181 $format =~ s/%m/$m/g;
182 $format =~ s/%d/$d/g;
183 $format =~ s/%H/$H/g;
184 $format =~ s/%M/$M/g;
185 $format =~ s/%S/$S/g;
198 my ($S,$M,$H,$d,$m,$y) = (localtime)[0..5];
200 my $date = sprintf('%04d%02d%02d%02d%02d%02d',
201 1900 + $y, $m + 1, $d, $H, $M, $S);
203 return Gruta
::Data
::format_date
($date, $format);
211 # no salt? pick one at random
213 $salt = sprintf('%02d', rand(100));
216 return crypt($key, $salt);