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