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