]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Admin.pm
added version checks, fixed some issues ;)
[selfforum.git] / selfforum-cgi / shared / Posting / Admin.pm
1 package Posting::Admin;
2
3 ################################################################################
4 # #
5 # File: shared/Posting/Admin.pm #
6 # (was: ~Handle.pm) #
7 # #
8 # Authors: Frank Schoenmann <fs@tower.de>, 2001-03-13 #
9 # Andre Malo <nd@o3media.de>, 2001-03-29 #
10 # #
11 # Description: Allow administration of postings #
12 # #
13 # Todo: * Lock files before modification #
14 # * Change body in change_posting_body() #
15 # * Recursively set invisibility flag in main forum xml by #
16 # hide_posting() and recover_posting() #
17 # #
18 ################################################################################
19
20 use strict;
21
22 use base qw(Exporter);
23
24 @Posting::Admin::EXPORT = qw(hide_posting recover_posting modify_posting add_user_vote level_vote);
25
26 use Lock qw(:READ);
27 use Posting::_lib qw(get_message_node save_file get_all_threads
28 create_forum_xml_string);
29
30 use XML::DOM;
31
32 ### add_user_vote () ###########################################################
33 #
34 # Increase number of user votes (only in thread file)
35 #
36 # Params: $forum Path and filename of forum
37 # $tpath Path to thread files
38 # \%info Hash reference: 'thread', 'posting', 'percent'
39 # Return: Status code (Bool)
40 #
41 # Todo:
42 # * Lock files before modification
43 #
44 sub add_user_vote()
45 {
46 my ($forum, $tpath, $info) = @_;
47 my ($tid, $mid, $percent) = ($info->{'thread'},
48 $info->{'posting'},
49 $info->{'percent'});
50
51 # Thread
52 my $tfile = $tpath . '/t' . $tid . '.xml';
53
54 my $parser = new XML::DOM::Parser;
55 my $xml = $parser->parsefile($tfile);
56
57 my $mnode = get_message_node($xml, $tid, $mid);
58 my $votes = $mnode->getAttribute('votingUser') + 1;
59 $mnode->setAttribute('votingUser', $votes);
60
61 return save_file($tfile, \$xml->toString);
62 }
63
64 ### level_vote () ##############################################################
65 #
66 # Set 1st or 2nd level voting (only in thread file)
67 #
68 # Params: $forum Path and filename of forum
69 # $tpath Path to thread files
70 # \%info Hash reference: 'thread', 'posting', 'level', 'value'
71 # Return: Status code (Bool)
72 #
73 # Todo:
74 # * Lock files before modification
75 #
76 sub level_vote
77 {
78 my ($forum, $tpath, $infoยด) = @_;
79 my ($tid, $mid, $level, $value) = ($info->{'thread'},
80 $info->{'posting'},
81 $info->{'level'},
82 $info->{'value'});
83
84 # Thread
85 my $tfile = $tpath . '/t' . $tid . '.xml';
86
87 my $parser = new XML::DOM::Parser;
88 my $xml = $parser->parsefile($tfile);
89
90 my $mnode = get_message_node($xml, $tid, $mid);
91
92 if ($value == undef)
93 {
94 removeAttribute($level);
95 }
96 else
97 {
98 $mnode->setAttribute($level, $value);
99 }
100
101 return save_file($tfile, \$xml->toString);
102 }
103
104 ### hide_posting () ############################################################
105 #
106 # Hide a posting: set 'invisible' flag
107 #
108 # Params: $forum Path and filename of forum
109 # $tpath Path to thread files
110 # \%info Hash reference: 'thread', 'posting', 'indexFile'
111 # Return: -none-
112 #
113 # Todo:
114 # * set flags recursively in forum xml
115 # * lock files before modification
116 #
117 sub hide_posting($$$)
118 {
119 my ($forum, $tpath, $info) = @_;
120 my ($tid, $mid, $indexFile) = ($info->{'thread'},
121 $info->{'posting'},
122 $info->{'indexFile'});
123
124 # Thread
125 my $tfile = $tpath . '/t' . $tid . '.xml';
126 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 1);
127
128 # Forum
129 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 0, 0); # filter deleted, descending
130
131 for (@{$f->{$tid}})
132 {
133 if ($_->{'mid'} == $mid)
134 {
135 $_->{'deleted'} = 1;
136 }
137 }
138
139 my %cfxs = (
140 'dtd' => $dtd,
141 'lastMessage' => $lmsg,
142 'lastThread' => $lthread
143 );
144 my $xmlstring = create_forum_xml_string($f, \%cfxs);
145 save_file($forum, $$xmlstring);
146 }
147
148 ### recover_posting() ##########################################################
149 #
150 # Recover a posting: delete 'invisible' flag
151 #
152 # Params: $forum Path and filename of forum
153 # $tpath Path to thread files
154 # \%info Hash reference: 'thread', 'posting', 'indexFile'
155 # Return: -none-
156 #
157 # Todo:
158 # * set flags recursive in forum xml
159 # * lock files before modification
160 #
161 sub recover_posting($$$)
162 {
163 my ($forum, $tpath, $info) = @_;
164 my ($tid, $mid, $indexFile) = ($info->{'thread'},
165 $info->{'posting'},
166 $info->{'indexFile'});
167
168 # Thread
169 my $tfile = $tpath . '/t' . $tid . '.xml';
170 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 0);
171
172 # Forum
173 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0); # do not filter deleted, descending
174
175 for (@{$f->{$tid}})
176 {
177 if ($_->{'mid'} == $mid)
178 {
179 $_->{'deleted'} = 0;
180 }
181 }
182
183 my %cfxs = (
184 'dtd' => $dtd,
185 'lastMessage' => $lmsg,
186 'lastThread' => $lthread
187 );
188 my $xmlstring = create_forum_xml_string($f, \%cfxs);
189 save_file($forum, $$xmlstring);
190 }
191
192 ### change_posting_visibility () ###############################################
193 #
194 # Set a postings visibility flag to $invisible
195 #
196 # Params: $fname Filename
197 # $tid Thread ID
198 # $mid Message ID
199 # $invisible 1 - invisible, 0 - visible
200 # Return: Status code
201 #
202 sub change_posting_visibility($$$$)
203 {
204 my ($fname, $tid, $mid, $invisible) = @_;
205
206 my $parser = new XML::DOM::Parser;
207 my $xml = $parser->parsefile($fname);
208
209 # Set flag in given msg
210 my $mnode = get_message_node($xml, $tid, $mid);
211 $mnode->setAttribute('invisible', $invisible);
212
213 # Set flag in sub nodes
214 for ($mnode->getElementsByTagName('Message'))
215 {
216 $_->setAttribute('invisible', $invisible);
217 }
218
219 return save_file($fname, \$xml->toString);
220 }
221
222 ### modify_posting () ##########################################################
223 #
224 # Modify a posting (only subject and category until now!)
225 #
226 # Params: $forum Path and filename of forum
227 # $tpath Path to thread files
228 # \%info Reference: 'thread', 'posting', 'indexFile', 'data'
229 # (data = \%hashref: 'subject', 'category', 'body')
230 # Return: -none-
231 #
232 sub modify_posting($$$)
233 {
234 my ($forum, $tpath, $info) = @_;
235 my ($tid, $mid, $indexFile, $data) = ($info->{'thread'},
236 $info->{'posting'},
237 $info->{'indexFile'},
238 $info->{'data'});
239 my ($subject, $category, $body) = ($data->{'subject'}, $data->{'category'}, $data->{'body'});
240
241 my %msgdata;
242
243 # These values may be changed by change_posting_value()
244 $subject && $msgdata{'Subject'} = $subject;
245 $category && $msgdata{'Category'} = $category;
246
247 # Thread
248 my $tfile = $tpath . '/t' . $tid . '.xml';
249 change_posting_value($tfile, 't'.$tid, 'm'.$mid, \$msgdata);
250 $body && change_posting_body($tfile, 't'.$tid, 'm'.$mid, $body);
251
252 # Forum (does not contain msg bodies)
253 if ($subject or $category)
254 {
255 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0);
256
257 for (@{$f->{$tid}})
258 {
259 if ($_->{'mid'} == $mid)
260 {
261 $subject && $_->{'subject'} = $subject;
262 $category && $_->{'cat'} = $category;
263 }
264 }
265
266 my %cfxs = (
267 'dtd' => $dtd,
268 'lastMessage' => $lmsg,
269 'lastThread' => $lthread
270 );
271 my $xmlstring = create_forum_xml_string($f, \%cfxs);
272 save_file($forum, $$xmlstring);
273 }
274
275 ### change_posting_value () ####################################################
276 #
277 # Change specific values of a posting
278 #
279 # Params: $fname Filename
280 # $tid Thread ID
281 # $mid Message ID
282 # \%values New values
283 # Return: Status code
284 #
285 sub change_posting_value($$$$)
286 {
287 my ($fname, $tid, $mid, $values) = @_;
288
289 my $parser = new XML::DOM::Parser;
290 my $xml = $parser->parsefile($fname);
291
292 my $mnode = get_message_node($xml, $tid, $mid);
293
294 for (keys %$values)
295 {
296 # Find first direct child node with name $_
297 my $nodes = $mnode->getElementsByTagName($_, 0);
298 my $node = $nodes->item(0);
299 $node->setValue($values->{$_});
300 }
301
302 return save_file($fname, \$xml->toString);
303 }
304
305 ### change_posting_body () #####################################################
306 #
307 # Change body of a posting
308 #
309 # Params: $fname Filename
310 # $tid Thread ID (unused, for compatibility purposes)
311 # $mid Message ID
312 # $body New body
313 # Return: Status code
314 #
315 # Todo:
316 # * Change body
317 #
318 sub change_posting_body($$$$)
319 {
320 my ($fname, $tid, $mid, $body) = @_;
321
322 my $parser = new XML::DOM::Parser;
323 my $xml = $parser->parsefile($fname);
324
325 my $mbnody = get_message_body($xml, $mid);
326
327 # todo: change body
328
329 return save_file($fname, \$xml->toString);
330 }
331
332
333 # Let it be true
334 1;

patrick-canterino.de