]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Forum.pm
810f547af6346541ca99576d5c6e4f5bb0532ffb
[ndwebbie.git] / NDWeb / Forum.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package NDWeb::Forum;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw{:standard};
24 use HTML::Template;
25 use NDWeb::Include;
26 require Exporter;
27
28 our @ISA = qw/Exporter/; 
29 our @EXPORT = qw/viewForumThread addForumPost addForumThread markThreadAsRead/;
30
31 sub viewForumThread {
32         my ($thread) = @_;
33
34         my $template = HTML::Template->new(filename => "templates/viewthread.tmpl", global_vars => 1, cache => 1);
35
36         $template->param(Id => $thread->{id});
37         $template->param(Post => $thread->{post});
38
39         my $posts = $ND::DBH->prepare(q{SELECT u.username,date_trunc('seconds',fp.time::timestamp) AS time,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
40 FROM forum_threads ft JOIN forum_posts fp USING (ftid) JOIN users u ON u.uid = fp.uid LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
41 WHERE ft.ftid = $1
42 ORDER BY fp.time ASC
43 });
44         $posts->execute($thread->{id},$ND::UID) or $ND::ERROR .= p($ND::DBH->errstr);
45         my @posts;
46         my $old = 1;
47         while (my $post = $posts->fetchrow_hashref){
48                 if ($old && $post->{unread}){
49                         $old = 0;
50                         $post->{NewPosts} = 1;
51                 }
52                 $post->{message} = parseMarkup($post->{message});
53                 push @posts,$post;
54         }
55
56         if (defined param('cmd') && param('cmd') eq 'Preview'){
57                 my $text = parseMarkup(escapeHTML(param('message')));
58                 $text .= p b $@ if $@;
59                 push @posts,{message => $text, unread => 1, username => 'PREVIEW', Time => 'Not submitted yet', NewPosts => $old ? 1 : 0};
60
61                 $text = escapeHTML param('message');
62                 $text =~ s/\x{3}\d\d?//g; #mirc color TODO: possibly match until \x{0F} and change to [color] block
63                 $text =~ s/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
64                 $template->param(Message => $text);
65         }
66         $template->param(Posts => \@posts);
67
68         markThreadAsRead($thread->{id});
69
70         return $template->output;
71 }
72
73 sub addForumPost {
74         my ($dbh,$thread,$uid,$message) = @_;
75         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid) VALUES($1,$2,$3)});
76         unless ($insert->execute($thread->{id},escapeHTML($message),$uid)){
77                 $ND::ERROR .= p($dbh->errstr);
78                 return 0;
79         }
80         return 1;
81 }
82
83 sub addForumThread {
84         my ($dbh,$board,$uid,$subject) = @_;
85
86         my $insert = $dbh->prepare(q{INSERT INTO forum_threads (fbid,subject,uid) VALUES($1,$2,$3)});
87
88         if ($insert->execute($board->{id},escapeHTML($subject),$uid)){
89                 my $id = $dbh->last_insert_id(undef,undef,undef,undef,"forum_threads_ftid_seq");
90                 return $dbh->selectrow_hashref(q{SELECT ftid AS id, subject, $2::boolean AS post FROM forum_threads WHERE ftid = $1}
91                         ,undef,$id,$board->{post})
92                         or $ND::ERROR .= p($dbh->errstr);
93         }else{
94                 $ND::ERROR .= p($dbh->errstr);
95         }
96 }
97
98 sub markThreadAsRead {
99         my ($thread) = @_;
100         my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now() 
101 WHERE uid =     $1 AND ftid = $2},undef,$ND::UID,$thread);
102         if ($rows == 0){
103                 $ND::DBH->do(q{INSERT INTO forum_thread_visits (uid,ftid) VALUES ($1,$2)}
104                         ,undef,$ND::UID,$thread) or $ND::ERROR .= p($ND::DBH->errstr);
105         }elsif(not defined $rows){
106                 $ND::ERROR .= p($ND::DBH->errstr);
107         }
108 }
109
110 1;