Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Ldap / Ldif / Encoder.php
blobea08fb35d77e05d563419bd884f6659d7652dc12
1 <?php
2 /**
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
8 */
10 namespace Zend\Ldap\Ldif;
12 use Zend\Ldap;
14 /**
15 * Zend\Ldap\Ldif\Encoder provides methods to encode and decode LDAP data into/from Ldif.
17 class Encoder
19 /**
20 * Additional options used during encoding
22 * @var array
24 protected $options = array(
25 'sort' => true,
26 'version' => 1,
27 'wrap' => 78
30 /**
31 * @var bool
33 protected $versionWritten = false;
35 /**
36 * Constructor.
38 * @param array $options Additional options used during encoding
40 protected function __construct(array $options = array())
42 $this->options = array_merge($this->options, $options);
45 /**
46 * Decodes the string $string into an array of Ldif items
48 * @param string $string
49 * @return array
51 public static function decode($string)
53 $encoder = new static(array());
54 return $encoder->_decode($string);
57 /**
58 * Decodes the string $string into an array of Ldif items
60 * @param string $string
61 * @return array
63 protected function _decode($string)
65 $items = array();
66 $item = array();
67 $last = null;
68 $inComment = false;
69 foreach (explode("\n", $string) as $line) {
70 $line = rtrim($line, "\x09\x0A\x0D\x00\x0B");
71 $matches = array();
72 if (substr($line, 0, 1) === ' ' && $last !== null && !$inComment) {
73 $last[2] .= substr($line, 1);
74 } elseif (substr($line, 0, 1) === '#') {
75 $inComment = true;
76 continue;
77 } elseif (preg_match('/^([a-z0-9;-]+)(:[:<]?\s*)([^<]*)$/i', $line, $matches)) {
78 $inComment = false;
79 $name = strtolower($matches[1]);
80 $type = trim($matches[2]);
81 $value = $matches[3];
82 if ($last !== null) {
83 $this->pushAttribute($last, $item);
85 if ($name === 'version') {
86 continue;
87 } elseif (count($item) > 0 && $name === 'dn') {
88 $items[] = $item;
89 $item = array();
90 $last = null;
92 $last = array($name, $type, $value);
93 } elseif (trim($line) === '') {
94 continue;
97 if ($last !== null) {
98 $this->pushAttribute($last, $item);
100 $items[] = $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;
123 } else {
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);
159 return null;
163 * Encodes $string according to RFC2849
165 * @link http://www.faqs.org/rfcs/rfc2849.html
167 * @param string $string
168 * @param bool $base64
169 * @return string
171 protected function encodeString($string, &$base64 = null)
173 $string = (string) $string;
174 if (!is_numeric($string) && empty($string)) {
175 return '';
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,
190 * ; and CR
192 $unsafeChar = array(0, 10, 13);
194 $base64 = false;
195 for ($i = 0, $len = strlen($string); $i < $len; $i++) {
196 $char = ord(substr($string, $i, 1));
197 if ($char >= 127) {
198 $base64 = true;
199 break;
200 } elseif ($i === 0 && in_array($char, $unsafeInitChar)) {
201 $base64 = true;
202 break;
203 } elseif (in_array($char, $unsafeChar)) {
204 $base64 = true;
205 break;
208 // Test for ending space
209 if (substr($string, -1) == ' ') {
210 $base64 = true;
213 if ($base64 === true) {
214 $string = base64_encode($string);
217 return $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
227 * @return string
229 protected function encodeAttribute($name, $value)
231 if (!is_array($value)) {
232 $value = array($value);
235 $output = '';
237 if (count($value) < 1) {
238 return $name . ': ';
241 foreach ($value as $v) {
242 $base64 = null;
243 $v = $this->encodeString($v, $base64);
244 $attribute = $name . ':';
245 if ($base64 === true) {
246 $attribute .= ': ' . $v;
247 } else {
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
265 * @return string
267 protected function encodeAttributes(array $attributes)
269 $string = '';
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);