function ldapEscape

@param string $subject The subject string
@param bool $dn Treat subject as a DN if TRUE
@param string|array $ignore Set of characters to leave untouched
@return string The escaped string
@author Chris Wright
@version 2.0
194788
public function ldapEscape($subject, $dn = false, $ignore = null) {
		// The base array of characters to escape
		// Flip to keys for easy use of unset()
		$search = array_flip($dn ? ['\\', ',', '=', '+', '<', '>', ';', '"', '#'] : ['\\', '*', '(', ')', "\x00"]);

		// Process characters to ignore
		if (is_array($ignore)) {
			$ignore = array_values($ignore);
		}
		for ($char = 0; isset($ignore[$char]); $char++) {
			unset($search[$ignore[$char]]);
		}

		// Flip $search back to values and build $replace array
		$search = array_keys($search);
		$replace = [];
		foreach ($search as $char) {
			$replace[] = sprintf('\\%02x', ord($char));
		}

		// Do the main replacement
		$result = str_replace($search, $replace, $subject);

		// Encode leading/trailing spaces in DN values
		if ($dn) {
			if ($result[0] == ' ') {
				$result = '\\20' . substr($result, 1);
			}
			if ($result[strlen($result) - 1] == ' ') {
				$result = substr($result, 0, -1) . '\\20';
			}
		}

		return $result;
	}
1290393
function getSubjectDN($format = self::DN_ARRAY)
    {
        switch (true) {
            case !empty($this->dn):
                return $this->getDN($format);
            case !isset($this->currentCert) || !is_array($this->currentCert):
                break;
            case isset($this->currentCert['tbsCertificate']):
                return $this->getDN($format, $this->currentCert['tbsCertificate']['subject']);
            case isset($this->currentCert['certificationRequestInfo']):
                return $this->getDN($format, $this->currentCert['certificationRequestInfo']['subject']);
        }

        return false;
    }
911895
protected function escapeManualWithFlags($value, $ignore = '', $flags = 0)
    {
        // Convert ignore string into an array
        $ignores = str_split($ignore);

        $escapeFilter = ['\\', '*', '(', ')'];
        $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_merge($escapeFilter, $escapeDn);
                break;
            default:
                return false;
        }

        foreach ($escapes as $escape) {
            // Make sure the escaped value isn't being ignored
            if (!in_array($escape, $ignores)) {
                $hexed = chunk_split(bin2hex($escape), 2, '\\');

                $hexed = '\\'.substr($hexed, 0, -1);

                $value = str_replace($escape, $hexed, $value);
            }
        }

        return $value;
    }
