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