Index: /trunk/expressoAdmin1_2/inc/class.user.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 1521)
+++ /trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 1913)
@@ -1034,5 +1034,5 @@
 					
 					//IMAP
-					$result_imap = $this->imap_functions->delete_user($user_info['uid']);
+					$result_imap = $this->imap_functions->delete_mailbox($user_info['uid']);
 					if (!$result_imap['status'])
 					{
Index: /trunk/expressoAdmin1_2/inc/class.uishared_accounts.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.uishared_accounts.inc.php	(revision 1913)
+++ /trunk/expressoAdmin1_2/inc/class.uishared_accounts.inc.php	(revision 1913)
@@ -0,0 +1,121 @@
+<?php
+	/***********************************************************************************\
+	* Expresso Administraï¿œï¿œo															*
+	* by Joao Alfredo Knopik Junior (joao.alfredo@gmail.com, jakjr@celepar.pr.gov.br)  	*
+	* modified by Valmir Andre de Sena valmirse@gmail.com valmir.sena@ati.pe.gov.br
+	* ----------------------------------------------------------------------------------*
+	*  This program is free software; you can redistribute it and/or modify it			*
+	*  under the terms of the GNU General Public License as published by the			*
+	*  Free Software Foundation; either version 2 of the License, or (at your			*
+	*  option) any later version.														*
+	\***********************************************************************************/
+
+	define('PHPGW_API_INC','../phpgwapi/inc');
+
+	class uishared_accounts
+	{
+		var $public_functions = array
+		(
+			'index' => True
+		);
+		
+		var $functions;
+                var $current_config;
+
+		function uishared_accounts()
+		{			
+			if (function_exists('CreateObject'))
+			{
+				$this->functions = CreateObject('expressoAdmin1_2.functions');
+				
+				if(!@is_object($GLOBALS['phpgw']->js))
+				{
+					$GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
+				}
+				$GLOBALS['phpgw']->js->validate_file('jscode','connector','expressoAdmin1_2');#diretorio, arquivo.js, aplicacao
+				$GLOBALS['phpgw']->js->validate_file('jscode','shared_accounts','expressoAdmin1_2');
+				$GLOBALS['phpgw']->js->validate_file('modal','modal','expressoAdmin1_2');
+				$GLOBALS['phpgw']->js->validate_file('jscode','expressoadmin','expressoAdmin1_2');
+                                $c = CreateObject('phpgwapi.config','expressoAdmin1_2');
+                                $c->read_repository();
+                                $this->current_config = $c->config_data;
+			}
+		}
+
+		function index()
+		{
+			/* Begin:  Check manager access */
+			$account_lid = $GLOBALS['phpgw']->accounts->data['account_lid'];
+			$acl = $this->functions->read_acl($account_lid);
+			$contexts = $acl['contexts'];
+                        
+			foreach ($acl['contexts_display'] as $index=>$tmp_context) {
+				$context_display .= '<br>'.$tmp_context;
+			}
+			
+			if (!$this->functions->check_acl($account_lid,'list_shared_accounts')) {
+				$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/expressoAdmin1_2/inc/access_denied.php'));
+			}
+			/* End: Check manager access */
+
+			unset($GLOBALS['phpgw_info']['flags']['noheader']);
+			unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
+			$GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['expressoAdmin1_2']['title'].' - '.lang('shared accounts');
+			$GLOBALS['phpgw']->common->phpgw_header();
+
+			$p = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
+
+			$p->set_file(
+						Array(
+								'shared_accounts'		=> 'shared_accounts.tpl',
+								'shared_accounts_modal'	=>'shared_accounts_modal.tpl'
+							)
+						);
+			$p->set_block('shared_accounts','body');
+			
+			/* dinamic load lang */                        
+			$p->set_var($this->functions->make_dinamic_lang($p, 'body'));
+			$p->set_var($this->functions->make_dinamic_lang($p, 'shared_accounts_modal'));
+
+
+			/* Begin: set modal */
+
+			$functions = CreateObject('expressoAdmin1_2.functions');
+			
+			//$organizations = $functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context']);
+			
+			foreach ($contexts as $index=>$context)
+				$combo_manager_org .= $this->functions->get_organizations($context);
+			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context']);
+			
+			$p->set_var('manager_organizations', $combo_manager_org);
+			$p->set_var('all_organizations', $combo_all_orgs);
+			
+			$modal_id = 'shared_accounts_modal';
+			$p->set_var('modal_id', $modal_id);
+                        
+                        $var = Array(
+                            'mailquota' =>  $this->current_config['expressoAdmin_defaultUserQuota'],
+                            'changequote_disabled' => $this->functions->check_acl($account_lid,'edit_shared_accounts_quote') ? '' : 'readonly',
+                            'disabled_empty_inbox' => $this->functions->check_acl($account_lid,'empty_shared_accounts_inbox') ? '' : 'disabled',
+                            'display_quota_used' => 'none'
+                        );
+                        $p->set_var($var);
+			$shared_accounts_modal_tpl = $p->fp('out','shared_accounts_modal');
+                        /* End: set modal */
+			
+			$var = Array(
+				'th_bg'						=> $GLOBALS['phpgw_info']['theme']['th_bg'],
+				'back_url'					=> $GLOBALS['phpgw']->link('/expressoAdmin1_2/index.php'),
+				'context_display'			=> $context_display,
+				'shared_accounts_modal' => $shared_accounts_modal_tpl,                                
+				'onclick_create_shared_account' => "modal(\"$modal_id\",\"create\")"
+			);
+			$p->set_var($var);
+			$p->pfp('out','body');
+		}
+	}
+
+
+	
+?>
Index: /trunk/expressoAdmin1_2/inc/class.shared_accounts.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.shared_accounts.inc.php	(revision 1913)
+++ /trunk/expressoAdmin1_2/inc/class.shared_accounts.inc.php	(revision 1913)
@@ -0,0 +1,147 @@
+<?php
+	/***********************************************************************************\
+	* Expresso Administraï¿œï¿œo															*
+	* by Valmir André de Sena (valmirse@gmail.com, valmir.sena@ati.pe.gov.br
+	* ----------------------------------------------------------------------------------*
+	*  This program is free software; you can redistribute it and/or modify it			*
+	*  under the terms of the GNU General Public License as published by the			*
+	*  Free Software Foundation; either version 2 of the License, or (at your			*
+	*  option) any later version.														*
+	\***********************************************************************************/
+
+include_once('class.functions.inc.php');
+include_once('class.ldap_functions.inc.php');
+include_once('class.imap_functions.inc.php');
+
+	class shared_accounts
+	{
+		var $functions;
+		var $ldap_functions;
+		var $imap_functions;
+
+		function shared_accounts()
+		{			
+			
+			$this->ldap_functions = new ldap_functions;			
+			$this->imap_functions = new imap_functions;
+			$this->functions = new functions;
+			
+		}
+		function create($params)
+		{
+			$params['uid'] =  $this->get_shared_mail2uid($params);
+                        $return = $this->ldap_functions->create_shared_accounts($params);
+						
+			if( $return['status'] ){
+				//Create mailbox				
+				$mailquota = 10;
+				$return = $this->imap_functions->create($params['uid'], $params['mailquota']);
+                                $owners_acl_new = unserialize($params['owners_acl']); 
+                                //add new users e set permissions
+                                foreach($owners_acl_new as $user => $acl){
+                                   $result &= $this->imap_functions->setaclfrombox($user,$acl,$params['uid']);
+                                }
+			} 		
+			return $return;
+		}
+	
+		function save($params)
+		{
+			
+                        $params['uid'] = $this->get_shared_mail2uid($params);
+                        $params['old_uid'] = $this->get_shared_dn2uid($params['anchor']);
+                        $result = $this->ldap_functions->save_shared_accounts($params);
+
+                        if( $result['status']){
+                            $result = $this->imap_functions->save_shared_account($params);                            
+                        }
+
+                      return  $result;
+		}
+		
+		function get($params)
+		{
+                        if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_institutional_accounts'))
+                        {
+                                $return['status'] = false;
+                                $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
+                                return $return;
+                        }
+
+                        $input = $params['input'];
+                        $justthese = array("cn", "mail", "uid");
+                        $trs = array();
+
+                        foreach ($this->manager_contexts as $idx=>$context)
+                        {
+                                $shared_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=s)(|(mail=$input*)(cn=*$input*)))"), $justthese);
+                                $entries = ldap_get_entries($this->ldap, $shared_accounts);
+
+                                for ($i=0; $i<$entries['count']; $i++)
+                                {
+                                        $tr = "<tr class='normal' onMouseOver=this.className='selected' onMouseOut=this.className='normal'><td onClick=edit_shared_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['cn'][0] . "</td><td onClick=edit_shared_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['mail'][0] . "</td><td align='center' onClick=delete_shared_accounts('".$entries[$i]['uid'][0]."')><img HEIGHT='16' WIDTH='16' src=./expressoAdmin1_2/templates/default/images/delete.png></td></tr>";
+                                        $trs[$tr] = $entries[$i]['cn'][0];
+                                }
+                        }
+    	
+                        $trs_string = '';
+                        if (count($trs))
+                        {
+                                natcasesort($trs);
+                                foreach ($trs as $tr=>$cn)
+                                {
+                                        $trs_string .= $tr;
+                                }
+                        }
+
+                        $return['status'] = 'true';
+                        $return['trs'] = $trs_string;
+                        return $return;
+                }
+	
+                function get_data($params)
+                {
+                        $return = $this->ldap_functions->get_shared_account_data($params);
+                        $owners_acl = $this->imap_functions->getaclfrombox($params);
+                        $quota = $this->imap_functions->get_user_info($params['uid']);
+                        $return['mailquota'] = $quota['mailquota'];
+                        $return['display_empty_inbox'] = $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'],'empty_shared_accounts_inbox') ? 'block' : 'none';
+                        $return['allow_edit_shared_account_acl'] = $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'],'edit_shared_accounts_acl');
+                        $return['mailquota_used'] = $quota['mailquota_used'];
+                        //print_r(	$owners_acl);
+                        $i = 0;
+                        foreach($owners_acl as $key => $value)
+                        {
+                                $cn = $this->ldap_functions->uid2cn($key);
+                                $return['owners_options'] .= '<option value='. $key .'>' . $cn . '</option>';
+                                $return['owners'][$i] .= $key;
+                                $return['owners_acl'][$i] .= $value;
+                                $i++;
+                        }
+                        return $return;
+                }
+                function delete($params){
+                    $result = $this->ldap_functions->delete_shared_account_data($params);
+                    if( $result['status'] ) $result = $this->imap_functions->delete_mailbox($params['uid']);
+                    return $result;
+                }
+                //Get the shared uid from mail
+                function get_shared_mail2uid($params){
+                        list($uid) = explode("@",$params['mail']);
+                        if( preg_match("/^(ou|dc)=(\w+),.*/", $params['context'], $match) ){
+                            $uid = $uid."_".$match[2];
+                        }
+                        return $uid;
+                }
+                function get_shared_dn2uid($dn){
+                        $uid = "";
+                        list($uid) = explode(",", str_replace("uid=","", $dn));
+                        return $uid;                	
+                }
+                function empty_inbox($params){
+                    $params['uid'] = $this->get_shared_dn2uid($params['uid']);
+                    return $this->imap_functions->empty_shared_account_inbox($params);
+                }
+	
+}
+?>
Index: /trunk/expressoAdmin1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.imap_functions.inc.php	(revision 1523)
+++ /trunk/expressoAdmin1_2/inc/class.imap_functions.inc.php	(revision 1913)
@@ -126,5 +126,5 @@
 	}
 	
