1 | <?php |
---|
2 | class ldap_im |
---|
3 | { |
---|
4 | private $ldap_host; |
---|
5 | private $ldap_context; |
---|
6 | private $conn; |
---|
7 | private $user; |
---|
8 | private $jid; |
---|
9 | private $password; |
---|
10 | |
---|
11 | function __construct() |
---|
12 | { |
---|
13 | try |
---|
14 | { |
---|
15 | if ( !file_exists(dirname(__FILE__) . '/../instant_messenger.define.php') ) |
---|
16 | throw new Exception(__CLASS__ . '[ ERROR #1 ] : Not found configuration file'); |
---|
17 | |
---|
18 | require_once dirname(__FILE__) . '/../instant_messenger.define.php'; |
---|
19 | |
---|
20 | if ( !(defined('IM_SERVER_LDAP_JABBER') && defined('IM_CONTEXT_LDAP_JABBER')) ) |
---|
21 | throw new Exception(__CLASS__ . '[ ERROR #2 ] : Ldap server is not cofigured'); |
---|
22 | |
---|
23 | if ( !defined('IM_USER_LDAP_JABBER') ) |
---|
24 | throw new Exception(__CLASS__ . '[ ERROR #3 ] : Ldap server is not cofigured'); |
---|
25 | |
---|
26 | $this->ldap_host = constant('IM_SERVER_LDAP_JABBER'); |
---|
27 | $this->ldap_context = constant('IM_CONTEXT_LDAP_JABBER'); |
---|
28 | $this->user = constant('IM_USER_LDAP_JABBER'); |
---|
29 | |
---|
30 | session_start(); |
---|
31 | $this->password = $_SESSION['phpgw_info']['instant_messenger']['password_ldap_jabber']; |
---|
32 | $this->jid = $_SESSION['phpgw_info']['instant_messenger']['user']; |
---|
33 | session_write_close(); |
---|
34 | |
---|
35 | $this->connect_ldap(false); |
---|
36 | |
---|
37 | } |
---|
38 | catch(Exception $e) |
---|
39 | { |
---|
40 | echo $e->getMessage(); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | function __destruct() |
---|
45 | { |
---|
46 | ldap_close($this->conn); |
---|
47 | } |
---|
48 | |
---|
49 | function connect_ldap($refer) |
---|
50 | { |
---|
51 | $this->conn = ldap_connect($this->ldap_host); |
---|
52 | ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3); |
---|
53 | ldap_set_option($this->conn, LDAP_OPT_REFERRALS, $refer); |
---|
54 | if(trim($this->user) != "") |
---|
55 | @ldap_bind($this->conn,$this->user,$this->password); |
---|
56 | else |
---|
57 | @ldap_bind($this->conn); |
---|
58 | } |
---|
59 | |
---|
60 | function get_photo_ldap($pUser,$getPhoto) |
---|
61 | { |
---|
62 | $user = ( $pUser == "im_avatar" ) ? $this->jid : $pUser ; |
---|
63 | |
---|
64 | if ($this->conn) |
---|
65 | { |
---|
66 | $filter="(&(uid=".$user.")(phpgwAccountType=u))"; |
---|
67 | $justthese = array("jpegphoto"); |
---|
68 | $search = @ldap_search($this->conn,$this->ldap_context, $filter, $justthese); |
---|
69 | $entry = @ldap_first_entry($this->conn, $search); |
---|
70 | $jpeg_data = @ldap_get_values_len($this->conn, $entry, "jpegphoto"); |
---|
71 | |
---|
72 | if( count($jpeg_data) > 1) |
---|
73 | { |
---|
74 | $photo = imagecreatefromstring($jpeg_data[0]); |
---|
75 | if($photo) |
---|
76 | { |
---|
77 | if($getPhoto) |
---|
78 | { |
---|
79 | header("Content-Type: image/jpeg"); |
---|
80 | $width = imagesx($photo); |
---|
81 | $height = imagesy($photo); |
---|
82 | $twidth = 49; |
---|
83 | $theight = 65; |
---|
84 | $small_photo = imagecreatetruecolor ($twidth, $theight); |
---|
85 | imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height); |
---|
86 | imagejpeg($small_photo,'',100); |
---|
87 | return; |
---|
88 | }else{ |
---|
89 | return "true"; |
---|
90 | } |
---|
91 | }else{ |
---|
92 | return "false"; |
---|
93 | } |
---|
94 | }else{ |
---|
95 | return "false"; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | function photo_ldap($user) |
---|
101 | { |
---|
102 | $uid = ( $user['uid'] == "im_avatar" ) ? $this->jid : $user['uid'] ; |
---|
103 | return $this->get_photo_ldap($uid,false); |
---|
104 | } |
---|
105 | |
---|
106 | function list_users_ldap($search) |
---|
107 | { |
---|
108 | |
---|
109 | if($this->conn){ |
---|
110 | $filter = "(&(".$search.")(phpgwAccountType=u))"; |
---|
111 | $justthese = array("uid","uidNumber","cn","mail","phpgwAccountVisible","dn"); |
---|
112 | $search = ldap_search($this->conn,$this->ldap_context,$filter,$justthese); |
---|
113 | $entry = ldap_get_entries($this->conn,$search); |
---|
114 | } |
---|
115 | |
---|
116 | if( $entry['count'] > 0){ |
---|
117 | $i = 0; |
---|
118 | $result_user = array(); |
---|
119 | foreach($entry as $tmp) |
---|
120 | { |
---|
121 | if($tmp['uidnumber'][0] != "" && $tmp['phpgwaccountvisible'][0] != "-1"){ |
---|
122 | $result_user[$i]['uidnumber'] = $tmp['uidnumber'][0]; |
---|
123 | $result_user[$i]['mail'] = $tmp['mail'][0]; |
---|
124 | $result_user[$i]['uid'] = $tmp['uid'][0]; |
---|
125 | $result_user[$i]['cn'] = $tmp['cn'][0]; |
---|
126 | $ou = explode("dc=",$tmp['dn']); |
---|
127 | $ou = explode("ou=",$ou[0]); |
---|
128 | $ou = array_pop($ou); |
---|
129 | $result_user[$i]['dn'] = strtoupper(substr($ou,0,strlen($ou)-1)); |
---|
130 | $i++; |
---|
131 | } |
---|
132 | } |
---|
133 | return $result_user; |
---|
134 | } |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | function list_groups_ldap($search) |
---|
139 | { |
---|
140 | |
---|
141 | if( $this->conn ){ |
---|
142 | $filter = "(&(".$search.")(objectClass=posixGroup))"; |
---|
143 | $justthese = array("gidnumber","cn","memberuid"); |
---|
144 | $search = ldap_search($this->conn,$this->ldap_context, $filter, $justthese); |
---|
145 | $entry = ldap_get_entries($this->conn,$search); |
---|
146 | } |
---|
147 | |
---|
148 | if($entry['count'] > 0){ |
---|
149 | $i = 0; |
---|
150 | $result_groups = array(); |
---|
151 | foreach($entry as $tmpg){ |
---|
152 | if($tmpg['gidnumber'] != ""){ |
---|
153 | if(array_key_exists("memberuid", $tmpg)){ |
---|
154 | $result_groups[$i]['gidnumber'] = $tmpg['gidnumber'][0]; |
---|
155 | @array_shift($tmpg['memberuid']); |
---|
156 | foreach($tmpg['memberuid'] as $tmp) |
---|
157 | $result_groups[$i]['members'][] = $tmp; |
---|
158 | $i++; |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | if(count($result_groups) > 0 ){ |
---|
165 | return $result_groups; |
---|
166 | }else{ |
---|
167 | return 0; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | function list_organizations_ldap() |
---|
172 | { |
---|
173 | $result_org = "<organizations>"; |
---|
174 | |
---|
175 | if ($this->conn) |
---|
176 | { |
---|
177 | $filter="ou=*"; |
---|
178 | $justthese = array("ou"); |
---|
179 | $search = ldap_list($this->conn,$this->ldap_context, $filter, $justthese); |
---|
180 | $entry = ldap_get_entries($this->conn, $search); |
---|
181 | } |
---|
182 | |
---|
183 | foreach($entry as $tmp) |
---|
184 | if($tmp['ou'][0] != "") |
---|
185 | $result_org .= "<ou>" . $tmp['ou'][0] . "</ou>"; |
---|
186 | |
---|
187 | $result_org .= "</organizations>"; |
---|
188 | |
---|
189 | return $result_org; |
---|
190 | } |
---|
191 | |
---|
192 | function info_user($user) |
---|
193 | { |
---|
194 | $uid = "uid=".$user['uid']; |
---|
195 | $i= 0; |
---|
196 | |
---|
197 | $entry = $this->list_users_ldap($uid); |
---|
198 | |
---|
199 | if( count($entry) > 0 ) |
---|
200 | return $entry; |
---|
201 | else |
---|
202 | return false; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | if(trim($_REQUEST['user']) != "") |
---|
207 | { |
---|
208 | $obj_img = new ldap_im(); |
---|
209 | $obj_img->get_photo_ldap($_REQUEST['user'],true); |
---|
210 | } |
---|
211 | |
---|
212 | ?> |
---|