Cleanup javascript again.
[platal.git] / include / banana / forum.inc.php
CommitLineData
24cec3d8 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 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
24cec3d8 22require_once 'banana/hooks.inc.php';
23
24function hook_checkcancel($_headers)
25{
dd70cd28 26 return ($_headers['x-org-id'] == S::v('hruid') or S::admin());
24cec3d8 27}
28
fa7d6c7b 29class ForumsBanana extends Banana
24cec3d8 30{
e3a55098 31 private $user;
b2bffbe6 32
e3a55098 33 public function __construct(User &$user, $params = null)
24cec3d8 34 {
e3a55098 35 $this->user = &$user;
b2bffbe6 36
24cec3d8 37 global $globals;
38 Banana::$msgedit_canattach = false;
5f0ecf6c 39 Banana::$spool_root = $globals->spoolroot . '/spool/banana/';
24cec3d8 40 array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
ad59fc86 41 Banana::$nntp_host = self::buildURL($user->login());
dd70cd28 42 if (S::admin()) {
1515e65a 43 Banana::$msgshow_mimeparts[] = 'source';
44 }
81e9c63f 45 Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
46 Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
8c12f931
FB
47 Banana::$feed_active = S::hasAuthToken();
48
b8458bd1 49 parent::__construct($params, 'NNTP', 'PlatalBananaPage');
ed0c62e4 50 if (@$params['action'] == 'profile') {
b5163f52
FB
51 Banana::$action = 'profile';
52 }
24cec3d8 53 }
54
ad59fc86
FB
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;
708d70eb
RB
62 $pass = $globals->banana->password;
63 } else {
64 $pass = $globals->banana->web_pass;
ad59fc86
FB
65 }
66 return $scheme . '://' . $user
708d70eb 67 . ":{$pass}@{$globals->banana->server}:{$globals->banana->port}/";
ad59fc86
FB
68
69 }
70
2bd55de5
FB
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
24cec3d8 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']);
5f0ecf6c 107 $this->user->banana_last = $time;
24cec3d8 108 }
109
2bd55de5
FB
110 $infos = $this->fetchProfile();
111 if ($infos['maj']) {
24cec3d8 112 $time = time();
113 }
114
115 // Build user profile
7d15f750
FB
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());
2bd55de5 120 Banana::$profile['headers']['From'] = $infos['name'] . ' <' . $infos['mail'] . '>';
437a98e0 121 Banana::$profile['headers']['Organization'] = make_Organization();
2bd55de5
FB
122 Banana::$profile['signature'] = $infos['sig'];
123 Banana::$profile['display'] = $infos['threads'];
124 Banana::$profile['autoup'] = $infos['maj'];
5f0ecf6c 125 Banana::$profile['lastnews'] = $this->user->banana_last;
24cec3d8 126 Banana::$profile['subscribe'] = $req->fetchColumn();
2bd55de5
FB
127 Banana::$tree_unread = $infos['tree_unread'];
128 Banana::$tree_read = $infos['tree_read'];
24cec3d8 129
eaf30d86 130 // Update the "unread limit"
24cec3d8 131 if (!is_null($time)) {
7d15f750
FB
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) {
c88a3cd0
FB
137 XDB::execute('INSERT IGNORE INTO forum_profiles (uid, last_seen)
138 VALUES ({?}, FROM_UNIXTIME({?}))',
7d15f750
FB
139 $this->user->id(), $time);
140 }
24cec3d8 141 }
142
4f355064 143 if (!empty($GLOBALS['IS_XNET_SITE'])) {
144 Banana::$page->killPage('forums');
145 Banana::$page->killPage('subscribe');
146 Banana::$spool_boxlist = false;
eaf30d86 147 } else {
4f355064 148 // Register custom Banana links and tabs
149 if (!Banana::$profile['autoup']) {
e5b254d5 150 Banana::$page->registerAction('<a href=\'javascript:$.dynPost("'
4f355064 151 . $platal->path . '", "updateall", ' . time() . ')\'>'
152 . 'Marquer tous les messages comme lus'
153 . '</a>', array('forums', 'thread', 'message'));
eaf30d86 154 }
4f355064 155 Banana::$page->registerPage('profile', 'Préférences', null);
156 }
24cec3d8 157
b5163f52
FB
158 // Run Bananai
159 if (Banana::$action == 'profile') {
160 Banana::$page->run();
161 return $this->action_updateProfile();
162 } else {
163 return parent::run();
164 }
24cec3d8 165 }
166
b2bffbe6 167 public function post($dest, $reply, $subject, $body)
168 {
169 global $globals;
7d15f750 170 Banana::$profile['headers']['From'] = $this->user->fullName() . ' <' . $this->user->bestEmail() . '>';
437a98e0 171 Banana::$profile['headers']['Organization'] = make_Organization();
b2bffbe6 172 return parent::post($dest, $reply, $subject, $body);
173 }
174
24cec3d8 175 protected function action_saveSubs($groups)
176 {
177 global $globals;
7d15f750 178 $uid = $this->user->id();
24cec3d8 179
180 Banana::$profile['subscribe'] = array();
7d15f750
FB
181 XDB::execute('DELETE FROM forum_subs
182 WHERE uid = {?}', $this->user->id());
24cec3d8 183 if (!count($groups)) {
184 return true;
185 }
186
27fd34d1
FB
187 $fids = XDB::fetchAllAssoc('name', 'SELECT fid, name
188 FROM forums');
24cec3d8 189 $diff = array_diff($groups, array_keys($fids));
190 foreach ($diff as $g) {
7d15f750
FB
191 XDB::execute('INSERT INTO forums (name)
192 VALUES ({?})', $g);
24cec3d8 193 $fids[$g] = XDB::insertId();
194 }
195
196 foreach ($groups as $g) {
7d15f750
FB
197 XDB::execute('INSERT INTO forum_subs (fid, uid)
198 VALUES ({?}, {?})',
24cec3d8 199 $fids[$g], $uid);
200 Banana::$profile['subscribe'][] = $g;
201 }
202 }
b5163f52
FB
203
204 protected function action_updateProfile()
205 {
d7610c35 206 global $globals;
7d15f750 207 $page =& Platal::page();
b5163f52 208
43cddb05
FB
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();
7d15f750 218 $flags = new PlFlagSet();
b5163f52 219 if (Post::b('bananadisplay')) {
748b472c 220 $flags->addFlag('threads');
b5163f52
FB
221 }
222 if (Post::b('bananaupdate')) {
748b472c 223 $flags->addFlag('automaj');
b5163f52
FB
224 }
225 if (Post::b('bananaxface')) {
748b472c 226 $flags->addFlag('xface');
b5163f52 227 }
43cddb05
FB
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');
748b472c 232 } else {
7d15f750
FB
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 }
00ba8a74
SJ
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)',
7d15f750
FB
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');
748b472c
FB
249 }
250 }
251
2bd55de5 252 $infos = $this->fetchProfile();
5660032a
SJ
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']);
2bd55de5 259 $page->assign('unread', $infos['tree_unread']);
5660032a 260 $page->assign('read', $infos['tree_read']);
b5163f52
FB
261 return null;
262 }
24cec3d8 263}
264
a7de4ef7 265// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
24cec3d8 266?>