Some improvements for banana 'stand-alone'
[banana.git] / banana / banana.inc.php.in
index ac38001..5a514bc 100644 (file)
@@ -26,6 +26,7 @@ class Banana
                                     'autoup' => 1);
     static public $boxpattern;
     static public $withtabs = true;
+    static public $baseurl   = null;
     static public $mimeparts = array();
 
 ### Spool ###
@@ -53,6 +54,7 @@ class Banana
     static public $msgshow_externalimages = false;
     static public $msgshow_hasextimages   = false;
     static public $msgshow_withthread = true;
+    static public $msgshow_javascript = true;
 
     /** Match an url
      * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
@@ -65,7 +67,7 @@ class Banana
      *   $matches[2] = "http://www.polytechnique.org"
      *   $matches[3] = "]"
      */
-    static public $msgshow_url     = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
+    static public $msgshow_url     = '(["\[\<])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]\>])?';
 
 ### Message edition ###
     static public $msgedit_canattach  = true;
@@ -81,7 +83,9 @@ class Banana
     static public $feed_active         = true;
     static public $feed_format         = 'rss2';
     static public $feed_updateOnDemand = false; // Update the feed each time sbd check it
-    static public $feed_copyright      = null;
+    static public $feed_copyright      = null;  // Global copyright informations
+    static public $feed_generator      = 'Banana @VERSION@'; // Feed generator
+    static public $feed_email          = null;  // Admin e-mail
     static public $feed_namePrefix     = 'Banana :: ';
     static public $feed_size           = 15;    // Number of messages in the feed
 
@@ -254,7 +258,7 @@ class Banana
             return Banana::$page->kill(_b_('Connexion non-valide'));
         }
         if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
-            if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) {
+            if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', Banana::$group)) {
                 Banana::$page->setPage('group');        
                 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
             }
@@ -296,6 +300,27 @@ class Banana
         return Banana::$page->run();
     }
 
+    /** Build and post a new message
+     * @return postid (or -1 if the message has not been found)
+     */
+    public function post($dest, $reply, $subject, $body)
+    {
+        $hdrs = Banana::$protocole->requestedHeaders();
+        $headers                 = Banana::$profile['headers'];
+        $headers[$hdrs['dest']]  = $dest;
+        if ($reply) {
+            $headers[$hdrs['reply']] = $reply;
+        } 
+        $headers['Subject']      = $subject;
+        $msg = BananaMessage::newMessage($headers, $body);
+        if (Banana::$protocole->send($msg)) {
+            Banana::$group = ($reply ? $reply : $dest);
+            $this->loadSpool(Banana::$group);
+            return Banana::$spool->getPostId($subject);
+        }
+        return -1;
+    }
+
     /** Return the CSS code to include in the headers
      */
     public function css()
@@ -360,6 +385,9 @@ class Banana
     {
         Banana::load('feed');
         if (Banana::$group) {
+            if (Banana::$feed_updateOnDemand) { 
+                $this->loadSpool(Banana::$group); 
+            } 
             $feed =& BananaFeed::getFeed();
             $feed->toXML();
         }
@@ -464,16 +492,25 @@ class Banana
             $hdr_values = array();
             foreach ($hdrs as $header) {
                 $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
+                if (!is_utf8($hdr_values[$header])) {
+                    $hdr_values[$header] = utf8_encode($hdr_values[$header]);
+                }
+                if ($headers != 'Subject') {
+                    $hdr_values[$header] = str_replace(', ', ',', $hdr_values[$header]);
+                }
             }
-            if ($artid) {
+            if (!is_null($artid)) {
                 $old =& $this->loadMessage($group, $artid);
                 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
             }
             $msg = null;
+            if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
+                $_POST['body'] = utf8_encode($_POST['body']);
+            }
             if (empty($hdr_values['Subject'])) {
                 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
-            } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) {
-                $uploaded = $_FILES['attachment'];
+            } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
+                $uploaded =& $_FILES['attachment'];
                 if (!is_uploaded_file($uploaded['tmp_name'])) {
                     Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
                 } else {
@@ -484,10 +521,13 @@ class Banana
             }
             if (!is_null($msg)) {
                 if (Banana::$protocole->send($msg)) {
-                    Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
-                }
-                Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
+                    $this->loadSpool($group);
+                    $newid = Banana::$spool->updateUnread(Banana::$profile['lastnews']);
+                    Banana::$page->redirect(array('group' => $group, 'artid' => $newid ? $newid : $artid));
+                } else {
+                    Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
                                    . Banana::$protocole->lastError());
+                }
             }
         } else {
             if (!is_null($artid)) {
@@ -629,19 +669,23 @@ class Banana
         Banana::load('spool');
         if (!Banana::$spool || Banana::$spool->group != $group) {
             $clean = false;
-            if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
-                Banana::$spool = unserialize($_SESSION['banana_spool']);
-                $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
-            } else {
-                unset($_SESSION['banana_message']);
-                unset($_SESSION['banana_artid']);
-                unset($_SESSION['banana_showhdr']);
+            if (php_sapi_name() != 'cli') {
+                if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
+                    Banana::$spool = unserialize($_SESSION['banana_spool']);
+                    $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
+                } else {
+                    unset($_SESSION['banana_message']);
+                    unset($_SESSION['banana_artid']);
+                    unset($_SESSION['banana_showhdr']);
+                }
             }
             BananaSpool::getSpool($group, Banana::$profile['lastnews'], Banana::$profile['autoup'] || $clean);
-            $_SESSION['banana_group'] = $group;
-            if (!Banana::$profile['display']) {
-                $_SESSION['banana_spool'] = serialize(Banana::$spool);
-                $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
+            if (php_sapi_name() != 'cli') {
+                $_SESSION['banana_group'] = $group;
+                if (!Banana::$profile['display']) {
+                    $_SESSION['banana_spool'] = serialize(Banana::$spool);
+                    $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
+                }
             }
             Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
         }