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