Index: trunk/expressoMail1_2/inc/class.exporteml.inc.php
===================================================================
--- trunk/expressoMail1_2/inc/class.exporteml.inc.php	(revision 5079)
+++ trunk/expressoMail1_2/inc/class.exporteml.inc.php	(revision 5134)
@@ -17,12 +17,4 @@
 	var $tempDir;
 	
-	
-	/**
-	* Construtor da classe ExportEml
-	*
-	* @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
-	*/
 	function ExportEml() {
 	   
@@ -303,19 +295,17 @@
     }
 
+    function export_eml( $params ){
+
+	return $this->export_msg_data( $params['msgs_to_export'],
+				       $params['folder'] );
+    }
+
 	function export_msg($params) {
-		$this-> folder = $params['folder'];
-		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
-		$id_number = $params['msgs_to_export'];
 		$tempDir = $this->tempDir;
-		$this->connectImap();
-		$header	 	= $this-> getHeader($id_number);
-		$body		= $this-> getBody($id_number);
-		
 		$file = "source_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".php";
 		$f = fopen($tempDir.'/'.$file,"w");
-		fputs($f,$header ."\r\n\r\n". $body);
+		fputs($f, $this->export_msg_data( $params['msgs_to_export'], $params['folder'] ) );
 		fclose($f);
 		
-		imap_close($this->mbox_stream);
 		return $tempDir.'/'.$file;
 	}
@@ -336,7 +326,6 @@
 
 		function export_to_archive($id_msg,$folder) {
-		$this-> folder = $folder; 
+		$this->folder = $folder;
  		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
- 	           
  		$tempDir = $this->tempDir;
  		                 
@@ -344,74 +333,17 @@
  		$header         = $this-> getHeader($id_msg); 
  		$body           = $this-> getBody($id_msg); 
- 		$name = md5($_SESSION[ 'phpgw_session' ][ 'session_id' ].microtime());
  		
-		//$file = $_SESSION[ 'phpgw_session' ][ 'session_id' ].$name;
- 		$file = "source_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]."_".time().".php"; //source_[sessao]_[timestamp].php 
-		$f = fopen($tempDir . '/'.$file,"w");
- 		fputs($f,utf8_encode($header) ."\r\n\r\n". utf8_encode($body));
+		$file = tempnam ($tempDir, 'source_#'.$id_msg);
+		$file .= '.php';
+		$fileName = basename ($file);
+		$f = fopen($file, "w");
+		fputs($f,$phpheader.$header ."\r\n\r\n". $body);
  		fclose($f); 
+		$urlPath = 'tmpLclAtt/' . $fileName;
  		                 
  		imap_close($this->mbox_stream); 
 		return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt";
+        }
  		                 
