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