Patch of PHP LDAP module (ldap.c) - binary attribute supports
| From: | Terrence Miao | Date: | Fri, 17 Sep 1999 06:32:13 +0000 |
| Subject: | Patch of PHP LDAP module (ldap.c) - binary attribute supports | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-11021@lists.php.net to get a copy of this message | ||
G'day Buddies,
I have a patch for PHP3 ldap module 3.0.12 (ldap.c) that can
let you get/add/modify *binary* attribute wihouting changing
your PHP code. Four old functions have been changed:
php3_ldap_get_values ()
php3_ldap_get_entries ()
php3_ldap_get_attributes ()
php3_ldap_modify ()
New functions can treat binary AND text attributes as same.
The patch file and mod_php3-ldap RPM package can be found at:
ftp://igloo.its.unimelb.edu.au/pub/Linux/PHP3/
I have a demo - LDAP Explorer implementing with the patch that
let you show binary jpeg/gif files retrieving from OpenLDAP
server. Try:
http://igloo.its.unimelb.edu.au/LDAPExplorer
dn: uid=10001, ou=Information Software Development, o=igloo CA, l=Melbourne, st=Victoria, c=AU
Cheers,
__
---- / / _
--- / / (_)__ __ ____ __ Terrence Miao
-- / /__/ / _ \/ // /\ \/ / ITS of University of Melbourne
- /____/_/_//_/\_,_/ /_/\_\ terrence_miao@email.com
Choice of GNU Generation http://igloo.its.unimelb.edu.au/linux-online
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
--- php-3.0.12.orig/functions/ldap.c Thu Jul 1 22:28:16 1999
+++ php-3.0.12/functions/ldap.c Thu Sep 16 14:32:53 1999
@@ -885,13 +885,13 @@
{
pval *link, *result;
LDAPMessage *ldap_result, *ldap_result_entry;
-pval tmp1, tmp2;
+pval tmp, tmp1, tmp2;
LDAP *ldap;
int num_entries, num_attrib, num_values, i;
int attr_count, entry_count;
BerElement *ber;
char *attribute;
-char **ldap_value;
+struct berval **ldap_value_len;
char *dn;
if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &link, &result) == FAILURE) {
@@ -931,15 +931,18 @@
attr_count = 0;
attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
while (attribute != NULL) {
- ldap_value = ldap_get_values(ldap, ldap_result_entry, attribute);
- num_values = ldap_count_values(ldap_value);
+ ldap_value_len = ldap_get_values_len(ldap, ldap_result_entry, attribute);
+ num_values = ldap_count_values_len(ldap_value_len);
array_init(&tmp2);
add_assoc_long(&tmp2, "count", num_values);
for(i=0; i<num_values; i++) {
- add_index_string(&tmp2, i, ldap_value[i], 1);
+ 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);
+ _php3_hash_next_index_insert(tmp2.value.ht, &tmp, sizeof(pval),NULL);
}
- ldap_value_free(ldap_value);
+ ldap_value_free_len(ldap_value_len);
_php3_hash_update(tmp1.value.ht, _php3_strtolower(attribute), strlen(attribute)+1, (void *)
&tmp2, sizeof(pval), NULL);
add_index_string(&tmp1, attr_count, attribute, 1);
@@ -1036,11 +1039,11 @@
void php3_ldap_get_attributes(INTERNAL_FUNCTION_PARAMETERS)
{
pval *link, *result_entry;
-pval tmp;
+pval tmp, tmptmp;
LDAP *ldap;
LDAPMessage *ldap_result_entry;
char *attribute;
-char **ldap_value;
+struct berval **ldap_value_len;
int i, count, num_values, num_attrib;
BerElement *ber;
@@ -1067,15 +1070,19 @@
count=0;
attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
while (attribute != NULL) {
- ldap_value = ldap_get_values(ldap, ldap_result_entry, attribute);
- num_values = ldap_count_values(ldap_value);
+ ldap_value_len = ldap_get_values_len(ldap, ldap_result_entry, attribute);
+ num_values = ldap_count_values_len(ldap_value_len);
array_init(&tmp);
add_assoc_long(&tmp, "count", num_values);
+ array_init(&tmptmp);
for(i=0; i<num_values; i++) {
- add_index_string(&tmp, i, ldap_value[i], 1);
+ tmptmp.type = IS_STRING;
+ tmptmp.value.str.len = ldap_value_len[i]->bv_len;
+ tmptmp.value.str.val = estrndup(ldap_value_len[i]->bv_val,
tmptmp.value.str.len);
+ _php3_hash_next_index_insert(tmp.value.ht, &tmptmp, sizeof(pval),NULL);
}
- ldap_value_free(ldap_value);
+ ldap_value_free_len(ldap_value_len);
_php3_hash_update(return_value->value.ht, attribute, strlen(attribute)+1, (void *) &tmp,
sizeof(pval), NULL);
add_index_string(return_value, count, attribute, 1);
@@ -1096,8 +1103,9 @@
LDAP *ldap;
LDAPMessage *ldap_result_entry;
char *attribute;
-char **ldap_value;
+struct berval **ldap_value_len;
int i, num_values;
+pval tmp;
if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &link, &result_entry, &attr) ==
FAILURE) {
WRONG_PARAM_COUNT;
@@ -1112,7 +1120,7 @@
convert_to_string(attr);
attribute = attr->value.str.val;
- if ((ldap_value = ldap_get_values(ldap, ldap_result_entry, attribute)) == NULL) {
+ 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)));
@@ -1123,19 +1131,22 @@
RETURN_FALSE;
}
- num_values = ldap_count_values(ldap_value);
+ num_values = ldap_count_values_len(ldap_value_len);
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
for(i=0; i<num_values; i++) {
- add_next_index_string(return_value, ldap_value[i], 1);
+ 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);
+ _php3_hash_next_index_insert(return_value->value.ht, &tmp,
sizeof(pval),NULL);
}
add_assoc_long(return_value, "count", num_values);
- ldap_value_free(ldap_value);
+ ldap_value_free_len(ldap_value_len);
}
/* }}} */
@@ -1244,6 +1255,7 @@
char *attribute;
ulong index;
int is_full_add=0; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit
THomson */
+int *num_berval;
if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &link, &dn, &entry) == FAILURE) {
WRONG_PARAM_COUNT;
@@ -1263,6 +1275,7 @@
num_attribs = _php3_hash_num_elements(entry->value.ht);
ldap_mods = emalloc((num_attribs+1) * sizeof(LDAPMod *));
+ num_berval = emalloc (num_attribs * sizeof (int));
_php3_hash_internal_pointer_reset(entry->value.ht);
/* added by gerrit thomson to fix ldap_add using ldap_mod_add */
@@ -1276,7 +1289,7 @@
for(i=0; i<num_attribs; i++) {
ldap_mods[i] = emalloc(sizeof(LDAPMod));
- ldap_mods[i]->mod_op = oper;
+ ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
if (_php3_hash_get_current_key(entry->value.ht, &attribute, &index) ==
HASH_KEY_IS_STRING) {
ldap_mods[i]->mod_type = estrdup(attribute);
@@ -1292,22 +1305,26 @@
} else {
num_values = _php3_hash_num_elements(value->value.ht);
}
-
- ldap_mods[i]->mod_values = emalloc((num_values+1) * sizeof(char *));
+ num_berval[i] = num_values;
+ ldap_mods[i]->mod_bvalues = emalloc ((num_values + 1) * sizeof (struct berval *));
/* allow for arrays with one element, no allowance for arrays with none but probably not required,
gerrit thomson. */
/* if (num_values == 1) {*/
if ((num_values == 1) && (value->type != IS_ARRAY)) {
convert_to_string(value);
- ldap_mods[i]->mod_values[0] = value->value.str.val;
+ ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof (struct berval));
+ ldap_mods[i]->mod_bvalues[0]->bv_len = value->value.str.len;
+ ldap_mods[i]->mod_bvalues[0]->bv_val = value->value.str.val;
} else {
for(j=0; j<num_values; j++) {
_php3_hash_index_find(value->value.ht, j, (void **) &ivalue);
convert_to_string(ivalue);
- ldap_mods[i]->mod_values[j] = ivalue->value.str.val;
+ ldap_mods[i]->mod_bvalues[j] = (struct berval *) emalloc (sizeof (struct berval));
+ ldap_mods[i]->mod_bvalues[j]->bv_len = ivalue->value.str.len;
+ ldap_mods[i]->mod_bvalues[j]->bv_val = ivalue->value.str.val;
}
}
- ldap_mods[i]->mod_values[num_values] = NULL;
+ ldap_mods[i]->mod_bvalues[num_values] = NULL;
_php3_hash_move_forward(entry->value.ht);
}
@@ -1330,9 +1347,11 @@
for(i=0; i<num_attribs; i++) {
efree(ldap_mods[i]->mod_type);
- efree(ldap_mods[i]->mod_values);
+ for (j = 0; j < num_berval[i]; j++)
+ efree(ldap_mods[i]->mod_bvalues[j]);
efree(ldap_mods[i]);
}
+ efree (num_berval);
efree(ldap_mods);
return;