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