[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 | |
---|
| 13 | define('PHPGW_INCLUDE_ROOT', '../'); |
---|
| 14 | define('PHPGW_API_INC','../phpgwapi/inc'); |
---|
| 15 | require_once( PHPGW_API_INC . '/class.common.inc.php'); |
---|
| 16 | |
---|
| 17 | class ldap_im |
---|
| 18 | { |
---|
[1504] | 19 | private $attr_org; |
---|
[946] | 20 | private $common; |
---|
| 21 | private $hostsJabber; |
---|
[382] | 22 | private $ldap; |
---|
| 23 | private $ldap_context; |
---|
[1160] | 24 | private $ldap_user; |
---|
[946] | 25 | private $ldap_host; |
---|
[951] | 26 | private $ldap_org; |
---|
[382] | 27 | private $ldap_pass; |
---|
| 28 | private $max_result; |
---|
| 29 | |
---|
| 30 | public final function __construct() |
---|
| 31 | { |
---|
[1504] | 32 | // Attributes org ldap; |
---|
| 33 | $this->attr_org = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['attributes_org_ldap_jabberit']); |
---|
| 34 | |
---|
[946] | 35 | // Hosts Jabber |
---|
| 36 | $this->hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']); |
---|
| 37 | |
---|
| 38 | // Result Ldap |
---|
| 39 | $this->max_result = 30; |
---|
[382] | 40 | } |
---|
| 41 | |
---|
| 42 | public final function __destruct() |
---|
| 43 | { |
---|
| 44 | if( $this->ldap ) |
---|
| 45 | ldap_close($this->ldap); |
---|
| 46 | } |
---|
| 47 | |
---|
[946] | 48 | private final function ldapConn() |
---|
[382] | 49 | { |
---|
[946] | 50 | $this->common = new common(); |
---|
[382] | 51 | |
---|
[946] | 52 | $GLOBALS['phpgw_info']['server']['ldap_version3'] = true; |
---|
[1504] | 53 | |
---|
| 54 | if( $this->ldap_user && $this->ldap_pass ) |
---|
| 55 | $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false ); |
---|
| 56 | else |
---|
| 57 | $this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_context , "", false ); |
---|
[382] | 58 | } |
---|
| 59 | |
---|
[946] | 60 | private final function ldapRoot() |
---|
[551] | 61 | { |
---|
[1504] | 62 | $this->ldap_host = $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit']; |
---|
| 63 | $this->ldap_context = $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit']; |
---|
| 64 | $this->ldap_user = $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit']; |
---|
| 65 | $this->ldap_pass = $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit']; |
---|
[551] | 66 | |
---|
[946] | 67 | $this->ldapConn(); |
---|
| 68 | } |
---|
[551] | 69 | |
---|
[946] | 70 | private final function ldapCatalog() |
---|
| 71 | { |
---|
| 72 | $version3 = true; |
---|
| 73 | $refer = true; |
---|
| 74 | |
---|
| 75 | if(!function_exists('ldap_connect')) |
---|
| 76 | return false; |
---|
[551] | 77 | |
---|
[946] | 78 | if(!$conn = ldap_connect($this->ldap_host)) |
---|
| 79 | return false; |
---|
| 80 | |
---|
| 81 | if( $version3 ) |
---|
| 82 | if( !ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3) ) |
---|
| 83 | $version3 = false; |
---|
| 84 | |
---|
| 85 | ldap_set_option($conn, LDAP_OPT_REFERRALS, $refer); |
---|
| 86 | |
---|
| 87 | // Bind as Admin |
---|
[1160] | 88 | if($this->ldap_user && $this->ldap_pass && !ldap_bind($conn, $this->ldap_user . "," .$this->ldap_context, $this->ldap_pass)) |
---|
[946] | 89 | return false; |
---|
| 90 | |
---|
| 91 | // Bind as Anonymous |
---|
[1160] | 92 | if(!$this->ldap_user && !$this->ldap_pass && !@ldap_bind($conn)) |
---|
[946] | 93 | return false; |
---|
[1160] | 94 | |
---|
[946] | 95 | return $conn; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public final function getGroupsLdap($pOrg) |
---|
| 99 | { |
---|
| 100 | $this->ldapRoot(); |
---|
[1160] | 101 | $result_groups = ""; |
---|
[946] | 102 | |
---|
[1160] | 103 | if( $this->ldap && $pOrg['ou'] != "-1") |
---|
[551] | 104 | { |
---|
[946] | 105 | $organization = 'ou=' . $pOrg['ou'] .",". $this->ldap_context; |
---|
| 106 | $filter = "(&(phpgwAccountType=g)(objectClass=posixGroup))"; |
---|
| 107 | $justthese = array("cn","gidNumber"); |
---|
| 108 | $search = ldap_list($this->ldap, $organization, $filter, $justthese); |
---|
| 109 | $entry = ldap_get_entries( $this->ldap, $search ); |
---|
| 110 | |
---|
| 111 | if( $entry ) |
---|
| 112 | { |
---|
| 113 | $result_groups = "<ldap>"; |
---|
| 114 | foreach($entry as $tmp) |
---|
| 115 | if( $tmp['gidnumber'][0] != "" ) |
---|
| 116 | $result_groups .= "<org><cn>".$tmp['cn'][0]."</cn><gid>".$tmp['gidnumber'][0]."</gid></org>"; |
---|
| 117 | |
---|
| 118 | $result_groups .= "</ldap>"; |
---|
| 119 | } |
---|
[1160] | 120 | |
---|
| 121 | return $result_groups; |
---|
[551] | 122 | } |
---|
[1160] | 123 | |
---|
[946] | 124 | return $result_groups; |
---|
[551] | 125 | } |
---|
| 126 | |
---|
[946] | 127 | public final function getGroupsMemberUid($pGroup) |
---|
[697] | 128 | { |
---|
[946] | 129 | $this->ldapRoot(); |
---|
[697] | 130 | |
---|
| 131 | if( $this->ldap ) |
---|
| 132 | { |
---|
| 133 | $filter = "(&(objectclass=posixgroup)(|".$pGroup."))"; |
---|
| 134 | $justthese = array("dn","memberuid","gidnumber"); |
---|
| 135 | $search = ldap_search($this->ldap,$this->ldap_context,$filter, $justthese); |
---|
| 136 | $result = ldap_get_entries($this->ldap,$search); |
---|
| 137 | |
---|
| 138 | if( $result['count'] > 0 ) |
---|
| 139 | return $result; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
[946] | 145 | public final function getOrganizationsLdap() |
---|
[551] | 146 | { |
---|
[946] | 147 | $this->ldapRoot(); |
---|
[1160] | 148 | |
---|
[551] | 149 | if( $this->ldap ) |
---|
| 150 | { |
---|
[1504] | 151 | $filter = "(ou=*)"; |
---|
| 152 | $justthese = array("dn"); |
---|
| 153 | $search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese); |
---|
| 154 | $info = ldap_get_entries($this->ldap, $search); |
---|
| 155 | |
---|
| 156 | for ($i=0; $i<$info["count"]; $i++) |
---|
| 157 | $a_sectors[] = $info[$i]['dn']; |
---|
[946] | 158 | } |
---|
[551] | 159 | |
---|
[1504] | 160 | // Retiro o count do array info e inverto o array para ordenação. |
---|
| 161 | foreach ($a_sectors as $context) |
---|
| 162 | { |
---|
| 163 | $array_dn = ldap_explode_dn ( $context, 1 ); |
---|
| 164 | $array_dn_reverse = array_reverse ( $array_dn, true ); |
---|
| 165 | array_pop ( $array_dn_reverse ); |
---|
| 166 | $inverted_dn[$context] = implode ( "#", $array_dn_reverse ); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // Ordenação |
---|
| 170 | natcasesort($inverted_dn); |
---|
[551] | 171 | |
---|
[1504] | 172 | foreach ( $inverted_dn as $dn=>$invert_ufn ) |
---|
| 173 | { |
---|
| 174 | $display = ''; |
---|
[1438] | 175 | |
---|
[1504] | 176 | $array_dn_reverse = explode ( "#", $invert_ufn ); |
---|
| 177 | $array_dn = array_reverse ( $array_dn_reverse, true ); |
---|
[1438] | 178 | |
---|
[1504] | 179 | $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1); |
---|
| 180 | |
---|
| 181 | if ($level == 0) |
---|
| 182 | $display .= '+'; |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | for( $i = 0; $i < $level; $i++) |
---|
| 186 | $display .= '---'; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | reset ( $array_dn ); |
---|
| 190 | $display .= ' ' . (current ( $array_dn ) ); |
---|
| 191 | |
---|
| 192 | $dn = trim(strtolower($dn)); |
---|
| 193 | $options[$dn] = $display; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return $options; |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | |
---|
[988] | 200 | public final function getUsersLdapCatalog( $search ) |
---|
[382] | 201 | { |
---|
[946] | 202 | $confHosts = $this->hostsJabber; |
---|
| 203 | $result = array(); |
---|
| 204 | $return = array(); |
---|
| 205 | $conn = ""; |
---|
| 206 | |
---|
| 207 | for($i = 0; $i < count($confHosts); $i++ ) |
---|
[382] | 208 | { |
---|
[946] | 209 | $this->ldap_host = $confHosts[$i]['serverLdap']; |
---|
[1504] | 210 | $this->ldap_context = $confHosts[$i]['contextLdap']; |
---|
[1160] | 211 | $this->ldap_user = $confHosts[$i]['user']; |
---|
[951] | 212 | $this->ldap_org = $confHosts[$i]['org']; |
---|
[946] | 213 | $this->ldap_pass = $confHosts[$i]['password']; |
---|
[951] | 214 | |
---|
[946] | 215 | $conn = $this->ldapCatalog(); |
---|
[382] | 216 | |
---|
[946] | 217 | if( $conn ) |
---|
[382] | 218 | { |
---|
[946] | 219 | $filter = "(&(phpgwaccounttype=u)(".$search ."))"; |
---|
[1504] | 220 | $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto"); |
---|
[951] | 221 | $searchRoot = ( $this->ldap_org != "*" ) ? "ou=".$this->ldap_org.",".$this->ldap_context : $this->ldap_context; |
---|
[1160] | 222 | |
---|
[951] | 223 | $search1 = @ldap_search($conn, $searchRoot, $filter, $justthese, 0, $this->max_result + 1); |
---|
[946] | 224 | $entry1 = @ldap_get_entries( $conn, $search1 ); |
---|
| 225 | $result = $this->resultArray($entry1, $conn ); |
---|
[1160] | 226 | |
---|
[946] | 227 | if( count($return) > 0 ) |
---|
| 228 | $return = array_merge($return, $result); |
---|
| 229 | else |
---|
| 230 | $return = $result; |
---|
[382] | 231 | |
---|
[946] | 232 | unset($result); |
---|
[382] | 233 | |
---|
[946] | 234 | ldap_close($conn); |
---|
[382] | 235 | } |
---|
| 236 | } |
---|
[946] | 237 | |
---|
| 238 | return $return; |
---|
[382] | 239 | } |
---|
[946] | 240 | |
---|
[988] | 241 | public final function getUsersLdapRoot( $search, $uidnumber, $ous = false ) |
---|
[417] | 242 | { |
---|
[946] | 243 | $this->ldapRoot(); |
---|
| 244 | $result = array(); |
---|
[519] | 245 | |
---|
| 246 | if( $this->ldap ) |
---|
| 247 | { |
---|
[946] | 248 | $searchRoot = ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ; |
---|
| 249 | $filter = "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))"; |
---|
[1504] | 250 | $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn","jpegPhoto"); |
---|
[946] | 251 | $search = ldap_search($this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1); |
---|
| 252 | $entry = ldap_get_entries( $this->ldap, $search ); |
---|
[519] | 253 | |
---|
[946] | 254 | $result = $this->resultArray($entry, $this->ldap ); |
---|
| 255 | } |
---|
[519] | 256 | |
---|
[946] | 257 | return $result; |
---|
[417] | 258 | } |
---|
[563] | 259 | |
---|
[946] | 260 | private final function resultArray($pArray, $pConn) |
---|
[563] | 261 | { |
---|
[946] | 262 | $entry = $pArray; |
---|
| 263 | $result = array(); |
---|
| 264 | |
---|
| 265 | $j = 0; |
---|
| 266 | for($i = 0 ; $i < $entry['count']; $i++) |
---|
[563] | 267 | { |
---|
[946] | 268 | if ( $entry[$i]['phpgwaccountvisible'][0] != '-1' ) |
---|
| 269 | { |
---|
| 270 | $result[$j]['uidnumber'] = $entry[$i]['uidnumber'][0]; |
---|
[1504] | 271 | $result[$j]['mail'] = $entry[$i]['mail'][0]; |
---|
| 272 | $result[$j]['uid'] = $entry[$i]['uid'][0]; |
---|
| 273 | $result[$j]['jid'] = $entry[$i]['uid'][0]; |
---|
[946] | 274 | $ou = explode("dc=", $entry[$i]['dn']); |
---|
| 275 | $ou = explode("ou=",$ou[0]); |
---|
| 276 | $ou = array_pop($ou); |
---|
| 277 | $result[$j]['ou'] = strtoupper(substr($ou,0,strlen($ou)-1)); |
---|
| 278 | if( $entry[$i]['jpegphoto'][0] ) |
---|
| 279 | { |
---|
| 280 | $result[$j]['photo'] = "1"; |
---|
| 281 | $filterPhoto = "(objectclass=*)"; |
---|
| 282 | $photoLdap = ldap_read($pConn, $entry[$i]['dn'], $filterPhoto, array("jpegPhoto")); |
---|
| 283 | $firstEntry = ldap_first_entry($pConn, $photoLdap); |
---|
| 284 | $photo = ldap_get_values_len($pConn, $firstEntry, "jpegPhoto"); |
---|
[1504] | 285 | $_SESSION['phpgw_info']['jabberit_messenger']['photo'][trim($result[$j]['ou'])][trim($result[$j]['uid'])] = $photo[0]; |
---|
[946] | 286 | } |
---|
| 287 | else |
---|
| 288 | $result[$j]['photo'] = "0"; |
---|
[563] | 289 | |
---|
[946] | 290 | $result[$j++]['cn'] = $entry[$i]['cn'][0]; |
---|
[563] | 291 | } |
---|
[1504] | 292 | |
---|
| 293 | $organization = $this->attr_org; |
---|
| 294 | |
---|
| 295 | if(is_array($organization)) |
---|
[946] | 296 | { |
---|
[1504] | 297 | foreach($organization as $attr) |
---|
| 298 | { |
---|
| 299 | $tmp = explode(";",$attr); |
---|
| 300 | if( strtolower(trim($tmp[0])) == strtolower(trim($result[$i]['dn'])) ) |
---|
| 301 | { |
---|
| 302 | switch(strtolower(trim($tmp[1]))) |
---|
| 303 | { |
---|
| 304 | case "mail" : |
---|
| 305 | $uid = $result[$i]['mail']; |
---|
| 306 | $uid = substr($uid,0,strpos($uid,"@")); |
---|
| 307 | $result[$i]['uid'] = $uid; |
---|
| 308 | break; |
---|
| 309 | |
---|
| 310 | case "description" : |
---|
| 311 | // SERPRO |
---|
| 312 | // parte antes do arroba; |
---|
| 313 | $result[$i]['uid'] = $result[$i]['description']; |
---|
| 314 | break; |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | } |
---|
[946] | 318 | } |
---|
[563] | 319 | } |
---|
[946] | 320 | return $result; |
---|
[563] | 321 | } |
---|
[382] | 322 | } |
---|
| 323 | |
---|
[1504] | 324 | ?> |
---|