Adldap\Utilities::escapeManualWithFlags PHP Method

escapeManualWithFlags() protected static method

Escapes the inserted value with flags. Supplying either 1 or 2 into the flags parameter will escape only certain values.
protected static escapeManualWithFlags ( string $value, string $ignore = '', integer $flags ) : string
$value string
$ignore string
$flags integer
return string
    protected static function escapeManualWithFlags($value, $ignore = '', $flags = 0)
    {
        // Convert ignore string into an array
        $ignores = static::ignoreStrToArray($ignore);
        // The escape characters for search filters
        $escapeFilter = ['\\', '*', '(', ')'];
        // The escape characters for distinguished names
        $escapeDn = ['\\', ',', '=', '+', '<', '>', ';', '"', '#'];
        switch ($flags) {
            case 1:
                // Int 1 equals to LDAP_ESCAPE_FILTER
                $escapes = $escapeFilter;
                break;
            case 2:
                // Int 2 equals to LDAP_ESCAPE_DN
                $escapes = $escapeDn;
                break;
            case 3:
                // If both LDAP_ESCAPE_FILTER and LDAP_ESCAPE_DN are used
                $escapes = array_unique(array_merge($escapeDn, $escapeFilter));
                break;
            default:
                // We've been given an invalid flag, we'll escape everything to be safe.
                return static::escapeManual($value, $ignore);
        }
        foreach ($escapes as $escape) {
            // Make sure the escaped value isn't being ignored.
            if (!in_array($escape, $ignores)) {
                $hexed = static::escape($escape);
                $value = str_replace($escape, $hexed, $value);
            }
        }
        return $value;
    }