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