-	function delete_user($uid)
+	function delete_mailbox($uid)
 	{
 		$result['status'] = true;
@@ -137,5 +137,5 @@
 		{
 			$result['status'] = false;
-			$result['msg'] = $this->functions->lang('it was not possible to delete users mailbox') . ".\n";
+			$result['msg'] = $this->functions->lang('it was not possible to delete mailbox') . ".\n";
 			$result['msg'] .= $this->functions->lang('Server returns') . ': ' . imap_last_error();
 		}
@@ -193,16 +193,34 @@
 		return $result;
 	}
-	
+	function empty_user_inbox($params){
+	       if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
+		{
+			$result['status'] = false;
+			$result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
+			return $result;
+		} else return $this->empty_inbox($params);
+		
+        }
+        function empty_shared_account_inbox($params){
+            if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox'))
+		{
+			$result['status'] = false;
+			$result['msg'] = $this->functions->lang('You do not have right to empty an shared account inbox');
+			return $result;
+		} else return $this->empty_inbox($params);
+        }
 	function empty_inbox($params)
 	{
 		// Verifica o acesso do gerente
-		if (!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox'))
-		{
-			$result['status'] = false;
-			$result['msg'] = $this->functions->lang('You do not have access to clean an user inbox');
-			return $result;
-		}
-		
-		if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
+		if (!($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_user_inbox') ||
+                      $this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'empty_shared_accounts_inbox')
+                  ))
+		{
+			$result['status'] = false;
+			$result['msg'] = $this->functions->lang('You do not have access to clean an inbox');
+			return $result;
+		}
+
+                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
 		{
 			$imap_options = '/tls/novalidate-cert';
@@ -241,5 +259,5 @@
 			{
 				$result['status'] = false;
-				$result['msg'] = $this->functions->lang('It was not possible clean the users inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
+				$result['msg'] = $this->functions->lang('It was not possible clean the inbox') . ".\n" . $this->functions->lang('Server returns') . ': ' . imap_last_error();
 			}
 		}
@@ -251,3 +269,124 @@
 		return $result;
 	}
+	function getaclfrombox($params)
+	{
+		$boxacl = $params['uid'];
+		
+		$return = array();	
+		
+		$mbox_acl = imap_getacl($this->imap, "user" . $this->imapDelimiter . $boxacl);
+		
+		foreach ($mbox_acl as $user => $acl)
+		{
+			if ($user != $boxacl )
+			{
+				$return[$user] = $acl;
+			}
+		}
+		return $return;
+	}
+	function setaclfrombox($user, $acl, $mailbox)
+	{
+		$serverString = '{'.$this->imap_server.':'.$this->imap_port.'/novalidate-cert}';
+		$mailboxes_list = imap_getmailboxes($this->imap, $serverString, "user".$this->imapDelimiter.$mailbox.$this->imapDelimiter."*");                
+		$result = Array();
+                $result['status'] = true;
+                if (is_array($mailboxes_list))
+		{
+                        $folder = str_replace($serverString, "", imap_utf7_encode("user".$this->imapDelimiter.$mailbox));
+                        $folder = str_replace("&-", "&", $folder);
+
+                        if (imap_setacl ($this->imap, $folder, $user, $acl) ) {
+                            foreach ($mailboxes_list as $key => $val)
+                            {
+                                    $folder = str_replace($serverString, "", imap_utf7_encode($val->name));
+                                    $folder = str_replace("&-", "&", $folder);
+                                    if (!imap_setacl ($this->imap, $folder, $user, $acl))
+                                    {
+                                            $result['status'] = false;
+                                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
+                                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
+                                            break;
+                                    }
+                            }
+                        } else {
+                            $result['status'] = false;
+                            $result['msg']  = $this->functions->lang('Error on function') . ' imap_functions->setaclfrombox: imap_setacl';
+                            $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . imap_last_error();
+                            break;
+                        }
+		}
+		return $result;
+	}
+        function save_shared_account($params){           
+            //Rename mailbox
+            $result = Array();
+            $result['status'] = true;
+            if($params['uid'] != $params['old_uid'] ){
+                    $result = $this->rename_mailbox($params['old_uid'], $params['uid']);
+                    if(!$result['status']) return $result;
+            }
+
+            //Begin edit Quota
+            $quota = $this->get_user_info($params['uid'] );
+
+            if ( $quota['mailquota'] != $params['mailquota'] ){
+                    if(!$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_quote') )
+                    {
+                                $result['status'] = false;
+                                $result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox quota');
+                                return $result;
+                    } else $this->change_user_quota($params['old_uid'], $params['mailquota']);
+            }
+            //End Edit Quota
+ 
+            /* Begin Set ACL */
+            //Old users set in the box
+            $owners_acl_old = $this->getaclfrombox($params);
+            //new settings
+            $owners_acl_new = unserialize($params['owners_acl']);
+
+            $owners_to_remove = array_diff_assoc($owners_acl_old,$owners_acl_new);
+            $owners_to_add = array_diff_assoc($owners_acl_new,$owners_acl_old);
+
+            //Find modified users
+            $tmp_update = array_diff_assoc($owners_acl_old,$owners_to_remove);
+            $owners_to_update = Array();
+            foreach($tmp_update  as $user => $acl){
+                if($owners_acl_old[$user] != $acl){
+                    $owners_to_update[$user] = $acl;
+                }
+            }
+            
+            //Check Modify manage acl
+            
+            if( (count($owners_to_remove) > 0 || count($owners_to_add) > 0 || count($owners_to_update) > 0) && !$this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_shared_accounts_acl') ){
+                $result['status'] = false;
+		$result['msg'] = $this->functions->lang('You do not have access to edit an shared account inbox acl');
+		return $result;
+            }
+            
+            if( is_array($owners_acl_new)){
+                foreach($owners_to_remove as $user => $acl){
+                    $params['user'] = $user;
+                    $params['acl'] = "none";
+                    $user = $params['user'];
+                    $result = $this->setaclfrombox($user,"none",$params['uid']);
+                }
+                //add new users
+                foreach($owners_to_add as $user => $acl){
+                    $params['user'] = $user;
+                    $params['acl'] = $acl;
+                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
+                }
+                //update users
+                 foreach($owners_to_update as $user => $acl){
+                    $params['user'] = $user;
+                    $params['acl'] = $acl;
+                    $result = $this->setaclfrombox($user,$acl,$params['uid']);
+                }               
+            }
+            /* End Set Acl */
+            return $result;
+        }
 }
Index: /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 1867)
+++ /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 1913)
@@ -45,4 +45,104 @@
 		$this->manager_contexts = $manager_acl['contexts'];
 	}