655163
function getDN($format = self::DN_ARRAY, $dn = null)
    {
        if (!isset($dn)) {
            $dn = isset($this->currentCert['tbsCertList']) ? $this->currentCert['tbsCertList']['issuer'] : $this->dn;
        }

        switch ((int) $format) {
            case self::DN_ARRAY:
                return $dn;
            case self::DN_ASN1:
                $asn1 = new ASN1();
                $asn1->loadOIDs($this->oids);
                $filters = array();
                $filters['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING);
                $asn1->loadFilters($filters);
                $this->_mapOutDNs($dn, 'rdnSequence', $asn1);
                return $asn1->encodeDER($dn, $this->Name);
            case self::DN_CANON:
                //  No SEQUENCE around RDNs and all string values normalized as
                // trimmed lowercase UTF-8 with all spacing as one blank.
                // constructed RDNs will not be canonicalized
                $asn1 = new ASN1();
                $asn1->loadOIDs($this->oids);
                $filters = array();
                $filters['value'] = array('type' => ASN1::TYPE_UTF8_STRING);
                $asn1->loadFilters($filters);
                $result = '';
                $this->_mapOutDNs($dn, 'rdnSequence', $asn1);
                foreach ($dn['rdnSequence'] as $rdn) {
                    foreach ($rdn as $i => $attr) {
                        $attr = &$rdn[$i];
                        if (is_array($attr['value'])) {
                            foreach ($attr['value'] as $type => $v) {
                                $type = array_search($type, $asn1->ANYmap, true);
                                if ($type !== false && isset($asn1->stringTypeSize[$type])) {
                                    $v = $asn1->convert($v, $type);
                                    if ($v !== false) {
                                        $v = preg_replace('/\s+/', ' ', $v);
                                        $attr['value'] = strtolower(trim($v));
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    $result .= $asn1->encodeDER($rdn, $this->RelativeDistinguishedName);
                }
                return $result;
            case self::DN_HASH:
                $dn = $this->getDN(self::DN_CANON, $dn);
                $hash = new Hash('sha1');
                $hash = $hash->hash($dn);
                extract(unpack('Vhash', $hash));
                return strtolower(bin2hex(pack('N', $hash)));
        }

        // Default is to return a string.
        $start = true;
        $output = '';

        $result = array();
        $asn1 = new ASN1();
        $asn1->loadOIDs($this->oids);
        $filters = array();
        $filters['rdnSequence']['value'] = array('type' => ASN1::TYPE_UTF8_STRING);
        $asn1->loadFilters($filters);
        $this->_mapOutDNs($dn, 'rdnSequence', $asn1);

        foreach ($dn['rdnSequence'] as $field) {
            $prop = $field[0]['type'];
            $value = $field[0]['value'];

            $delim = ', ';
            switch ($prop) {
                case 'id-at-countryName':
                    $desc = 'C';
                    break;
                case 'id-at-stateOrProvinceName':
                    $desc = 'ST';
                    break;
                case 'id-at-organizationName':
                    $desc = 'O';
                    break;
                case 'id-at-organizationalUnitName':
                    $desc = 'OU';
                    break;
                case 'id-at-commonName':
                    $desc = 'CN';
                    break;
                case 'id-at-localityName':
                    $desc = 'L';
                    break;
                case 'id-at-surname':
                    $desc = 'SN';
                    break;
                case 'id-at-uniqueIdentifier':
                    $delim = '/';
                    $desc = 'x500UniqueIdentifier';
                    break;
                case 'id-at-postalAddress':
                    $delim = '/';
                    $desc = 'postalAddress';
                    break;
                default:
                    $delim = '/';
                    $desc = preg_replace('#.+-([^-]+)$#', '$1', $prop);
            }

            if (!$start) {
                $output.= $delim;
            }
            if (is_array($value)) {
                foreach ($value as $type => $v) {
                    $type = array_search($type, $asn1->ANYmap, true);
                    if ($type !== false && isset($asn1->stringTypeSize[$type])) {
                        $v = $asn1->convert($v, $type);
                        if ($v !== false) {
                            $value = $v;
                            break;
                        }
                    }
                }
                if (is_array($value)) {
                    $value = array_pop($value); // Always strip data type.
                }
            } elseif (is_object($value) && $value instanceof Element) {
                $callback = function ($x) {
                    return "\x" . bin2hex($x[0]);
                };
                $value = strtoupper(preg_replace_callback('#[^\x20-\x7E]#', $callback, $value->element));
            }
            $output.= $desc . '=' . $value;
            $result[$desc] = isset($result[$desc]) ?
                array_merge((array) $dn[$prop], array($value)) :
                $value;
            $start = false;
        }

        return $format == self::DN_OPENSSL ? $result : $output;
    }
1108945
public function email($subject) : bool
    {
        if ($subject === null) {
            return false;
        }
        // FILTER_VALIDATE_EMAIL doesn't reliably require .tld.
        $v = '' . $subject;
        return $v === '' ? false : (
            !!filter_var($v, FILTER_VALIDATE_EMAIL)
            && !!preg_match('/\.[a-zA-Z\d]+$/', $v)
        );
    }
1160150
public static function replaceAssoc(array $replace, string $subject){
        return str_replace(array_keys($replace), array_values($replace), $subject);
    }
860803
public function subject($subject = null)
    {
        $headers = $this->headers();
        if (!func_num_args()) {
            return $headers['Subject'];
        }
        $headers['Subject'] = Mime::encodeValue($subject, 998, "\r\n ");
        return $this;
    }
615167
function authorize($subject='',$ondeny=NULL) {
        $f3=\Base::instance();
        if (!$this->granted($route=$f3->VERB.' '.$f3->PATH,$subject) &&
            (!isset($ondeny) || FALSE===$f3->call($ondeny,array($route,$subject)))) {
            $f3->error($subject?403:401);
            return FALSE;
        }
        return TRUE;
    }
222267
public static function replaceString($from, $to, $subject, $limit = 100000)
    {
        $from = '/' . preg_quote($from, '/') . '/';
        return preg_replace($from, $to, $subject, $limit);
    }
505716
public static function explodeDn($dn, $removeAttributePrefixes = true)
    {
        $dn = ldap_explode_dn($dn, ($removeAttributePrefixes ? 1 : 0));

        if (is_array($dn) && array_key_exists('count', $dn)) {
            foreach ($dn as $rdn => $value) {
                $dn[$rdn] = self::unescape($value);
            }
        }

        return $dn;
    }
14679
public function userUsernameFromDn($dn) {

    $ldap_entry = @$this->dnExists($dn, 'ldap_entry', array());
    if (!$ldap_entry || !is_array($ldap_entry)) {
      return FALSE;
    }
    else {
      return $this->userUsernameFromLdapEntry($ldap_entry);
    }

  }
254404
function ldap_dn_encode($str) {
        $decoded=array("\\",",","+","<",">",";","\"","=");
        
        $encoded=array();
        foreach ($decoded as $id => $char) {
            $encoded[$id]="\\".$char;
        }
        
        $str = str_replace($decoded,$encoded,$str);
        
        // Leading and trailing space
        if (" " == (substr($str,0,1))) {
            $str = "\\ ".$str;
        }
        if (" " == (substr($str,-1))) {
            $str = substr($str,0,-1)."\\ ";
        }
        // Leading # (RFC says leading one only)
        if ("#" == (substr($str,0,1))) {
            $str = "\\#".$str;
        }
        return ($str);
    }
189798
def escape_LDAP(ldap_string):
    # type: (str) -> str
    # pylint: disable=C0103
    
    if not ldap_string:
        # No content
        return ldap_string

    # Protect escape character previously in the string
    assert is_string(ldap_string)
    ldap_string = ldap_string.replace(
        ESCAPE_CHARACTER, ESCAPE_CHARACTER + ESCAPE_CHARACTER
    )

    # Leading space
    if ldap_string.startswith(" "):
        ldap_string = "\\ {0}".format(ldap_string[1:])

    # Trailing space
    if ldap_string.endswith(" "):
        ldap_string = "{0}\\ ".format(ldap_string[:-1])

    # Escape other characters
    for escaped in ESCAPED_CHARACTERS:
        ldap_string = ldap_string.replace(escaped, ESCAPE_CHARACTER + escaped)

    return ldap_string
1053214
public function dnStrToArr($dnStr, $excludeBaseDn = true, $includeAttributes = false)
    {
        if ($excludeBaseDn) {
            return ldap_explode_dn($dnStr, ($includeAttributes ? 0 : 1));
        } else {
            return ldap_explode_dn($this->adldap->getBaseDn().$dnStr, ($includeAttributes ? 0 : 1));
        }
    }
568740
public function asciiPrintable($subject) : bool
    {
        if ($subject === null) {
            return false;
        }
        $v = '' . $subject;
        if ($v === '') {
            return true;
        }
        // filter_var() is not so picky about it's return value :-(
        if (
            !($filtered = filter_var($v, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH))
            || !is_string($filtered)
        ) {
            return false;
        }
        return !strcmp($v, $filtered)
            // Prefix space to avoid expensive type check (boolean false).
            && !strpos(' ' . $v, chr(127));
    }
1294923
public static function urlEncode($str = null, $keep_mask = true)
    {
        if (
            (!empty($str) && is_numeric($str)) ||
            (true===$keep_mask && $str==='%s')
        ) {
            return $str;
        }
        if (empty($str) || !is_string($str)) {
            return '';
        }
        return urlencode($str);
    }
192483
public function subject($subject) {

		if(false === is_string($subject)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type string, "%s" given', __METHOD__, gettype($subject)), E_USER_ERROR);
		}

		$this -> subject = $subject;

		return $this;
	}