Merge commit 'origin/master' into hruid.
[platal.git] / include / banana / forum.inc.php
index 5e0978e..25d8caf 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,35 @@ 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);
+        if (!S::v('core_rss_hash')) {
+            Banana::$feed_active = false;
+        }
+        parent::__construct($params, 'NNTP', 'PlatalBananaPage');
+        if (@$params['action'] == 'profile') {
+            Banana::$action = 'profile';
+        }
     }
 
     public function run()
@@ -82,7 +73,7 @@ class ForumsBanana extends Banana
                             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";
+            $mail = $this->user->forlifeEmail();
             $sig  = $nom." (".S::v('promo').")";
             $disp = 0;
             $maj  = 1;
@@ -92,20 +83,20 @@ class ForumsBanana extends Banana
         }
 
         // Build user profile
-        $req = XDB::query("      
-                 SELECT  nom     
+        $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['headers']['From']         = "$nom <$mail>";
+        Banana::$profile['headers']['Organization'] = make_Organization();
+        Banana::$profile['signature']               = $sig;
         Banana::$profile['display']                 = $disp;
         Banana::$profile['autoup']                  = $maj;
         Banana::$profile['lastnews']                = S::v('banana_last');
         Banana::$profile['subscribe']               = $req->fetchColumn();
 
-        // Update the "unread limit" 
+        // Update the "unread limit"
         if (!is_null($time)) {
             XDB::execute("UPDATE  auth_user_quick
                              SET  banana_last = FROM_UNIXTIME({?})
@@ -113,18 +104,40 @@ class ForumsBanana extends Banana
                          $time, S::i('uid'));
         }
 
-        // 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();
+        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);
+        }
+
+        // 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;
+        $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']['Organization'] = make_Organization();
+        return parent::post($dest, $reply, $subject, $body);
     }
 
     protected function action_saveSubs($groups)
@@ -156,6 +169,60 @@ class ForumsBanana extends Banana
             Banana::$profile['subscribe'][] = $g;
         }
     }
+
+    protected function action_updateProfile()
+    {
+        global $globals;
+        $page = Platal::page();
+
+        if (Post::has('action') && Post::has('banananame') && Post::has('bananasig')
+                && Post::has('bananadisplay') && Post::has('bananamail')
+                && Post::has('bananaupdate') && Post::v('action')=="Enregistrer" ) {
+            $flags = new PlFlagSet();
+            if (Post::b('bananadisplay')) {
+                $flags->addFlag('threads');
+            }
+            if (Post::b('bananaupdate')) {
+                $flags->addFlag('automaj');
+            }
+            if (Post::b('bananaxface')) {
+                $flags->addFlag('xface');
+            }
+            if (XDB::execute("REPLACE INTO  forums.profils (uid, sig, mail, nom, flags)
+                                VALUES  ({?}, {?}, {?}, {?}, {?})",
+                         S::v('uid'), Post::v('bananasig'),
+                         Post::v('bananamail'), Post::v('banananame'),
+                         $flags)) {
+                $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");
+            }
+        }
+
+        $req = XDB::query("
+            SELECT  nom, mail, sig,
+                    FIND_IN_SET('threads', flags),
+                    FIND_IN_SET('automaj', flags),
+                    FIND_IN_SET('xface', flags)
+              FROM  forums.profils
+             WHERE  uid = {?}", S::v('uid'));
+        if (!(list($nom, $mail, $sig, $disp, $maj, $xface) = $req->fetchOneRow())) {
+            $nom   = S::v('prenom').' '.S::v('nom');
+            $mail  = S::user()->forlifeEmail();
+            $sig   = $nom.' ('.S::v('promo').')';
+            $disp  = 0;
+            $maj   = 0;
+            $xface = 0;
+        }
+        $page->assign('nom' ,  $nom);
+        $page->assign('mail',  $mail);
+        $page->assign('sig',   $sig);
+        $page->assign('disp',  $disp);
+        $page->assign('maj',   $maj);
+        $page->assign('xface', $xface);
+        return null;
+    }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>