Merge commit 'origin/fusionax' into account
[platal.git] / include / banana / forum.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
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
22 require_once 'banana/banana.inc.php';
23 require_once 'banana/hooks.inc.php';
24
25 function hook_checkcancel($_headers)
26 {
27 return ($_headers['x-org-id'] == S::v('hruid') or S::has_perms());
28 }
29
30 class ForumsBanana extends Banana
31 {
32 private $user;
33
34 public function __construct(User &$user, $params = null)
35 {
36 $this->user = &$user;
37
38 global $globals;
39 Banana::$msgedit_canattach = false;
40 Banana::$spool_root = $globals->banana->spool_root;
41 array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
42 Banana::$nntp_host = 'news://web_' . $user->login()
43 . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
44 if (S::has_perms()) {
45 Banana::$msgshow_mimeparts[] = 'source';
46 }
47 Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
48 Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
49 Banana::$feed_active = S::hasAuthToken();
50
51 parent::__construct($params, 'NNTP', 'PlatalBananaPage');
52 if (@$params['action'] == 'profile') {
53 Banana::$action = 'profile';
54 }
55 }
56
57 private function fetchProfile()
58 {
59 // Get user profile from SQL
60 $req = XDB::query("SELECT name, mail, sig,
61 FIND_IN_SET('threads',flags) AS threads,
62 FIND_IN_SET('automaj',flags) AS maj,
63 FIND_IN_SET('xface', flags) AS xface,
64 tree_unread, tree_read
65 FROM forum_profiles
66 WHERE uid = {?}", $this->user->id());
67 if ($req->numRows()) {
68 $infos = $req->fetchOneAssoc();
69 } else {
70 $infos = array();
71 }
72 if (empty($infos['name'])) {
73 $infos = array('name' => $this->user->fullName(),
74 'mail' => $this->user->forlifeEmail(),
75 'sig' => $this->user->displayName(),
76 'threads' => false,
77 'maj' => true,
78 'xface' => false,
79 'tree_unread' => 'o',
80 'tree_read' => 'dg' );
81 }
82 return $infos;
83 }
84
85 public function run()
86 {
87 global $platal, $globals;
88
89 // Update last unread time
90 $time = null;
91 if (!is_null($this->params) && isset($this->params['updateall'])) {
92 $time = intval($this->params['updateall']);
93 S::set('banana_last', $time);
94 }
95
96 $infos = $this->fetchProfile();
97 if ($infos['maj']) {
98 $time = time();
99 }
100
101 // Build user profile
102 $req = XDB::query("SELECT name
103 FROM forum_subs AS fs
104 LEFT JOIN forums AS f ON (f.fid = fs.fid)
105 WHERE uid={?}", $this->user->id());
106 Banana::$profile['headers']['From'] = $infos['name'] . ' <' . $infos['mail'] . '>';
107 Banana::$profile['headers']['Organization'] = make_Organization();
108 Banana::$profile['signature'] = $infos['sig'];
109 Banana::$profile['display'] = $infos['threads'];
110 Banana::$profile['autoup'] = $infos['maj'];
111 Banana::$profile['lastnews'] = S::v('banana_last');
112 Banana::$profile['subscribe'] = $req->fetchColumn();
113 Banana::$tree_unread = $infos['tree_unread'];
114 Banana::$tree_read = $infos['tree_read'];
115
116 // Update the "unread limit"
117 if (!is_null($time)) {
118 XDB::execute('UPDATE forum_profiles
119 SET last_seen = FROM_UNIXTIME({?})
120 WHERE uid = {?}',
121 $time, $this->user->id());
122 if (XDB::affectedRows() == 0) {
123 XDB::execute('INSERT INTO forum_profiles (uid, last_seen)
124 VALUES ({?}, FROM_UNIXTIME({?}))',
125 $this->user->id(), $time);
126 }
127 }
128
129 if (!empty($GLOBALS['IS_XNET_SITE'])) {
130 Banana::$page->killPage('forums');
131 Banana::$page->killPage('subscribe');
132 Banana::$spool_boxlist = false;
133 } else {
134 // Register custom Banana links and tabs
135 if (!Banana::$profile['autoup']) {
136 Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
137 . $platal->path . '", "updateall", ' . time() . ')\'>'
138 . 'Marquer tous les messages comme lus'
139 . '</a>', array('forums', 'thread', 'message'));
140 }
141 Banana::$page->registerPage('profile', 'Préférences', null);
142 }
143
144 // Run Bananai
145 if (Banana::$action == 'profile') {
146 Banana::$page->run();
147 return $this->action_updateProfile();
148 } else {
149 return parent::run();
150 }
151 }
152
153 public function post($dest, $reply, $subject, $body)
154 {
155 global $globals;
156 Banana::$profile['headers']['From'] = $this->user->fullName() . ' <' . $this->user->bestEmail() . '>';
157 Banana::$profile['headers']['Organization'] = make_Organization();
158 return parent::post($dest, $reply, $subject, $body);
159 }
160
161 protected function action_saveSubs($groups)
162 {
163 global $globals;
164 $uid = $this->user->id();
165
166 Banana::$profile['subscribe'] = array();
167 XDB::execute('DELETE FROM forum_subs
168 WHERE uid = {?}', $this->user->id());
169 if (!count($groups)) {
170 return true;
171 }
172
173 $fids = XDB::fetchAllAssoc('name', 'SELECT fid, name
174 FROM forums');
175 $diff = array_diff($groups, array_keys($fids));
176 foreach ($diff as $g) {
177 XDB::execute('INSERT INTO forums (name)
178 VALUES ({?})', $g);
179 $fids[$g] = XDB::insertId();
180 }
181
182 foreach ($groups as $g) {
183 XDB::execute('INSERT INTO forum_subs (fid, uid)
184 VALUES ({?}, {?})',
185 $fids[$g], $uid);
186 Banana::$profile['subscribe'][] = $g;
187 }
188 }
189
190 protected function action_updateProfile()
191 {
192 global $globals;
193 $page =& Platal::page();
194
195 $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
196 foreach ($colors as $key=>$path) {
197 $path = basename($path, '.gif');
198 $colors[$key] = substr($path, 2);
199 }
200 $page->assign('colors', $colors);
201
202 if (Post::has('action') && Post::v('action') == 'Enregistrer') {
203 S::assert_xsrf_token();
204 $flags = new PlFlagSet();
205 if (Post::b('bananadisplay')) {
206 $flags->addFlag('threads');
207 }
208 if (Post::b('bananaupdate')) {
209 $flags->addFlag('automaj');
210 }
211 if (Post::b('bananaxface')) {
212 $flags->addFlag('xface');
213 }
214 $unread = Post::s('unread');
215 $read = Post::s('read');
216 if (!in_array($unread, $colors) || !in_array($read, $colors)) {
217 $page->trigError('Le choix de type pour l\'arborescence est invalide');
218 } else {
219 $last_seen = XDB::query('SELECT last_seen
220 FROM forum_profiles
221 WHERE uid = {?}', $this->user->id());
222 if ($last_seen->numRows() > 0) {
223 $last_seen = $last_seen->fetchOneCell();
224 } else {
225 $last_seen = '0000-00-00';
226 }
227 XDB::execute('REPLACE INTO forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
228 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
229 $this->user->id(), Post::v('bananasig'),
230 Post::v('bananamail'), Post::v('banananame'),
231 $flags, $unread, $read, $last_seen);
232 $page->trigSuccess('Ton profil a été mis à jour');
233 }
234 }
235
236 $infos = $this->fetchProfile();
237 $page->assign('nom' , $infos['name']);
238 $page->assign('mail', $infos['mail']);
239 $page->assign('sig', $infos['sig']);
240 $page->assign('disp', $infos['threads']);
241 $page->assign('maj', $infos['maj']);
242 $page->assign('xface', $infos['xface']);
243 $page->assign('unread', $infos['tree_unread']);
244 $page->assign('read', $infos['tree_read']);
245 return null;
246 }
247 }
248
249 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
250 ?>