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