Attribute.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 mixed $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. private static function _valueToLdap($value)
  167. {
  168. if (is_string($value)) return $value;
  169. else if (is_int($value) || is_float($value)) return (string)$value;
  170. else if (is_bool($value)) return ($value === true) ? 'TRUE' : 'FALSE';
  171. else if (is_object($value) || is_array($value)) return serialize($value);
  172. else if (is_resource($value) && get_resource_type($value) === 'stream')
  173. return stream_get_contents($value);
  174. else return null;
  175. }
  176. private static function _valueFromLdap($value)
  177. {
  178. $value = (string)$value;
  179. if ($value === 'TRUE') return true;
  180. else if ($value === 'FALSE') return false;
  181. else return $value;
  182. }
  183. /**
  184. * Converts a PHP data type into its LDAP representation
  185. *
  186. * @param mixed $value
  187. * @return string|null - null if the PHP data type cannot be converted.
  188. */
  189. public static function convertToLdapValue($value)
  190. {
  191. return self::_valueToLdap($value);
  192. }
  193. /**
  194. * Converts an LDAP value into its PHP data type
  195. *
  196. * @param string $value
  197. * @return mixed
  198. */
  199. public static function convertFromLdapValue($value)
  200. {
  201. return self::_valueFromLdap($value);
  202. }
  203. /**
  204. * Converts a timestamp into its LDAP date/time representation
  205. *
  206. * @param integer $value
  207. * @param boolean $utc
  208. * @return string|null - null if the value cannot be converted.
  209. */
  210. public static function convertToLdapDateTimeValue($value, $utc = false)
  211. {
  212. return self::_valueToLdapDateTime($value, $utc);
  213. }
  214. /**
  215. * Converts LDAP date/time representation into a timestamp
  216. *
  217. * @param string $value
  218. * @return integer|null - null if the value cannot be converted.
  219. */
  220. public static function convertFromLdapDateTimeValue($value)
  221. {
  222. return self::_valueFromLdapDateTime($value);
  223. }
  224. /**
  225. * Sets a LDAP password.
  226. *
  227. * @param array $data
  228. * @param string $password
  229. * @param string $hashType
  230. * @param string $attribName
  231. * @return null
  232. */
  233. public static function setPassword(array &$data, $password, $hashType = self::PASSWORD_HASH_MD5,
  234. $attribName = 'userPassword')
  235. {
  236. $hash = self::createPassword($password, $hashType);
  237. self::setAttribute($data, $attribName, $hash, false);
  238. }
  239. /**
  240. * Creates a LDAP password.
  241. *
  242. * @param string $password
  243. * @param string $hashType
  244. * @return string
  245. */
  246. public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
  247. {
  248. switch ($hashType) {
  249. case self::PASSWORD_HASH_SHA:
  250. $rawHash = sha1($password, true);
  251. $method = '{SHA}';
  252. break;
  253. case self::PASSWORD_HASH_MD5:
  254. default:
  255. $rawHash = md5($password, true);
  256. $method = '{MD5}';
  257. break;
  258. }
  259. return $method . base64_encode($rawHash);
  260. }
  261. /**
  262. * Sets a LDAP date/time attribute.
  263. *
  264. * @param array $data
  265. * @param string $attribName
  266. * @param integer|array $value
  267. * @param boolean $utc
  268. * @param boolean $append
  269. * @return null
  270. */
  271. public static function setDateTimeAttribute(array &$data, $attribName, $value, $utc = false,
  272. $append = false)
  273. {
  274. $convertedValues = array();
  275. if (is_array($value) || ($value instanceof Traversable))
  276. {
  277. foreach ($value as $v) {
  278. $v = self::_valueToLdapDateTime($v, $utc);
  279. if (!is_null($v)) $convertedValues[] = $v;
  280. }
  281. }
  282. else if (!is_null($value)) {
  283. $value = self::_valueToLdapDateTime($value, $utc);
  284. if (!is_null($value)) $convertedValues[] = $value;
  285. }
  286. self::setAttribute($data, $attribName, $convertedValues, $append);
  287. }
  288. private static function _valueToLdapDateTime($value, $utc)
  289. {
  290. if (is_int($value)) {
  291. if ($utc === true) return gmdate('YmdHis', $value) . 'Z';
  292. else return date('YmdHisO', $value);
  293. }
  294. else return null;
  295. }
  296. /**
  297. * Gets a LDAP date/time attribute.
  298. *
  299. * @param array $data
  300. * @param string $attribName
  301. * @param integer $index
  302. * @return array|integer
  303. */
  304. public static function getDateTimeAttribute(array $data, $attribName, $index = null)
  305. {
  306. $values = self::getAttribute($data, $attribName, $index);
  307. if (is_array($values)) {
  308. for ($i = 0; $i<count($values); $i++) {
  309. $newVal = self::_valueFromLdapDateTime($values[$i]);
  310. if (!is_null($newVal)) $values[$i] = $newVal;
  311. }
  312. }
  313. else $values = self::_valueFromLdapDateTime($values);
  314. return $values;
  315. }
  316. private static function _valueFromLdapDateTime($value)
  317. {
  318. $matches = array();
  319. if (preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})([+-]\d{4}|Z)$/', $value, $matches)) {
  320. $year = $matches[1];
  321. $month = $matches[2];
  322. $day = $matches[3];
  323. $hour = $matches[4];
  324. $minute = $matches[5];
  325. $second = $matches[6];
  326. $timezone = $matches[7];
  327. $date = gmmktime($hour, $minute, $second, $month, $day, $year);
  328. if ($timezone !== 'Z') {
  329. $tzDirection = substr($timezone, 0, 1);
  330. $tzOffsetHour = substr($timezone, 1, 2);
  331. $tzOffsetMinute = substr($timezone, 3, 2);
  332. $tzOffset = ($tzOffsetHour*60*60) + ($tzOffsetMinute*60);
  333. if ($tzDirection == '+') $date -= $tzOffset;
  334. else if ($tzDirection == '-') $date += $tzOffset;
  335. }
  336. return $date;
  337. }
  338. else return null;
  339. }
  340. }