PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/adodb/drivers/adodb-ldap.inc.php

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