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