Index: /trunk/controller.php
===================================================================
--- /trunk/controller.php	(revision 2459)
+++ /trunk/controller.php	(revision 2480)
@@ -9,5 +9,6 @@
         *  Free Software Foundation; either version 2 of the License, or (at your  *
         *  option) any later version.                                              *
-        \**************************************************************************/
+	\**************************************************************************/
+sleep(3);
 	if(!isset($GLOBALS['phpgw_info'])){
         	$GLOBALS['phpgw_info']['flags'] = array(
@@ -104,4 +105,7 @@
 		echo serialize($result);
 	else
+	{
+		@session_start();
 		$_SESSION['response'] = $result;
+	}
 ?>
Index: /trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 2450)
+++ /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 2480)
@@ -29,5 +29,7 @@
 	var $ldap;
 	var $mbox;
-	var $imap_port;
+	var $email_server;
+	var $cert;
+	var $preferences;
 	var $has_cid;
 	var $imap_options = '';
@@ -38,14 +40,31 @@
 	function imap_functions (){
 		$this->foldersLimit = $_SESSION['phpgw_info']['expressomail']['max_folders_to_show']; //Limit of folders (mailboxes) user can see
-	 	$this->username 	  = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
-		$this->password 	  = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
-		$this->imap_server	  = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
-		$this->imap_port	  = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
-		$this->imap_delimiter = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDelimiter'];
+		$this->email_server = $_SESSION['phpgw_info']['expressomail']['email_server'];
+		$this->cert = $_SESSION['phpgw_info']['expressomail']['server']['certificado'];
+		$this->server_files_dir = $_SESSION['phpgw_info']['expressomail']['server']['files_dir'];
+
+		$this->username  = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
+		$this->password  = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
+		$this->account_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];
+		$this->email = $_SESSION['phpgw_info']['expressomail']['user']['email'];
+		$this->fullname = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
+		$this->lang = $_SESSION['phpgw_info']['expressomail']['user']['preferences']['common']['lang'];
+		$this->session_ip = $_SESSION['phpgw_info']['expressomail']['user']['session_ip'];
+		$this->shared_mailboxes = $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'];
+
+		$this->command_for_ham = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_ham'];
+		$this->command_for_spam = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_spam'];
+		$this->enable_log_messages = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'];
+
+		$this->preferences = $_SESSION['phpgw_info']['user']['preferences']['expressoMail'];
+		$this->session_id = $_SESSION[ 'phpgw_session' ][ 'session_id' ];
+
+		session_write_close();
+
 		$this->functions	  = new functions();
-		$this->imap_sentfolder = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->getLang("Sent"));
+		$this->imap_sentfolder = $this->email_server['imapDefaultSentFolder']  ? $this->email_server['imapDefaultSentFolder'] : str_replace("*","", $this->functions->getLang("Sent"));
 		$this->has_cid = false;
 
