Ajoute la possibilité de poster une piÚce jointe par message...
[banana.git] / banana / banana.inc.php.in
CommitLineData
78cd27b3 1<?php
2/********************************************************************************
3* install.d/config.inc.php : configuration file
4* --------------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10class Banana
11{
12 var $maxspool = 3000;
13
14 var $hdecode = array('from','name','organization','subject');
7a0e2710 15 var $parse_hdr = array('content-disposition', 'content-transfer-encoding', 'content-type', 'date', 'followup-to', 'from',
78cd27b3 16 'message-id', 'newsgroups', 'organization', 'references', 'subject', 'x-face');
17 var $show_hdr = array('from', 'subject', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face');
18
19
20 var $tbefore = 5;
21 var $tafter = 5;
22 var $tmax = 50;
23
24 var $wrap = 74;
25
0a61409e 26 var $boundary = "bananaBoundary42";
78cd27b3 27 var $custom = "Content-Type: text/plain; charset=utf-8\nMime-Version: 1.0\nContent-Transfer-Encoding: 8bit\nUser-Agent: Banana @VERSION@\n";
0a61409e 28 var $custom_mp = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\nUser-Agent: Banana @VERSION@\n";
29 var $custom_bd = "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit";
78cd27b3 30
31 var $host = 'news://localhost:119/';
32
33 var $profile = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig' => '', 'org' => '',
34 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
35
36 var $state = Array('group' => null, 'artid' => null);
37 var $nntp;
38 var $groups;
39 var $newgroups;
40 var $post;
41 var $spool;
42
43 function Banana()
44 {
45 $this->_require('NetNNTP');
46 setlocale(LC_ALL, $this->profile['locale']);
47 $this->nntp = new nntp($this->host);
48 }
49
50 function run($class = 'Banana')
51 {
52 global $banana;
53 Banana::_require('misc');
54 $banana = new $class();
55
56 if (!$banana->nntp) {
57 return '<p class="error">'._b_('Impossible de contacter le serveur').'</p>';
58 }
59
60 $group = empty($_GET['group']) ? null : strtolower($_GET['group']);
61 $artid = empty($_GET['artid']) ? null : strtolower($_GET['artid']);
1f75b135 62 $partid = empty($_GET['part']) ? 0 : $_GET['part'];
78cd27b3 63 $banana->state = Array ('group' => $group, 'artid' => $artid);
64
65 if (is_null($group)) {
66
67 if (isset($_GET['subscribe'])) {
68 return $banana->action_listSubs();
69 } elseif (isset($_POST['subscribe'])) {
70 $banana->action_saveSubs();
71 }
72 return $banana->action_listGroups();
73
74 } elseif (is_null($artid)) {
78cd27b3 75 if (isset($_POST['action']) && $_POST['action'] == 'new') {
76 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
77 } elseif (isset($_GET['action']) && $_GET['action'] == 'new') {
78 return $banana->action_newFup($group);
79 } else {
80 return $banana->action_showThread($group, isset($_GET['first']) ? intval($_GET['first']) : 1);
81 }
82
83 } else {
84
85 if (isset($_POST['action']) && $_POST['action']=='cancel') {
86 $res = $banana->action_cancelArticle($group, $artid);
87 } else {
88 $res = '';
89 }
90
91 if (isset($_GET['action'])) {
92 switch ($_GET['action']) {
93 case 'cancel':
1f75b135 94 $res .= $banana->action_showArticle($group, $artid, $partid);
78cd27b3 95 if ($banana->post->checkcancel()) {
96 $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
97 . "<form action=\"?group=$group&amp;artid=$artid\" method='post'><p>"
98 . '<input type="hidden" name="action" value="cancel" />'
99 . '<input type="submit" value="Annuler !" />'
100 . '</p></form>';
101 return $form.$res;
102 }
103 return $res;
104
105 case 'new':
106 return $banana->action_newFup($group, $artid);
107 }
108 }
7a0e2710 109
110 if (isset($_GET['pj'])) {
111 $action = false;
112 if (isset($_GET['action']) && $_GET['action'] == 'view') {
113 $action = true;
114 }
115 return $banana->action_getAttachment($group, $artid, $_GET['pj'], $action);
116 }
117
1f75b135 118 return $res . $banana->action_showArticle($group, $artid, $partid);
78cd27b3 119 }
120 }
121
122 /**************************************************************************/
123 /* actions */
124 /**************************************************************************/
125
126 function action_saveSubs()
127 {
128 return;
129 }
130
131 function action_listGroups()
132 {
133 $this->_newGroup();
134
135 $cuts = displayshortcuts();
136 $res = '<h1>'._b_('Les forums de Banana').'</h1>'.$cuts.$this->groups->to_html();
137 if (count($this->newgroups->overview)) {
138 $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
139 $res .= $this->newgroups->to_html();
140 }
141
142 $this->nntp->quit();
143 return $res.$cuts;
144 }
145
146 function action_listSubs()
147 {
148 $this->_require('groups');
149 $this->groups = new BananaGroups(BANANA_GROUP_ALL);
150
151 $cuts = displayshortcuts();
152 $res = '<h1>'._b_('Abonnements').'</h1>'.$cuts.$this->groups->to_html(true).$cuts;
153
154 $this->nntp->quit();
155 return $res;
156 }
157
158 function action_showThread($group, $first)
159 {
160 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
161
162 if ($first > count($this->spool->overview)) {
163 $first = count($this->spool->overview);
164 }
165
166 $first = $first - ($first % $this->tmax) + 1;
167
168 $cuts = displayshortcuts($first);
169
170 $res = '<h1>'.$group.'</h1>'.$cuts;
171 $res .= $this->spool->to_html($first, $first+$this->tmax);
172
173 $this->nntp->quit();
174
175 return $res.$cuts;
176 }
177
1f75b135 178 function action_showArticle($group, $id, $part)
78cd27b3 179 {
180 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
181 $this->_newPost($id);
182 if (!$this->post) {
183 if ($this->nntp->lasterrorcode == "423") {
184 $this->spool->delid($id);
185 }
186 $this->nntp->quit();
187 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
188 }
189
190 $cuts = displayshortcuts();
191 $res = '<h1>'._b_('Message').'</h1>'.$cuts;
1f75b135 192 $res .= $this->post->to_html($part);
78cd27b3 193
194 $this->nntp->quit();
195
196 return $res.$cuts;
197 }
198
7a0e2710 199 function action_getAttachment($group, $id, $pjid, $action)
200 {
201 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
202 $this->_newPost($id);
203 if (!$this->post) {
204 if ($this->nntp->lasterrorcode == "423") {
205 $this->spool->delid($id);
206 }
207 $this->nntp->quit();
208 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
209 }
210
211 $this->nntp->quit();
212 if ($this->post->get_attachment($pjid, $action)) {
213 return "";
214 } else {
215 return displayshortcuts().'<p calss="error">'._b_('Impossible d\'accéder à la pièce jointe.').'</p>';
216 }
217 }
218
78cd27b3 219 function action_cancelArticle($group, $id)
220 {
221 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
222 $this->_newPost($id);
223 $mid = array_search($id, $this->spool->ids);
224
225 if (!$this->post->checkcancel()) {
226 return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>';
227 }
228 $msg = 'From: '.$this->profile['name']."\n"
229 . "Newsgroups: $group\n"
230 . "Subject: cmsg $mid\n"
231 . $this->custom
232 . "Control: cancel $mid\n"
233 . "\n"
234 . "Message canceled with Banana";
235 if ($this->nntp->post($msg)) {
236 $this->spool->delid($id);
237 $this->nntp->quit();
238 header("Location: ?group=$group&amp;first=$id");
239 } else {
240 return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
241 }
242 }
243
244 function action_newFup($group, $id = -1)
245 {
246 $subject = $body = '';
247 $target = $group;
248
249 if ($id > 0) {
250 $this->nntp->group($group);
251 $this->_newPost($id);
252 if ($this->post) {
253 $subject = preg_replace("/^re\s*:\s*/i", '', 'Re: '.$this->post->headers['subject']);
254 $body = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->body, "> ");
255 $target = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
256 }
257 }
258
259 $this->nntp->quit();
260
261 $cuts = displayshortcuts();
262 $html = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
0a61409e 263 $html .= '<form enctype="multipart/form-data" action="?group='.$group.'" method="post" accept-charset="utf-8">';
78cd27b3 264 $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
265 $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
266 $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
267 $html .= '<tr><td>'._b_('Sujet').'</td><td><input type="text" name="subject" value="'.htmlentities($subject).'" size="60" /></td></tr>';
268 $html .= '<tr><td>'._b_('Forums').'</td><td><input type="text" name="newsgroups" value="'.htmlentities($target).'" size="60" /></td></tr>';
269 $html .= '<tr><td>'._b_('Suivi à').'</td><td><input type="text" name="followup" value="" size="60" /></td></tr>';
270 $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
271 $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
272 $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
273 .to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></th>';
0a61409e 274 $html .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
275 $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
276 $html .= '<input type="file" name="newpj" /></td></tr>';
277 $html .= '<tr><th colspan="2">';
78cd27b3 278 if ($id > 0) {
279 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
280 }
281 $html .= '<input type="hidden" name="action" value="new" />';
0a61409e 282 $html .= '<input type="submit" /></th></tr>';
78cd27b3 283 $html .= '</table></form>';
284
285 return $html.$cuts;
286 }
287
288 function action_doFup($group, $artid = -1)
289 {
290 if ( ! ( is_utf8($_POST['subject']) && is_utf8($_POST['name'])
291 && is_utf8($_POST['org']) && is_utf8($_POST['body']) )
292 ) {
293 foreach(array('subject', 'name', 'org', 'body') as $key) {
294 $_POST[$key] = utf8_encode($_POST[$key]);
295 }
296 }
297
298 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
299 $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
300 $msg = 'From: '.$this->profile['name']."\n"
301 . "Newsgroups: ".$_POST['newsgroups']."\n"
302 . "Subject: ".headerEncode($_POST['subject'], 128)."\n"
303 . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
304 . (empty($_POST['followup']) ? '' : 'Followup-To: '.$_POST['followup']."\n");
305
306 if ($artid != -1) {
307 $this->_require('post');
308 $post = new BananaPost($artid);
309 $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
310 $msg .= "References: $refs{$post->headers['message-id']}\n";
311 }
312
0a61409e 313 $body = wrap($body, "", $this->wrap);
314
315 // include attachment in the body
316 if (isset($_FILES['newpj'])) {
317 $this->custom = $this->custom_mp;
318 $body = "\n--".$this->boundary."\n".$this->custom_bd."\n\n".$body."\n--".$this->boundary."\n";
319 $tmpname = $_FILES['newpj']['tmp_name'];
320 $file = basename($_FILES['newpj']['name']);
321 $mime = shell_exec("file -bi $tmpname"); //Because mime_content_type don't work :(
322 if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) {
323 $mime = $format[1];
324 $encod = $format[2];
325 } else {
326 preg_match("@([^ ]+/[^ ]+)\r?\n@", $mime, $format);
327 $mime = $format[1];
328 $encod = 'base64';
329 }
330
331 $body .= 'Content-Type: '.$mime.'; name="'.$file."\"\n";
332 $body .= 'Content-Disposition: attachment; filename="'.$file."\"\n";
333 $body .= 'Content-Transfer-Encoding: '.$encod."\n\n";
334 if ($encod == 'base64') {
335 $body .= chunk_split(base64_encode(fread(fopen($tmpname, 'r'), filesize($tmpname))));
336 } else {
337 $body .= fread(fopen($tmpname, 'r'), filesize($tmpname));
338 }
339 $body .= '--'.$this->boundary.'--';
340 }
341
342 // finalise and post the message
343 $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
78cd27b3 344
345 if ($this->nntp->post($msg)) {
346 header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
347 } else {
348 return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
349 }
350 }
351
352 /**************************************************************************/
353 /* Private functions */
354 /**************************************************************************/
355
356 function _newSpool($group, $disp=0, $since='') {
357 $this->_require('spool');
358 if (!$this->spool || $this->spool->group != $group) {
359 $this->spool = new BananaSpool($group, $disp, $since);
360 }
361 }
362
363 function _newPost($id)
364 {
365 $this->_require('post');
366 $this->post = new BananaPost($id);
367 }
368
369 function _newGroup()
370 {
371 $this->_require('groups');
372 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
373 if ($this->groups->type == BANANA_GROUP_SUB) {
374 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
375 }
376 }
377
378 function _require($file)
379 {
380 require_once (dirname(__FILE__).'/'.$file.'.inc.php');
381 }
382}
383
384?>