adLDAPContacts.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * PHP LDAP CLASS FOR MANIPULATING ACTIVE DIRECTORY
  4. * Version 4.0.4
  5. *
  6. * PHP Version 5 with SSL and LDAP support
  7. *
  8. * Written by Scott Barnett, Richard Hyland
  9. * email: scott@wiggumworld.com, adldap@richardhyland.com
  10. * http://adldap.sourceforge.net/
  11. *
  12. * Copyright (c) 2006-2012 Scott Barnett, Richard Hyland
  13. *
  14. * We'd appreciate any improvements or additions to be submitted back
  15. * to benefit the entire community :)
  16. *
  17. * This library is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU Lesser General Public
  19. * License as published by the Free Software Foundation; either
  20. * version 2.1 of the License.
  21. *
  22. * This library is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * Lesser General Public License for more details.
  26. *
  27. * @category ToolsAndUtilities
  28. * @package adLDAP
  29. * @subpackage Contacts
  30. * @author Scott Barnett, Richard Hyland
  31. * @copyright (c) 2006-2012 Scott Barnett, Richard Hyland
  32. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPLv2.1
  33. * @revision $Revision: 97 $
  34. * @version 4.0.4
  35. * @link http://adldap.sourceforge.net/
  36. */
  37. require_once(dirname(__FILE__) . '/../adLDAP.php');
  38. require_once(dirname(__FILE__) . '/../collections/adLDAPContactCollection.php');
  39. use dokuwiki\Utf8\Sort;
  40. class adLDAPContacts {
  41. /**
  42. * The current adLDAP connection via dependency injection
  43. *
  44. * @var adLDAP
  45. */
  46. protected $adldap;
  47. public function __construct(adLDAP $adldap) {
  48. $this->adldap = $adldap;
  49. }
  50. //*****************************************************************************************************************
  51. // CONTACT FUNCTIONS
  52. // * Still work to do in this area, and new functions to write
  53. /**
  54. * Create a contact
  55. *
  56. * @param array $attributes The attributes to set to the contact
  57. * @return bool
  58. */
  59. public function create($attributes)
  60. {
  61. // Check for compulsory fields
  62. if (!array_key_exists("display_name", $attributes)) { return "Missing compulsory field [display_name]"; }
  63. if (!array_key_exists("email", $attributes)) { return "Missing compulsory field [email]"; }
  64. if (!array_key_exists("container", $attributes)) { return "Missing compulsory field [container]"; }
  65. if (!is_array($attributes["container"])) { return "Container attribute must be an array."; }
  66. // Translate the schema
  67. $add = $this->adldap->adldap_schema($attributes);
  68. // Additional stuff only used for adding contacts
  69. $add["cn"][0] = $attributes["display_name"];
  70. $add["objectclass"][0] = "top";
  71. $add["objectclass"][1] = "person";
  72. $add["objectclass"][2] = "organizationalPerson";
  73. $add["objectclass"][3] = "contact";
  74. if (!isset($attributes['exchange_hidefromlists'])) {
  75. $add["msExchHideFromAddressLists"][0] = "TRUE";
  76. }
  77. // Determine the container
  78. $attributes["container"] = array_reverse($attributes["container"]);
  79. $container= "OU=" . implode(",OU=", $attributes["container"]);
  80. // Add the entry
  81. $result = @ldap_add($this->adldap->getLdapConnection(), "CN=" . $this->adldap->utilities()->escapeCharacters($add["cn"][0]) . ", " . $container . "," . $this->adldap->getBaseDn(), $add);
  82. if ($result != true) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * Determine the list of groups a contact is a member of
  89. *
  90. * @param string $distinguisedname The full DN of a contact
  91. * @param bool $recursive Recursively check groups
  92. * @return array
  93. */
  94. public function groups($distinguishedName, $recursive = NULL)
  95. {
  96. if ($distinguishedName === NULL) { return false; }
  97. if ($recursive === NULL) { $recursive = $this->adldap->getRecursiveGroups(); } //use the default option if they haven't set it
  98. if (!$this->adldap->getLdapBind()){ return false; }
  99. // Search the directory for their information
  100. $info = @$this->info($distinguishedName, array("memberof", "primarygroupid"));
  101. $groups = $this->adldap->utilities()->niceNames($info[0]["memberof"]); //presuming the entry returned is our contact
  102. if ($recursive === true){
  103. foreach ($groups as $id => $groupName){
  104. $extraGroups = $this->adldap->group()->recursiveGroups($groupName);
  105. $groups = array_merge($groups, $extraGroups);
  106. }
  107. }
  108. return $groups;
  109. }
  110. /**
  111. * Get contact information. Returned in a raw array format from AD
  112. *
  113. * @param string $distinguisedname The full DN of a contact
  114. * @param array $fields Attributes to be returned
  115. * @return array
  116. */
  117. public function info($distinguishedName, $fields = NULL)
  118. {
  119. if ($distinguishedName === NULL) { return false; }
  120. if (!$this->adldap->getLdapBind()) { return false; }
  121. $filter = "distinguishedName=" . $distinguishedName;
  122. if ($fields === NULL) {
  123. $fields = array("distinguishedname", "mail", "memberof", "department", "displayname", "telephonenumber", "primarygroupid", "objectsid");
  124. }
  125. $sr = ldap_search($this->adldap->getLdapConnection(), $this->adldap->getBaseDn(), $filter, $fields);
  126. $entries = ldap_get_entries($this->adldap->getLdapConnection(), $sr);
  127. if ($entries[0]['count'] >= 1) {
  128. // AD does not return the primary group in the ldap query, we may need to fudge it
  129. if ($this->adldap->getRealPrimaryGroup() && isset($entries[0]["primarygroupid"][0]) && isset($entries[0]["primarygroupid"][0])){
  130. //$entries[0]["memberof"][]=$this->group_cn($entries[0]["primarygroupid"][0]);
  131. $entries[0]["memberof"][] = $this->adldap->group()->getPrimaryGroup($entries[0]["primarygroupid"][0], $entries[0]["objectsid"][0]);
  132. } else {
  133. $entries[0]["memberof"][] = "CN=Domain Users,CN=Users," . $this->adldap->getBaseDn();
  134. }
  135. }
  136. $entries[0]["memberof"]["count"]++;
  137. return $entries;
  138. }
  139. /**
  140. * Find information about the contacts. Returned in a raw array format from AD
  141. *
  142. * @param string $distinguishedName The full DN of a contact
  143. * @param array $fields Array of parameters to query
  144. * @return mixed
  145. */
  146. public function infoCollection($distinguishedName, $fields = NULL)
  147. {
  148. if ($distinguishedName === NULL) { return false; }
  149. if (!$this->adldap->getLdapBind()) { return false; }
  150. $info = $this->info($distinguishedName, $fields);
  151. if ($info !== false) {
  152. $collection = new adLDAPContactCollection($info, $this->adldap);
  153. return $collection;
  154. }
  155. return false;
  156. }
  157. /**
  158. * Determine if a contact is a member of a group
  159. *
  160. * @param string $distinguisedName The full DN of a contact
  161. * @param string $group The group name to query
  162. * @param bool $recursive Recursively check groups
  163. * @return bool
  164. */
  165. public function inGroup($distinguisedName, $group, $recursive = NULL)
  166. {
  167. if ($distinguisedName === NULL) { return false; }
  168. if ($group === NULL) { return false; }
  169. if (!$this->adldap->getLdapBind()) { return false; }
  170. if ($recursive === NULL) { $recursive = $this->adldap->getRecursiveGroups(); } //use the default option if they haven't set it
  171. // Get a list of the groups
  172. $groups = $this->groups($distinguisedName, array("memberof"), $recursive);
  173. // Return true if the specified group is in the group list
  174. if (in_array($group, $groups)){
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * Modify a contact
  181. *
  182. * @param string $distinguishedName The contact to query
  183. * @param array $attributes The attributes to modify. Note if you set the enabled attribute you must not specify any other attributes
  184. * @return bool
  185. */
  186. public function modify($distinguishedName, $attributes) {
  187. if ($distinguishedName === NULL) { return "Missing compulsory field [distinguishedname]"; }
  188. // Translate the update to the LDAP schema
  189. $mod = $this->adldap->adldap_schema($attributes);
  190. // Check to see if this is an enabled status update
  191. if (!$mod) {
  192. return false;
  193. }
  194. // Do the update
  195. $result = ldap_modify($this->adldap->getLdapConnection(), $distinguishedName, $mod);
  196. if ($result == false) {
  197. return false;
  198. }
  199. return true;
  200. }
  201. /**
  202. * Delete a contact
  203. *
  204. * @param string $distinguishedName The contact dn to delete (please be careful here!)
  205. * @return array
  206. */
  207. public function delete($distinguishedName)
  208. {
  209. $result = $this->folder()->delete($distinguishedName);
  210. if ($result != true) {
  211. return false;
  212. }
  213. return true;
  214. }
  215. /**
  216. * Return a list of all contacts
  217. *
  218. * @param bool $includeDescription Include a description of a contact
  219. * @param string $search The search parameters
  220. * @param bool $sorted Whether to sort the results
  221. * @return array
  222. */
  223. public function all($includeDescription = false, $search = "*", $sorted = true) {
  224. if (!$this->adldap->getLdapBind()) { return false; }
  225. // Perform the search and grab all their details
  226. $filter = "(&(objectClass=contact)(cn=" . $search . "))";
  227. $fields = array("displayname","distinguishedname");
  228. $sr = ldap_search($this->adldap->getLdapConnection(), $this->adldap->getBaseDn(), $filter, $fields);
  229. $entries = ldap_get_entries($this->adldap->getLdapConnection(), $sr);
  230. $usersArray = array();
  231. for ($i=0; $i<$entries["count"]; $i++){
  232. if ($includeDescription && strlen($entries[$i]["displayname"][0])>0){
  233. $usersArray[$entries[$i]["distinguishedname"][0]] = $entries[$i]["displayname"][0];
  234. } elseif ($includeDescription){
  235. $usersArray[$entries[$i]["distinguishedname"][0]] = $entries[$i]["distinguishedname"][0];
  236. } else {
  237. array_push($usersArray, $entries[$i]["distinguishedname"][0]);
  238. }
  239. }
  240. if ($sorted) {
  241. Sort::asort($usersArray);
  242. }
  243. return $usersArray;
  244. }
  245. /**
  246. * Mail enable a contact
  247. * Allows email to be sent to them through Exchange
  248. *
  249. * @param string $distinguishedname The contact to mail enable
  250. * @param string $emailaddress The email address to allow emails to be sent through
  251. * @param string $mailnickname The mailnickname for the contact in Exchange. If NULL this will be set to the display name
  252. * @return bool
  253. */
  254. public function contactMailEnable($distinguishedName, $emailAddress, $mailNickname = NULL){
  255. return $this->adldap->exchange()->contactMailEnable($distinguishedName, $emailAddress, $mailNickname);
  256. }
  257. }
  258. ?>