Ajoute le support du 'richtext'... avec un jeu de balise relativement réduit
[banana.git] / banana / banana.inc.php.in
CommitLineData
78cd27b3 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
10class Banana
11{
d28aa62d 12 var $maxspool = 3000;
13 var $maxfilesize = 100000;
78cd27b3 14
d28aa62d 15 var $hdecode = array('from','name','organization','subject');
16 var $parse_hdr = array('content-disposition', 'content-transfer-encoding', 'content-type', 'date', 'followup-to', 'from',
78cd27b3 17 'message-id', 'newsgroups', 'organization', 'references', 'subject', 'x-face');
d28aa62d 18 var $show_hdr = array('from', 'subject', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face');
78cd27b3 19
cc43419f 20 /** Favorites MIMEtypes to use, by order for reading multipart messages
21 */
22 var $body_mime = array('text/plain', 'text/html', 'text/richtext');
78cd27b3 23
4f6a209a 24 /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
25 * ex : '^xorg\..*' for xorg.*
26 */
27 var $grp_pattern;
28
d28aa62d 29 var $tbefore = 5;
30 var $tafter = 5;
31 var $tmax = 50;
78cd27b3 32
d28aa62d 33 var $wrap = 74;
78cd27b3 34
d28aa62d 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";
78cd27b3 39
d28aa62d 40 var $host = 'news://localhost:119/';
78cd27b3 41
d28aa62d 42 var $profile = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig' => '', 'org' => '',
78cd27b3 43 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
44
d28aa62d 45 var $state = Array('group' => null, 'artid' => null);
78cd27b3 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;
aef14768 62
78cd27b3 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']);
cc43419f 72 $partid = !isset($_GET['part']) ? -1 : $_GET['part'];
78cd27b3 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)) {
78cd27b3 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':
1f75b135 104 $res .= $banana->action_showArticle($group, $artid, $partid);
78cd27b3 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 }
7a0e2710 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
1f75b135 128 return $res . $banana->action_showArticle($group, $artid, $partid);
78cd27b3 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
1f75b135 188 function action_showArticle($group, $id, $part)
78cd27b3 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;
1f75b135 202 $res .= $this->post->to_html($part);
78cd27b3 203
204 $this->nntp->quit();
205
206 return $res.$cuts;
207 }
208
7a0e2710 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
78cd27b3 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']);
a529ea7b 264 $body = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->get_body(), "> ");
78cd27b3 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;
0a61409e 273 $html .= '<form enctype="multipart/form-data" action="?group='.$group.'" method="post" accept-charset="utf-8">';
78cd27b3 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">'
17b39752 283 .to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></tr>';
0a61409e 284 $html .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
d28aa62d 285 $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxfilesize.'" />';
a529ea7b 286 $html .= '<input type="file" name="newpj" size="40"/></td></tr>';
0a61409e 287 $html .= '<tr><th colspan="2">';
78cd27b3 288 if ($id > 0) {
289 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
290 }
291 $html .= '<input type="hidden" name="action" value="new" />';
0a61409e 292 $html .= '<input type="submit" /></th></tr>';
78cd27b3 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
0a61409e 323 $body = wrap($body, "", $this->wrap);
324
325 // include attachment in the body
d28aa62d 326 $uploaded = $this->_upload('newpj');
327 if ($uploaded['error'] == 0) {
0a61409e 328 $this->custom = $this->custom_mp;
329 $body = "\n--".$this->boundary."\n".$this->custom_bd."\n\n".$body."\n--".$this->boundary."\n";
d28aa62d 330
331 $body .= 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n";
d28aa62d 332 $body .= 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n\n";
a529ea7b 333 $body .= 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
d28aa62d 334 $body .= $uploaded['data'];
0a61409e 335 $body .= '--'.$this->boundary.'--';
d28aa62d 336 }
337 #TODO:afficher les erreurs lorsque l'upload ne marche pas
0a61409e 338
339 // finalise and post the message
340 $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
78cd27b3 341
342 if ($this->nntp->post($msg)) {
343 header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
344 } else {
345 return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
346 }
347 }
348
349 /**************************************************************************/
350 /* Private functions */
351 /**************************************************************************/
352
353 function _newSpool($group, $disp=0, $since='') {
354 $this->_require('spool');
355 if (!$this->spool || $this->spool->group != $group) {
356 $this->spool = new BananaSpool($group, $disp, $since);
357 }
358 }
359
360 function _newPost($id)
361 {
362 $this->_require('post');
363 $this->post = new BananaPost($id);
364 }
365
366 function _newGroup()
367 {
368 $this->_require('groups');
369 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
370 if ($this->groups->type == BANANA_GROUP_SUB) {
371 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
372 }
373 }
374
375 function _require($file)
376 {
377 require_once (dirname(__FILE__).'/'.$file.'.inc.php');
378 }
d28aa62d 379
380 function _upload($file)
381 {
382 if ($_FILES[$file]['name'] == "") {
383 return Array( 'error' => -1 );
384 }
385
386 // upload
387 $_FILES[$file]['tmp_name'];
388
389 // test if upload is ok
390 $file = $_FILES[$file];
391 if ($file['size'] == 0 || $file['error'] != 0) {
392 if ($file['error'] == 0) {
393 $file['error'] = -1;
394 }
395 return $file;
396 }
397
398 // adding custum data
399 $mime = rtrim(shell_exec('file -bi '.$file['tmp_name'])); //Because mime_content_type don't work :(
400 $encod = 'base64';
401 if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) {
402 $mime = $format[1];
403 $encod = $format[2];
404 }
405 $data = fread(fopen($file['tmp_name'], 'r'), $file['size']);
406 if ($encod == 'base64') {
407 $data = chunk_split(base64_encode($data));
408 }
409 $file['name'] = basename($file['name']);
410 $file['type'] = $mime;
411 $file['encoding'] = $encod;
412 $file['data'] = $data;
413
414 return $file;
415 }
78cd27b3 416}
417
418?>