]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-ldap.inc.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-ldap.inc.php
1 <?php
2 /*
3   V4.22 15 Apr 2004  (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved.
4    Released under both BSD license and Lesser GPL library license.
5   Whenever there is any discrepancy between the two licenses,
6   the BSD license will take precedence.
7   Set tabs to 8.
8
9   Joshua Eldridge (joshuae74#hotmail.com)
10 */
11
12 class ADODB_ldap extends ADOConnection
13 {
14     var $databaseType = 'ldap';
15     var $dataProvider = 'ldap';
16
17     # Connection information
18     var $username = false;
19     var $password = false;
20
21     # Used during searches
22     var $filter;
23     var $dn;
24
25     function ADODB_ldap()
26     {
27
28     }
29
30     // returns true or false
31
32     function _connect($host, $username, $password, $ldapbase)
33     {
34
35         if (!function_exists('ldap_connect')) return false;
36
37         $conn_info = array($host);
38
39         if (strstr($host, ':')) {
40             $conn_info = explode(':', $host);
41         }
42
43         $this->_connectionID = ldap_connect($conn_info[0], $conn_info[1])
44             or die('Could not connect to ' . $this->_connectionID);
45         if ($username && $password) {
46             $bind = ldap_bind($this->_connectionID, $username, $password)
47                 or die('Could not bind to ' . $this->_connectionID . ' with $username & $password');
48         } else {
49             $bind = ldap_bind($this->_connectionID)
50                 or die('Could not bind anonymously to ' . $this->_connectionID);
51         }
52         return $this->_connectionID;
53     }
54
55     /* returns _queryID or false */
56     function _query($sql, $inputarr)
57     {
58         $rs = ldap_search($this->_connectionID, $this->database, $sql);
59         return $rs;
60
61     }
62
63     /* closes the LDAP connection */
64     function _close()
65     {
66         @ldap_close($this->_connectionID);
67         $this->_connectionID = false;
68     }
69
70     function ServerInfo()
71     {
72         if (is_array($this->version)) return $this->version;
73         $version = array();
74         /*
75         Determines how aliases are handled during search.
76         LDAP_DEREF_NEVER (0x00)
77         LDAP_DEREF_SEARCHING (0x01)
78         LDAP_DEREF_FINDING (0x02)
79         LDAP_DEREF_ALWAYS (0x03)
80         The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
81         not when locating the base object of the search. The LDAP_DEREF_FINDING value means
82         aliases are dereferenced when locating the base object but not during the search.
83         Default: LDAP_DEREF_NEVER
84         */
85         ldap_get_option($this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF']);
86         switch ($version['LDAP_OPT_DEREF']) {
87             case 0:
88                 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
89             case 1:
90                 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
91             case 2:
92                 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
93             case 3:
94                 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
95         }
96
97         /*
98         A limit on the number of entries to return from a search.
99         LDAP_NO_LIMIT (0) means no limit.
100         Default: LDAP_NO_LIMIT
101         */
102         ldap_get_option($this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT']);
103         if ($version['LDAP_OPT_SIZELIMIT'] == 0) {
104             $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
105         }
106
107         /*
108         A limit on the number of seconds to spend on a search.
109         LDAP_NO_LIMIT (0) means no limit.
110         Default: LDAP_NO_LIMIT
111         */
112         ldap_get_option($this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT']);
113         if ($version['LDAP_OPT_TIMELIMIT'] == 0) {
114             $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
115         }
116
117         /*
118         Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
119         LDAP_OPT_ON
120         LDAP_OPT_OFF
121         Default: ON
122         */
123         ldap_get_option($this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS']);
124         if ($version['LDAP_OPT_REFERRALS'] == 0) {
125             $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
126         } else {
127             $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
128
129         }
130         /*
131         Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
132         LDAP_OPT_ON
133         LDAP_OPT_OFF
134         Default: OFF
135         */
136         ldap_get_option($this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART']);
137         if ($version['LDAP_OPT_RESTART'] == 0) {
138             $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
139         } else {
140             $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
141
142         }
143         /*
144         This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
145         LDAP_VERSION2 (2)
146         LDAP_VERSION3 (3)
147         Default: LDAP_VERSION2 (2)
148         */
149         ldap_get_option($this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION']);
150         if ($version['LDAP_OPT_PROTOCOL_VERSION'] == 2) {
151             $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
152         } else {
153             $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
154
155         }
156         /* The host name (or list of hosts) for the primary LDAP server. */
157         ldap_get_option($this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME']);
158         ldap_get_option($this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER']);
159         ldap_get_option($this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING']);
160         ldap_get_option($this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN']);
161
162         return $this->version = $version;
163
164     }
165 }
166
167 /*--------------------------------------------------------------------------------------
168      Class Name: Recordset
169 --------------------------------------------------------------------------------------*/
170
171 class ADORecordSet_ldap extends ADORecordSet
172 {
173
174     var $databaseType = "ldap";
175     var $canSeek = false;
176     var $_entryID; /* keeps track of the entry resource identifier */
177
178     function ADORecordSet_ldap($queryID, $mode = false)
179     {
180         if ($mode === false) {
181             global $ADODB_FETCH_MODE;
182             $mode = $ADODB_FETCH_MODE;
183         }
184         switch ($mode) {
185             case ADODB_FETCH_NUM:
186                 $this->fetchMode = LDAP_NUM;
187                 break;
188             case ADODB_FETCH_ASSOC:
189                 $this->fetchMode = LDAP_ASSOC;
190                 break;
191             default:
192             case ADODB_FETCH_DEFAULT:
193             case ADODB_FETCH_BOTH:
194                 $this->fetchMode = LDAP_BOTH;
195                 break;
196         }
197
198         $this->ADORecordSet($queryID);
199     }
200
201     function _initrs()
202     {
203         /*
204         This could be teaked to respect the $COUNTRECS directive from ADODB
205         It's currently being used in the _fetch() function and the
206         GetAssoc() function
207         */
208         $this->_numOfRows = ldap_count_entries($this->connection->_connectionID, $this->_queryID);
209
210     }
211
212     /*
213     Return whole recordset as a multi-dimensional associative array
214     */
215     function &GetAssoc($force_array = false, $first2cols = false)
216     {
217         $records = $this->_numOfRows;
218         $results = array();
219         for ($i = 0; $i < $records; $i++) {
220             foreach ($this->fields as $k => $v) {
221                 if (is_array($v)) {
222                     if ($v['count'] == 1) {
223                         $results[$i][$k] = $v[0];
224                     } else {
225                         array_shift($v);
226                         $results[$i][$k] = $v;
227                     }
228                 }
229             }
230         }
231
232         return $results;
233     }
234
235     function &GetRowAssoc()
236     {
237         $results = array();
238         foreach ($this->fields as $k => $v) {
239             if (is_array($v)) {
240                 if ($v['count'] == 1) {
241                     $results[$k] = $v[0];
242                 } else {
243                     array_shift($v);
244                     $results[$k] = $v;
245                 }
246             }
247         }
248
249         return $results;
250     }
251
252     function GetRowNums()
253     {
254         $results = array();
255         foreach ($this->fields as $k => $v) {
256             static $i = 0;
257             if (is_array($v)) {
258                 if ($v['count'] == 1) {
259                     $results[$i] = $v[0];
260                 } else {
261                     array_shift($v);
262                     $results[$i] = $v;
263                 }
264                 $i++;
265             }
266         }
267         return $results;
268     }
269
270     function _fetch()
271     {
272         if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
273             return false;
274
275         if ($this->_currentRow == 0) {
276             $this->_entryID = ldap_first_entry($this->connection->_connectionID, $this->_queryID);
277         } else {
278             $this->_entryID = ldap_next_entry($this->connection->_connectionID, $this->_entryID);
279         }
280
281         $this->fields = ldap_get_attributes($this->connection->_connectionID, $this->_entryID);
282         $this->_numOfFields = $this->fields['count'];
283         switch ($this->fetchMode) {
284
285             case LDAP_ASSOC:
286                 $this->fields = $this->GetRowAssoc();
287                 break;
288
289             case LDAP_NUM:
290                 $this->fields = $this->GetRowNums();
291                 break;
292
293             case LDAP_BOTH:
294             default:
295                 break;
296         }
297         return (is_array($this->fields));
298     }
299
300     function _close()
301     {
302         @ldap_free_result($this->_queryID);
303         $this->_queryID = false;
304     }
305
306 }