Merge commit 'origin/fusionax' into account
[platal.git] / include / banana / forum.inc.php
index 5e0978e..86d13b5 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,44 +24,62 @@ require_once 'banana/hooks.inc.php';
 
 function hook_checkcancel($_headers)
 {
-    return ($_headers['x-org-id'] == S::v('forlife') or S::has_perms());
-}
-
-function hook_makeLink($params)
-{
-    global $globals;
-    $base = $globals->baseurl . '/banana';
-    if (isset($params['page'])) {
-        return $base . '/' . $params['page'];
-    }
-    if (@$params['action'] == 'subscribe') {
-        return $base . '/subscription';
-    }
-
-    if (!isset($params['group'])) {
-        return $base;
-    }
-    $base .= '/' . $params['group'];
-    $base = $base . hook_platalMessageLink($params);
-    if (@$params['action'] == 'showext') {
-        $base .= '?action=showext';
-    }
-    return $base;
+    return ($_headers['x-org-id'] == S::v('hruid') or S::has_perms());
 }
 
 class ForumsBanana extends Banana
 {
-    function __construct($params = null)
+    private $user;
+
+    public function __construct(User &$user, $params = null)
     {
+        $this->user = &$user;
+
         global $globals;
         Banana::$msgedit_canattach = false;
+        Banana::$spool_root = $globals->banana->spool_root;
         array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
-        Banana::$nntp_host = 'news://web_'.S::v('forlife')
+        Banana::$nntp_host = 'news://web_' . $user->login()
                            . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
         if (S::has_perms()) {
             Banana::$msgshow_mimeparts[] = 'source';
         }
-        parent::__construct($params);
+        Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
+        Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
+        Banana::$feed_active = S::hasAuthToken();
+
+        parent::__construct($params, 'NNTP', 'PlatalBananaPage');
+        if (@$params['action'] == 'profile') {
+            Banana::$action = 'profile';
+        }
+    }
+
+    private function fetchProfile()
+    {
+        // Get user profile from SQL
+        $req = XDB::query("SELECT  name, mail, sig,
+                                   FIND_IN_SET('threads',flags) AS threads,
+                                   FIND_IN_SET('automaj',flags) AS maj,
+                                   FIND_IN_SET('xface', flags) AS xface,
+                                   tree_unread, tree_read
+                             FROM  forum_profiles
+                            WHERE  uid = {?}", $this->user->id());
+        if ($req->numRows()) {
+            $infos = $req->fetchOneAssoc();
+        } else {
+            $infos = array();
+        }
+        if (empty($infos['name'])) {
+            $infos = array('name' => $this->user->fullName(),
+                           'mail' => $this->user->forlifeEmail(),
+                           'sig'  => $this->user->displayName(),
+                           'threads' => false,
+                           'maj'  => true,
+                           'xface' => false,
+                           'tree_unread' => 'o',
+                           'tree_read' => 'dg' );
+        }
+        return $infos;
     }
 
     public function run()
@@ -72,90 +90,161 @@ class ForumsBanana extends Banana
         $time = null;
         if (!is_null($this->params) && isset($this->params['updateall'])) {
             $time = intval($this->params['updateall']);
-            $_SESSION['banana_last']     = $time;
+            S::set('banana_last', $time);
         }
 
-        // Get user profile from SQL
-        $req = XDB::query("SELECT  nom, mail, sig,
-                                   FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags)
-                             FROM  {$globals->banana->table_prefix}profils
-                            WHERE  uid={?}", S::i('uid'));
-        if (!(list($nom,$mail,$sig,$disp,$maj) = $req->fetchOneRow())) {
-            $nom  = S::v('prenom')." ".S::v('nom');
-            $mail = S::v('forlife')."@polytechnique.org";
-            $sig  = $nom." (".S::v('promo').")";
-            $disp = 0;
-            $maj  = 1;
-        }
-        if ($maj) {
+        $infos = $this->fetchProfile();
+        if ($infos['maj']) {
             $time = time();
         }
 
         // Build user profile
-        $req = XDB::query("      
-                 SELECT  nom     
-                   FROM  {$globals->banana->table_prefix}abos
-              LEFT JOIN  {$globals->banana->table_prefix}list ON list.fid=abos.fid
-                  WHERE  uid={?}", S::i('uid'));
-        Banana::$profile['headers']['From']         = utf8_encode("$nom <$mail>");
-        Banana::$profile['headers']['Organization'] = 'Utilisateur de Polytechnique.org';
-        Banana::$profile['signature']               = utf8_encode($sig);
-        Banana::$profile['display']                 = $disp;
-        Banana::$profile['autoup']                  = $maj;
+        $req = XDB::query("SELECT  name
+                             FROM  forum_subs AS fs
+                        LEFT JOIN  forums AS f ON (f.fid = fs.fid)
+                            WHERE  uid={?}", $this->user->id());
+        Banana::$profile['headers']['From']         = $infos['name'] . ' <' . $infos['mail'] . '>';
+        Banana::$profile['headers']['Organization'] = make_Organization();
+        Banana::$profile['signature']               = $infos['sig'];
+        Banana::$profile['display']                 = $infos['threads'];
+        Banana::$profile['autoup']                  = $infos['maj'];
         Banana::$profile['lastnews']                = S::v('banana_last');
         Banana::$profile['subscribe']               = $req->fetchColumn();
+        Banana::$tree_unread = $infos['tree_unread'];
+        Banana::$tree_read = $infos['tree_read'];
 
-        // Update the "unread limit" 
+        // Update the "unread limit"
         if (!is_null($time)) {
-            XDB::execute("UPDATE  auth_user_quick
-                             SET  banana_last = FROM_UNIXTIME({?})
-                           WHERE  user_id={?}",
-                         $time, S::i('uid'));
+            XDB::execute('UPDATE  forum_profiles
+                             SET  last_seen = FROM_UNIXTIME({?})
+                           WHERE  uid = {?}',
+                         $time, $this->user->id());
+            if (XDB::affectedRows() == 0) {
+                XDB::execute('INSERT INTO  forum_profiles (uid, last_seen)
+                                   VALUES  ({?}, FROM_UNIXTIME({?}))',
+                             $this->user->id(), $time);
+            }
+        }
+
+        if (!empty($GLOBALS['IS_XNET_SITE'])) {
+            Banana::$page->killPage('forums');
+            Banana::$page->killPage('subscribe');
+            Banana::$spool_boxlist = false;
+        } else {
+            // Register custom Banana links and tabs
+            if (!Banana::$profile['autoup']) {
+                Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
+                                    . $platal->path . '", "updateall", ' . time() . ')\'>'
+                                    . 'Marquer tous les messages comme lus'
+                                    . '</a>', array('forums', 'thread', 'message'));
+            }
+            Banana::$page->registerPage('profile', 'Préférences', null);
         }
 
-        // Register custom Banana links and tabs
-        if (!Banana::$profile['autoup']) {
-            Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
-                                . $platal->path . '", "updateall", ' . time() . ')\'>'
-                                . 'Marquer tous les messages comme lus'
-                                . '</a>', array('forums', 'thread', 'message'));
-        }   
-        Banana::$page->registerPage('profile', utf8_encode('Préférences'), null);
-        
-
-        // Run Banana
-        return parent::run();
+        // Run Bananai
+        if (Banana::$action == 'profile') {
+            Banana::$page->run();
+            return $this->action_updateProfile();
+        } else {
+            return parent::run();
+        }
+    }
+
+    public function post($dest, $reply, $subject, $body)
+    {
+        global $globals;
+        Banana::$profile['headers']['From']         = $this->user->fullName() .  ' <' . $this->user->bestEmail() . '>';
+        Banana::$profile['headers']['Organization'] = make_Organization();
+        return parent::post($dest, $reply, $subject, $body);
     }
 
     protected function action_saveSubs($groups)
     {
         global $globals;
-        $uid = S::v('uid');
+        $uid = $this->user->id();
 
         Banana::$profile['subscribe'] = array();
-        XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
+        XDB::execute('DELETE FROM  forum_subs
+                            WHERE  uid = {?}', $this->user->id());
         if (!count($groups)) {
             return true;
         }
 
-        $req  = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
-        $fids = array();
-        while (list($fid,$fnom) = $req->next()) {
-            $fids[$fnom] = $fid;
-        }
-
+        $fids = XDB::fetchAllAssoc('name', 'SELECT  fid, name
+                                              FROM  forums');
         $diff = array_diff($groups, array_keys($fids));
         foreach ($diff as $g) {
-            XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
+            XDB::execute('INSERT INTO  forums (name)
+                               VALUES  ({?})', $g);
             $fids[$g] = XDB::insertId();
         }
 
         foreach ($groups as $g) {
-            XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})",
+            XDB::execute('INSERT INTO  forum_subs (fid, uid)
+                               VALUES  ({?}, {?})',
                          $fids[$g], $uid);
             Banana::$profile['subscribe'][] = $g;
         }
     }
+
+    protected function action_updateProfile()
+    {
+        global $globals;
+        $page =& Platal::page();
+
+        $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
+        foreach ($colors as $key=>$path) {
+            $path = basename($path, '.gif');
+            $colors[$key] = substr($path, 2);
+        }
+        $page->assign('colors', $colors);
+
+        if (Post::has('action') && Post::v('action') == 'Enregistrer') {
+            S::assert_xsrf_token();
+            $flags = new PlFlagSet();
+            if (Post::b('bananadisplay')) {
+                $flags->addFlag('threads');
+            }
+            if (Post::b('bananaupdate')) {
+                $flags->addFlag('automaj');
+            }
+            if (Post::b('bananaxface')) {
+                $flags->addFlag('xface');
+            }
+            $unread = Post::s('unread');
+            $read = Post::s('read');
+            if (!in_array($unread, $colors) || !in_array($read, $colors)) {
+                $page->trigError('Le choix de type pour l\'arborescence est invalide');
+            } else {
+                $last_seen = XDB::query('SELECT  last_seen
+                                           FROM  forum_profiles
+                                          WHERE  uid = {?}', $this->user->id());
+                if ($last_seen->numRows() > 0) {
+                    $last_seen = $last_seen->fetchOneCell();
+                } else {
+                    $last_seen = '0000-00-00';
+                }
+                XDB::execute('REPLACE INTO  forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
+                                    VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
+                             $this->user->id(), Post::v('bananasig'),
+                             Post::v('bananamail'), Post::v('banananame'),
+                             $flags, $unread, $read, $last_seen);
+                $page->trigSuccess('Ton profil a été mis à jour');
+            }
+        }
+
+        $infos = $this->fetchProfile();
+        $page->assign('nom' ,  $infos['name']);
+        $page->assign('mail',  $infos['mail']);
+        $page->assign('sig',   $infos['sig']);
+        $page->assign('disp',  $infos['threads']);
+        $page->assign('maj',   $infos['maj']);
+        $page->assign('xface', $infos['xface']);
+        $page->assign('unread', $infos['tree_unread']);
+        $page->assign('read', $infos['tree_read']);
+        return null;
+    }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>