-        }
-
-	function get_attachments_in_array($params) {
-		$return_attachments = array();
-
-		$id_number = $params['num_msg'];		
-		$attachments =unserialize(rawurldecode($params['s_attachments']));
-		
-		$tempDir = dirname( __FILE__ ) . '/../tmpLclAtt';
-		$tempSubDir = md5(microtime());
-		exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
-		
-		$this-> folder = $params['folder'];
-		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
-		$this->connectImap();
-
-		include_once("class.imap_attachment.inc.php");
-
-		$imap_attachment = new imap_attachment();
-		$attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
-		foreach($attachments as $i => $attachment){
-			if($i && $i == 'names')
-				continue;
-			$fileNameReal = $this->remove_accents($attachment['name']);
-			$ContentType = $this->getFileType($fileNameReal);
-			$fileName = $fileNameReal . ".php";
-			$f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
-			if(!$f)
-				return $tempDir . '/'.$tempSubDir.'/'.$fileName;			
-
-			$fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID);
-			$urlPath = 'tmpLclAtt/'.$tempSubDir.'/'.$fileName;
-			
-			$headers = "<?php header('Content-Type: ".$ContentType."');
-				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
-				header('Pragma: public');
-				header('Expires: 0'); // set expiration time
-	 	 		header('Content-Disposition: attachment; filename=\"". addslashes($fileNameReal) ."\"');\n echo ";
-			
-			if($attachment['encoding'] == 'base64') {
-				$headers.=" imap_base64('".$fileContent."');?>";
-			}
-			else if($attachment['encoding'] == 'quoted_printable_decode') {
-				$headers.=" quoted_printable_decode('".$fileContent."');?>";
-			}
-			else {
-				$headers.=" '".$fileContent."';?>";
-			}
-			
-			fputs($f,$headers);
-
-			array_push($return_attachments,array('name' => $fileName,'url' => $urlPath,'pid' =>$attachment['pid']));
-			fclose($f);
-		}
-		imap_close($this->mbox_stream);
-		return $return_attachments;
-        }
-
-
 	function remove_accents($string) {
 		/*
@@ -423,7 +355,52 @@
 			"áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ",
 			"aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC");
-	}
-	
-
+        }
+
+	function get_attachments_headers( $folder, $id_number ){
+
+	    $this->folder = mb_convert_encoding($folder, "UTF7-IMAP","UTF-8");
+		
+	    $return_attachments = array();
+		
+	    include_once("class.attachment.inc.php");
+
+	    $imap_attachment = new attachment();
+	    $imap_attachment->setStructureFromMail( $folder, $id_number );
+	    $attachments = $imap_attachment->getAttachmentsInfo();
+
+		foreach($attachments as $i => $attachment){
+
+		    $fileContent = $imap_attachment->getAttachment( $attachment['pid'] );
+			
+		    $headers = "<?php header('Content-Type: {$attachment['type']}');
+				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
+				header('Pragma: public');
+				header('Expires: 0'); // set expiration time
+				      header('Content-Disposition: attachment; filename=\"{$attachment['name']}\"');\n
+				      echo '$fileContent';?>";
+			
+		    $return_attachments[ $attachment['name'] ] = array( "content" => $headers, "pid" => $attachment['pid'] );
+			}
+
+	    return( $return_attachments );
+			}
+			
+	function get_attachments_in_array($params) {
+		$return_attachments = array();
+
+		$attachments = $this->get_attachments_headers( $params['folder'], $params['num_msg'] );
+
+		if( !empty( $attachments ) )
+		{
+		    foreach($attachments as $fileNameReal => $attachment){
+
+			    array_push($return_attachments,array('name' => $fileNameReal, 'pid' =>$attachment['pid'], 'contentType' => $this->getFileType( $fileNameReal  ) ));
+		}
+        }
+
+		return $return_attachments;
+
+	}
+	
 	private function getFileType($nameFile) {
 		$strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4));
@@ -529,4 +506,6 @@
 	   	if ($strFileType == ".eml")
 	   		$ContentType = "text/plain";
+		if ($strFileType == ".png")
+			$ContentType = "image/png";
 		return $ContentType;
 	}
@@ -534,7 +513,9 @@
 	function download_all_attachments($params) {
 		
+		require_once $_SESSION['rootPath'].'/expressoMail1_2/inc/class.attachment.inc.php';
+		$atObj = new attachment();
+		$atObj->setStructureFromMail($params['folder'],$params['num_msg']);
+		$attachments = $atObj->getAttachmentsInfo();
 		$id_number = $params['num_msg'];		
-		$attachments =unserialize(rawurldecode($params['s_attachments']));
-		
 		$tempDir = $this->tempDir;
 		$tempSubDir = $_SESSION['phpgw_session']['session_id'];
@@ -543,8 +524,4 @@
 		$this-> folder = $params['folder'];
 		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
-		$this->connectImap();
-		include("class.imap_attachment.inc.php");
-		$imap_attachment = new imap_attachment();
-		$attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
 		
 		$fileNames = Array();
@@ -583,9 +560,5 @@
 			if(!$f)
 				return False;			
-						
-			$fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachments[$i]['pid'], FT_UID);
-			if($attachments[$i]['encoding'] == 'base64')
-				fputs($f,imap_base64($fileContent));
-			else		
+			$fileContent = $atObj->getAttachment( $attachments[$i]['pid'] );	
 				fputs($f,$fileContent);
 				
