SimpleSAML_Auth_LDAP::escape_filter_value PHP Méthode

escape_filter_value() public static méthode

Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters. Any 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.
public static escape_filter_value ( array $values = [], $singleValue = true ) : array
$values array Array of values to escape
Résultat array Array $values, but escaped
    public static function escape_filter_value($values = array(), $singleValue = true)
    {
        // Parameter validation
        if (!is_array($values)) {
            $values = array($values);
        }
        foreach ($values as $key => $val) {
            // 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) {
                $val = '\\0';
                // apply escaped "null" if string is empty
            }
            $values[$key] = $val;
        }
        if ($singleValue) {
            return $values[0];
        }
        return $values;
    }

Usage Example

 /**
  * Add attributes from an LDAP server.
  *
  * @param array &$request  The current request
  */
 public function process(&$request)
 {
     assert('is_array($request)');
     assert('array_key_exists("Attributes", $request)');
     $attributes =& $request['Attributes'];
     $map =& $this->attribute_map;
     if (!isset($attributes[$map['username']])) {
         throw new Exception('The user\'s identity does not have an attribute called "' . $map['username'] . '"');
     }
     // perform a merge on the ldap_search_filter
     // loop over the attributes and build the search and replace arrays
     foreach ($attributes as $attr => $val) {
         $arrSearch[] = '%' . $attr . '%';
         if (strlen($val[0]) > 0) {
             $arrReplace[] = SimpleSAML_Auth_LDAP::escape_filter_value($val[0]);
         } else {
             $arrReplace[] = '';
         }
     }
     // merge the attributes into the ldap_search_filter
     $filter = str_replace($arrSearch, $arrReplace, $this->search_filter);
     // search for matching entries
     $entries = $this->getLdap()->searchformultiple($this->base_dn, $filter, (array) $this->search_attribute, TRUE, FALSE);
     // handle [multiple] values
     if (is_array($entries) && is_array($entries[0])) {
         $results = array();
         foreach ($entries as $entry) {
             $entry = $entry[strtolower($this->search_attribute)];
             for ($i = 0; $i < $entry['count']; $i++) {
                 $results[] = $entry[$i];
             }
         }
         $attributes[$this->new_attribute] = array_values($results);
     }
 }
All Usage Examples Of SimpleSAML_Auth_LDAP::escape_filter_value