Use link abstraction for xface
[banana.git] / banana / banana.inc.php.in
index 9989956..b1ec2b2 100644 (file)
@@ -10,7 +10,6 @@
 class Banana
 {
     var $maxspool    = 3000;
-    var $maxfilesize = 100000;
 
     var $hdecode     = array('from','name','organization','subject');
     var $parse_hdr   = array('content-disposition', 'content-transfer-encoding', 'content-type', 'date', 'followup-to', 'from',
@@ -20,6 +19,15 @@ class Banana
     /** Favorites MIMEtypes to use, by order for reading multipart messages
      */
     var $body_mime   = array('text/plain', 'text/html', 'text/richtext');
+    /** Indicate wether posting attachment is allowed
+     */
+    var $can_attach  = true;
+    /** Maximum allowed file size for attachment
+     */
+    var $maxfilesize = 100000;
+    /** Indicate wether x-face should be skinned as specials data or not
+     */
+    var $formatxface = true;
 
     /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
      * ex : '^xorg\..*' for xorg.*
@@ -31,14 +39,39 @@ class Banana
     var $tmax        = 50;
 
     var $wrap        = 74;
+    /** Match an url
+     * Should be included in a regexp delimited using ! (eg: "!$url_regexp!i")
+     * If it matches, return 3 main parts :
+     *  \\1 and \\3 are delimiters
+     *  \\2 is the url
+     *
+     * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
+     *   $matches[1] = "["
+     *   $matches[2] = "http://www.polytechnique.org"
+     *   $matches[3] = "]"
+     */
+    var $url_regexp  = '(["\[])?((?:https?|ftp|news)://(?:&|,?[a-z@0-9.~%$£µ&i#\-+=_/\?])*)(["\]])?';
 
-    var $boundary    = "bananaBoundary42";
-    var $custom      = "Content-Type: text/plain; charset=utf-8\nMime-Version: 1.0\nContent-Transfer-Encoding: 8bit\nUser-Agent: Banana @VERSION@\n";
-    var $custom_mp   = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\nUser-Agent: Banana @VERSION@\n";
-    var $custom_bd   = "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit";
+    
+    /** Boundary for multipart messages
+     */
+    var $boundary    = 'bananaBoundary42';
+    /** Global headers to use for messages
+     */
+    var $custom      = "Mime-Version: 1.0\nUser-Agent: Banana @VERSION@\n";
+    /** Global headers to use from multipart messages
+     */
+    var $custom_mp   = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\n";
+    /** Body type when using plain text
+     */
+    var $custom_plain= "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit\n"; 
 
+    /** News serveur to use
+     */
     var $host        = 'news://localhost:119/';
 
+    /** User profile
+     */
     var $profile     = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig'  => '', 'org'  => '',
             'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale'  => 'fr_FR', 'subscribe' => array());
     
@@ -49,34 +82,49 @@ class Banana
     var $post;
     var $spool;
 
+    var $get;
+
     function Banana()
     {
         $this->_require('NetNNTP');
         setlocale(LC_ALL,  $this->profile['locale']);
         $this->nntp = new nntp($this->host);
+        if (!$this->nntp || !$this->nntp->valid) {
+            $this->nntp = null;
+        }
     }
 
-    function run($class = 'Banana')
+    /** Run Banana
+     * @param STRING class Name of the class to use
+     * @param ARRAY  myget If defined is used instead of get
+     */
+    function run($class = 'Banana', $myget = null)
     {
         global $banana;
 
         Banana::_require('misc');
         $banana = new $class();
 
+        if (is_null($myget)) {
+            $banana->get = $_GET;
+        } else {
+            $banana->get = $myget;
+        }
+
+
         if (!$banana->nntp) {
             return '<p class="error">'._b_('Impossible de contacter le serveur').'</p>';
         }
 
-        $group  = empty($_GET['group']) ? null : strtolower($_GET['group']);
-        $artid  = empty($_GET['artid']) ? null : strtolower($_GET['artid']);
-        $partid = !isset($_GET['part']) ? -1 : $_GET['part'];
+        $group  = empty($banana->get['group']) ? null : $banana->get['group'];
+        $artid  = empty($banana->get['artid']) ? null : strtolower($banana->get['artid']);
+        $partid = !isset($banana->get['part']) ? -1 : $banana->get['part'];
         $banana->state = Array ('group' => $group, 'artid' => $artid);
 
         if (is_null($group)) {
-
-            if (isset($_GET['subscribe'])) {
+            if (isset($banana->get['subscribe'])) {
                 return $banana->action_listSubs();
-            } elseif (isset($_POST['subscribe'])) {
+            } elseif (isset($_POST['validsubs'])) {
                 $banana->action_saveSubs();
             }
             return $banana->action_listGroups();
@@ -84,27 +132,29 @@ class Banana
         } elseif (is_null($artid)) {
             if (isset($_POST['action']) && $_POST['action'] == 'new') {
                 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
-            } elseif (isset($_GET['action']) && $_GET['action'] == 'new') {
+            } elseif (isset($banana->get['action']) && $banana->get['action'] == 'new') {
                 return $banana->action_newFup($group);
             } else {
-                return $banana->action_showThread($group, isset($_GET['first']) ? intval($_GET['first']) : 1);
+                return $banana->action_showThread($group, isset($banana->get['first']) ? intval($banana->get['first']) : 1);
             }
 
         } else {
-
             if (isset($_POST['action']) && $_POST['action']=='cancel') {
                 $res = $banana->action_cancelArticle($group, $artid);
             } else {
                 $res = '';
             }
 
-            if (isset($_GET['action'])) {
-                switch ($_GET['action']) {
+            if (isset($banana->get['action'])) {
+                switch ($banana->get['action']) {
                     case 'cancel':
                         $res .= $banana->action_showArticle($group, $artid, $partid);
                         if ($banana->post->checkcancel()) {
                             $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
-                                  . "<form action=\"?group=$group&amp;artid=$artid\" method='post'><p>"
+                                  . '<form action="' 
+                                  . htmlentities(makeLink(Array('group' => $group,
+                                                                'artid' => $artid)))
+                                  . '" method="post"><p>'
                                   . '<input type="hidden" name="action" value="cancel" />'
                                   . '<input type="submit" value="Annuler !" />'
                                   . '</p></form>';
@@ -117,12 +167,16 @@ class Banana
                 }
             }
 
-            if (isset($_GET['pj'])) {
+            if (isset($banana->get['pj'])) {
                 $action = false;
-                if (isset($_GET['action']) && $_GET['action'] == 'view') {
+                if (isset($banana->get['action']) && $banana->get['action'] == 'view') {
                     $action = true;
                 }
-                return $banana->action_getAttachment($group, $artid, $_GET['pj'], $action);
+                $att = $banana->action_getAttachment($group, $artid, $banana->get['pj'], $action);
+                if ($att != "") {
+                    return $res.$att;
+                }
+                return "";
             }
             
             return $res . $banana->action_showArticle($group, $artid, $partid);
@@ -167,7 +221,9 @@ class Banana
 
     function action_showThread($group, $first)
     {
-        $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
+        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
+            return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
+        }
 
         if ($first > count($this->spool->overview)) {
             $first = count($this->spool->overview);
@@ -187,9 +243,11 @@ class Banana
 
     function action_showArticle($group, $id, $part)
     {
-        $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
-        $this->_newPost($id);
-        if (!$this->post) {
+        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
+            return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
+        }
+
+        if (!$this->_newPost($id)) {
             if ($this->nntp->lasterrorcode == "423") {
                 $this->spool->delid($id);
             }
@@ -208,9 +266,11 @@ class Banana
 
     function action_getAttachment($group, $id, $pjid, $action)
     {
-        $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
-        $this->_newPost($id);
-        if (!$this->post) {
+        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
+            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
+        }
+
+        if (!$this->_newPost($id)) {
             if ($this->nntp->lasterrorcode == "423") {
                 $this->spool->delid($id);
             }
@@ -228,8 +288,13 @@ class Banana
 
     function action_cancelArticle($group, $id)
     {
-        $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
-        $this->_newPost($id);
+        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
+            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
+        }
+
+        if (!$this->_newPost($id)) {
+            return '<p class="error">'._b_('Impossible de trouver le message à annuler').'</p>';
+        }
         $mid  = array_search($id, $this->spool->ids);
 
         if (!$this->post->checkcancel()) {
@@ -245,7 +310,8 @@ class Banana
         if ($this->nntp->post($msg)) {
             $this->spool->delid($id);
             $this->nntp->quit();
-            header("Location: ?group=$group&amp;first=$id");
+            redirectInBanana(Array('group' => $group,
+                           'first' => $id));
         } else {
             return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
         }
@@ -258,8 +324,7 @@ class Banana
         
         if ($id > 0) {
             $this->nntp->group($group);
-            $this->_newPost($id);
-            if ($this->post) {
+            if ($this->_newPost($id)) {
                 $subject = preg_replace("/^re\s*:\s*/i", '', 'Re: '.$this->post->headers['subject']);
                 $body    = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->get_body(), "> ");
                 $target  = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
@@ -270,7 +335,9 @@ class Banana
 
         $cuts  = displayshortcuts();
         $html  = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
-        $html .= '<form enctype="multipart/form-data" action="?group='.$group.'" method="post" accept-charset="utf-8">';
+        $html .= '<form enctype="multipart/form-data" action="'
+               . htmlentities(makeLink(Array('group' => $group)))
+               .'" method="post" accept-charset="utf-8">';
         $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
         $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
         $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
@@ -280,16 +347,18 @@ class Banana
         $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
         $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
         $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
-            .to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></tr>';
-       $html .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
-        $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxfilesize.'" />';
-        $html .= '<input type="file" name="newpj" size="40"/></td></tr>';
-           $html .= '<tr><th colspan="2">';
+              .  to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></tr>';
+        if ($this->can_attach) {
+            $html .= '<tr><th colspan="2">'._b_('Pièce jointe').'</th></tr>';
+            $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxfilesize.'" />';
+            $html .= '<input type="file" name="newpj" size="40"/></td></tr>';
+        }
+        $html .= '<tr><th colspan="2">';
         if ($id > 0) {
             $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
         }
         $html .= '<input type="hidden" name="action" value="new" />';
-       $html .= '<input type="submit" /></th></tr>';
+        $html .= '<input type="submit" value="Envoyer le message" /></th></tr>';
         $html .= '</table></form>';
 
         return $html.$cuts;
@@ -304,43 +373,82 @@ class Banana
                 $_POST[$key] = utf8_encode($_POST[$key]);
             }
         }
+       
+        $forums = preg_split('/\s*(,|;)\s*/', $_POST['newsgroups']);
+        $fup    = $_POST['followup'];
+        if (sizeof($forums) > 1) {
+            if (empty($fup)) {
+                $fup = $forums[0];
+            }
+        }
+        $to     = implode(',', $forums);
+        
+        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
+            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
+        }
         
-        $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
         $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
-        $msg  = 'From: '.$this->profile['name']."\n"
-              . "Newsgroups: ".$_POST['newsgroups']."\n"
-              . "Subject: ".headerEncode($_POST['subject'], 128)."\n"
+        $msg  = 'From: ' . $this->profile['name'] . "\n"
+              . "Newsgroups: ". $to . "\n"
+              . "Subject: " . headerEncode($_POST['subject'], 128) . "\n"
               . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
-              . (empty($_POST['followup'])    ? '' : 'Followup-To: '.$_POST['followup']."\n");
+              . (empty($fup) ? '' : 'Followup-To: ' . $fup . "\n");
 
         if ($artid != -1) {
             $this->_require('post');
             $post = new BananaPost($artid);
+            if (!$post || !$post->valid) {
+                return '<p class="error">'._b_('Impossible charger le message d\'origine').'</p>';
+            }
             $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
             $msg .= "References: $refs{$post->headers['message-id']}\n";
         }
 
-        $body = wrap($body, "", $this->wrap);
+        $body_headers  = $this->custom_plain;
+        $body          = wrap($body, "");
 
         // include attachment in the body
         $uploaded = $this->_upload('newpj');
-        if ($uploaded['error'] == 0) {
-            $this->custom = $this->custom_mp;
-            $body    = "\n--".$this->boundary."\n".$this->custom_bd."\n\n".$body."\n--".$this->boundary."\n";
-
-            $body   .= 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n";
-            $body   .= 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n\n";
-            $body   .= 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
-            $body   .= $uploaded['data'];
-            $body   .= '--'.$this->boundary.'--';
+        switch ($uploaded['error']) {
+            case UPLOAD_ERR_OK:
+                $this->custom = $this->custom_mp.$this->custom;
+                $body         = $this->_make_part($body_headers, $body);
+                $file_head    = 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n"
+                              . 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n"
+                              . 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
+                $body        .= $this->_make_part($file_head, $uploaded['data']);
+                $body        .= "\n--".$this->boundary.'--';
+                break;
+
+            case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE:
+                return '<p class="error">'._b_('Fichier trop gros pour être envoyé : ')
+                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
+
+            case UPLOAD_ERR_PARTIAL:
+                return '<p class="error">'._b_('Erreur lors de l\'upload de ')
+                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
+
+            case UPLOAD_ERR_NO_FILE:
+                return '<p class="error">'._b_('Le fichier spécifié n\'existe pas : ')
+                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
+
+            case UPLOAD_ERR_NO_TMP_DIR:
+                return '<p class="error">'._b_('Une erreur est survenue sur le serveur lors de l\'upload de ')
+                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
+
+            default:
+                $this->custom = $body_headers.$this->custom;
         }
-        #TODO:afficher les erreurs lorsque l'upload ne marche pas
 
         // finalise and post the message
         $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
 
         if ($this->nntp->post($msg)) {
-            header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
+            $dir = Array('group' => $group);
+            if ($artid != -1) {
+                $dir['first'] = $artid;
+            }
+            redirectInBanana($dir);
         } else {
             return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
         }
@@ -354,13 +462,23 @@ class Banana
         $this->_require('spool');
         if (!$this->spool || $this->spool->group != $group) {
             $this->spool = new BananaSpool($group, $disp, $since);
+            if (!$this->spool || !$this->spool->valid) {
+                $this->spool = null;
+                return false;
+            }
         }
+        return true;
     }
 
     function _newPost($id)
     {
         $this->_require('post');
         $this->post = new BananaPost($id);
+        if (!$this->post || !$this->post->valid) {
+            $this->post = null;
+            return false;
+        }
+        return true;
     }
 
     function _newGroup()
@@ -413,6 +531,12 @@ class Banana
 
         return $file;
     }
+
+    function _make_part($headers, $body)
+    {
+        return "\n--".$this->boundary."\n".$headers."\n".$body;
+    }
 }
 
+// vim:set et sw=4 sts=4 ts=4
 ?>