+
+	function create_shared_accounts($params)
+		{
+			/* Begin: Access verification */
+			
+			if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_shared_accounts'))
+			{
+				$return['status'] = false;
+				$return['msg'] = $this->functions->lang('You do not have right to create shared accounts') . ".";
+				return $return;
+			}
+			
+			$access_granted = false;
+			foreach ($this->manager_contexts as $idx=>$manager_context)
+			{
+				if (stristr($params['context'], $manager_context))
+				{
+					$access_granted = true;
+					break;
+				}
+			}
+			
+			if (!$access_granted)
+			{
+				$return['status'] = false;				
+				$return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";							
+				return $return;
+			}			
+			/* End: Access verification */
+	
+				
+			/* Begin: Validation */
+			if ( (empty($params['cn'])) || (empty($params['mail'])) )
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Field mail or name is empty');	return $result;
+			}
+	
+			if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
+				return $result;
+			}			                  
+			$dn = "uid=$params[uid]," . $params['context'];
+                        $filter = "(mail=".$params['mail'].")";
+			$justthese = array("cn");
+			$search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+			$entries = @ldap_get_entries($this->ldap,$search);
+			if ($entries['count'] != 0)
+			{
+				$result['status'] = false;
+				$result['msg'] = $this->functions->lang('Field mail already in use');
+				return $result;
+			}
+			/* End: Validation */
+							
+			$info = array();
+			$info['cn']					= $params['cn'];
+			$info['sn']					= $params['cn'];
+			$info['uid']				= $params['uid'];
+			$info['mail']				= $params['mail'];
+			$info['description'] = utf8_encode($params['description']);			
+			$info['phpgwAccountType']	= 's';
+			$info['objectClass'][]		= 'inetOrgPerson';
+			$info['objectClass'][]		= 'phpgwAccount';
+			$info['objectClass'][]		= 'top';
+			$info['objectClass'][]		= 'person';
+			$info['objectClass'][]		= 'qmailUser';
+			$info['objectClass'][]		= 'organizationalPerson';
+			
+			if ($params['accountStatus'] == 'on')
+			{
+				$info['accountStatus'] = 'active';
+			}
+			if ($params['phpgwAccountVisible'] == 'on')
+			{
+				$info['phpgwAccountVisible'] = '-1';
+			}
+			
+			/*if (!empty($params['owners']))
+			{
+				foreach($params['owners'] as $index=>$uidnumber)
+				{
+					$info['mailForwardingAddress'][] = $this->uidnumber2mail($uidnumber);
+				}
+			}*/
+			$result = array();
+			//print_r($info);exit();
+			if (!@ldap_add ( $this->ldap, $dn, $info ))
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->create_shared_accounts';
+				$result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
+			}
+			else{
+				$result['status'] = true;							
+			}
+			return $result;
+		}
 	
 	/* expressoAdmin: email lists : deve utilizar o ldap Host Master com o usuario e senha do CC*/
@@ -601,5 +701,36 @@
     	return $options;
 	}
-
+	//Busca usuarios de um contexto e ja retorna as options do select;
+	function get_available_users2($params)
+	{
+		$context= $params['context'];
+		$justthese = array("cn", "uid");
+		$filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
+
+	    if ($this->ldap)
+	    {
+			$sr=ldap_search($this->ldap, $context, $filter, $justthese);
+			$entries = ldap_get_entries($this->ldap, $sr);			
+
+			for ($i=0; $i<$entries["count"]; $i++){				
+					$u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
+			}
+
+			natcasesort($u_tmp);
+
+			$i = 0;
+			$users = array();
+
+			if (count($u_tmp))
+			{
+				foreach ($u_tmp as $uidnumber => $cn)
+				{
+					$options .= "<option value=$uidnumber>$cn</option>";
+				}
+				unset($u_tmp);
+			}			
+    		return $options;
+		}
+	}
 	//Busca usuários e listas de um contexto e já retorna as options do select;
 	function get_available_users_and_maillist($params)
@@ -1382,4 +1513,13 @@
 		return $entry[0]['uid'][0];
 	}
+	
+	function uid2cn($uid)
+	{
+		$justthese = array("cn");
+		$filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=".$uid."))";
+		$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+		$entry = ldap_get_entries($this->ldap, $search);
+		return $entry[0]['cn'][0];
+	}
 
 	function uidnumber2mail($uidnumber)
@@ -2124,5 +2264,5 @@
     	$return['trs'] = $trs_string;
     	return $return;
-	}
+}	
 	
 	function get_institutional_account_data($params)
@@ -2131,5 +2271,5 @@
 		{
 			$return['status'] = false;
-			$return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
+			$return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
 			return $return;
 		}
@@ -2180,5 +2320,183 @@
 		return $return;
 	}
