You are here

function ldap_pear_escape_dn_value in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_servers/ldap_servers.functions.inc \ldap_pear_escape_dn_value()
  2. 7 ldap_servers/ldap_servers.functions.inc \ldap_pear_escape_dn_value()

Escapes a DN value according to RFC 2253.

Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs. The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252 are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair. Finally all leading and trailing spaces are converted to sequences of \20.

@static

Parameters

array $values: An array containing the DN values that should be escaped.

Return value

array The array $values, but escaped

1 call to ldap_pear_escape_dn_value()
ldap_server_massage_text in ldap_servers/ldap_servers.functions.inc
Function to massage (change case, escape, unescape) ldap attribute names and values. The primary purpose of this is to articulate and ensure consistency across ldap modules.

File

ldap_servers/ldap_servers.functions.inc, line 323
Collection of functions that don't belong in server object.

Code

function ldap_pear_escape_dn_value($values = []) {

  // Parameter validation.
  $is_scalar = is_scalar($values);
  if (!is_array($values)) {
    $values = [
      $values,
    ];
  }
  foreach ($values as $key => $val) {

    // Escaping of filter meta characters.
    $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);

    // ASCII < 32 escaping.
    $val = ldap_pear_asc2hex32($val);

    // Convert all leading and trailing spaces to sequences of \20.
    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';
      }
    }

    // Apply escaped "null" if string is empty.
    if (NULL === $val) {
      $val = '\\0';
    }
    $values[$key] = $val;
  }
  return $is_scalar ? $values[0] : $values;
}