Merge commit 'origin/fusionax' into account
[platal.git] / include / banana / forum.inc.php
index 7ccabd7..393d39a 100644 (file)
@@ -46,15 +46,42 @@ class ForumsBanana extends Banana
         }
         Banana::$debug_nntp = ($globals->debug & DEBUG_BT);
         Banana::$debug_smarty = ($globals->debug & DEBUG_SMARTY);
-        if (!S::v('core_rss_hash')) {
-            Banana::$feed_active = false;
-        }
+        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()
     {
         global $platal, $globals;
@@ -63,50 +90,40 @@ 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),
-                                   tree_unread, tree_read
-                             FROM  {$globals->banana->table_prefix}profils
-                            WHERE  uid={?}", S::i('uid'));
-        if (!(list($nom, $mail, $sig, $disp, $maj, $unread, $read) = $req->fetchOneRow())) {
-            $nom  = S::v('prenom')." ".S::v('nom');
-            $mail = $this->user->forlifeEmail();
-            $sig  = $nom." (".S::v('promo').")";
-            $disp = 0;
-            $maj  = 1;
-            $unread = 'o';
-            $read   = 'dg';
-        }
-        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']         = "$nom <$mail>";
+        $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']               = $sig;
-        Banana::$profile['display']                 = $disp;
-        Banana::$profile['autoup']                  = $maj;
+        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 = $unread;
-        Banana::$tree_read = $read;
+        Banana::$tree_unread = $infos['tree_unread'];
+        Banana::$tree_read = $infos['tree_read'];
 
         // 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'])) {
@@ -136,11 +153,7 @@ class ForumsBanana extends Banana
     public function post($dest, $reply, $subject, $body)
     {
         global $globals;
-        $res = XDB::query('SELECT  nom, prenom, promo
-                             FROM  auth_user_md5 AS u
-                            WHERE  u.user_id = {?}', $this->user->id());
-        list($nom, $prenom, $promo) = $res->fetchOneRow();
-        Banana::$profile['headers']['From']         = "$prenom $nom ($promo) <{$this->user->bestEmail()}>";
+        Banana::$profile['headers']['From']         = $this->user->fullName() .  ' <' . $this->user->bestEmail() . '>';
         Banana::$profile['headers']['Organization'] = make_Organization();
         return parent::post($dest, $reply, $subject, $body);
     }
@@ -148,28 +161,27 @@ class ForumsBanana extends Banana
     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;
         }
@@ -178,7 +190,7 @@ class ForumsBanana extends Banana
     protected function action_updateProfile()
     {
         global $globals;
-        $page = Platal::page();
+        $page =& Platal::page();
 
         $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
         foreach ($colors as $key=>$path) {
@@ -189,7 +201,7 @@ class ForumsBanana extends Banana
 
         if (Post::has('action') && Post::v('action') == 'Enregistrer') {
             S::assert_xsrf_token();
-            $flags = new FlagSet();
+            $flags = new PlFlagSet();
             if (Post::b('bananadisplay')) {
                 $flags->addFlag('threads');
             }
@@ -203,44 +215,33 @@ class ForumsBanana extends Banana
             $read = Post::s('read');
             if (!in_array($unread, $colors) || !in_array($read, $colors)) {
                 $page->trigError('Le choix de type pour l\'arborescence est invalide');
-            } elseif (XDB::execute("REPLACE INTO  forums.profils (uid, sig, mail, nom, flags, tree_unread, tree_read)
-                                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
-                                    S::v('uid'), Post::v('bananasig'),
-                                    Post::v('bananamail'), Post::v('banananame'),
-                                    $flags, $unread, $read)) {
-                $page->trigSuccess("Ton profil a été enregistré avec succès.");
             } else {
-                $page->trigError("Une erreur s'est produite lors de l'enregistrement de ton profil");
+                $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');
             }
         }
 
-        $req = XDB::query("
-            SELECT  nom, mail, sig,
-                    FIND_IN_SET('threads', flags),
-                    FIND_IN_SET('automaj', flags),
-                    FIND_IN_SET('xface', flags),
-                    tree_unread,
-                    tree_read
-              FROM  forums.profils
-             WHERE  uid = {?}", S::v('uid'));
-        if (!(list($nom, $mail, $sig, $disp, $maj, $xface, $unread, $read) = $req->fetchOneRow())) {
-            $nom   = S::v('prenom').' '.S::v('nom');
-            $mail  = S::user()->forlifeEmail();
-            $sig   = $nom.' ('.S::v('promo').')';
-            $disp  = 0;
-            $maj   = 0;
-            $xface = 0;
-            $unread = 'o';
-            $read  = 'dg';
-        }
-        $page->assign('nom' ,  $nom);
-        $page->assign('mail',  $mail);
-        $page->assign('sig',   $sig);
-        $page->assign('disp',  $disp);
-        $page->assign('maj',   $maj);
-        $page->assign('xface', $xface);
-        $page->assign('unread', $unread);
-        $page->assign('read', $read);
+        $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;
     }
 }