Index: sandbox/2.2.0.2/expressoMail1_2/inc/class.exporteml.inc.php
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/inc/class.exporteml.inc.php	(revision 4192)
+++ sandbox/2.2.0.2/expressoMail1_2/inc/class.exporteml.inc.php	(revision 4348)
@@ -15,4 +15,12 @@
 	var $folder;
 	var $mbox_stream;
+	var $tempDir;
+	
+	function ExportEml() {
+	   
+		//TODO: modificar o caminho hardcodificado '/tmp' para o definido na configuracao do expresso
+		//$this->tempDir = $GLOBALS['phpgw_info']['server']['temp_dir'];
+		$this->tempDir = '/tmp';
+	}
 	
 	function connectImap(){
@@ -59,8 +67,10 @@
  
 			//$subject = ereg_replace('/', '\'', $subject);
-			$from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
-			$to =   "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
+			$from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
+			$to =   "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
 			$subject = strtr($subject,$from,$to);
-            $file =    $subject."_".$id.".eml";
+
+			$subject = eregi_replace("[^a-zA-Z0-9_]", "_", $subject); 
+			$file = $subject."_".$id.".eml"; 
 		} else{
 			// Se mensagem for arquivada localmente, $subject (assunto da mensagem)
@@ -68,10 +78,12 @@
 
 			if($subject && $i){
-				$from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
-				$to =   "aaaaaeeeeiiiiooooouuuuc__________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
+				$from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
+				$to =   "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
 				$subject = strtr($subject,$from,$to);
-				//$subject = ereg_replace(':', '_', $subject);
-				//$subject = ereg_replace('/', '_', $subject);
-				$file = $subject."_".$i.".eml";
+
+				$subject = eregi_replace("[^a-zA-Z0-9_]", "_", $subject); 
+
+				// é necessário que a sessão faça parte do nome do arquivo para que o mesmo não venha vazio o.O 
+				$file = $subject."_".$i."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml";  
 			} else{
 				$file = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml";
@@ -124,5 +136,5 @@
 		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
 		$fileNames = "";
-		$tempDir = ini_get("session.save_path");
+		$tempDir = $this->tempDir;
 		$this->connectImap();
 		
@@ -167,7 +179,12 @@
 	// Rommel Cysne (rommel.cysne@serpro.gov.br)
 	// em 17/12/2008.
+	//  
+	// Funcao alterada para que, quando houver  
+	// apenas um arquivo a ser exportado, 
+	// não seja criado em zip 
 	function makeAll($params) {
 		// Exportacao de mensagens arquivadas localmente
-		if($params['l_msg'] == "t"){
+		if($params['l_msg'] == "t")
+		{
     		// Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao
      		// e gera arrays com os conteudos separados;
@@ -175,8 +192,18 @@
         	$array_subjects = explode('@@',$params['subjects']);
             $array_ids = explode(',', $params['msgs_to_export']);
-            $tempDir = ini_get("session.save_path");
-			// Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma;
+			$tempDir = $this->tempDir;
+			
 			include_once("class.imap_functions.inc.php");
 			$imapf = new imap_functions();
+
+			// quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip 
+			if (count($array_ids)==1) 
+			{ 
+				$sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]); 
+				$fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i); 
+				return $tempDir.'/'.$fileName; 
+			}
+
+			// Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma;
     		foreach($array_ids as $i=>$id) {
 				$sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]);
@@ -203,45 +230,67 @@
 
             return $file;
-
+		
+		} else
 		// Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso
-		} else{
-            $this-> folder = $params['folder'];
-            $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
-            $array_ids = explode(',', $params['msgs_to_export']);
-            $error = False;
-            $fileNames = "";
-            $tempDir = ini_get("session.save_path");
-            $this->connectImap();
-			for($i = 0; $i < count($array_ids); $i++) {
-			$header	 	= $this-> getHeader($array_ids[$i]);											
-			$body		= $this-> getBody($array_ids[$i]);			
-			$sEMLData 	= $this -> parseEml($header, $body);			
-			$fileName 	= $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]);
-			
-			if(!$fileName)	{
-				$error = True;					
-				break;
-			}
-			else
-				$fileNames .= "\"".$fileName."\" ";			
-			
-		}
-		imap_close($this->mbox_stream);
-		
-		
-		$nameFileZip = 'False';			
-		if($fileNames && !$error) {			
-			$nameFileZip = $this -> createFileZip($fileNames, $tempDir);
-			if($nameFileZip)			
-				$file = $tempDir.'/'.$nameFileZip;
-			else {
+		{
+			$this-> folder = $params['folder'];
+			$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
+			$array_ids = explode(',', $params['msgs_to_export']);
+			$error = False;
+			$fileNames = "";
+			$tempDir = $this->tempDir;
+			$this->connectImap();
+
+			// quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip
+			if (count($array_ids)==1)
+			{
+				$header         = $this->getHeader($array_ids[0]);                                                                                     
+				$body           = $this->getBody($array_ids[0]);                       
+				$sEMLData       = $this->parseEml($header, $body);                     
+				$fileName       = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]);
+
+				imap_close($this->mbox_stream);
+				if (!$fileName) {
+					return false;
+				} else {
+					return $tempDir.'/'.$fileName;
+				}
+			}
+
+			for($i = 0; $i < count($array_ids); $i++) 
+			{
+				$header         = $this-> getHeader($array_ids[$i]);                                                                                   
+				$body           = $this-> getBody($array_ids[$i]);                     
+				$sEMLData       = $this -> parseEml($header, $body);                   
+				$fileName       = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]);
+
+				if(!$fileName) 
+				{
+					$error = True;                                 
+					break;
+				} else {
+					$fileNames .= "\"".$fileName."\" ";                     
+				}
+			}
+			imap_close($this->mbox_stream);
+
+			$nameFileZip = 'False';                 
+			if($fileNames && !$error) 
+			{
+				$nameFileZip = $this -> createFileZip($fileNames, $tempDir);
+				if($nameFileZip) 
+				{               
+					$file = $tempDir.'/'.$nameFileZip;
+				} else {
+					$file = false;
+				}                                                               
+			}
+			else 
+			{
 				$file = false;
-			}								
-		}
-		else 
-			$file = false;
-			
-		return $file;
-        }
+			}
+
+			return $file;
+		}
     }
 
