[382] | 1 | <?php |
---|
[417] | 2 | /***************************************************************************\ |
---|
| 3 | * Expresso - Expresso Messenger * |
---|
[1504] | 4 | * - Alexandre Correia / Rodrigo Souza * |
---|
| 5 | * - JETI - http://jeti-im.org/ * |
---|
[417] | 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 | \***************************************************************************/ |
---|
[382] | 12 | |
---|
[1757] | 13 | if( is_dir('../phpgwapi/inc') ) |
---|
| 14 | define('PHPGW_API_INC','../phpgwapi/inc'); |
---|
| 15 | else |
---|
| 16 | define('PHPGW_API_INC','../../phpgwapi/inc'); |
---|
| 17 | |
---|
[382] | 18 | require_once( PHPGW_API_INC . '/class.common.inc.php'); |
---|
| 19 | |
---|
| 20 | class ldap_im |
---|
| 21 | { |
---|
[1504] | 22 | private $attr_org; |
---|
[946] | 23 | private $common; |
---|
| 24 | private $hostsJabber; |
---|
[382] | 25 | private $ldap; |
---|
| 26 | private $ldap_context; |
---|
[1160] | 27 | private $ldap_user; |
---|
[946] | 28 | private $ldap_host; |
---|
[951] | 29 | private $ldap_org; |
---|
[382] | 30 | private $ldap_pass; |
---|
| 31 | private $max_result; |
---|
| 32 | |
---|
| 33 | public final function __construct() |
---|
| 34 | { |
---|
[1757] | 35 | $this->ldap_host = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit']; |
---|
| 36 | $this->ldap_context = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit']; |
---|
| 37 | $this->ldap_user = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit']; |
---|
| 38 | $this->ldap_pass = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit']; |
---|
| 39 | |
---|
[1504] | 40 | // Attributes org ldap; |
---|
| 41 | $this->attr_org = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['attributes_org_ldap_jabberit']); |
---|
| 42 | |
---|
[946] | 43 | // Hosts Jabber |
---|
| 44 | $this->hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); |
---|
| 45 | |
---|
| 46 | // Result Ldap |
---|
| 47 | $this->max_result = 30; |
---|
[1757] | 48 | |
---|
[382] | 49 | } |
---|
| 50 | |
---|
| 51 | public final function __destruct() |
---|
| 52 | { |
---|
| 53 | if( $this->ldap ) |
---|
| 54 | ldap_close($this->ldap); |
---|
| 55 | } |
---|
| 56 | |
---|
[946] | 57 | private final function ldapConn() |
---|
[382] | 58 | { |
---|
[946] | 59 | $this->common = new common(); |
---|
[382] | 60 | |
---|
[946] | 61 | $GLOBALS['phpgw_info']['server']['ldap_version3'] = true; |
---|
[1504] | 62 | |
---|
| 63 | if( $this->ldap_user && $this->ldap_pass ) |
---|
| 64 | $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false ); |
---|
| 65 | else |
---|
| 66 | $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_context , "", false ); |
---|
[382] | 67 | } |
---|
| 68 | |
---|
[946] | 69 | private final function ldapRoot() |
---|
[551] | 70 | { |
---|
[946] | 71 | $this->ldapConn(); |
---|
| 72 | } |
---|
[551] | 73 | |
---|
[946] | 74 | private final function ldapCatalog() |
---|
| 75 | { |
---|
| 76 | $version3 = true; |
---|
| 77 | $refer = true; |
---|
| 78 | |
---|
| 79 | if(!function_exists('ldap_connect')) |
---|
| 80 | return false; |
---|
[551] | 81 | |
---|
[946] | 82 | if(!$conn = ldap_connect($this->ldap_host)) |
---|
| 83 | return false; |
---|
| 84 | |
---|
| 85 | if( $version3 ) |
---|
| 86 | if( !ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3) ) |
---|
| 87 | $version3 = false; |
---|
| 88 | |
---|
| 89 | ldap_set_option($conn, LDAP_OPT_REFERRALS, $refer); |
---|
| 90 | |
---|
| 91 | // Bind as Admin |
---|
[1160] | 92 | if($this->ldap_user && $this->ldap_pass && !ldap_bind($conn, $this->ldap_user . "," .$this->ldap_context, $this->ldap_pass)) |
---|
[946] | 93 | return false; |
---|
| 94 | |
---|
| 95 | // Bind as Anonymous |
---|
[1160] | 96 | if(!$this->ldap_user && !$this->ldap_pass && !@ldap_bind($conn)) |
---|
[946] | 97 | return false; |
---|
[1160] | 98 | |
---|
[946] | 99 | return $conn; |
---|
| 100 | } |
---|
| 101 | |
---|
[1757] | 102 | public final function getGroupsLdap($pData) |
---|
[946] | 103 | { |
---|
[1160] | 104 | $result_groups = ""; |
---|
[1757] | 105 | |
---|
| 106 | if( $pData['serverLdap'] == $this->ldap_host || $pData['serverLdap'] == 'localhost' ) |
---|
| 107 | { |
---|
| 108 | $this->ldapRoot(); |
---|
| 109 | } |
---|
| 110 | else |
---|
| 111 | { |
---|
| 112 | $confHosts = $this->hostsJabber; |
---|
| 113 | |
---|
| 114 | for($i = 0; $i < count($confHosts); $i++ ) |
---|
| 115 | { |
---|
| 116 | if( $pData['serverLdap'] == $confHosts[$i]['serverLdap'] ) |
---|
| 117 | { |
---|
| 118 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
| 119 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
| 120 | $this->ldap_user = $confHosts[$i]['user']; |
---|
| 121 | $this->ldap_org = $confHosts[$i]['org']; |
---|
| 122 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
| 123 | |
---|
| 124 | $this->ldap = $this->ldapCatalog(); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | } |
---|
[946] | 128 | |
---|
[1757] | 129 | if( $this->ldap ) |
---|
[551] | 130 | { |
---|
[1757] | 131 | if( !$pData['search'] && $pData['ou'] != "-1" ) |
---|
| 132 | { |
---|
| 133 | $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))"; |
---|
| 134 | $justthese = array("cn","gidNumber"); |
---|
| 135 | $search = ldap_list( $this->ldap, $pData['ou'] , $filter, $justthese ); |
---|
| 136 | $entry = ldap_get_entries( $this->ldap, $search ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if( $pData['search'] ) |
---|
| 140 | { |
---|
| 141 | $filter = "(&(phpgwAccountType=g)(&(objectClass=posixGroup)(cn=".$pData['search']."*)))"; |
---|
| 142 | $justthese = array("cn","gidNumber"); |
---|
| 143 | $search = ldap_search( $this->ldap, $this->ldap_context , $filter, $justthese ); |
---|
| 144 | $entry = ldap_get_entries( $this->ldap, $search ); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | if( $entry && $entry['count'] > 0 ) |
---|
| 148 | { |
---|
| 149 | array_shift($entry); |
---|
[946] | 150 | |
---|
| 151 | foreach($entry as $tmp) |
---|
[1757] | 152 | $groups[] = $tmp['cn'][0]."/".$tmp['gidnumber'][0]; |
---|
[946] | 153 | |
---|
[1757] | 154 | natsort($groups); |
---|
| 155 | |
---|
| 156 | $result_groups = "<ldap>"; |
---|
| 157 | foreach($groups as $gtmp) |
---|
| 158 | { |
---|
| 159 | $tmp = explode("/",$gtmp); |
---|
| 160 | $result_groups .= "<org><cn>".$tmp[0]."</cn><gid>".$tmp[1]."</gid></org>"; |
---|
| 161 | } |
---|
| 162 | $result_groups .= "</ldap>"; |
---|
[946] | 163 | } |
---|
[551] | 164 | } |
---|
[1757] | 165 | |
---|
[946] | 166 | return $result_groups; |
---|
[551] | 167 | } |
---|
| 168 | |
---|
[1757] | 169 | public final function getGroupsMemberUid( $pGroup, $pLdap ) |
---|
[697] | 170 | { |
---|
[1757] | 171 | if( $pLdap == $this->ldap_host || $pLdap == 'localhost' ) |
---|
[697] | 172 | { |
---|
[1757] | 173 | $this->ldapRoot(); |
---|
[697] | 174 | |
---|
[1757] | 175 | if( $this->ldap ) |
---|
| 176 | { |
---|
| 177 | $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))"; |
---|
| 178 | $justthese = array("memberuid","gidnumber"); |
---|
| 179 | $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese); |
---|
| 180 | $result = ldap_get_entries($this->ldap,$search); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | $confHosts = $this->hostsJabber; |
---|
[697] | 186 | |
---|
[1757] | 187 | for($i = 0; $i < count($confHosts); $i++ ) |
---|
| 188 | { |
---|
| 189 | if( $this->ldap ) |
---|
| 190 | ldap_close($this->ldap); |
---|
| 191 | |
---|
| 192 | if( $pLdap == $confHosts[$i]['serverLdap'] ) |
---|
| 193 | { |
---|
| 194 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
| 195 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
| 196 | $this->ldap_user = $confHosts[$i]['user']; |
---|
| 197 | $this->ldap_org = $confHosts[$i]['org']; |
---|
| 198 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
| 199 | |
---|
| 200 | $this->ldap = $this->ldapCatalog(); |
---|
| 201 | |
---|
| 202 | if( $this->ldap ) |
---|
| 203 | { |
---|
| 204 | $filter = "(&(objectclass=posixgroup)(cn=".$pGroup."))"; |
---|
| 205 | $justthese = array("memberuid","gidnumber"); |
---|
| 206 | $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese); |
---|
| 207 | $result = ldap_get_entries($this->ldap,$search); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if( $result['count'] > 0 ) |
---|
| 214 | { |
---|
| 215 | array_shift($result[0]['memberuid']); |
---|
| 216 | return $result[0]['memberuid']; |
---|
| 217 | } |
---|
| 218 | |
---|
[697] | 219 | return false; |
---|
| 220 | } |
---|
| 221 | |
---|
[1757] | 222 | public final function getOrganizationsLdap($pLdap_host) |
---|
[551] | 223 | { |
---|
[1757] | 224 | |
---|
| 225 | if( $pLdap_host == $this->ldap_host || $pLdap_host == 'localhost' ) |
---|
| 226 | { |
---|
| 227 | $this->ldapRoot(); |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | $confHosts = $this->hostsJabber; |
---|
| 232 | |
---|
| 233 | for($i = 0; $i < count($confHosts); $i++ ) |
---|
| 234 | { |
---|
| 235 | if( $pLdap_host == $confHosts[$i]['serverLdap'] ) |
---|
| 236 | { |
---|
| 237 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
| 238 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
| 239 | $this->ldap_user = $confHosts[$i]['user']; |
---|
| 240 | $this->ldap_org = $confHosts[$i]['org']; |
---|
| 241 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
| 242 | |
---|
| 243 | $this->ldap = $this->ldapCatalog(); |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | } |
---|
[1160] | 247 | |
---|
[551] | 248 | if( $this->ldap ) |
---|
| 249 | { |
---|
[1504] | 250 | $filter = "(ou=*)"; |
---|
| 251 | $justthese = array("dn"); |
---|
| 252 | $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese); |
---|
| 253 | $info = ldap_get_entries($this->ldap, $search); |
---|
| 254 | |
---|
| 255 | for ($i=0; $i<$info["count"]; $i++) |
---|
[1757] | 256 | { |
---|
| 257 | $a_sectors[] = $info[$i]['dn']; |
---|
| 258 | } |
---|
[946] | 259 | } |
---|
[551] | 260 | |
---|
[1504] | 261 | // Retiro o count do array info e inverto o array para ordenação. |
---|
| 262 | foreach ($a_sectors as $context) |
---|
| 263 | { |
---|
| 264 | $array_dn = ldap_explode_dn ( $context, 1 ); |
---|
| 265 | $array_dn_reverse = array_reverse ( $array_dn, true ); |
---|
| 266 | array_pop ( $array_dn_reverse ); |
---|
| 267 | $inverted_dn[$context] = implode ( "#", $array_dn_reverse ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // Ordenação |
---|
| 271 | natcasesort($inverted_dn); |
---|
[551] | 272 | |
---|
[1504] | 273 | foreach ( $inverted_dn as $dn=>$invert_ufn ) |
---|
| 274 | { |
---|
| 275 | $display = ''; |
---|
[1438] | 276 | |
---|
[1504] | 277 | $array_dn_reverse = explode ( "#", $invert_ufn ); |
---|
| 278 | $array_dn = array_reverse ( $array_dn_reverse, true ); |
---|
[1438] | 279 | |
---|
[1504] | 280 | $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1); |
---|
| 281 | |
---|
| 282 | if ($level == 0) |
---|
| 283 | $display .= '+'; |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | for( $i = 0; $i < $level; $i++) |
---|
| 287 | $display .= '---'; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | reset ( $array_dn ); |
---|
| 291 | $display .= ' ' . (current ( $array_dn ) ); |
---|
| 292 | |
---|
| 293 | $dn = trim(strtolower($dn)); |
---|
| 294 | $options[$dn] = $display; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | return $options; |
---|
| 298 | |
---|
| 299 | } |
---|
| 300 | |
---|
[1757] | 301 | public final function getUsersLdapCatalog( $search, $uid = false, $pLdap = false ) |
---|
[382] | 302 | { |
---|
[946] | 303 | $confHosts = $this->hostsJabber; |
---|
| 304 | $result = array(); |
---|
| 305 | $return = array(); |
---|
| 306 | $conn = ""; |
---|
| 307 | |
---|
[1757] | 308 | for( $i = 0; $i < count($confHosts); $i++ ) |
---|
[382] | 309 | { |
---|
[1757] | 310 | if( $this->ldap ) |
---|
| 311 | ldap_close($this->ldap); |
---|
[951] | 312 | |
---|
[1757] | 313 | if( $pLdap && $pLdap == $confHosts[$i]['serverLdap'] ) |
---|
| 314 | { |
---|
| 315 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
| 316 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
| 317 | $this->ldap_user = $confHosts[$i]['user']; |
---|
| 318 | $this->ldap_org = $confHosts[$i]['org']; |
---|
| 319 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
| 320 | $this->ldap = $this->ldapCatalog(); |
---|
| 321 | } |
---|
| 322 | else |
---|
| 323 | { |
---|
| 324 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
| 325 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
| 326 | $this->ldap_user = $confHosts[$i]['user']; |
---|
| 327 | $this->ldap_org = $confHosts[$i]['org']; |
---|
| 328 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
| 329 | $this->ldap = $this->ldapCatalog(); |
---|
| 330 | } |
---|
[382] | 331 | |
---|
[1757] | 332 | if( $this->ldap ) |
---|
[382] | 333 | { |
---|
[1757] | 334 | $filter = ( $uid ) ? "(&(phpgwaccounttype=u)(|".$uid.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))"; |
---|
| 335 | $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto"); |
---|
| 336 | $searchRoot = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context; |
---|
| 337 | $search1 = @ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1); |
---|
| 338 | $entry1 = @ldap_get_entries( $this->ldap, $search1 ); |
---|
| 339 | $result = $this->resultArray( $entry1, $this->ldap ); |
---|
| 340 | |
---|
[946] | 341 | if( count($return) > 0 ) |
---|
| 342 | $return = array_merge($return, $result); |
---|
| 343 | else |
---|
| 344 | $return = $result; |
---|
[382] | 345 | } |
---|
| 346 | } |
---|
[946] | 347 | |
---|
| 348 | return $return; |
---|
[382] | 349 | } |
---|
[946] | 350 | |
---|
[988] | 351 | public final function getUsersLdapRoot( $search, $uidnumber, $ous = false ) |
---|
[417] | 352 | { |
---|
[1757] | 353 | $result = array(); |
---|
[946] | 354 | $this->ldapRoot(); |
---|
[519] | 355 | |
---|
| 356 | if( $this->ldap ) |
---|
| 357 | { |
---|
[1757] | 358 | $searchRoot = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ; |
---|
| 359 | $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))"; |
---|
| 360 | $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto"); |
---|
| 361 | $search = ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1); |
---|
| 362 | $entry = ldap_get_entries( $this->ldap, $search ); |
---|
| 363 | $result = $this->resultArray($entry, $this->ldap ); |
---|
[946] | 364 | } |
---|
[519] | 365 | |
---|
[946] | 366 | return $result; |
---|
[417] | 367 | } |
---|
[563] | 368 | |
---|
[946] | 369 | private final function resultArray($pArray, $pConn) |
---|
[563] | 370 | { |
---|
[946] | 371 | $entry = $pArray; |
---|
| 372 | $result = array(); |
---|
| 373 | |
---|
| 374 | $j = 0; |
---|
| 375 | for($i = 0 ; $i < $entry['count']; $i++) |
---|
[563] | 376 | { |
---|
[946] | 377 | if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' ) |
---|
| 378 | { |
---|
| 379 | $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0]; |
---|
[1504] | 380 | $result[$j]['mail'] = $entry[$i]['mail'][0]; |
---|
| 381 | $result[$j]['uid'] = $entry[$i]['uid'][0]; |
---|
| 382 | $result[$j]['jid'] = $entry[$i]['uid'][0]; |
---|
[946] | 383 | $ou = explode("dc=", $entry[$i]['dn']); |
---|
| 384 | $ou = explode("ou=",$ou[0]); |
---|
| 385 | $ou = array_pop($ou); |
---|
| 386 | $result[$j]['ou'] = strtoupper(substr($ou,0,strlen($ou)-1)); |
---|
| 387 | if( $entry[$i]['jpegphoto'][0] ) |
---|
| 388 | { |
---|
| 389 | $result[$j]['photo'] = "1"; |
---|
| 390 | $filterPhoto = "(objectclass=*)"; |
---|
| 391 | $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto")); |
---|
| 392 | $firstEntry = ldap_first_entry($pConn, $photoLdap); |
---|
| 393 | $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto"); |
---|
[1504] | 394 | $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0]; |
---|
[946] | 395 | } |
---|
| 396 | else |
---|
| 397 | $result[$j]['photo'] = "0"; |
---|
[563] | 398 | |
---|
[946] | 399 | $result[$j++]['cn'] = $entry[$i]['cn'][0]; |
---|
[563] | 400 | } |
---|
[1504] | 401 | |
---|
| 402 | $organization = $this->attr_org; |
---|
| 403 | |
---|
| 404 | if(is_array($organization)) |
---|
[946] | 405 | { |
---|
[1504] | 406 | foreach($organization as $attr) |
---|
| 407 | { |
---|
| 408 | $tmp = explode(";",$attr); |
---|
| 409 | if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) ) |
---|
| 410 | { |
---|
| 411 | switch(strtolower(trim($tmp[1]))) |
---|
| 412 | { |
---|
| 413 | case "mail" : |
---|
| 414 | $uid = $result[$i]['mail']; |
---|
| 415 | $uid = substr($uid,0,strpos($uid,"@")); |
---|
| 416 | $result[$i]['uid'] = $uid; |
---|
| 417 | break; |
---|
| 418 | |
---|
| 419 | case "description" : |
---|
| 420 | // SERPRO |
---|
| 421 | // parte antes do arroba; |
---|
| 422 | $result[$i]['uid'] = $result[$i]['description']; |
---|
| 423 | break; |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | } |
---|
[946] | 427 | } |
---|
[563] | 428 | } |
---|
[946] | 429 | return $result; |
---|
[563] | 430 | } |
---|
[382] | 431 | } |
---|
| 432 | |
---|
[1504] | 433 | ?> |
---|