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