Index: trunk/expressoMail1_2/inc/get_archive.php
===================================================================
--- trunk/expressoMail1_2/inc/get_archive.php	(revision 5069)
+++ trunk/expressoMail1_2/inc/get_archive.php	(revision 5134)
@@ -1,40 +1,3 @@
 <?php
-/**
-*
-* Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
-*
-* This program is free software; you can redistribute it and/or modify it under
-* the terms of the GNU Affero General Public License version 3 as published by
-* the Free Software Foundation with the addition of the following permission
-* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
-* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
-* WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
-*
-* This program is distributed in the hope that it will be useful, but WITHOUT
-* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
-* details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program; if not, see www.gnu.org/licenses or write to
-* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-* MA 02110-1301 USA.
-*
-* This code is based on the OpenXchange Connector and on the Prognus pSync
-* Connector both developed by the community and licensed under the GPL
-* version 2 or above as published by the Free Software Foundation.
-*
-* You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
-* 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at
-* e-mail address prognus@prognus.com.br.
-*
-*
-* @package    expressoMail
-* @license    http://www.gnu.org/copyleft/gpl.html GPL
-* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
-* @version    1.0
-* @sponsor    Caixa Econômica Federal
-*/
-
 /*
  * Definitions
@@ -52,9 +15,4 @@
 //-----------------------//
 
-/*
- * Requires
- */
-require_once 'class.attachment.inc.php';
-require_once '../../header.inc.php';
 //-----------------------//
 
@@ -62,183 +20,223 @@
  * Get variables
  */
-$newFilename = urldecode($_GET['newFilename']);
-$indexFile = $_GET['idx_file'];
+if(array_key_exists('newFilename', $_GET ) && urldecode($_GET['newFilename']) !== 'application.octet-stream')
+	$newFilename = urldecode($_GET['newFilename']);
+else
+	$newFilename = null;
+if(array_key_exists('idx_file', $_GET ))
+	$indexFile = $_GET['idx_file'];
+else
+	$indexFile = null;
+
 $indexPart = $_GET['indexPart'];
 $msgNumber = $_GET['msgNumber'];
 $msgFolder = $_GET['msgFolder'];
+if(array_key_exists('fileType', $_GET ))
+	$ContentType = $_GET['fileType'];
+else
+	$ContentType = null;
+if(array_key_exists('image', $_GET ))
+	$image = $_GET['image'];
+else
+	$image = null;
 //----------------------------------------------//
 
