You are here

public static function ConversionHelper::escapeFilterValue in Lightweight Directory Access Protocol (LDAP) 8.3

Escapes the given values so that they can be safely used in LDAP filters.

Follow RFC 2254 so that control characters with an ACII code < 32 as well as the characters with special meaning in LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a backslash followed by two hex digits representing the hexadecimal value of the character.

@static

Parameters

array|string $values: Array of values to escape.

Return value

array Array of values, but escaped.

1 call to ConversionHelper::escapeFilterValue()
MassageAttributes::queryLdapAttributeValue in ldap_servers/src/Helper/MassageAttributes.php
Escape filter values and attribute values when querying ldap.

File

ldap_servers/src/Helper/ConversionHelper.php, line 29

Class

ConversionHelper
Conversion helper to escape values correctly for LDAP filters.

Namespace

Drupal\ldap_servers\Helper

Code

public static function escapeFilterValue($values) {

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

    // Might be a Drupal field.
    if (isset($val->value)) {
      $isField = TRUE;
      $val = $val
        ->getValue();
    }
    else {
      $isField = FALSE;
    }

    // Escaping of filter meta characters.
    $val = str_replace('\\', '\\5c', $val);
    $val = str_replace('*', '\\2a', $val);
    $val = str_replace('(', '\\28', $val);
    $val = str_replace(')', '\\29', $val);

    // ASCII < 32 escaping.
    $val = self::asc2hex32($val);
    if (NULL === $val) {

      // Apply escaped "null" if string is empty.
      $val = '\\0';
    }
    if ($isField) {
      $values[$key]
        ->setValue($val);
    }
    else {
      $values[$key] = $val;
    }
  }
  if ($input_is_scalar) {
    return $values[0];
  }
  else {
    return $values;
  }
}