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 my $salt = sprintf('%02d', rand(100));
149 my $pw = crypt($passwd, $salt);
151 $self->set('password', $pw);
157 package Gruta
::Data
::Session
;
159 use base
'Gruta::Data::BASE';
161 sub fields
{ return qw(id time user_id ip); }
170 my ($y, $m, $d, $H, $M, $S) = ($date =~
171 /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/);
173 $format =~ s/%Y/$y/g;
174 $format =~ s/%y/$y/g;
175 $format =~ s/%m/$m/g;
176 $format =~ s/%d/$d/g;
177 $format =~ s/%H/$H/g;
178 $format =~ s/%M/$M/g;
179 $format =~ s/%S/$S/g;
192 my ($S,$M,$H,$d,$m,$y) = (localtime)[0..5];
194 my $date = sprintf('%04d%02d%02d%02d%02d%02d',
195 1900 + $y, $m + 1, $d, $H, $M, $S);
197 return Gruta
::Data
::format_date
($date, $format);