PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/zendframework1/library/Zend/Ldap/Attribute.php

https://bitbucket.org/sergiu-tot-fb/vendors
PHP | 420 lines | 240 code | 27 blank | 153 comment | 53 complexity | 0118d7cc8b321fd232ec34b201dbd675 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, Apache-2.0
  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-2015 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. * @see Zend_Ldap_Converter
  23. */
  24. #require_once 'Zend/Ldap/Converter.php';
  25. /** @see Zend_Crypt_Math */
  26. require_once 'Zend/Crypt/Math.php';
  27. /**
  28. * Zend_Ldap_Attribute is a collection of LDAP attribute related functions.
  29. *
  30. * @category Zend
  31. * @package Zend_Ldap
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Ldap_Attribute
  36. {
  37. const PASSWORD_HASH_MD5 = 'md5';
  38. const PASSWORD_HASH_SMD5 = 'smd5';
  39. const PASSWORD_HASH_SHA = 'sha';
  40. const PASSWORD_HASH_SSHA = 'ssha';
  41. const PASSWORD_UNICODEPWD = 'unicodePwd';
  42. /**
  43. * Sets a LDAP attribute.
  44. *
  45. * @param array $data
  46. * @param string $attribName
  47. * @param scalar|array|Traversable $value
  48. * @param boolean $append
  49. * @return void
  50. */
  51. public static function setAttribute(array &$data, $attribName, $value, $append = false)
  52. {
  53. $attribName = strtolower($attribName);
  54. $valArray = array();
  55. if (is_array($value) || ($value instanceof Traversable))
  56. {
  57. foreach ($value as $v)
  58. {
  59. $v = self::_valueToLdap($v);
  60. if ($v !== null) $valArray[] = $v;
  61. }
  62. }
  63. else if ($value !== null)
  64. {
  65. $value = self::_valueToLdap($value);
  66. if ($value !== null) $valArray[] = $value;
  67. }
  68. if ($append === true && isset($data[$attribName]))
  69. {
  70. if (is_string($data[$attribName])) $data[$attribName] = array($data[$attribName]);
  71. $data[$attribName] = array_merge($data[$attribName], $valArray);
  72. }
  73. else
  74. {
  75. $data[$attribName] = $valArray;
  76. }
  77. }
  78. /**
  79. * Gets a LDAP attribute.
  80. *
  81. * @param array $data
  82. * @param string $attribName
  83. * @param integer $index
  84. * @return array|mixed
  85. */
  86. public static function getAttribute(array $data, $attribName, $index = null)
  87. {
  88. $attribName = strtolower($attribName);
  89. if ($index === null) {
  90. if (!isset($data[$attribName])) return array();
  91. $retArray = array();
  92. foreach ($data[$attribName] as $v)
  93. {
  94. $retArray[] = self::_valueFromLdap($v);
  95. }
  96. return $retArray;
  97. } else if (is_int($index)) {
  98. if (!isset($data[$attribName])) {
  99. return null;
  100. } else if ($index >= 0 && $index<count($data[$attribName])) {
  101. return self::_valueFromLdap($data[$attribName][$index]);
  102. } else {
  103. return null;
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * Checks if the given value(s) exist in the attribute
  110. *
  111. * @param array $data
  112. * @param string $attribName
  113. * @param mixed|array $value
  114. * @return boolean
  115. */
  116. public static function attributeHasValue(array &$data, $attribName, $value)
  117. {
  118. $attribName = strtolower($attribName);
  119. if (!isset($data[$attribName])) return false;
  120. if (is_scalar($value)) {
  121. $value = array($value);
  122. }
  123. foreach ($value as $v) {
  124. $v = self::_valueToLdap($v);
  125. if (!in_array($v, $data[$attribName], true)) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * Removes duplicate values from a LDAP attribute
  133. *
  134. * @param array $data
  135. * @param string $attribName
  136. * @return void
  137. */
  138. public static function removeDuplicatesFromAttribute(array &$data, $attribName)
  139. {
  140. $attribName = strtolower($attribName);
  141. if (!isset($data[$attribName])) return;
  142. $data[$attribName] = array_values(array_unique($data[$attribName]));
  143. }
  144. /**
  145. * Remove given values from a LDAP attribute
  146. *
  147. * @param array $data
  148. * @param string $attribName
  149. * @param mixed|array $value
  150. * @return void
  151. */
  152. public static function removeFromAttribute(array &$data, $attribName, $value)
  153. {
  154. $attribName = strtolower($attribName);
  155. if (!isset($data[$attribName])) return;
  156. if (is_scalar($value)) {
  157. $value = array($value);
  158. }
  159. $valArray = array();
  160. foreach ($value as $v)
  161. {
  162. $v = self::_valueToLdap($v);
  163. if ($v !== null) $valArray[] = $v;
  164. }
  165. $resultArray = $data[$attribName];
  166. foreach ($valArray as $rv) {
  167. $keys = array_keys($resultArray, $rv);
  168. foreach ($keys as $k) {
  169. unset($resultArray[$k]);
  170. }
  171. }
  172. $resultArray = array_values($resultArray);
  173. $data[$attribName] = $resultArray;
  174. }
  175. /**
  176. * @param mixed $value
  177. * @return string|null
  178. */
  179. private static function _valueToLdap($value)
  180. {
  181. return Zend_Ldap_Converter::toLdap($value);
  182. }
  183. /**
  184. * @param string $value
  185. * @return mixed
  186. */
  187. private static function _valueFromLdap($value)
  188. {
  189. try {
  190. $return = Zend_Ldap_Converter::fromLdap($value, Zend_Ldap_Converter::STANDARD, false);
  191. if ($return instanceof DateTime) {
  192. return Zend_Ldap_Converter::toLdapDateTime($return, false);
  193. } else {
  194. return $return;
  195. }
  196. } catch (InvalidArgumentException $e) {
  197. return $value;
  198. }
  199. }
  200. /**
  201. * Converts a PHP data type into its LDAP representation
  202. *
  203. * @deprected use Zend_Ldap_Converter instead
  204. * @param mixed $value
  205. * @return string|null - null if the PHP data type cannot be converted.
  206. */
  207. public static function convertToLdapValue($value)
  208. {
  209. return self::_valueToLdap($value);
  210. }
  211. /**
  212. * Converts an LDAP value into its PHP data type
  213. *
  214. * @deprected use Zend_Ldap_Converter instead
  215. * @param string $value
  216. * @return mixed
  217. */
  218. public static function convertFromLdapValue($value)
  219. {
  220. return self::_valueFromLdap($value);
  221. }
  222. /**
  223. * Converts a timestamp into its LDAP date/time representation
  224. *
  225. * @param integer $value
  226. * @param boolean $utc
  227. * @return string|null - null if the value cannot be converted.
  228. */
  229. public static function convertToLdapDateTimeValue($value, $utc = false)
  230. {
  231. return self::_valueToLdapDateTime($value, $utc);
  232. }
  233. /**
  234. * Converts LDAP date/time representation into a timestamp
  235. *
  236. * @param string $value
  237. * @return integer|null - null if the value cannot be converted.
  238. */
  239. public static function convertFromLdapDateTimeValue($value)
  240. {
  241. return self::_valueFromLdapDateTime($value);
  242. }
  243. /**
  244. * Sets a LDAP password.
  245. *
  246. * @param array $data
  247. * @param string $password
  248. * @param string $hashType
  249. * @param string|null $attribName
  250. * @return null
  251. */
  252. public static function setPassword(array &$data, $password, $hashType = self::PASSWORD_HASH_MD5,
  253. $attribName = null)
  254. {
  255. if ($attribName === null) {
  256. if ($hashType === self::PASSWORD_UNICODEPWD) {
  257. $attribName = 'unicodePwd';
  258. } else {
  259. $attribName = 'userPassword';
  260. }
  261. }
  262. $hash = self::createPassword($password, $hashType);
  263. self::setAttribute($data, $attribName, $hash, false);
  264. }
  265. /**
  266. * Creates a LDAP password.
  267. *
  268. * @param string $password
  269. * @param string $hashType
  270. * @return string
  271. */
  272. public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
  273. {
  274. switch ($hashType) {
  275. case self::PASSWORD_UNICODEPWD:
  276. /* see:
  277. * http://msdn.microsoft.com/en-us/library/cc223248(PROT.10).aspx
  278. */
  279. $password = '"' . $password . '"';
  280. if (function_exists('mb_convert_encoding')) {
  281. $password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
  282. } else if (function_exists('iconv')) {
  283. $password = iconv('UTF-8', 'UTF-16LE', $password);
  284. } else {
  285. $len = strlen($password);
  286. $new = '';
  287. for($i=0; $i < $len; $i++) {
  288. $new .= $password[$i] . "\x00";
  289. }
  290. $password = $new;
  291. }
  292. return $password;
  293. case self::PASSWORD_HASH_SSHA:
  294. $salt = Zend_Crypt_Math::randBytes(4);
  295. $rawHash = sha1($password . $salt, true) . $salt;
  296. $method = '{SSHA}';
  297. break;
  298. case self::PASSWORD_HASH_SHA:
  299. $rawHash = sha1($password, true);
  300. $method = '{SHA}';
  301. break;
  302. case self::PASSWORD_HASH_SMD5:
  303. $salt = Zend_Crypt_Math::randBytes(4);
  304. $rawHash = md5($password . $salt, true) . $salt;
  305. $method = '{SMD5}';
  306. break;
  307. case self::PASSWORD_HASH_MD5:
  308. default:
  309. $rawHash = md5($password, true);
  310. $method = '{MD5}';
  311. break;
  312. }
  313. return $method . base64_encode($rawHash);
  314. }
  315. /**
  316. * Sets a LDAP date/time attribute.
  317. *
  318. * @param array $data
  319. * @param string $attribName
  320. * @param integer|array|Traversable $value
  321. * @param boolean $utc
  322. * @param boolean $append
  323. * @return null
  324. */
  325. public static function setDateTimeAttribute(array &$data, $attribName, $value, $utc = false,
  326. $append = false)
  327. {
  328. $convertedValues = array();
  329. if (is_array($value) || ($value instanceof Traversable))
  330. {
  331. foreach ($value as $v) {
  332. $v = self::_valueToLdapDateTime($v, $utc);
  333. if ($v !== null) $convertedValues[] = $v;
  334. }
  335. }
  336. else if ($value !== null) {
  337. $value = self::_valueToLdapDateTime($value, $utc);
  338. if ($value !== null) $convertedValues[] = $value;
  339. }
  340. self::setAttribute($data, $attribName, $convertedValues, $append);
  341. }
  342. /**
  343. * @param integer $value
  344. * @param boolean $utc
  345. * @return string|null
  346. */
  347. private static function _valueToLdapDateTime($value, $utc)
  348. {
  349. if (is_int($value)) {
  350. return Zend_Ldap_Converter::toLdapDateTime($value, $utc);
  351. }
  352. else return null;
  353. }
  354. /**
  355. * Gets a LDAP date/time attribute.
  356. *
  357. * @param array $data
  358. * @param string $attribName
  359. * @param integer $index
  360. * @return array|integer
  361. */
  362. public static function getDateTimeAttribute(array $data, $attribName, $index = null)
  363. {
  364. $values = self::getAttribute($data, $attribName, $index);
  365. if (is_array($values)) {
  366. for ($i = 0; $i<count($values); $i++) {
  367. $newVal = self::_valueFromLdapDateTime($values[$i]);
  368. if ($newVal !== null) $values[$i] = $newVal;
  369. }
  370. }
  371. else {
  372. $newVal = self::_valueFromLdapDateTime($values);
  373. if ($newVal !== null) $values = $newVal;
  374. }
  375. return $values;
  376. }
  377. /**
  378. * @param string|DateTime $value
  379. * @return integer|null
  380. */
  381. private static function _valueFromLdapDateTime($value)
  382. {
  383. if ($value instanceof DateTime) {
  384. return $value->format('U');
  385. } else if (is_string($value)) {
  386. try {
  387. return Zend_Ldap_Converter::fromLdapDateTime($value, false)->format('U');
  388. } catch (InvalidArgumentException $e) {
  389. return null;
  390. }
  391. } else return null;
  392. }
  393. }