-	
+	function get_shared_accounts($params)
+		{
+		if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_shared_accounts'))
+		{
+			$return['status'] = false;
+			$return['msg'] = $this->functions->lang('You do not have right to list shared accounts') . ".";
+			return $return;
+		}
+
+		$input = $params['input'];
+		$justthese = array("cn", "mail", "uid");
+		$trs = array();
+				
+		foreach ($this->manager_contexts as $idx=>$context)
+		{
+	    	$institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=s)(|(mail=$input*)(cn=*$input*)))"), $justthese);
+    		$entries = ldap_get_entries($this->ldap, $institutional_accounts);
+    	    	
+			for ($i=0; $i<$entries['count']; $i++)
+			{
+				$tr = "<tr class='normal' onMouseOver=this.className='selected' onMouseOut=this.className='normal'><td onClick=edit_shared_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['cn'][0] . "</td><td onClick=edit_shared_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['mail'][0] . "</td><td align='center' onClick=delete_shared_accounts('".$entries[$i]['uid'][0]."')><img HEIGHT='16' WIDTH='16' src=./expressoAdmin1_2/templates/default/images/delete.png></td></tr>";
+				$trs[$tr] = $entries[$i]['cn'][0];
+			}
+		}
+    	
+    	$trs_string = '';
+    	if (count($trs))
+    	{
+    		natcasesort($trs);
+    		foreach ($trs as $tr=>$cn)
+    		{
+    			$trs_string .= $tr;
+    		}
+    	}
+    	
+    	$return['status'] = 'true';
+    	$return['trs'] = $trs_string;
+    	return $return;
+	}
+	
+	function save_shared_accounts($params)
+	{
+			/* Begin: Access verification */
+			if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_shared_accounts'))
+			{
+				$return['status'] = false;
+				$return['msg'] = $this->functions->lang('You do not have right to edit shared accounts') . ".";
+				return $return;
+			}
+			$access_granted = false;
+			foreach ($this->manager_contexts as $idx=>$manager_context)
+			{
+				if (stristr($params['context'], $manager_context))
+				{
+					$access_granted = true;
+					break;
+				}
+			}
+			if (!$access_granted)
+			{
+				$return['status'] = false;
+				$return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
+				return $return;
+			}
+			/* End: Access verification */
+			
+			/* Begin: Validation */
+			if ( (empty($params['cn'])) || (empty($params['mail'])) )
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
+				return $result;
+			}
+	
+			if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
+				return $result;
+			}			
+			
+			$dn = strtolower("uid=$params[uid]," . $params['context']);
+			$anchor = strtolower($params['anchor']);
+                        
+			$filter = "(mail=".$params['mail'].")";
+			$justthese = array("cn");
+			$search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+			$entries = @ldap_get_entries($this->ldap,$search);
+			
+			if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
+			{
+				$result['status'] = false;
+				$result['msg'] = $this->functions->lang('Field mail already in use.');
+				return $result;
+			}
+			/* End: Validation */
+
+			$result = array();
+			$result['status'] = true;
+			
+			if ($anchor != $dn)
+			{
+				if (!@ldap_rename($this->ldap, $anchor, "uid=$params[uid]", $params['context'], true))
+				{
+					$result['status'] = false;
+					$result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_shared_accounts: ldap_rename';
+					$result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
+				}
+			}
+			
+			$info = array();
+			$info['cn']					= $params['cn'];
+			$info['sn']					= $params['cn'];
+			$info['uid']				= $params['uid'];
+			$info['mail']				= $params['mail'];
+			
+			if ($params['accountStatus'] == 'on')
+				$info['accountStatus'] = 'active';
+			else
+				$info['accountStatus'] = array();
+			
+			if ($params['phpgwAccountVisible'] == 'on')
+				$info['phpgwAccountVisible'] = '-1';
+			else
+				$info['phpgwAccountVisible'] = array();
+			
+			if ($params['description'] != '')
+				$info['description'] = utf8_encode($params['description']);
+			else
+				$info['description'] = array();
+			
+			if (!@ldap_modify ( $this->ldap, $dn, $info ))
+			{
+				$result['status'] = false;
+				$result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_shared_accounts: ldap_modify';
+				$result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
+			}
+                        //print_r($info);echo "Teste $dn".$result['msg'];exit();
+			return $result;
+	}	
+	
+	function get_shared_account_data($params)
+	{
+		if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_shared_accounts'))
+		{
+			$return['status'] = false;
+			$return['msg'] = $this->functions->lang('You do not have right to edit an shared accounts') . ".";
+			return $return;
+		}
+		
+		$uid = $params['uid'];
+		//$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
+				
+    	$shared_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=s)(uid=$uid))"));
+    	$entrie = ldap_get_entries($this->ldap, $shared_accounts);
+		
+		if ($entrie['count'] != 1)
+		{
+			$return['status'] = 'false';
+			$result['msg'] = $this->functions->lang('Problems loading datas') . '.';
+		}
+		else
+		{
+			$tmp_user_context = split(",", $entrie[0]['dn']);
+			$tmp_reverse_user_context = array_reverse($tmp_user_context);
+			array_pop($tmp_reverse_user_context);
+			$return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
+			
+			$return['status'] = 'true';
+			$return['accountStatus']		= $entrie[0]['accountstatus'][0];
+			$return['phpgwAccountVisible']	= $entrie[0]['phpgwaccountvisible'][0];
+			$return['cn']					= $entrie[0]['cn'][0];
+			$return['mail']					= $entrie[0]['mail'][0];
+			$return['description']			= utf8_decode($entrie[0]['description'][0]);
+			
+		}
+		
+		return $return;
+	}		
 	function mailforwardingaddress2uidnumber($mail)
 	{
@@ -2259,4 +2577,44 @@
 		return $result;
 	}
+        function delete_shared_account_data($params)
+	{
+		if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'delete_shared_accounts'))
+		{
+			$return['status'] = false;
+			$return['msg'] = $this->functions->lang('You do not have right to delete shared accounts') . ".";
+			return $return;
+		}                
+		$uid = $params['uid'];
+		$return['status'] = true;
+
+		$justthese = array("cn");
+    	$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=s)(uid=$uid))", $justthese);
+
+    	$entrie = ldap_get_entries($this->ldap, $search);
+        
+		if ($entrie['count'] > 1)
+		{
+			$return['status'] = false;
+			$return['msg']  = $this->functions->lang('More then one uid was found');
+			return $return;
+		}
+		if ($entrie['count'] == 0)
+		{
+			$return['status'] = false;
+			$return['msg']  = $this->functions->lang('No uid was found');
+			return $return;
+		}
+
+		$dn = $entrie[0]['dn'];
+		if (!@ldap_delete($this->ldap, $dn))
+		{
+			$return['status'] = false;
+			$return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_shared_accounts: ldap_delete";
+			$return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
+			return $return;
+		}
+
+		return $return;
+	}
 }
 ?>
Index: /trunk/expressoAdmin1_2/inc/class.functions.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.functions.inc.php	(revision 1516)
+++ /trunk/expressoAdmin1_2/inc/class.functions.inc.php	(revision 1913)
@@ -89,4 +89,8 @@
 					if ($array_acl[acl_add_institutional_accounts] || $array_acl[acl_edit_institutional_accounts] || $array_acl[acl_delete_institutional_accounts])
 						return true;
+                                        break;
+                                case list_shared_accounts:
+					if ($array_acl[acl_add_shared_accounts] || $array_acl[acl_edit_shared_accounts] || $array_acl[acl_delete_shared_accounts])
+						return true;
 					break;
 
@@ -298,4 +302,11 @@
 		// Last acl:	8.589.934.592 -> edit institutional accounts
 		// Last acl:   17.179.869.184 -> remove institutional accounts
+                // Last acl:   34.359.738.368 -> add share accounts
+                // Last acl:   68.719.476.736 -> edit share accounts
+                // Last acl:  137.438.953.472 -> delete share accounts
+                // Last acl:  274.877.906.944 -> edit share accounts acl
+                // Last acl:  549.755.813.888 -> edit quota share quote
+                // Last acl:  1.099.511.627.776 -> empty share accounts inbox
+
 		function make_array_acl($acl)
 		{
@@ -335,7 +346,13 @@
 							 	"acl_add_institutional_accounts",
 							 	"acl_edit_institutional_accounts",
-							 	"acl_remove_institutional_accounts"
+							 	"acl_remove_institutional_accounts",
+                                                                "acl_add_shared_accounts",
+                                                                "acl_edit_shared_accounts",
+                                                                "acl_delete_shared_accounts",
+                                                                "acl_edit_shared_accounts_acl",
+                                                                "acl_edit_shared_accounts_quote",
+                                                                "acl_empty_shared_accounts_inbox"
 							 	);
-			
+
 			foreach ($tmp as $index => $right)
 			{
Index: /trunk/expressoAdmin1_2/inc/class.manager.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.manager.inc.php	(revision 493)
+++ /trunk/expressoAdmin1_2/inc/class.manager.inc.php	(revision 1913)
@@ -57,5 +57,8 @@
 				if ($acl !== false)
 				{
-					$total_manager_acl += $value;
+					/* Valmir Andre de Sena - valmir.sena@ati.pe.gov.br
+					* changed + operation to bcadd, because php plus operation get wrong over 41 bits
+					*/
+					$total_manager_acl = bcadd( $total_manager_acl, $value);
 				}
 			}
