Use last version of the core.
[platal.git] / include / banana / forum.inc.php
CommitLineData
24cec3d8 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
24cec3d8 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22require_once 'banana/banana.inc.php';
23require_once 'banana/hooks.inc.php';
24
25function hook_checkcancel($_headers)
26{
e3a55098 27 return ($_headers['x-org-id'] == S::v('hruid') or S::has_perms());
24cec3d8 28}
29
fa7d6c7b 30class ForumsBanana extends Banana
24cec3d8 31{
e3a55098 32 private $user;
b2bffbe6 33
e3a55098 34 public function __construct(User &$user, $params = null)
24cec3d8 35 {
e3a55098 36 $this->user = &$user;
b2bffbe6 37
24cec3d8 38 global $globals;
39 Banana::$msgedit_canattach = false;
580d1c7d 40 Banana::$spool_root = $globals->banana->spool_root;
24cec3d8 41 array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
e3a55098 42 Banana::$nntp_host = 'news://web_' . $user->login()
24cec3d8 43 . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
1515e65a 44 if (S::has_perms()) {
45 Banana::$msgshow_mimeparts[] = 'source';
46 }
81e9c63f 47 Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
48 Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
8c12f931
FB
49 Banana::$feed_active = S::hasAuthToken();
50
b8458bd1 51 parent::__construct($params, 'NNTP', 'PlatalBananaPage');
ed0c62e4 52 if (@$params['action'] == 'profile') {
b5163f52
FB
53 Banana::$action = 'profile';
54 }
24cec3d8 55 }
56
57 public function run()
58 {
59 global $platal, $globals;
60
61 // Update last unread time
62 $time = null;
63 if (!is_null($this->params) && isset($this->params['updateall'])) {
64 $time = intval($this->params['updateall']);
65 $_SESSION['banana_last'] = $time;
66 }
67
68 // Get user profile from SQL
69 $req = XDB::query("SELECT nom, mail, sig,
43cddb05
FB
70 FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags),
71 tree_unread, tree_read
24cec3d8 72 FROM {$globals->banana->table_prefix}profils
73 WHERE uid={?}", S::i('uid'));
43cddb05 74 if (!(list($nom, $mail, $sig, $disp, $maj, $unread, $read) = $req->fetchOneRow())) {
24cec3d8 75 $nom = S::v('prenom')." ".S::v('nom');
e3a55098 76 $mail = $this->user->forlifeEmail();
24cec3d8 77 $sig = $nom." (".S::v('promo').")";
78 $disp = 0;
79 $maj = 1;
2a0d8734 80 $unread = 'o';
43cddb05 81 $read = 'dg';
24cec3d8 82 }
83 if ($maj) {
84 $time = time();
85 }
86
87 // Build user profile
eaf30d86
PH
88 $req = XDB::query("
89 SELECT nom
24cec3d8 90 FROM {$globals->banana->table_prefix}abos
91 LEFT JOIN {$globals->banana->table_prefix}list ON list.fid=abos.fid
92 WHERE uid={?}", S::i('uid'));
fa2056fe 93 Banana::$profile['headers']['From'] = "$nom <$mail>";
437a98e0 94 Banana::$profile['headers']['Organization'] = make_Organization();
fa2056fe 95 Banana::$profile['signature'] = $sig;
24cec3d8 96 Banana::$profile['display'] = $disp;
97 Banana::$profile['autoup'] = $maj;
98 Banana::$profile['lastnews'] = S::v('banana_last');
99 Banana::$profile['subscribe'] = $req->fetchColumn();
43cddb05
FB
100 Banana::$tree_unread = $unread;
101 Banana::$tree_read = $read;
24cec3d8 102
eaf30d86 103 // Update the "unread limit"
24cec3d8 104 if (!is_null($time)) {
105 XDB::execute("UPDATE auth_user_quick
106 SET banana_last = FROM_UNIXTIME({?})
107 WHERE user_id={?}",
108 $time, S::i('uid'));
109 }
110
4f355064 111 if (!empty($GLOBALS['IS_XNET_SITE'])) {
112 Banana::$page->killPage('forums');
113 Banana::$page->killPage('subscribe');
114 Banana::$spool_boxlist = false;
eaf30d86 115 } else {
4f355064 116 // Register custom Banana links and tabs
117 if (!Banana::$profile['autoup']) {
118 Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
119 . $platal->path . '", "updateall", ' . time() . ')\'>'
120 . 'Marquer tous les messages comme lus'
121 . '</a>', array('forums', 'thread', 'message'));
eaf30d86 122 }
4f355064 123 Banana::$page->registerPage('profile', 'Préférences', null);
124 }
24cec3d8 125
b5163f52
FB
126 // Run Bananai
127 if (Banana::$action == 'profile') {
128 Banana::$page->run();
129 return $this->action_updateProfile();
130 } else {
131 return parent::run();
132 }
24cec3d8 133 }
134
b2bffbe6 135 public function post($dest, $reply, $subject, $body)
136 {
137 global $globals;
e3a55098 138 $res = XDB::query('SELECT nom, prenom, promo
b2bffbe6 139 FROM auth_user_md5 AS u
e3a55098
VZ
140 WHERE u.user_id = {?}', $this->user->id());
141 list($nom, $prenom, $promo) = $res->fetchOneRow();
142 Banana::$profile['headers']['From'] = "$prenom $nom ($promo) <{$this->user->bestEmail()}>";
437a98e0 143 Banana::$profile['headers']['Organization'] = make_Organization();
b2bffbe6 144 return parent::post($dest, $reply, $subject, $body);
145 }
146
24cec3d8 147 protected function action_saveSubs($groups)
148 {
149 global $globals;
150 $uid = S::v('uid');
151
152 Banana::$profile['subscribe'] = array();
153 XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
154 if (!count($groups)) {
155 return true;
156 }
157
158 $req = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
159 $fids = array();
160 while (list($fid,$fnom) = $req->next()) {
161 $fids[$fnom] = $fid;
162 }
163
164 $diff = array_diff($groups, array_keys($fids));
165 foreach ($diff as $g) {
166 XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
167 $fids[$g] = XDB::insertId();
168 }
169
170 foreach ($groups as $g) {
171 XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})",
172 $fids[$g], $uid);
173 Banana::$profile['subscribe'][] = $g;
174 }
175 }
b5163f52
FB
176
177 protected function action_updateProfile()
178 {
d7610c35
FB
179 global $globals;
180 $page = Platal::page();
b5163f52 181
43cddb05
FB
182 $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
183 foreach ($colors as $key=>$path) {
184 $path = basename($path, '.gif');
185 $colors[$key] = substr($path, 2);
186 }
187 $page->assign('colors', $colors);
188
189 if (Post::has('action') && Post::v('action') == 'Enregistrer') {
190 S::assert_xsrf_token();
748b472c 191 $flags = new FlagSet();
b5163f52 192 if (Post::b('bananadisplay')) {
748b472c 193 $flags->addFlag('threads');
b5163f52
FB
194 }
195 if (Post::b('bananaupdate')) {
748b472c 196 $flags->addFlag('automaj');
b5163f52
FB
197 }
198 if (Post::b('bananaxface')) {
748b472c 199 $flags->addFlag('xface');
b5163f52 200 }
43cddb05
FB
201 $unread = Post::s('unread');
202 $read = Post::s('read');
203 if (!in_array($unread, $colors) || !in_array($read, $colors)) {
204 $page->trigError('Le choix de type pour l\'arborescence est invalide');
205 } elseif (XDB::execute("REPLACE INTO forums.profils (uid, sig, mail, nom, flags, tree_unread, tree_read)
206 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
207 S::v('uid'), Post::v('bananasig'),
208 Post::v('bananamail'), Post::v('banananame'),
209 $flags, $unread, $read)) {
748b472c
FB
210 $page->trigSuccess("Ton profil a été enregistré avec succès.");
211 } else {
212 $page->trigError("Une erreur s'est produite lors de l'enregistrement de ton profil");
213 }
214 }
215
216 $req = XDB::query("
217 SELECT nom, mail, sig,
218 FIND_IN_SET('threads', flags),
219 FIND_IN_SET('automaj', flags),
43cddb05
FB
220 FIND_IN_SET('xface', flags),
221 tree_unread,
222 tree_read
748b472c
FB
223 FROM forums.profils
224 WHERE uid = {?}", S::v('uid'));
43cddb05 225 if (!(list($nom, $mail, $sig, $disp, $maj, $xface, $unread, $read) = $req->fetchOneRow())) {
748b472c 226 $nom = S::v('prenom').' '.S::v('nom');
eb0dd7ad 227 $mail = S::user()->forlifeEmail();
748b472c
FB
228 $sig = $nom.' ('.S::v('promo').')';
229 $disp = 0;
230 $maj = 0;
231 $xface = 0;
2a0d8734 232 $unread = 'o';
43cddb05 233 $read = 'dg';
b5163f52 234 }
748b472c
FB
235 $page->assign('nom' , $nom);
236 $page->assign('mail', $mail);
237 $page->assign('sig', $sig);
238 $page->assign('disp', $disp);
239 $page->assign('maj', $maj);
240 $page->assign('xface', $xface);
43cddb05
FB
241 $page->assign('unread', $unread);
242 $page->assign('read', $read);
b5163f52
FB
243 return null;
244 }
24cec3d8 245}
246
a7de4ef7 247// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
24cec3d8 248?>