RSS and email are auth_user free.
[platal.git] / include / banana / forum.inc.php
CommitLineData
24cec3d8 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
27fd34d1
FB
173 $fids = XDB::fetchAllAssoc('name', 'SELECT fid, name
174 FROM forums');
24cec3d8 175 $diff = array_diff($groups, array_keys($fids));
176 foreach ($diff as $g) {
7d15f750
FB
177 XDB::execute('INSERT INTO forums (name)
178 VALUES ({?})', $g);
24cec3d8 179 $fids[$g] = XDB::insertId();
180 }
181
182 foreach ($groups as $g) {
7d15f750
FB
183 XDB::execute('INSERT INTO forum_subs (fid, uid)
184 VALUES ({?}, {?})',
24cec3d8 185 $fids[$g], $uid);
186 Banana::$profile['subscribe'][] = $g;
187 }
188 }
b5163f52
FB
189
190 protected function action_updateProfile()
191 {
d7610c35 192 global $globals;
7d15f750 193 $page =& Platal::page();
b5163f52 194
43cddb05
FB
195 $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
196 foreach ($colors as $key=>$path) {
197 $path = basename($path, '.gif');
198 $colors[$key] = substr($path, 2);
199 }
200 $page->assign('colors', $colors);
201
202 if (Post::has('action') && Post::v('action') == 'Enregistrer') {
203 S::assert_xsrf_token();
7d15f750 204 $flags = new PlFlagSet();
b5163f52 205 if (Post::b('bananadisplay')) {
748b472c 206 $flags->addFlag('threads');
b5163f52
FB
207 }
208 if (Post::b('bananaupdate')) {
748b472c 209 $flags->addFlag('automaj');
b5163f52
FB
210 }
211 if (Post::b('bananaxface')) {
748b472c 212 $flags->addFlag('xface');
b5163f52 213 }
43cddb05
FB
214 $unread = Post::s('unread');
215 $read = Post::s('read');
216 if (!in_array($unread, $colors) || !in_array($read, $colors)) {
217 $page->trigError('Le choix de type pour l\'arborescence est invalide');
748b472c 218 } else {
7d15f750
FB
219 $last_seen = XDB::query('SELECT last_seen
220 FROM forum_profiles
221 WHERE uid = {?}', $this->user->id());
222 if ($last_seen->numRows() > 0) {
223 $last_seen = $last_seen->fetchOneCell();
224 } else {
225 $last_seen = '0000-00-00';
226 }
227 XDB::execute('REPLACE INTO forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
228 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
229 $this->user->id(), Post::v('bananasig'),
230 Post::v('bananamail'), Post::v('banananame'),
231 $flags, $unread, $read, $last_seen);
232 $page->trigSuccess('Ton profil a été mis à jour');
748b472c
FB
233 }
234 }
235
2bd55de5
FB
236 $infos = $this->fetchProfile();
237 $page->assign('nom' , $infos['name']);
238 $page->assign('mail', $infos['mail']);
239 $page->assign('sig', $infos['sig']);
240 $page->assign('disp', $infos['threads']);
241 $page->assign('maj', $infos['maj']);
242 $page->assign('xface', $infos['xface']);
243 $page->assign('unread', $infos['tree_unread']);
244 $page->assign('read', $infos['tree_read']);
b5163f52
FB
245 return null;
246 }
24cec3d8 247}
248
a7de4ef7 249// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
24cec3d8 250?>