Index: /trunk/expressoAdmin1_2/js/jscode/shared_accounts.js
===================================================================
--- /trunk/expressoAdmin1_2/js/jscode/shared_accounts.js	(revision 1913)
+++ /trunk/expressoAdmin1_2/js/jscode/shared_accounts.js	(revision 1913)
@@ -0,0 +1,542 @@
+countFiles = 1;
+
+function create_shared_accounts()
+{
+	select_owners = Element('ea_select_owners');
+        hidden_owners_acl = Element('owners_acls');
+	for(var i = 0;i < select_owners.options.length; i++){		
+		var user = select_owners.options[i].value;
+		select_owners.options[i].value = user;                
+	}
+        hidden_owners_acl.value =  connector.serialize(sharemailbox.ownersAcl);
+	cExecuteForm ("$this.shared_accounts.create", document.forms['shared_accounts_form'], handler_create_shared_accounts);
+        hidden_owners_acl.value = "";
+}
+
+function handler_create_shared_accounts(data_return)
+{
+	handler_create_shared_accounts2(data_return);
+	return;
+}
+
+function handler_create_shared_accounts2(data_return)
+{
+	if (!data_return.status)
+	{
+		write_msg(data_return.msg, 'error');
+	}
+	else
+	{
+		close_lightbox();
+		write_msg(get_lang('Shared account successful created') + '.', 'normal');
+	}
+	return;
+}
+
+function empty_inbox(uid)
+{
+var action = get_lang('Cleanned user mailbox');
+var handler_write_log = function(){}
+
+var handler_empty_inbox = function(data)
+{
+    if (!data.status)
+        alert(data.msg);
+    else{
+        cExecute ('$this.user.write_log_from_ajax&_action='+action+'&userinfo='+uid, handler_write_log);
+        alert(get_lang('Emptied') +' '+ data.inbox_size + ' ' + get_lang('MB from user inbox'));
+        document.getElementById('mailquota_used').value = data.mailquota_used;
+    }
+}
+    cExecute ('$this.shared_accounts.empty_inbox&uid='+Element('anchor').value, handler_empty_inbox);
+}
+function set_onload()
+{
+	sinc_combos_org(Element('ea_combo_org').value);
+	get_available_users(Element('ea_combo_org').value);
+}
+
+function get_available_users(context)
+{
+	var	handler_get_users = function(data)
+	{
+		select_available_users = Element('ea_select_available_users');
+		
+		//Clean select
+		for(var i=0; i<select_available_users.options.length; i++)
+		{
+			select_available_users.options[i] = null;
+			i--;
+		}
+
+		if ((data) && (data.length > 0))
+		{
+			// Necessario, pois o IE6 tem um bug que retira o primeiro options se o innerHTML estiver vazio.
+			select_available_users.innerHTML = '&nbsp;' + data;
+			select_available_users.outerHTML = select_available_users.outerHTML;
+			
+			select_available_users.disabled = false;
+			select_available_users_clone = Element('ea_select_available_users').cloneNode(true);
+			//document.getElementById('ea_input_searchUser').value = '';
+		}
+	}
+	cExecute ('$this.ldap_functions.get_available_users2&context='+context, handler_get_users);
+}
+
+function search_organization(key, element)
+{
+	var organizations = Element(element);
+	var RegExp_org = new RegExp("\\b"+key, "i");
+	
+	for(i = 0; i < organizations.length; i++)
+	{
+		if (RegExp_org.test(organizations[i].text))
+		{
+			organizations[i].selected = true;
+			return;
+		}
+	}
+}
+
+function sinc_combos_org(context)
+{
+	combo_org_available_users = Element('ea_combo_org_available_users');
+
+	for (i=0; i<combo_org_available_users.length; i++)
+	{
+		if (combo_org_available_users.options[i].value == context)
+		{
+			combo_org_available_users.options[i].selected = true;
+			get_available_users(context);
+			break;
+		}
+	}
+}
+
+var finderTimeout = '';
+function optionFinderTimeout(obj)
+{
+	clearTimeout(finderTimeout);
+	var oWait = Element("ea_span_searching");
+	oWait.innerHTML = get_lang('searching') + '...';
+	finderTimeout = setTimeout("optionFinder('"+obj.id+"')",500);
+}
+function optionFinder(id) {
+	var oWait = Element("ea_span_searching");
+	var oText = Element(id);
+		
+	//Limpa todo o select
+	var select_available_users_tmp = Element('ea_select_available_users')
+
+	for(var i = 0;i < select_available_users_tmp.options.length; i++)
+		select_available_users_tmp.options[i--] = null;
+
+	var RegExp_name = new RegExp("\\b"+oText.value, "i");
+	
+	//Inclui usuï¿œrio comeï¿œando com a pesquisa
+	for(i = 0; i < select_available_users_clone.length; i++){
+		if ( RegExp_name.test(select_available_users_clone[i].text) || (select_available_users_clone[i].value == -1) )
+		{
+			sel = select_available_users_tmp.options;
+			option = new Option(select_available_users_clone[i].text,select_available_users_clone[i].value);
+
+			if (select_available_users_clone[i].value == -1)
+				option.disabled = true;
+
+			sel[sel.length] = option;
+		}
+	}
+	oWait.innerHTML = '&nbsp;';
+}			
+
+function add_user()
+{
+	select_available_users = Element('ea_select_available_users');
+	select_owners = Element('ea_select_owners');
+
+	var count_available_users = select_available_users.length;
+	var new_options = '';
+
+	for (i = 0 ; i < count_available_users ; i++)
+	{
+		if (select_available_users.options[i].selected)
+		{
+			if(document.all)
+			{
+				if ( (select_owners.innerHTML.indexOf('value='+select_available_users.options[i].value)) == '-1' )
+				{
+					new_options +=  "<option value="
+								+ select_available_users.options[i].value
+								+ ">"
+								+ select_available_users.options[i].text
+								+ "</options>";
+				}
+			}
+			else
+			{
+				if ( (select_owners.innerHTML.indexOf('value="'+select_available_users.options[i].value+'"')) == '-1' )
+				{
+					new_options +=  "<option value="
+								+ select_available_users.options[i].value
+								+ ">"
+								+ select_available_users.options[i].text
+								+ "</options>";
+				}
+			}
+		}
+	}
+
+	if (new_options != '')
+	{
+		select_owners.innerHTML = "&nbsp;" + new_options + select_owners.innerHTML;
+		select_owners.outerHTML = select_owners.outerHTML;
+		select_owners.options[0].selected = true;
+                Element('em_input_readAcl').checked = false;
+		Element('em_input_deleteAcl').checked = false;
+		Element('em_input_writeAcl').checked = false;
+		Element('em_input_sendAcl').checked = false;
+                Element('em_input_sendAcl').disabled = true;
+	}
+}
+
+function remove_user()
+{
+	select_owners = Element('ea_select_owners');
+	for(var i = 0;i < select_owners.options.length; i++)
+		if(select_owners.options[i].selected){
+                        var user = select_owners.options[i].value;
+                        delete sharemailbox.ownersAcl[user];
+                        select_owners.options[i--] = null;
+                }
+       if(select_owners.options.length > 1 ){
+            select_owners.options[0].selected = true;
+            var user = select_owners.options[0].value;
+            sharemailbox.getaclfromuser(user);
+       }
+}
+
+function get_shared_accounts_timeOut(input)
+{
+	var head_table = '<table border="0" width="90%"><tr bgcolor="#d3dce3"><td width="30%">'+get_lang("full name")+'</td><td width="30%">'+get_lang("mail")+'</td><td width="5%" align="center">'+get_lang("remove")+'</td></tr></table>';
+	
+	if (input.length > 4)
+	{
+		clearTimeout(finderTimeout);
+		finderTimeout = setTimeout("get_shared_accounts('"+input+"')",500);
+	}
+	else
+	{
+		Element('shared_accounts_content').innerHTML = head_table;
+	}
+}
+
+function get_shared_accounts(input)
+{
+	var handler_get_shared_accounts = function(data)
+	{
+		if (data.status == 'true')
+		{
+			var table = '<table border="0" width="90%"><tr bgcolor="#d3dce3"><td width="30%">'+get_lang("full name")+'</td><td width="30%">'+get_lang("mail")+'</td><td width="5%" align="center">'+get_lang("remove")+'</td></tr>'+data.trs+'</table>';
+			Element('shared_accounts_content').innerHTML = table;
+		}
+		else
+			write_msg(data.msg, 'error');
+	}
+	cExecute ('$this.ldap_functions.get_shared_accounts&input='+input, handler_get_shared_accounts);
+}
+
+function edit_shared_account(uid)
+{
+	var handle_edit_shared_account = function(data)
+	{
+		if (data.status == 'true')
+		{
+			modal('shared_accounts_modal','save');
+			
+			var combo_org = Element('ea_combo_org');
+			for (i=0; i<combo_org.length; i++)
+			{
+				if (combo_org.options[i].value == data.user_context)
+				{
+					combo_org.options[i].selected = true;
+					break;
+				}
+			}
+			
+			// anchor
+			Element('anchor').value = "uid=" + uid + ',' + data.user_context;                        
+                        
+			if (data.accountStatus != 'active')
+				Element('accountStatus').checked = false;
+				
+			if (data.phpgwAccountVisible == '-1')
+				Element('phpgwAccountVisible').checked = true;
+			
+			Element('cn').value = data.cn;
+			Element('mail').value = data.mail;
+                        Element('mailquota').value = data.mailquota;
+                        Element('mailquota_used').value = data.mailquota_used;
+                        Element('quota_used_field').style.display = 'inline';
+			Element('description').value = data.description;
+			Element('ea_select_owners').innerHTML = data.owners_options;
+                        Element('display_empty_inbox').style.display = data.display_empty_inbox;
+                        if( data.allow_edit_shared_account_acl == "0"){
+                            Element('bt_add_user').disabled = true;
+                            Element('bt_remove_user').disabled = true;
+                            Element('em_input_readAcl').disabled = true;
+                            Element('em_input_deleteAcl').disabled = true;
+                            Element('em_input_writeAcl').disabled = true;
+                            Element('em_input_sendAcl').disabled = true;
+                        }
+
+			sinc_combos_org(data.user_context);
+                        sharemailbox.ownersAcl = new Array();
+			for (i=0; i<data.owners.length; i++){
+					sharemailbox.ownersAcl[ data.owners[i] ] = data.owners_acl[i];	
+			}
+		}
+		else
+			write_msg(data.msg, 'error');
+	}
+	cExecute ('$this.shared_accounts.get_data&uid='+uid, handle_edit_shared_account);
+}
+
+function save_shared_accounts()
+{
+	if (is_ie){
+		var i = 0;
+		while (document.forms(i).name != "shared_accounts_form"){i++}
+		form = document.forms(i);
+	}
+	else   form = document.forms["shared_accounts_form"];
+        
+	hidden_owners_acl = Element('owners_acls');
+	select_owners = Element('ea_select_owners');
+	for(var i = 0;i < select_owners.options.length; i++){		
+		var user = select_owners.options[i].value;                
+		select_owners.options[i].value = user;
+		}
+        hidden_owners_acl.value =  connector.serialize(sharemailbox.ownersAcl);
+	cExecuteForm ("$this.shared_accounts.save", form, handler_save_shared_accounts);
+        hidden_owners_acl.value = "";
+}
+
+function handler_save_shared_accounts(data_return)
+{
+	handler_save_shared_accounts2(data_return);
+	return;
+}
+function handler_save_shared_accounts2(data_return)
+{
+	if (!data_return.status)
+	{
+		write_msg(data_return.msg, 'error');
+	}
+	else
+	{
+		get_shared_accounts(Element('ea_shared_account_search').value);
+		close_lightbox();
+		write_msg(get_lang('Shared account successful saved') + '.', 'normal');
+	}
+	return;
+}
+
+function delete_shared_accounts(uid)
+{
+	if (!confirm(get_lang('Are you sure that you want to delete this shared account') + "?"))
+		return;
+	
+	var handle_delete_shared_account = function(data_return)
+	{
+		if (!data_return.status)
+		{
+			write_msg(data_return.msg, 'error');
+		}
+		else
+		{
+			write_msg(get_lang('Shared account successful deleted') + '.', 'normal');
+			get_shared_accounts(Element('ea_shared_account_search').value);
+		}
+		return;
+	}
+	cExecute ('$this.shared_accounts.delete&uid='+uid, handle_delete_shared_account);
+}	function cShareMailbox()
+	{
+		this.arrayWin = new Array();
+		this.el;
+		this.alert = false;
+		this.ownersAcl = new Array();
+	}
+
+	cShareMailbox.prototype.get_available_users = function(context)
+	{
+		var handler_get_available_users = function(data)
+		{
+			select_available_users = document.getElementById('em_select_available_users');
+		
+			//Limpa o select
+			for(var i=0; i<select_available_users.options.length; i++)
+			{
+				select_available_users.options[i] = null;
+				i--;
+			}
+
+			if ((data) && (data.length > 0))
+			{
+				// Necessario, pois o IE6 tem um bug que retira o primeiro options se o innerHTML estiver vazio.
+				select_available_users.innerHTML = '#' + data;
+				select_available_users.outerHTML = select_available_users.outerHTML;
+			
+				select_available_users.disabled = false;
+				select_available_users_clone = document.getElementById('em_select_available_users').cloneNode(true);
+				document.getElementById('em_input_searchUser').value = '';
+			}
+		}
+		cExecute ("$this.ldap_functions.get_available_users2&context="+context, handler_get_available_users);
+	}
+
+	cShareMailbox.prototype.getaclfromuser = function(user)
+	{
+	
+		Element('em_input_readAcl').checked = false;
+		Element('em_input_deleteAcl').checked = false;
+		Element('em_input_writeAcl').checked = false;
+		Element('em_input_sendAcl').checked = false;
+		Element('em_input_saveAcl').checked = false;
+		Element('em_input_saveAcl').disabled = true;
+		
+		if (this.ownersAcl[user] != "false" && this.ownersAcl[user].indexOf('lrs',0) >= 0)
+		{
+			Element('em_input_sendAcl').disabled = false;
+			Element('em_input_readAcl').checked = true;
+		}
+		else
+			Element('em_input_sendAcl').disabled = true;
+			
+		if (this.ownersAcl[user] != "false" && this.ownersAcl[user].indexOf('d',0) >= 0)
+		{
+			Element('em_input_deleteAcl').checked = true;
+		}
+		if (this.ownersAcl[user] != "false" && this.ownersAcl[user].indexOf('wi',0) >= 0)
+		{
+			Element('em_input_writeAcl').checked = true;
+		}		
+		if (this.ownersAcl[user] != "false" && this.ownersAcl[user].indexOf('a',0) >= 0)
+		{
+			Element('em_input_sendAcl').disabled = false;
+			Element('em_input_sendAcl').checked = true;
+		}
+		if (this.ownersAcl[user] != "false" && this.ownersAcl[user].indexOf('p',0) >= 0)
+		{
+			Element('em_input_saveAcl').disabled = false;
+			Element('em_input_saveAcl').checked = true;
+		}
+		if( this.ownersAcl[user] != "false" && Element('em_input_writeAcl').checked && Element('em_input_sendAcl').checked ){
+			Element('em_input_saveAcl').disabled = false;
+		} else Element('em_input_saveAcl').disabled = true;
+	}
+	
+	cShareMailbox.prototype.setaclfromuser = function()
+	{
+		var acl		= '';
+		var select 	= Element('ea_select_owners');
+
+		if(select.selectedIndex == "-1"){
+			alert("Selecione antes um usuario!");
+			return false;
+		}
+		var user = select.options[select.selectedIndex].value;
+		
+		if (Element('em_input_readAcl').checked) {
+			Element('em_input_sendAcl').disabled = false;
+			acl = 'lrs';
+		}
+		else{
+			Element('em_input_sendAcl').disabled = true;
+			Element('em_input_sendAcl').checked = false;
+		}
+				
+		if (Element('em_input_deleteAcl').checked)
+			acl += 'd';
+
+		if (Element('em_input_writeAcl').checked)
+			acl += 'wi';
+
+		if (Element('em_input_sendAcl').checked)
+			acl += 'a';
+				
+		if (Element('em_input_sendAcl').checked && Element('em_input_writeAcl').checked){
+			Element('em_input_saveAcl').disabled = false;				
+		} else {
+			Element('em_input_saveAcl').disabled = true;
+			Element('em_input_saveAcl').checked = false;
+		}
+		if (Element('em_input_saveAcl').checked)
+			acl += 'p';
+		this.ownersAcl[user] = acl;		
+	}
+		
+	cShareMailbox.prototype.add_user = function()
+	{
+		var select_available_users = document.getElementById('em_select_available_users');
+		var select_users = document.getElementById('ea_select_owners');
+
+		var count_available_users = select_available_users.length;
+		var count_users = select_users.options.length;
+		var new_options = '';
+	
+		for (i = 0 ; i < count_available_users ; i++)
+		{
+			if (select_available_users.options[i].selected)
+			{
+				if(document.all)
+				{
+					if ( (select_users.innerHTML.indexOf('value='+select_available_users.options[i].value)) == '-1' )
+					{
+						new_options +=  '<option value='
+									+ select_available_users.options[i].value
+									+ '>'
+									+ select_available_users.options[i].text
+									+ '</option>';
+					}
+				}
+				else
+				{
+					if ( (select_users.innerHTML.indexOf('value="'+select_available_users.options[i].value+'"')) == '-1' )
+					{
+						new_options +=  '<option value='
+									+ select_available_users.options[i].value
+									+ '>'
+									+ select_available_users.options[i].text
+									+ '</option>';
+					}
+				}
+			}
+		}
+
+		if (new_options != '')
+		{
+			select_users.innerHTML = '#' + new_options + select_users.innerHTML;
+			select_users.outerHTML = select_users.outerHTML;
+                        
+		}
+	}
+
+	cShareMailbox.prototype.remove_user = function()
+	{
+		select_users = document.getElementById('ea_select_owners');
+	
+		for(var i = 0;i < select_users.options.length; i++)
+			if(select_users.options[i].selected)
+				select_users.options[i--] = null;
+				
+		Element('em_input_readAcl').checked = false;
+		Element('em_input_deleteAcl').checked = false;
+		Element('em_input_writeAcl').checked = false;
+		Element('em_input_sendAcl').checked = false;
+	}
+	
+	
+/* Build the Object */
+	var sharemailbox;
+	sharemailbox = new cShareMailbox();
Index: /trunk/expressoAdmin1_2/js/jscode/users.js
===================================================================
--- /trunk/expressoAdmin1_2/js/jscode/users.js	(revision 1594)
+++ /trunk/expressoAdmin1_2/js/jscode/users.js	(revision 1913)
@@ -667,9 +667,9 @@
 		else{
 			cExecute ('$this.user.write_log_from_ajax&_action='+action+'&userinfo='+uid, handler_write_log);
-			alert(get_lang('Emptied ') + data.inbox_size + ' ' + get_lang('MB from user inbox'));
+			alert(get_lang('Emptied')+' '+ data.inbox_size + ' ' + get_lang('MB from user inbox'));
 			document.getElementById('mailquota_used').value = data.mailquota_used;
 		}
 	}
