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