-
Notifications
You must be signed in to change notification settings - Fork 1
/
stanford.directory.php
271 lines (205 loc) · 5.85 KB
/
stanford.directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
// Include StanfordPerson
require_once(dirname(__FILE__) . "/stanford.person.php");
/**
* A class used for interacting with the directory at Stanford
*
* @author ddonahue
*
* @date July 23, 2008
*
* Copyright 2008,2009 Board of Trustees, Leland Stanford Jr. University
* See LICENSE for licensing terms.
*
*/
class StanfordDirectory {
const VERSION = '1.0.0';
const LDAP_SERVER = 'ldap.stanford.edu';
public $ldap; // LDAP connection handle
public $error; // Error message
/**
* Creates a new StanfordDirectory object
*/
function __construct() {
$this->ldap = 0; // Connect only when necessary
$this->error = '';
}
/**
* Deconstructor calls disconnect
*/
function __destruct() {
$this->disconnect();
}
/**
* Gets the version number of the class
*
* @return string The version number
*/
function get_version() {
return self::VERSION;
}
/**
* Connects and binds to the LDAP server
*
* @throws An Exception when ldap_connect, ldap_bind, or ldap_set_option fail
*
* @return void
*/
function connect_and_bind() {
// Check if already connected
if($this->is_connected()) return;
// Connect to the LDAP server
$this->ldap = ldap_connect(StanfordDirectory::LDAP_SERVER);
if ($this->ldap == false) {
$this->error = 'Unable to connect to the directory';
throw new Exception('Unable to connect to the directory');
return;
}
// Bind
if(ldap_bind($this->ldap) == false) {
$this->error = 'Unable to bind to the directory';
throw new Exception('Unable to bind to the directory');
return;
}
// Set protocol version
if(ldap_set_option($this->ldap,LDAP_OPT_PROTOCOL_VERSION,3) == false) {
$this->error = 'Unable to set LDAP protocol version to 3';
throw new Exception('Unable to set LDAP protocol version');
return;
}
// Problematic...
// SASL bind
//if(ldap_sasl_bind($this->ldap,"","","GSSAPI") == false) {
// $error = 'Unable to perform SASL bind';
// throw new Exception('Unable to perform SASL bind');
// return;
//}
}
/**
* Closes the LDAP connection
*
*/
function disconnect() {
// Check ldap handle
if($this->ldap) {
// Close the connection
ldap_close($this->ldap);
// Reset the ldap variable
$this->ldap = 0;
}
}
/**
* Queries the directory for a single attribute for a particular SUNetID
*
* @param sunetid string SUNetID of the user to query
* @param attr string name of attribute to return
*
* @return string value of the queried attribute
*/
function get_attr($sunetid, $attr) {
$result = $this->get_user_info($sunetid, array($attr));
if(sizeof($result)) {
return $result[$attr][0];
}
else {
return false;
}
}
/**
* Gets information about a user and returns it as a StanfordPerson
*
* @param sunetid string SUNetID of the user to query
*
* @return StanfordPerson A StanfordPerson
*/
function get_person($sunetid) {
// Connect if necessary
if($this->is_connected() == false) $this->connect_and_bind();
// Check SUNetID
if($sunetid == '') return false;
// Return result
$person = new StanfordPerson($sunetid, $this);
// Force a directory query (to ensure that it's a valid SUNetID)
$person->fetch_info();
// Check result
if($person->get_search_status() == true) {
return $person;
}
else {
return false;
}
}
/**
* Returns all or limited information about a particular user given a SUNetID and an optional list of attributes
*
* @param sunetid string SUNetID of the user to query
* @param attributes array list of attributes to return
*
* @return array The dataset associated with the queried SUNetID
*/
function get_user_info($sunetid, $attributes=0) {
// Connect if necessary
if($this->is_connected() == false) $this->connect_and_bind();
$result = $this->search("uid=$sunetid", $attributes);
if($result) {
return $result[0];
}
else {
return array();
}
}
/**
* Checks if there is a connection to the directory
*
* @return boolean true if connected to LDAP server, false otherwise
*
*/
function is_connected() {
if($this->ldap) {
return true;
}
else {
return false;
}
}
/**
* Searches the directory given a filter and optional list of fields
*
* @param filter string LDAP search filter
* @param attributes array list of attributes to return
*
* @throws An Exception when unable to connect and bind to LDAP server or if search filter is null
*
* @return array The data returned from the search
*/
function search($filter, $attributes=0) {
// Connect if necessary
$this->connect_and_bind();
// Check arguments
if(!$this->is_connected()) {
throw new Exception("Unable to connect and bind to LDAP server");
}
// Check filter
if(!$filter) {
throw new Exception("Search filter is empty");
}
// Configure search parameters
$dn = "cn=people,dc=stanford,dc=edu";
if(!$attributes) $attributes = array();
// Search
$sr=@ldap_search($this->ldap, $dn, $filter, $attributes);
// Check search result
if($sr == FALSE) {
throw new Exception("ldap_search returned false, bad search filter");
}
// Number of entries returned from the search
$num_entries = ldap_count_entries($this->ldap, $sr);
if($num_entries > 0) {
return ldap_get_entries($this->ldap, $sr);
}
else {
return array();
}
}
};
?>