]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Template/Posting.pm
3fafa4a081a5edb76beef0f4b095fc371e4fd169
[selfforum.git] / selfforum-cgi / shared / Template / Posting.pm
1 package Template::Posting;
2
3 ################################################################################
4 # #
5 # File: shared/Template/Posting.pm #
6 # #
7 # Authors: André Malo <nd@o3media.de> #
8 # #
9 # Description: show HTML formatted posting #
10 # #
11 ################################################################################
12
13 use strict;
14 use vars qw(
15 @EXPORT
16 );
17
18 use Encode::Posting;
19 use Encode::Plain; $Encode::Plain::utf8 = 1;
20 use Id;
21 use Lock;
22 use Posting::_lib qw(
23 get_message_node
24 get_message_header
25 get_message_body
26 parse_single_thread
27 parse_xml_file
28 hr_time
29 );
30 use Posting::Cache;
31 use Template;
32 use Template::_conf;
33 use Template::_query;
34 use Template::_thread;
35
36 use XML::DOM;
37
38 ################################################################################
39 #
40 # Version check
41 #
42 # last modified:
43 # $Date$ (GMT)
44 # by $Author$
45 #
46 sub VERSION {(q$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
47
48 ################################################################################
49 #
50 # Export
51 #
52 use base qw(Exporter);
53 @EXPORT = qw(
54 print_posting_as_HTML
55 message_as_HTML
56 );
57
58 ### print_posting_as_HTML () ###################################################
59 #
60 # print HTML formatted Posting to STDOUT
61 #
62 # Params: $threadpath - /path/to/thread_files
63 # $tempfile - template file
64 # $param - Hash-Reference (see doc for details)
65 #
66 # Return: -none-
67 #
68 sub print_posting_as_HTML ($$$) {
69 my ($threadpath, $tempfile, $param) = @_;
70
71 my $template = new Template $tempfile;
72 my $assign = $param -> {assign};
73
74 my $view = get_view_params ({
75 adminDefault => $param -> {adminDefault}
76 });
77
78 my $fh = new Lock ($threadpath.'t'.$param -> {thread}.'.xml');
79
80 unless ($fh -> lock (LH_SHARED)) {
81 print ${$template -> scrap (
82 $assign -> {errorDoc},
83 { $assign -> {errorText} => $template -> insert (
84 $assign -> {
85 $fh -> masterlocked
86 ? 'notAvailable'
87 : 'occupied'
88 }
89 )
90 }
91 )};
92 }
93 else {
94 unless (-f $fh -> filename) {
95 $fh -> unlock;
96 print ${$template -> scrap ($assign -> {errorDoc}, {$assign -> {errorText} => $template -> insert ($assign->{notAvailable})})};
97 }
98 else {
99 my $xml = parse_xml_file ($fh -> filename); #...
100 $fh -> unlock;
101
102 unless ($xml) {
103 print ${$template -> scrap ($assign -> {errorDoc}, {$assign -> {errorText} => $template -> insert ($assign->{corrupt})})};
104 }
105 else {
106 my ($mnode, $tnode) = get_message_node ($xml, 't'.$param -> {thread}, 'm'.$param -> {posting});
107
108 unless ($mnode and not $mnode->getAttribute('invisible')) {
109 print ${$template -> scrap (
110 $assign -> {errorDoc},
111 { $assign -> {errorText} => $template -> insert ($assign -> {'notAvailable'}) }
112 )};
113 }
114 else {
115 my $pnode = $mnode -> getParentNode;
116 my $header = get_message_header ($mnode);
117 my $msg = parse_single_thread ($tnode, $param -> {showDeleted}, $view -> {sortedMsg});
118 my $pheader = ($pnode -> getNodeName eq 'Message')?get_message_header ($pnode):{};
119
120 my $formdata = $param -> {form} -> {data};
121 my $formact = $param -> {form} -> {action};
122
123 my $body = get_message_body ($xml, 'm'.$param -> {posting});
124
125 my $text = message_field (
126 $body,
127 { quoteChars => plain($view -> {quoteChars}),
128 quoting => $view -> {quoting},
129 startCite => ${$template -> scrap ($assign -> {startCite})},
130 endCite => ${$template -> scrap ($assign -> {endCite})}
131 }
132 );
133
134 my $area = answer_field (
135 $body,
136 { quoteArea => 1,
137 quoteChars => plain($view -> {quoteChars}),
138 messages => $param -> {messages}
139 }
140 );
141
142 my $pars = {};
143
144 $pars -> {$formdata -> {$_} -> {assign} -> {name}} = plain($formdata -> {$_} -> {name})
145 for (qw(
146 posterBody
147 uniqueID
148 followUp
149 quoteChar
150 userID
151 posterName
152 posterEmail
153 posterURL
154 posterImage
155 )
156 );
157
158 my $cgi = $param -> {cgi};
159
160 my $tpar = {
161 thread => $param -> {thread},
162 template => $param -> {tree},
163 start => $param -> {posting},
164 cgi => $cgi
165 };
166
167 my $parent_pars;
168
169 $parent_pars = {
170 $assign->{parentTitle} => plain(defined $pheader->{subject} ? $pheader->{subject} : ''),
171 $assign->{parentCat} => plain(defined $pheader->{category} ? $pheader->{category} : ''),
172 $assign->{parentName} => plain(defined $pheader->{name} ? $pheader->{name} : ''),
173 $assign->{parentTime} => plain(hr_time($pheader->{time})),
174 $assign->{parentLink} => query_string (
175 { $cgi -> {thread} => $param -> {thread},
176 $cgi -> {posting} => ($pnode -> getAttribute ('id') =~ /(\d+)/)[0]
177 })
178 } if (%$pheader);
179
180 print ${$template -> scrap (
181 $assign->{mainDoc},
182 { $assign->{name} => plain(defined $header->{name} ? $header->{name} : ''),
183 $assign->{email} => plain(defined $header->{email} ? $header->{email} : ''),
184 $assign->{home} => plain(defined $header->{home} ? $header->{home} : ''),
185 $assign->{image} => plain(defined $header->{image} ? $header->{image} : ''),
186 $assign->{time} => plain(hr_time($header->{time})),
187 $assign->{message} => $text,
188 $assign->{messageTitle} => plain(defined $header->{subject} ? $header->{subject} : ''),
189 $assign->{messageCat} => plain(defined $header->{category} ? $header->{category} : ''),
190 $param->{tree}->{main} => html_thread ($msg, $template, $tpar),
191 $formact->{post}->{assign} => $formact->{post}->{url},
192 $formact->{vote}->{assign} => $formact->{vote}->{url},
193 $formdata->{posterBody}->{assign}->{value} => $area,
194 $formdata->{uniqueID} ->{assign}->{value} => plain(unique_id),
195 $formdata->{followUp} ->{assign}->{value} => plain($param -> {thread}.';'.$param -> {posting}),
196 $formdata->{quoteChar} ->{assign}->{value} => "&#255;".plain(defined $view -> {quoteChars} ? $view -> {quoteChars} : ''),
197 $formdata->{userID} ->{assign}->{value} => '',
198 $assign->{firsttime} => $param->{firsttime} ? $param->{firsttime} : '',
199 $assign->{voted} => $param->{voted} ? $param->{voted} : ''
200 },
201 $pars,
202 $parent_pars
203 )};
204
205 # all output done
206 #
207 close STDOUT;
208
209 if ($param->{firsttime}) {
210 my $cache = new Posting::Cache ($param->{cachepath});
211 $cache -> add_view (
212 { thread => $param -> {thread},
213 posting => $param -> {posting}
214 }
215 );
216 }
217 }
218 }
219 }
220 }
221
222 return;
223 }
224
225 ### message_as_HTML () #########################################################
226 #
227 # create HTML String for the Messagetext
228 #
229 # Params: $xml - XML::DOM::Document object
230 # $template - Template object
231 # $param - Hash reference
232 # (assign, posting, quoteChars, quoting)
233 #
234 # Return: HTML String
235 #
236 sub message_as_HTML ($$$) {
237 my ($xml, $template, $param) = @_;
238
239 my $assign = $param -> {assign};
240 my $body = get_message_body ($xml, $param -> {posting});
241
242 my $text = message_field (
243 $body,
244 { quoteChars => plain ($param -> {quoteChars}),
245 quoting => $param -> {quoting},
246 startCite => ${$template -> scrap ($assign -> {startCite})},
247 endCite => ${$template -> scrap ($assign -> {endCite})}
248 }
249 );
250
251 # return
252 $text;
253 }
254
255 # keep 'require' happy
256 1;
257
258 #
259 #
260 ### end of Template::Posting ###################################################

patrick-canterino.de