Attribute.php 11 KB

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