@@ -250,6 +299,5 @@
 		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
 		$id_number = $params['msgs_to_export'];
-		$tempDir = ini_get("session.save_path");
-		
+		$tempDir = $this->tempDir;
 		$this->connectImap();
 		$header	 	= $this-> getHeader($id_number);
@@ -283,5 +331,5 @@
  		$this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
  	           
- 		$tempDir = $_SESSION['phpgw_info']['server']['temp_dir'];
+ 		$tempDir = $this->tempDir;
  		                 
  		$this->connectImap(); 
@@ -471,5 +519,5 @@
 		$attachments =unserialize(rawurldecode($params['s_attachments']));
 		
-		$tempDir = ini_get("session.save_path");
+		$tempDir = $this->tempDir;
 		$tempSubDir = $_SESSION['phpgw_session']['session_id'];
 		$fileNames = '';
Index: sandbox/2.2.0.2/expressoMail1_2/inc/gotodownload.php
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/inc/gotodownload.php	(revision 3369)
+++ sandbox/2.2.0.2/expressoMail1_2/inc/gotodownload.php	(revision 4348)
@@ -8,9 +8,10 @@
 }
 require_once '../../header.inc.php';
-
+	//TODO: alterar a string /tmp logo abaixo, pelo caminho temporario configurado no expresso       
+ 	$GLOBALS['phpgw_info']['server']['download_temp_dir'] = "/tmp"; 
 
 	$msg_number = $_GET['msg_number'];
 	$idx_file = $_GET['idx_file'];
-        $newfilename = html_entity_decode(rawurldecode($_GET['newfilename']));
+	$newfilename = html_entity_decode(rawurldecode($_GET['newfilename']));
 	$msg_part = $_GET['msg_part'];
 	$msg_folder = $_GET['msg_folder'];
@@ -162,4 +163,5 @@
 		header("Expires: 0"); // set expiration time
 		header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
+		
 		if($fileContent) {
 			if($encoding == 'base64')
@@ -171,42 +173,35 @@
 		}
 		else
-
-			if (strstr($strFileName,$GLOBALS['phpgw']->session->sessionid)&&file_exists($strFileName))
+		{
+			$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))
 			{
-			    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($newFileName)."\"");
-			    readfile($strFileName);
-			}else{
-			    header("HTTP/1.1 404 Not Found");
+				//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) {
+						echo fread($fp, $i);
+						flush();
+					}
+					fclose ($fp);
+				}
+
+				//removendo pelo php, garante a suportabilidade cross-platform
+				cleanup( dirname( $strFileName ) );
 			}
-
-
-			if (preg_match("#^".ini_get('session.save_path')."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$strFileName))
+			else 
 			{
-				if ( ! preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
-					//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)
-						{
-							echo fread($fp, $i);
-							flush();
-						}
-						fclose ($fp);
-					}
-					//readfile($strFileName);
-
-					exec("rm -f ".escapeshellcmd(escapeshellarg($strFileName)));
+				if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
+					readfile($strFileName);
 				}
-				else
-					readfile($strFileName);
 			}
+		}
 	}
 ?>
Index: sandbox/2.2.0.2/expressoMail1_2/js/main.js
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/js/main.js	(revision 4312)
+++ sandbox/2.2.0.2/expressoMail1_2/js/main.js	(revision 4348)
@@ -2366,9 +2366,9 @@
 
 function export_all_selected_msgs(){
+	var msgs_to_export = this.get_selected_messages(); 
+
 	if (openTab.type[currentTab] > 1){
-		source_msg(currentTab,current_folder);
-		return;
-	}
-	var msgs_to_export = this.get_selected_messages();
+		msgs_to_export = currentTab.substr(0,(currentTab.length - 2));
+	}
 
 	var handler_export_all_selected_msgs = function(data){
@@ -2377,6 +2377,21 @@
 			write_msg(get_lang('Error compressing messages (ZIP). Contact the administrator.'));
 		}
-		else
-			download_attachments(null, null, data, null,null,'mensagens.zip');
+		else 
+		{ 
+			if (data.substr(data.length-3, 3) == "zip") { 
+				download_attachments(null, null, data, null, null,'mensagens.zip'); 
+			} else if (data.substr(data.length-3, 3) == "eml") { 
+				//extracts the file name from file path 
+				var string_start  = data.lastIndexOf("/") + 1; 
+				var string_length = data.length - string_start; 
+				var fname = data.substr(string_start, string_length); 
+				//extracts the session_id from file name 
+				var string_end  = data.lastIndexOf("_") - 5; 
+				fname = fname.substr(0, string_end).concat(".eml"); 
+				download_attachments(null, null, data, null, null, fname); 
+			} else { 
+				download_attachments(null, null, data, null, null, 'mensagens'); 
+			} 
+		} 
 	}
 
