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