10 $Gruta::VERSION
= '2.3.1-dev';
11 $Gruta::VERSION_CODENAME
= '"Sienna"';
26 $Gruta::VERSION
. ' ' . $Gruta::VERSION_CODENAME
;
33 print STDERR
$self->{id
}, ' ', scalar(localtime), ': ', $msg, "\n";
39 my $story = shift; # Gruta::Data::Story
41 my $format = $story->get('format') || 'grutatxt';
43 if (my $rndr = $self->{renderers_h
}->{$format}) {
53 $self->{auth
} = shift; # Gruta::Data::User
64 $self->{session
} = shift; # Gruta::Data::Session
67 return $self->{session
};
78 $self->source->purge_old_sessions();
80 if (my $session = $self->source->session($sid)) {
81 $u = $session->source->user( $session->get('user_id') );
85 $self->session($session);
101 if (my $u = $self->source->user( $user_id )) {
103 # account expired? go!
104 if (my $xdate = $u->get('xdate')) {
105 if (Gruta
::Data
::today
() > $xdate) {
110 my $p = $u->get('password');
112 if (Gruta
::Data
::crypt($passwd, $p) eq $p) {
114 my $session = Gruta
::Data
::Session
->new(user_id
=> $user_id);
115 $self->source->insert_session($session);
117 # store user and session
119 $self->session($session);
121 # and return sid to signal a valid login
122 $sid = $session->get('id');
134 if (my $session = $self->session()) {
139 $self->session(undef);
146 $_[0]->{args
}->{base_url
} || ''
152 my $st = shift || '';
155 if (scalar(@_) % 2) {
156 $self->log('Bad url: ' . join(';', $st, @_));
159 my $ret = $self->base_url();
161 # strip all undefined or empty arguments
162 %args = map { $_, $args{$_} } grep { $args{$_} } keys(%args);
164 if ($self->{args
}->{static_urls
}) {
165 my $kn = scalar(keys(%args));
171 'SITEMAP' => 'sitemap.xml',
172 'CSS' => 'style.css',
177 if (exists($p{$st})) {
178 return $ret . $p{$st};
183 if ($st eq 'INDEX' && $args{offset
}) {
184 return $ret . $args{offset
} . '.html';
186 if ($st eq 'TOPIC' && $args{topic
}) {
187 return $ret . $args{topic
} . '/';
189 if ($st eq 'SEARCH_BY_TAG' && $args{tag
}) {
190 return $ret . 'tag/' . $args{tag
} . '.html';
195 if ($st eq 'STORY' && $args{topic
} && $args{id
}) {
196 return $ret . $args{topic
} . '/' . $args{id
} . '.html';
198 if ($st eq 'TOPIC' && $args{topic
} && $args{offset
}) {
199 return $ret . $args{topic
} . '/' . $args{offset
} . '.html';
201 if ($st eq 'SEARCH_BY_DATE' && $args{from
} && $args{to
}) {
202 return $ret . $args{from
} . '-' . $args{to
} . '.html';
210 $ret .= '?' . join(';', map { "$_=$args{$_}" } sort keys(%args));
217 sub _topic_special_uri
{
219 my $topic_id = shift;
223 if (my $t = $self->source->topic($topic_id)) {
224 $ret = sprintf('<a href="%s">%s</a>',
225 $self->url('TOPIC', 'topic' => $topic_id),
230 $ret = "Bad topic $topic_id";
237 sub _story_special_uri
{
239 my $topic_id = shift;
240 my $story_id = shift;
244 if (my $s = $self->source->story($topic_id, $story_id)) {
246 if ($s->is_visible($self->auth())) {
247 $ret = sprintf('<a href = "%s">%s</a>',
249 'topic' => $topic_id,
256 $ret = $s->get('title');
260 my $topic = $self->source->topic($topic_id);
263 $ret = "Bad topic '$topic_id'";
266 $ret = "Bad story '$topic_id/$story_id'";
268 if ($topic->is_editor($self->auth())) {
269 $ret .= sprintf(' <a href = "%s">[+]</a>',
270 $self->url('EDIT_STORY',
271 'topic' => $topic_id,
283 sub _img_special_uri
{
290 # try to load Image::Size
291 eval("use Image::Size;");
294 # if available and image is found, add dimensions to <img>
295 my ($w, $h) = imgsize
('img/' . $src);
298 $more = sprintf('width = "%d" height = "%d"', $w, $h);
302 my $r = sprintf('<img src = "%simg/%s" %s />',
303 $self->base_url(), $src, $more
307 $r = "<span class = '$class'>" . $r . '</span>';
314 sub _content_special_uri
{
316 my $topic_id = shift;
317 my $story_id = shift;
322 if (my $s = $self->source->story($topic_id, $story_id)) {
323 $ret = $self->special_uris($s->get($field));
326 $ret = "Bad story '$topic_id/$story_id'";
338 $string =~ s!topic://([\w\d_-]+)!$self->_topic_special_uri($1)!ge;
339 $string =~ s!story://([\w\d_-]+)/([\w\d_-]+)!$self->_story_special_uri($1,$2)!ge;
340 $string =~ s!img://([\w\d_\.-]+)/?([\w\d_-]*)!$self->_img_special_uri($1,$2)!ge;
341 $string =~ s!body://([\w\d_-]+)/([\w\d_-]+)!$self->_content_special_uri($1,$2,'body')!ge;
342 $string =~ s!abstract://([\w\d_-]+)/([\w\d_-]+)!$self->_content_special_uri($1,$2,'abstract')!ge;
348 sub transfer_to_source
{
352 foreach my $id ($self->source->users()) {
353 my $u = $self->source->user($id);
354 $dst->insert_user($u);
357 foreach my $topic_id (sort $self->source->topics()) {
358 my $t = $self->source->topic($topic_id);
363 if ($nti =~ /-arch$/) {
364 # don't insert topic, just rename
368 $dst->insert_topic($t);
371 foreach my $id ($self->source->stories($topic_id)) {
373 # get story and its tags
374 my $s = $self->source->story($topic_id, $id);
375 my @tags = $s->tags();
378 $s->set('topic_id', $nti);
380 my $ns = $dst->insert_story($s);
388 foreach my $id ($self->source->templates()) {
389 my $t = $self->source->template($id);
390 $dst->insert_template($t);
400 my $g = bless( { @_ } , $class);
402 $g->{id
} ||= 'Gruta';
405 $g->{renderers_h
} = {};
408 if (ref($g->{sources
}) ne 'ARRAY') {
409 $g->{sources
} = [ $g->{sources
} ];
413 $g->{source
} = (@
{$g->{sources
}})[0];
418 $g->source->data($g);
421 if ($g->{renderers
}) {
422 if (ref($g->{renderers
}) ne 'ARRAY') {
423 $g->{renderers
} = [ $g->{renderers
} ];
426 foreach my $r (@
{$g->{renderers
}}) {
427 $g->{renderers_h
}->{$r->{renderer_id
}} = $r;
431 if ($g->{template
}) {
432 $g->template->data($g);