Fix xface handling in case it contains =?(B|Q)?...?=
[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{
d28aa62d 12 var $maxspool = 3000;
78cd27b3 13
d28aa62d 14 var $hdecode = array('from','name','organization','subject');
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');
820ce67b 17 var $show_hdr = array('from', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face');
78cd27b3 18
cc43419f 19 /** Favorites MIMEtypes to use, by order for reading multipart messages
20 */
21 var $body_mime = array('text/plain', 'text/html', 'text/richtext');
a1937df3 22 /** Indicate wether posting attachment is allowed
23 */
24 var $can_attach = true;
25 /** Maximum allowed file size for attachment
26 */
ca3b1040 27 var $maxfilesize = 100000;
28 /** Indicate wether x-face should be skinned as specials data or not
29 */
30 var $formatxface = true;
78cd27b3 31
4f6a209a 32 /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
33 * ex : '^xorg\..*' for xorg.*
34 */
35 var $grp_pattern;
36
d28aa62d 37 var $tbefore = 5;
38 var $tafter = 5;
39 var $tmax = 50;
78cd27b3 40
d28aa62d 41 var $wrap = 74;
87514711 42 /** Match an url
43 * Should be included in a regexp delimited using ! (eg: "!$url_regexp!i")
44 * If it matches, return 3 main parts :
45 * \\1 and \\3 are delimiters
46 * \\2 is the url
47 *
48 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
49 * $matches[1] = "["
50 * $matches[2] = "http://www.polytechnique.org"
51 * $matches[3] = "]"
52 */
e7241984 53 var $url_regexp = '(["\[])?((?:https?|ftp|news)://(?:&amp;|,?[a-z@0-9.~%$£µ&i#\-+=_/\?])*)(["\]])?';
78cd27b3 54
87514711 55
a1937df3 56 /** Boundary for multipart messages
57 */
f12fdb59 58 var $boundary = 'bananaBoundary42';
59 /** Global headers to use for messages
a1937df3 60 */
f12fdb59 61 var $custom = "Mime-Version: 1.0\nUser-Agent: Banana @VERSION@\n";
a1937df3 62 /** Global headers to use from multipart messages
63 */
f12fdb59 64 var $custom_mp = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\n";
65 /** Body type when using plain text
a1937df3 66 */
f12fdb59 67 var $custom_plain= "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit\n";
78cd27b3 68
a1937df3 69 /** News serveur to use
70 */
d28aa62d 71 var $host = 'news://localhost:119/';
78cd27b3 72
a1937df3 73 /** User profile
74 */
d28aa62d 75 var $profile = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig' => '', 'org' => '',
78cd27b3 76 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
77
d28aa62d 78 var $state = Array('group' => null, 'artid' => null);
78cd27b3 79 var $nntp;
80 var $groups;
81 var $newgroups;
82 var $post;
83 var $spool;
84
f6df9eb2 85 var $get;
1786dc36 86
78cd27b3 87 function Banana()
88 {
89 $this->_require('NetNNTP');
90 setlocale(LC_ALL, $this->profile['locale']);
91 $this->nntp = new nntp($this->host);
856dc84a 92 if (!$this->nntp || !$this->nntp->valid) {
93 $this->nntp = null;
94 }
78cd27b3 95 }
96
f6df9eb2 97 /** Run Banana
98 * @param STRING class Name of the class to use
99 * @param ARRAY myget If defined is used instead of get
100 */
1786dc36 101 function run($class = 'Banana', $myget = null)
78cd27b3 102 {
103 global $banana;
aef14768 104
78cd27b3 105 Banana::_require('misc');
106 $banana = new $class();
107
1786dc36 108 if (is_null($myget)) {
109 $banana->get = $_GET;
110 } else {
111 $banana->get = $myget;
112 }
113
114
78cd27b3 115 if (!$banana->nntp) {
116 return '<p class="error">'._b_('Impossible de contacter le serveur').'</p>';
117 }
118
f37bac43 119 $group = empty($banana->get['group']) ? null : $banana->get['group'];
3c3d29bb 120 if (isset($banana->grp_pattern) && !preg_match('/' . $banana->grp_pattern . '/', $group)) {
121 return '<p class="error">'
122 . $group . _b_(' : ce newsgroup n\'existe pas ou vous n\'avez pas l\'autorisation d\'y accéder')
123 . '</p>';
124 }
1786dc36 125 $artid = empty($banana->get['artid']) ? null : strtolower($banana->get['artid']);
126 $partid = !isset($banana->get['part']) ? -1 : $banana->get['part'];
78cd27b3 127 $banana->state = Array ('group' => $group, 'artid' => $artid);
128
129 if (is_null($group)) {
1786dc36 130 if (isset($banana->get['subscribe'])) {
78cd27b3 131 return $banana->action_listSubs();
20e25a42 132 } elseif (isset($_POST['validsubs'])) {
78cd27b3 133 $banana->action_saveSubs();
134 }
135 return $banana->action_listGroups();
136
137 } elseif (is_null($artid)) {
78cd27b3 138 if (isset($_POST['action']) && $_POST['action'] == 'new') {
139 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
1786dc36 140 } elseif (isset($banana->get['action']) && $banana->get['action'] == 'new') {
78cd27b3 141 return $banana->action_newFup($group);
142 } else {
1786dc36 143 return $banana->action_showThread($group, isset($banana->get['first']) ? intval($banana->get['first']) : 1);
78cd27b3 144 }
145
146 } else {
78cd27b3 147 if (isset($_POST['action']) && $_POST['action']=='cancel') {
148 $res = $banana->action_cancelArticle($group, $artid);
149 } else {
150 $res = '';
151 }
152
1786dc36 153 if (isset($banana->get['action'])) {
154 switch ($banana->get['action']) {
78cd27b3 155 case 'cancel':
1f75b135 156 $res .= $banana->action_showArticle($group, $artid, $partid);
78cd27b3 157 if ($banana->post->checkcancel()) {
158 $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
5440bf74 159 . '<form action="'
f6df9eb2 160 . htmlentities(makeLink(Array('group' => $group,
161 'artid' => $artid)))
162 . '" method="post"><p>'
78cd27b3 163 . '<input type="hidden" name="action" value="cancel" />'
164 . '<input type="submit" value="Annuler !" />'
165 . '</p></form>';
166 return $form.$res;
167 }
168 return $res;
169
170 case 'new':
171 return $banana->action_newFup($group, $artid);
172 }
173 }
7a0e2710 174
1786dc36 175 if (isset($banana->get['pj'])) {
7a0e2710 176 $action = false;
1786dc36 177 if (isset($banana->get['action']) && $banana->get['action'] == 'view') {
7a0e2710 178 $action = true;
179 }
1786dc36 180 $att = $banana->action_getAttachment($group, $artid, $banana->get['pj'], $action);
a1937df3 181 if ($att != "") {
182 return $res.$att;
183 }
184 return "";
7a0e2710 185 }
186
1f75b135 187 return $res . $banana->action_showArticle($group, $artid, $partid);
78cd27b3 188 }
189 }
190
191 /**************************************************************************/
192 /* actions */
193 /**************************************************************************/
194
195 function action_saveSubs()
196 {
197 return;
198 }
199
200 function action_listGroups()
201 {
202 $this->_newGroup();
203
204 $cuts = displayshortcuts();
205 $res = '<h1>'._b_('Les forums de Banana').'</h1>'.$cuts.$this->groups->to_html();
206 if (count($this->newgroups->overview)) {
207 $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
208 $res .= $this->newgroups->to_html();
209 }
210
211 $this->nntp->quit();
212 return $res.$cuts;
213 }
214
215 function action_listSubs()
216 {
217 $this->_require('groups');
218 $this->groups = new BananaGroups(BANANA_GROUP_ALL);
219
220 $cuts = displayshortcuts();
221 $res = '<h1>'._b_('Abonnements').'</h1>'.$cuts.$this->groups->to_html(true).$cuts;
222
223 $this->nntp->quit();
224 return $res;
225 }
226
227 function action_showThread($group, $first)
228 {
856dc84a 229 if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
f37bac43 230 return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
856dc84a 231 }
78cd27b3 232
233 if ($first > count($this->spool->overview)) {
234 $first = count($this->spool->overview);
235 }
236
237 $first = $first - ($first % $this->tmax) + 1;
238
239 $cuts = displayshortcuts($first);
240
241 $res = '<h1>'.$group.'</h1>'.$cuts;
242 $res .= $this->spool->to_html($first, $first+$this->tmax);
243
244 $this->nntp->quit();
245
246 return $res.$cuts;
247 }
248
1f75b135 249 function action_showArticle($group, $id, $part)
78cd27b3 250 {
856dc84a 251 if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
f37bac43 252 return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
856dc84a 253 }
254
255 if (!$this->_newPost($id)) {
78cd27b3 256 if ($this->nntp->lasterrorcode == "423") {
257 $this->spool->delid($id);
258 }
259 $this->nntp->quit();
260 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
261 }
262
263 $cuts = displayshortcuts();
264 $res = '<h1>'._b_('Message').'</h1>'.$cuts;
1f75b135 265 $res .= $this->post->to_html($part);
78cd27b3 266
267 $this->nntp->quit();
268
269 return $res.$cuts;
270 }
271
7a0e2710 272 function action_getAttachment($group, $id, $pjid, $action)
273 {
856dc84a 274 if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
275 return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
276 }
277
278 if (!$this->_newPost($id)) {
7a0e2710 279 if ($this->nntp->lasterrorcode == "423") {
280 $this->spool->delid($id);
281 }
282 $this->nntp->quit();
283 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
284 }
285
286 $this->nntp->quit();
287 if ($this->post->get_attachment($pjid, $action)) {
288 return "";
289 } else {
290 return displayshortcuts().'<p calss="error">'._b_('Impossible d\'accéder à la pièce jointe.').'</p>';
291 }
292 }
293
78cd27b3 294 function action_cancelArticle($group, $id)
295 {
856dc84a 296 if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
297 return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
298 }
299
300 if (!$this->_newPost($id)) {
301 return '<p class="error">'._b_('Impossible de trouver le message à annuler').'</p>';
302 }
78cd27b3 303 $mid = array_search($id, $this->spool->ids);
304
305 if (!$this->post->checkcancel()) {
306 return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>';
307 }
308 $msg = 'From: '.$this->profile['name']."\n"
309 . "Newsgroups: $group\n"
310 . "Subject: cmsg $mid\n"
311 . $this->custom
312 . "Control: cancel $mid\n"
313 . "\n"
314 . "Message canceled with Banana";
315 if ($this->nntp->post($msg)) {
316 $this->spool->delid($id);
317 $this->nntp->quit();
41b90d00 318 redirectInBanana(Array('group' => $group,
f6df9eb2 319 'first' => $id));
78cd27b3 320 } else {
321 return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
322 }
323 }
324
325 function action_newFup($group, $id = -1)
326 {
327 $subject = $body = '';
328 $target = $group;
329
330 if ($id > 0) {
331 $this->nntp->group($group);
856dc84a 332 if ($this->_newPost($id)) {
78cd27b3 333 $subject = preg_replace("/^re\s*:\s*/i", '', 'Re: '.$this->post->headers['subject']);
a529ea7b 334 $body = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->get_body(), "> ");
78cd27b3 335 $target = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
336 }
337 }
338
339 $this->nntp->quit();
340
341 $cuts = displayshortcuts();
342 $html = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
5440bf74 343 $html .= '<form enctype="multipart/form-data" action="'
f6df9eb2 344 . htmlentities(makeLink(Array('group' => $group)))
345 .'" method="post" accept-charset="utf-8">';
78cd27b3 346 $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
347 $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
348 $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
349 $html .= '<tr><td>'._b_('Sujet').'</td><td><input type="text" name="subject" value="'.htmlentities($subject).'" size="60" /></td></tr>';
350 $html .= '<tr><td>'._b_('Forums').'</td><td><input type="text" name="newsgroups" value="'.htmlentities($target).'" size="60" /></td></tr>';
351 $html .= '<tr><td>'._b_('Suivi à').'</td><td><input type="text" name="followup" value="" size="60" /></td></tr>';
352 $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
353 $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
354 $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
f12fdb59 355 . to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></tr>';
e9e9463c 356 if ($this->can_attach) {
f6df9eb2 357 $html .= '<tr><th colspan="2">'._b_('Pièce jointe').'</th></tr>';
e9e9463c 358 $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxfilesize.'" />';
359 $html .= '<input type="file" name="newpj" size="40"/></td></tr>';
360 }
f6df9eb2 361 $html .= '<tr><th colspan="2">';
78cd27b3 362 if ($id > 0) {
363 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
364 }
365 $html .= '<input type="hidden" name="action" value="new" />';
f6df9eb2 366 $html .= '<input type="submit" value="Envoyer le message" /></th></tr>';
78cd27b3 367 $html .= '</table></form>';
368
369 return $html.$cuts;
370 }
371
372 function action_doFup($group, $artid = -1)
373 {
374 if ( ! ( is_utf8($_POST['subject']) && is_utf8($_POST['name'])
375 && is_utf8($_POST['org']) && is_utf8($_POST['body']) )
376 ) {
377 foreach(array('subject', 'name', 'org', 'body') as $key) {
378 $_POST[$key] = utf8_encode($_POST[$key]);
379 }
380 }
fb6bea16 381
f6df9eb2 382 $forums = preg_split('/\s*(,|;)\s*/', $_POST['newsgroups']);
383 $fup = $_POST['followup'];
384 if (sizeof($forums) > 1) {
385 if (empty($fup)) {
386 $fup = $forums[0];
387 }
388 }
389 $to = implode(',', $forums);
390
856dc84a 391 if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
392 return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
393 }
f6df9eb2 394
78cd27b3 395 $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
d14182c8 396 $msg = 'From: ' . $this->profile['name'] . "\n"
fb6bea16 397 . "Newsgroups: ". $to . "\n"
d14182c8 398 . "Subject: " . headerEncode($_POST['subject'], 128) . "\n"
78cd27b3 399 . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
d14182c8 400 . (empty($fup) ? '' : 'Followup-To: ' . $fup . "\n");
78cd27b3 401
402 if ($artid != -1) {
403 $this->_require('post');
404 $post = new BananaPost($artid);
856dc84a 405 if (!$post || !$post->valid) {
406 return '<p class="error">'._b_('Impossible charger le message d\'origine').'</p>';
407 }
78cd27b3 408 $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
409 $msg .= "References: $refs{$post->headers['message-id']}\n";
410 }
411
f12fdb59 412 $body_headers = $this->custom_plain;
d6f4a433 413 $body = wrap($body, "");
0a61409e 414
415 // include attachment in the body
d28aa62d 416 $uploaded = $this->_upload('newpj');
ef84d640 417 switch ($uploaded['error']) {
418 case UPLOAD_ERR_OK:
f12fdb59 419 $this->custom = $this->custom_mp.$this->custom;
420 $body = $this->_make_part($body_headers, $body);
421 $file_head = 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n"
422 . 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n"
423 . 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
424 $body .= $this->_make_part($file_head, $uploaded['data']);
425 $body .= "\n--".$this->boundary.'--';
ef84d640 426 break;
427
428 case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE:
429 return '<p class="error">'._b_('Fichier trop gros pour être envoyé : ')
430 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
431
432 case UPLOAD_ERR_PARTIAL:
433 return '<p class="error">'._b_('Erreur lors de l\'upload de ')
434 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
435
436 case UPLOAD_ERR_NO_FILE:
437 return '<p class="error">'._b_('Le fichier spécifié n\'existe pas : ')
438 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
439
440 case UPLOAD_ERR_NO_TMP_DIR:
441 return '<p class="error">'._b_('Une erreur est survenue sur le serveur lors de l\'upload de ')
442 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
f12fdb59 443
444 default:
445 $this->custom = $body_headers.$this->custom;
d28aa62d 446 }
0a61409e 447
448 // finalise and post the message
449 $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
78cd27b3 450
451 if ($this->nntp->post($msg)) {
f6df9eb2 452 $dir = Array('group' => $group);
453 if ($artid != -1) {
454 $dir['first'] = $artid;
455 }
41b90d00 456 redirectInBanana($dir);
78cd27b3 457 } else {
f0dc6af4 458 return '<p class="error">' . _b_('Impossible de poster le message') . '</p>'
459 . $this->action_showThread($group, $artid);
78cd27b3 460 }
461 }
462
463 /**************************************************************************/
464 /* Private functions */
465 /**************************************************************************/
466
467 function _newSpool($group, $disp=0, $since='') {
468 $this->_require('spool');
469 if (!$this->spool || $this->spool->group != $group) {
470 $this->spool = new BananaSpool($group, $disp, $since);
856dc84a 471 if (!$this->spool || !$this->spool->valid) {
472 $this->spool = null;
473 return false;
474 }
78cd27b3 475 }
e230b41a 476 if (count($this->profile['subscribe']) > 0) {
477 $this->_newGroup(false);
478 }
856dc84a 479 return true;
78cd27b3 480 }
481
482 function _newPost($id)
483 {
484 $this->_require('post');
485 $this->post = new BananaPost($id);
856dc84a 486 if (!$this->post || !$this->post->valid) {
487 $this->post = null;
488 return false;
489 }
490 return true;
78cd27b3 491 }
492
e230b41a 493 function _newGroup($showNew = true)
78cd27b3 494 {
495 $this->_require('groups');
496 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
e230b41a 497 if ($showNew && $this->groups->type == BANANA_GROUP_SUB) {
78cd27b3 498 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
499 }
500 }
501
502 function _require($file)
503 {
504 require_once (dirname(__FILE__).'/'.$file.'.inc.php');
505 }
d28aa62d 506
507 function _upload($file)
508 {
509 if ($_FILES[$file]['name'] == "") {
510 return Array( 'error' => -1 );
511 }
512
513 // upload
514 $_FILES[$file]['tmp_name'];
515
516 // test if upload is ok
517 $file = $_FILES[$file];
518 if ($file['size'] == 0 || $file['error'] != 0) {
519 if ($file['error'] == 0) {
520 $file['error'] = -1;
521 }
522 return $file;
523 }
524
525 // adding custum data
526 $mime = rtrim(shell_exec('file -bi '.$file['tmp_name'])); //Because mime_content_type don't work :(
527 $encod = 'base64';
528 if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) {
529 $mime = $format[1];
530 $encod = $format[2];
531 }
532 $data = fread(fopen($file['tmp_name'], 'r'), $file['size']);
533 if ($encod == 'base64') {
534 $data = chunk_split(base64_encode($data));
535 }
536 $file['name'] = basename($file['name']);
537 $file['type'] = $mime;
538 $file['encoding'] = $encod;
539 $file['data'] = $data;
540
541 return $file;
542 }
f12fdb59 543
544 function _make_part($headers, $body)
545 {
546 return "\n--".$this->boundary."\n".$headers."\n".$body;
547 }
78cd27b3 548}
549
f6df9eb2 550// vim:set et sw=4 sts=4 ts=4
78cd27b3 551?>