-	cExecute ('$this.imap_functions.empty_inbox&uid='+uid, handler_empty_inbox);
+	cExecute ('$this.imap_functions.empty_user_inbox&uid='+uid, handler_empty_inbox);
 }
 
Index: /trunk/expressoAdmin1_2/index.php
===================================================================
--- /trunk/expressoAdmin1_2/index.php	(revision 593)
+++ /trunk/expressoAdmin1_2/index.php	(revision 1913)
@@ -37,4 +37,5 @@
 	$var = Array(
 		'lang_user_accounts'	=> lang('User Accounts'),
+		'lang_shared_accounts'=> lang('Shared Accounts'),
 		'lang_institutional_accounts'=> lang('Institutional Accounts'),
 		'lang_user_groups'		=> lang('User Groups'),
Index: /trunk/expressoAdmin1_2/templates/default/shared_accounts.tpl
===================================================================
--- /trunk/expressoAdmin1_2/templates/default/shared_accounts.tpl	(revision 1913)
+++ /trunk/expressoAdmin1_2/templates/default/shared_accounts.tpl	(revision 1913)
@@ -0,0 +1,41 @@
+<!-- BEGIN body -->
+	<link rel="stylesheet" type="text/css" href="./expressoAdmin1_2/templates/default/shared_accounts.css">
+	<div style="display:none" id="{modal_id}">{shared_accounts_modal}</div>
+
+	<div align="center">
+		<table border="0" width="90%">
+			<tr>
+				<td align="left" width="25%">
+					<input type="button" value="{lang_create_shared_account}" "{create_share:_account_disabled}" onClick='{onclick_create_shared_account}'>
+					<input type="button" value="{lang_back}" onClick="document.location.href='{back_url}'">
+				</td>
+				<td align="center" "left" width="50%">
+					{lang_contexts}: <font color="blue">{context_display}</font>
+				</td>
+				<td align="right" "left" width="25%">
+						{lang_to_search}:
+						<input type="text" onKeyUp="javascript:get_shared_accounts_timeOut(this.value)" id="ea_shared_account_search" autocomplete="off" value="{query}">
+				</td>
+			</tr>
+		</table>
+	</div>
+ 
+	<div align="center" id="shared_accounts_content">
+		<table border="0" width="90%">
+			<tr bgcolor="{th_bg}">
+				<td width="30%">{lang_full_name}</td>
+				<td width="30%">{lang_mail}</td>
+				<td width="5%" align="center">{lang_remove}</td>
+			</tr>
+		</table>
+	</div>	
+
+	<div align="center">
+		<table border="0" width="90%">
+			<tr>
+				<td><input type="button" value="{lang_back}" onClick="document.location.href='{back_url}'"></td>
+			</tr>
+		</table>
+	</div>
+
+<!-- END body -->
Index: /trunk/expressoAdmin1_2/templates/default/managers_form.tpl
===================================================================
--- /trunk/expressoAdmin1_2/templates/default/managers_form.tpl	(revision 493)
+++ /trunk/expressoAdmin1_2/templates/default/managers_form.tpl	(revision 1913)
@@ -105,5 +105,7 @@
 							<td>{lang_view_user}:</td>
 							<td><input type="checkbox" name="acl_view_users" value="33554432" {acl_view_users}></td>
-							<td>{lang_add_institutional_accounts}:</td>
+							<td>{lang_add_shared_accounts}:</td>
+							<td><input type="checkbox" name="acl_add_shared_accounts" value="34359738368" {acl_add_shared_accounts}></td>
+                                                        <td>{lang_add_institutional_accounts}:</td>
 							<td><input type="checkbox" name="acl_add_institutional_accounts" value="4294967296" {acl_add_institutional_accounts}></td>
 						</tr>
@@ -111,10 +113,15 @@
 							<td>{lang_edit_users_picture}:</td>
 							<td><input type="checkbox" name="acl_edit_users_picture" value="536870912" {acl_edit_users_picture}></td>
+							<td>{lang_edit_shared_accounts}:</td>
+							<td><input type="checkbox" name="acl_edit_shared_accounts" value="68719476736" {acl_edit_shared_accounts}></td>
 							<td>{lang_edit_institutional_accounts}:</td>
 							<td><input type="checkbox" name="acl_edit_institutional_accounts" value="8589934592" {acl_edit_institutional_accounts}></td>
+
 						</tr>
 						<tr bgcolor="{color_font2}" align='right'>
 							<td>{lang_edit_users_phonenumber}:</td>
 							<td><input type="checkbox" name="acl_edit_users_phonenumber" value="2147483648" {acl_edit_users_phonenumber}></td>
+							<td>{lang_delete_shared_accounts}:</td>
+							<td><input type="checkbox" name="acl_delete_shared_accounts" value="137438953472" {acl_delete_shared_accounts}></td>
 							<td>{lang_remove_institutional_accounts}:</td>
 							<td><input type="checkbox" name="acl_remove_institutional_accounts" value="17179869184" {acl_remove_institutional_accounts}></td>
@@ -123,12 +130,18 @@
 							<td>{lang_change_users_password}:</td>
 							<td><input type="checkbox" name="acl_change_users_password" value="128" {acl_change_users_password}></td>
+							<td>{lang_edit_shared_accounts_acl}:</td>
+							<td><input type="checkbox" name="acl_edit_shared_accounts_acl" value="274877906944" {acl_edit_shared_accounts_acl}></td>
 						</tr>
 						<tr bgcolor="{color_font2}" align='right'>
 							<td>{lang_change_users_quote}:</td>
 							<td><input type="checkbox" name="acl_change_users_quote" value="262144" {acl_change_users_quote}></td>
+							<td>{lang_edit_shared_accounts_quote}:</td>
+							<td><input type="checkbox" name="acl_edit_shared_accounts_quote" value="549755813888" {acl_edit_shared_accounts_quote}></td>
 						</tr>
 						<tr bgcolor="{color_font1}" align='right'>
 							<td>{lang_set_default_users_password}:</td>
 							<td><input type="checkbox" name="acl_set_user_default_password" value="524288" {acl_set_user_default_password}></td>
+							<td>{lang_empty_shared_accounts_inbox}:</td>
+							<td><input type="checkbox" name="acl_empty_shared_accounts_inbox" value="1099511627776" {acl_empty_shared_accounts_inbox}></td>
 						</tr>
 						<tr bgcolor="{color_font2}" align='right'>
Index: /trunk/expressoAdmin1_2/templates/default/shared_accounts_modal.tpl
===================================================================
--- /trunk/expressoAdmin1_2/templates/default/shared_accounts_modal.tpl	(revision 1913)
+++ /trunk/expressoAdmin1_2/templates/default/shared_accounts_modal.tpl	(revision 1913)
@@ -0,0 +1,73 @@
+<input type="hidden" id="{modal_id}_title" value="{lang_creation_of_shared_accounts}">
+<input type="hidden" id="{modal_id}_height" value="503">
+<input type="hidden" id="{modal_id}_width" value="930">
+<input type="hidden" id="{modal_id}_close_action" value="close_lightbox()">
+<input type="hidden" id="{modal_id}_create_action" value="create_shared_accounts()">
+<input type="hidden" id="{modal_id}_save_action" value="save_shared_accounts()">
+<input type="hidden" id="{modal_id}_onload_action" value="set_onload({manager_context})">
+<form enctype="multipart/form-data" name="shared_accounts_form" method="post">
+<input type="hidden" id="anchor" name="anchor">
+<input type="hidden" id="owners_acls" name="owners_acl" value="">
+<table border="0" cellspacing="4">
+	<tr>
+		<td width="35%" bgcolor="#DDDDDD">
+			<p style="line-height: 220%">{lang_search_organization}:
+			<input type="text" id="organization_search" autocomplete="off" size=20 onKeyUp="javascript:search_organization(this.value, 'ea_combo_org');" onBlur="javascript:sinc_combos_org(context.value);"><br>
+			
+			{lang_organization}:
+			<select id="ea_combo_org" name="context" onchange="javascript:sinc_combos_org(this.value);">{manager_organizations}</select><br>
+							
+			{lang_full_name}: 
+			<input id="cn" name="cn" size="36" autocomplete="off"><br>
+						
+			{lang_mail}: 
+			<input id="mail" name="mail" size="45" autocomplete="off"><br>
+
+			{lang_description}:
+			<input id="description" name="description" size="42" autocomplete="off"><br>
+                        {lang_Email_quota_in_MB}:
+                        <input type="text" id="mailquota" name="mailquota" autocomplete="off" value="{mailquota}" {changequote_disabled} {disabled} size=16><br>
+                        <spam  id='quota_used_field' name='quota_used_field' style="display:{display_quota_used}">{lang_quota_used_in_mb}:
+                        <input type="text" name="mailquota_used" id="mailquota_used" value="{mailquota_used}" disabled size=10><br></spam>
+                        <spam id='display_empty_inbox' name='display_empty_inbox' style="display:none"><input type='button' {disabled} {disabled_empty_inbox} value='{lang_empty_inbox}' onclick="javascript:empty_inbox(anchor.value);"><br></spam>
+							
+			{lang_is_account_active}: <input type="checkbox" id="accountStatus" name="accountStatus" checked><br>
+			{lang_omit_account_from_the_catalog}: <input type="checkbox" id="phpgwAccountVisible" name="phpgwAccountVisible"></p>
+							
+			<b>{lang_owners}:</b><br>
+			<select style="width:350px; height:160px" id="ea_select_owners" onchange="sharemailbox.getaclfromuser(this.value);" name="owners[]" multiple size="13"></select>
+                        <span style="position:absolute;top:250;left:410"><table><tbody><tr><td colspan="2"><b>lang_</b></td></tr><tr><td>Leitura:</td><td><input id="em_input_readAcl" onclick="return sharemailbox.setaclfromuser();" type="checkbox"><img title="Outros usuarios poderÃ£o LER suas mensagens." src="./expressoAdmin1_2/templates/default/images/ajuda.jpg"></td></tr><tr><td>Exclusao:</td><td><input id="em_input_deleteAcl" onclick="return sharemailbox.setaclfromuser();" type="checkbox"><img title="Outros usuarios poderÃ£o APAGAR/MOVER suas mensagens." src="./expressoAdmin1_2/templates/default/images/ajuda.jpg"></td></tr><tr><td>Criacao:</td><td><input id="em_input_writeAcl" onclick="return sharemailbox.setaclfromuser();" type="checkbox"><img title="Outros usuÃ¡rios poderÃ£o CRIAR/ADICIONAR novas mensagens." src="./expressoAdmin1_2/templates/default/images/ajuda.jpg"></td></tr><tr><td>Enviar:</td><td><input disabled="disabled" id="em_input_sendAcl" onclick="return sharemailbox.setaclfromuser();" type="checkbox"><img title="Outros usuarios poderÃ£o ENVIAR mensagens como sendo vocÃª." src="./expressoAdmin1_2/templates/default/images/ajuda.jpg"></td><tr><td>Salvar:</td><td><input disabled="disabled" id="em_input_saveAcl" onclick="return sharemailbox.setaclfromuser();" type="checkbox"><img title="Outros usuarios irão SALVAR mensagens na pasta enviada" src="./expressoAdmin1_2/templates/default/images/ajuda.jpg"></td></tr>
+                        </span></tbody></table>
+		</td>
+						
+		<td width="15%" valign="middle" align="center" bgcolor="#DDDDDD">
+			<button id="bt_add_user" type="button" onClick="javascript:add_user();"><img src="expressoAdmin1_2/templates/default/images/add.png" style="vertical-align: middle;">&nbsp;{lang_add_owner}</button>
+			<br><br>
+			<button id="bt_remove_user" type="button" onClick="javascript:remove_user();"><img src="expressoAdmin1_2/templates/default/images/rem.png" style="vertical-align: middle;">&nbsp;{lang_remove_owner}</button>
+		</td>
+						
+		<td width="25%" valign="bottom" bgcolor="#DDDDDD">
+			{lang_search_organization}:<br>
+			<input type="text" id="organization_search" autocomplete="off" size=20 onKeyUp="javascript:search_organization(this.value, 'ea_combo_org_available_users');" onBlur="javascript:get_available_users(org_context.value);">
+			<br>
+							
+			{lang_organizations}:<br>
+			<select name="org_context" id="ea_combo_org_available_users" onchange="javascript:get_available_users(this.value);">{all_organizations}</select>
+			<br>
+			<!--
+			<input type="checkbox" name="ea_check_allUsers" id="ea_check_allUsers" onclick="javascript:get_available_users(org_context.value, this.checked);">{lang_all_users}.
+			-->
+			<br><br>
+							
+			{lang_search_user}:<br>
+			<input id="ea_input_searchUser" size="35" autocomplete="off" onkeyup="javascript:optionFinderTimeout(this)"><br>
+							
+			<font color="red"><span id="ea_span_searching">&nbsp;</span></font>
+			<br>
+			<b>{lang_users}:</b><br>
+			<select id="ea_select_available_users" style="width:350px; height:160px" multiple size="13"></select>
+		</td>
+	</tr>
+</table>
+
+</form>
Index: /trunk/expressoAdmin1_2/templates/default/shared_accounts.css
===================================================================
--- /trunk/expressoAdmin1_2/templates/default/shared_accounts.css	(revision 1913)
+++ /trunk/expressoAdmin1_2/templates/default/shared_accounts.css	(revision 1913)
@@ -0,0 +1,8 @@
+.normal {
+	background-color: #DDDDDD;	
+}
+
+.selected {
+	cursor: pointer;
+	background-color: #CCCBBB;
+}
Index: /trunk/expressoAdmin1_2/templates/default/index.tpl
===================================================================
--- /trunk/expressoAdmin1_2/templates/default/index.tpl	(revision 493)
+++ /trunk/expressoAdmin1_2/templates/default/index.tpl	(revision 1913)
@@ -16,5 +16,13 @@
   			<a href="../index.php?menuaction=expressoAdmin1_2.institutional_accounts.index">{lang_institutional_accounts}</a>
   		</td>
-  	</tr> 
+  	</tr>
+<tr>
+		<td width="1%" align="center">
+			<img src='./templates/default/images/mail_share.png'>
+  		</td>
+  		<td>
+  			<a href="../index.php?menuaction=expressoAdmin1_2.uishared_accounts.index">{lang_shared_accounts}</a>
+  		</td>
+  	</tr>  
 	<tr>
 		<td width="1%" align="center">
Index: /trunk/expressoMail1_2/js/sharemailbox.js
===================================================================
--- /trunk/expressoMail1_2/js/sharemailbox.js	(revision 1912)
+++ /trunk/expressoMail1_2/js/sharemailbox.js	(revision 1913)
@@ -60,8 +60,5 @@
 				Element('em_input_writeAcl').checked = true;
 			}
-			if (data[user].indexOf('p',0) >= 0)
-			{
-				Element('em_input_saveAcl').checked = true;
-			}
+			
 			if (data[user] != "false" && data[user].indexOf('a',0) >= 0)
 			{
@@ -106,13 +103,8 @@
 		if (Element('em_input_writeAcl').checked) {
 			acl += 'wi';			
-		}
-		
+		}		
 		if (Element('em_input_sendAcl').checked){
 			acl += 'a';			
-		} else { 
-			Element('em_input_saveAcl').disabled = true;
-			Element('em_input_saveAcl').checked = false;
-		}
-		
+		}
 		if (Element('em_input_sendAcl').checked && Element('em_input_writeAcl').checked){
 			Element('em_input_saveAcl').disabled = false;				
