adLDAPFolders.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Folders
  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. /**
  39. * FOLDER / OU MANAGEMENT FUNCTIONS
  40. */
  41. class adLDAPFolders {
  42. /**
  43. * The current adLDAP connection via dependency injection
  44. *
  45. * @var adLDAP
  46. */
  47. protected $adldap;
  48. public function __construct(adLDAP $adldap) {
  49. $this->adldap = $adldap;
  50. }
  51. /**
  52. * Delete a distinguished name from Active Directory
  53. * You should never need to call this yourself, just use the wrapper functions user_delete and contact_delete
  54. *
  55. * @param string $dn The distinguished name to delete
  56. * @return bool
  57. */
  58. public function delete($dn){
  59. $result = ldap_delete($this->adldap->getLdapConnection(), $dn);
  60. if ($result != true) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Returns a folder listing for a specific OU
  67. * See http://adldap.sourceforge.net/wiki/doku.php?id=api_folder_functions
  68. *
  69. * @param array $folderName An array to the OU you wish to list.
  70. * If set to NULL will list the root, strongly recommended to set
  71. * $recursive to false in that instance!
  72. * @param string $dnType The type of record to list. This can be ADLDAP_FOLDER or ADLDAP_CONTAINER.
  73. * @param bool $recursive Recursively search sub folders
  74. * @param bool $type Specify a type of object to search for
  75. * @return array
  76. */
  77. public function listing($folderName = NULL, $dnType = adLDAP::ADLDAP_FOLDER, $recursive = NULL, $type = NULL)
  78. {
  79. if ($recursive === NULL) { $recursive = $this->adldap->getRecursiveGroups(); } //use the default option if they haven't set it
  80. if (!$this->adldap->getLdapBind()) { return false; }
  81. $filter = '(&';
  82. if ($type !== NULL) {
  83. switch ($type) {
  84. case 'contact':
  85. $filter .= '(objectClass=contact)';
  86. break;
  87. case 'computer':
  88. $filter .= '(objectClass=computer)';
  89. break;
  90. case 'group':
  91. $filter .= '(objectClass=group)';
  92. break;
  93. case 'folder':
  94. $filter .= '(objectClass=organizationalUnit)';
  95. break;
  96. case 'container':
  97. $filter .= '(objectClass=container)';
  98. break;
  99. case 'domain':
  100. $filter .= '(objectClass=builtinDomain)';
  101. break;
  102. default:
  103. $filter .= '(objectClass=user)';
  104. break;
  105. }
  106. }
  107. else {
  108. $filter .= '(objectClass=*)';
  109. }
  110. // If the folder name is null then we will search the root level of AD
  111. // This requires us to not have an OU= part, just the base_dn
  112. $searchOu = $this->adldap->getBaseDn();
  113. if (is_array($folderName)) {
  114. $ou = $dnType . "=" . implode("," . $dnType . "=", $folderName);
  115. $filter .= '(!(distinguishedname=' . $ou . ',' . $this->adldap->getBaseDn() . ')))';
  116. $searchOu = $ou . ',' . $this->adldap->getBaseDn();
  117. }
  118. else {
  119. $filter .= '(!(distinguishedname=' . $this->adldap->getBaseDn() . ')))';
  120. }
  121. if ($recursive === true) {
  122. $sr = ldap_search($this->adldap->getLdapConnection(), $searchOu, $filter, array('objectclass', 'distinguishedname', 'samaccountname'));
  123. $entries = @ldap_get_entries($this->adldap->getLdapConnection(), $sr);
  124. if (is_array($entries)) {
  125. return $entries;
  126. }
  127. }
  128. else {
  129. $sr = ldap_list($this->adldap->getLdapConnection(), $searchOu, $filter, array('objectclass', 'distinguishedname', 'samaccountname'));
  130. $entries = @ldap_get_entries($this->adldap->getLdapConnection(), $sr);
  131. if (is_array($entries)) {
  132. return $entries;
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Create an organizational unit
  139. *
  140. * @param array $attributes Default attributes of the ou
  141. * @return bool
  142. */
  143. public function create($attributes)
  144. {
  145. if (!is_array($attributes)){ return "Attributes must be an array"; }
  146. if (!is_array($attributes["container"])) { return "Container attribute must be an array."; }
  147. if (!array_key_exists("ou_name",$attributes)) { return "Missing compulsory field [ou_name]"; }
  148. if (!array_key_exists("container",$attributes)) { return "Missing compulsory field [container]"; }
  149. $attributes["container"] = array_reverse($attributes["container"]);
  150. $add=array();
  151. $add["objectClass"] = "organizationalUnit";
  152. $add["OU"] = $attributes['ou_name'];
  153. $containers = "";
  154. if (count($attributes['container']) > 0) {
  155. $containers = "OU=" . implode(",OU=", $attributes["container"]) . ",";
  156. }
  157. $containers = "OU=" . implode(",OU=", $attributes["container"]);
  158. $result = ldap_add($this->adldap->getLdapConnection(), "OU=" . $add["OU"] . ", " . $containers . $this->adldap->getBaseDn(), $add);
  159. if ($result != true) {
  160. return false;
  161. }
  162. return true;
  163. }
  164. }
  165. ?>