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