From: x2003bruneau Date: Tue, 28 Feb 2006 08:21:14 +0000 (+0000) Subject: * Meilleure gestion de l'upload des fichiers X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=d28aa62dec47863abe29ac3ce76e729d324b7468;p=banana.git * Meilleure gestion de l'upload des fichiers * Limitation de la taille des fichiers uploadable aisément paramétrable * Améliore la lisibilité du code git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@6 9869982d-c50d-0410-be91-f2a2ec7c7c7b --- diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in index 7aa348c..d79bb04 100644 --- a/banana/banana.inc.php.in +++ b/banana/banana.inc.php.in @@ -9,31 +9,32 @@ class Banana { - var $maxspool = 3000; + 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', + var $hdecode = array('from','name','organization','subject'); + var $parse_hdr = array('content-disposition', 'content-transfer-encoding', 'content-type', 'date', 'followup-to', 'from', 'message-id', 'newsgroups', 'organization', 'references', 'subject', 'x-face'); - var $show_hdr = array('from', 'subject', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face'); + var $show_hdr = array('from', 'subject', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face'); - var $tbefore = 5; - var $tafter = 5; - var $tmax = 50; + var $tbefore = 5; + var $tafter = 5; + var $tmax = 50; - var $wrap = 74; + var $wrap = 74; - 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"; + 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"; - var $host = 'news://localhost:119/'; + var $host = 'news://localhost:119/'; - var $profile = Array( 'name' => 'Anonymous ', 'sig' => '', 'org' => '', + var $profile = Array( 'name' => 'Anonymous ', 'sig' => '', 'org' => '', 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array()); - var $state = Array('group' => null, 'artid' => null); + var $state = Array('group' => null, 'artid' => null); var $nntp; var $groups; var $newgroups; @@ -272,7 +273,7 @@ class Banana $html .= ''; $html .= ''._b_('Pièces jointes').''; - $html .= ''; + $html .= ''; $html .= ''; $html .= ''; if ($id > 0) { @@ -313,31 +314,18 @@ class Banana $body = wrap($body, "", $this->wrap); // include attachment in the body - if (isset($_FILES['newpj'])) { + $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"; - $tmpname = $_FILES['newpj']['tmp_name']; - $file = basename($_FILES['newpj']['name']); - $mime = shell_exec("file -bi $tmpname"); //Because mime_content_type don't work :( - if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) { - $mime = $format[1]; - $encod = $format[2]; - } else { - preg_match("@([^ ]+/[^ ]+)\r?\n@", $mime, $format); - $mime = $format[1]; - $encod = 'base64'; - } - - $body .= 'Content-Type: '.$mime.'; name="'.$file."\"\n"; - $body .= 'Content-Disposition: attachment; filename="'.$file."\"\n"; - $body .= 'Content-Transfer-Encoding: '.$encod."\n\n"; - if ($encod == 'base64') { - $body .= chunk_split(base64_encode(fread(fopen($tmpname, 'r'), filesize($tmpname)))); - } else { - $body .= fread(fopen($tmpname, 'r'), filesize($tmpname)); - } + + $body .= 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n"; + $body .= 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n"; + $body .= 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n\n"; + $body .= $uploaded['data']; $body .= '--'.$this->boundary.'--'; - } + } + #TODO:afficher les erreurs lorsque l'upload ne marche pas // finalise and post the message $msg .= $this->custom.$this->profile['customhdr']."\n".$body; @@ -379,6 +367,43 @@ class Banana { require_once (dirname(__FILE__).'/'.$file.'.inc.php'); } + + function _upload($file) + { + if ($_FILES[$file]['name'] == "") { + return Array( 'error' => -1 ); + } + + // upload + $_FILES[$file]['tmp_name']; + + // test if upload is ok + $file = $_FILES[$file]; + if ($file['size'] == 0 || $file['error'] != 0) { + if ($file['error'] == 0) { + $file['error'] = -1; + } + return $file; + } + + // adding custum data + $mime = rtrim(shell_exec('file -bi '.$file['tmp_name'])); //Because mime_content_type don't work :( + $encod = 'base64'; + if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) { + $mime = $format[1]; + $encod = $format[2]; + } + $data = fread(fopen($file['tmp_name'], 'r'), $file['size']); + if ($encod == 'base64') { + $data = chunk_split(base64_encode($data)); + } + $file['name'] = basename($file['name']); + $file['type'] = $mime; + $file['encoding'] = $encod; + $file['data'] = $data; + + return $file; + } } ?>