/** * Returns last error message if error was found. * * Example: * <code> * $ldif->someAction(); * if ($ldif->error()) { * echo "Error: ".$ldif->error()." at input line: ".$ldif->error_lines(); * } * </code> * * @param boolean $as_string If set to true, only the message is returned * * @return false|Net_LDAP_Error */ function error($as_string = false) { if (Net_LDAP::isError($this->_error['error'])) { return $as_string ? $this->_error['error']->getMessage() : $this->_error['error']; } else { return false; } }
/** * Encodes or decodes attribute values if needed * * @access private * @param array Array of attributes * @param array Function to apply to attribute values * @return array Array of attributes with function applied to values */ function _utf8($attributes, $function) { if (!$this->_ldap || !$this->_schema || !function_exists($function)) { return $attributes; } if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $k => $v) { $attr = $this->_schema->get('attribute', $k); if (Net_LDAP::isError($attr)) { continue; } if (false !== strpos($attr['syntax'], '1.3.6.1.4.1.1466.115.121.1.15')) { if (is_array($v)) { foreach ($v as $ak => $av) { $v[$ak] = call_user_func($function, $av); } } else { $v = call_user_func($function, $v); } } $attributes[$k] = $v; } } return $attributes; }
/** * Returns wether a attribute syntax is binary or not * * This method gets used by Net_LDAP_Entry to decide which * PHP function needs to be used to fetch the value in the * proper format (e.g. binary or string) * * @param string $attribute the name of the attribute (eg.: 'sn') * @return boolean */ function isBinary($attribute) { // This list contains all syntax that should be treaten as // containing binary values // The Syntax Definitons go into constants at the top of this page $syntax_binary = array(NET_LDAP_SYNTAX_OCTET_STRING, NET_LDAP_SYNTAX_JPEG); // Check Syntax $attr_s = $this->get('attribute', $attribute); if (false === Net_LDAP::isError($attr_s) && isset($attr_s['syntax']) && in_array($attr_s['syntax'], $syntax_binary)) { $return = true; return $return; } else { $return = false; return $return; } }
/** * Returns wether a attribute syntax is binary or not * * This method gets used by Net_LDAP_Entry to decide which * PHP function needs to be used to fetch the value in the * proper format (e.g. binary or string) * * @param string $attribute The name of the attribute (eg.: 'sn') * * @access public * @return boolean */ function isBinary($attribute) { $return = false; // default to false // This list contains all syntax that should be treaten as // containing binary values // The Syntax Definitons go into constants at the top of this page $syntax_binary = array(NET_LDAP_SYNTAX_OCTET_STRING, NET_LDAP_SYNTAX_JPEG); // Check Syntax $attr_s = $this->get('attribute', $attribute); if (Net_LDAP::isError($attr_s)) { // Attribute not found in schema $return = false; // consider attr not binary } elseif (isset($attr_s['syntax']) && in_array($attr_s['syntax'], $syntax_binary)) { // Syntax is defined as binary in schema $return = true; } else { // Syntax not defined as binary, or not found // if attribute is a subtype, check superior attribute syntaxes if (isset($attr_s['sup'])) { foreach ($attr_s['sup'] as $superattr) { $return = $this->isBinary($superattr); if ($return) { break; // stop checking parents since we are binary } } } } return $return; }
/** * Applies a regular expression onto a single- or multivalued attribute (like preg_match()) * * This method behaves like PHPs preg_match() but with some exceptions. * If you want to retrieve match information, then you MUST pass the * $matches parameter via reference! otherwise you will get no matches. * Since it is possible to have multi valued attributes the $matches * array will have a additionally numerical dimension (one for each value): * <code> * $matches = array( * 0 => array (usual preg_match() returnarray), * 1 => array (usual preg_match() returnarray) * ) * </code> * Please note, that $matches will be initialized to an empty array inside. * * Usage example: * <code> * $result = $entry->preg_match('/089(\d+)/', 'telephoneNumber', &$matches); * if ( $result === true ){ * echo "First match: ".$matches[0][1]; // Match of value 1, content of first bracket * } else { * if ( Net_LDAP::isError($result) ) { * echo "Error: ".$result->getMessage(); * } else { * echo "No match found."; * } * } * </code> * * Please note that it is important to test for an Net_LDAP_Error, because objects are * evaluating to true by default, thus if a error occured, and you only check using "==" then * you get misleading results. Use the "identical" (===) operator to test for matches to * avoid this as shown above. * * @param string $regex The regular expression * @param string $attr_name The attribute to search in * @param array $matches (optional, PASS BY REFERENCE!) Array to store matches in * @return boolean|Net_LDAP_Error TRUE, if we had a match in one of the values, otherwise false. Net_LDAP_Error in case something went wrong */ function preg_match($regex, $attr_name, $matches = array()) { $matches = array(); // fetch attribute values $attr = $this->getValue($attr_name, 'all'); if (Net_LDAP::isError($attr)) { return $attr; } else { unset($attr['count']); } // perform preg_match() on all values $match = false; foreach ($attr as $thisvalue) { $matches_int = array(); if (preg_match($regex, $thisvalue, $matches_int)) { $match = true; array_push($matches, $matches_int); // store matches in reference } } return $match; }
/** * Encodes or decodes attribute values if needed * * @param array $attributes Array of attributes * @param array $function Function to apply to attribute values * * @access private * @return array|Net_LDAP_Error Array of attributes with function * applied to values or Error */ function _utf8($attributes, $function) { if (!is_array($attributes) || array_key_exists(0, $attributes)) { $msg = 'Parameter $attributes is expected to be an associative array'; return PEAR::raiseError($msg); } if (!$this->_schema) { $this->_schema = $this->schema(); } if (!$this->_link || Net_LDAP::isError($this->_schema) || !function_exists($function)) { return $attributes; } if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $k => $v) { if (!isset($this->_schemaAttrs[$k])) { $attr = $this->_schema->get('attribute', $k); if (Net_LDAP::isError($attr)) { continue; } $haystack = '1.3.6.1.4.1.1466.115.121.1.15'; if (false !== strpos($attr['syntax'], $haystack)) { $encode = true; } else { $encode = false; } $this->_schemaAttrs[$k] = $encode; } else { $encode = $this->_schemaAttrs[$k]; } if ($encode) { if (is_array($v)) { foreach ($v as $ak => $av) { $v[$ak] = call_user_func($function, $av); } } else { $v = call_user_func($function, $v); } } $attributes[$k] = $v; } } return $attributes; }
/** * Returns the name(s) of the immediate superclass(es) * * @param string Name or OID of objectclass * @return mixed Array of names or Net_LDAP_Error */ function superclass($oc) { $o = $this->get('objectclass', $oc); if (Net_LDAP::isError($o)) { return $o; } return key_exists('sup', $o) ? $o['sup'] : array(); }