Util.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * File containing the Net_LDAP2_Util interface class.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Net
  9. * @package Net_LDAP2
  10. * @author Benedikt Hallinger <beni@php.net>
  11. * @copyright 2009 Benedikt Hallinger
  12. * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
  13. * @version SVN: $Id: Util.php 332278 2013-12-05 11:01:15Z beni $
  14. * @link http://pear.php.net/package/Net_LDAP2/
  15. */
  16. /**
  17. * Includes
  18. */
  19. require_once 'PEAR.php';
  20. /**
  21. * Utility Class for Net_LDAP2
  22. *
  23. * This class servers some functionality to the other classes of Net_LDAP2 but most of
  24. * the methods can be used separately as well.
  25. *
  26. * @category Net
  27. * @package Net_LDAP2
  28. * @author Benedikt Hallinger <beni@php.net>
  29. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  30. * @link http://pear.php.net/package/Net_LDAP22/
  31. */
  32. class Net_LDAP2_Util extends PEAR
  33. {
  34. /**
  35. * Constructor
  36. *
  37. * @access public
  38. */
  39. public function __construct()
  40. {
  41. // We do nothing here, since all methods can be called statically.
  42. // In Net_LDAP <= 0.7, we needed a instance of Util, because
  43. // it was possible to do utf8 encoding and decoding, but this
  44. // has been moved to the LDAP class. The constructor remains only
  45. // here to document the downward compatibility of creating an instance.
  46. }
  47. /**
  48. * Explodes the given DN into its elements
  49. *
  50. * {@link http://www.ietf.org/rfc/rfc2253.txt RFC 2253} says, a Distinguished Name is a sequence
  51. * of Relative Distinguished Names (RDNs), which themselves
  52. * are sets of Attributes. For each RDN a array is constructed where the RDN part is stored.
  53. *
  54. * For example, the DN 'OU=Sales+CN=J. Smith,DC=example,DC=net' is exploded to:
  55. * <kbd>array( [0] => array([0] => 'OU=Sales', [1] => 'CN=J. Smith'), [2] => 'DC=example', [3] => 'DC=net' )</kbd>
  56. *
  57. * [NOT IMPLEMENTED] DNs might also contain values, which are the bytes of the BER encoding of
  58. * the X.500 AttributeValue rather than some LDAP string syntax. These values are hex-encoded
  59. * and prefixed with a #. To distinguish such BER values, ldap_explode_dn uses references to
  60. * the actual values, e.g. '1.3.6.1.4.1.1466.0=#04024869,DC=example,DC=com' is exploded to:
  61. * [ { '1.3.6.1.4.1.1466.0' => "\004\002Hi" }, { 'DC' => 'example' }, { 'DC' => 'com' } ];
  62. * See {@link http://www.vijaymukhi.com/vmis/berldap.htm} for more information on BER.
  63. *
  64. * It also performs the following operations on the given DN:
  65. * - Unescape "\" followed by ",", "+", """, "\", "<", ">", ";", "#", "=", " ", or a hexpair
  66. * and strings beginning with "#".
  67. * - Removes the leading 'OID.' characters if the type is an OID instead of a name.
  68. * - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order.
  69. *
  70. * OPTIONS is a list of name/value pairs, valid options are:
  71. * casefold Controls case folding of attribute types names.
  72. * Attribute values are not affected by this option.
  73. * The default is to uppercase. Valid values are:
  74. * lower Lowercase attribute types names.
  75. * upper Uppercase attribute type names. This is the default.
  76. * none Do not change attribute type names.
  77. * reverse If TRUE, the RDN sequence is reversed.
  78. * onlyvalues If TRUE, then only attributes values are returned ('foo' instead of 'cn=foo')
  79. *
  80. * @param string $dn The DN that should be exploded
  81. * @param array $options Options to use
  82. *
  83. * @static
  84. * @return array Parts of the exploded DN
  85. * @todo implement BER
  86. */
  87. public static function ldap_explode_dn($dn, $options = array('casefold' => 'upper'))
  88. {
  89. if (!isset($options['onlyvalues'])) $options['onlyvalues'] = false;
  90. if (!isset($options['reverse'])) $options['reverse'] = false;
  91. if (!isset($options['casefold'])) $options['casefold'] = 'upper';
  92. // Escaping of DN and stripping of "OID."
  93. $dn = self::canonical_dn($dn, array('casefold' => $options['casefold']));
  94. // splitting the DN
  95. $dn_array = preg_split('/(?<=[^\\\\]),/', $dn);
  96. // clear wrong splitting (possibly we have split too much)
  97. // /!\ Not clear, if this is neccessary here
  98. //$dn_array = self::correct_dn_splitting($dn_array, ',');
  99. // construct subarrays for multivalued RDNs and unescape DN value
  100. // also convert to output format and apply casefolding
  101. foreach ($dn_array as $key => $value) {
  102. $value_u = self::unescape_dn_value($value);
  103. $rdns = self::split_rdn_multival($value_u[0]);
  104. if (count($rdns) > 1) {
  105. // MV RDN!
  106. foreach ($rdns as $subrdn_k => $subrdn_v) {
  107. // Casefolding
  108. if ($options['casefold'] == 'upper') $subrdn_v = preg_replace("/^(\w+=)/e", "''.strtoupper('\\1').''", $subrdn_v);
  109. if ($options['casefold'] == 'lower') $subrdn_v = preg_replace("/^(\w+=)/e", "''.strtolower('\\1').''", $subrdn_v);
  110. if ($options['onlyvalues']) {
  111. preg_match('/(.+?)(?<!\\\\)=(.+)/', $subrdn_v, $matches);
  112. $rdn_ocl = $matches[1];
  113. $rdn_val = $matches[2];
  114. $unescaped = self::unescape_dn_value($rdn_val);
  115. $rdns[$subrdn_k] = $unescaped[0];
  116. } else {
  117. $unescaped = self::unescape_dn_value($subrdn_v);
  118. $rdns[$subrdn_k] = $unescaped[0];
  119. }
  120. }
  121. $dn_array[$key] = $rdns;
  122. } else {
  123. // normal RDN
  124. // Casefolding
  125. if ($options['casefold'] == 'upper') $value = preg_replace("/^(\w+=)/e", "''.strtoupper('\\1').''", $value);
  126. if ($options['casefold'] == 'lower') $value = preg_replace("/^(\w+=)/e", "''.strtolower('\\1').''", $value);
  127. if ($options['onlyvalues']) {
  128. preg_match('/(.+?)(?<!\\\\)=(.+)/', $value, $matches);
  129. $dn_ocl = $matches[1];
  130. $dn_val = $matches[2];
  131. $unescaped = self::unescape_dn_value($dn_val);
  132. $dn_array[$key] = $unescaped[0];
  133. } else {
  134. $unescaped = self::unescape_dn_value($value);
  135. $dn_array[$key] = $unescaped[0];
  136. }
  137. }
  138. }
  139. if ($options['reverse']) {
  140. return array_reverse($dn_array);
  141. } else {
  142. return $dn_array;
  143. }
  144. }
  145. /**
  146. * Escapes a DN value according to RFC 2253
  147. *
  148. * Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs.
  149. * The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252
  150. * are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair.
  151. * Finally all leading and trailing spaces are converted to sequences of \20.
  152. *
  153. * @param array $values An array containing the DN values that should be escaped
  154. *
  155. * @static
  156. * @return array The array $values, but escaped
  157. */
  158. public static function escape_dn_value($values = array())
  159. {
  160. // Parameter validation
  161. if (!is_array($values)) {
  162. $values = array($values);
  163. }
  164. foreach ($values as $key => $val) {
  165. // Escaping of filter meta characters
  166. $val = str_replace('\\', '\\\\', $val);
  167. $val = str_replace(',', '\,', $val);
  168. $val = str_replace('+', '\+', $val);
  169. $val = str_replace('"', '\"', $val);
  170. $val = str_replace('<', '\<', $val);
  171. $val = str_replace('>', '\>', $val);
  172. $val = str_replace(';', '\;', $val);
  173. $val = str_replace('#', '\#', $val);
  174. $val = str_replace('=', '\=', $val);
  175. // ASCII < 32 escaping
  176. $val = self::asc2hex32($val);
  177. // Convert all leading and trailing spaces to sequences of \20.
  178. if (preg_match('/^(\s*)(.+?)(\s*)$/', $val, $matches)) {
  179. $val = $matches[2];
  180. for ($i = 0; $i < strlen($matches[1]); $i++) {
  181. $val = '\20'.$val;
  182. }
  183. for ($i = 0; $i < strlen($matches[3]); $i++) {
  184. $val = $val.'\20';
  185. }
  186. }
  187. if (null === $val) $val = '\0'; // apply escaped "null" if string is empty
  188. $values[$key] = $val;
  189. }
  190. return $values;
  191. }
  192. /**
  193. * Undoes the conversion done by escape_dn_value().
  194. *
  195. * Any escape sequence starting with a baskslash - hexpair or special character -
  196. * will be transformed back to the corresponding character.
  197. *
  198. * @param array $values Array of DN Values
  199. *
  200. * @return array Same as $values, but unescaped
  201. * @static
  202. */
  203. public static function unescape_dn_value($values = array())
  204. {
  205. // Parameter validation
  206. if (!is_array($values)) {
  207. $values = array($values);
  208. }
  209. foreach ($values as $key => $val) {
  210. // strip slashes from special chars
  211. $val = str_replace('\\\\', '\\', $val);
  212. $val = str_replace('\,', ',', $val);
  213. $val = str_replace('\+', '+', $val);
  214. $val = str_replace('\"', '"', $val);
  215. $val = str_replace('\<', '<', $val);
  216. $val = str_replace('\>', '>', $val);
  217. $val = str_replace('\;', ';', $val);
  218. $val = str_replace('\#', '#', $val);
  219. $val = str_replace('\=', '=', $val);
  220. // Translate hex code into ascii
  221. $values[$key] = self::hex2asc($val);
  222. }
  223. return $values;
  224. }
  225. /**
  226. * Returns the given DN in a canonical form
  227. *
  228. * Returns false if DN is not a valid Distinguished Name.
  229. * DN can either be a string or an array
  230. * as returned by ldap_explode_dn, which is useful when constructing a DN.
  231. * The DN array may have be indexed (each array value is a OCL=VALUE pair)
  232. * or associative (array key is OCL and value is VALUE).
  233. *
  234. * It performs the following operations on the given DN:
  235. * - Removes the leading 'OID.' characters if the type is an OID instead of a name.
  236. * - Escapes all RFC 2253 special characters (",", "+", """, "\", "<", ">", ";", "#", "="), slashes ("/"), and any other character where the ASCII code is < 32 as \hexpair.
  237. * - Converts all leading and trailing spaces in values to be \20.
  238. * - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order.
  239. *
  240. * OPTIONS is a list of name/value pairs, valid options are:
  241. * casefold Controls case folding of attribute type names.
  242. * Attribute values are not affected by this option. The default is to uppercase.
  243. * Valid values are:
  244. * lower Lowercase attribute type names.
  245. * upper Uppercase attribute type names. This is the default.
  246. * none Do not change attribute type names.
  247. * [NOT IMPLEMENTED] mbcescape If TRUE, characters that are encoded as a multi-octet UTF-8 sequence will be escaped as \(hexpair){2,*}.
  248. * reverse If TRUE, the RDN sequence is reversed.
  249. * separator Separator to use between RDNs. Defaults to comma (',').
  250. *
  251. * Note: The empty string "" is a valid DN, so be sure not to do a "$can_dn == false" test,
  252. * because an empty string evaluates to false. Use the "===" operator instead.
  253. *
  254. * @param array|string $dn The DN
  255. * @param array $options Options to use
  256. *
  257. * @static
  258. * @return false|string The canonical DN or FALSE
  259. * @todo implement option mbcescape
  260. */
  261. public static function canonical_dn($dn, $options = array('casefold' => 'upper', 'separator' => ','))
  262. {
  263. if ($dn === '') return $dn; // empty DN is valid!
  264. // options check
  265. if (!isset($options['reverse'])) {
  266. $options['reverse'] = false;
  267. } else {
  268. $options['reverse'] = true;
  269. }
  270. if (!isset($options['casefold'])) $options['casefold'] = 'upper';
  271. if (!isset($options['separator'])) $options['separator'] = ',';
  272. if (!is_array($dn)) {
  273. // It is not clear to me if the perl implementation splits by the user defined
  274. // separator or if it just uses this separator to construct the new DN
  275. $dn = preg_split('/(?<=[^\\\\])'.$options['separator'].'/', $dn);
  276. // clear wrong splitting (possibly we have split too much)
  277. $dn = self::correct_dn_splitting($dn, $options['separator']);
  278. } else {
  279. // Is array, check, if the array is indexed or associative
  280. $assoc = false;
  281. foreach ($dn as $dn_key => $dn_part) {
  282. if (!is_int($dn_key)) {
  283. $assoc = true;
  284. }
  285. }
  286. // convert to indexed, if associative array detected
  287. if ($assoc) {
  288. $newdn = array();
  289. foreach ($dn as $dn_key => $dn_part) {
  290. if (is_array($dn_part)) {
  291. ksort($dn_part, SORT_STRING); // we assume here, that the rdn parts are also associative
  292. $newdn[] = $dn_part; // copy array as-is, so we can resolve it later
  293. } else {
  294. $newdn[] = $dn_key.'='.$dn_part;
  295. }
  296. }
  297. $dn =& $newdn;
  298. }
  299. }
  300. // Escaping and casefolding
  301. foreach ($dn as $pos => $dnval) {
  302. if (is_array($dnval)) {
  303. // subarray detected, this means very surely, that we had
  304. // a multivalued dn part, which must be resolved
  305. $dnval_new = '';
  306. foreach ($dnval as $subkey => $subval) {
  307. // build RDN part
  308. if (!is_int($subkey)) {
  309. $subval = $subkey.'='.$subval;
  310. }
  311. $subval_processed = self::canonical_dn($subval);
  312. if (false === $subval_processed) return false;
  313. $dnval_new .= $subval_processed.'+';
  314. }
  315. $dn[$pos] = substr($dnval_new, 0, -1); // store RDN part, strip last plus
  316. } else {
  317. // try to split multivalued RDNS into array
  318. $rdns = self::split_rdn_multival($dnval);
  319. if (count($rdns) > 1) {
  320. // Multivalued RDN was detected!
  321. // The RDN value is expected to be correctly split by split_rdn_multival().
  322. // It's time to sort the RDN and build the DN!
  323. $rdn_string = '';
  324. sort($rdns, SORT_STRING); // Sort RDN keys alphabetically
  325. foreach ($rdns as $rdn) {
  326. $subval_processed = self::canonical_dn($rdn);
  327. if (false === $subval_processed) return false;
  328. $rdn_string .= $subval_processed.'+';
  329. }
  330. $dn[$pos] = substr($rdn_string, 0, -1); // store RDN part, strip last plus
  331. } else {
  332. // no multivalued RDN!
  333. // split at first unescaped "="
  334. $dn_comp = preg_split('/(?<=[^\\\\])=/', $rdns[0], 2);
  335. $ocl = ltrim($dn_comp[0]); // trim left whitespaces 'cause of "cn=foo, l=bar" syntax (whitespace after comma)
  336. $val = $dn_comp[1];
  337. // strip 'OID.', otherwise apply casefolding and escaping
  338. if (substr(strtolower($ocl), 0, 4) == 'oid.') {
  339. $ocl = substr($ocl, 4);
  340. } else {
  341. if ($options['casefold'] == 'upper') $ocl = strtoupper($ocl);
  342. if ($options['casefold'] == 'lower') $ocl = strtolower($ocl);
  343. $ocl = self::escape_dn_value(array($ocl));
  344. $ocl = $ocl[0];
  345. }
  346. // escaping of dn-value
  347. $val = self::escape_dn_value(array($val));
  348. $val = str_replace('/', '\/', $val[0]);
  349. $dn[$pos] = $ocl.'='.$val;
  350. }
  351. }
  352. }
  353. if ($options['reverse']) $dn = array_reverse($dn);
  354. return implode($options['separator'], $dn);
  355. }
  356. /**
  357. * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
  358. *
  359. * Any control characters with an ACII code < 32 as well as the characters with special meaning in
  360. * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
  361. * backslash followed by two hex digits representing the hexadecimal value of the character.
  362. *
  363. * @param array $values Array of values to escape
  364. *
  365. * @static
  366. * @return array Array $values, but escaped
  367. */
  368. public static function escape_filter_value($values = array())
  369. {
  370. // Parameter validation
  371. if (!is_array($values)) {
  372. $values = array($values);
  373. }
  374. foreach ($values as $key => $val) {
  375. // Escaping of filter meta characters
  376. $val = str_replace('\\', '\5c', $val);
  377. $val = str_replace('*', '\2a', $val);
  378. $val = str_replace('(', '\28', $val);
  379. $val = str_replace(')', '\29', $val);
  380. // ASCII < 32 escaping
  381. $val = self::asc2hex32($val);
  382. if (null === $val) $val = '\0'; // apply escaped "null" if string is empty
  383. $values[$key] = $val;
  384. }
  385. return $values;
  386. }
  387. /**
  388. * Undoes the conversion done by {@link escape_filter_value()}.
  389. *
  390. * Converts any sequences of a backslash followed by two hex digits into the corresponding character.
  391. *
  392. * @param array $values Array of values to escape
  393. *
  394. * @static
  395. * @return array Array $values, but unescaped
  396. */
  397. public static function unescape_filter_value($values = array())
  398. {
  399. // Parameter validation
  400. if (!is_array($values)) {
  401. $values = array($values);
  402. }
  403. foreach ($values as $key => $value) {
  404. // Translate hex code into ascii
  405. $values[$key] = self::hex2asc($value);
  406. }
  407. return $values;
  408. }
  409. /**
  410. * Converts all ASCII chars < 32 to "\HEX"
  411. *
  412. * @param string $string String to convert
  413. *
  414. * @static
  415. * @return string
  416. */
  417. public static function asc2hex32($string)
  418. {
  419. for ($i = 0; $i < strlen($string); $i++) {
  420. $char = substr($string, $i, 1);
  421. if (ord($char) < 32) {
  422. $hex = dechex(ord($char));
  423. if (strlen($hex) == 1) $hex = '0'.$hex;
  424. $string = str_replace($char, '\\'.$hex, $string);
  425. }
  426. }
  427. return $string;
  428. }
  429. /**
  430. * Converts all Hex expressions ("\HEX") to their original ASCII characters
  431. *
  432. * @param string $string String to convert
  433. *
  434. * @static
  435. * @author beni@php.net, heavily based on work from DavidSmith@byu.net
  436. * @return string
  437. */
  438. public static function hex2asc($string)
  439. {
  440. $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
  441. return $string;
  442. }
  443. /**
  444. * Split an multivalued RDN value into an Array
  445. *
  446. * A RDN can contain multiple values, spearated by a plus sign.
  447. * This function returns each separate ocl=value pair of the RDN part.
  448. *
  449. * If no multivalued RDN is detected, an array containing only
  450. * the original rdn part is returned.
  451. *
  452. * For example, the multivalued RDN 'OU=Sales+CN=J. Smith' is exploded to:
  453. * <kbd>array([0] => 'OU=Sales', [1] => 'CN=J. Smith')</kbd>
  454. *
  455. * The method trys to be smart if it encounters unescaped "+" characters, but may fail,
  456. * so ensure escaped "+"es in attr names and attr values.
  457. *
  458. * [BUG] If you have a multivalued RDN with unescaped plus characters
  459. * and there is a unescaped plus sign at the end of an value followed by an
  460. * attribute name containing an unescaped plus, then you will get wrong splitting:
  461. * $rdn = 'OU=Sales+C+N=J. Smith';
  462. * returns:
  463. * array('OU=Sales+C', 'N=J. Smith');
  464. * The "C+" is treaten as value of the first pair instead as attr name of the second pair.
  465. * To prevent this, escape correctly.
  466. *
  467. * @param string $rdn Part of an (multivalued) escaped RDN (eg. ou=foo OR ou=foo+cn=bar)
  468. *
  469. * @static
  470. * @return array Array with the components of the multivalued RDN or Error
  471. */
  472. public static function split_rdn_multival($rdn)
  473. {
  474. $rdns = preg_split('/(?<!\\\\)\+/', $rdn);
  475. $rdns = self::correct_dn_splitting($rdns, '+');
  476. return array_values($rdns);
  477. }
  478. /**
  479. * Splits an attribute=value syntax into an array
  480. *
  481. * If escaped delimeters are used, they are returned escaped as well.
  482. * The split will occur at the first unescaped delimeter character.
  483. * In case an invalid delimeter is given, no split will be performed and an
  484. * one element array gets returned.
  485. * Optional also filter-assertion delimeters can be considered (>, <, >=, <=, ~=).
  486. *
  487. * @param string $attr Attribute and Value Syntax ("foo=bar")
  488. * @param boolean $extended If set to true, also filter-assertion delimeter will be matched
  489. * @param boolean $withDelim If set to true, the return array contains the delimeter at index 1, putting the value to index 2
  490. *
  491. * @return array Indexed array: 0=attribute name, 1=attribute value OR ($withDelim=true): 0=attr, 1=delimeter, 2=value
  492. */
  493. public static function split_attribute_string($attr, $extended=false, $withDelim=false)
  494. {
  495. if ($withDelim) $withDelim = PREG_SPLIT_DELIM_CAPTURE;
  496. if (!$extended) {
  497. return preg_split('/(?<!\\\\)(=)/', $attr, 2, $withDelim);
  498. } else {
  499. return preg_split('/(?<!\\\\)(>=|<=|>|<|~=|=)/', $attr, 2, $withDelim);
  500. }
  501. }
  502. /**
  503. * Corrects splitting of dn parts
  504. *
  505. * @param array $dn Raw DN array
  506. * @param array $separator Separator that was used when splitting
  507. *
  508. * @return array Corrected array
  509. * @access protected
  510. */
  511. protected static function correct_dn_splitting($dn = array(), $separator = ',')
  512. {
  513. foreach ($dn as $key => $dn_value) {
  514. $dn_value = $dn[$key]; // refresh value (foreach caches!)
  515. // if the dn_value is not in attr=value format, then we had an
  516. // unescaped separator character inside the attr name or the value.
  517. // We assume, that it was the attribute value.
  518. // [TODO] To solve this, we might ask the schema. Keep in mind, that UTIL class
  519. // must remain independent from the other classes or connections.
  520. if (!preg_match('/.+(?<!\\\\)=.+/', $dn_value)) {
  521. unset($dn[$key]);
  522. if (array_key_exists($key-1, $dn)) {
  523. $dn[$key-1] = $dn[$key-1].$separator.$dn_value; // append to previous attr value
  524. } else {
  525. $dn[$key+1] = $dn_value.$separator.$dn[$key+1]; // first element: prepend to next attr name
  526. }
  527. }
  528. }
  529. return array_values($dn);
  530. }
  531. }
  532. ?>