adodb-ldap.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 8.
  10. Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
  11. users to access the options in the ldap_set_option function appropriately. Most importantly
  12. LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
  13. bugs that surfaced when PHP error levels were set high.
  14. Joshua Eldridge (joshuae74#hotmail.com)
  15. */
  16. // security - hide paths
  17. if (!defined('ADODB_DIR')) die();
  18. if (!defined('LDAP_ASSOC')) {
  19. define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
  20. define('LDAP_NUM',ADODB_FETCH_NUM);
  21. define('LDAP_BOTH',ADODB_FETCH_BOTH);
  22. }
  23. class ADODB_ldap extends ADOConnection {
  24. var $databaseType = 'ldap';
  25. var $dataProvider = 'ldap';
  26. # Connection information
  27. var $username = false;
  28. var $password = false;
  29. # Used during searches
  30. var $filter;
  31. var $dn;
  32. var $version;
  33. var $port = 389;
  34. # Options configuration information
  35. var $LDAP_CONNECT_OPTIONS;
  36. # error on binding, eg. "Binding: invalid credentials"
  37. var $_bind_errmsg = "Binding: %s";
  38. function __construct()
  39. {
  40. }
  41. // returns true or false
  42. function _connect( $host, $username, $password, $ldapbase)
  43. {
  44. global $LDAP_CONNECT_OPTIONS;
  45. if ( !function_exists( 'ldap_connect' ) ) return null;
  46. if (strpos($host,'ldap://') === 0 || strpos($host,'ldaps://') === 0) {
  47. $this->_connectionID = @ldap_connect($host);
  48. } else {
  49. $conn_info = array( $host,$this->port);
  50. if ( strstr( $host, ':' ) ) {
  51. $conn_info = explode( ':', $host );
  52. }
  53. $this->_connectionID = @ldap_connect( $conn_info[0], $conn_info[1] );
  54. }
  55. if (!$this->_connectionID) {
  56. $e = 'Could not connect to ' . $conn_info[0];
  57. $this->_errorMsg = $e;
  58. if ($this->debug) ADOConnection::outp($e);
  59. return false;
  60. }
  61. if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
  62. $this->_inject_bind_options( $LDAP_CONNECT_OPTIONS );
  63. }
  64. if ($username) {
  65. $bind = @ldap_bind( $this->_connectionID, $username, $password );
  66. } else {
  67. $username = 'anonymous';
  68. $bind = @ldap_bind( $this->_connectionID );
  69. }
  70. if (!$bind) {
  71. $e = sprintf($this->_bind_errmsg,ldap_error($this->_connectionID));
  72. $this->_errorMsg = $e;
  73. if ($this->debug) ADOConnection::outp($e);
  74. return false;
  75. }
  76. $this->_errorMsg = '';
  77. $this->database = $ldapbase;
  78. return $this->_connectionID;
  79. }
  80. /*
  81. Valid Domain Values for LDAP Options:
  82. LDAP_OPT_DEREF (integer)
  83. LDAP_OPT_SIZELIMIT (integer)
  84. LDAP_OPT_TIMELIMIT (integer)
  85. LDAP_OPT_PROTOCOL_VERSION (integer)
  86. LDAP_OPT_ERROR_NUMBER (integer)
  87. LDAP_OPT_REFERRALS (boolean)
  88. LDAP_OPT_RESTART (boolean)
  89. LDAP_OPT_HOST_NAME (string)
  90. LDAP_OPT_ERROR_STRING (string)
  91. LDAP_OPT_MATCHED_DN (string)
  92. LDAP_OPT_SERVER_CONTROLS (array)
  93. LDAP_OPT_CLIENT_CONTROLS (array)
  94. Make sure to set this BEFORE calling Connect()
  95. Example:
  96. $LDAP_CONNECT_OPTIONS = Array(
  97. Array (
  98. "OPTION_NAME"=>LDAP_OPT_DEREF,
  99. "OPTION_VALUE"=>2
  100. ),
  101. Array (
  102. "OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
  103. "OPTION_VALUE"=>100
  104. ),
  105. Array (
  106. "OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
  107. "OPTION_VALUE"=>30
  108. ),
  109. Array (
  110. "OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
  111. "OPTION_VALUE"=>3
  112. ),
  113. Array (
  114. "OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
  115. "OPTION_VALUE"=>13
  116. ),
  117. Array (
  118. "OPTION_NAME"=>LDAP_OPT_REFERRALS,
  119. "OPTION_VALUE"=>FALSE
  120. ),
  121. Array (
  122. "OPTION_NAME"=>LDAP_OPT_RESTART,
  123. "OPTION_VALUE"=>FALSE
  124. )
  125. );
  126. */
  127. function _inject_bind_options( $options ) {
  128. foreach( $options as $option ) {
  129. ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
  130. or die( "Unable to set server option: " . $option["OPTION_NAME"] );
  131. }
  132. }
  133. /* returns _queryID or false */
  134. function _query($sql,$inputarr=false)
  135. {
  136. $rs = @ldap_search( $this->_connectionID, $this->database, $sql );
  137. $this->_errorMsg = ($rs) ? '' : 'Search error on '.$sql.': '.ldap_error($this->_connectionID);
  138. return $rs;
  139. }
  140. function ErrorMsg()
  141. {
  142. return $this->_errorMsg;
  143. }
  144. function ErrorNo()
  145. {
  146. return @ldap_errno($this->_connectionID);
  147. }
  148. /* closes the LDAP connection */
  149. function _close()
  150. {
  151. @ldap_close( $this->_connectionID );
  152. $this->_connectionID = false;
  153. }
  154. function SelectDB($db) {
  155. $this->database = $db;
  156. return true;
  157. } // SelectDB
  158. function ServerInfo()
  159. {
  160. if( !empty( $this->version ) ) {
  161. return $this->version;
  162. }
  163. $version = array();
  164. /*
  165. Determines how aliases are handled during search.
  166. LDAP_DEREF_NEVER (0x00)
  167. LDAP_DEREF_SEARCHING (0x01)
  168. LDAP_DEREF_FINDING (0x02)
  169. LDAP_DEREF_ALWAYS (0x03)
  170. The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
  171. not when locating the base object of the search. The LDAP_DEREF_FINDING value means
  172. aliases are dereferenced when locating the base object but not during the search.
  173. Default: LDAP_DEREF_NEVER
  174. */
  175. ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
  176. switch ( $version['LDAP_OPT_DEREF'] ) {
  177. case 0:
  178. $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
  179. case 1:
  180. $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
  181. case 2:
  182. $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
  183. case 3:
  184. $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
  185. }
  186. /*
  187. A limit on the number of entries to return from a search.
  188. LDAP_NO_LIMIT (0) means no limit.
  189. Default: LDAP_NO_LIMIT
  190. */
  191. ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
  192. if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
  193. $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
  194. }
  195. /*
  196. A limit on the number of seconds to spend on a search.
  197. LDAP_NO_LIMIT (0) means no limit.
  198. Default: LDAP_NO_LIMIT
  199. */
  200. ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
  201. if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
  202. $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
  203. }
  204. /*
  205. Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
  206. LDAP_OPT_ON
  207. LDAP_OPT_OFF
  208. Default: ON
  209. */
  210. ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
  211. if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
  212. $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
  213. } else {
  214. $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
  215. }
  216. /*
  217. Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
  218. LDAP_OPT_ON
  219. LDAP_OPT_OFF
  220. Default: OFF
  221. */
  222. ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
  223. if ( $version['LDAP_OPT_RESTART'] == 0 ) {
  224. $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
  225. } else {
  226. $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
  227. }
  228. /*
  229. This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
  230. LDAP_VERSION2 (2)
  231. LDAP_VERSION3 (3)
  232. Default: LDAP_VERSION2 (2)
  233. */
  234. ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
  235. if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
  236. $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
  237. } else {
  238. $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
  239. }
  240. /* The host name (or list of hosts) for the primary LDAP server. */
  241. ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
  242. ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_NUMBER, $version['LDAP_OPT_ERROR_NUMBER'] );
  243. ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_STRING, $version['LDAP_OPT_ERROR_STRING'] );
  244. ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
  245. return $this->version = $version;
  246. }
  247. }
  248. /*--------------------------------------------------------------------------------------
  249. Class Name: Recordset
  250. --------------------------------------------------------------------------------------*/
  251. class ADORecordSet_ldap extends ADORecordSet{
  252. var $databaseType = "ldap";
  253. var $canSeek = false;
  254. var $_entryID; /* keeps track of the entry resource identifier */
  255. function __construct($queryID,$mode=false)
  256. {
  257. if ($mode === false) {
  258. global $ADODB_FETCH_MODE;
  259. $mode = $ADODB_FETCH_MODE;
  260. }
  261. switch ($mode)
  262. {
  263. case ADODB_FETCH_NUM:
  264. $this->fetchMode = LDAP_NUM;
  265. break;
  266. case ADODB_FETCH_ASSOC:
  267. $this->fetchMode = LDAP_ASSOC;
  268. break;
  269. case ADODB_FETCH_DEFAULT:
  270. case ADODB_FETCH_BOTH:
  271. default:
  272. $this->fetchMode = LDAP_BOTH;
  273. break;
  274. }
  275. parent::__construct($queryID);
  276. }
  277. function _initrs()
  278. {
  279. /*
  280. This could be teaked to respect the $COUNTRECS directive from ADODB
  281. It's currently being used in the _fetch() function and the
  282. GetAssoc() function
  283. */
  284. $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
  285. }
  286. /*
  287. Return whole recordset as a multi-dimensional associative array
  288. */
  289. function GetAssoc($force_array = false, $first2cols = false)
  290. {
  291. $records = $this->_numOfRows;
  292. $results = array();
  293. for ( $i=0; $i < $records; $i++ ) {
  294. foreach ( $this->fields as $k=>$v ) {
  295. if ( is_array( $v ) ) {
  296. if ( $v['count'] == 1 ) {
  297. $results[$i][$k] = $v[0];
  298. } else {
  299. array_shift( $v );
  300. $results[$i][$k] = $v;
  301. }
  302. }
  303. }
  304. }
  305. return $results;
  306. }
  307. function GetRowAssoc($upper = ADODB_ASSOC_CASE)
  308. {
  309. $results = array();
  310. foreach ( $this->fields as $k=>$v ) {
  311. if ( is_array( $v ) ) {
  312. if ( $v['count'] == 1 ) {
  313. $results[$k] = $v[0];
  314. } else {
  315. array_shift( $v );
  316. $results[$k] = $v;
  317. }
  318. }
  319. }
  320. return $results;
  321. }
  322. function GetRowNums()
  323. {
  324. $results = array();
  325. foreach ( $this->fields as $k=>$v ) {
  326. static $i = 0;
  327. if (is_array( $v )) {
  328. if ( $v['count'] == 1 ) {
  329. $results[$i] = $v[0];
  330. } else {
  331. array_shift( $v );
  332. $results[$i] = $v;
  333. }
  334. $i++;
  335. }
  336. }
  337. return $results;
  338. }
  339. function _fetch()
  340. {
  341. if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 ) {
  342. return false;
  343. }
  344. if ( $this->_currentRow == 0 ) {
  345. $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
  346. } else {
  347. $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
  348. }
  349. $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
  350. $this->_numOfFields = $this->fields['count'];
  351. switch ( $this->fetchMode ) {
  352. case LDAP_ASSOC:
  353. $this->fields = $this->GetRowAssoc();
  354. break;
  355. case LDAP_NUM:
  356. $this->fields = array_merge($this->GetRowNums(),$this->GetRowAssoc());
  357. break;
  358. case LDAP_BOTH:
  359. default:
  360. $this->fields = $this->GetRowNums();
  361. break;
  362. }
  363. return is_array( $this->fields );
  364. }
  365. function _close() {
  366. @ldap_free_result( $this->_queryID );
  367. $this->_queryID = false;
  368. }
  369. }