123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- <?php
- require_once 'PEAR.php';
- class Net_LDAP2_Util extends PEAR
- {
-
- public function __construct()
- {
-
-
-
-
-
- }
-
- public static function ldap_explode_dn($dn, $options = array('casefold' => 'upper'))
- {
- if (!isset($options['onlyvalues'])) $options['onlyvalues'] = false;
- if (!isset($options['reverse'])) $options['reverse'] = false;
- if (!isset($options['casefold'])) $options['casefold'] = 'upper';
-
- $dn = self::canonical_dn($dn, array('casefold' => $options['casefold']));
-
- $dn_array = preg_split('/(?<=[^\\\\]),/', $dn);
-
-
-
-
-
- foreach ($dn_array as $key => $value) {
- $value_u = self::unescape_dn_value($value);
- $rdns = self::split_rdn_multival($value_u[0]);
- if (count($rdns) > 1) {
-
- foreach ($rdns as $subrdn_k => $subrdn_v) {
-
- if ($options['casefold'] == 'upper') {
- $subrdn_v = preg_replace_callback(
- "/^\w+=/",
- function ($matches) {
- return strtoupper($matches[0]);
- },
- $subrdn_v
- );
- } else if ($options['casefold'] == 'lower') {
- $subrdn_v = preg_replace_callback(
- "/^\w+=/",
- function ($matches) {
- return strtolower($matches[0]);
- },
- $subrdn_v
- );
- }
- if ($options['onlyvalues']) {
- preg_match('/(.+?)(?<!\\\\)=(.+)/', $subrdn_v, $matches);
- $rdn_ocl = $matches[1];
- $rdn_val = $matches[2];
- $unescaped = self::unescape_dn_value($rdn_val);
- $rdns[$subrdn_k] = $unescaped[0];
- } else {
- $unescaped = self::unescape_dn_value($subrdn_v);
- $rdns[$subrdn_k] = $unescaped[0];
- }
- }
- $dn_array[$key] = $rdns;
- } else {
-
-
- if ($options['casefold'] == 'upper') {
- $value = preg_replace_callback(
- "/^\w+=/",
- function ($matches) {
- return strtoupper($matches[0]);
- },
- $value
- );
- } else if ($options['casefold'] == 'lower') {
- $value = preg_replace_callback(
- "/^\w+=/",
- function ($matches) {
- return strtolower($matches[0]);
- },
- $value
- );
- }
- if ($options['onlyvalues']) {
- preg_match('/(.+?)(?<!\\\\)=(.+)/', $value, $matches);
- $dn_ocl = $matches[1];
- $dn_val = $matches[2];
- $unescaped = self::unescape_dn_value($dn_val);
- $dn_array[$key] = $unescaped[0];
- } else {
- $unescaped = self::unescape_dn_value($value);
- $dn_array[$key] = $unescaped[0];
- }
- }
- }
- if ($options['reverse']) {
- return array_reverse($dn_array);
- } else {
- return $dn_array;
- }
- }
-
- public static function escape_dn_value($values = array())
- {
-
- if (!is_array($values)) {
- $values = array($values);
- }
- foreach ($values as $key => $val) {
-
- $val = str_replace('\\', '\\\\', $val);
- $val = str_replace(',', '\,', $val);
- $val = str_replace('+', '\+', $val);
- $val = str_replace('"', '\"', $val);
- $val = str_replace('<', '\<', $val);
- $val = str_replace('>', '\>', $val);
- $val = str_replace(';', '\;', $val);
- $val = str_replace('#', '\#', $val);
- $val = str_replace('=', '\=', $val);
-
- $val = self::asc2hex32($val);
-
- if (preg_match('/^(\s*)(.+?)(\s*)$/', $val, $matches)) {
- $val = $matches[2];
- for ($i = 0; $i < strlen($matches[1]); $i++) {
- $val = '\20'.$val;
- }
- for ($i = 0; $i < strlen($matches[3]); $i++) {
- $val = $val.'\20';
- }
- }
- if (null === $val) $val = '\0';
- $values[$key] = $val;
- }
- return $values;
- }
-
- public static function unescape_dn_value($values = array())
- {
-
- if (!is_array($values)) {
- $values = array($values);
- }
- foreach ($values as $key => $val) {
-
- $val = str_replace('\\\\', '\\', $val);
- $val = str_replace('\,', ',', $val);
- $val = str_replace('\+', '+', $val);
- $val = str_replace('\"', '"', $val);
- $val = str_replace('\<', '<', $val);
- $val = str_replace('\>', '>', $val);
- $val = str_replace('\;', ';', $val);
- $val = str_replace('\#', '#', $val);
- $val = str_replace('\=', '=', $val);
-
- $values[$key] = self::hex2asc($val);
- }
- return $values;
- }
-
- public static function canonical_dn($dn, $options = array('casefold' => 'upper', 'separator' => ','))
- {
- if ($dn === '') return $dn;
-
- if (!isset($options['reverse'])) {
- $options['reverse'] = false;
- } else {
- $options['reverse'] = true;
- }
- if (!isset($options['casefold'])) $options['casefold'] = 'upper';
- if (!isset($options['separator'])) $options['separator'] = ',';
- if (!is_array($dn)) {
-
-
- $dn = preg_split('/(?<=[^\\\\])'.$options['separator'].'/', $dn);
-
- $dn = self::correct_dn_splitting($dn, $options['separator']);
- } else {
-
- $assoc = false;
- foreach ($dn as $dn_key => $dn_part) {
- if (!is_int($dn_key)) {
- $assoc = true;
- }
- }
-
- if ($assoc) {
- $newdn = array();
- foreach ($dn as $dn_key => $dn_part) {
- if (is_array($dn_part)) {
- ksort($dn_part, SORT_STRING);
- $newdn[] = $dn_part;
- } else {
- $newdn[] = $dn_key.'='.$dn_part;
- }
- }
- $dn =& $newdn;
- }
- }
-
- foreach ($dn as $pos => $dnval) {
- if (is_array($dnval)) {
-
-
- $dnval_new = '';
- foreach ($dnval as $subkey => $subval) {
-
- if (!is_int($subkey)) {
- $subval = $subkey.'='.$subval;
- }
- $subval_processed = self::canonical_dn($subval);
- if (false === $subval_processed) return false;
- $dnval_new .= $subval_processed.'+';
- }
- $dn[$pos] = substr($dnval_new, 0, -1);
- } else {
-
- $rdns = self::split_rdn_multival($dnval);
- if (count($rdns) > 1) {
-
-
-
- $rdn_string = '';
- sort($rdns, SORT_STRING);
- foreach ($rdns as $rdn) {
- $subval_processed = self::canonical_dn($rdn);
- if (false === $subval_processed) return false;
- $rdn_string .= $subval_processed.'+';
- }
- $dn[$pos] = substr($rdn_string, 0, -1);
- } else {
-
-
- $dn_comp = preg_split('/(?<=[^\\\\])=/', $rdns[0], 2);
- $ocl = ltrim($dn_comp[0]);
- $val = $dn_comp[1];
-
- if (substr(strtolower($ocl), 0, 4) == 'oid.') {
- $ocl = substr($ocl, 4);
- } else {
- if ($options['casefold'] == 'upper') $ocl = strtoupper($ocl);
- if ($options['casefold'] == 'lower') $ocl = strtolower($ocl);
- $ocl = self::escape_dn_value(array($ocl));
- $ocl = $ocl[0];
- }
-
- $val = self::escape_dn_value(array($val));
- $val = str_replace('/', '\/', $val[0]);
- $dn[$pos] = $ocl.'='.$val;
- }
- }
- }
- if ($options['reverse']) $dn = array_reverse($dn);
- return implode($options['separator'], $dn);
- }
-
- public static function escape_filter_value($values = array())
- {
-
- if (!is_array($values)) {
- $values = array($values);
- }
- foreach ($values as $key => $val) {
-
- $val = str_replace('\\', '\5c', $val);
- $val = str_replace('*', '\2a', $val);
- $val = str_replace('(', '\28', $val);
- $val = str_replace(')', '\29', $val);
-
- $val = self::asc2hex32($val);
- if (null === $val) $val = '\0';
- $values[$key] = $val;
- }
- return $values;
- }
-
- public static function unescape_filter_value($values = array())
- {
-
- if (!is_array($values)) {
- $values = array($values);
- }
- foreach ($values as $key => $value) {
-
- $values[$key] = self::hex2asc($value);
- }
- return $values;
- }
-
- public static function asc2hex32($string)
- {
- for ($i = 0; $i < strlen($string); $i++) {
- $char = substr($string, $i, 1);
- if (ord($char) < 32) {
- $hex = dechex(ord($char));
- if (strlen($hex) == 1) $hex = '0'.$hex;
- $string = str_replace($char, '\\'.$hex, $string);
- }
- }
- return $string;
- }
-
- public static function hex2asc($string)
- {
- $string = preg_replace_callback(
- "/\\\[0-9A-Fa-f]{2}/",
- function ($matches) {
- return chr(hexdec($matches[0]));
- },
- $string
- );
- return $string;
- }
-
- public static function split_rdn_multival($rdn)
- {
- $rdns = preg_split('/(?<!\\\\)\+/', $rdn);
- $rdns = self::correct_dn_splitting($rdns, '+');
- return array_values($rdns);
- }
-
- public static function split_attribute_string($attr, $extended=false, $withDelim=false)
- {
- if ($withDelim) $withDelim = PREG_SPLIT_DELIM_CAPTURE;
- if (!$extended) {
- return preg_split('/(?<!\\\\)(=)/', $attr, 2, $withDelim);
- } else {
- return preg_split('/(?<!\\\\)(>=|<=|>|<|~=|=)/', $attr, 2, $withDelim);
- }
- }
-
- protected static function correct_dn_splitting($dn = array(), $separator = ',')
- {
- foreach ($dn as $key => $dn_value) {
- $dn_value = $dn[$key];
-
-
-
-
-
- if (!preg_match('/.+(?<!\\\\)=.+/', $dn_value)) {
- unset($dn[$key]);
- if (array_key_exists($key-1, $dn)) {
- $dn[$key-1] = $dn[$key-1].$separator.$dn_value;
- } else {
- $dn[$key+1] = $dn_value.$separator.$dn[$key+1];
- }
- }
- }
- return array_values($dn);
- }
- }
- ?>
|