/** * Parse FILTER into a Net_LDAP_Filter object * * This parses an filter string into Net_LDAP_Filter objects. * * @param string $FILTER The filter string * * @access static * @return Net_LDAP_Filter|Net_LDAP_Error * @todo Leaf-mode: Do we need to escape at all? what about *-chars?check for the need of encoding values, tackle problems (see code comments) */ function parse($FILTER) { if (preg_match('/^\\((.+?)\\)$/', $FILTER, $matches)) { if (in_array(substr($matches[1], 0, 1), array('!', '|', '&'))) { // Subfilter processing: pass subfilters to parse() and combine // the objects using the logical operator detected // we have now something like "(...)(...)(...)" but at least one part ("(...)"). // extract logical operator and subfilters $log_op = substr($matches[1], 0, 1); $remaining_component = substr($matches[1], 1); // bite off the next filter part and parse $subfilters = array(); while (preg_match('/^(\\(.+?\\))(.*)/', $remaining_component, $matches)) { $remaining_component = $matches[2]; $filter_o = Net_LDAP_Filter::parse($matches[1]); if (PEAR::isError($filter_o)) { return $filter_o; } array_push($subfilters, $filter_o); } // combine subfilters using the logical operator $filter_o = Net_LDAP_Filter::combine($log_op, $subfilters); return $filter_o; } else { // This is one leaf filter component, do some syntax checks, then escape and build filter_o // $matches[1] should be now something like "foo=bar" // detect multiple leaf components // [TODO] Maybe this will make problems with filters containing brackets inside the value if (stristr($matches[1], ')(')) { return PEAR::raiseError("Filter parsing error: invalid filter syntax - multiple leaf components detected!"); } else { $filter_parts = preg_split('/(?<!\\\\)(=|=~|>|<|>=|<=)/', $matches[1], 2, PREG_SPLIT_DELIM_CAPTURE); if (count($filter_parts) != 3) { return PEAR::raiseError("Filter parsing error: invalid filter syntax - unknown matching rule used"); } else { $filter_o = new Net_LDAP_Filter(); // [TODO]: Do we need to escape at all? what about *-chars user provide and that should remain special? // I think, those prevent escaping! We need to check against PERL Net::LDAP! // $value_arr = Net_LDAP_Util::escape_filter_value(array($filter_parts[2])); // $value = $value_arr[0]; $value = $filter_parts[2]; $filter_o->_filter = '(' . $filter_parts[0] . $filter_parts[1] . $value . ')'; return $filter_o; } } } } else { // ERROR: Filter components must be enclosed in round brackets return PEAR::raiseError("Filter parsing error: invalid filter syntax - filter components must be enclosed in round brackets"); } }