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