PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/library/Zend/Ldap/Attribute.php

http://github.com/frapi/frapi
PHP | 420 lines | 249 code | 25 blank | 146 comment | 73 complexity | 3855179865cc5ccda83a9118160c755f MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Attribute.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * Zend_Ldap_Attribute is a collection of LDAP attribute related functions.
  23. *
  24. * @category Zend
  25. * @package Zend_Ldap
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Ldap_Attribute
  30. {
  31. const PASSWORD_HASH_MD5 = 'md5';
  32. const PASSWORD_HASH_SMD5 = 'smd5';
  33. const PASSWORD_HASH_SHA = 'sha';
  34. const PASSWORD_HASH_SSHA = 'ssha';
  35. const PASSWORD_UNICODEPWD = 'unicodePwd';
  36. /**
  37. * Sets a LDAP attribute.
  38. *
  39. * @param array $data
  40. * @param string $attribName
  41. * @param scalar|array|Traversable $value
  42. * @param boolean $append
  43. * @return void
  44. */
  45. public static function setAttribute(array &$data, $attribName, $value, $append = false)
  46. {
  47. $attribName = strtolower($attribName);
  48. $valArray = array();
  49. if (is_array($value) || ($value instanceof Traversable))
  50. {
  51. foreach ($value as $v)
  52. {
  53. $v = self::_valueToLdap($v);
  54. if (!is_null($v)) $valArray[] = $v;
  55. }
  56. }
  57. else if (!is_null($value))
  58. {
  59. $value = self::_valueToLdap($value);
  60. if (!is_null($value)) $valArray[] = $value;
  61. }
  62. if ($append === true && isset($data[$attribName]))
  63. {
  64. if (is_string($data[$attribName])) $data[$attribName] = array($data[$attribName]);
  65. $data[$attribName] = array_merge($data[$attribName], $valArray);
  66. }
  67. else
  68. {
  69. $data[$attribName] = $valArray;
  70. }
  71. }
  72. /**
  73. * Gets a LDAP attribute.
  74. *
  75. * @param array $data
  76. * @param string $attribName
  77. * @param integer $index
  78. * @return array|mixed
  79. */
  80. public static function getAttribute(array $data, $attribName, $index = null)
  81. {
  82. $attribName = strtolower($attribName);
  83. if (is_null($index)) {
  84. if (!isset($data[$attribName])) return array();
  85. $retArray = array();
  86. foreach ($data[$attribName] as $v)
  87. {
  88. $retArray[] = self::_valueFromLdap($v);
  89. }
  90. return $retArray;
  91. } else if (is_int($index)) {
  92. if (!isset($data[$attribName])) {
  93. return null;
  94. } else if ($index >= 0 && $index<count($data[$attribName])) {
  95. return self::_valueFromLdap($data[$attribName][$index]);
  96. } else {
  97. return null;
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Checks if the given value(s) exist in the attribute
  104. *
  105. * @param array $data
  106. * @param string $attribName
  107. * @param mixed|array $value
  108. * @return boolean
  109. */
  110. public static function attributeHasValue(array &$data, $attribName, $value)
  111. {
  112. $attribName = strtolower($attribName);
  113. if (!isset($data[$attribName])) return false;
  114. if (is_scalar($value)) {
  115. $value = array($value);
  116. }
  117. foreach ($value as $v) {
  118. $v = self::_valueToLdap($v);
  119. if (!in_array($v, $data[$attribName], true)) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. /**
  126. * Removes duplicate values from a LDAP attribute
  127. *
  128. * @param array $data
  129. * @param string $attribName
  130. * @return void
  131. */
  132. public static function removeDuplicatesFromAttribute(array &$data, $attribName)
  133. {
  134. $attribName = strtolower($attribName);
  135. if (!isset($data[$attribName])) return;
  136. $data[$attribName] = array_values(array_unique($data[$attribName]));
  137. }
  138. /**
  139. * Remove given values from a LDAP attribute
  140. *
  141. * @param array $data
  142. * @param string $attribName
  143. * @param mixed|array $value
  144. * @return void
  145. */
  146. public static function removeFromAttribute(array &$data, $attribName, $value)
  147. {
  148. $attribName = strtolower($attribName);
  149. if (!isset($data[$attribName])) return;
  150. if (is_scalar($value)) {
  151. $value = array($value);
  152. }
  153. $valArray = array();
  154. foreach ($value as $v)
  155. {
  156. $v = self::_valueToLdap($v);
  157. if ($v !== null) $valArray[] = $v;
  158. }
  159. $resultArray = $data[$attribName];
  160. foreach ($valArray as $rv) {
  161. $keys = array_keys($resultArray, $rv);
  162. foreach ($keys as $k) {
  163. unset($resultArray[$k]);
  164. }
  165. }
  166. $resultArray = array_values($resultArray);
  167. $data[$attribName] = $resultArray;
  168. }
  169. /**
  170. * @param mixed $value
  171. * @return string|null
  172. */
  173. private static function _valueToLdap($value)
  174. {
  175. if (is_string($value)) return $value;
  176. else if (is_int($value) || is_float($value)) return (string)$value;
  177. else if (is_bool($value)) return ($value === true) ? 'TRUE' : 'FALSE';
  178. else if (is_object($value) || is_array($value)) return serialize($value);
  179. else if (is_resource($value) && get_resource_type($value) === 'stream')
  180. return stream_get_contents($value);
  181. else return null;
  182. }
  183. /**
  184. * @param string $value
  185. * @return string|boolean
  186. */
  187. private static function _valueFromLdap($value)
  188. {
  189. $value = (string)$value;
  190. if ($value === 'TRUE') return true;
  191. else if ($value === 'FALSE') return false;
  192. else return $value;
  193. }
  194. /**
  195. * Converts a PHP data type into its LDAP representation
  196. *
  197. * @param mixed $value
  198. * @return string|null - null if the PHP data type cannot be converted.
  199. */
  200. public static function convertToLdapValue($value)
  201. {
  202. return self::_valueToLdap($value);
  203. }
  204. /**
  205. * Converts an LDAP value into its PHP data type
  206. *
  207. * @param string $value
  208. * @return mixed
  209. */
  210. public static function convertFromLdapValue($value)
  211. {
  212. return self::_valueFromLdap($value);
  213. }
  214. /**
  215. * Converts a timestamp into its LDAP date/time representation
  216. *
  217. * @param integer $value
  218. * @param boolean $utc
  219. * @return string|null - null if the value cannot be converted.
  220. */
  221. public static function convertToLdapDateTimeValue($value, $utc = false)
  222. {
  223. return self::_valueToLdapDateTime($value, $utc);
  224. }
  225. /**
  226. * Converts LDAP date/time representation into a timestamp
  227. *
  228. * @param string $value
  229. * @return integer|null - null if the value cannot be converted.
  230. */
  231. public static function convertFromLdapDateTimeValue($value)
  232. {
  233. return self::_valueFromLdapDateTime($value);
  234. }
  235. /**
  236. * Sets a LDAP password.
  237. *
  238. * @param array $data
  239. * @param string $password
  240. * @param string $hashType
  241. * @param string|null $attribName
  242. * @return null
  243. */
  244. public static function setPassword(array &$data, $password, $hashType = self::PASSWORD_HASH_MD5,
  245. $attribName = null)
  246. {
  247. if ($attribName === null) {
  248. if ($hashType === self::PASSWORD_UNICODEPWD) {
  249. $attribName = 'unicodePwd';
  250. } else {
  251. $attribName = 'userPassword';
  252. }
  253. }
  254. $hash = self::createPassword($password, $hashType);
  255. self::setAttribute($data, $attribName, $hash, false);
  256. }
  257. /**
  258. * Creates a LDAP password.
  259. *
  260. * @param string $password
  261. * @param string $hashType
  262. * @return string
  263. */
  264. public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
  265. {
  266. switch ($hashType) {
  267. case self::PASSWORD_UNICODEPWD:
  268. /* see:
  269. * http://msdn.microsoft.com/en-us/library/cc223248(PROT.10).aspx
  270. */
  271. $password = '"' . $password . '"';
  272. if (function_exists('mb_convert_encoding')) {
  273. $password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
  274. } else if (function_exists('iconv')) {
  275. $password = iconv('UTF-8', 'UTF-16LE', $password);
  276. } else {
  277. $len = strlen($password);
  278. $new = '';
  279. for($i=0; $i < $len; $i++) {
  280. $new .= $password[$i] . "\x00";
  281. }
  282. $password = $new;
  283. }
  284. return $password;
  285. case self::PASSWORD_HASH_SSHA:
  286. $salt = substr(sha1(uniqid(mt_rand(), true), true), 0, 4);
  287. $rawHash = sha1($password . $salt, true) . $salt;
  288. $method = '{SSHA}';
  289. break;
  290. case self::PASSWORD_HASH_SHA:
  291. $rawHash = sha1($password, true);
  292. $method = '{SHA}';
  293. break;
  294. case self::PASSWORD_HASH_SMD5:
  295. $salt = substr(sha1(uniqid(mt_rand(), true), true), 0, 4);
  296. $rawHash = md5($password . $salt, true) . $salt;
  297. $method = '{SMD5}';
  298. break;
  299. case self::PASSWORD_HASH_MD5:
  300. default:
  301. $rawHash = md5($password, true);
  302. $method = '{MD5}';
  303. break;
  304. }
  305. return $method . base64_encode($rawHash);
  306. }
  307. /**
  308. * Sets a LDAP date/time attribute.
  309. *
  310. * @param array $data
  311. * @param string $attribName
  312. * @param integer|array|Traversable $value
  313. * @param boolean $utc
  314. * @param boolean $append
  315. * @return null
  316. */
  317. public static function setDateTimeAttribute(array &$data, $attribName, $value, $utc = false,
  318. $append = false)
  319. {
  320. $convertedValues = array();
  321. if (is_array($value) || ($value instanceof Traversable))
  322. {
  323. foreach ($value as $v) {
  324. $v = self::_valueToLdapDateTime($v, $utc);
  325. if (!is_null($v)) $convertedValues[] = $v;
  326. }
  327. }
  328. else if (!is_null($value)) {
  329. $value = self::_valueToLdapDateTime($value, $utc);
  330. if (!is_null($value)) $convertedValues[] = $value;
  331. }
  332. self::setAttribute($data, $attribName, $convertedValues, $append);
  333. }
  334. /**
  335. * @param integer $value
  336. * @param boolean $utc
  337. * @return string|null
  338. */
  339. private static function _valueToLdapDateTime($value, $utc)
  340. {
  341. if (is_int($value)) {
  342. if ($utc === true) return gmdate('YmdHis', $value) . 'Z';
  343. else return date('YmdHisO', $value);
  344. }
  345. else return null;
  346. }
  347. /**
  348. * Gets a LDAP date/time attribute.
  349. *
  350. * @param array $data
  351. * @param string $attribName
  352. * @param integer $index
  353. * @return array|integer
  354. */
  355. public static function getDateTimeAttribute(array $data, $attribName, $index = null)
  356. {
  357. $values = self::getAttribute($data, $attribName, $index);
  358. if (is_array($values)) {
  359. for ($i = 0; $i<count($values); $i++) {
  360. $newVal = self::_valueFromLdapDateTime($values[$i]);
  361. if (!is_null($newVal)) $values[$i] = $newVal;
  362. }
  363. }
  364. else $values = self::_valueFromLdapDateTime($values);
  365. return $values;
  366. }
  367. /**
  368. * @param string $value
  369. * @return integer|null
  370. */
  371. private static function _valueFromLdapDateTime($value)
  372. {
  373. $matches = array();
  374. if (preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})([+-]\d{4}|Z)$/', $value, $matches)) {
  375. $year = $matches[1];
  376. $month = $matches[2];
  377. $day = $matches[3];
  378. $hour = $matches[4];
  379. $minute = $matches[5];
  380. $second = $matches[6];
  381. $timezone = $matches[7];
  382. $date = gmmktime($hour, $minute, $second, $month, $day, $year);
  383. if ($timezone !== 'Z') {
  384. $tzDirection = substr($timezone, 0, 1);
  385. $tzOffsetHour = substr($timezone, 1, 2);
  386. $tzOffsetMinute = substr($timezone, 3, 2);
  387. $tzOffset = ($tzOffsetHour*60*60) + ($tzOffsetMinute*60);
  388. if ($tzDirection == '+') $date -= $tzOffset;
  389. else if ($tzDirection == '-') $date += $tzOffset;
  390. }
  391. return $date;
  392. }
  393. else return null;
  394. }
  395. }