1 package Gruta
::Template
::Artemus
;
7 use base
'Gruta::Template::BASE';
16 my $a = bless( {}, $class );
18 $a->{_artemus
} = undef;
19 $a->{path
} = $args{path
};
28 if (not $self->{_artemus
}) {
29 my $data = $self->data();
37 return '?t=' . $t . ';' . join(';', @_);
40 $f{'add'} = sub { $_[0] + $_[1]; };
41 $f{'sub'} = sub { $_[0] - $_[1]; };
42 $f{'gt'} = sub { $_[0] > $_[1]; };
43 $f{'lt'} = sub { $_[0] < $_[1]; };
44 $f{'eq'} = sub { $_[0] eq $_[1] ?
1 : 0; };
46 $f{date
} = sub { Gruta
::Data
::today
(); };
47 $f{random
} = sub { $_[rand(scalar(@_))]; };
59 foreach my $e (split(/;/, $list)) {
60 my @e = split(/,/, $e);
73 return join($sep, @ret);
76 foreach my $p (Gruta
::Data
::Topic
->new->afields()) {
77 $f{'topic_' . $p} = sub {
82 $ret = $data->topic($topic)->get($p) || '';
89 foreach my $p (Gruta
::Data
::Story
->new->afields()) {
90 $f{'story_' . $p} = sub {
96 $ret = $data->story($topic_id, $id)->get($p);
103 $f{story_tags
} = sub {
104 my $topic_id = shift;
109 my $story = $data->story($topic_id, $id);
111 $ret = join(', ', $story->tags());
117 $f{story_abstract
} = sub {
118 my $story = $data->story($_[0], $_[1]);
120 return $data->special_uris($story->get('abstract'));
123 $f{story_body
} = sub {
124 my $topic_id = shift;
128 if (my $topic = $data->topic($topic_id)) {
129 if (my $story = $data->story($topic_id, $id)) {
130 my $date2 = $story->get('date2');
132 # if no user and story is not freed, bounce
133 if (!$data->auth() && $date2 && $date2 > Gruta
::Data
::today
()) {
134 $ret = '{-restricted_access}';
137 # touch the story if user is not
138 # (potentially) involved on it
139 if (! $topic->is_editor($data->auth())) {
143 $ret = $data->special_uris($story->get('body'));
151 $f{story_date
} = sub {
153 my $topic_id = shift;
158 $ret = $data->story($topic_id, $id)->date($format);
164 $f{story_date2
} = sub {
166 my $topic_id = shift;
171 $ret = $data->story($topic_id, $id)->date2($format);
177 foreach my $p (Gruta
::Data
::User
->new->afields()) {
178 $f{'user_' . $p} = sub {
183 $ret = $data->user($id)->get($p);
190 $f{user_xdate
} = sub {
196 $ret = $data->user($id)->xdate($format);
207 $t = $data->template->template($t);
208 $ret = $self->{_artemus
}->armor($t);
214 $f{save_template
} = sub {
215 my $template = shift;
219 $content = $self->{_artemus
}->unarmor($content);
220 $data->template->save_template($template, $content);
222 return $msg || "Template saved.";
225 $f{loop_topics
} = sub {
226 my $template = shift;
229 return join($sep, map {
230 my $t = $data->topic($_);
231 sprintf('{-%s|%s|%s}',
232 $template, $t->get('name'),
238 $f{loop_users
} = sub {
239 return join($_[1], map { "{-$_[0]|$_}" } $data->users());
242 $f{loop_renderers
} = sub {
243 return join($_[1], map { "{-$_[0]|$_}" }
244 sort(keys(%{$data->{renderers_h
}})));
247 $f{loop_templates
} = sub {
248 return join($_[1], map { "{-$_[0]|$_}" }
249 $data->template->templates());
252 $f{loop_upload_dirs
} = sub {
253 return join($_[1], map { "{-$_[0]|$_}" }
254 $data->cgi->upload_dirs());
257 $f{loop_story_tags
} = sub {
258 my $topic_id = shift;
261 return join($_[1], map { "{-$_[0]|$_}" }
262 $data->story($topic_id, $id)->tags());
265 $f{story_loop_by_date
} = sub {
269 my $template = shift;
275 return join($sep, map { "{-$template|$topic|$_}" }
276 $data->stories_by_date(
287 $f{is_logged_in
} = sub {
288 return $data->auth() ?
1 : 0;
292 return $data->auth() && $data->auth->get('is_admin') ?
1 : 0;
295 $f{is_topic_editor
} = sub {
296 if (my $topic = $data->topic($_[0])) {
297 return $topic->is_editor($data->auth()) ?
1 : 0;
305 my $password = shift;
306 my $error_msg = shift;
308 if ($user_id eq '' || $user_id eq 'cgi-userid') {
309 $error_msg = '{-login_box}';
311 elsif (my $sid = $data->login($user_id, $password)) {
312 $data->cgi->cookie("sid=$sid");
313 $data->cgi->redirect('?t=INDEX');
317 return $error_msg || 'Login incorrect.';
322 $data->cgi->redirect('?t=INDEX');
328 my $redir = shift || 'ADMIN';
331 $data->cgi->redirect('?t=' . $redir);
339 return $data->auth() && $data->auth->get('username') || '';
343 return $data->auth() && $data->auth->get('id') || '';
346 $f{search_stories
} = sub {
347 my $topic_id = shift;
350 my $template = shift || '_story_link_as_item_with_edit';
351 my $sep = shift || '';
354 my @l = $data->search_stories($topic_id, $query, $future);
357 $ret = "<p><b>{-topic_name|$topic_id}</b><br>\n";
359 $ret .= join($sep, map { "{-$template|$topic_id|$_}" } @l);
361 $self->{search_count
} += scalar(@l);
367 $f{story_loop_top_ten
} = sub {
369 my $internal = shift; # ignored
370 my $template = shift;
373 return join($sep, map { "{-$template|$_->[1]|$_->[2]}" }
374 $data->stories_top_ten($num)
378 $f{is_visible_story
} = sub {
379 if (my $story = $data->story($_[0], $_[1])) {
380 return $story->is_visible($data->auth()) ?
1 : 0;
386 $f{redir_if_archived
} = sub {
387 my $template = shift;
388 my $topic_id = shift;
391 if ($topic_id =~ /-arch$/) {
395 my $story = $data->story($topic_id, $id);
397 if ($story->get('topic_id') =~ /-arch$/) {
398 $data->cgi->redirect(
399 sprintf('?t=%s;topic=%s;id=%s',
401 $story->get('topic_id'),
410 $f{topic_has_archive
} = sub {
411 return $data->topic($_[0] . '-arch') ?
1 : 0;
414 $f{save_topic
} = sub {
415 my $topic_id = shift || return 'Error 1';
419 if (not $topic = $data->topic($topic_id)) {
420 $topic = Gruta
::Data
::Topic
->new (
424 $topic->set('name', shift);
425 $topic->set('editors', shift);
426 $topic->set('internal', shift eq 'on' ?
1 : 0);
427 $topic->set('max_stories', shift);
430 if ($topic->source()) {
431 $topic = $topic->save();
434 $topic = $data->insert_topic($topic);
437 return $topic ?
'OK' : 'Error 2';
440 $f{save_story
} = sub {
441 my $topic_id = shift || return 'Error 1';
446 if (not $story = $data->story($topic_id, $id)) {
447 $story = Gruta
::Data
::Story
->new (
448 topic_id
=> $topic_id,
454 $content = $self->{_artemus
}->unarmor($content);
456 $story->set('content', $content);
458 # pick date and drop time
463 my $date = Gruta
::Data
::today
();
465 if ($y && $m && $d) {
466 $date = sprintf("%04d%02d%02d000000", $y, $m, $d);
469 $story->set('date', $date);
470 $story->set('format', shift || 'grutatxt');
480 if ($y && $m && $d) {
481 $date = sprintf("%04d%02d%02d000000", $y, $m, $d);
487 $story->set('date2', $date);
489 # drop all cached stories
490 $data->flush_story_cache();
492 if ($story->source()) {
493 $story = $story->save();
496 $story = $data->insert_story($story);
499 if ($tags ne 'cgi-tags') {
500 $story->tags(split(/\s*,\s*/, $tags));
503 return $story ?
$story->get('id') : 'Error 2';
506 $f{save_user
} = sub {
507 shift; # new (ignored)
508 my $id = shift || return 'Error 1';
509 my $username = shift;
511 my $is_admin = shift;
512 my $can_upload = shift;
519 if ($data->auth->get('username') ne $username &&
520 ! $data->auth->get('is_admin')) {
521 $data->cgi->redirect('?t=LOGIN');
528 if (not $user = $data->user($id)) {
529 $user = Gruta
::Data
::User
->new (
537 $user->set('username', $username);
538 $user->set('email', $email);
540 # these params can only be set by an admin
541 if ($data->auth->get('is_admin')) {
543 $user->set('is_admin', $is_admin eq 'on' ?
1 : 0);
544 $user->set('can_upload', $can_upload eq 'on' ?
1 : 0);
546 if ($xy and $xm and $xd) {
548 sprintf('%04d%02d%02d000000',
552 $user->set('xdate', '');
556 if ($pass1 and $pass2) {
557 if ($pass1 ne $pass2) {
558 croak
"Passwords are different";
561 my $salt = sprintf('%02d', rand(100));
562 my $pw = crypt($pass1, $salt);
564 $user->set('password', $pw);
567 if ($user->source()) {
568 $user = $user->save();
571 $user = $data->insert_user($user);
574 return $user ?
'OK' : 'Error 2';
579 $data->cgi->upload($_[0], $_[1]);
583 $f{delete_story
} = sub {
584 my $topic_id = shift || return 'Error 1';
587 $data->story($topic_id, $id)->delete();
589 # drop all cached stories
590 $data->flush_story_cache();
595 $f{search_stories_by_tag
} = sub {
597 my $template = shift;
601 my @ret = $data->search_stories_by_tag($tag, $future);
602 $self->{search_count
} = scalar(@ret);
604 return join($sep, map { "{-$template|$_->[0]|$_->[1]}" } @ret);
607 $f{search_count
} = sub { $self->{search_count
}; };
609 $f{content_type
} = sub {
610 $data->cgi->http_headers('Content-Type' => $_[0]);
614 $f{loop_tags
} = sub {
615 return join($_[1], map { "{-$_[0]|$_->[0]|$_->[1]}" }
620 $self->{unresolved
} = [];
621 $self->{search_count
} = 0;
623 $self->{_artemus
} = Artemus
->new(
624 'include-path' => $self->{path
},
627 'unresolved' => $self->{unresolved
},
628 'abort' => \
$self->{abort
},
631 if ($self->{cgi_vars
}) {
632 foreach my $k (keys(%{ $self->{cgi_vars
} })) {
633 my $c = $self->{_artemus
}->
634 armor
($self->{cgi_vars
}->{$k});
642 return $self->{_artemus
};
650 if (defined($data)) {
651 $self->{data
} = $data;
652 $self->{_artemus
} = undef;
655 return $self->{data
};
663 $self->{cgi_vars
} = shift;
664 $self->{_artemus
} = undef;
667 return $self->{cgi_vars
};
671 sub process
{ $_[0]->_artemus->process('{-' . $_[1] . '}'); }