Factorize code.
[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
2bd55de5
FB
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
24cec3d8 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']);
7d15f750 93 S::set('banana_last', $time);
24cec3d8 94 }
95
2bd55de5
FB
96 $infos = $this->fetchProfile();
97 if ($infos['maj']) {
24cec3d8 98 $time = time();
99 }
100
101 // Build user profile
7d15f750
FB
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());
2bd55de5 106 Banana::$profile['headers']['From'] = $infos['name'] . ' <' . $infos['mail'] . '>';
437a98e0 107 Banana::$profile['headers']['Organization'] = make_Organization();
2bd55de5
FB
108 Banana::$profile['signature'] = $infos['sig'];
109 Banana::$profile['display'] = $infos['threads'];
110 Banana::$profile['autoup'] = $infos['maj'];
24cec3d8 111 Banana::$profile['lastnews'] = S::v('banana_last');
112 Banana::$profile['subscribe'] = $req->fetchColumn();
2bd55de5
FB
113 Banana::$tree_unread = $infos['tree_unread'];
114 Banana::$tree_read = $infos['tree_read'];
24cec3d8 115
eaf30d86 116 // Update the "unread limit"
24cec3d8 117 if (!is_null($time)) {
7d15f750
FB
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 }
24cec3d8 127 }
128
4f355064 129 if (!empty($GLOBALS['IS_XNET_SITE'])) {
130 Banana::$page->killPage('forums');
131 Banana::$page->killPage('subscribe');
132 Banana::$spool_boxlist = false;
eaf30d86 133 } else {
4f355064 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'));
eaf30d86 140 }
4f355064 141 Banana::$page->registerPage('profile', 'Préférences', null);
142 }
24cec3d8 143
b5163f52
FB
144 // Run Bananai
145 if (Banana::$action == 'profile') {
146 Banana::$page->run();
147 return $this->action_updateProfile();
148 } else {
149 return parent::run();
150 }
24cec3d8 151 }
152
b2bffbe6 153 public function post($dest, $reply, $subject, $body)
154 {
155 global $globals;
7d15f750 156 Banana::$profile['headers']['From'] = $this->user->fullName() . ' <' . $this->user->bestEmail() . '>';
437a98e0 157 Banana::$profile['headers']['Organization'] = make_Organization();
b2bffbe6 158 return parent::post($dest, $reply, $subject, $body);
159 }
160
24cec3d8 161 protected function action_saveSubs($groups)
162 {
163 global $globals;
7d15f750 164 $uid = $this->user->id();
24cec3d8 165
166 Banana::$profile['subscribe'] = array();
7d15f750
FB
167 XDB::execute('DELETE FROM forum_subs
168 WHERE uid = {?}', $this->user->id());
24cec3d8 169 if (!count($groups)) {
170 return true;
171 }
172
7d15f750
FB
173 $req = XDB::iterRow('SELECT fid, name
174 FROM forums');
24cec3d8 175 $fids = array();
7d15f750 176 while (list($fid, $fnom) = $req->next()) {
24cec3d8 177 $fids[$fnom] = $fid;
178 }
179
180 $diff = array_diff($groups, array_keys($fids));
181 foreach ($diff as $g) {
7d15f750
FB
182 XDB::execute('INSERT INTO forums (name)
183 VALUES ({?})', $g);
24cec3d8 184 $fids[$g] = XDB::insertId();
185 }
186
187 foreach ($groups as $g) {
7d15f750
FB
188 XDB::execute('INSERT INTO forum_subs (fid, uid)
189 VALUES ({?}, {?})',
24cec3d8 190 $fids[$g], $uid);
191 Banana::$profile['subscribe'][] = $g;
192 }
193 }
b5163f52
FB
194
195 protected function action_updateProfile()
196 {
d7610c35 197 global $globals;
7d15f750 198 $page =& Platal::page();
b5163f52 199
43cddb05
FB
200 $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
201 foreach ($colors as $key=>$path) {
202 $path = basename($path, '.gif');
203 $colors[$key] = substr($path, 2);
204 }
205 $page->assign('colors', $colors);
206
207 if (Post::has('action') && Post::v('action') == 'Enregistrer') {
208 S::assert_xsrf_token();
7d15f750 209 $flags = new PlFlagSet();
b5163f52 210 if (Post::b('bananadisplay')) {
748b472c 211 $flags->addFlag('threads');
b5163f52
FB
212 }
213 if (Post::b('bananaupdate')) {
748b472c 214 $flags->addFlag('automaj');
b5163f52
FB
215 }
216 if (Post::b('bananaxface')) {
748b472c 217 $flags->addFlag('xface');
b5163f52 218 }
43cddb05
FB
219 $unread = Post::s('unread');
220 $read = Post::s('read');
221 if (!in_array($unread, $colors) || !in_array($read, $colors)) {
222 $page->trigError('Le choix de type pour l\'arborescence est invalide');
748b472c 223 } else {
7d15f750
FB
224 $last_seen = XDB::query('SELECT last_seen
225 FROM forum_profiles
226 WHERE uid = {?}', $this->user->id());
227 if ($last_seen->numRows() > 0) {
228 $last_seen = $last_seen->fetchOneCell();
229 } else {
230 $last_seen = '0000-00-00';
231 }
232 XDB::execute('REPLACE INTO forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
233 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
234 $this->user->id(), Post::v('bananasig'),
235 Post::v('bananamail'), Post::v('banananame'),
236 $flags, $unread, $read, $last_seen);
237 $page->trigSuccess('Ton profil a été mis à jour');
748b472c
FB
238 }
239 }
240
2bd55de5
FB
241 $infos = $this->fetchProfile();
242 $page->assign('nom' , $infos['name']);
243 $page->assign('mail', $infos['mail']);
244 $page->assign('sig', $infos['sig']);
245 $page->assign('disp', $infos['threads']);
246 $page->assign('maj', $infos['maj']);
247 $page->assign('xface', $infos['xface']);
248 $page->assign('unread', $infos['tree_unread']);
249 $page->assign('read', $infos['tree_read']);
b5163f52
FB
250 return null;
251 }
24cec3d8 252}
253
a7de4ef7 254// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
24cec3d8 255?>