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