]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Write.pm
1 package Posting
::Write
;
3 ################################################################################
5 # File: shared/Posting/Write.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-02-25 #
9 # Description: Save a posting #
11 ################################################################################
15 use base
qw(Exporter);
17 # ====================================================
19 # ====================================================
21 @Posting::Write
::EXPORT
= qw(write_posting);
23 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
25 use Lock
qw(:WRITE release_file);
26 use Posting
::_lib
qw(get_message_node get_message_header create_forum_xml_string save_file);
30 ################################
33 # Neues Posting speichern
34 ################################
36 sub write_posting
($) {
39 my $mid = 'm'.($param -> {lastMessage
} + 1);
41 my $pars = {quoteChars
=> $param -> {quoteChars
},
42 messages
=> $param -> {messages
}};
44 my %error = (threadWrite
=> '1 could not write thread file',
45 forumWrite
=> '2 could not write forum file',
46 threadFile
=> '3 could not load thread file',
47 noParent
=> '4 could not find parent message');
50 unless ($param -> {parentMessage
}) {
51 $tid = 't'.($param -> {lastThread
} + 1);
52 $thread = create_new_thread
({msg
=> $mid,
54 name
=> $param -> {author
},
55 email
=> $param -> {email
},
56 home
=> $param -> {homepage
},
57 image
=> $param -> {image
},
58 category
=> $param -> {category
},
59 subject
=> $param -> {subject
},
60 time => $param -> {time},
61 dtd
=> $param -> {dtd
},
63 body
=> $param -> {body
},
66 save_file
($param -> {messagePath
}.$tid.'.xml',\
($thread -> toString
)) or return $error{threadWrite
};
69 $param -> {parsedThreads
}
70 -> {$param -> {lastThread
} + 1} = [{mid
=> $param -> {lastMessage
} + 1,
71 unid
=> $param -> {uniqueID
},
72 name
=> plain
($param -> {author
}),
73 cat
=> plain
(length($param -> {category
})?
$param->{category
}:''),
74 subject
=> plain
($param -> {subject
}),
75 time => plain
($param -> {time})}];
77 my $forum = create_forum_xml_string
($param -> {parsedThreads
},
78 {dtd
=> $param -> {dtd
},
82 save_file
($param -> {forumFile
}, $forum) or return $error{forumWrite
};
83 release_file
($param -> {messagePath
}.$tid.'.xml');
84 return (0, $thread, $mid);}
88 $tid = 't'.($param -> {thread
});
89 my $tfile = $param -> {messagePath
}.$tid.'.xml';
92 unless (write_lock_file
($tfile)) {
93 violent_unlock_file
($tfile);
94 return $error{threadFile
};}
97 $xml = eval {local $SIG{__DIE__
}; new XML
::DOM
::Parser
(KeepCDATA
=> 1) -> parsefile
($tfile);};
100 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
101 return $error{threadFile
};}
103 my $mnode = get_message_node
($xml, $tid, 'm'.$param -> {parentMessage
});
105 unless (defined $mnode) {
106 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
107 return $error{noParent
};}
109 my $pheader = get_message_header
($mnode);
111 my $message = create_message
($xml,
113 ip
=> $param -> {ip
},
114 name
=> $param -> {author
},
115 email
=> $param -> {email
},
116 home
=> $param -> {homepage
},
117 image
=> $param -> {image
},
118 category
=> length($param -> {category
})?
$param -> {category
}:$pheader -> {category
},
119 subject
=> length($param -> {subject
})?
$param -> {subject
}:$pheader -> {subject
},
120 time => $param -> {time},
123 $mnode -> appendChild
($message);
125 my $mcontent = $xml -> createElement
('MessageContent');
126 $mcontent -> setAttribute
('mid', $mid);
127 $mcontent -> appendChild
($xml -> createCDATASection
(${encoded_body
(\
($param -> {body
}), $pars)}));
129 my $content = $xml -> getElementsByTagName
('ContentList', 1) -> item
(0);
130 $content -> appendChild
($mcontent);
132 unless (save_file
($tfile, \
($xml -> toString
))) {
133 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
134 return $error{threadWrite
};}
136 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
141 # ACHTUNG! danach kann der Threadbaum nicht mehr fuer die visuelle
142 # Ausgabe genutzt werden, da die answers nicht angepasst werden
143 # (und somit nicht mehr stimmen...)
146 my $cat = length($param -> {category
})?
$param -> {category
}:$pheader -> {category
};
147 my $subj = length($param -> {subject
})?
$param -> {subject
}:$pheader -> {subject
};
149 for (@
{$param -> {parsedThreads
} -> {$param -> {thread
}}}) {
150 if ($_ -> {mid
} == $param -> {parentMessage
}) {
151 splice @
{$param -> {parsedThreads
} -> {$param -> {thread
}}},$i,0,
152 {mid
=> $param -> {lastMessage
} + 1,
153 unid
=> $param -> {uniqueID
},
154 name
=> plain
($param -> {author
}),
155 cat
=> plain
(length($cat)?
$cat:''),
156 subject
=> plain
(length($subj)?
$subj:''),
157 level
=> $_ -> {level
} + 1,
158 time => plain
($param -> {time})};
162 my $forum = create_forum_xml_string
($param -> {parsedThreads
},
163 {dtd
=> $param -> {dtd
},
165 lastThread
=> 't'.$param -> {lastThread
}});
167 save_file
($param -> {forumFile
}, $forum) or return $error{forumWrite
};
170 return (0, $thread, $mid);
174 # ====================================================
176 # ====================================================
178 sub create_message
($$) {
181 my $message = $xml -> createElement
('Message');
182 $message -> setAttribute
('id', $par -> {msg
});
183 $message -> setAttribute
('ip', $par -> {ip
});
186 my $header = $xml -> createElement
('Header');
188 # alles inside of 'Header'
189 my $author = $xml -> createElement
('Author');
190 my $name = $xml -> createElement
('Name');
191 $name -> addText
($par -> {name
});
192 $author -> appendChild
($name);
194 my $email = $xml -> createElement
('Email');
195 $email -> addText
($par -> {email
});
196 $author -> appendChild
($email);
198 if (length ($par -> {home
})) {
199 my $home = $xml -> createElement
('HomepageUrl');
200 $home -> addText
($par -> {home
});
201 $author -> appendChild
($home);}
203 if (length ($par -> {image
})) {
204 my $image = $xml -> createElement
('ImageUrl');
205 $image -> addText
($par -> {image
});
206 $author -> appendChild
($image);}
208 my $category = $xml -> createElement
('Category');
209 $category -> addText
($par -> {category
});
211 my $subject = $xml -> createElement
('Subject');
212 $subject -> addText
($par -> {subject
});
214 my $date = $xml -> createElement
('Date');
215 $date -> setAttribute
('longSec', $par -> {time});
217 $header -> appendChild
($author);
218 $header -> appendChild
($category);
219 $header -> appendChild
($subject);
220 $header -> appendChild
($date);
221 $message -> appendChild
($header);
226 sub create_new_thread
($) {
230 my $xml = new XML
::DOM
::Document
;
233 my $decl = new XML
::DOM
::XMLDecl
;
234 $decl -> setVersion
('1.0');
235 $decl -> setEncoding
('UTF-8');
236 $xml -> setXMLDecl
($decl);
239 my $dtd = $xml -> createDocumentType
('Forum', $par -> {dtd
}, undef, undef);
240 $xml -> setDoctype
($dtd);
243 my $forum = $xml -> createElement
('Forum');
246 my $thread = $xml -> createElement
('Thread');
247 $thread -> setAttribute
('id', $par -> {thread
});
250 my $message = create_message
($xml,$par);
253 my $content = $xml -> createElement
('ContentList');
254 my $mcontent = $xml -> createElement
('MessageContent');
255 $mcontent -> setAttribute
('mid', $par -> {msg
});
256 $mcontent -> appendChild
($xml -> createCDATASection
(${encoded_body
(\
($par -> {body
}), $par -> {pars
} )}));
258 # die ganzen Nodes verknuepfen
259 $thread -> appendChild
($message);
260 $forum -> appendChild
($thread);
262 $content -> appendChild
($mcontent);
263 $forum -> appendChild
($content);
265 $xml -> appendChild
($forum);
267 # und fertiges Dokument zurueckgeben
271 # ====================================================
272 # Modulinitialisierung
273 # ====================================================
275 # making require happy
278 # ====================================================
279 # end of Posting::Write
280 # ====================================================
patrick-canterino.de