Version 2.3.1 (Sienna) RELEASED.
[gruta.git] / Gruta.pm
bloba9a73dc359b761761fdc564475b6d2881b73dcc8
1 package Gruta;
3 use strict;
4 use warnings;
6 use locale;
8 use Gruta::Data;
10 $Gruta::VERSION = '2.3.1';
11 $Gruta::VERSION_CODENAME = '"Sienna"';
13 sub source {
14 $_[0]->{source};
17 sub template {
18 $_[0]->{template};
21 sub cgi {
22 $_[0]->{cgi};
25 sub version {
26 $Gruta::VERSION . ' ' . $Gruta::VERSION_CODENAME;
29 sub log {
30 my $self = shift;
31 my $msg = shift;
33 print STDERR $self->{id}, ' ', scalar(localtime), ': ', $msg, "\n";
37 sub render {
38 my $self = shift;
39 my $story = shift; # Gruta::Data::Story
41 my $format = $story->get('format') || 'grutatxt';
43 if (my $rndr = $self->{renderers_h}->{$format}) {
44 $rndr->story($story);
49 sub auth {
50 my $self = shift;
52 if (@_) {
53 $self->{auth} = shift; # Gruta::Data::User
56 return $self->{auth};
60 sub session {
61 my $self = shift;
63 if (@_) {
64 $self->{session} = shift; # Gruta::Data::Session
67 return $self->{session};
71 sub auth_from_sid {
72 my $self = shift;
73 my $sid = shift;
75 my $u = undef;
77 if ($sid) {
78 $self->source->purge_old_sessions();
80 if (my $session = $self->source->session($sid)) {
81 $u = $session->source->user( $session->get('user_id') );
83 if ($u) {
84 $self->auth($u);
85 $self->session($session);
90 return $u;
94 sub login {
95 my $self = shift;
96 my $user_id = shift;
97 my $passwd = shift;
99 my $sid = undef;
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) {
106 return undef;
110 my $p = $u->get('password');
112 if (Gruta::Data::crypt($passwd, $p) eq $p) {
113 # create new sid
114 my $session = Gruta::Data::Session->new(user_id => $user_id);
115 $self->source->insert_session($session);
117 # store user and session
118 $self->auth($u);
119 $self->session($session);
121 # and return sid to signal a valid login
122 $sid = $session->get('id');
127 return $sid;
131 sub logout {
132 my $self = shift;
134 if (my $session = $self->session()) {
135 $session->delete();
138 $self->auth(undef);
139 $self->session(undef);
141 return $self;
145 sub base_url {
146 $_[0]->{args}->{base_url} || ''
150 sub url {
151 my $self = shift;
152 my $st = shift || '';
153 my %args = @_;
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));
167 if ($kn == 0) {
168 my %p = (
169 'INDEX' => '',
170 'RSS' => 'rss.xml',
171 'SITEMAP' => 'sitemap.xml',
172 'CSS' => 'style.css',
173 'TAGS' => 'tag/',
174 'TOP_TEN' => 'top/'
177 if (exists($p{$st})) {
178 return $ret . $p{$st};
182 if ($kn == 1) {
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';
194 if ($kn == 2) {
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';
207 if ($st) {
208 $args{t} = $st;
210 $ret .= '?' . join(';', map { "$_=$args{$_}" } sort keys(%args));
213 return $ret;
217 sub _topic_special_uri {
218 my $self = shift;
219 my $topic_id = shift;
221 my $ret = undef;
223 if (my $t = $self->source->topic($topic_id)) {
224 $ret = sprintf('<a href="%s">%s</a>',
225 $self->url('TOPIC', 'topic' => $topic_id),
226 $t->get('name')
229 else {
230 $ret = "Bad topic $topic_id";
233 return $ret;
237 sub _story_special_uri {
238 my $self = shift;
239 my $topic_id = shift;
240 my $story_id = shift;
242 my $ret = undef;
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>',
248 $self->url('STORY',
249 'topic' => $topic_id,
250 'id' => $story_id
252 $s->get('title')
255 else {
256 $ret = $s->get('title');
259 else {
260 my $topic = $self->source->topic($topic_id);
262 if (!$topic) {
263 $ret = "Bad topic '$topic_id'";
265 else {
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,
272 'id' => $story_id
279 return $ret;
283 sub _img_special_uri {
284 my $self = shift;
285 my $src = shift;
286 my $class = shift;
288 my $more = '';
290 # try to load Image::Size
291 eval("use Image::Size;");
293 if (!$@) {
294 # if available and image is found, add dimensions to <img>
295 my ($w, $h) = imgsize('img/' . $src);
297 if ($w && $h) {
298 $more = sprintf('width = "%d" height = "%d"', $w, $h);
302 my $r = sprintf('<img src = "%simg/%s" %s />',
303 $self->base_url(), $src, $more
306 if ($class) {
307 $r = "<span class = '$class'>" . $r . '</span>';
310 return $r;
314 sub _content_special_uri {
315 my $self = shift;
316 my $topic_id = shift;
317 my $story_id = shift;
318 my $field = shift;
320 my $ret = undef;
322 if (my $s = $self->source->story($topic_id, $story_id)) {
323 $ret = $self->special_uris($s->get($field));
325 else {
326 $ret = "Bad story '$topic_id/$story_id'";
329 return $ret;
334 sub special_uris {
335 my $self = shift;
336 my $string = shift;
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;
344 return $string;
348 sub transfer_to_source {
349 my $self = shift;
350 my $dst = shift;
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);
360 my $nti = $topic_id;
362 # is it an archive?
363 if ($nti =~ /-arch$/) {
364 # don't insert topic, just rename
365 $nti =~ s/-arch$//;
367 else {
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();
377 # set new topic
378 $s->set('topic_id', $nti);
380 my $ns = $dst->insert_story($s);
382 if (@tags) {
383 $ns->tags(@tags);
388 foreach my $id ($self->source->templates()) {
389 my $t = $self->source->template($id);
390 $dst->insert_template($t);
393 return $self;
397 sub new {
398 my $class = shift;
400 my $g = bless( { @_ } , $class);
402 $g->{id} ||= 'Gruta';
403 $g->{args} ||= {};
405 $g->{renderers_h} = {};
407 if ($g->{sources}) {
408 if (ref($g->{sources}) ne 'ARRAY') {
409 $g->{sources} = [ $g->{sources} ];
412 if (!$g->{source}) {
413 $g->{source} = (@{$g->{sources}})[0];
417 if ($g->{source}) {
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);
435 if ($g->{cgi}) {
436 $g->cgi->data($g);
439 return $g;
442 sub run {
443 my $self = shift;
445 if ($self->{cgi}) {
446 $self->cgi->run();