Attribute.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2008 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. /**
  36. * Sets a LDAP attribute.
  37. *
  38. * @param array $data
  39. * @param string $attribName
  40. * @param scalar|array|Traversable $value
  41. * @param boolean $append
  42. * @return void
  43. */
  44. public static function setAttribute(array &$data, $attribName, $value, $append = false)
  45. {
  46. $attribName = strtolower($attribName);
  47. $valArray = array();
  48. if (is_array($value) || ($value instanceof Traversable))
  49. {
  50. foreach ($value as $v)
  51. {
  52. $v = self::_valueToLdap($v);
  53. if (!is_null($v)) $valArray[] = $v;
  54. }
  55. }
  56. else if (!is_null($value))
  57. {
  58. $value = self::_valueToLdap($value);
  59. if (!is_null($value)) $valArray[] = $value;
  60. }
  61. if ($append === true && isset($data[$attribName]))
  62. {
  63. if (is_string($data[$attribName])) $data[$attribName] = array($data[$attribName]);
  64. $data[$attribName] = array_merge($data[$attribName], $valArray);
  65. }
  66. else
  67. {
  68. $data[$attribName] = $valArray;
  69. }
  70. }
  71. /**
  72. * Gets a LDAP attribute.
  73. *
  74. * @param array $data
  75. * @param string $attribName
  76. * @param integer $index
  77. * @return array|mixed
  78. */
  79. public static function getAttribute(array $data, $attribName, $index = null)
  80. {
  81. $attribName = strtolower($attribName);
  82. if (is_null($index)) {
  83. if (!isset($data[$attribName])) return array();
  84. $retArray = array();
  85. foreach ($data[$attribName] as $v)
  86. {
  87. $retArray[] = self::_valueFromLdap($v);
  88. }
  89. return $retArray;
  90. } else if (is_int($index)) {
  91. if (!isset($data[$attribName])) {
  92. return null;
  93. } else if ($index >= 0 && $index<count($data[$attribName])) {
  94. return self::_valueFromLdap($data[$attribName][$index]);
  95. } else {
  96. return null;
  97. }
  98. }
  99. return null;
  100. }
  101. /**
  102. * Checks if the given value(s) exist in the attribute
  103. *
  104. * @param array $data
  105. * @param string $attribName
  106. * @param mixed|array $value
  107. * @return boolean
  108. */
  109. public static function attributeHasValue(array &$data, $attribName, $value)
  110. {
  111. $attribName = strtolower($attribName);
  112. if (!isset($data[$attribName])) return false;
  113. if (is_scalar($value)) {
  114. $value = array($value);
  115. }
  116. foreach ($value as $v) {
  117. $v = self::_valueToLdap($v);
  118. if (!in_array($v, $data[$attribName], true)) {
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. /**
  125. * Removes duplicate values from a LDAP attribute
  126. *
  127. * @param array $data
  128. * @param string $attribName
  129. * @return void
  130. */
  131. public static function removeDuplicatesFromAttribute(array &$data, $attribName)
  132. {
  133. $attribName = strtolower($attribName);
  134. if (!isset($data[$attribName])) return;
  135. $data[$attribName] = array_values(array_unique($data[$attribName]));
  136. }
  137. /**
  138. * Remove given values from a LDAP attribute
  139. *
  140. * @param array $data
  141. * @param string $attribName
  142. * @param mixed|array $value
  143. * @return void
  144. */
  145. public static function removeFromAttribute(array &$data, $attribName, $value)
  146. {
  147. $attribName = strtolower($attribName);
  148. if (!isset($data[$attribName])) return;
  149. if (is_scalar($value)) {
  150. $value = array($value);
  151. }
  152. $valArray = array();
  153. foreach ($value as $v)
  154. {
  155. $v = self::_valueToLdap($v);
  156. if ($v !== null) $valArray[] = $v;
  157. }
  158. $resultArray = $data[$attribName];
  159. foreach ($valArray as $rv) {
  160. $keys = array_keys($resultArray, $rv);
  161. foreach ($keys as $k) {
  162. unset($resultArray[$k]);
  163. }
  164. }
  165. $resultArray = array_values($resultArray);
  166. $data[$attribName] = $resultArray;
  167. }
  168. /**
  169. * @param mixed $value
  170. * @return string|null
  171. */
  172. private static function _valueToLdap($value)
  173. {
  174. if (is_string($value)) return $value;
  175. else if (is_int($value) || is_float($value)) return (string)$value;
  176. else if (is_bool($value)) return ($value === true) ? 'TRUE' : 'FALSE';
  177. else if (is_object($value) || is_array($value)) return serialize($value);
  178. else if (is_resource($value) && get_resource_type($value) === 'stream')
  179. return stream_get_contents($value);
  180. else return null;
  181. }
  182. /**
  183. * @param string $value
  184. * @return string|boolean
  185. */
  186. private static function _valueFromLdap($value)
  187. {
  188. $value = (string)$value;
  189. if ($value === 'TRUE') return true;
  190. else if ($value === 'FALSE') return false;
  191. else return $value;
  192. }
  193. /**
  194. * Converts a PHP data type into its LDAP representation
  195. *
  196. * @param mixed $value
  197. * @return string|null - null if the PHP data type cannot be converted.
  198. */
  199. public static function convertToLdapValue($value)
  200. {
  201. return self::_valueToLdap($value);
  202. }
  203. /**
  204. * Converts an LDAP value into its PHP data type
  205. *
  206. * @param string $value
  207. * @return mixed
  208. */
  209. public static function convertFromLdapValue($value)
  210. {
  211. return self::_valueFromLdap($value);
  212. }
  213. /**
  214. * Converts a timestamp into its LDAP date/time representation
  215. *
  216. * @param integer $value
  217. * @param boolean $utc
  218. * @return string|null - null if the value cannot be converted.
  219. */
  220. public static function convertToLdapDateTimeValue($value, $utc = false)
  221. {
  222. return self::_valueToLdapDateTime($value, $utc);
  223. }
  224. /**
  225. * Converts LDAP date/time representation into a timestamp
  226. *
  227. * @param string $value
  228. * @return integer|null - null if the value cannot be converted.
  229. */
  230. public static function convertFromLdapDateTimeValue($value)
  231. {
  232. return self::_valueFromLdapDateTime($value);
  233. }
  234. /**
  235. * Sets a LDAP password.
  236. *
  237. * @param array $data
  238. * @param string $password
  239. * @param string $hashType
  240. * @param string $attribName
  241. * @return null
  242. */
  243. public static function setPassword(array &$data, $password, $hashType = self::PASSWORD_HASH_MD5,
  244. $attribName = 'userPassword')
  245. {
  246. $hash = self::createPassword($password, $hashType);
  247. self::setAttribute($data, $attribName, $hash, false);
  248. }
  249. /**
  250. * Creates a LDAP password.
  251. *
  252. * @param string $password
  253. * @param string $hashType
  254. * @return string
  255. */
  256. public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
  257. {
  258. switch ($hashType) {
  259. case self::PASSWORD_HASH_SSHA:
  260. $salt = substr(sha1(uniqid(mt_rand(), true), true), 0, 4);
  261. $rawHash = sha1($password . $salt, true) . $salt;
  262. $method = '{SSHA}';
  263. break;
  264. case self::PASSWORD_HASH_SHA:
  265. $rawHash = sha1($password, true);
  266. $method = '{SHA}';
  267. break;
  268. case self::PASSWORD_HASH_SMD5:
  269. $salt = substr(sha1(uniqid(mt_rand(), true), true), 0, 4);
  270. $rawHash = md5($password . $salt, true) . $salt;
  271. $method = '{SMD5}';
  272. break;
  273. case self::PASSWORD_HASH_MD5:
  274. default:
  275. $rawHash = md5($password, true);
  276. $method = '{MD5}';
  277. break;
  278. }
  279. return $method . base64_encode($rawHash);
  280. }
  281. /**
  282. * Sets a LDAP date/time attribute.
  283. *
  284. * @param array $data
  285. * @param string $attribName
  286. * @param integer|array|Traversable $value
  287. * @param boolean $utc
  288. * @param boolean $append
  289. * @return null
  290. */
  291. public static function setDateTimeAttribute(array &$data, $attribName, $value, $utc = false,
  292. $append = false)
  293. {
  294. $convertedValues = array();
  295. if (is_array($value) || ($value instanceof Traversable))
  296. {
  297. foreach ($value as $v) {
  298. $v = self::_valueToLdapDateTime($v, $utc);
  299. if (!is_null($v)) $convertedValues[] = $v;
  300. }
  301. }
  302. else if (!is_null($value)) {
  303. $value = self::_valueToLdapDateTime($value, $utc);
  304. if (!is_null($value)) $convertedValues[] = $value;
  305. }
  306. self::setAttribute($data, $attribName, $convertedValues, $append);
  307. }
  308. /**
  309. * @param integer $value
  310. * @param boolean $utc
  311. * @return string|null
  312. */
  313. private static function _valueToLdapDateTime($value, $utc)
  314. {
  315. if (is_int($value)) {
  316. if ($utc === true) return gmdate('YmdHis', $value) . 'Z';
  317. else return date('YmdHisO', $value);
  318. }
  319. else return null;
  320. }
  321. /**
  322. * Gets a LDAP date/time attribute.
  323. *
  324. * @param array $data
  325. * @param string $attribName
  326. * @param integer $index
  327. * @return array|integer
  328. */
  329. public static function getDateTimeAttribute(array $data, $attribName, $index = null)
  330. {
  331. $values = self::getAttribute($data, $attribName, $index);
  332. if (is_array($values)) {
  333. for ($i = 0; $i<count($values); $i++) {
  334. $newVal = self::_valueFromLdapDateTime($values[$i]);
  335. if (!is_null($newVal)) $values[$i] = $newVal;
  336. }
  337. }
  338. else $values = self::_valueFromLdapDateTime($values);
  339. return $values;
  340. }
  341. /**
  342. * @param string $value
  343. * @return integer|null
  344. */
  345. private static function _valueFromLdapDateTime($value)
  346. {
  347. $matches = array();
  348. if (preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})([+-]\d{4}|Z)$/', $value, $matches)) {
  349. $year = $matches[1];
  350. $month = $matches[2];
  351. $day = $matches[3];
  352. $hour = $matches[4];
  353. $minute = $matches[5];
  354. $second = $matches[6];
  355. $timezone = $matches[7];
  356. $date = gmmktime($hour, $minute, $second, $month, $day, $year);
  357. if ($timezone !== 'Z') {
  358. $tzDirection = substr($timezone, 0, 1);
  359. $tzOffsetHour = substr($timezone, 1, 2);
  360. $tzOffsetMinute = substr($timezone, 3, 2);
  361. $tzOffset = ($tzOffsetHour*60*60) + ($tzOffsetMinute*60);
  362. if ($tzDirection == '+') $date -= $tzOffset;
  363. else if ($tzDirection == '-') $date += $tzOffset;
  364. }
  365. return $date;
  366. }
  367. else return null;
  368. }
  369. }