ldap = new LdapIM();
$this->db = new DataBaseIM();
// (DN) User
$this->dn_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn'];
// (OU) User
$this->ou_User = $this->dn_User;
$this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou="));
$this->ou_User = strtoupper(substr($this->ou_User, 0, strpos($this->ou_User, ",dc=")));
// Server Name Jabber
$this->serverJabber = $_SESSION['phpgw_info']['jabberit_messenger']['name_jabberit'];
// Server Name Ldap
$this->serverLdap = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
// Hosts Jabber / Ldap
$this->hostsJabberLdap = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
if ( file_exists('attributeLdap.php') )
{
require_once('attributeLdap.php');
$this->attribute = trim($attributeTypeName);
}
else
$this->attribute = "uid";
}
public final function getListContacts($param)
{
$order = array();
$ou_User = substr($this->ou_User, (strpos($this->ou_User,"=")+1));
$return = '';
$users = $this->getUsersIm($param['name']);
if (!is_array($users) && trim($users) === 'Many Results')
return "Many Results";
// Hosts Jabber
$hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
if( is_array($users) )
{
for($i = 0; $i < count($users); $i++)
{
if( is_array($hostsJabber) )
{
foreach($hostsJabber as $itens)
{
if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false )
{
$users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
}
if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" )
{
$users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName'];
}
}
}
if( strpos($users[$i]['jid'],"@") === false )
{
$users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber;
}
}
foreach($users as $tmp)
{
if ( !array_key_exists($tmp['ou'], $order) )
$order[$tmp['ou']] = array();
$order[$tmp['ou']][] = ''.$tmp['ou'].''.$tmp['cn'].''.$tmp['mail'].''.$tmp['uid'].''.$tmp['jid'].''.$tmp['photo'].'';
}
ksort($order);
$return = '';
foreach ( $order as $key => $val )
$return .= '<'.$key.'>'.implode('',$val).''.$key.'>';
$return .= '';
}
return $return;
}
private final function getUsersIm($pName)
{
$array_uids = array();
$members = array();
$result = array();
$uidType = "uid";
$serversLdap = unserialize(trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']));
if( $serversLdap )
{
if( array_key_exists($this->serverLdap, $serversLdap) )
{
$groups = unserialize($serversLdap[$this->serverLdap]);
}
if( count($groups) > 0 )
{
foreach($groups as $tmp)
{
$group = explode(":",$tmp);
$array_result = $this->ldap->getGroupsMemberUid($group[0], $this->serverLdap );
@array_shift($array_result[0]['memberuid']);
$array_uids = @array_merge($array_uids, $array_result[0]['memberuid']);
unset($array_result);
}
}
}
if( count($array_uids) == 0 )
{
$array_uids = $this->db->get_accounts_acl();
$uidType = "uidnumber";
}
for( $i = 0; $i < count($array_uids) ; $i+=50 )
{
$partial_uids = array_slice($array_uids,$i,50);
$filter_uid = implode(")(".$uidType."=",$partial_uids);
$filter_uid = "(".$uidType."=". $filter_uid. ")";
if( $this->groupsLocked() )
{
$orgs[] = $this->ou_User;
$orgsGroupsLocked = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']);
foreach( $orgsGroupsLocked as $tmp )
{
if( $tmp != "" )
{
if( strpos($tmp, "/") !== false )
{
$tt = explode("/", $tmp);
$newOU = implode(",OU=",array_reverse($tt));
$orgs[] = "OU=". $newOU ;
}
else
$orgs[] = "OU=". $tmp;
}
}
$orgs = array_unique($orgs);
foreach( $orgs as $orgB )
$result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid, $orgB );
}
else
$result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid);
}
if( !$this->groupsLocked() )
{
unset($serversLdap[$this->serverLdap]);
if( $serversLdap && count($serversLdap))
{
foreach( $serversLdap as $key => $tmp )
{
$array_uids_external = array( );
$groupsExternal = unserialize($tmp);
if( count($groupsExternal) )
{
foreach( $groupsExternal as $tmpExt )
{
$group = explode(":",$tmpExt);
$array_result = $this->ldap->getGroupsMemberUid( $group[0], $key );
if( $array_result[0]['memberuid']['count'] )
unset($array_result[0]['memberuid']['count']);
$array_uids_external = @array_merge($array_uids_external, $array_result[0]['memberuid']);
}
if( is_array($array_uids_external) )
{
for( $i = 0; $i < count( $array_uids_external ) ; $i+=50 )
{
$partial_uids_external = array_slice($array_uids_external,$i,50);
$filter_uid_external = implode(")(".$this->attribute."=",$partial_uids_external);
$filter_uid_external = "(".$this->attribute."=". $filter_uid_external. ")";
$result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $filter_uid_external, $key );
}
}
}
}
}
if( count($this->hostsJabberLdap) )
{
for($i = 0 ; $i < count($this->hostsJabberLdap); $i++)
{
if( !@array_key_exists($this->hostsJabberLdap[$i]['serverLdap'], $serversLdap) )
{
$result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", false, $this->hostsJabberLdap[$i]['serverLdap'] );
}
}
}
}
if ( is_array($result) )
{
for( $i = 0; $i < count($result); $i++ )
if( is_array($result[$i]) )
$members = @array_merge($members,$result[$i]);
}
if( count($members) > 150)
{
$ou = substr( $this->ou_User, strpos($this->ou_User, "=") + 1 );
for($i = 0 ; $i < count($members); $i++ )
{
if( $ou == $members[$i]['ou'] )
{
$uids_org[] = $members[$i];
}
else
{
if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
}
}
return $uids_org;
}
return $members;
}
private final function groupsLocked()
{
$memberShip = array();
$groupsLocked = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp)
$memberShip[] = $tmp['account_name'];
foreach($groupsLocked as $tmp)
{
$groups = explode(":", $tmp);
if( array_search($groups[1], $memberShip) !== False )
{
$_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2];
return true;
}
}
return false;
}
private final function strallpos($haystack, $needle, $offset = 0)
{
$result = array();
for($i = $offset; $i< strlen($haystack); $i++ )
{
$pos = strpos($haystack,$needle,$i);
if($pos !== FALSE)
{
$offset = $pos;
if($offset >= $i)
$result[] = $i = $offset;
}
}
return $result;
}
public final function verifyAddNewContact($pUid)
{
$groupsLocked = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']);
$gidNumbers = array();
$uid = $pUid['uid'];
$uid_User = substr($this->dn_User, 0, strpos($this->dn_User, ","));
$uid_User = substr($uid_User, 4);
foreach($groupsLocked as $tmp)
{
$groups = explode(":", $tmp);
$gidNumbers[] = $groups[1];
}
$filter_gid = implode(")(gidnumber=",$gidNumbers);
$filter_gid = "(gidnumber=". $filter_gid. ")";
$result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" );
if( $result && is_array($result) )
{
array_shift($result);
$i = 0;
foreach($result as $value)
{
$Groups[$i]['dn'] = $value['dn'];
$Groups[$i]['gidnumber'] = $value['gidnumber'][0];
if(array_key_exists('memberuid',$value))
{
array_shift($value['memberuid']);
$Groups[$i++]['memberuid'] = $value['memberuid'];
}
}
$search = array();
$search_Gid = array();
// Verifica Uid em Grupo Bloqueado
foreach($Groups as $value)
{
if( array_search( $uid , $value['memberuid'] ) !== false )
{
$ou = substr($value['dn'],strpos($value['dn'], "ou="));
if( array_search($uid_User, $value['memberuid']) === false )
{
$search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
$search_Gid[] = $value['gidnumber'];
}
}
}
}
if( $this->groupsLocked() )
{
if( count($search) > 0 )
{
// Verifica permissões do grupo
foreach($groupsLocked as $value)
{
$tpGroups = explode(":",$value);
if( $tpGroups[1] == $search_Gid[0] )
{
$ousTp = explode(",",$tpGroups[2]);
$ou_User = strtoupper(trim($this->dn_User));
$posAll = $this->strallpos($ou_User, "OU=" );
$orgs = array();
for( $i = 0 ; $i < count($posAll); $i++ )
{
$pos = strpos($ou_User, ",");
$tmpString = substr($ou_User, $posAll[$i] + 3);
$orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
}
$ou_User = implode("/", array_reverse($orgs));
if( array_search( $ou_User, $ousTp) !== false )
return "true";
}
}
return "false";
}
else
return "true";
}
else
{
// Se Bloqueado verifica o Grupo
if( count($search) > 0 )
{
if( array_search($this->ou_User, $search) === false )
{
// Verifica permissões do grupo
foreach($groupsLocked as $value)
{
$tpGroups = explode(":",$value);
if( $tpGroups[1] == $search_Gid[0] )
{
$ousTp = explode(",",$tpGroups[2]);
$ou_User = strtoupper(trim($this->dn_User));
$posAll = $this->strallpos($ou_User, "OU=" );
$orgs = array();
for( $i = 0 ; $i < count($posAll); $i++ )
{
$pos = strpos($ou_User, ",");
$tmpString = substr($ou_User, $posAll[$i] + 3);
$orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
}
$ou_User = implode("/", array_reverse($orgs));
if( array_search( $ou_User, $ousTp) !== false )
return "true";
}
}
return "false";
}
return "true";
}
return "true";
}
}
}
?>