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

/application/libraries/A5_Ldap.php

https://github.com/marifrahman/a5ldap
PHP | 165 lines | 123 code | 22 blank | 20 comment | 8 complexity | 85c2abd52d772e3816850ac4515b9957 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * A5 ldap Class
  4. *
  5. * LDAP Authentication library for Code Igniter.
  6. *
  7. * @author M. Arifur Rahman
  8. * @version 0.0.1
  9. * @ci version 1.7 [not tested for version 2 ]
  10. * @based on
  11. * @link http://strangerzlog.blogspot.com
  12. * @license MIT License Copyright (c) 2008 Erick Hartanto
  13. * @credits
  14. */
  15. class A5_Ldap
  16. {
  17. var $ldap;
  18. function __construct()
  19. {
  20. $this->CI =& get_instance();
  21. $this->CI->load->config('a5ldap');
  22. }
  23. function _ldap_connect()
  24. {
  25. $this->ldap = @ldap_connect($this->CI->config->item('ldapurl'))
  26. or die("Couldn't connect to AD!");
  27. // Set version number
  28. ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3)
  29. or die ("Could not set ldap protocol");
  30. ldap_set_option($this->ldap, LDAP_OPT_REFERRALS,0)
  31. or die ("Could no set the ldap referrals");
  32. }
  33. function _ldap_disconnect()
  34. {
  35. @ldap_unbind($this->ldap);
  36. $this->ldap = NULL;
  37. }
  38. /*
  39. * To search if an email exists within the directory
  40. */
  41. function isEmailinDirectory($email)
  42. {
  43. if(!isset($this->ldap))$this->_ldap_connect();
  44. $bd = ldap_bind($this->ldap,$this->CI->config->item('ldapuser')."@".$this->CI->config->item('ldapdomain'),$this->CI->config->item('ldappwd'))
  45. or die("Couldn't bind to AD!");
  46. $ldap_dcs = explode('.',$this->CI->config->item('ldapdomain'));
  47. $dn = "";
  48. foreach($ldap_dcs as $ldap_dc)
  49. $dn = $dn."DC=".$ldap_dc.",";
  50. $dn = rtrim($dn, strrchr($dn, ","));//removes the last ','
  51. $filter = "(mail=".$email.")";
  52. $result = ldap_search($this->ldap,$dn, $filter,array("mail"),0,0) or die ("ldap search failed");
  53. $entries = ldap_get_entries($this->ldap, $result);
  54. if($entries["count"]>0)
  55. $retval = true;
  56. else
  57. $retval = false;
  58. $this->_ldap_disconnect();
  59. return $retval;
  60. }
  61. /*
  62. * To search if an email exists within the directory
  63. */
  64. function authenticate($email,$password)
  65. {
  66. $email = $email."@".$this->CI->config->item('ldapdomain');
  67. if(!isset($this->ldap))$this->_ldap_connect();
  68. if(@ldap_bind($this->ldap,$email,$password))
  69. {
  70. $ldap_dcs = explode('.',$this->CI->config->item('ldapdomain'));
  71. $dn = "";
  72. foreach($ldap_dcs as $ldap_dc)
  73. $dn = $dn."DC=".$ldap_dc.",";
  74. $dn = rtrim($dn, strrchr($dn, ","));//removes the last ','
  75. $filter = "(mail=".$email.")";
  76. $attributes = array("displayname",
  77. "department",
  78. "title");
  79. $result = @ldap_search($this->ldap,$dn, $filter,$attributes,0,0) or die ("ldap search failed");
  80. $entries = @ldap_get_entries($this->ldap, $result);
  81. if($entries["count"]>0)
  82. {
  83. for ($i=0; $i<$entries["count"]; $i++)
  84. {
  85. foreach($attributes as $attribute)
  86. $retval[$attribute] = $entries[$i][$attribute][0];
  87. }
  88. }
  89. else
  90. $retval = false;
  91. }
  92. else
  93. $retval = false;
  94. $this->_ldap_disconnect();
  95. return $retval;
  96. }
  97. /*
  98. * Retrive information of a user in the directory using his email address
  99. * Retruns an array if success else retun false
  100. */
  101. function getuserInfobyEmail($email,$attributes)
  102. {
  103. if(!isset($this->ldap))$this->_ldap_connect();
  104. $bd = ldap_bind($this->ldap,$this->CI->config->item('ldapuser')."@".$this->CI->config->item('ldapdomain'),$this->CI->config->item('ldappwd'))
  105. or die("Couldn't bind to AD!");
  106. $ldap_dcs = explode('.',$this->CI->config->item('ldapdomain'));
  107. $dn = "";
  108. foreach($ldap_dcs as $ldap_dc)
  109. $dn = $dn."DC=".$ldap_dc.",";
  110. $dn = rtrim($dn, strrchr($dn, ","));//removes the last ','
  111. $filter = "(mail=".$email.")";
  112. /*$attributes = array("displayname", "mail",
  113. "department",
  114. "title");*/
  115. $result = ldap_search($this->ldap,$dn, $filter,$attributes,0,0) or die ("ldap search failed");
  116. $entries = ldap_get_entries($this->ldap, $result);
  117. if($entries["count"]>0)
  118. {
  119. for ($i=0; $i<$entries["count"]; $i++)
  120. {
  121. foreach($attributes as $attribute)
  122. $retval[$attribute] = $entries[$i][$attribute][0];
  123. }
  124. }
  125. else
  126. $retval = false;
  127. $this->_ldap_disconnect();
  128. return $retval;
  129. }
  130. }