10 $Gruta::VERSION
= '2.3.3-dev';
11 $Gruta::VERSION_CODENAME
= '"Volterra"';
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 # special case: full day search
203 $args{from
} =~ /^([0-9]{8})/;
206 if ($d && $args{from
} =~ /^${d}000000$/ && $args{to
} =~ /^${d}235959$/) {
207 return $ret . $d . '/';
210 return $ret . $args{from
} . '-' . $args{to
} . '.html';
214 # if static_urls is 2, *never* return non-static
215 if ($self->{args
}->{static_urls
} == 2) {
223 $ret .= '?' . join(';', map { "$_=$args{$_}" } sort keys(%args));
230 sub _topic_special_uri
{
232 my $topic_id = shift;
236 if (my $t = $self->source->topic($topic_id)) {
237 $ret = sprintf('<a href="%s">%s</a>',
238 $self->url('TOPIC', 'topic' => $topic_id),
243 $ret = "Bad topic $topic_id";
250 sub _story_special_uri
{
252 my $topic_id = shift;
253 my $story_id = shift;
257 if (my $s = $self->source->story($topic_id, $story_id)) {
259 if ($s->is_visible($self->auth())) {
260 $ret = sprintf('<a href = "%s">%s</a>',
262 'topic' => $topic_id,
269 $ret = $s->get('title');
273 my $topic = $self->source->topic($topic_id);
276 $ret = "Bad topic '$topic_id'";
279 $ret = "Bad story '$topic_id/$story_id'";
281 if ($topic->is_editor($self->auth())) {
282 $ret .= sprintf(' <a href = "%s">[+]</a>',
283 $self->url('EDIT_STORY',
284 'topic' => $topic_id,
296 sub _img_special_uri
{
303 # try to load Image::Size
304 eval("use Image::Size;");
307 # if available and image is found, add dimensions to <img>
308 my ($w, $h) = imgsize
('img/' . $src);
311 $more = sprintf('width = "%d" height = "%d"', $w, $h);
315 my $r = sprintf('<img src = "%simg/%s" %s />',
316 $self->base_url(), $src, $more
320 $r = "<span class = '$class'>" . $r . '</span>';
327 sub _thumb_special_uri
{
335 '<a href = "%simg/%s"><img src = "%simg/%s" width = "%d" /></a>',
340 ($self->{args
}->{thumbnail_size
} || 400)
344 $r = "<span class = '$class'>" . $r . '</span>';
351 sub _content_special_uri
{
353 my $topic_id = shift;
354 my $story_id = shift;
359 if (my $s = $self->source->story($topic_id, $story_id)) {
360 $ret = $self->special_uris($s->get($field));
363 $ret = "Bad story '$topic_id/$story_id'";
375 $string =~ s!topic://([\w\d_-]+)!$self->_topic_special_uri($1)!ge;
376 $string =~ s!story://([\w\d_-]+)/([\w\d_-]+)!$self->_story_special_uri($1,$2)!ge;
377 $string =~ s!img://([\w\d_\.-]+)/?([\w\d_-]*)!$self->_img_special_uri($1,$2)!ge;
378 $string =~ s!thumb://([\w\d_\.-]+)/?([\w\d_-]*)!$self->_thumb_special_uri($1,$2)!ge;
379 $string =~ s!body://([\w\d_-]+)/([\w\d_-]+)!$self->_content_special_uri($1,$2,'body')!ge;
380 $string =~ s!abstract://([\w\d_-]+)/([\w\d_-]+)!$self->_content_special_uri($1,$2,'abstract')!ge;
386 sub transfer_to_source
{
390 foreach my $id ($self->source->users()) {
391 my $u = $self->source->user($id);
392 $dst->insert_user($u);
395 foreach my $topic_id (sort $self->source->topics()) {
396 my $t = $self->source->topic($topic_id);
401 if ($nti =~ /-arch$/) {
402 # don't insert topic, just rename
406 $dst->insert_topic($t);
409 foreach my $id ($self->source->stories($topic_id)) {
411 # get story and its tags
412 my $s = $self->source->story($topic_id, $id);
413 my @tags = $s->tags();
416 $s->set('topic_id', $nti);
418 my $ns = $dst->insert_story($s);
426 foreach my $id ($self->source->templates()) {
427 my $t = $self->source->template($id);
428 $dst->insert_template($t);
438 my $g = bless( { @_ } , $class);
440 $g->{id
} ||= 'Gruta';
443 $g->{renderers_h
} = {};
446 if (ref($g->{sources
}) ne 'ARRAY') {
447 $g->{sources
} = [ $g->{sources
} ];
451 $g->{source
} = (@
{$g->{sources
}})[0];
456 $g->source->data($g);
459 if ($g->{renderers
}) {
460 if (ref($g->{renderers
}) ne 'ARRAY') {
461 $g->{renderers
} = [ $g->{renderers
} ];
464 foreach my $r (@
{$g->{renderers
}}) {
465 $g->{renderers_h
}->{$r->{renderer_id
}} = $r;
469 if ($g->{template
}) {
470 $g->template->data($g);
477 # ensure base_url has a final /
478 if ($g->{args
}->{base_url
} && $g->{args
}->{base_url
} !~ /\/$/) {
479 $g->{args
}->{base_url
} .= '/';