PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Ldap/Ldif/Encoder.php

https://github.com/Exercise/zf2
PHP | 309 lines | 171 code | 20 blank | 118 comment | 51 complexity | 9d068ad5030abfe078bf27bee97a6ac7 MD5 | raw file
  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. * @subpackage Ldif
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Ldap\Ldif;
  26. /**
  27. * Zend_Ldap_Ldif_Encoder provides methods to encode and decode LDAP data into/from Ldif.
  28. *
  29. * @category Zend
  30. * @package Zend_Ldap
  31. * @subpackage Ldif
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Encoder
  36. {
  37. /**
  38. * Additional options used during encoding
  39. *
  40. * @var array
  41. */
  42. protected $_options = array(
  43. 'sort' => true,
  44. 'version' => 1,
  45. 'wrap' => 78
  46. );
  47. /**
  48. * @var boolean
  49. */
  50. protected $_versionWritten = false;
  51. /**
  52. * Constructor.
  53. *
  54. * @param array $options Additional options used during encoding
  55. * @return void
  56. */
  57. protected function __construct(array $options = array())
  58. {
  59. $this->_options = array_merge($this->_options, $options);
  60. }
  61. /**
  62. * Decodes the string $string into an array of Ldif items
  63. *
  64. * @param string $string
  65. * @return array
  66. */
  67. public static function decode($string)
  68. {
  69. $encoder = new self(array());
  70. return $encoder->_decode($string);
  71. }
  72. /**
  73. * Decodes the string $string into an array of Ldif items
  74. *
  75. * @param string $string
  76. * @return array
  77. */
  78. protected function _decode($string)
  79. {
  80. $items = array();
  81. $item = array();
  82. $last = null;
  83. foreach (explode("\n", $string) as $line) {
  84. $line = rtrim($line, "\x09\x0A\x0D\x00\x0B");
  85. $matches = array();
  86. if (substr($line, 0, 1) === ' ' && $last !== null) {
  87. $last[2] .= substr($line, 1);
  88. } else if (substr($line, 0, 1) === '#') {
  89. continue;
  90. } else if (preg_match('/^([a-z0-9;-]+)(:[:<]?\s*)([^:<]*)$/i', $line, $matches)) {
  91. $name = strtolower($matches[1]);
  92. $type = trim($matches[2]);
  93. $value = $matches[3];
  94. if ($last !== null) {
  95. $this->_pushAttribute($last, $item);
  96. }
  97. if ($name === 'version') {
  98. continue;
  99. } else if (count($item) > 0 && $name === 'dn') {
  100. $items[] = $item;
  101. $item = array();
  102. $last = null;
  103. }
  104. $last = array($name, $type, $value);
  105. } else if (trim($line) === '') {
  106. continue;
  107. }
  108. }
  109. if ($last !== null) {
  110. $this->_pushAttribute($last, $item);
  111. }
  112. $items[] = $item;
  113. return (count($items)>1) ? $items : $items[0];
  114. }
  115. /**
  116. * Pushes a decoded attribute to the stack
  117. *
  118. * @param array $attribute
  119. * @param array $entry
  120. */
  121. protected function _pushAttribute(array $attribute, array &$entry)
  122. {
  123. $name = $attribute[0];
  124. $type = $attribute[1];
  125. $value = $attribute[2];
  126. if ($type === '::') {
  127. $value = base64_decode($value);
  128. }
  129. if ($name === 'dn') {
  130. $entry[$name] = $value;
  131. } else if (isset($entry[$name]) && $value !== '') {
  132. $entry[$name][] = $value;
  133. } else {
  134. $entry[$name] = ($value !== '') ? array($value) : array();
  135. }
  136. }
  137. /**
  138. * Encode $value into a Ldif representation
  139. *
  140. * @param mixed $value The value to be encoded
  141. * @param array $options Additional options used during encoding
  142. * @return string The encoded value
  143. */
  144. public static function encode($value, array $options = array())
  145. {
  146. $encoder = new self($options);
  147. return $encoder->_encode($value);
  148. }
  149. /**
  150. * Recursive driver which determines the type of value to be encoded
  151. * and then dispatches to the appropriate method.
  152. *
  153. * @param mixed $value The value to be encoded
  154. * @return string Encoded value
  155. */
  156. protected function _encode($value)
  157. {
  158. if (is_scalar($value)) {
  159. return $this->_encodeString($value);
  160. } else if (is_array($value)) {
  161. return $this->_encodeAttributes($value);
  162. } else if ($value instanceof \Zend\Ldap\Node) {
  163. return $value->toLdif($this->_options);
  164. }
  165. return null;
  166. }
  167. /**
  168. * Encodes $string according to RFC2849
  169. *
  170. * @link http://www.faqs.org/rfcs/rfc2849.html
  171. *
  172. * @param string $string
  173. * @param boolen $base64
  174. * @return string
  175. */
  176. protected function _encodeString($string, &$base64 = null)
  177. {
  178. $string = (string)$string;
  179. if (!is_numeric($string) && empty($string)) {
  180. return '';
  181. }
  182. /*
  183. * SAFE-INIT-CHAR = %x01-09 / %x0B-0C / %x0E-1F /
  184. * %x21-39 / %x3B / %x3D-7F
  185. * ; any value <= 127 except NUL, LF, CR,
  186. * ; SPACE, colon (":", ASCII 58 decimal)
  187. * ; and less-than ("<" , ASCII 60 decimal)
  188. *
  189. */
  190. $unsafe_init_char = array(0, 10, 13, 32, 58, 60);
  191. /*
  192. * SAFE-CHAR = %x01-09 / %x0B-0C / %x0E-7F
  193. * ; any value <= 127 decimal except NUL, LF,
  194. * ; and CR
  195. */
  196. $unsafe_char = array(0, 10, 13);
  197. $base64 = false;
  198. for ($i = 0; $i < strlen($string); $i++) {
  199. $char = ord(substr($string, $i, 1));
  200. if ($char >= 127) {
  201. $base64 = true;
  202. break;
  203. } else if ($i === 0 && in_array($char, $unsafe_init_char)) {
  204. $base64 = true;
  205. break;
  206. } else if (in_array($char, $unsafe_char)) {
  207. $base64 = true;
  208. break;
  209. }
  210. }
  211. // Test for ending space
  212. if (substr($string, -1) == ' ') {
  213. $base64 = true;
  214. }
  215. if ($base64 === true) {
  216. $string = base64_encode($string);
  217. }
  218. return $string;
  219. }
  220. /**
  221. * Encodes an attribute with $name and $value according to RFC2849
  222. *
  223. * @link http://www.faqs.org/rfcs/rfc2849.html
  224. *
  225. * @param string $name
  226. * @param array|string $value
  227. * @return string
  228. */
  229. protected function _encodeAttribute($name, $value)
  230. {
  231. if (!is_array($value)) {
  232. $value = array($value);
  233. }
  234. $output = '';
  235. if (count($value) < 1) {
  236. return $name . ': ';
  237. }
  238. foreach ($value as $v) {
  239. $base64 = null;
  240. $v = $this->_encodeString($v, $base64);
  241. $attribute = $name . ':';
  242. if ($base64 === true) {
  243. $attribute .= ': ' . $v;
  244. } else {
  245. $attribute .= ' ' . $v;
  246. }
  247. if (isset($this->_options['wrap']) && strlen($attribute) > $this->_options['wrap']) {
  248. $attribute = trim(chunk_split($attribute, $this->_options['wrap'], PHP_EOL . ' '));
  249. }
  250. $output .= $attribute . PHP_EOL;
  251. }
  252. return trim($output, PHP_EOL);
  253. }
  254. /**
  255. * Encodes a collection of attributes according to RFC2849
  256. *
  257. * @link http://www.faqs.org/rfcs/rfc2849.html
  258. *
  259. * @param array $attributes
  260. * @return string
  261. */
  262. protected function _encodeAttributes(array $attributes)
  263. {
  264. $string = '';
  265. $attributes = array_change_key_case($attributes, CASE_LOWER);
  266. if (!$this->_versionWritten && array_key_exists('dn', $attributes) && isset($this->_options['version'])
  267. && array_key_exists('objectclass', $attributes)) {
  268. $string .= sprintf('version: %d', $this->_options['version']) . PHP_EOL;
  269. $this->_versionWritten = true;
  270. }
  271. if (isset($this->_options['sort']) && $this->_options['sort'] === true) {
  272. ksort($attributes, SORT_STRING);
  273. if (array_key_exists('objectclass', $attributes)) {
  274. $oc = $attributes['objectclass'];
  275. unset($attributes['objectclass']);
  276. $attributes = array_merge(array('objectclass' => $oc), $attributes);
  277. }
  278. if (array_key_exists('dn', $attributes)) {
  279. $dn = $attributes['dn'];
  280. unset($attributes['dn']);
  281. $attributes = array_merge(array('dn' => $dn), $attributes);
  282. }
  283. }
  284. foreach ($attributes as $key => $value) {
  285. $string .= $this->_encodeAttribute($key, $value) . PHP_EOL;
  286. }
  287. return trim($string, PHP_EOL);
  288. }
  289. }