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