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