Source for file LDAPConnector.class.php

Documentation is available at LDAPConnector.class.php

  1. <?php
  2. /**
  3. * @package harmoni.osid_v2.agentmanagement.authn_methods
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: LDAPConnector.class.php,v 1.13 2007/09/04 20:25:37 adamfranco Exp $
  9. */
  10.  
  11. /**
  12. * LDAPConnector is a class used by the LDAPAuthNMethod and the LDAPAuthNTokens
  13. * to handle common functions
  14. *
  15. * @package harmoni.osid_v2.agentmanagement.authn_methods
  16. *
  17. * @copyright Copyright &copy; 2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  19. *
  20. * @version $Id: LDAPConnector.class.php,v 1.13 2007/09/04 20:25:37 adamfranco Exp $
  21. */
  22. class LDAPConnector {
  23. /**
  24. * The LDAP connection ID.
  25. * @access private
  26. * @var integer $_con
  27. */
  28. var $_conn;
  29. /**
  30. * LDAP bind result.
  31. * @access private
  32. * @var boolean $_bind
  33. */
  34. var $_bind;
  35. /**
  36. * The configuration for this method.
  37. * @access private
  38. * @var object Properties $_configuration
  39. */
  40. var $_configuration;
  41. /**
  42. * The constructor.
  43. * @param ref object $configuration A {@link Properties} Properties with configuration for connection.
  44. * @access public
  45. * @return void
  46. ***/
  47. function LDAPConnector( $configuration ) {
  48. $this->_configuration =$configuration;
  49. // Validate the configuration options we use:
  50. ArgumentValidator::validate (
  51. $this->_configuration->getProperty('LDAPHost'),
  52. FieldRequiredValidatorRule::getRule());
  53. ArgumentValidator::validate (
  54. $this->_configuration->getProperty('LDAPPort'),
  55. OptionalRule::getRule(NumericValidatorRule::getRule()));
  56. ArgumentValidator::validate (
  57. $this->_configuration->getProperty('UserBaseDN'),
  58. FieldRequiredValidatorRule::getRule());
  59. ArgumentValidator::validate (
  60. $this->_configuration->getProperty('ClassesBaseDN'),
  61. FieldRequiredValidatorRule::getRule());
  62. ArgumentValidator::validate (
  63. $this->_configuration->getProperty('GroupBaseDN'),
  64. FieldRequiredValidatorRule::getRule());
  65. ArgumentValidator::validate (
  66. $this->_configuration->getProperty('bindDN'),
  67. OptionalRule::getRule(StringValidatorRule::getRule()));
  68. ArgumentValidator::validate (
  69. $this->_configuration->getProperty('bindDNPassword'),
  70. OptionalRule::getRule(StringValidatorRule::getRule()));
  71. }
  72. /**
  73. * Attempt to bind to the LDAP server using $dn and $password credentials.
  74. * @param string $dn The LDAP DN.
  75. * @param string $password The password.
  76. * @access private
  77. * @return boolean TRUE if bind was successful, FALSE otherwise.
  78. ***/
  79. function _bind( $dn, $password ) {
  80. $this->_bind = @ldap_bind($this->_conn, $dn, $password);
  81.  
  82. if ($this->_bind) return true;
  83. return false;
  84. }
  85. /**
  86. * Attempts to bind to the LDAP server anonymously.
  87. * @access private
  88. * @return boolean TRUE if bind was successful, FALSE otherwise.
  89. ***/
  90. function _anonymousBind() {
  91. $this->_bind = ldap_bind($this->_conn);
  92. if ($this->_bind) return true;
  93. return false;
  94. }
  95. /**
  96. * Attempts to bind to the LDAP server either anonymously or with a
  97. * DN and password supplied in the configuration so that we can
  98. * search the database.
  99. * @access public
  100. * @return void
  101. ***/
  102. function _bindForSearch() {
  103. $dn = $this->_configuration->getProperty("bindDN");
  104. $pass = $this->_configuration->getProperty("bindDNPassword");
  105. if ($dn && $dn != '') { // we don't *require* the passwd
  106. $this->_bind($dn,$pass);
  107. } else $this->_anonymousBind();
  108. }
  109. /**
  110. * Connects to the LDAP server.
  111. * @access private
  112. * @return void
  113. ***/
  114. function _connect() {
  115. $this->_conn =
  116. ldap_connect($this->_configuration->getProperty("LDAPHost"),
  117. $this->_configuration->getProperty("LDAPPort"));
  118. if ($this->_conn == false)
  119. throwError(new Error("LDAPAuthenticationMethod::_connect() - could
  120. not connect to LDAP host <b>".
  121. $this->_configuration->getProperty("LDAPHost")."</b>!",
  122. "LDAPAuthenticationMethod",true));
  123. }
  124. /**
  125. * Disconnects from the LDAP server.
  126. * @access private
  127. * @return void
  128. ***/
  129. function _disconnect() {
  130. ldap_close($this->_conn);
  131. $this->_conn = NULL;
  132. }
  133. /**
  134. * authenticate will check a DN/password pair against the LDAP server.
  135. *
  136. * @param string $dn
  137. * @param string $password the password associated with $systemName
  138. * @access public
  139. * @return boolean true if authentication succeeded with the method, false if not
  140. ***/
  141. function authenticateDN( $dn, $password ) {
  142. // connect to the LDAP server.
  143. if ((!is_string($password)) || (strlen($password) < 1))
  144. return false;
  145. $this->_connect();
  146. if ($this->_bind($dn,$password)) {// they're good!
  147. $this->_disconnect();
  148. return true;
  149. }
  150. $this->_disconnect();
  151.  
  152. return false;
  153. }
  154. /**
  155. * Get the course DNs that match the search
  156. *
  157. * @param string $systemName
  158. * @return string
  159. * @access public
  160. * @since 3/4/05
  161. */
  162. function getClassesDNsBySearch ( $filter ) {
  163. return $this->getDNsBySearch($filter, $this->_configuration->getProperty("ClassesBaseDN"));
  164. }
  165. /**
  166. * Get the user DNs that match the search
  167. *
  168. * @param string $systemName
  169. * @return string
  170. * @access public
  171. * @since 3/4/05
  172. */
  173. function getUserDNsBySearch ( $filter ) {
  174. return $this->getDNsBySearch($filter, $this->_configuration->getProperty("UserBaseDN"));
  175. }
  176. /**
  177. * returns true if the User dn exists
  178. * @param string $systemName The name to fetch the DN for.
  179. * @access private
  180. * @return string|nullThe DN, or NULL if it can't be found.
  181. ***/
  182. function userDNExists( $dn ) {
  183. return $this->dnExists($dn, $this->_configuration->getProperty("UserBaseDN"));
  184. }
  185. /**
  186. * Get the user DNs that match the search
  187. *
  188. * @param string $systemName
  189. * @return string
  190. * @access public
  191. * @since 3/4/05
  192. */
  193. function getGroupDNsBySearch ( $filter ) {
  194. return $this->getDNsBySearch($filter, $this->_configuration->getProperty("GroupBaseDN"));
  195. }
  196. /**
  197. * returns true if the Group dn exists
  198. * @param string $systemName The name to fetch the DN for.
  199. * @access private
  200. * @return string|nullThe DN, or NULL if it can't be found.
  201. ***/
  202. function groupDNExists( $dn ) {
  203. return $this->dnExists($dn, $this->_configuration->getProperty("GroupBaseDN"));
  204. }
  205. /**
  206. * Get the DNs that match the search
  207. *
  208. * @param string $systemName
  209. * @return string
  210. * @access public
  211. * @since 3/4/05
  212. */
  213. function getDNsBySearch ( $filter, $baseDN ) {
  214. $this->_connect();
  215. $this->_bindForSearch();
  216. $sr = ldap_search($this->_conn,
  217. $baseDN,
  218. $filter);
  219. if (ldap_errno($this->_conn))
  220. throwError(new Error(ldap_error($this->_conn), "LDAPConnector"));
  221. $dns = array();
  222. $entry = ldap_first_entry($this->_conn, $sr);
  223. while($entry) {
  224. $dns[] = ldap_get_dn($this->_conn, $entry);
  225. $entry = ldap_next_entry($this->_conn, $entry);
  226. }
  227. ldap_free_result($sr);
  228. $this->_disconnect();
  229. return $dns;
  230. }
  231. /**
  232. * Get the DNs that match the search immediately below the baseDN
  233. *
  234. * @param string $systemName
  235. * @return string
  236. * @access public
  237. * @since 3/4/05
  238. */
  239. function getDNsByList ( $filter, $baseDN ) {
  240. $this->_connect();
  241. $this->_bindForSearch();
  242. $sr = ldap_list($this->_conn,
  243. $baseDN,
  244. $filter);
  245. if (ldap_errno($this->_conn))
  246. throwError(new Error(ldap_error($this->_conn), "LDAPConnector"));
  247. $dns = array();
  248. $entry = ldap_first_entry($this->_conn, $sr);
  249. while($entry) {
  250. $dns[] = ldap_get_dn($this->_conn, $entry);
  251. $entry = ldap_next_entry($this->_conn, $entry);
  252. }
  253. ldap_free_result($sr);
  254. $this->_disconnect();
  255. return $dns;
  256. }
  257. /**
  258. * returns true if the dn exists
  259. * @param string $systemName The name to fetch the DN for.
  260. * @access private
  261. * @return string|nullThe DN, or NULL if it can't be found.
  262. ***/
  263. function dnExists( $dn ) {
  264. $valid = false;
  265. $this->_connect();
  266. $this->_bindForSearch();
  267. $sr = @ldap_read($this->_conn,
  268. $dn,
  269. '(objectclass=*)',
  270. array('dn'));
  271. if ($sr) {
  272. if (ldap_count_entries($this->_conn,$sr))
  273. $valid = TRUE;
  274.  
  275. ldap_free_result($sr);
  276. }
  277. $this->_disconnect();
  278. return $valid;
  279. }
  280. /**
  281. * Get the DN for the systemname passed
  282. *
  283. * @param string $dn
  284. * @param optional array $return An array of fields to return.
  285. * @return string
  286. * @access public
  287. * @since 3/4/05
  288. */
  289. function getInfo ($dn, $fields) {
  290. $this->_connect();
  291. $this->_bindForSearch();
  292. $sr = ldap_read($this->_conn, $dn, "(objectclass=*)", $fields);
  293. $entries = ldap_get_entries($this->_conn, $sr);
  294. ldap_free_result($sr);
  295. $this->_disconnect();
  296. // Rebuild the array
  297. if (!$entries['count'])
  298. return array();
  299. $entry = $entries[0];
  300. $numValues = $entry['count'];
  301. $values = array();
  302. for ($i=0; $i<$numValues; $i++) {
  303. $key = $entry[$i];
  304. $value = $entry[$entry[$i]];
  305. $values[$key] = array();
  306. for ($j = 0; $j < $value['count']; $j++)
  307. $values[$key][] = $value[$j];
  308. }
  309. return $values;
  310. }
  311. }
  312.  
  313. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:50 -0400 by phpDocumentor 1.3.0RC3