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