You are here

public static function LdapTransformationTraits::php56PolyfillLdapEscape in Lightweight Directory Access Protocol (LDAP) 8.4

Stub implementation of the {@link ldap_escape()} function of ext-ldap.

Escape strings for safe use in LDAP filters and DNs. Copied from polyfill due to issues from testing infrastructure.

@author Chris Wright <ldapi@daverandom.com>

Parameters

string $subject: Subject.

string $ignore: Ignore.

int $flags: Flags.

Return value

string Escaped string.

See also

http://stackoverflow.com/a/8561604

2 calls to LdapTransformationTraits::php56PolyfillLdapEscape()
LdapTransformationTraits::ldapEscapeDn in ldap_servers/src/LdapTransformationTraits.php
Wrapper for ldap_escape().
LdapTransformationTraits::ldapEscapeFilter in ldap_servers/src/LdapTransformationTraits.php
Wrapper for ldap_escape().

File

ldap_servers/src/LdapTransformationTraits.php, line 84

Class

LdapTransformationTraits
Helper functions to work around hard dependencies on the LDAP extension.

Namespace

Drupal\ldap_servers

Code

public static function php56PolyfillLdapEscape(string $subject, $ignore = '', $flags = 0) : string {
  $ldap_escape_filter = 1;
  $ldap_escape_dn = 2;
  static $charMaps = NULL;
  if (NULL === $charMaps) {
    $charMaps = [
      $ldap_escape_filter => [
        '\\',
        '*',
        '(',
        ')',
        "\0",
      ],
      $ldap_escape_dn => [
        '\\',
        ',',
        '=',
        '+',
        '<',
        '>',
        ';',
        '"',
        '#',
        "\r",
      ],
    ];
    $charMaps[0] = [];
    for ($i = 0; $i < 256; ++$i) {
      $charMaps[0][\chr($i)] = sprintf('\\%02x', $i);
    }
    for ($i = 0, $l = \count($charMaps[$ldap_escape_filter]); $i < $l; ++$i) {
      $chr = $charMaps[$ldap_escape_filter][$i];
      unset($charMaps[$ldap_escape_filter][$i]);
      $charMaps[$ldap_escape_filter][$chr] = $charMaps[0][$chr];
    }
    for ($i = 0, $l = \count($charMaps[$ldap_escape_dn]); $i < $l; ++$i) {
      $chr = $charMaps[$ldap_escape_dn][$i];
      unset($charMaps[$ldap_escape_dn][$i]);
      $charMaps[$ldap_escape_dn][$chr] = $charMaps[0][$chr];
    }
  }

  // Create the base char map to escape.
  $flags = (int) $flags;
  $charMap = [];
  if ($flags & $ldap_escape_filter) {
    $charMap += $charMaps[$ldap_escape_filter];
  }
  if ($flags & $ldap_escape_dn) {
    $charMap += $charMaps[$ldap_escape_dn];
  }
  if (!$charMap) {
    $charMap = $charMaps[0];
  }

  // Remove any chars to ignore from the list.
  $ignore = (string) $ignore;
  for ($i = 0, $l = \strlen($ignore); $i < $l; ++$i) {
    unset($charMap[$ignore[$i]]);
  }

  // Do the main replacement.
  $result = strtr($subject, $charMap);

  // Encode leading/trailing spaces if self::LDAP_ESCAPE_DN is passed.
  if ($flags & $ldap_escape_dn) {
    if ($result[0] === ' ') {
      $result = '\\20' . substr($result, 1);
    }
    if ($result[\strlen($result) - 1] === ' ') {
      $result = substr($result, 0, -1) . '\\20';
    }
  }
  return $result;
}