adLDAPUtils.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Utils
  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. * UTILITY FUNCTIONS
  40. */
  41. class adLDAPUtils {
  42. const ADLDAP_VERSION = '4.0.4';
  43. /**
  44. * The current adLDAP connection via dependency injection
  45. *
  46. * @var adLDAP
  47. */
  48. protected $adldap;
  49. public function __construct(adLDAP $adldap) {
  50. $this->adldap = $adldap;
  51. }
  52. /**
  53. * Take an LDAP query and return the nice names, without all the LDAP prefixes (eg. CN, DN)
  54. *
  55. * @param array $groups
  56. * @return array
  57. */
  58. public function niceNames($groups)
  59. {
  60. $groupArray = array();
  61. for ($i=0; $i<$groups["count"]; $i++){ // For each group
  62. $line = $groups[$i];
  63. if (strlen($line)>0) {
  64. // More presumptions, they're all prefixed with CN=
  65. // so we ditch the first three characters and the group
  66. // name goes up to the first comma
  67. $bits=explode(",", $line);
  68. $groupArray[] = substr($bits[0], 3, (strlen($bits[0])-3));
  69. }
  70. }
  71. return $groupArray;
  72. }
  73. /**
  74. * Escape characters for use in an ldap_create function
  75. *
  76. * @param string $str
  77. * @return string
  78. */
  79. public function escapeCharacters($str) {
  80. $str = str_replace(",", "\,", $str);
  81. return $str;
  82. }
  83. /**
  84. * Escape strings for the use in LDAP filters
  85. *
  86. * DEVELOPERS SHOULD BE DOING PROPER FILTERING IF THEY'RE ACCEPTING USER INPUT
  87. * Ported from Perl's Net::LDAP::Util escape_filter_value
  88. *
  89. * @param string $str The string the parse
  90. * @author Port by Andreas Gohr <andi@splitbrain.org>
  91. * @return string
  92. */
  93. public function ldapSlashes($str) {
  94. // see https://github.com/adldap/adLDAP/issues/22
  95. return preg_replace_callback(
  96. '/([\x00-\x1F\*\(\)\\\\])/',
  97. function ($matches) {
  98. return "\\".join("", unpack("H2", $matches[1]));
  99. },
  100. $str
  101. );
  102. }
  103. /**
  104. * Converts a string GUID to a hexdecimal value so it can be queried
  105. *
  106. * @param string $strGUID A string representation of a GUID
  107. * @return string
  108. */
  109. public function strGuidToHex($strGUID)
  110. {
  111. $strGUID = str_replace('-', '', $strGUID);
  112. $octet_str = '\\' . substr($strGUID, 6, 2);
  113. $octet_str .= '\\' . substr($strGUID, 4, 2);
  114. $octet_str .= '\\' . substr($strGUID, 2, 2);
  115. $octet_str .= '\\' . substr($strGUID, 0, 2);
  116. $octet_str .= '\\' . substr($strGUID, 10, 2);
  117. $octet_str .= '\\' . substr($strGUID, 8, 2);
  118. $octet_str .= '\\' . substr($strGUID, 14, 2);
  119. $octet_str .= '\\' . substr($strGUID, 12, 2);
  120. //$octet_str .= '\\' . substr($strGUID, 16, strlen($strGUID));
  121. for ($i=16; $i<=(strlen($strGUID)-2); $i++) {
  122. if (($i % 2) == 0) {
  123. $octet_str .= '\\' . substr($strGUID, $i, 2);
  124. }
  125. }
  126. return $octet_str;
  127. }
  128. /**
  129. * Convert a binary SID to a text SID
  130. *
  131. * @param string $binsid A Binary SID
  132. * @return string
  133. */
  134. public function getTextSID($binsid) {
  135. $hex_sid = bin2hex($binsid);
  136. $rev = hexdec(substr($hex_sid, 0, 2));
  137. $subcount = hexdec(substr($hex_sid, 2, 2));
  138. $auth = hexdec(substr($hex_sid, 4, 12));
  139. $result = "$rev-$auth";
  140. for ($x=0;$x < $subcount; $x++) {
  141. $subauth[$x] =
  142. hexdec($this->littleEndian(substr($hex_sid, 16 + ($x * 8), 8)));
  143. $result .= "-" . $subauth[$x];
  144. }
  145. // Cheat by tacking on the S-
  146. return 'S-' . $result;
  147. }
  148. /**
  149. * Converts a little-endian hex number to one that hexdec() can convert
  150. *
  151. * @param string $hex A hex code
  152. * @return string
  153. */
  154. public function littleEndian($hex)
  155. {
  156. $result = '';
  157. for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
  158. $result .= substr($hex, $x, 2);
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Converts a binary attribute to a string
  164. *
  165. * @param string $bin A binary LDAP attribute
  166. * @return string
  167. */
  168. public function binaryToText($bin)
  169. {
  170. $hex_guid = bin2hex($bin);
  171. $hex_guid_to_guid_str = '';
  172. for($k = 1; $k <= 4; ++$k) {
  173. $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
  174. }
  175. $hex_guid_to_guid_str .= '-';
  176. for($k = 1; $k <= 2; ++$k) {
  177. $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
  178. }
  179. $hex_guid_to_guid_str .= '-';
  180. for($k = 1; $k <= 2; ++$k) {
  181. $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
  182. }
  183. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
  184. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
  185. return strtoupper($hex_guid_to_guid_str);
  186. }
  187. /**
  188. * Converts a binary GUID to a string GUID
  189. *
  190. * @param string $binaryGuid The binary GUID attribute to convert
  191. * @return string
  192. */
  193. public function decodeGuid($binaryGuid)
  194. {
  195. if ($binaryGuid === null){ return "Missing compulsory field [binaryGuid]"; }
  196. $strGUID = $this->binaryToText($binaryGuid);
  197. return $strGUID;
  198. }
  199. /**
  200. * Convert a boolean value to a string
  201. * You should never need to call this yourself
  202. *
  203. * @param bool $bool Boolean value
  204. * @return string
  205. */
  206. public function boolToStr($bool)
  207. {
  208. return ($bool) ? 'TRUE' : 'FALSE';
  209. }
  210. /**
  211. * Convert 8bit characters e.g. accented characters to UTF8 encoded characters
  212. */
  213. public function encode8Bit(&$item, $key) {
  214. $encode = false;
  215. if (is_string($item)) {
  216. for ($i=0; $i<strlen($item); $i++) {
  217. if (ord($item[$i]) >> 7) {
  218. $encode = true;
  219. }
  220. }
  221. }
  222. if ($encode === true && $key != 'password') {
  223. $item = utf8_encode($item);
  224. }
  225. }
  226. /**
  227. * Get the current class version number
  228. *
  229. * @return string
  230. */
  231. public function getVersion() {
  232. return self::ADLDAP_VERSION;
  233. }
  234. /**
  235. * Round a Windows timestamp down to seconds and remove the seconds between 1601-01-01 and 1970-01-01
  236. *
  237. * @param long $windowsTime
  238. * @return long $unixTime
  239. */
  240. public static function convertWindowsTimeToUnixTime($windowsTime) {
  241. $unixTime = round($windowsTime / 10000000) - 11644477200;
  242. return $unixTime;
  243. }
  244. }
  245. ?>