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