UPDATED: Re: [PHP-DEV] Patch to fix BUG: ldap_search: sizelimit exceeded, no result..

From: Date: Wed, 19 Jul 2000 15:46:23 +0000
Subject: UPDATED: Re: [PHP-DEV] Patch to fix BUG: ldap_search: sizelimit exceeded, no result..
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-25133@lists.php.net to get a copy of this message
On Mon, 17 Jul 2000 jtaskine@hit.fi wrote: >Included patch fixes this problem: > >When doing ldap-searches which extend beyond the (server) sizelimit, there >were no partial results (for example 50 first) returned at all. > >And some of the compile warnings are nuked with this too. I hope I got >this right.. > >Question: >========= > >How do you add an optional parameter for php-function? >And in this case, how do you add TWO optional parameters?? I finally figured it, how to add parameters to php-functions..quite easy even when I'm not a c-coder myself. But then again..if there are enough monkeys with typewriters..some of them could write a novel..in a couple of hundreds years..maybe..=) The attached patch replaces the earlier one I sent here. AND it adds sizelimit-parameter to ldap_search(),ldap_list() and ldap_read() functions. If some better C-coder could check it out because it's probably an ugly hack..? The parameter is now optional WITH the array of attributes..but it should be _optional_ itself. I don't have any idea how to make it so. But this patch works like charm for me. --Jani p.s. BTW. This fixes my 'bug' report, id #5298 if someone could close it ? Index: ldap.c =================================================================== RCS file: /repository/php4/ext/ldap/ldap.c,v retrieving revision 1.46 diff -u -r1.46 ldap.c --- ldap.c 2000/06/27 09:28:05 1.46 +++ ldap.c 2000/07/19 15:19:11 @@ -450,11 +450,12 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope) { - pval **link, **base_dn, **filter, **attrs, **attr; + pval **link, **base_dn, **filter, **attrs, **attr, **sizelimit; char *ldap_base_dn, *ldap_filter; LDAP *ldap; char **ldap_attrs = NULL; - int attrsonly; + int attrsonly, errno; + int *ldap_sizelimit = 0; LDAPMessage *ldap_result; int num_attribs=0, i; LDAPLS_FETCH(); @@ -493,15 +494,12 @@ if ((ldap_attrs = emalloc((num_attribs+1) * sizeof(char *))) == NULL) { php_error(E_WARNING, "LDAP: Could not allocate memory"); RETURN_FALSE; - return; } for(i=0; i<num_attribs; i++) { - if(zend_hash_index_find((*attrs)->value.ht, i, (void **) &attr) == FAILURE) -{ + if(zend_hash_index_find((*attrs)->value.ht, i, (void **) &attr) == FAILURE) { php_error(E_WARNING, "LDAP: Array initialization wrong"); RETURN_FALSE; - return; } SEPARATE_ZVAL(attr); convert_to_string_ex(attr); @@ -511,6 +509,44 @@ break; + case 5 : + if (zend_get_parameters_ex(5, &link, &base_dn,&filter, &attrs, &sizelimit) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if ((*attrs)->type != IS_ARRAY) { + php_error(E_WARNING, "LDAP: Expected Array as 4th element"); + RETURN_FALSE; + } + + convert_to_string_ex(base_dn); + convert_to_string_ex(filter); + + ldap_base_dn = (*base_dn)->value.str.val; + ldap_filter = (*filter)->value.str.val; + + num_attribs = zend_hash_num_elements((*attrs)->value.ht); + if ((ldap_attrs = emalloc((num_attribs+1) * sizeof(char *))) == NULL) { + php_error(E_WARNING, "LDAP: Could not allocate memory"); + RETURN_FALSE; + } + + for(i=0; i<num_attribs; i++) { + if(zend_hash_index_find((*attrs)->value.ht, i, (void **) &attr) == FAILURE) { + php_error(E_WARNING, "LDAP: Array initialization wrong"); + RETURN_FALSE; + } + SEPARATE_ZVAL(attr); + convert_to_string_ex(attr); + ldap_attrs[i] = (*attr)->value.str.val; + } + ldap_attrs[num_attribs] = NULL; + + convert_to_long_ex(sizelimit); + ldap_sizelimit = (*sizelimit)->value.lval; + + break; + default: WRONG_PARAM_COUNT; break; @@ -526,19 +562,26 @@ /* Is it useful to only get the attributes ? */ attrsonly = 0; - - /* We can possibly add the timeout value also */ - if (ldap_search_s(ldap, ldap_base_dn, scope, ldap_filter, ldap_attrs, attrsonly, &ldap_result) != LDAP_SUCCESS) { + /* Sizelimit */ + if(ldap_sizelimit > 0) { + ldap->ld_sizelimit = ldap_sizelimit; + } + + errno = ldap_search_s(ldap, ldap_base_dn, scope, ldap_filter, ldap_attrs, attrsonly, &ldap_result); + if (errno != LDAP_SUCCESS && errno != LDAP_SIZELIMIT_EXCEEDED) { #if !HAVE_NSLDAP #if LDAP_API_VERSION > 2000 - php_error(E_WARNING,"LDAP: Unable to perform the search: %s",ldap_err2string(ldap_get_lderrno(ldap,NULL,NULL))); + php_error(E_WARNING,"LDAP: Unable to perform the search: %s",ldap_err2string(ldap_get_lderrno(ldap,NULL,NULL))); #else php_error(E_WARNING, "LDAP: Unable to perform the search: %s", ldap_err2string(ldap->ld_errno)); #endif #endif - RETVAL_FALSE; - } else { + RETVAL_FALSE; + } else { + if (errno == LDAP_SIZELIMIT_EXCEEDED) { + php_error(E_WARNING,"LDAP: Partial search results returned: Sizelimit exceeded."); + } RETVAL_LONG(zend_list_insert(ldap_result, le_result)); } @@ -549,15 +592,17 @@ return; } -/* {{{ proto int ldap_read(int link, string base_dn, string filter [, array attributes]) + +/* {{{ proto int ldap_read(int link, string base_dn, string filter [, array attributes, int sizelimit]) Read an entry */ PHP_FUNCTION(ldap_read) { php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_BASE); } /* }}} */ + -/* {{{ proto int ldap_list(int link, string base_dn, string filter [, array attributes]) +/* {{{ proto int ldap_list(int link, string base_dn, string filter [, array attributes, int sizelimit]) Single-level search */ PHP_FUNCTION(ldap_list) { @@ -566,7 +611,7 @@ /* }}} */ -/* {{{ proto int ldap_search(int link, string base_dn, string filter [, array attributes]) +/* {{{ proto int ldap_search(int link, string base_dn, string filter [, array attributes, int sizelimit]) Search LDAP tree under base_dn */ PHP_FUNCTION(ldap_search) { @@ -1292,7 +1337,6 @@ #else RETURN_LONG( ldap_get_lderrno(ldap, NULL, NULL) ); #endif - RETURN_LONG(0); } /* }}} */

« previous php.dev (#25133) next »