PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/adLDAP/src/classes/adLDAPUtils.php

https://bitbucket.org/bertramtruong/phpipam
PHP | 264 lines | 178 code | 11 blank | 75 comment | 5 complexity | 27bf7c5ef357500ef7fbc75a565a5d0a MD5 | raw file
  1. <?php
  2. /**
  3. * PHP LDAP CLASS FOR MANIPULATING ACTIVE DIRECTORY
  4. * Version 4.0.3
  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-2011 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-2011 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.3
  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.3';
  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. return preg_replace('/([\x00-\x1F\*\(\)\\\\])/e',
  95. '"\\\\\".join("",unpack("H2","$1"))',
  96. $str);
  97. }
  98. /**
  99. * Converts a string GUID to a hexdecimal value so it can be queried
  100. *
  101. * @param string $strGUID A string representation of a GUID
  102. * @return string
  103. */
  104. public function strGuidToHex($strGUID)
  105. {
  106. $strGUID = str_replace('-', '', $strGUID);
  107. $octet_str = '\\' . substr($strGUID, 6, 2);
  108. $octet_str .= '\\' . substr($strGUID, 4, 2);
  109. $octet_str .= '\\' . substr($strGUID, 2, 2);
  110. $octet_str .= '\\' . substr($strGUID, 0, 2);
  111. $octet_str .= '\\' . substr($strGUID, 10, 2);
  112. $octet_str .= '\\' . substr($strGUID, 8, 2);
  113. $octet_str .= '\\' . substr($strGUID, 14, 2);
  114. $octet_str .= '\\' . substr($strGUID, 12, 2);
  115. //$octet_str .= '\\' . substr($strGUID, 16, strlen($strGUID));
  116. for ($i=16; $i<=(strlen($strGUID)-2); $i++) {
  117. if (($i % 2) == 0) {
  118. $octet_str .= '\\' . substr($strGUID, $i, 2);
  119. }
  120. }
  121. return $octet_str;
  122. }
  123. /**
  124. * Convert a binary SID to a text SID
  125. *
  126. * @param string $binsid A Binary SID
  127. * @return string
  128. */
  129. public function getTextSID($binsid) {
  130. $hex_sid = bin2hex($binsid);
  131. $rev = hexdec(substr($hex_sid, 0, 2));
  132. $subcount = hexdec(substr($hex_sid, 2, 2));
  133. $auth = hexdec(substr($hex_sid, 4, 12));
  134. $result = "$rev-$auth";
  135. for ($x=0;$x < $subcount; $x++) {
  136. $subauth[$x] =
  137. hexdec($this->littleEndian(substr($hex_sid, 16 + ($x * 8), 8)));
  138. $result .= "-" . $subauth[$x];
  139. }
  140. // Cheat by tacking on the S-
  141. return 'S-' . $result;
  142. }
  143. /**
  144. * Converts a little-endian hex number to one that hexdec() can convert
  145. *
  146. * @param string $hex A hex code
  147. * @return string
  148. */
  149. public function littleEndian($hex)
  150. {
  151. $result = '';
  152. for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
  153. $result .= substr($hex, $x, 2);
  154. }
  155. return $result;
  156. }
  157. /**
  158. * Converts a binary attribute to a string
  159. *
  160. * @param string $bin A binary LDAP attribute
  161. * @return string
  162. */
  163. public function binaryToText($bin)
  164. {
  165. $hex_guid = bin2hex($bin);
  166. $hex_guid_to_guid_str = '';
  167. for($k = 1; $k <= 4; ++$k) {
  168. $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
  169. }
  170. $hex_guid_to_guid_str .= '-';
  171. for($k = 1; $k <= 2; ++$k) {
  172. $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
  173. }
  174. $hex_guid_to_guid_str .= '-';
  175. for($k = 1; $k <= 2; ++$k) {
  176. $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
  177. }
  178. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
  179. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
  180. return strtoupper($hex_guid_to_guid_str);
  181. }
  182. /**
  183. * Converts a binary GUID to a string GUID
  184. *
  185. * @param string $binaryGuid The binary GUID attribute to convert
  186. * @return string
  187. */
  188. public function decodeGuid($binaryGuid)
  189. {
  190. if ($binaryGuid === null){ return "Missing compulsory field [binaryGuid]"; }
  191. $strGUID = $this->binaryToText($binaryGuid);
  192. return $strGUID;
  193. }
  194. /**
  195. * Convert a boolean value to a string
  196. * You should never need to call this yourself
  197. *
  198. * @param bool $bool Boolean value
  199. * @return string
  200. */
  201. public function boolToStr($bool)
  202. {
  203. return ($bool) ? 'TRUE' : 'FALSE';
  204. }
  205. /**
  206. * Convert 8bit characters e.g. accented characters to UTF8 encoded characters
  207. */
  208. public function encode8Bit(&$item, $key) {
  209. $encode = false;
  210. if (is_string($item)) {
  211. for ($i=0; $i<strlen($item); $i++) {
  212. if (ord($item[$i]) >> 7) {
  213. $encode = true;
  214. }
  215. }
  216. }
  217. if ($encode === true && $key != 'password') {
  218. $item = utf8_encode($item);
  219. }
  220. }
  221. /**
  222. * Get the current class version number
  223. *
  224. * @return string
  225. */
  226. public function getVersion() {
  227. return self::ADLDAP_VERSION;
  228. }
  229. /**
  230. * Round a Windows timestamp down to seconds and remove the seconds between 1601-01-01 and 1970-01-01
  231. *
  232. * @param long $windowsTime
  233. * @return long $unixTime
  234. */
  235. public function convertWindowsTimeToUnixTime($windowsTime) {
  236. $unixTime = round($windowsTime / 10000000) - 11644477200;
  237. return $unixTime;
  238. }
  239. }
  240. ?>