3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Ldap\Ldif
;
15 * Zend\Ldap\Ldif\Encoder provides methods to encode and decode LDAP data into/from Ldif.
20 * Additional options used during encoding
24 protected $options = array(
33 protected $versionWritten = false;
38 * @param array $options Additional options used during encoding
40 protected function __construct(array $options = array())
42 $this->options
= array_merge($this->options
, $options);
46 * Decodes the string $string into an array of Ldif items
48 * @param string $string
51 public static function decode($string)
53 $encoder = new static(array());
54 return $encoder->_decode($string);
58 * Decodes the string $string into an array of Ldif items
60 * @param string $string
63 protected function _decode($string)
69 foreach (explode("\n", $string) as $line) {
70 $line = rtrim($line, "\x09\x0A\x0D\x00\x0B");
72 if (substr($line, 0, 1) === ' ' && $last !== null && !$inComment) {
73 $last[2] .= substr($line, 1);
74 } elseif (substr($line, 0, 1) === '#') {
77 } elseif (preg_match('/^([a-z0-9;-]+)(:[:<]?\s*)([^<]*)$/i', $line, $matches)) {
79 $name = strtolower($matches[1]);
80 $type = trim($matches[2]);
83 $this->pushAttribute($last, $item);
85 if ($name === 'version') {
87 } elseif (count($item) > 0 && $name === 'dn') {
92 $last = array($name, $type, $value);
93 } elseif (trim($line) === '') {
98 $this->pushAttribute($last, $item);
102 return (count($items) > 1) ?
$items : $items[0];
106 * Pushes a decoded attribute to the stack
108 * @param array $attribute
109 * @param array $entry
111 protected function pushAttribute(array $attribute, array &$entry)
113 $name = $attribute[0];
114 $type = $attribute[1];
115 $value = $attribute[2];
116 if ($type === '::') {
117 $value = base64_decode($value);
119 if ($name === 'dn') {
120 $entry[$name] = $value;
121 } elseif (isset($entry[$name]) && $value !== '') {
122 $entry[$name][] = $value;
124 $entry[$name] = ($value !== '') ?
array($value) : array();
129 * Encode $value into a Ldif representation
131 * @param mixed $value The value to be encoded
132 * @param array $options Additional options used during encoding
133 * @return string The encoded value
135 public static function encode($value, array $options = array())
137 $encoder = new static($options);
139 return $encoder->_encode($value);
143 * Recursive driver which determines the type of value to be encoded
144 * and then dispatches to the appropriate method.
146 * @param mixed $value The value to be encoded
147 * @return string Encoded value
149 protected function _encode($value)
151 if (is_scalar($value)) {
152 return $this->encodeString($value);
153 } elseif (is_array($value)) {
154 return $this->encodeAttributes($value);
155 } elseif ($value instanceof Ldap\Node
) {
156 return $value->toLdif($this->options
);
163 * Encodes $string according to RFC2849
165 * @link http://www.faqs.org/rfcs/rfc2849.html
167 * @param string $string
168 * @param bool $base64
171 protected function encodeString($string, &$base64 = null)
173 $string = (string) $string;
174 if (!is_numeric($string) && empty($string)) {
179 * SAFE-INIT-CHAR = %x01-09 / %x0B-0C / %x0E-1F /
180 * %x21-39 / %x3B / %x3D-7F
181 * ; any value <= 127 except NUL, LF, CR,
182 * ; SPACE, colon (":", ASCII 58 decimal)
183 * ; and less-than ("<" , ASCII 60 decimal)
186 $unsafeInitChar = array(0, 10, 13, 32, 58, 60);
188 * SAFE-CHAR = %x01-09 / %x0B-0C / %x0E-7F
189 * ; any value <= 127 decimal except NUL, LF,
192 $unsafeChar = array(0, 10, 13);
195 for ($i = 0, $len = strlen($string); $i < $len; $i++
) {
196 $char = ord(substr($string, $i, 1));
200 } elseif ($i === 0 && in_array($char, $unsafeInitChar)) {
203 } elseif (in_array($char, $unsafeChar)) {
208 // Test for ending space
209 if (substr($string, -1) == ' ') {
213 if ($base64 === true) {
214 $string = base64_encode($string);
221 * Encodes an attribute with $name and $value according to RFC2849
223 * @link http://www.faqs.org/rfcs/rfc2849.html
225 * @param string $name
226 * @param array|string $value
229 protected function encodeAttribute($name, $value)
231 if (!is_array($value)) {
232 $value = array($value);
237 if (count($value) < 1) {
241 foreach ($value as $v) {
243 $v = $this->encodeString($v, $base64);
244 $attribute = $name . ':';
245 if ($base64 === true) {
246 $attribute .= ': ' . $v;
248 $attribute .= ' ' . $v;
250 if (isset($this->options
['wrap']) && strlen($attribute) > $this->options
['wrap']) {
251 $attribute = trim(chunk_split($attribute, $this->options
['wrap'], PHP_EOL
. ' '));
253 $output .= $attribute . PHP_EOL
;
256 return trim($output, PHP_EOL
);
260 * Encodes a collection of attributes according to RFC2849
262 * @link http://www.faqs.org/rfcs/rfc2849.html
264 * @param array $attributes
267 protected function encodeAttributes(array $attributes)
270 $attributes = array_change_key_case($attributes, CASE_LOWER
);
271 if (!$this->versionWritten
&& array_key_exists('dn', $attributes) && isset($this->options
['version'])
272 && array_key_exists('objectclass', $attributes)
274 $string .= sprintf('version: %d', $this->options
['version']) . PHP_EOL
;
275 $this->versionWritten
= true;
278 if (isset($this->options
['sort']) && $this->options
['sort'] === true) {
279 ksort($attributes, SORT_STRING
);
280 if (array_key_exists('objectclass', $attributes)) {
281 $oc = $attributes['objectclass'];
282 unset($attributes['objectclass']);
283 $attributes = array_merge(array('objectclass' => $oc), $attributes);
285 if (array_key_exists('dn', $attributes)) {
286 $dn = $attributes['dn'];
287 unset($attributes['dn']);
288 $attributes = array_merge(array('dn' => $dn), $attributes);
291 foreach ($attributes as $key => $value) {
292 $string .= $this->encodeAttribute($key, $value) . PHP_EOL
;
295 return trim($string, PHP_EOL
);