-
-/**
-* Método que faz o download do arquivo
-*
-* @license    http://www.gnu.org/copyleft/gpl.html GPL
-* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
-* @sponsor    Caixa Econômica Federal
-* @param  $strFileType
-* @param  $strFileName
-* @param  $newFileName
-* @param  $fileContent
-*/
-function downloadFile($strFileType, $strFileName, $newFileName, $fileContent) {
-
-        $ContentType = "application/octet-stream";
-
-        if ($strFileType == ".asf")
-                $ContentType = "video/x-ms-asf";
-        if ($strFileType == ".avi")
-                $ContentType = "video/avi";
-        if ($strFileType == ".doc")
-                $ContentType = "application/msword";
-        if ($strFileType == ".zip")
-                $ContentType = "application/zip";
-        if ($strFileType == ".xls")
-                $ContentType = "application/vnd.ms-excel";
-        if ($strFileType == ".gif")
-                $ContentType = "image/gif";
-        if ($strFileType == ".jpg" || $strFileType == "jpeg")
-                $ContentType = "image/jpeg";
-        if ($strFileType == ".wav")
-                $ContentType = "audio/wav";
-        if ($strFileType == ".mp3")
-                $ContentType = "audio/mpeg3";
-        if ($strFileType == ".mpg" || $strFileType == "mpeg")
-                $ContentType = "video/mpeg";
-        if ($strFileType == ".rtf")
-                $ContentType = "application/rtf";
-        if ($strFileType == ".htm" || $strFileType == "html")
-                $ContentType = "text/html";
-        if ($strFileType == ".xml")
-                $ContentType = "text/xml";
-        if ($strFileType == ".xsl")
-                $ContentType = "text/xsl";
-        if ($strFileType == ".css")
-                $ContentType = "text/css";
-        if ($strFileType == ".php")
-                $ContentType = "text/php";
-        if ($strFileType == ".asp")
-                $ContentType = "text/asp";
-        if ($strFileType == ".pdf")
-                $ContentType = "application/pdf";
-        if ($strFileType == ".txt")
-                $ContentType = "text/plain";
-        if ($strFileType == ".log")
-                $ContentType = "text/plain";
-        if ($strFileType == ".wmv")
-                $ContentType = "video/x-ms-wmv";
-        if ($strFileType == ".sxc")
-                $ContentType = "application/vnd.sun.xml.calc";
-        if ($strFileType == ".odt")
-                $ContentType = "application/vnd.oasis.opendocument.text";
-        if ($strFileType == ".stc")
-                $ContentType = "application/vnd.sun.xml.calc.template";
-        if ($strFileType == ".sxd")
-                $ContentType = "application/vnd.sun.xml.draw";
-        if ($strFileType == ".std")
-                $ContentType = "application/vnd.sun.xml.draw.template";
-        if ($strFileType == ".sxi")
-                $ContentType = "application/vnd.sun.xml.impress";
-        if ($strFileType == ".sti")
-                $ContentType = "application/vnd.sun.xml.impress.template";
-        if ($strFileType == ".sxm")
-                $ContentType = "application/vnd.sun.xml.math";
-        if ($strFileType == ".sxw")
-                $ContentType = "application/vnd.sun.xml.writer";
-        if ($strFileType == ".sxq")
-                $ContentType = "application/vnd.sun.xml.writer.global";
-        if ($strFileType == ".stw")
-                $ContentType = "application/vnd.sun.xml.writer.template";
-        if ($strFileType == ".ps")
-                $ContentType = "application/postscript";
-        if ($strFileType == ".pps")
-                $ContentType = "application/vnd.ms-powerpoint";
-        if ($strFileType == ".odt")
-                $ContentType = "application/vnd.oasis.opendocument.text";
-        if ($strFileType == ".ott")
-                $ContentType = "application/vnd.oasis.opendocument.text-template";
-        if ($strFileType == ".oth")
-                $ContentType = "application/vnd.oasis.opendocument.text-web";
-        if ($strFileType == ".odm")
-                $ContentType = "application/vnd.oasis.opendocument.text-master";
-        if ($strFileType == ".odg")
-                $ContentType = "application/vnd.oasis.opendocument.graphics";
-        if ($strFileType == ".otg")
-                $ContentType = "application/vnd.oasis.opendocument.graphics-template";
-        if ($strFileType == ".odp")
-                $ContentType = "application/vnd.oasis.opendocument.presentation";
-        if ($strFileType == ".otp")
-                $ContentType = "application/vnd.oasis.opendocument.presentation-template";
-        if ($strFileType == ".ods")
-                $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
-        if ($strFileType == ".ots")
-                $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
-        if ($strFileType == ".odc")
-                $ContentType = "application/vnd.oasis.opendocument.chart";
-        if ($strFileType == ".odf")
-                $ContentType = "application/vnd.oasis.opendocument.formula";
-        if ($strFileType == ".odi")
-                $ContentType = "application/vnd.oasis.opendocument.image";
-        if ($strFileType == ".ndl")
-                $ContentType = "application/vnd.lotus-notes";
-        if ($strFileType == ".eml")
-                $ContentType = "text/plain";
-
-        header ("Content-Type: $ContentType");
-        header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
-        header("Pragma: public");
-        header("Expires: 0"); // set expiration time
-        header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
-
-        if($fileContent)
+/*
+ * Requires
+ */
+if( $indexFile )
+    require_once '../../header.inc.php';
+else
+    require_once '../../header.session.inc.php';
+
+
+/*
+ * Functions
+ */
+
+function unhtmlentities($string)
+{
+    $string = utf8_encode($string);
+    
+    static $trans_tbl;
+ 
+    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
+    $string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);
+
+    if (!isset($trans_tbl))
         {
-          echo $fileContent;
+        $trans_tbl = array();
+       
+        foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
+            $trans_tbl[$key] = utf8_encode($val);
         }
-        else
-        {
-                $tempDir = $GLOBALS['phpgw_info']['server']['download_temp_dir'];
-                if (preg_match("#^".$tempDir."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z0-9_]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$strFileName))
-                {
-                //avoid stuck request
-                        session_write_close();
-
-                        //reset time limit for big files
-                        set_time_limit(0);
-                        ob_end_flush();
-
-                        if ($fp = fopen ($strFileName, 'rb'))
-                        {
-                                $bufferSize=1024;
-                                for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize)
+    return strtr($string, $trans_tbl);
+}
+
+function code2utf($num)
+{
+    if ($num < 128) return chr($num);
+    if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
+    if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
+    if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
+    return '';
+}
+
+//-----------------------//
+
+/*
+ * Main
+ */
+
+
+if($msgNumber != 'null' && $indexPart !== null && $msgFolder)
+{
+    require_once $_SESSION['rootPath'].'/expressoMail1_2/inc/class.attachment.inc.php';
+
+    $attachment = new attachment();
+    $attachment->setStructureFromMail($msgFolder, $msgNumber);
+    $fileContent = $attachment->getAttachment($indexPart);
+    $info = $attachment->getAttachmentInfo($indexPart);
+    $filename = $newFilename ? $newFilename : $info['name'];       
+    $filename = unhtmlentities($filename);
+}
+else
+{
+    if( $image && $_SESSION['phpgw_info']['expressomail']['contact_photo'] )
                                 {
-                                        echo fread($fp, $i);
-                                        flush();
-                                }
-                                fclose ($fp);
-                        }
-
-                        //removendo pelo php, garante a suportabilidade cross-platform
-                        cleanup( dirname( $strFileName ) );
-
+	$image = "thumbnail";
+
+	$photo = array( 'width' => 60, 'height' => 80, 'quality' => 100 );
+
+	$fileContent = $_SESSION['phpgw_info']['expressomail']['contact_photo'][0];
+	unset( $_SESSION['phpgw_info']['expressomail']['contact_photo'] );
                 }
+
+    $filename = $indexFile;
+}
+
+$filename = $filename 	? $filename 	: "attachment.bin";
+$newFilename = $newFilename ? $newFilename 	: $filename;
+$newFilename = unhtmlentities($newFilename);
+$disposition = $image ? "inline" : "attachment; filename=\"". addslashes($newFilename)."\"";
+
+if( !$ContentType || $ContentType == 'application/octet-stream')
+{
+    
+    if( strstr($_SERVER['HTTP_USER_AGENT'],'MSIE') && $disposition != 'inline' )
+        $ContentType = 'application-download';
                 else
                 {
-                        if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
-                                readfile($strFileName);
+        function guessContentType( $fileName )
+        {
+            $strFileType = strtolower(substr ( $fileName , strrpos($fileName, '.') ));
+                         
+	    switch( $strFileType )
+	    {
+		case ".asf": return "video/x-ms-asf";
+		case ".avi": return "video/avi";
+		case ".doc": return "application/msword";
+		case ".zip": return "application/zip";
+		case ".xls": return "application/vnd.ms-excel";
+		case ".gif": return "image/gif";
+                case ".bmp": return "image/bmp";
+		case ".jpeg":
+		case ".jpg": return "image/jpeg";
+		case ".wav": return "audio/wav";
+		case ".mp3": return "audio/mpeg3";
+		case ".mpeg":
+		case ".mpg": return "video/mpeg";
+		case ".rtf": return "application/rtf";
+		case ".html":
+		case ".htm": return "text/html";
+		case ".xml": return "text/xml";
+		case ".xsl": return "text/xsl";
+		case ".css": return "text/css";
+		case ".php": return "text/php";
+		case ".asp": return "text/asp";
+		case ".pdf": return "application/pdf";
+		case ".png": return "image/png";
+		case ".txt": return "text/plain";
+		case ".log": return "text/plain";
+		case ".wmv": return "video/x-ms-wmv";
+		case ".sxc": return "application/vnd.sun.xml.calc";
+		case ".odt": return "application/vnd.oasis.opendocument.text";
+		case ".stc": return "application/vnd.sun.xml.calc.template";
+		case ".sxd": return "application/vnd.sun.xml.draw";
+		case ".std": return "application/vnd.sun.xml.draw.template";
+		case ".sxi": return "application/vnd.sun.xml.impress";
+		case ".sti": return "application/vnd.sun.xml.impress.template";
+		case ".sxm": return "application/vnd.sun.xml.math";
+		case ".sxw": return "application/vnd.sun.xml.writer";
+		case ".sxq": return "application/vnd.sun.xml.writer.global";
+		case ".stw": return "application/vnd.sun.xml.writer.template";
+		case ".pps": return "application/vnd.ms-powerpoint";
+		case ".odt": return "application/vnd.oasis.opendocument.text";
+		case ".ott": return "application/vnd.oasis.opendocument.text-template";
+		case ".oth": return "application/vnd.oasis.opendocument.text-web";
+		case ".odm": return "application/vnd.oasis.opendocument.text-master";
+		case ".odg": return "application/vnd.oasis.opendocument.graphics";
+		case ".otg": return "application/vnd.oasis.opendocument.graphics-template";
+		case ".odp": return "application/vnd.oasis.opendocument.presentation";
+		case ".otp": return "application/vnd.oasis.opendocument.presentation-template";
+		case ".ods": return "application/vnd.oasis.opendocument.spreadsheet";
+		case ".ots": return "application/vnd.oasis.opendocument.spreadsheet-template";
+		case ".odc": return "application/vnd.oasis.opendocument.chart";
+		case ".odf": return "application/vnd.oasis.opendocument.formula";
+		case ".odi": return "application/vnd.oasis.opendocument.image";
+		case ".ndl": return "application/vnd.lotus-notes";
+		case ".eml": return "text/plain";
+		case ".ps" : return "application/postscript";
+		default    : return "application/octet-stream";
                         }
                 }
+
+        $ContentType = guessContentType( $filename );
         }
 }
 
-
-/**
-* Método que limpa o diretório
-*
-* @license    http://www.gnu.org/copyleft/gpl.html GPL
-* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
-* @sponsor    Caixa Econômica Federal
-* @param  $dir
-*/
-function cleanup( $dir )
-{
+header("Content-Type: $ContentType");
+header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
+header("Pragma: public");
+header("Expires: 0"); // set expiration time
+header("Content-Disposition: $disposition");
+
+if( $fileContent )
+{
+    header("Content-transfer-encoding: ".$info["encoding"] );
+
+    if( $image === "thumbnail" )
+    {
+	$pic = imagecreatefromstring( $fileContent );
+
+	if( $pic )
+	{
+	    $width = imagesx($pic);
+	    $height = imagesy($pic);
+
+	    if(!isset($photo))
+			$photo = array( 'width' => 160, 'height' => max( ( 160 * $height / $width ), 1 ) );
+
+	    $thumb = imagecreatetruecolor( $photo['width'], $photo['height'] );
+
+	    if(array_key_exists('quality', $photo)  && $photo['quality'] )
+			imagecopyresampled($thumb, $pic, 0, 0, 0, 0, $photo['width'], $photo['height'], $width, $height);
+	    else
+		imagecopyresized($thumb, $pic, 0, 0, 0, 0, $photo['width'], $photo['height'], $width, $height); # resize image into thumb
+
+	    imagejpeg( $thumb,"", 100 ); # Thumbnail as JPEG
+	}
+    }
+    else
+	echo $fileContent;
+}
+else
+{
+    /**
+    * Delete Diretorio
+    * @param string $dir 
+    */
+    function cleanup( $dir )
+    {
     if ( is_dir( $dir ) )
     {
@@ -260,40 +258,28 @@
     }
 
-}
-//-----------------------//
-
-/*
- * Main
- */
-
-
-if($msgNumber != 'null' && $indexPart !== null && $msgFolder)
-{
-
-    $attachment = new attachment(PHPGW_INCLUDE_ROOT);
-    $attachment->setStructureFromMail($msgFolder, $msgNumber);
-    $fileContent = $attachment->getAttachment($indexPart);
-
-    if($newFilename)
-        $filename = $newFilename;
+    }
+
+    $tempDir = $GLOBALS['phpgw_info']['server']['download_temp_dir'];
+
+    if (preg_match("#^".$tempDir."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z0-9_]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$filename))
+    {
+        session_write_close();
+        set_time_limit(0);
+        ob_clean();
+        flush();
+        readfile($filename);
+
+        //removendo pelo php, garante a suportabilidade cross-platform
+	cleanup(  $filename  );
+
+    }
     else
     {
-        $info = $attachment->getAttachmentInfo($indexPart);
-        $filename = $info['name'];
-    }
-
-
-}
-else
-    $filename = $indexFile;
-
-
-$filename 	 = $filename 	? $filename 	: "attachment.bin";
-$newFilename = $newFilename ? $newFilename 	: $filename;
-
-
-$strFileType = strrev(substr(strrev(strtolower($filename)),0,4));
-
-downloadFile($strFileType,  $filename, $newFilename, $fileContent)
+	    if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$filename)) {
+		    readfile($filename);
+	    }
+    }
+}
+
 //-----------------------//
 