-		if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
+		if ($this->email_server['imapTLSEncryption'] == 'yes')
 		{
 			$this->imap_options = '/tls/novalidate-cert';
@@ -62,5 +81,5 @@
 			return $this->mbox;
 		$folder = mb_convert_encoding($folder, "UTF7-IMAP","ISO_8859-1");
-		$this->mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
+		$this->mbox = @imap_open("{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
 		return $this->mbox;
 	 }
@@ -73,5 +92,5 @@
 		// This error is returned from Postfix.
 		elseif(strstr($error,'message file too big')) {
-			return str_replace("%1",$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['max_attachment_size'],$this->functions->getLang('The size of this message has exceeded  the limit (%1B).'));
+			return str_replace("%1",$this->preferences->max_attachment_size,$this->functions->getLang('The size of this message has exceeded  the limit (%1B).'));
 		}
 		elseif(strstr($error,'virus')) {
@@ -79,5 +98,5 @@
 		}
 		// This condition verifies if SESSION is expired.
-		elseif(!count($_SESSION))
+		elseif(!count( $this->email_server ))
 			return "nosession";
 
@@ -87,7 +106,4 @@
 	function get_range_msgs2($params)
 	{
-		// Free others requests
-		session_write_close();
-
 		$folder = $params['folder'];
 		$msg_range_begin = $params['msg_range_begin'];
@@ -251,8 +267,8 @@
 		{
 			// PLEASE, BE CAREFULL!!! YOU SHOULD USE EMAIL CONFIGURATION VALUES (EMAILADMIN MODULE)
-			$tmp_box = mb_convert_encoding('INBOX'.$this->imap_delimiter.$_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder'].$this->imap_delimiter.'tmpMoveToLocal', "UTF7-IMAP", "UTF-8");
+			$tmp_box = mb_convert_encoding('INBOX'.$this->email_server['imapDelimiter'].$this->email_server['imapDefaultTrashFolder'].$this->email_server['imapDelimiter'].'tmpMoveToLocal', "UTF7-IMAP", "UTF-8");
 			if ( ! imap_createmailbox( $this -> mbox,"{".$this -> imap_server."}$tmp_box" ) )
 				return $this->functions->getLang( 'Import to Local : fail...' );
-			imap_reopen($this->mbox, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$tmp_box);
+			imap_reopen($this->mbox, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$tmp_box);
 			$params["folder"] = $tmp_box;
 		}
@@ -275,5 +291,5 @@
 						$email = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
 						$status = @imap_append($this->mbox,
-								"{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"],
+								"{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$params["folder"],
 									$email
 									);
@@ -303,5 +319,5 @@
 			$email = implode("",file($params['FILES'][0]['tmp_name']));
 			$status = @imap_append($this->mbox,
-								"{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"],
+								"{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$params["folder"],
 									$email
 									);
@@ -360,5 +376,5 @@
 
 		$status = imap_append($this->mbox,
-				"{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$params["folder"],
+				"{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$params["folder"],
 					$header.
 					"\r\n".
@@ -376,5 +392,5 @@
 		else
 		{
-			$status = imap_status($this->mbox, "{".$this->imap_server.":".$this->imap_port."}".$params['folder'], SA_UIDNEXT);
+			$status = imap_status($this->mbox, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort']."}".$params['folder'], SA_UIDNEXT);
 			$return['msg_no'] = $status->uidnext - 1;
 			imap_delete($this->mbox, imap_msgno($this->mbox, $params["msg_num"]));
@@ -734,5 +750,5 @@
 		//All this is to help in local messages
 		$return['timestamp'] = $header->udate_original;
-		$return['login'] = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];//$GLOBALS['phpgw_info']['user']['account_id'];
+		$return['login'] = $this->account_id;
 		$return['reply_toaddress'] = $header->reply_toaddress;
 		return $return;
@@ -748,5 +764,5 @@
 		$return = "";
 
-		if( (isset($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['preview_msg_subject']) && ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['preview_msg_subject'] == "0")) && (isset($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['preview_msg_tip']) && ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['preview_msg_tip'] == "0")) )
+		if( (isset($this->preferences->preview_msg_subject) && ($this->preferences->preview_msg_subject == "0")) && (isset($this->preferences->preview_msg_tip) && ($this->preferences->preview_msg_tip == "0")) )
 		{
 			$return['body'] = "";
@@ -1451,5 +1467,5 @@
 
 		//$mbox_stream = $this->open_mbox($folder);
-	 	$mbox_stream = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
+	 	$mbox_stream = @imap_open("{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$folder, $this->username, $this->password) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
 
 		foreach ($msgs_number as $msg_number)
@@ -1470,5 +1486,4 @@
 	function refresh($params)
 	{
-		session_write_close();
 		include_once("class.imap_attachment.inc.php");
 		$imap_attachment = new imap_attachment();
@@ -1634,20 +1649,18 @@
 	{
 		$inbox = 'INBOX';
-		$trash = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder'];
-        	$drafts = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultDraftsFolder'];
-	        $spam = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSpamFolder'];
-		$sent = $inbox . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'];
-		$uid2cn = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'];
-		// Free others requests
-		session_write_close();
+		$trash = $inbox . $this->email_server['imapDelimiter ']. $this->email_server['imapDefaultTrashFolder;'];
+        	$drafts = $inbox . $this->email_server['imapDelimiter ']. $this->email_server['imapDefaultDraftsFolder'];
+	        $spam = $inbox . $this->email_server['imapDelimiter ']. $this->email_server['imapDefaultSpamFolder'];
+		$sent = $inbox . $this->email_server['imapDelimiter ']. $this->email_server['imapDefaultSentFolder'];
+		$uid2cn = $this->preferences->uid2cn;
 
 		if(!$this->mbox || !is_resource($this->mbox))
 			$this->mbox = $this->open_mbox();
 
-		if($params && $params['onload'] && $_SESSION['phpgw_info']['expressomail']['server']['certificado']){
+		if($params && $params['onload'] && $this->cert){
 			$this->delete_mailbox(array("del_past" => "INBOX/decifradas"));
 		}
 
-		$serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}";
+		$serverString = "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}";
 		$folders_list = imap_getmailboxes($this->mbox, $serverString, ($params && $params['noSharedFolders']) ? "INBOX/*" : "*");
 		$folders_list = array_slice($folders_list,0,$this->foldersLimit);
@@ -1667,7 +1680,7 @@
   				//$tmp_folder_id = explode("}", imap_utf7_decode($val->name));
   				$tmp_folder_id = explode("}", mb_convert_encoding($val->name, "ISO_8859-1", "UTF7-IMAP" ));
-    			        if($tmp_folder_id[1]=='INBOX'.$this->imap_delimiter.'decifradas') {
+    			        if($tmp_folder_id[1]=='INBOX'.$this->email_server['imapDelimiter'].'decifradas') {
 		        	        //error_log('passou', 3,'/tmp/imap_get_list.log');
-                			//imap_deletemailbox($this->mbox, imap_utf7_encode("{".$this->imap_server."}".'INBOX/decifradas'));
+                			//imap_deletemailbox($this->mbox, imap_utf7_encode("{".$this->email_server['imapServer']."}".'INBOX/decifradas'));
 		                	continue;
 		                }
@@ -1676,5 +1689,5 @@
 				$result[$i]['folder_id'] = $folder_id;
 
-				$tmp_folder_parent = explode($this->imap_delimiter, $folder_id);
+				$tmp_folder_parent = explode($this->email_server['imapDelimiter'], $folder_id);
 				$result[$i]['folder_name'] = array_pop($tmp_folder_parent);
 				$result[$i]['folder_name'] = $result[$i]['folder_name'] == 'INBOX' ? 'Inbox' : $result[$i]['folder_name'];
@@ -1686,5 +1699,5 @@
 				}
 
-				$tmp_folder_parent = implode($this->imap_delimiter, $tmp_folder_parent);
+				$tmp_folder_parent = implode($this->email_server['imapDelimiter'], $tmp_folder_parent);
 				$result[$i]['folder_parent'] = $tmp_folder_parent == 'INBOX' ? '' : $tmp_folder_parent;
 
@@ -1759,5 +1772,5 @@
 		$namebox	= $arr['newp'];
 		$mbox_stream = $this->open_mbox();
-		$imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
+		$imap_server = $this->email_server['imapServer'];
 		$namebox =  mb_convert_encoding($namebox, "UTF7-IMAP", "UTF-8");
 
@@ -1780,5 +1793,5 @@
 		$result = "";
 		$mbox_stream = $this->open_mbox();
-		$imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
+		$imap_server = $this->email_server['imapServer'];
 		foreach($nameboxs as $key=>$tmp){
 			if($tmp != ""){
@@ -1799,5 +1812,5 @@
 	{
 		$namebox = $arr['del_past'];
-		$imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
+		$imap_server = $this->email_server['imapServer'];
 		$mbox_stream = $this->mbox ? $this->mbox : $this->open_mbox();
 		//$del_folder = imap_deletemailbox($mbox_stream,"{".$imap_server."}INBOX.$namebox");
@@ -1820,5 +1833,5 @@
 		$namebox = $arr['current'];
 		$new_box = $arr['rename'];
-		$imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
+		$imap_server = $this->email_server['imapServer'];
 		$mbox_stream = $this->open_mbox();
 		//$ren_folder = imap_renamemailbox($mbox_stream,"{".$imap_server."}INBOX.$namebox","{".$imap_server."}INBOX.$new_box");
@@ -1860,5 +1873,5 @@
 	function folder_exists($folder){
 		$mbox =  $this->open_mbox();
-		$serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}";		
+		$serverString = "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}";		
 		$list = imap_getmailboxes($mbox,$serverString, $folder);
 		$return = is_array($list);		
@@ -1872,5 +1885,5 @@
 		$mail = new PHPMailer();
 		// Set lang for PHPMailer using user prefs.
-		list($prefix,$userlang) = explode('-',$_SESSION['phpgw_info']['expressomail']['user']['preferences']['common']['lang']);
+		list($prefix,$userlang) = explode('-',$this->lang);
 		$mail->SetLanguage($userlang,dirname( __FILE__ )."/../setup/");
 		include_once("class.db_functions.inc.php");
@@ -1882,10 +1895,10 @@
 		# @BRIEF Checks if the user has permission to send an email with the email address used.
 		##
-		if ( is_array($fromaddress) && ($fromaddress[1] != $_SESSION['phpgw_info']['expressomail']['user']['email']) )
+		if ( is_array($fromaddress) && ($fromaddress[1] != $this->email) )
 		{
 			$deny = true;
-			foreach( $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] as $key => $val )
+			foreach( $this->shared_mailboxes as $key => $val )
 				if ( array_key_exists('mail', $val) && $val['mail'][0] == $fromaddress[1] )
-					$deny = false and end($_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes']);
+					$deny = false and end($this->shared_mailboxes);
 
 			if ( $deny )
@@ -1951,5 +1964,5 @@
 		if( $fromaddress[2] == 'y' ){
 			//build shared folder path
-			$newfolder = "user".$this->imap_delimiter.$fromaddress[3].$this->imap_delimiter.$this->imap_sentfolder;
+			$newfolder = "user".$this->email_server['imapDelimiter'].$fromaddress[3].$this->email_server['imapDelimiter'].$this->imap_sentfolder;
 			if( $this->folder_exists($newfolder) ) $folder = $newfolder;
 			else $folder =  $params['folder'];  			
@@ -1962,6 +1975,6 @@
 		// Fix problem with cyrus delimiter changes.
 		// Dots in names: enabled/disabled.
-		$folder = @eregi_replace("INBOX/", "INBOX".$this->imap_delimiter, $folder);
-		$folder = @eregi_replace("INBOX.", "INBOX".$this->imap_delimiter, $folder);
+		$folder = @eregi_replace("INBOX/", "INBOX".$this->email_server['imapDelimiter'], $folder);
+		$folder = @eregi_replace("INBOX.", "INBOX".$this->email_server['imapDelimiter'], $folder);
 		// End Fix.
 		if ($folder != 'null'){
@@ -1979,8 +1992,8 @@
             $mail->IsSMTP();
 
-		$mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer'];
-		$mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort'];
-		$mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email'];
-		$mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
+		$mail->Host = $this->email_server['smtpServer'];
+		$mail->Port = $this->email_server['smtpPort'];
+		$mail->From = $this->email;
+		$mail->FromName = $this->fullname;
 		if($fromaddress){
 			$mail->Sender = $mail->From;
@@ -2006,5 +2019,5 @@
             // Se uma das verificacoes falhar, nao enviar o e-mail e avisar o usuario.
             // O array $mail->Certs_crypt soh deve ser preenchido se os certificados passarem nas verificacoes.
-            $numero_maximo = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['num_max_certs_to_cipher'];  // Este valor dever ser configurado pelo administrador do site ....
+            $numero_maximo = $this->preferences->num_max_certs_to_cipher;  // Este valor dever ser configurado pelo administrador do site ....
             $erros_acumulados = "";
             $aux_mails = array();
@@ -2016,5 +2029,5 @@
             }
             // adiciona o email do remetente. eh para cifrar a msg para ele tambem. Assim vai poder visualizar a msg na pasta enviados..
-            $email[] = $_SESSION['phpgw_info']['expressomail']['user']['email'];
+            $email[] = $this->email;
             foreach($email as $item)
             {
@@ -2126,5 +2139,5 @@
 				}
 				$tempDir = ini_get("session.save_path");
-				$file = "cidimage_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].$cid_imgs[6][$j].".dat";
+				$file = "cidimage_".$this->session_id.$cid_imgs[6][$j].".dat";
 				$f = fopen($tempDir.'/'.$file,"w");
 				fputs($f,$fileContent);
@@ -2141,5 +2154,5 @@
 		{
 			$total_uploaded_size = 0;
-			$upload_max_filesize = str_replace("M","",$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['max_attachment_size']) * 1024 * 1024;
+			$upload_max_filesize = str_replace("M","",$this->preferences->max_attachment_size) * 1024 * 1024;
 			foreach ($attachments as $attach)
 			{
@@ -2200,5 +2213,5 @@
 		// Disposition-Notification-To
 		if ($return_receipt)
-			$mail->ConfirmReadingTo = $_SESSION['phpgw_info']['expressomail']['user']['email'];
+			$mail->ConfirmReadingTo = $this->email;
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -2215,8 +2228,8 @@
 				return $sent;
 			}
-			if($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True")
+			if($this->enable_log_messages == "True")
 			{
-				$userid = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
-				$userip = $_SESSION['phpgw_info']['expressomail']['user']['session_ip'];
+				$userid = $this->username;
+				$userip = $this->session_ip;
 				$now = date("d/m/y H:i:s");
 				$addrs = $toaddress.$ccaddress.$ccoaddress;
@@ -2224,6 +2237,6 @@
    				error_log("$now - $userip - $sent [$subject] - $userid => $addrs\r\n", 3, "/home/expressolivre/mail_senders.log");
 			}
-			if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] &&
-		   	   $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_dynamic_contacts']) {
+			if($this->preferences->number_of_contacts &&
+		   	   $this->preferences->use_dynamic_contacts) {
 				$contacts = new dynamic_contacts();
 				$new_contacts = $contacts->add_dynamic_contacts($toaddress.",".$ccaddress.",".$ccoaddress);
@@ -2369,5 +2382,5 @@
 
 				if($sort_box_type=='SORTFROM') {
-					if (($header->from[0]->mailbox . "@" . $header->from[0]->host) == $_SESSION['phpgw_info']['expressomail']['user']['email'])
+					if (($header->from[0]->mailbox . "@" . $header->from[0]->host) == $this->email)
 						$from = $header->to;
 					else
@@ -2457,9 +2470,9 @@
 						'status' => true); //Status foi adicionado para validar as permissoes ACL
 
-		$error_log_file=$_SESSION['phpgw_info']['expressomail']['server']['files_dir']."/expressoMail.log";
-		if($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True" && 
+		$error_log_file=$this->server_files_dir."/expressoMail.log";
+		if($this->enable_log_messages == "True" && 
 		strpos($msgs_number,',') != False)
 			error_log(date("D M j G:i:s T Y").": expressoMail, move_messages: "
-				.$_SESSION['phpgw_info']['expressomail']['user']['userid']." ".$folder."=>".$new_folder_name.
+				.$this->username." ".$folder."=>".$new_folder_name.
 				" ".$msgs_number."\n", 3, $error_log_file);
 
@@ -2484,8 +2497,8 @@
         }
         //Este bloco tem a finalidade de transformar o CPF das pastas compartilhadas em common name
-        if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn']){
+        if ($this->preferences->uid2cn){
             if (substr($new_folder_name,0,4) == 'user'){
                 $this->ldap = new ldap_functions();
-                $tmp_folder_name = explode($this->imap_delimiter, $new_folder_name);
+                $tmp_folder_name = explode($this->email_server['imapDelimiter'], $new_folder_name);
                 $return['new_folder_name'] = array_pop($tmp_folder_name);
                 if( $cn = $this->ldap->uid2cn($return['new_folder_name']))
@@ -2512,18 +2525,18 @@
 		}else {
 			if(strstr(imap_last_error(),'Over quota')) {
-				$accountID	= $_SESSION['phpgw_info']['expressomail']['email_server']['imapAdminUsername'];
-				$pass		= $_SESSION['phpgw_info']['expressomail']['email_server']['imapAdminPW'];
-				$userID 	= $_SESSION['phpgw_info']['expressomail']['user']['userid'];
-				$server 	= $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
-				$mbox		= @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}INBOX", $accountID, $pass) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
+				$accountID	= $this->email_server['imapAdminUsername'];
+				$pass		= $this->email_server['imapAdminPW'];
+				$userID 	= $this->username;
+				$server 	= $this->email_server['imapServer'];
+				$mbox		= @imap_open("{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}INBOX", $accountID, $pass) or die(serialize(array('imap_error' => $this->parse_error(imap_last_error()))));
 				if(!$mbox)
 					return imap_last_error();
 				$quota 	= imap_get_quotaroot($mbox_stream, "INBOX");
-				if(! imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, 2.1 * $quota['usage'])) {
+				if(! imap_set_quota($mbox, "user".$this->email_server['imapDelimiter'].$userID, 2.1 * $quota['usage'])) {
 					if($mbox_stream)
 						imap_close($mbox_stream);
 					if($mbox)
 						imap_close($mbox);
-					return "move_messages(): Error setting quota for MOVE or DELETE!! ". "user".$this->imap_delimiter.$userID." line ".__LINE__."\n";
+					return "move_messages(): Error setting quota for MOVE or DELETE!! ". "user".$this->email_server['imapDelimiter'].$userID." line ".__LINE__."\n";
 				}
 				if(imap_mail_move($mbox_stream, $msgs_number, $newmailbox, CP_UID)) {
@@ -2532,5 +2545,5 @@
 						imap_close($mbox_stream);
 					// return to original quota limit.
-					if(!imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, $quota['limit'])) {
+					if(!imap_set_quota($mbox, "user".$this->email_server['imapDelimiter'].$userID, $quota['limit'])) {
 						if($mbox)
 							imap_close($mbox);
@@ -2542,5 +2555,5 @@
 					if($mbox_stream)
 						imap_close($mbox_stream);
-					if(!imap_set_quota($mbox, "user".$this->imap_delimiter.$userID, $quota['limit'])) {
+					if(!imap_set_quota($mbox, "user".$this->email_server['imapDelimiter'].$userID, $quota['limit'])) {
 						if($mbox)
 							imap_close($mbox);
@@ -2565,5 +2578,5 @@
 		$mail = new PHPMailer();
 		// Set lang for PHPMailer using user prefs.
-		list($prefix,$userlang) = explode('-',$_SESSION['phpgw_info']['expressomail']['user']['preferences']['common']['lang']);		
+		list($prefix,$userlang) = explode('-',$this->lang);		
 		$mail->SetLanguage($userlang,dirname( __FILE__ )."/../setup/");		
 		include_once("class.db_functions.inc.php");
@@ -2585,6 +2598,6 @@
 		// Fix problem with cyrus delimiter changes.
 		// Dots in names: enabled/disabled.
-		$folder = @eregi_replace("INBOX/", "INBOX".$this->imap_delimiter, $folder);
-		$folder = @eregi_replace("INBOX.", "INBOX".$this->imap_delimiter, $folder);
+		$folder = @eregi_replace("INBOX/", "INBOX".$this->email_server['imapDelimiter'], $folder);
+		$folder = @eregi_replace("INBOX.", "INBOX".$this->email_server['imapDelimiter'], $folder);
 		// End Fix.
                 if(strtoupper($folder) == 'INBOX/DRAFTS')
@@ -2596,18 +2609,15 @@
 
 		$mail->IsSMTP();
-		$mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer'];
-		$mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort'];
-		$mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email'];
-		$mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
+		$mail->Host = $this->email_server['smtpServer'];
+		$mail->Port = $this->email_server['smtpPort'];
+		$mail->From = $this->email;
+		$mail->FromName = $this->fullname;
 
 		$mail->Sender = $mail->From;
 		$mail->SenderName = $mail->FromName;
-		$mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
-		$mail->From =  $_SESSION['phpgw_info']['expressomail']['user']['email'];
-		$session_id = $_SESSION[ 'phpgw_session' ][ 'session_id' ];
-		$max_resolution = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['image_size'];
-
-		// Free others requests 
-		session_write_close(); 
+		$mail->FromName = $this->fullname;
+		$mail->From =  $this->email;
+		$session_id = $this->session_id;
+		$max_resolution = $this->preferences->image_size;
 
 		$this->add_recipients("to", $toaddress, &$mail);
@@ -2764,6 +2774,6 @@
 		$new_header = str_replace("\n", "\r\n", $header);
 		$new_body = str_replace("\n", "\r\n", $body);
-		$return['append'] = imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $new_header . $new_body, "\\Seen \\Draft");
-		$status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
+		$return['append'] = imap_append($mbox_stream, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort']."}".$folder, $new_header . $new_body, "\\Seen \\Draft");
+		$status = imap_status($mbox_stream, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort']."}".$folder, SA_UIDNEXT);
 		$return['msg_no'] = $status->uidnext - 1;
 		$return['folder_id'] = $folder;
@@ -2790,5 +2800,4 @@
 			$return['append'] = imap_last_error();
 	
-		session_start();
 		return $return;
 	}
@@ -2826,5 +2835,5 @@
 					imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number))
 					,$importance);
-				if(strtolower($importance[1])=="high" && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) {
+				if(strtolower($importance[1])=="high" && $this->preferences->use_important_flag) {
 					$flag_importance=true;
 				}
@@ -2842,5 +2851,5 @@
 			}
 
-			if($flag_importance && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) {
+			if($flag_importance && $this->preferences->use_important_flag) {
 				$return["status"] = false;
 				$return["msg"] = $this->functions->getLang("At least one of selected message cant be marked as normal");
@@ -3034,7 +3043,7 @@
 	function get_quota($params){
 		// folder_id = user/{uid} for shared folders
-		if(substr($params['folder_id'],0,5) != 'INBOX' && preg_match('/user\\'.$this->imap_delimiter.'/i', $params['folder_id'])){
-			$array_folder =  explode($this->imap_delimiter,$params['folder_id']);
-			$folder_id = "user".$this->imap_delimiter.$array_folder[1];
+		if(substr($params['folder_id'],0,5) != 'INBOX' && preg_match('/user\\'.$this->email_server['imapDelimiter'].'/i', $params['folder_id'])){
+			$array_folder =  explode($this->email_server['imapDelimiter'],$params['folder_id']);
+			$folder_id = "user".$this->email_server['imapDelimiter'].$array_folder[1];
 		}
 		// folder_id = INBOX for inbox folders
@@ -3083,5 +3092,5 @@
 		$mail = new PHPMailer();
 		// Set lang for PHPMailer using user prefs.
-		list($prefix,$userlang) = explode('-',$_SESSION['phpgw_info']['expressomail']['user']['preferences']['common']['lang']);		
+		list($prefix,$userlang) = explode('-',$this->lang);		
 		$mail->SetLanguage($userlang,dirname( __FILE__ )."/../setup/");
 
@@ -3090,11 +3099,11 @@
 		$subject = 'Confirmação de leitura: ' . $params['subject'];
 		$body = 'Sua mensagem: ' . $params['subject'] . '<br>';
-		$body .= 'foi lida por: ' . $_SESSION['phpgw_info']['expressomail']['user']['fullname'] . ' &lt;' . $_SESSION['phpgw_info']['expressomail']['user']['email'] . '&gt; em ' . date("d/m/Y H:i");
+		$body .= 'foi lida por: ' . $this->fullname . ' &lt;' . $this->email . '&gt; em ' . date("d/m/Y H:i");
 		$mail->SMTPDebug = false;
 		$mail->IsSMTP();
-		$mail->Host = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpServer'];
-		$mail->Port = $_SESSION['phpgw_info']['expressomail']['email_server']['smtpPort'];
-		$mail->From = $_SESSION['phpgw_info']['expressomail']['user']['email'];
-		$mail->FromName = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
+		$mail->Host = $this->email_server['smtpServer'];
+		$mail->Port = $this->email_server['smtpPort'];
+		$mail->From = $this->email;
+		$mail->FromName = $this->fullname;
 		$mail->AddAddress($toaddress);
 		$mail->Subject = $this->htmlspecialchars_decode($subject);
@@ -3112,5 +3121,5 @@
 	function empty_folder($params)
 	{
-		$folder = 'INBOX' . $this->imap_delimiter . $_SESSION['phpgw_info']['expressomail']['email_server'][$params['clean_folder']];
+		$folder = 'INBOX' . $this->email_server['imapDelimiter ']. $this->email_server[$params['clean_folder']];
 		$mbox_stream = $this->open_mbox($folder);
 		$return = imap_delete($mbox_stream,'1:*');
@@ -3267,12 +3276,7 @@
 		$before_date = date("m/d/Y", strtotime("-".$params['before_date']." day"));
 		$criteria =  'BEFORE "'.$before_date.'"';
-		$trash_folder = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultTrashFolder'];
-		// Free others requests
-		session_write_close();
-
-		$mbox_stream = $this->open_mbox('INBOX'.$this->imap_delimiter.$trash_folder);
-		
-		// Free others requests
-		session_write_close();
+		$trash_folder = $this->email_server['imapDefaultTrashFolder'];
+
+		$mbox_stream = $this->open_mbox('INBOX'.$this->email_server['imapDelimiter'].$trash_folder);
 
 		$messages = imap_search($mbox_stream, $criteria, SE_UID);
@@ -3330,5 +3334,5 @@
             if($search){
                 $search_criteria = '';
-                $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number'];
+                $search_result_number = $this->preferences->search_result_number;
                 foreach($search as $tmp)
                 {
@@ -3360,7 +3364,7 @@
                     $filter = $this->remove_accents($filter);
                     //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name
-                    if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['uid2cn'] && substr($name_box,0,4) == 'user')
+                    if ($this->preferences->uid2cn && substr($name_box,0,4) == 'user')
                     {
-                        $folder_name = explode($this->imap_delimiter,$name_box);
+                        $folder_name = explode($this->email_server['imapDelimiter'],$name_box);
                         $this->ldap = new ldap_functions();
                         if ($cn = $this->ldap->uid2cn($folder_name[1]))
@@ -3368,5 +3372,5 @@
                             $folder_name[1] = $cn;
                         }
-                        $folder_name = implode($this->imap_delimiter,$folder_name);
+                        $folder_name = implode($this->email_server['imapDelimiter'],$folder_name);
                     }
                     else
@@ -3381,5 +3385,5 @@
                     else
                         {
-                            imap_reopen($mbox_stream, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$name_box);
+                            imap_reopen($mbox_stream, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$name_box);
                         }
 
@@ -3545,6 +3549,6 @@
 		$mbox_stream = $this->open_mbox();
 
-		$serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}";
-		$mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->imap_delimiter.$this->username."*");
+		$serverString = "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}";
+		$mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->email_server['imapDelimiter'].$this->username."*");
 
 		/*if (count($add_share))
@@ -3604,9 +3608,9 @@
 		$return = array();
 		$mbox_stream = $this->open_mbox();
-		//Alterado, antes era 'imap_getacl($mbox_stream, 'user'.$this->imap_delimiter.$user);
+		//Alterado, antes era 'imap_getacl($mbox_stream, 'user'.$this->email_server['imapDelimiter'].$user);
 		//Afim de tratar as pastas compartilhadas, verificandos as permissoes de operacao sobre as mesmas
 		//No caso de se tratar da caixa do proprio usuario logado, utiliza a sintaxe abaixo
 		if(substr($user,0,4) != 'user')
-		$mbox_acl = imap_getacl($mbox_stream, 'user'.$this->imap_delimiter.$user);
+		$mbox_acl = imap_getacl($mbox_stream, 'user'.$this->email_server['imapDelimiter'].$user);
 		else
 		  $mbox_acl = imap_getacl($mbox_stream, $user);
@@ -3622,6 +3626,6 @@
 		$mbox_stream = $this->open_mbox();
 
-		$serverString = "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}";
-		$mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->imap_delimiter.$this->username."*");
+		$serverString = "{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}";
+		$mailboxes_list = imap_getmailboxes($mbox_stream, $serverString, "user".$this->email_server['imapDelimiter'].$this->username."*");
 
 		if (is_array($mailboxes_list))
@@ -3680,5 +3684,5 @@
 			$body = imap_body($mbox_stream, $imap_msg_number);
 			$msg = $header . $body;
-			$email = $_SESSION['phpgw_info']['expressomail']['user']['email'];
+			$email = $this->email;
 			$username = $this->username;
 			strtok($email, '@');
@@ -3699,6 +3703,6 @@
 			// Seleciona qual comando a ser executado
 			switch($is_spam){
-				case 'true':  $cmd = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_spam']; break;
-				case 'false': $cmd = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_command_for_ham']; break;
+				case 'true':  $cmd = $this->command_for_spam; break;
+				case 'false': $cmd = $this->command_for_ham; break;
 			}
 
@@ -3719,5 +3723,5 @@
  		$header->udate +=  $pdate['zone']*(-60);
 
-		if($header->Flagged != "F" && $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag']) {
+		if($header->Flagged != "F" && $this->preferences->use_important_flag) {
 			$flag = preg_match('/importance *: *(.*)\r/i',
 						imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number))
@@ -3733,8 +3737,8 @@
 
     function insert_email($source,$folder,$timestamp){
-        $username = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
-        $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
-        $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
-        $imap_port 	= $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
+        $username = $this->username;
+        $password = $this->password;
+        $imap_server = $this->email_server['imapServer'];
+        $imap_port 	= $this->email_server['imapPort'];
         $imap_options = '/notls/novalidate-cert';
         $mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$folder, $username, $password);
@@ -3745,5 +3749,5 @@
         if($timestamp){
             $tempDir = ini_get("session.save_path");
-            $file = $tempDir."imap_".$_SESSION[ 'phpgw_session' ][ 'session_id' ];
+            $file = $tempDir."imap_".$this->session_id;
     		$f = fopen($file,"w");
         	fputs($f,base64_encode($source));
@@ -3754,5 +3758,5 @@
             $return['append'] = imap_append($mbox_stream, "{".$imap_server.":".$imap_port."}".$folder, $source, "\\Seen");
         }
-        $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
+        $status = imap_status($mbox_stream, "{".$this->email_server['imapServer'].":".$this->email_server['imapPort']."}".$folder, SA_UIDNEXT);
         $return['msg_no'] = $status->uidnext - 1;
 		$return['error'] = imap_last_error();
@@ -3778,8 +3782,8 @@
         }
 
-        $insert = $this->insert_email($source,'INBOX'.$this->imap_delimiter.'decifradas');
+        $insert = $this->insert_email($source,'INBOX'.$this->email_server['imapDelimiter'].'decifradas');
 
 		$get['msg_number'] = $insert['msg_no'];
-		$get['msg_folder'] = 'INBOX'.$this->imap_delimiter.'decifradas';
+		$get['msg_folder'] = 'INBOX'.$this->email_server['imapDelimiter'].'decifradas';
 		$return = $this->get_info_msg($get);
 		$get['msg_number'] = $params['ID'];
@@ -3857,8 +3861,8 @@
         $source = $params['source'];
         $source = $this->treat_base64_from_post($source);
-        $insert = $this->insert_email($source,'INBOX'.$this->imap_delimiter.'decifradas');
+        $insert = $this->insert_email($source,'INBOX'.$this->email_server['imapDelimiter'].'decifradas');
         $exporteml = new ExportEml();
         $params['num_msg']=$insert['msg_no'];
-        $params['folder']='INBOX'.$this->imap_delimiter.'decifradas';
+        $params['folder']='INBOX'.$this->email_server['imapDelimiter'].'decifradas';
         return $exporteml->download_all_attachments($params);
     }
@@ -3870,5 +3874,5 @@
 		$imapfp = new imapfp();
 		
-	    if(!$imapfp->open($this->imap_server,$this->imap_port))
+	    if(!$imapfp->open($this->email_server['imapServer'],$this->email_server['imapPort']))
 	        return $imapfp->get_error();	        
 		if (!$imapfp->login( $this->username,$this->password ))
@@ -3896,9 +3900,9 @@
 				$quota_used = $size." b";
 			$folder = mb_convert_encoding($folder, "ISO_8859-1", "UTF7-IMAP");
-			if(!preg_match('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i',$folder)){
+			if(!preg_match('/user\\'.$this->email_server['imapDelimiter'].$this->username.'\\'.$this->email_server['imapDelimiter'].'/i',$folder)){
 				$folder = $this->functions->getLang("Inbox");
 			}
 			else
-				$folder = preg_replace('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i','', $folder);
+				$folder = preg_replace('/user\\'.$this->email_server['imapDelimiter'].$this->username.'\\'.$this->email_server['imapDelimiter'].'/i','', $folder);
 			
 			$data[$folder] = array("quota_percent" => sprintf("%.1f",round($quotaPercent,1)), "quota_used" => $quota_used);
