ldap_get_values_len wanted, patch enclosed

From: Date: Thu, 01 Jan 1970 00:00:00 +0000
Subject: ldap_get_values_len wanted, patch enclosed
Groups: php.dev 
Request: Send a blank email to php-dev+get-9176@lists.php.net to get a copy of this message
Hi I need to retrieve binary data from an LDAP server. The functions that are implemented all use ldap_get_values() from the C API, which only works for string data. I've made a patch that adds the PHP function ldap_get_values_len() which is similar to the PHP function ldap_get_values() except that it uses ldap_get_values_len() from the C API. If this patch is accepted, the doc should be updated too I guess, I'll try to write down something if you're interested. It would be nice if the higher level functions worked with both string and binary data, but I see no easy way of doing it at the moment. Here's the patch vs stock 3.0.11: -------8<-------------- diff -ur php-3.0.11/functions/ldap.c php-3.0.11.value_len/functions/ldap.c --- php-3.0.11/functions/ldap.c Mon Mar 8 17:56:47 1999 +++ php-3.0.11.value_len/functions/ldap.c Fri Jul 23 22:51:46 1999 @@ -128,6 +128,9 @@ {"ldap_mod_replace", php3_ldap_mod_replace, NULL}, {"ldap_mod_del", php3_ldap_mod_del, NULL}, /* end gjt mod */ +/* additional function that handles binary data, Stig Venaas */ + {"ldap_get_values_len", php3_ldap_get_values_len, NULL}, +/* end sv mod */ {NULL, NULL, NULL} }; @@ -1052,6 +1055,63 @@ add_assoc_long(return_value, "count", num_values); ldap_value_free(ldap_value); +} +/* }}} */ + +/* {{{ proto array ldap_get_values_len(int link, int result, string attribute) + Get all values from a result entry */ +void php3_ldap_get_values_len(INTERNAL_FUNCTION_PARAMETERS) +{ +pval *link, *result_entry, *attr; +LDAP *ldap; +LDAPMessage *ldap_result_entry; +char *attribute; +struct berval **ldap_value_len; +pval tmp; + +int i, num_values; + + if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &link, &result_entry, &attr) == FAILURE) { + WRONG_PARAM_COUNT; + } + + ldap = _get_ldap_link(link, list); + if (ldap == NULL) RETURN_FALSE; + + ldap_result_entry = _get_ldap_result_entry(result_entry, list); + if (ldap_result_entry == NULL) RETURN_FALSE; + + convert_to_string(attr); + attribute = attr->value.str.val; + + if ((ldap_value_len = ldap_get_values_len(ldap, ldap_result_entry, attribute)) == NULL) { +#if !HAVE_NSLDAP +#if LDAP_API_VERSION > 2000 + php3_error(E_WARNING, "LDAP: Cannot get the value(s) of attribute %s", ldap_err2string(ldap_get_lderrno(ldap,NULL,NULL))); +#else + php3_error(E_WARNING, "LDAP: Cannot get the value(s) of attribute %s", ldap_err2string(ldap->ld_errno)); +#endif +#endif + RETURN_FALSE; + } + + num_values = ldap_count_values_len(ldap_value_len); + + if (array_init(return_value) == FAILURE) { + RETURN_FALSE; + } + + for(i=0; i<num_values; i++) { + tmp.type = IS_STRING; + tmp.value.str.len = ldap_value_len[i]->bv_len; + tmp.value.str.val = estrndup(ldap_value_len[i]->bv_val, tmp.value.str.len); + /* add_next_index_string(return_value, ldap_value[i], 1); */ + _php3_hash_next_index_insert(return_value->value.ht, &tmp, sizeof(pval),NULL); + } + + add_assoc_long(return_value, "count", num_values); + + ldap_value_free_len(ldap_value_len); } /* }}} */ diff -ur php-3.0.11/functions/php3_ldap.h php-3.0.11.value_len/functions/php3_ldap.h --- php-3.0.11/functions/php3_ldap.h Mon Mar 8 17:56:48 1999 +++ php-3.0.11.value_len/functions/php3_ldap.h Fri Jul 23 22:49:53 1999 @@ -75,6 +75,7 @@ extern void php3_ldap_get_attributes(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ldap_get_values(INTERNAL_FUNCTION_PARAMETERS); +extern void php3_ldap_get_values_len(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ber_free(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ldap_get_dn(INTERNAL_FUNCTION_PARAMETERS); -------8<-------------- Stig -- Stig Venaas UNINETT

« previous php.dev (#9176) next »