[3102] | 1 | <?php |
---|
| 2 | /***************************************************************************\ |
---|
| 3 | * Expresso - Expresso Messenger * |
---|
| 4 | * - Alexandre Correia / Rodrigo Souza * |
---|
| 5 | * - JETI - http://jeti-im.org/ * |
---|
| 6 | * ------------------------------------------------------------------------- * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 8 | * under the terms of the GNU General Public License as published by the * |
---|
| 9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 10 | * option) any later version. * |
---|
| 11 | \***************************************************************************/ |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | require_once "class.ldap_im.inc.php"; |
---|
| 15 | require_once "class.db_im.inc.php"; |
---|
| 16 | |
---|
| 17 | class contacts_im |
---|
| 18 | { |
---|
| 19 | private $db; |
---|
| 20 | private $dn_User; |
---|
| 21 | private $hostsJabberLdap; |
---|
| 22 | private $ldap; |
---|
| 23 | private $ou_User; |
---|
| 24 | private $serverJabber; |
---|
| 25 | private $serverLdap; |
---|
| 26 | |
---|
| 27 | function __construct() |
---|
| 28 | { |
---|
| 29 | $this->ldap = new ldap_im(); |
---|
| 30 | $this->db = new db_im(); |
---|
| 31 | |
---|
| 32 | // (DN) User |
---|
| 33 | $this->dn_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn']; |
---|
| 34 | |
---|
| 35 | // (OU) User |
---|
| 36 | $this->ou_User = $this->dn_User; |
---|
| 37 | $this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou=")); |
---|
| 38 | $this->ou_User = strtoupper(substr($this->ou_User, 0, strpos($this->ou_User, ",dc="))); |
---|
| 39 | |
---|
| 40 | // Server Name Jabber |
---|
| 41 | $this->serverJabber = $_SESSION['phpgw_info']['jabberit_messenger']['name_jabberit']; |
---|
| 42 | |
---|
| 43 | // Server Name Ldap |
---|
| 44 | $this->serverLdap = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit']; |
---|
| 45 | |
---|
| 46 | // Hosts Jabber / Ldap |
---|
| 47 | $this->hostsJabberLdap = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | private final function groupsLocked() |
---|
| 51 | { |
---|
| 52 | $memberShip = array(); |
---|
| 53 | $groupsLocked = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']); |
---|
| 54 | |
---|
| 55 | foreach($_SESSION['phpgw_info']['jabberit_messenger']['membership'] as $tmp) |
---|
| 56 | $memberShip[] = $tmp['account_name']; |
---|
| 57 | |
---|
| 58 | foreach($groupsLocked as $tmp) |
---|
| 59 | { |
---|
| 60 | $groups = explode(":", $tmp); |
---|
| 61 | |
---|
| 62 | if( array_search($groups[1], $memberShip) !== False ) |
---|
| 63 | { |
---|
| 64 | $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked'] = $groups[2]; |
---|
| 65 | return true; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | return false; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public final function list_contacts($param) |
---|
| 73 | { |
---|
| 74 | $order = array(); |
---|
| 75 | $ou_User = substr($this->ou_User, (strpos($this->ou_User,"=")+1)); |
---|
[3266] | 76 | $return = ""; |
---|
[3102] | 77 | $users = $this->getUsersIm($param['name']); |
---|
| 78 | |
---|
[3266] | 79 | if( count($users) == 0 ) |
---|
| 80 | return "<empty></empty>"; |
---|
[3102] | 81 | |
---|
[3266] | 82 | if( $users === "manyresults" ) |
---|
| 83 | { |
---|
| 84 | if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']) ) |
---|
| 85 | unset($_SESSION['phpgw_info']['jabberit_messenger']['photo']); |
---|
| 86 | |
---|
| 87 | return "<manyresults></manyresults>"; |
---|
| 88 | } |
---|
| 89 | |
---|
[3102] | 90 | // Hosts Jabber |
---|
| 91 | $hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); |
---|
| 92 | |
---|
| 93 | if( is_array($users) ) |
---|
[7673] | 94 | { |
---|
| 95 | $users_count = count($users); |
---|
| 96 | for($i = 0; $i < $users_count; ++$i) |
---|
[3102] | 97 | { |
---|
| 98 | if( is_array($hostsJabber) ) |
---|
| 99 | { |
---|
| 100 | foreach($hostsJabber as $itens) |
---|
| 101 | { |
---|
| 102 | if( trim($users[$i]['ou']) === trim($itens['org']) && strpos($users[$i]['jid'],"@") === false ) |
---|
| 103 | { |
---|
| 104 | $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName']; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if( array_key_exists('ouAll', $users[$i]) && trim($itens['org']) === "*" ) |
---|
| 108 | { |
---|
| 109 | $users[$i]['jid'] = $users[$i]['jid']."@".$itens['jabberName']; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if( strpos($users[$i]['jid'],"@") === false ) |
---|
| 115 | { |
---|
| 116 | $users[$i]['jid'] = $users[$i]['jid']."@".$this->serverJabber; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | foreach($users as $tmp) |
---|
| 121 | { |
---|
[3530] | 122 | if ( !array_key_exists($tmp['ou'], $order) ) |
---|
[3102] | 123 | $order[$tmp['ou']] = array(); |
---|
| 124 | |
---|
| 125 | $order[$tmp['ou']][] = '<data><ou>'.$tmp['ou'].'</ou><cn>'.$tmp['cn'].'</cn><mail>'.$tmp['mail'].'</mail><uid>'.$tmp['uid'].'</uid><jid>'.$tmp['jid'].'</jid><photo>'.$tmp['photo'].'</photo></data>'; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | ksort($order); |
---|
| 129 | |
---|
| 130 | $return = '<uids>'; |
---|
| 131 | foreach ( $order as $key => $val ) |
---|
| 132 | $return .= '<'.$key.'>'.implode('',$val).'</'.$key.'>'; |
---|
| 133 | $return .= '</uids>'; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return $return; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | private final function getUsersIm($pName) |
---|
[3266] | 140 | { |
---|
[3102] | 141 | $array_uids = array(); |
---|
[3266] | 142 | $countUids = 0; |
---|
[3102] | 143 | $members = array(); |
---|
| 144 | $result = array(); |
---|
| 145 | $uidType = "uid"; |
---|
[3266] | 146 | $serversLdap = unserialize( trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']) ); |
---|
[3102] | 147 | |
---|
[3266] | 148 | if( $serversLdap ) |
---|
| 149 | { |
---|
| 150 | // Usa Grupos Ldap |
---|
| 151 | $filters = array( ); |
---|
| 152 | |
---|
| 153 | foreach( $serversLdap as $servers => $groups ) |
---|
| 154 | { |
---|
| 155 | $filter = ''; |
---|
| 156 | foreach( unserialize($groups) as $group ) |
---|
| 157 | $filter .= '(gidnumber' . strstr( $group, ':' ) . ')'; |
---|
[3102] | 158 | |
---|
[3266] | 159 | $filters[ $servers ] = str_replace( ':', '=', $filter ); |
---|
| 160 | } |
---|
[3102] | 161 | |
---|
[3266] | 162 | foreach( $serversLdap as $key => $tmp ) |
---|
[3102] | 163 | { |
---|
[3266] | 164 | if( $key === $this->serverLdap ) |
---|
[3102] | 165 | { |
---|
[3266] | 166 | $result[$key] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*"); |
---|
| 167 | $countUids += count($result[$key]); |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | if( !$this->groupsLocked() ) |
---|
[3102] | 172 | { |
---|
[3266] | 173 | $result[$key] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $key ); |
---|
| 174 | $countUids += count($result[$key]); |
---|
| 175 | } |
---|
[3102] | 176 | } |
---|
[3266] | 177 | } |
---|
[3102] | 178 | |
---|
[3266] | 179 | if( $countUids > $this->ldap->getMaxResults() ) |
---|
| 180 | { |
---|
| 181 | return "manyresults"; |
---|
[3102] | 182 | } |
---|
| 183 | |
---|
[3266] | 184 | $_RESULT = $this->ldap->getMembers($result, $filters); |
---|
| 185 | |
---|
| 186 | foreach( $_RESULT as $key => $value ) |
---|
| 187 | $array_uids = array_merge($array_uids, $_RESULT[$key]); |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
[3406] | 191 | // Ldap Root |
---|
[3266] | 192 | $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*"); |
---|
| 193 | |
---|
[3406] | 194 | // Ldap Catalog |
---|
| 195 | if( count($this->hostsJabberLdap) ) |
---|
| 196 | { |
---|
| 197 | foreach( $this->hostsJabberLdap as $conf ) |
---|
| 198 | { |
---|
| 199 | $result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $conf['serverLdap'] ); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
[3266] | 203 | foreach( $result as $key => $value ) |
---|
| 204 | { |
---|
[3406] | 205 | $array_uids = array_merge($array_uids, $value); |
---|
[3266] | 206 | } |
---|
| 207 | |
---|
| 208 | if( count($array_uids) > $this->ldap->getMaxResults() ) |
---|
[3102] | 209 | { |
---|
[3266] | 210 | return "manyresults"; |
---|
| 211 | } |
---|
| 212 | } |
---|
[3102] | 213 | |
---|
[3266] | 214 | if( $this->groupsLocked() ) |
---|
| 215 | { |
---|
| 216 | $orgs[] = substr($this->ou_User, ( strpos($this->ou_User, "ou=") + 3 ) ); |
---|
| 217 | $orgsGroupsLocked = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']); |
---|
| 218 | |
---|
| 219 | foreach( $orgsGroupsLocked as $tmp ) |
---|
| 220 | { |
---|
| 221 | if( $tmp != "") |
---|
| 222 | $orgs[] = $tmp; |
---|
[3102] | 223 | } |
---|
| 224 | |
---|
[3266] | 225 | $orgs = array_unique($orgs); |
---|
| 226 | |
---|
| 227 | $_restrict = array(); |
---|
[7673] | 228 | |
---|
| 229 | $orgs_count = count($orgs); |
---|
| 230 | for( $i = 0 ; $i < $orgs_count; ++$i ) |
---|
[3102] | 231 | { |
---|
[7673] | 232 | $array_uids_count = count($array_uids); |
---|
| 233 | for( $j = 0 ; $j < $array_uids_count; ++$j ) |
---|
[3102] | 234 | { |
---|
[3266] | 235 | if( trim($array_uids[$j]['ou']) === trim($orgs[$i]) ) |
---|
[3102] | 236 | { |
---|
[3266] | 237 | $_restrict[] = $array_uids[$j]; |
---|
| 238 | } |
---|
| 239 | } |
---|
[3102] | 240 | } |
---|
| 241 | |
---|
[3266] | 242 | return $_restrict; |
---|
| 243 | } |
---|
| 244 | else |
---|
| 245 | { |
---|
| 246 | return $array_uids; |
---|
| 247 | } |
---|
[3102] | 248 | } |
---|
| 249 | |
---|
| 250 | private final function strallpos($haystack, $needle, $offset = 0) |
---|
| 251 | { |
---|
| 252 | $result = array(); |
---|
[7655] | 253 | for($i = $offset; $i< strlen($haystack); ++$i ) |
---|
[3102] | 254 | { |
---|
| 255 | $pos = strpos($haystack,$needle,$i); |
---|
| 256 | if($pos !== FALSE) |
---|
| 257 | { |
---|
| 258 | $offset = $pos; |
---|
| 259 | if($offset >= $i) |
---|
| 260 | $result[] = $i = $offset; |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | return $result; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | public final function verifyAddNewContact($pUid) |
---|
| 268 | { |
---|
| 269 | $groupsLocked = explode(";",$_SESSION['phpgw_info']['jabberit_messenger']['groups_locked']); |
---|
| 270 | $gidNumbers = array(); |
---|
| 271 | $uid = $pUid['uid']; |
---|
| 272 | $uid_User = substr($this->dn_User, 0, strpos($this->dn_User, ",")); |
---|
| 273 | $uid_User = substr($uid_User, 4); |
---|
| 274 | |
---|
| 275 | foreach($groupsLocked as $tmp) |
---|
| 276 | { |
---|
| 277 | $groups = explode(":", $tmp); |
---|
| 278 | $gidNumbers[] = $groups[1]; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | $filter_gid = implode(")(gidnumber=",$gidNumbers); |
---|
| 282 | $filter_gid = "(gidnumber=". $filter_gid. ")"; |
---|
| 283 | |
---|
| 284 | $result = $this->ldap->getGroupsMemberUid( $filter_gid, "localhost" ); |
---|
| 285 | |
---|
| 286 | if( $result && is_array($result) ) |
---|
| 287 | { |
---|
| 288 | array_shift($result); |
---|
| 289 | $i = 0; |
---|
| 290 | |
---|
| 291 | foreach($result as $value) |
---|
| 292 | { |
---|
| 293 | $Groups[$i]['dn'] = $value['dn']; |
---|
| 294 | $Groups[$i]['gidnumber'] = $value['gidnumber'][0]; |
---|
| 295 | if(array_key_exists('memberuid',$value)) |
---|
| 296 | { |
---|
| 297 | array_shift($value['memberuid']); |
---|
| 298 | $Groups[$i++]['memberuid'] = $value['memberuid']; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | $search = array(); |
---|
| 303 | $search_Gid = array(); |
---|
| 304 | |
---|
| 305 | // Verifica Uid em Grupo Bloqueado |
---|
| 306 | foreach($Groups as $value) |
---|
| 307 | { |
---|
| 308 | if( array_search( $uid , $value['memberuid'] ) !== false ) |
---|
| 309 | { |
---|
| 310 | $ou = substr($value['dn'],strpos($value['dn'], "ou=")); |
---|
| 311 | if( array_search($uid_User, $value['memberuid']) === false ) |
---|
| 312 | { |
---|
| 313 | $search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc="))); |
---|
| 314 | $search_Gid[] = $value['gidnumber']; |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | if( $this->groupsLocked() ) |
---|
| 322 | { |
---|
| 323 | if( count($search) > 0 ) |
---|
| 324 | { |
---|
| 325 | // Verifica permissões do grupo |
---|
| 326 | foreach($groupsLocked as $value) |
---|
| 327 | { |
---|
| 328 | $tpGroups = explode(":",$value); |
---|
| 329 | if( $tpGroups[1] == $search_Gid[0] ) |
---|
| 330 | { |
---|
| 331 | $ousTp = explode(",",$tpGroups[2]); |
---|
| 332 | $ou_User = strtoupper(trim($this->dn_User)); |
---|
| 333 | |
---|
| 334 | $posAll = $this->strallpos($ou_User, "OU=" ); |
---|
| 335 | $orgs = array(); |
---|
[7673] | 336 | |
---|
| 337 | $posAll_count = count($posAll); |
---|
| 338 | for( $i = 0 ; $i < $posAll_count; ++$i ) |
---|
[3102] | 339 | { |
---|
| 340 | $pos = strpos($ou_User, ","); |
---|
| 341 | $tmpString = substr($ou_User, $posAll[$i] + 3); |
---|
| 342 | $orgs[] = substr($tmpString, 0, strpos($tmpString, ",")); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | $ou_User = implode("/", array_reverse($orgs)); |
---|
| 346 | |
---|
| 347 | if( array_search( $ou_User, $ousTp) !== false ) |
---|
| 348 | return "true"; |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | return "false"; |
---|
| 352 | } |
---|
| 353 | else |
---|
| 354 | return "true"; |
---|
| 355 | } |
---|
| 356 | else |
---|
| 357 | { |
---|
| 358 | // Se Bloqueado verifica o Grupo |
---|
| 359 | if( count($search) > 0 ) |
---|
| 360 | { |
---|
| 361 | if( array_search($this->ou_User, $search) === false ) |
---|
| 362 | { |
---|
| 363 | // Verifica permissões do grupo |
---|
| 364 | foreach($groupsLocked as $value) |
---|
| 365 | { |
---|
| 366 | $tpGroups = explode(":",$value); |
---|
| 367 | |
---|
| 368 | if( $tpGroups[1] == $search_Gid[0] ) |
---|
| 369 | { |
---|
| 370 | $ousTp = explode(",",$tpGroups[2]); |
---|
| 371 | $ou_User = strtoupper(trim($this->dn_User)); |
---|
| 372 | |
---|
| 373 | $posAll = $this->strallpos($ou_User, "OU=" ); |
---|
| 374 | $orgs = array(); |
---|
[7673] | 375 | |
---|
| 376 | $posAll_count = count($posAll); |
---|
| 377 | for( $i = 0 ; $i < $posAll_count; ++$i ) |
---|
[3102] | 378 | { |
---|
| 379 | $pos = strpos($ou_User, ","); |
---|
| 380 | $tmpString = substr($ou_User, $posAll[$i] + 3); |
---|
| 381 | $orgs[] = substr($tmpString, 0, strpos($tmpString, ",")); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | $ou_User = implode("/", array_reverse($orgs)); |
---|
| 385 | |
---|
| 386 | if( array_search( $ou_User, $ousTp) !== false ) |
---|
| 387 | return "true"; |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | return "false"; |
---|
| 391 | } |
---|
| 392 | return "true"; |
---|
| 393 | } |
---|
| 394 | return "true"; |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | ?> |
---|