PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/ldap - ldap.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 934 1094 85.4 %
Date: 2014-04-10 Functions: 57 59 96.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Amitay Isaacs  <amitay@w-o-i.com>                           |
      16             :    |          Eric Warnke    <ericw@albany.edu>                           |
      17             :    |          Rasmus Lerdorf <rasmus@php.net>                             |
      18             :    |          Gerrit Thomson <334647@swin.edu.au>                         |
      19             :    |          Jani Taskinen  <sniper@iki.fi>                              |
      20             :    |          Stig Venaas    <venaas@uninett.no>                          |
      21             :    |          Doug Goldstein <cardoe@cardoe.com>                          |
      22             :    | PHP 4.0 updates:  Zeev Suraski <zeev@zend.com>                       |
      23             :    +----------------------------------------------------------------------+
      24             :  */
      25             :  
      26             : /* $Id$ */
      27             : #define IS_EXT_MODULE
      28             : 
      29             : #ifdef HAVE_CONFIG_H
      30             : #include "config.h"
      31             : #endif
      32             : 
      33             : /* Additional headers for NetWare */
      34             : #if defined(NETWARE) && (NEW_LIBC)
      35             : #include <sys/select.h>
      36             : #include <sys/timeval.h>
      37             : #endif
      38             : 
      39             : #include "php.h"
      40             : #include "php_ini.h"
      41             : 
      42             : #include <stddef.h>
      43             : 
      44             : #include "ext/standard/dl.h"
      45             : #include "php_ldap.h"
      46             : 
      47             : #ifdef PHP_WIN32
      48             : #include <string.h>
      49             : #include "config.w32.h"
      50             : #if HAVE_NSLDAP
      51             : #include <winsock2.h>
      52             : #endif
      53             : #define strdup _strdup
      54             : #undef WINDOWS
      55             : #undef strcasecmp
      56             : #undef strncasecmp
      57             : #define WINSOCK 1
      58             : #define __STDC__ 1
      59             : #endif
      60             : 
      61             : #include "ext/standard/php_string.h"
      62             : #include "ext/standard/info.h"
      63             : 
      64             : #ifdef HAVE_LDAP_SASL_H
      65             : #include <sasl.h>
      66             : #elif defined(HAVE_LDAP_SASL_SASL_H)
      67             : #include <sasl/sasl.h>
      68             : #endif
      69             : 
      70             : typedef struct {
      71             :         LDAP *link;
      72             : #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
      73             :         zval *rebindproc;
      74             : #endif
      75             : } ldap_linkdata;
      76             : 
      77             : typedef struct {
      78             :         LDAPMessage *data;
      79             :         BerElement *ber;
      80             :         int id;
      81             : } ldap_resultentry;
      82             : 
      83             : ZEND_DECLARE_MODULE_GLOBALS(ldap)
      84             : static PHP_GINIT_FUNCTION(ldap);
      85             : 
      86             : static int le_link, le_result, le_result_entry;
      87             : 
      88             : #ifdef COMPILE_DL_LDAP
      89             : ZEND_GET_MODULE(ldap)
      90             : #endif
      91             : 
      92         204 : static void _close_ldap_link(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
      93             : {
      94         204 :         ldap_linkdata *ld = (ldap_linkdata *)rsrc->ptr;
      95             : 
      96         204 :         ldap_unbind_s(ld->link);
      97             : #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
      98         204 :         if (ld->rebindproc != NULL) {
      99           1 :                 zval_dtor(ld->rebindproc);
     100           1 :                 FREE_ZVAL(ld->rebindproc);
     101             :         }
     102             : #endif
     103         204 :         efree(ld);
     104         204 :         LDAPG(num_links)--;
     105         204 : }
     106             : /* }}} */
     107             : 
     108          51 : static void _free_ldap_result(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
     109             : {
     110          51 :         LDAPMessage *result = (LDAPMessage *)rsrc->ptr;
     111          51 :         ldap_msgfree(result);
     112          51 : }
     113             : /* }}} */
     114             : 
     115          14 : static void _free_ldap_result_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
     116             : {
     117          14 :         ldap_resultentry *entry = (ldap_resultentry *)rsrc->ptr;
     118             : 
     119          14 :         if (entry->ber != NULL) {
     120           1 :                 ber_free(entry->ber, 0);
     121           1 :                 entry->ber = NULL;
     122             :         }
     123          14 :         zend_list_delete(entry->id);
     124          14 :         efree(entry);
     125          14 : } 
     126             : /* }}} */
     127             : 
     128             : /* {{{ PHP_INI_BEGIN
     129             :  */
     130             : PHP_INI_BEGIN()
     131             :         STD_PHP_INI_ENTRY_EX("ldap.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_ldap_globals, ldap_globals, display_link_numbers)
     132             : PHP_INI_END()
     133             : /* }}} */
     134             : 
     135             : /* {{{ PHP_GINIT_FUNCTION
     136             :  */
     137       20105 : static PHP_GINIT_FUNCTION(ldap)
     138             : {
     139       20105 :         ldap_globals->num_links = 0;
     140       20105 : }
     141             : /* }}} */
     142             : 
     143             : /* {{{ PHP_MINIT_FUNCTION
     144             :  */
     145       20105 : PHP_MINIT_FUNCTION(ldap)
     146             : {
     147       20105 :         REGISTER_INI_ENTRIES();
     148             : 
     149             :         /* Constants to be used with deref-parameter in php_ldap_do_search() */
     150       20105 :         REGISTER_LONG_CONSTANT("LDAP_DEREF_NEVER", LDAP_DEREF_NEVER, CONST_PERSISTENT | CONST_CS);
     151       20105 :         REGISTER_LONG_CONSTANT("LDAP_DEREF_SEARCHING", LDAP_DEREF_SEARCHING, CONST_PERSISTENT | CONST_CS);
     152       20105 :         REGISTER_LONG_CONSTANT("LDAP_DEREF_FINDING", LDAP_DEREF_FINDING, CONST_PERSISTENT | CONST_CS);
     153       20105 :         REGISTER_LONG_CONSTANT("LDAP_DEREF_ALWAYS", LDAP_DEREF_ALWAYS, CONST_PERSISTENT | CONST_CS);
     154             : 
     155             :         /* Constants to be used with ldap_modify_batch() */
     156       20105 :         REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_ADD", LDAP_MODIFY_BATCH_ADD, CONST_PERSISTENT | CONST_CS);
     157       20105 :         REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE", LDAP_MODIFY_BATCH_REMOVE, CONST_PERSISTENT | CONST_CS);
     158       20105 :         REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE_ALL", LDAP_MODIFY_BATCH_REMOVE_ALL, CONST_PERSISTENT | CONST_CS);
     159       20105 :         REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REPLACE", LDAP_MODIFY_BATCH_REPLACE, CONST_PERSISTENT | CONST_CS);
     160       20105 :         REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_ATTRIB", LDAP_MODIFY_BATCH_ATTRIB, CONST_PERSISTENT | CONST_CS);
     161       20105 :         REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_MODTYPE", LDAP_MODIFY_BATCH_MODTYPE, CONST_PERSISTENT | CONST_CS);
     162       20105 :         REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_VALUES", LDAP_MODIFY_BATCH_VALUES, CONST_PERSISTENT | CONST_CS);
     163             : 
     164             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10
     165             :         /* LDAP options */
     166       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_DEREF", LDAP_OPT_DEREF, CONST_PERSISTENT | CONST_CS);
     167       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_SIZELIMIT", LDAP_OPT_SIZELIMIT, CONST_PERSISTENT | CONST_CS);
     168       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_TIMELIMIT", LDAP_OPT_TIMELIMIT, CONST_PERSISTENT | CONST_CS);
     169             : #ifdef LDAP_OPT_NETWORK_TIMEOUT
     170       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_OPT_NETWORK_TIMEOUT, CONST_PERSISTENT | CONST_CS);
     171             : #elif defined (LDAP_X_OPT_CONNECT_TIMEOUT)
     172             :         REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_X_OPT_CONNECT_TIMEOUT, CONST_PERSISTENT | CONST_CS);
     173             : #endif
     174       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_PROTOCOL_VERSION", LDAP_OPT_PROTOCOL_VERSION, CONST_PERSISTENT | CONST_CS);
     175       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_NUMBER", LDAP_OPT_ERROR_NUMBER, CONST_PERSISTENT | CONST_CS);
     176       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_REFERRALS", LDAP_OPT_REFERRALS, CONST_PERSISTENT | CONST_CS);
     177             : #ifdef LDAP_OPT_RESTART
     178       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_RESTART", LDAP_OPT_RESTART, CONST_PERSISTENT | CONST_CS);
     179             : #endif
     180             : #ifdef LDAP_OPT_HOST_NAME
     181       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_HOST_NAME", LDAP_OPT_HOST_NAME, CONST_PERSISTENT | CONST_CS);
     182             : #endif
     183       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_STRING", LDAP_OPT_ERROR_STRING, CONST_PERSISTENT | CONST_CS);
     184             : #ifdef LDAP_OPT_MATCHED_DN
     185       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_MATCHED_DN", LDAP_OPT_MATCHED_DN, CONST_PERSISTENT | CONST_CS);
     186             : #endif
     187       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_SERVER_CONTROLS", LDAP_OPT_SERVER_CONTROLS, CONST_PERSISTENT | CONST_CS);
     188       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_CLIENT_CONTROLS", LDAP_OPT_CLIENT_CONTROLS, CONST_PERSISTENT | CONST_CS);
     189             : #endif
     190             : #ifdef LDAP_OPT_DEBUG_LEVEL
     191       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_DEBUG_LEVEL", LDAP_OPT_DEBUG_LEVEL, CONST_PERSISTENT | CONST_CS);
     192             : #endif
     193             : 
     194             : #ifdef HAVE_LDAP_SASL
     195       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_MECH", LDAP_OPT_X_SASL_MECH, CONST_PERSISTENT | CONST_CS);
     196       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_REALM", LDAP_OPT_X_SASL_REALM, CONST_PERSISTENT | CONST_CS);
     197       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHCID", LDAP_OPT_X_SASL_AUTHCID, CONST_PERSISTENT | CONST_CS);
     198       20105 :         REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHZID", LDAP_OPT_X_SASL_AUTHZID, CONST_PERSISTENT | CONST_CS);
     199             : #endif
     200             : 
     201             : #ifdef ORALDAP
     202             :         REGISTER_LONG_CONSTANT("GSLC_SSL_NO_AUTH", GSLC_SSL_NO_AUTH, CONST_PERSISTENT | CONST_CS);
     203             :         REGISTER_LONG_CONSTANT("GSLC_SSL_ONEWAY_AUTH", GSLC_SSL_ONEWAY_AUTH, CONST_PERSISTENT | CONST_CS);
     204             :         REGISTER_LONG_CONSTANT("GSLC_SSL_TWOWAY_AUTH", GSLC_SSL_TWOWAY_AUTH, CONST_PERSISTENT | CONST_CS);
     205             : #endif
     206             : 
     207       20105 :         le_link = zend_register_list_destructors_ex(_close_ldap_link, NULL, "ldap link", module_number);
     208       20105 :         le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
     209       20105 :         le_result_entry = zend_register_list_destructors_ex(_free_ldap_result_entry, NULL, "ldap result entry", module_number);
     210             : 
     211       20105 :         Z_TYPE(ldap_module_entry) = type;
     212             : 
     213       20105 :         return SUCCESS;
     214             : }
     215             : /* }}} */
     216             : 
     217             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     218             :  */
     219       20141 : PHP_MSHUTDOWN_FUNCTION(ldap)
     220             : {
     221       20141 :         UNREGISTER_INI_ENTRIES();
     222       20141 :         return SUCCESS;
     223             : }
     224             : /* }}} */
     225             : 
     226             : /* {{{ PHP_MINFO_FUNCTION
     227             :  */
     228         148 : PHP_MINFO_FUNCTION(ldap)
     229             : {
     230             :         char tmp[32];
     231             : #if HAVE_NSLDAP
     232             :         LDAPVersion ver;
     233             :         double SDKVersion;
     234             : #endif
     235             : 
     236         148 :         php_info_print_table_start();
     237         148 :         php_info_print_table_row(2, "LDAP Support", "enabled");
     238         148 :         php_info_print_table_row(2, "RCS Version", "$Id$");
     239             : 
     240         148 :         if (LDAPG(max_links) == -1) {
     241         148 :                 snprintf(tmp, 31, "%ld/unlimited", LDAPG(num_links));
     242             :         } else {
     243           0 :                 snprintf(tmp, 31, "%ld/%ld", LDAPG(num_links), LDAPG(max_links));
     244             :         }
     245         148 :         php_info_print_table_row(2, "Total Links", tmp);
     246             : 
     247             : #ifdef LDAP_API_VERSION
     248         148 :         snprintf(tmp, 31, "%d", LDAP_API_VERSION);
     249         148 :         php_info_print_table_row(2, "API Version", tmp);
     250             : #endif
     251             : 
     252             : #ifdef LDAP_VENDOR_NAME
     253         148 :         php_info_print_table_row(2, "Vendor Name", LDAP_VENDOR_NAME);
     254             : #endif
     255             : 
     256             : #ifdef LDAP_VENDOR_VERSION
     257         148 :         snprintf(tmp, 31, "%d", LDAP_VENDOR_VERSION);
     258         148 :         php_info_print_table_row(2, "Vendor Version", tmp);
     259             : #endif
     260             : 
     261             : #if HAVE_NSLDAP
     262             :         SDKVersion = ldap_version(&ver);
     263             :         snprintf(tmp, 31, "%F", SDKVersion/100.0);
     264             :         php_info_print_table_row(2, "SDK Version", tmp);
     265             : 
     266             :         snprintf(tmp, 31, "%F", ver.protocol_version/100.0);
     267             :         php_info_print_table_row(2, "Highest LDAP Protocol Supported", tmp);
     268             : 
     269             :         snprintf(tmp, 31, "%F", ver.SSL_version/100.0);
     270             :         php_info_print_table_row(2, "SSL Level Supported", tmp);
     271             : 
     272             :         if (ver.security_level != LDAP_SECURITY_NONE) {
     273             :                 snprintf(tmp, 31, "%d", ver.security_level);
     274             :         } else {
     275             :                 strcpy(tmp, "SSL not enabled");
     276             :         }
     277             :         php_info_print_table_row(2, "Level of Encryption", tmp);
     278             : #endif
     279             : 
     280             : #ifdef HAVE_LDAP_SASL
     281         148 :         php_info_print_table_row(2, "SASL Support", "Enabled");
     282             : #endif
     283             : 
     284         148 :         php_info_print_table_end();
     285         148 :         DISPLAY_INI_ENTRIES();
     286         148 : }
     287             : /* }}} */
     288             : 
     289             : /* {{{ proto resource ldap_connect([string host [, int port [, string wallet [, string wallet_passwd [, int authmode]]]]])
     290             :    Connect to an LDAP server */
     291         207 : PHP_FUNCTION(ldap_connect)
     292             : {
     293         207 :         char *host = NULL;
     294             :         int hostlen;
     295         207 :         long port = 389; /* Default port */
     296             : #ifdef HAVE_ORALDAP
     297             :         char *wallet = NULL, *walletpasswd = NULL;
     298             :         int walletlen = 0, walletpasswdlen = 0;
     299             :         long authmode = GSLC_SSL_NO_AUTH;
     300             :         int ssl=0;
     301             : #endif
     302             :         ldap_linkdata *ld;
     303             :         LDAP *ldap;
     304             : 
     305             : #ifdef HAVE_ORALDAP
     306             :         if (ZEND_NUM_ARGS() == 3 || ZEND_NUM_ARGS() == 4) {
     307             :                 WRONG_PARAM_COUNT;
     308             :         }
     309             : 
     310             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|slssl", &host, &hostlen, &port, &wallet, &walletlen, &walletpasswd, &walletpasswdlen, &authmode) != SUCCESS) {
     311             :                 RETURN_FALSE;
     312             :         }
     313             : 
     314             :         if (ZEND_NUM_ARGS() == 5) {
     315             :                 ssl = 1;
     316             :         }
     317             : #else
     318         207 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &host, &hostlen, &port) != SUCCESS) {
     319           1 :                 RETURN_FALSE;
     320             :         }
     321             : #endif
     322             : 
     323         206 :         if (LDAPG(max_links) != -1 && LDAPG(num_links) >= LDAPG(max_links)) {
     324           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", LDAPG(num_links));
     325           1 :                 RETURN_FALSE;
     326             :         }
     327             : 
     328         205 :         ld = ecalloc(1, sizeof(ldap_linkdata));
     329             : 
     330             : #ifdef LDAP_API_FEATURE_X_OPENLDAP
     331         207 :         if (host != NULL && strchr(host, '/')) {
     332             :                 int rc;
     333             :                 
     334           3 :                 rc = ldap_initialize(&ldap, host);
     335           3 :                 if (rc != LDAP_SUCCESS) {
     336           1 :                         efree(ld);
     337           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not create session handle: %s", ldap_err2string(rc));
     338           1 :                         RETURN_FALSE;
     339             :                 }
     340             :         } else {
     341         202 :                 ldap = ldap_init(host, port);
     342             :         }
     343             : #else
     344             :         ldap = ldap_open(host, port);
     345             : #endif
     346             :         
     347         204 :         if (ldap == NULL) {
     348           0 :                 efree(ld);
     349           0 :                 RETURN_FALSE;
     350             :         } else {
     351             : #ifdef HAVE_ORALDAP
     352             :                 if (ssl) {
     353             :                         if (ldap_init_SSL(&ldap->ld_sb, wallet, walletpasswd, authmode)) {
     354             :                                 efree(ld);
     355             :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL init failed");
     356             :                                 RETURN_FALSE;
     357             :                         }
     358             :                 }                       
     359             : #endif
     360         204 :                 LDAPG(num_links)++;
     361         204 :                 ld->link = ldap;
     362         204 :                 ZEND_REGISTER_RESOURCE(return_value, ld, le_link);
     363             :         }
     364             : 
     365             : }
     366             : /* }}} */
     367             : 
     368             : /* {{{ _get_lderrno
     369             :  */
     370          15 : static int _get_lderrno(LDAP *ldap)
     371             : {
     372             : #if !HAVE_NSLDAP
     373             : #if LDAP_API_VERSION > 2000 || HAVE_ORALDAP_10
     374             :         int lderr;
     375             : 
     376             :         /* New versions of OpenLDAP do it this way */
     377          15 :         ldap_get_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
     378          15 :         return lderr;
     379             : #else
     380             :         return ldap->ld_errno;
     381             : #endif
     382             : #else
     383             :         return ldap_get_lderrno(ldap, NULL, NULL);
     384             : #endif
     385             : }
     386             : /* }}} */
     387             : 
     388             : /* {{{ proto bool ldap_bind(resource link [, string dn [, string password]])
     389             :    Bind to LDAP directory */
     390         172 : PHP_FUNCTION(ldap_bind)
     391             : {
     392             :         zval *link;
     393         172 :         char *ldap_bind_dn = NULL, *ldap_bind_pw = NULL;
     394             :         int ldap_bind_dnlen, ldap_bind_pwlen;
     395             :         ldap_linkdata *ld;
     396             :         int rc;
     397             : 
     398         172 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ss", &link, &ldap_bind_dn, &ldap_bind_dnlen, &ldap_bind_pw, &ldap_bind_pwlen) != SUCCESS) {
     399           1 :                 RETURN_FALSE;
     400             :         }
     401             : 
     402         171 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     403             : 
     404         171 :         if ((rc = ldap_bind_s(ld->link, ldap_bind_dn, ldap_bind_pw, LDAP_AUTH_SIMPLE)) != LDAP_SUCCESS) {
     405           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
     406           2 :                 RETURN_FALSE;
     407             :         } else {
     408         169 :                 RETURN_TRUE;
     409             :         }
     410             : }
     411             : /* }}} */
     412             : 
     413             : #ifdef HAVE_LDAP_SASL
     414             : typedef struct {
     415             :         char *mech;
     416             :         char *realm;
     417             :         char *authcid;
     418             :         char *passwd;
     419             :         char *authzid;
     420             : } php_ldap_bictx;
     421             : 
     422             : /* {{{ _php_sasl_setdefs
     423             :  */
     424           6 : static php_ldap_bictx *_php_sasl_setdefs(LDAP *ld, char *sasl_mech, char *sasl_realm, char *sasl_authc_id, char *passwd, char *sasl_authz_id)
     425             : {
     426             :         php_ldap_bictx *ctx;
     427             : 
     428           6 :         ctx = ber_memalloc(sizeof(php_ldap_bictx));     
     429           6 :         ctx->mech    = (sasl_mech) ? ber_strdup(sasl_mech) : NULL;
     430           6 :         ctx->realm   = (sasl_realm) ? ber_strdup(sasl_realm) : NULL;
     431           6 :         ctx->authcid = (sasl_authc_id) ? ber_strdup(sasl_authc_id) : NULL;
     432           6 :         ctx->passwd  = (passwd) ? ber_strdup(passwd) : NULL;
     433           6 :         ctx->authzid = (sasl_authz_id) ? ber_strdup(sasl_authz_id) : NULL;
     434             : 
     435           6 :         if (ctx->mech == NULL) {
     436           1 :                 ldap_get_option(ld, LDAP_OPT_X_SASL_MECH, &ctx->mech);
     437             :         }
     438           6 :         if (ctx->realm == NULL) {
     439           1 :                 ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &ctx->realm);
     440             :         }
     441           6 :         if (ctx->authcid == NULL) {
     442           1 :                 ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHCID, &ctx->authcid);
     443             :         }
     444           6 :         if (ctx->authzid == NULL) {
     445           5 :                 ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHZID, &ctx->authzid);
     446             :         }
     447             : 
     448           6 :         return ctx;
     449             : }
     450             : /* }}} */
     451             : 
     452             : /* {{{ _php_sasl_freedefs
     453             :  */
     454           6 : static void _php_sasl_freedefs(php_ldap_bictx *ctx)
     455             : {
     456           6 :         if (ctx->mech) ber_memfree(ctx->mech);
     457           6 :         if (ctx->realm) ber_memfree(ctx->realm);
     458           6 :         if (ctx->authcid) ber_memfree(ctx->authcid);
     459           6 :         if (ctx->passwd) ber_memfree(ctx->passwd);
     460           6 :         if (ctx->authzid) ber_memfree(ctx->authzid);
     461           6 :         ber_memfree(ctx);
     462           6 : }
     463             : /* }}} */
     464             : 
     465             : /* {{{ _php_sasl_interact
     466             :    Internal interact function for SASL */
     467           0 : static int _php_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *in)
     468             : {
     469           0 :         sasl_interact_t *interact = in;
     470             :         const char *p;
     471           0 :         php_ldap_bictx *ctx = defaults;
     472             : 
     473           0 :         for (;interact->id != SASL_CB_LIST_END;interact++) {
     474           0 :                 p = NULL;
     475           0 :                 switch(interact->id) {
     476             :                         case SASL_CB_GETREALM:
     477           0 :                                 p = ctx->realm;
     478           0 :                                 break;
     479             :                         case SASL_CB_AUTHNAME:
     480           0 :                                 p = ctx->authcid;
     481           0 :                                 break;
     482             :                         case SASL_CB_USER:
     483           0 :                                 p = ctx->authzid;
     484           0 :                                 break;
     485             :                         case SASL_CB_PASS:
     486           0 :                                 p = ctx->passwd;
     487             :                                 break;
     488             :                 }
     489           0 :                 if (p) {
     490           0 :                         interact->result = p;
     491           0 :                         interact->len = strlen(interact->result);
     492             :                 }
     493             :         }
     494           0 :         return LDAP_SUCCESS;
     495             : }
     496             : /* }}} */
     497             : 
     498             : /* {{{ proto bool ldap_sasl_bind(resource link [, string binddn [, string password [, string sasl_mech [, string sasl_realm [, string sasl_authc_id [, string sasl_authz_id [, string props]]]]]]])
     499             :    Bind to LDAP directory using SASL */
     500           7 : PHP_FUNCTION(ldap_sasl_bind)
     501             : {
     502             :         zval *link;
     503             :         ldap_linkdata *ld;
     504           7 :         char *binddn = NULL;
     505           7 :         char *passwd = NULL;
     506           7 :         char *sasl_mech = NULL;
     507           7 :         char *sasl_realm = NULL;
     508           7 :         char *sasl_authz_id = NULL;
     509           7 :         char *sasl_authc_id = NULL;
     510           7 :         char *props = NULL;
     511             :         int rc, dn_len, passwd_len, mech_len, realm_len, authc_id_len, authz_id_len, props_len;
     512             :         php_ldap_bictx *ctx;
     513             : 
     514           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|sssssss", &link, &binddn, &dn_len, &passwd, &passwd_len, &sasl_mech, &mech_len, &sasl_realm, &realm_len, &sasl_authc_id, &authc_id_len, &sasl_authz_id, &authz_id_len, &props, &props_len) != SUCCESS) {
     515           1 :                 RETURN_FALSE;
     516             :         }
     517             : 
     518           6 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     519             : 
     520           6 :         ctx = _php_sasl_setdefs(ld->link, sasl_mech, sasl_realm, sasl_authc_id, passwd, sasl_authz_id);
     521             : 
     522           6 :         if (props) {
     523           0 :                 ldap_set_option(ld->link, LDAP_OPT_X_SASL_SECPROPS, props);
     524             :         }
     525             : 
     526           6 :         rc = ldap_sasl_interactive_bind_s(ld->link, binddn, ctx->mech, NULL, NULL, LDAP_SASL_QUIET, _php_sasl_interact, ctx);
     527           6 :         if (rc != LDAP_SUCCESS) {
     528           6 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
     529           6 :                 RETVAL_FALSE;
     530             :         } else {
     531           0 :                 RETVAL_TRUE;
     532             :         }
     533           6 :         _php_sasl_freedefs(ctx);
     534             : }
     535             : /* }}} */
     536             : #endif /* HAVE_LDAP_SASL */
     537             : 
     538             : /* {{{ proto bool ldap_unbind(resource link)
     539             :    Unbind from LDAP directory */
     540          70 : PHP_FUNCTION(ldap_unbind)
     541             : {
     542             :         zval *link;
     543             :         ldap_linkdata *ld;
     544             : 
     545          70 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
     546           3 :                 RETURN_FALSE;
     547             :         }
     548             : 
     549          67 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     550             : 
     551          66 :         zend_list_delete(Z_LVAL_P(link));
     552          66 :         RETURN_TRUE;
     553             : }
     554             : /* }}} */
     555             : 
     556             : /* {{{ php_set_opts
     557             :  */
     558         103 : static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref, int *old_sizelimit, int *old_timelimit, int *old_deref)
     559             : {
     560             :         /* sizelimit */
     561         103 :         if (sizelimit > -1) {
     562             : #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP_10
     563          14 :                 ldap_get_option(ldap, LDAP_OPT_SIZELIMIT, old_sizelimit);
     564          14 :                 ldap_set_option(ldap, LDAP_OPT_SIZELIMIT, &sizelimit);
     565             : #else
     566             :                 *old_sizelimit = ldap->ld_sizelimit; 
     567             :                 ldap->ld_sizelimit = sizelimit; 
     568             : #endif
     569             :         }
     570             : 
     571             :         /* timelimit */
     572         103 :         if (timelimit > -1) {
     573             : #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP_10
     574          10 :                 ldap_get_option(ldap, LDAP_OPT_SIZELIMIT, old_timelimit);
     575          10 :                 ldap_set_option(ldap, LDAP_OPT_TIMELIMIT, &timelimit);
     576             : #else
     577             :                 *old_timelimit = ldap->ld_timelimit; 
     578             :                 ldap->ld_timelimit = timelimit; 
     579             : #endif
     580             :         }
     581             : 
     582             :         /* deref */
     583         103 :         if (deref > -1) {
     584             : #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP_10
     585           8 :                 ldap_get_option(ldap, LDAP_OPT_SIZELIMIT, old_deref);
     586           8 :                 ldap_set_option(ldap, LDAP_OPT_DEREF, &deref);
     587             : #else
     588             :                 *old_deref = ldap->ld_deref; 
     589             :                 ldap->ld_deref = deref; 
     590             : #endif
     591             :         }
     592         103 : }
     593             : /* }}} */
     594             : 
     595             : /* {{{ php_ldap_do_search
     596             :  */
     597          65 : static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
     598             : {
     599          65 :         zval *link, *base_dn, **filter, *attrs = NULL, **attr;
     600             :         long attrsonly, sizelimit, timelimit, deref;
     601          65 :         char *ldap_base_dn = NULL, *ldap_filter = NULL, **ldap_attrs = NULL; 
     602          65 :         ldap_linkdata *ld = NULL;
     603             :         LDAPMessage *ldap_res;
     604          65 :         int ldap_attrsonly = 0, ldap_sizelimit = -1, ldap_timelimit = -1, ldap_deref = -1;       
     605          65 :         int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1;       
     606          65 :         int num_attribs = 0, ret = 1, i, errno, argcount = ZEND_NUM_ARGS();
     607             : 
     608          65 :         if (zend_parse_parameters(argcount TSRMLS_CC, "zzZ|allll", &link, &base_dn, &filter, &attrs, &attrsonly,
     609             :                 &sizelimit, &timelimit, &deref) == FAILURE) {
     610          10 :                 return;
     611             :         }
     612             : 
     613             :         /* Reverse -> fall through */
     614          55 :         switch (argcount) {
     615             :                 case 8:
     616           4 :                         ldap_deref = deref;
     617             :                 case 7:
     618           5 :                         ldap_timelimit = timelimit;
     619             :                 case 6:
     620           7 :                         ldap_sizelimit = sizelimit;
     621             :                 case 5:
     622           8 :                         ldap_attrsonly = attrsonly;
     623             :                 case 4:
     624          24 :                         num_attribs = zend_hash_num_elements(Z_ARRVAL_P(attrs));
     625          24 :                         ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0);
     626             : 
     627          51 :                         for (i = 0; i<num_attribs; i++) {
     628          28 :                                 if (zend_hash_index_find(Z_ARRVAL_P(attrs), i, (void **) &attr) != SUCCESS) {
     629           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array initialization wrong");
     630           1 :                                         ret = 0;
     631           1 :                                         goto cleanup;
     632             :                                 }
     633             : 
     634          54 :                                 SEPARATE_ZVAL(attr);
     635          27 :                                 convert_to_string_ex(attr);
     636          27 :                                 ldap_attrs[i] = Z_STRVAL_PP(attr);
     637             :                         }
     638          23 :                         ldap_attrs[num_attribs] = NULL;
     639             :                 default:
     640             :                         break;
     641             :         }
     642             : 
     643             :         /* parallel search? */
     644          54 :         if (Z_TYPE_P(link) == IS_ARRAY) {
     645             :                 int i, nlinks, nbases, nfilters, *rcs;
     646             :                 ldap_linkdata **lds;
     647             :                 zval **entry, *resource;
     648             :                 
     649           6 :                 nlinks = zend_hash_num_elements(Z_ARRVAL_P(link));
     650           6 :                 if (nlinks == 0) {
     651           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "No links in link array");
     652           1 :                         ret = 0;
     653           1 :                         goto cleanup;
     654             :                 }
     655             : 
     656           5 :                 if (Z_TYPE_P(base_dn) == IS_ARRAY) {
     657           1 :                         nbases = zend_hash_num_elements(Z_ARRVAL_P(base_dn));
     658           1 :                         if (nbases != nlinks) {
     659           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Base must either be a string, or an array with the same number of elements as the links array");
     660           1 :                                 ret = 0;
     661           1 :                                 goto cleanup;
     662             :                         }
     663           0 :                         zend_hash_internal_pointer_reset(Z_ARRVAL_P(base_dn));
     664             :                 } else {
     665           4 :                         nbases = 0; /* this means string, not array */
     666             :                         /* If anything else than string is passed, ldap_base_dn = NULL */
     667           4 :                         if (Z_TYPE_P(base_dn) == IS_STRING) {
     668           2 :                                 ldap_base_dn = Z_STRVAL_P(base_dn);
     669             :                         } else {
     670           2 :                                 ldap_base_dn = NULL;
     671             :                         }
     672             :                 }
     673             : 
     674           4 :                 if (Z_TYPE_PP(filter) == IS_ARRAY) {
     675           2 :                         nfilters = zend_hash_num_elements(Z_ARRVAL_PP(filter));
     676           2 :                         if (nfilters != nlinks) {
     677           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter must either be a string, or an array with the same number of elements as the links array");
     678           1 :                                 ret = 0;
     679           1 :                                 goto cleanup;
     680             :                         }
     681           1 :                         zend_hash_internal_pointer_reset(Z_ARRVAL_PP(filter));
     682             :                 } else {
     683           2 :                         nfilters = 0; /* this means string, not array */
     684           2 :                         convert_to_string_ex(filter);
     685           2 :                         ldap_filter = Z_STRVAL_PP(filter);
     686             :                 }
     687             : 
     688           3 :                 lds = safe_emalloc(nlinks, sizeof(ldap_linkdata), 0);
     689           3 :                 rcs = safe_emalloc(nlinks, sizeof(*rcs), 0);
     690             :                 
     691           3 :                 zend_hash_internal_pointer_reset(Z_ARRVAL_P(link));
     692           9 :                 for (i=0; i<nlinks; i++) {
     693           6 :                         zend_hash_get_current_data(Z_ARRVAL_P(link), (void **)&entry);
     694             : 
     695           6 :                         ld = (ldap_linkdata *) zend_fetch_resource(entry TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
     696           6 :                         if (ld == NULL) {
     697           0 :                                 ret = 0;
     698           0 :                                 goto cleanup_parallel;
     699             :                         }
     700           6 :                         if (nbases != 0) { /* base_dn an array? */
     701           0 :                                 zend_hash_get_current_data(Z_ARRVAL_P(base_dn), (void **)&entry);
     702           0 :                                 zend_hash_move_forward(Z_ARRVAL_P(base_dn));
     703             : 
     704             :                                 /* If anything else than string is passed, ldap_base_dn = NULL */
     705           0 :                                 if (Z_TYPE_PP(entry) == IS_STRING) {
     706           0 :                                         ldap_base_dn = Z_STRVAL_PP(entry);
     707             :                                 } else {
     708           0 :                                         ldap_base_dn = NULL;
     709             :                                 }
     710             :                         }
     711           6 :                         if (nfilters != 0) { /* filter an array? */
     712           2 :                                 zend_hash_get_current_data(Z_ARRVAL_PP(filter), (void **)&entry);
     713           2 :                                 zend_hash_move_forward(Z_ARRVAL_PP(filter));
     714           2 :                                 convert_to_string_ex(entry);
     715           2 :                                 ldap_filter = Z_STRVAL_PP(entry);
     716             :                         }
     717             : 
     718           6 :                         php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
     719             : 
     720             :                         /* Run the actual search */     
     721           6 :                         rcs[i] = ldap_search(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly);
     722           6 :                         lds[i] = ld;
     723           6 :                         zend_hash_move_forward(Z_ARRVAL_P(link));
     724             :                 }
     725             :                 
     726           3 :                 array_init(return_value);
     727             : 
     728             :                 /* Collect results from the searches */
     729           9 :                 for (i=0; i<nlinks; i++) {
     730           6 :                         MAKE_STD_ZVAL(resource);
     731           6 :                         if (rcs[i] != -1) {
     732           6 :                                 rcs[i] = ldap_result(lds[i]->link, LDAP_RES_ANY, 1 /* LDAP_MSG_ALL */, NULL, &ldap_res);
     733             :                         }
     734           6 :                         if (rcs[i] != -1) {
     735           6 :                                 ZEND_REGISTER_RESOURCE(resource, ldap_res, le_result);
     736           6 :                                 add_next_index_zval(return_value, resource);
     737             :                         } else {
     738           0 :                                 add_next_index_bool(return_value, 0);
     739             :                         }
     740             :                 }
     741             : 
     742             : cleanup_parallel:
     743           3 :                 efree(lds);
     744           3 :                 efree(rcs);
     745             :         } else {
     746          51 :                 convert_to_string_ex(filter);
     747          48 :                 ldap_filter = Z_STRVAL_PP(filter);
     748             : 
     749             :                 /* If anything else than string is passed, ldap_base_dn = NULL */
     750          48 :                 if (Z_TYPE_P(base_dn) == IS_STRING) {
     751          47 :                         ldap_base_dn = Z_STRVAL_P(base_dn);
     752             :                 }
     753             : 
     754          48 :                 ld = (ldap_linkdata *) zend_fetch_resource(&link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
     755          48 :                 if (ld == NULL) {
     756           1 :                         ret = 0;
     757           1 :                         goto cleanup;
     758             :                 }
     759             : 
     760          47 :                 php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
     761             : 
     762             :                 /* Run the actual search */     
     763          47 :                 errno = ldap_search_s(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, &ldap_res);
     764             :         
     765          64 :                 if (errno != LDAP_SUCCESS
     766           9 :                         && errno != LDAP_SIZELIMIT_EXCEEDED
     767             : #ifdef LDAP_ADMINLIMIT_EXCEEDED
     768           3 :                         && errno != LDAP_ADMINLIMIT_EXCEEDED
     769             : #endif
     770             : #ifdef LDAP_REFERRAL
     771           3 :                         && errno != LDAP_REFERRAL
     772             : #endif
     773             :                 ) {
     774           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Search: %s", ldap_err2string(errno));
     775           2 :                         ret = 0;
     776             :                 } else {
     777          45 :                         if (errno == LDAP_SIZELIMIT_EXCEEDED) {
     778           6 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Sizelimit exceeded");
     779             :                         }
     780             : #ifdef LDAP_ADMINLIMIT_EXCEEDED
     781          39 :                         else if (errno == LDAP_ADMINLIMIT_EXCEEDED) {
     782           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Adminlimit exceeded");
     783             :                         }
     784             : #endif
     785             :                         
     786          45 :                         ZEND_REGISTER_RESOURCE(return_value, ldap_res, le_result);
     787             :                 }
     788             :         }
     789             : 
     790             : cleanup:
     791          55 :         if (ld) {
     792             :                 /* Restoring previous options */
     793          50 :                 php_set_opts(ld->link, old_ldap_sizelimit, old_ldap_timelimit, old_ldap_deref, &ldap_sizelimit, &ldap_timelimit, &ldap_deref);
     794             :         }
     795          55 :         if (ldap_attrs != NULL) {
     796          24 :                 efree(ldap_attrs);
     797             :         }
     798          55 :         if (!ret) {
     799           7 :                 RETVAL_BOOL(ret);
     800             :         }
     801             : }
     802             : /* }}} */
     803             : 
     804             : /* {{{ proto resource ldap_read(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
     805             :    Read an entry */
     806           6 : PHP_FUNCTION(ldap_read)
     807             : {
     808           6 :         php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_BASE);
     809           6 : }
     810             : /* }}} */
     811             : 
     812             : /* {{{ proto resource ldap_list(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
     813             :    Single-level search */
     814           6 : PHP_FUNCTION(ldap_list)
     815             : {
     816           6 :         php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_ONELEVEL);
     817           6 : }
     818             : /* }}} */
     819             : 
     820             : /* {{{ proto resource ldap_search(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
     821             :    Search LDAP tree under base_dn */
     822          53 : PHP_FUNCTION(ldap_search)
     823             : {
     824          53 :         php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_SUBTREE);
     825          53 : }
     826             : /* }}} */
     827             : 
     828             : /* {{{ proto bool ldap_free_result(resource result)
     829             :    Free result memory */
     830           3 : PHP_FUNCTION(ldap_free_result)
     831             : {
     832             :         zval *result;
     833             :         LDAPMessage *ldap_result;
     834             : 
     835           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) != SUCCESS) {
     836           1 :                 return;
     837             :         }
     838             : 
     839           2 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
     840             : 
     841           1 :         zend_list_delete(Z_LVAL_P(result));  /* Delete list entry */
     842           1 :         RETVAL_TRUE;
     843             : }
     844             : /* }}} */
     845             : 
     846             : /* {{{ proto int ldap_count_entries(resource link, resource result)
     847             :    Count the number of entries in a search result */
     848           3 : PHP_FUNCTION(ldap_count_entries)
     849             : {
     850             :         zval *link, *result;
     851             :         ldap_linkdata *ld;
     852             :         LDAPMessage *ldap_result;
     853             : 
     854           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
     855           1 :                 return;
     856             :         }
     857             : 
     858           2 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     859           2 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
     860             : 
     861           1 :         RETURN_LONG(ldap_count_entries(ld->link, ldap_result));
     862             : }
     863             : /* }}} */
     864             : 
     865             : /* {{{ proto resource ldap_first_entry(resource link, resource result)
     866             :    Return first result id */
     867          12 : PHP_FUNCTION(ldap_first_entry)
     868             : {
     869             :         zval *link, *result;
     870             :         ldap_linkdata *ld;      
     871             :         ldap_resultentry *resultentry;
     872             :         LDAPMessage *ldap_result, *entry;
     873             : 
     874          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
     875           2 :                 return;
     876             :         }
     877             : 
     878          10 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     879          10 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
     880             : 
     881           9 :         if ((entry = ldap_first_entry(ld->link, ldap_result)) == NULL) {
     882           0 :                 RETVAL_FALSE;
     883             :         } else {
     884           9 :                 resultentry = emalloc(sizeof(ldap_resultentry));
     885           9 :                 ZEND_REGISTER_RESOURCE(return_value, resultentry, le_result_entry);
     886           9 :                 resultentry->id = Z_LVAL_P(result);
     887           9 :                 zend_list_addref(resultentry->id);
     888           9 :                 resultentry->data = entry;
     889           9 :                 resultentry->ber = NULL;
     890             :         }
     891             : }
     892             : /* }}} */
     893             : 
     894             : /* {{{ proto resource ldap_next_entry(resource link, resource result_entry)
     895             :    Get next result entry */
     896           5 : PHP_FUNCTION(ldap_next_entry)
     897             : {
     898             :         zval *link, *result_entry;
     899             :         ldap_linkdata *ld;
     900             :         ldap_resultentry *resultentry, *resultentry_next;
     901             :         LDAPMessage *entry_next;
     902             : 
     903           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
     904           2 :                 return;
     905             :         }
     906             : 
     907           3 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     908           3 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
     909             : 
     910           2 :         if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
     911           1 :                 RETVAL_FALSE;
     912             :         } else {
     913           1 :                 resultentry_next = emalloc(sizeof(ldap_resultentry));
     914           1 :                 ZEND_REGISTER_RESOURCE(return_value, resultentry_next, le_result_entry);
     915           1 :                 resultentry_next->id = resultentry->id;
     916           1 :                 zend_list_addref(resultentry->id);
     917           1 :                 resultentry_next->data = entry_next;
     918           1 :                 resultentry_next->ber = NULL;
     919             :         }
     920             : }
     921             : /* }}} */
     922             : 
     923             : /* {{{ proto array ldap_get_entries(resource link, resource result)
     924             :    Get all result entries */
     925          38 : PHP_FUNCTION(ldap_get_entries)
     926             : {
     927             :         zval *link, *result;
     928             :         LDAPMessage *ldap_result, *ldap_result_entry;
     929             :         zval *tmp1, *tmp2;
     930             :         ldap_linkdata *ld;
     931             :         LDAP *ldap;
     932             :         int num_entries, num_attrib, num_values, i;
     933             :         BerElement *ber;
     934             :         char *attribute;
     935             :         size_t attr_len;
     936             :         struct berval **ldap_value;
     937             :         char *dn;
     938             : 
     939          38 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
     940           3 :                 return;
     941             :         }
     942             : 
     943          35 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
     944          35 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
     945             : 
     946          35 :         ldap = ld->link;
     947          35 :         num_entries = ldap_count_entries(ldap, ldap_result);
     948             : 
     949          35 :         array_init(return_value);
     950          35 :         add_assoc_long(return_value, "count", num_entries);
     951             : 
     952          35 :         if (num_entries == 0) {
     953           6 :                 return;
     954             :         }
     955             :         
     956          29 :         ldap_result_entry = ldap_first_entry(ldap, ldap_result);
     957          29 :         if (ldap_result_entry == NULL) {
     958             :                 zval_dtor(return_value);
     959           0 :                 RETURN_FALSE;
     960             :         }
     961             : 
     962          29 :         num_entries = 0;
     963         114 :         while (ldap_result_entry != NULL) {
     964          56 :                 MAKE_STD_ZVAL(tmp1);
     965          56 :                 array_init(tmp1);
     966             : 
     967          56 :                 num_attrib = 0;
     968          56 :                 attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
     969             : 
     970         240 :                 while (attribute != NULL) {
     971         128 :                         ldap_value = ldap_get_values_len(ldap, ldap_result_entry, attribute);
     972         128 :                         num_values = ldap_count_values_len(ldap_value);
     973             : 
     974         128 :                         MAKE_STD_ZVAL(tmp2);
     975         128 :                         array_init(tmp2);
     976         128 :                         add_assoc_long(tmp2, "count", num_values);
     977         255 :                         for (i = 0; i < num_values; i++) {
     978         127 :                                 add_index_stringl(tmp2, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len, 1);
     979             :                         }       
     980         128 :                         ldap_value_free_len(ldap_value);
     981             : 
     982         128 :                         attr_len = strlen(attribute);
     983         128 :                         zend_hash_update(Z_ARRVAL_P(tmp1), php_strtolower(attribute, attr_len), attr_len+1, (void *) &tmp2, sizeof(zval *), NULL);
     984         128 :                         add_index_string(tmp1, num_attrib, attribute, 1);
     985             : 
     986         128 :                         num_attrib++;
     987             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
     988         128 :                         ldap_memfree(attribute);
     989             : #endif
     990         128 :                         attribute = ldap_next_attribute(ldap, ldap_result_entry, ber);
     991             :                 }
     992             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
     993          56 :                 if (ber != NULL) {
     994          56 :                         ber_free(ber, 0);
     995             :                 }
     996             : #endif
     997             : 
     998          56 :                 add_assoc_long(tmp1, "count", num_attrib);
     999          56 :                 dn = ldap_get_dn(ldap, ldap_result_entry);
    1000          56 :                 add_assoc_string(tmp1, "dn", dn, 1);
    1001             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1002          56 :                 ldap_memfree(dn);
    1003             : #else
    1004             :                 free(dn);
    1005             : #endif
    1006             : 
    1007          56 :                 zend_hash_index_update(Z_ARRVAL_P(return_value), num_entries, (void *) &tmp1, sizeof(zval *), NULL);
    1008             :                 
    1009          56 :                 num_entries++;
    1010          56 :                 ldap_result_entry = ldap_next_entry(ldap, ldap_result_entry);
    1011             :         }
    1012             : 
    1013          29 :         add_assoc_long(return_value, "count", num_entries);
    1014             : 
    1015             : }
    1016             : /* }}} */
    1017             : 
    1018             : /* {{{ proto string ldap_first_attribute(resource link, resource result_entry)
    1019             :    Return first attribute */
    1020           4 : PHP_FUNCTION(ldap_first_attribute)
    1021             : {
    1022             :         zval *link, *result_entry;
    1023             :         ldap_linkdata *ld;
    1024             :         ldap_resultentry *resultentry;
    1025             :         char *attribute;
    1026             :         long dummy_ber;
    1027             : 
    1028           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
    1029           1 :                 return;
    1030             :         }
    1031             : 
    1032           3 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1033           3 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    1034             : 
    1035           2 :         if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
    1036           0 :                 RETURN_FALSE;
    1037             :         } else {
    1038           2 :                 RETVAL_STRING(attribute, 1);
    1039             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1040           2 :                 ldap_memfree(attribute);
    1041             : #endif
    1042             :         }
    1043             : }
    1044             : /* }}} */
    1045             : 
    1046             : /* {{{ proto string ldap_next_attribute(resource link, resource result_entry)
    1047             :    Get the next attribute in result */
    1048           6 : PHP_FUNCTION(ldap_next_attribute)
    1049             : {
    1050             :         zval *link, *result_entry;
    1051             :         ldap_linkdata *ld;
    1052             :         ldap_resultentry *resultentry;
    1053             :         char *attribute;
    1054             :         long dummy_ber;
    1055             : 
    1056           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
    1057           1 :                 return;
    1058             :         }
    1059             : 
    1060           5 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1061           5 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    1062             : 
    1063           4 :         if (resultentry->ber == NULL) {
    1064           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "called before calling ldap_first_attribute() or no attributes found in result entry");
    1065           1 :                 RETURN_FALSE;
    1066             :         }
    1067             : 
    1068           3 :         if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
    1069             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1070           1 :                 if (resultentry->ber != NULL) {
    1071           1 :                         ber_free(resultentry->ber, 0);
    1072           1 :                         resultentry->ber = NULL;
    1073             :                 }
    1074             : #endif
    1075           1 :                 RETURN_FALSE;
    1076             :         } else {
    1077           2 :                 RETVAL_STRING(attribute, 1);
    1078             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1079           2 :                 ldap_memfree(attribute);
    1080             : #endif
    1081             :         }
    1082             : }
    1083             : /* }}} */
    1084             : 
    1085             : /* {{{ proto array ldap_get_attributes(resource link, resource result_entry)
    1086             :    Get attributes from a search result entry */
    1087           3 : PHP_FUNCTION(ldap_get_attributes)
    1088             : {
    1089             :         zval *link, *result_entry;
    1090             :         zval *tmp;
    1091             :         ldap_linkdata *ld;
    1092             :         ldap_resultentry *resultentry;
    1093             :         char *attribute;
    1094             :         struct berval **ldap_value;
    1095             :         int i, num_values, num_attrib;
    1096             :         BerElement *ber;
    1097             : 
    1098           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
    1099           1 :                 return;
    1100             :         }
    1101             : 
    1102           2 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1103           2 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    1104             : 
    1105           1 :         array_init(return_value);
    1106           1 :         num_attrib = 0;
    1107             :         
    1108           1 :         attribute = ldap_first_attribute(ld->link, resultentry->data, &ber);
    1109           5 :         while (attribute != NULL) {
    1110           3 :                 ldap_value = ldap_get_values_len(ld->link, resultentry->data, attribute);
    1111           3 :                 num_values = ldap_count_values_len(ldap_value);
    1112             : 
    1113           3 :                 MAKE_STD_ZVAL(tmp);
    1114           3 :                 array_init(tmp);
    1115           3 :                 add_assoc_long(tmp, "count", num_values);
    1116           8 :                 for (i = 0; i < num_values; i++) {
    1117           5 :                         add_index_stringl(tmp, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len, 1);
    1118             :                 }
    1119           3 :                 ldap_value_free_len(ldap_value);
    1120             : 
    1121           3 :                 zend_hash_update(Z_ARRVAL_P(return_value), attribute, strlen(attribute)+1, (void *) &tmp, sizeof(zval *), NULL);
    1122           3 :                 add_index_string(return_value, num_attrib, attribute, 1);
    1123             : 
    1124           3 :                 num_attrib++;
    1125             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1126           3 :                 ldap_memfree(attribute);
    1127             : #endif
    1128           3 :                 attribute = ldap_next_attribute(ld->link, resultentry->data, ber);
    1129             :         }
    1130             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1131           1 :         if (ber != NULL) {
    1132           1 :                 ber_free(ber, 0);
    1133             :         }
    1134             : #endif
    1135             :         
    1136           1 :         add_assoc_long(return_value, "count", num_attrib);
    1137             : }
    1138             : /* }}} */
    1139             : 
    1140             : /* {{{ proto array ldap_get_values_len(resource link, resource result_entry, string attribute)
    1141             :    Get all values with lengths from a result entry */
    1142           7 : PHP_FUNCTION(ldap_get_values_len)
    1143             : {
    1144             :         zval *link, *result_entry;
    1145             :         ldap_linkdata *ld;
    1146             :         ldap_resultentry *resultentry;
    1147             :         char *attr;
    1148             :         struct berval **ldap_value_len;
    1149             :         int i, num_values, attr_len;
    1150             :         
    1151           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs", &link, &result_entry, &attr, &attr_len) != SUCCESS) {
    1152           3 :                 return;
    1153             :         }
    1154             :         
    1155           4 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1156           4 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    1157             : 
    1158           4 :         if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
    1159           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
    1160           1 :                 RETURN_FALSE;
    1161             :         }
    1162             :         
    1163           3 :         num_values = ldap_count_values_len(ldap_value_len);
    1164           3 :         array_init(return_value);
    1165             :         
    1166           6 :         for (i=0; i<num_values; i++) {
    1167           3 :                 add_next_index_stringl(return_value, ldap_value_len[i]->bv_val, ldap_value_len[i]->bv_len, 1);
    1168             :         }
    1169             :         
    1170           3 :         add_assoc_long(return_value, "count", num_values);
    1171           3 :         ldap_value_free_len(ldap_value_len);
    1172             : 
    1173             : }
    1174             : /* }}} */
    1175             : 
    1176             : /* {{{ proto string ldap_get_dn(resource link, resource result_entry)
    1177             :    Get the DN of a result entry */
    1178           3 : PHP_FUNCTION(ldap_get_dn) 
    1179             : {
    1180             :         zval *link, *result_entry;
    1181             :         ldap_linkdata *ld;
    1182             :         ldap_resultentry *resultentry;
    1183             :         char *text;
    1184             : 
    1185           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
    1186           1 :                 return;
    1187             :         }
    1188             :         
    1189           2 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1190           2 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    1191             : 
    1192           1 :         text = ldap_get_dn(ld->link, resultentry->data);
    1193           1 :         if (text != NULL) {
    1194           1 :                 RETVAL_STRING(text, 1);
    1195             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1196           1 :                 ldap_memfree(text);
    1197             : #else
    1198             :                 free(text);
    1199             : #endif
    1200             :         } else {
    1201           0 :                 RETURN_FALSE;
    1202             :         }
    1203             : }
    1204             : /* }}} */
    1205             : 
    1206             : /* {{{ proto array ldap_explode_dn(string dn, int with_attrib)
    1207             :    Splits DN into its component parts */
    1208          10 : PHP_FUNCTION(ldap_explode_dn)
    1209             : {
    1210             :         long with_attrib;
    1211             :         char *dn, **ldap_value;
    1212             :         int i, count, dn_len;
    1213             : 
    1214          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &dn, &dn_len, &with_attrib) != SUCCESS) {
    1215           2 :                 return;
    1216             :         }
    1217             : 
    1218           8 :         if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
    1219             :                 /* Invalid parameters were passed to ldap_explode_dn */
    1220           4 :                 RETURN_FALSE;
    1221             :         }
    1222             : 
    1223           4 :         i=0;
    1224           4 :         while (ldap_value[i] != NULL) i++;
    1225           4 :         count = i;
    1226             : 
    1227           4 :         array_init(return_value);
    1228             : 
    1229           4 :         add_assoc_long(return_value, "count", count);
    1230          18 :         for (i = 0; i<count; i++) {
    1231          14 :                 add_index_string(return_value, i, ldap_value[i], 1);
    1232             :         }
    1233             : 
    1234           4 :         ldap_value_free(ldap_value);
    1235             : }
    1236             : /* }}} */
    1237             : 
    1238             : /* {{{ proto string ldap_dn2ufn(string dn)
    1239             :    Convert DN to User Friendly Naming format */
    1240           5 : PHP_FUNCTION(ldap_dn2ufn)
    1241             : {
    1242             :         char *dn, *ufn;
    1243             :         int dn_len;
    1244             : 
    1245           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dn, &dn_len) != SUCCESS) {
    1246           1 :                 return;
    1247             :         }
    1248             :         
    1249           4 :         ufn = ldap_dn2ufn(dn);
    1250             :         
    1251           4 :         if (ufn != NULL) {
    1252           2 :                 RETVAL_STRING(ufn, 1);
    1253             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS
    1254           2 :                 ldap_memfree(ufn);
    1255             : #endif
    1256             :         } else {
    1257           2 :                 RETURN_FALSE;
    1258             :         }
    1259             : }
    1260             : /* }}} */
    1261             : 
    1262             : 
    1263             : /* added to fix use of ldap_modify_add for doing an ldap_add, gerrit thomson. */
    1264             : #define PHP_LD_FULL_ADD 0xff
    1265             : /* {{{ php_ldap_do_modify
    1266             :  */
    1267         223 : static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper)
    1268             : {
    1269             :         zval *link, *entry, **value, **ivalue;
    1270             :         ldap_linkdata *ld;
    1271             :         char *dn;
    1272             :         LDAPMod **ldap_mods;
    1273             :         int i, j, num_attribs, num_values, dn_len;
    1274             :         int *num_berval;
    1275             :         char *attribute;
    1276             :         ulong index;
    1277         223 :         int is_full_add=0; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit THomson */
    1278             : 
    1279         223 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &link, &dn, &dn_len, &entry) != SUCCESS) {
    1280          20 :                 return;
    1281             :         }       
    1282             : 
    1283         203 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1284             : 
    1285         203 :         num_attribs = zend_hash_num_elements(Z_ARRVAL_P(entry));
    1286         203 :         ldap_mods = safe_emalloc((num_attribs+1), sizeof(LDAPMod *), 0);
    1287         203 :         num_berval = safe_emalloc(num_attribs, sizeof(int), 0);
    1288         203 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(entry));
    1289             : 
    1290             :         /* added by gerrit thomson to fix ldap_add using ldap_mod_add */
    1291         203 :         if (oper == PHP_LD_FULL_ADD) {
    1292         185 :                 oper = LDAP_MOD_ADD;
    1293         185 :                 is_full_add = 1;
    1294             :         }
    1295             :         /* end additional , gerrit thomson */
    1296             : 
    1297        1021 :         for (i = 0; i < num_attribs; i++) {
    1298         822 :                 ldap_mods[i] = emalloc(sizeof(LDAPMod));
    1299         822 :                 ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
    1300         822 :                 ldap_mods[i]->mod_type = NULL;
    1301             : 
    1302         822 :                 if (zend_hash_get_current_key(Z_ARRVAL_P(entry), &attribute, &index, 0) == HASH_KEY_IS_STRING) {
    1303         819 :                         ldap_mods[i]->mod_type = estrdup(attribute);
    1304             :                 } else {
    1305           3 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown attribute in the data");
    1306             :                         /* Free allocated memory */
    1307           9 :                         while (i >= 0) {
    1308           3 :                                 if (ldap_mods[i]->mod_type) {
    1309           0 :                                         efree(ldap_mods[i]->mod_type);
    1310             :                                 }
    1311           3 :                                 efree(ldap_mods[i]);
    1312           3 :                                 i--;
    1313             :                         }
    1314           3 :                         efree(num_berval);
    1315           3 :                         efree(ldap_mods);       
    1316           3 :                         RETURN_FALSE;
    1317             :                 }
    1318             : 
    1319         819 :                 zend_hash_get_current_data(Z_ARRVAL_P(entry), (void **)&value);
    1320             : 
    1321         819 :                 if (Z_TYPE_PP(value) != IS_ARRAY) {
    1322         758 :                         num_values = 1;
    1323             :                 } else {
    1324          61 :                         num_values = zend_hash_num_elements(Z_ARRVAL_PP(value));
    1325             :                 }
    1326             :                 
    1327         819 :                 num_berval[i] = num_values;
    1328         819 :                 ldap_mods[i]->mod_bvalues = safe_emalloc((num_values + 1), sizeof(struct berval *), 0);
    1329             : 
    1330             : /* allow for arrays with one element, no allowance for arrays with none but probably not required, gerrit thomson. */
    1331        1577 :                 if ((num_values == 1) && (Z_TYPE_PP(value) != IS_ARRAY)) {
    1332         758 :                         convert_to_string_ex(value);
    1333         758 :                         ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
    1334         758 :                         ldap_mods[i]->mod_bvalues[0]->bv_len = Z_STRLEN_PP(value);
    1335         758 :                         ldap_mods[i]->mod_bvalues[0]->bv_val = Z_STRVAL_PP(value);
    1336             :                 } else {        
    1337         237 :                         for (j = 0; j < num_values; j++) {
    1338         177 :                                 if (zend_hash_index_find(Z_ARRVAL_PP(value), j, (void **) &ivalue) != SUCCESS) {
    1339           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value array must have consecutive indices 0, 1, ...");
    1340           1 :                                         num_berval[i] = j;
    1341           1 :                                         num_attribs = i + 1;
    1342           1 :                                         RETVAL_FALSE;
    1343           1 :                                         goto errexit;
    1344             :                                 }
    1345         176 :                                 convert_to_string_ex(ivalue);
    1346         176 :                                 ldap_mods[i]->mod_bvalues[j] = (struct berval *) emalloc (sizeof(struct berval));
    1347         176 :                                 ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_PP(ivalue);
    1348         176 :                                 ldap_mods[i]->mod_bvalues[j]->bv_val = Z_STRVAL_PP(ivalue);
    1349             :                         }
    1350             :                 }
    1351         818 :                 ldap_mods[i]->mod_bvalues[num_values] = NULL;
    1352         818 :                 zend_hash_move_forward(Z_ARRVAL_P(entry));
    1353             :         }
    1354         199 :         ldap_mods[num_attribs] = NULL;
    1355             : 
    1356             : /* check flag to see if do_mod was called to perform full add , gerrit thomson */
    1357         199 :         if (is_full_add == 1) {
    1358         183 :                 if ((i = ldap_add_s(ld->link, dn, ldap_mods)) != LDAP_SUCCESS) {
    1359           6 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Add: %s", ldap_err2string(i));
    1360           6 :                         RETVAL_FALSE;
    1361         177 :                 } else RETVAL_TRUE;
    1362             :         } else {
    1363          16 :                 if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
    1364          12 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modify: %s", ldap_err2string(i));
    1365          12 :                         RETVAL_FALSE;
    1366           4 :                 } else RETVAL_TRUE;     
    1367             :         }
    1368             : 
    1369             : errexit:
    1370        1019 :         for (i = 0; i < num_attribs; i++) {
    1371         819 :                 efree(ldap_mods[i]->mod_type);
    1372        1753 :                 for (j = 0; j < num_berval[i]; j++) {
    1373         934 :                         efree(ldap_mods[i]->mod_bvalues[j]);
    1374             :                 }
    1375         819 :                 efree(ldap_mods[i]->mod_bvalues);
    1376         819 :                 efree(ldap_mods[i]);
    1377             :         }
    1378         200 :         efree(num_berval);
    1379         200 :         efree(ldap_mods);       
    1380             : 
    1381         200 :         return;
    1382             : }
    1383             : /* }}} */
    1384             : 
    1385             : /* {{{ proto bool ldap_add(resource link, string dn, array entry)
    1386             :    Add entries to LDAP directory */
    1387         189 : PHP_FUNCTION(ldap_add)
    1388             : {
    1389             :         /* use a newly define parameter into the do_modify so ldap_mod_add can be used the way it is supposed to be used , Gerrit THomson */
    1390         189 :         php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_LD_FULL_ADD);
    1391         189 : }
    1392             : /* }}} */
    1393             : 
    1394             : /* three functions for attribute base modifications, gerrit Thomson */
    1395             : 
    1396             : /* {{{ proto bool ldap_mod_replace(resource link, string dn, array entry)
    1397             :    Replace attribute values with new ones */
    1398          17 : PHP_FUNCTION(ldap_mod_replace)
    1399             : {
    1400          17 :         php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_REPLACE);
    1401          17 : }
    1402             : /* }}} */
    1403             : 
    1404             : /* {{{ proto bool ldap_mod_add(resource link, string dn, array entry)
    1405             :    Add attribute values to current */
    1406           9 : PHP_FUNCTION(ldap_mod_add)
    1407             : {
    1408           9 :         php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_ADD);
    1409           9 : }
    1410             : /* }}} */
    1411             : 
    1412             : /* {{{ proto bool ldap_mod_del(resource link, string dn, array entry)
    1413             :    Delete attribute values */
    1414           8 : PHP_FUNCTION(ldap_mod_del)
    1415             : {
    1416           8 :         php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_DELETE);
    1417           8 : }
    1418             : /* }}} */
    1419             : 
    1420             : /* {{{ proto bool ldap_delete(resource link, string dn)
    1421             :    Delete an entry from a directory */
    1422         185 : PHP_FUNCTION(ldap_delete)
    1423             : {
    1424             :         zval *link;
    1425             :         ldap_linkdata *ld;
    1426             :         char *dn;
    1427             :         int rc, dn_len;
    1428             : 
    1429         185 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &link, &dn, &dn_len) != SUCCESS) {
    1430           3 :                 return;
    1431             :         }
    1432             : 
    1433         182 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1434             : 
    1435         182 :         if ((rc = ldap_delete_s(ld->link, dn)) != LDAP_SUCCESS) {
    1436           5 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Delete: %s", ldap_err2string(rc));
    1437           5 :                 RETURN_FALSE;
    1438             :         }
    1439             : 
    1440         177 :         RETURN_TRUE;
    1441             : }
    1442             : /* }}} */
    1443             : 
    1444             : /* {{{ _ldap_str_equal_to_const
    1445             :  */
    1446          78 : static int _ldap_str_equal_to_const(const char *str, uint str_len, const char *cstr)
    1447             : {
    1448             :         int i;
    1449             : 
    1450          78 :         if (strlen(cstr) != str_len)
    1451          26 :                 return 0;
    1452             : 
    1453         306 :         for (i = 0; i < str_len; ++i) {
    1454         266 :                 if (str[i] != cstr[i]) {
    1455          12 :                         return 0;
    1456             :                 }
    1457             :         }
    1458             : 
    1459          40 :         return 1;
    1460             : }
    1461             : /* }}} */
    1462             : 
    1463             : /* {{{ _ldap_strlen_max
    1464             :  */
    1465          12 : static int _ldap_strlen_max(const char *str, uint max_len)
    1466             : {
    1467             :         int i;
    1468             : 
    1469         177 :         for (i = 0; i < max_len; ++i) {
    1470         165 :                 if (str[i] == '\0') {
    1471           0 :                         return i;
    1472             :                 }
    1473             :         }
    1474             : 
    1475          12 :         return max_len;
    1476             : }
    1477             : /* }}} */
    1478             : 
    1479             : /* {{{ _ldap_hash_fetch
    1480             :  */
    1481          21 : static void _ldap_hash_fetch(zval *hashTbl, const char *key, zval **out)
    1482             : {
    1483             :         zval **fetched;
    1484          21 :         if (zend_hash_find(Z_ARRVAL_P(hashTbl), key, strlen(key)+1, (void **) &fetched) == SUCCESS) {
    1485          20 :                 *out = *fetched;
    1486             :         }
    1487             :         else {
    1488           1 :                 *out = NULL;
    1489             :         }
    1490          21 : }
    1491             : /* }}} */
    1492             : 
    1493             : /* {{{ proto bool ldap_modify_batch(resource link, string dn, array modifs)
    1494             :    Perform multiple modifications as part of one operation */
    1495           9 : PHP_FUNCTION(ldap_modify_batch)
    1496             : {
    1497             :         ldap_linkdata *ld;
    1498             :         zval *link, *mods, *mod, *modinfo, *modval;
    1499             :         zval *attrib, *modtype, *vals;
    1500             :         zval **fetched;
    1501             :         char *dn;
    1502             :         int dn_len;
    1503             :         int i, j, k;
    1504             :         int num_mods, num_modprops, num_modvals;
    1505             :         LDAPMod **ldap_mods;
    1506             :         uint oper;
    1507             : 
    1508             :         /*
    1509             :         $mods = array(
    1510             :                 array(
    1511             :                         "attrib" => "unicodePwd",
    1512             :                         "modtype" => LDAP_MODIFY_BATCH_REMOVE,
    1513             :                         "values" => array($oldpw)
    1514             :                 ),
    1515             :                 array(
    1516             :                         "attrib" => "unicodePwd",
    1517             :                         "modtype" => LDAP_MODIFY_BATCH_ADD,
    1518             :                         "values" => array($newpw)
    1519             :                 ),
    1520             :                 array(
    1521             :                         "attrib" => "userPrincipalName",
    1522             :                         "modtype" => LDAP_MODIFY_BATCH_REPLACE,
    1523             :                         "values" => array("janitor@corp.contoso.com")
    1524             :                 ),
    1525             :                 array(
    1526             :                         "attrib" => "userCert",
    1527             :                         "modtype" => LDAP_MODIFY_BATCH_REMOVE_ALL
    1528             :                 )
    1529             :         );
    1530             :         */
    1531             : 
    1532           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &link, &dn, &dn_len, &mods) != SUCCESS) {
    1533           4 :                 return;
    1534             :         }
    1535             : 
    1536           5 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1537             : 
    1538             :         /* perform validation */
    1539             :         {
    1540             :                 char *modkey;
    1541             :                 uint modkeylen;
    1542             :                 long modtype;
    1543             : 
    1544             :                 /* to store the wrongly-typed keys */
    1545             :                 ulong tmpUlong;
    1546             : 
    1547             :                 /* make sure the DN contains no NUL bytes */
    1548           5 :                 if (_ldap_strlen_max(dn, dn_len) != dn_len) {
    1549           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "DN must not contain NUL bytes");
    1550           0 :                         RETURN_FALSE;
    1551             :                 }
    1552             : 
    1553             :                 /* make sure the top level is a normal array */
    1554           5 :                 zend_hash_internal_pointer_reset(Z_ARRVAL_P(mods));
    1555           5 :                 if (zend_hash_get_current_key_type(Z_ARRVAL_P(mods)) != HASH_KEY_IS_LONG) {
    1556           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modifications array must not be string-indexed");
    1557           0 :                         RETURN_FALSE;
    1558             :                 }
    1559             : 
    1560           5 :                 num_mods = zend_hash_num_elements(Z_ARRVAL_P(mods));
    1561             : 
    1562          12 :                 for (i = 0; i < num_mods; i++) {
    1563             :                         /* is the numbering consecutive? */
    1564           7 :                         if (zend_hash_index_find(Z_ARRVAL_P(mods), i, (void **) &fetched) != SUCCESS) {
    1565           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modifications array must have consecutive indices 0, 1, ...");
    1566           0 :                                 RETURN_FALSE;
    1567             :                         }
    1568           7 :                         mod = *fetched;
    1569             : 
    1570             :                         /* is it an array? */
    1571           7 :                         if (Z_TYPE_P(mod) != IS_ARRAY) {
    1572           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each entry of modifications array must be an array itself");
    1573           0 :                                 RETURN_FALSE;
    1574             :                         }
    1575             : 
    1576             :                         /* for the modification hashtable... */
    1577           7 :                         zend_hash_internal_pointer_reset(Z_ARRVAL_P(mod));
    1578           7 :                         num_modprops = zend_hash_num_elements(Z_ARRVAL_P(mod));
    1579             : 
    1580          27 :                         for (j = 0; j < num_modprops; j++) {
    1581             :                                 /* are the keys strings? */
    1582          20 :                                 if (zend_hash_get_current_key_ex(Z_ARRVAL_P(mod), &modkey, &modkeylen, &tmpUlong, 0, NULL) != HASH_KEY_IS_STRING) {
    1583           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each entry of modifications array must be string-indexed");
    1584           0 :                                         RETURN_FALSE;
    1585             :                                 }
    1586             : 
    1587             :                                 /* modkeylen includes the terminating NUL byte; remove that */
    1588          20 :                                 --modkeylen;
    1589             : 
    1590             :                                 /* is this a valid entry? */
    1591          39 :                                 if (
    1592          20 :                                         !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_ATTRIB) &&
    1593          13 :                                         !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_MODTYPE) &&
    1594           6 :                                         !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_VALUES)
    1595             :                                 ) {
    1596           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The only allowed keys in entries of the modifications array are '" LDAP_MODIFY_BATCH_ATTRIB "', '" LDAP_MODIFY_BATCH_MODTYPE "' and '" LDAP_MODIFY_BATCH_VALUES "'");
    1597           0 :                                         RETURN_FALSE;
    1598             :                                 }
    1599             : 
    1600          20 :                                 zend_hash_get_current_data(Z_ARRVAL_P(mod), (void **) &fetched);
    1601          20 :                                 modinfo = *fetched;
    1602             : 
    1603             :                                 /* does the value type match the key? */
    1604          20 :                                 if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_ATTRIB)) {
    1605           7 :                                         if (Z_TYPE_P(modinfo) != IS_STRING) {
    1606           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must be a string");
    1607           0 :                                                 RETURN_FALSE;
    1608             :                                         }
    1609             : 
    1610           7 :                                         if (Z_STRLEN_P(modinfo) != _ldap_strlen_max(Z_STRVAL_P(modinfo), Z_STRLEN_P(modinfo))) {
    1611           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must not contain NUL bytes");
    1612           0 :                                                 RETURN_FALSE;
    1613             :                                         }
    1614             :                                 }
    1615          13 :                                 else if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_MODTYPE)) {
    1616           7 :                                         if (Z_TYPE_P(modinfo) != IS_LONG) {
    1617           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_MODTYPE "' value must be a long");
    1618           0 :                                                 RETURN_FALSE;
    1619             :                                         }
    1620             : 
    1621             :                                         /* is the value in range? */
    1622           7 :                                         modtype = Z_LVAL_P(modinfo);
    1623           7 :                                         if (
    1624             :                                                 modtype != LDAP_MODIFY_BATCH_ADD &&
    1625             :                                                 modtype != LDAP_MODIFY_BATCH_REMOVE &&
    1626             :                                                 modtype != LDAP_MODIFY_BATCH_REPLACE &&
    1627             :                                                 modtype != LDAP_MODIFY_BATCH_REMOVE_ALL
    1628             :                                         ) {
    1629           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The '" LDAP_MODIFY_BATCH_MODTYPE "' value must match one of the LDAP_MODIFY_BATCH_* constants");
    1630           0 :                                                 RETURN_FALSE;
    1631             :                                         }
    1632             : 
    1633             :                                         /* if it's REMOVE_ALL, there must not be a values array; otherwise, there must */
    1634           7 :                                         if (modtype == LDAP_MODIFY_BATCH_REMOVE_ALL) {
    1635           1 :                                                 if (zend_hash_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES) + 1)) {
    1636           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must not be provided");
    1637           0 :                                                         RETURN_FALSE;
    1638             :                                                 }
    1639             :                                         }
    1640             :                                         else {
    1641           6 :                                                 if (!zend_hash_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES) + 1)) {
    1642           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is not LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must be provided");
    1643           0 :                                                         RETURN_FALSE;
    1644             :                                                 }
    1645             :                                         }
    1646             :                                 }
    1647           6 :                                 else if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_VALUES)) {
    1648           6 :                                         if (Z_TYPE_P(modinfo) != IS_ARRAY) {
    1649           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' value must be an array");
    1650           0 :                                                 RETURN_FALSE;
    1651             :                                         }
    1652             : 
    1653             :                                         /* is the array not empty? */
    1654           6 :                                         zend_hash_internal_pointer_reset(Z_ARRVAL_P(modinfo));
    1655           6 :                                         num_modvals = zend_hash_num_elements(Z_ARRVAL_P(modinfo));
    1656           6 :                                         if (num_modvals == 0) {
    1657           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have at least one element");
    1658           0 :                                                 RETURN_FALSE;
    1659             :                                         }
    1660             : 
    1661             :                                         /* are its keys integers? */
    1662           6 :                                         if (zend_hash_get_current_key_type(Z_ARRVAL_P(modinfo)) != HASH_KEY_IS_LONG) {
    1663           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must not be string-indexed");
    1664           0 :                                                 RETURN_FALSE;
    1665             :                                         }
    1666             : 
    1667             :                                         /* are the keys consecutive? */
    1668          13 :                                         for (k = 0; k < num_modvals; k++) {
    1669           7 :                                                 if (zend_hash_index_find(Z_ARRVAL_P(modinfo), k, (void **) &fetched) != SUCCESS) {
    1670           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have consecutive indices 0, 1, ...");
    1671           0 :                                                         RETURN_FALSE;
    1672             :                                                 }
    1673           7 :                                                 modval = *fetched;
    1674             : 
    1675             :                                                 /* is the data element a string? */
    1676           7 :                                                 if (Z_TYPE_P(modval) != IS_STRING) {
    1677           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each element of a '" LDAP_MODIFY_BATCH_VALUES "' array must be a string");
    1678           0 :                                                         RETURN_FALSE;
    1679             :                                                 }
    1680             :                                         }
    1681             :                                 }
    1682             : 
    1683          20 :                                 zend_hash_move_forward(Z_ARRVAL_P(mod));
    1684             :                         }
    1685             :                 }
    1686             :         }
    1687             :         /* validation was successful */
    1688             : 
    1689             :         /* allocate array of modifications */
    1690           5 :         ldap_mods = safe_emalloc((num_mods+1), sizeof(LDAPMod *), 0);
    1691             : 
    1692             :         /* for each modification */
    1693          12 :         for (i = 0; i < num_mods; i++) {
    1694             :                 /* allocate the modification struct */
    1695           7 :                 ldap_mods[i] = safe_emalloc(1, sizeof(LDAPMod), 0);
    1696             : 
    1697             :                 /* fetch the relevant data */
    1698           7 :                 zend_hash_index_find(Z_ARRVAL_P(mods), i, (void **) &fetched);
    1699           7 :                 mod = *fetched;
    1700             : 
    1701           7 :                 _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_ATTRIB, &attrib);
    1702           7 :                 _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_MODTYPE, &modtype);
    1703           7 :                 _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_VALUES, &vals);
    1704             : 
    1705             :                 /* map the modification type */
    1706           7 :                 switch (Z_LVAL_P(modtype)) {
    1707             :                         case LDAP_MODIFY_BATCH_ADD:
    1708           4 :                                 oper = LDAP_MOD_ADD;
    1709           4 :                                 break;
    1710             :                         case LDAP_MODIFY_BATCH_REMOVE:
    1711             :                         case LDAP_MODIFY_BATCH_REMOVE_ALL:
    1712           1 :                                 oper = LDAP_MOD_DELETE;
    1713           1 :                                 break;
    1714             :                         case LDAP_MODIFY_BATCH_REPLACE:
    1715           2 :                                 oper = LDAP_MOD_REPLACE;
    1716           2 :                                 break;
    1717             :                         default:
    1718           0 :                                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unknown and uncaught modification type.");
    1719           0 :                                 RETURN_FALSE;
    1720             :                 }
    1721             : 
    1722             :                 /* fill in the basic info */
    1723           7 :                 ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
    1724           7 :                 ldap_mods[i]->mod_type = estrndup(Z_STRVAL_P(attrib), Z_STRLEN_P(attrib));
    1725             : 
    1726           7 :                 if (Z_LVAL_P(modtype) == LDAP_MODIFY_BATCH_REMOVE_ALL) {
    1727             :                         /* no values */
    1728           1 :                         ldap_mods[i]->mod_bvalues = NULL;
    1729             :                 }
    1730             :                 else {
    1731             :                         /* allocate space for the values as part of this modification */
    1732           6 :                         num_modvals = zend_hash_num_elements(Z_ARRVAL_P(vals));
    1733           6 :                         ldap_mods[i]->mod_bvalues = safe_emalloc((num_modvals+1), sizeof(struct berval *), 0);
    1734             : 
    1735             :                         /* for each value */
    1736          13 :                         for (j = 0; j < num_modvals; j++) {
    1737             :                                 /* fetch it */
    1738           7 :                                 zend_hash_index_find(Z_ARRVAL_P(vals), j, (void **) &fetched);
    1739           7 :                                 modval = *fetched;
    1740             : 
    1741             :                                 /* allocate the data struct */
    1742           7 :                                 ldap_mods[i]->mod_bvalues[j] = safe_emalloc(1, sizeof(struct berval), 0);
    1743             : 
    1744             :                                 /* fill it */
    1745           7 :                                 ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_P(modval);
    1746           7 :                                 ldap_mods[i]->mod_bvalues[j]->bv_val = estrndup(Z_STRVAL_P(modval), Z_STRLEN_P(modval));
    1747             :                         }
    1748             : 
    1749             :                         /* NULL-terminate values */
    1750           6 :                         ldap_mods[i]->mod_bvalues[num_modvals] = NULL;
    1751             :                 }
    1752             :         }
    1753             : 
    1754             :         /* NULL-terminate modifications */
    1755           5 :         ldap_mods[num_mods] = NULL;
    1756             : 
    1757             :         /* perform (finally) */
    1758           5 :         if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
    1759           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Batch Modify: %s", ldap_err2string(i));
    1760           4 :                 RETVAL_FALSE;
    1761           1 :         } else RETVAL_TRUE;
    1762             : 
    1763             :         /* clean up */
    1764             :         {
    1765          12 :                 for (i = 0; i < num_mods; i++) {
    1766             :                         /* attribute */
    1767           7 :                         efree(ldap_mods[i]->mod_type);
    1768             : 
    1769           7 :                         if (ldap_mods[i]->mod_bvalues != NULL) {
    1770             :                                 /* each BER value */
    1771          13 :                                 for (j = 0; ldap_mods[i]->mod_bvalues[j] != NULL; j++) {
    1772             :                                         /* free the data bytes */
    1773           7 :                                         efree(ldap_mods[i]->mod_bvalues[j]->bv_val);
    1774             : 
    1775             :                                         /* free the bvalue struct */
    1776           7 :                                         efree(ldap_mods[i]->mod_bvalues[j]);
    1777             :                                 }
    1778             : 
    1779             :                                 /* the BER value array */
    1780           6 :                                 efree(ldap_mods[i]->mod_bvalues);
    1781             :                         }
    1782             : 
    1783             :                         /* the modification */
    1784           7 :                         efree(ldap_mods[i]);
    1785             :                 }
    1786             : 
    1787             :                 /* the modifications array */
    1788           5 :                 efree(ldap_mods);
    1789             :         }
    1790             : }
    1791             : /* }}} */
    1792             : 
    1793             : /* {{{ proto int ldap_errno(resource link)
    1794             :    Get the current ldap error number */
    1795           9 : PHP_FUNCTION(ldap_errno)
    1796             : {
    1797             :         zval *link;
    1798             :         ldap_linkdata *ld;
    1799             : 
    1800           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
    1801           2 :                 return;
    1802             :         }
    1803             : 
    1804           7 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1805             : 
    1806           7 :         RETURN_LONG(_get_lderrno(ld->link));
    1807             : }
    1808             : /* }}} */
    1809             : 
    1810             : /* {{{ proto string ldap_err2str(int errno)
    1811             :    Convert error number to error string */
    1812           4 : PHP_FUNCTION(ldap_err2str)
    1813             : {
    1814             :         long perrno;
    1815             : 
    1816           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &perrno) != SUCCESS) {
    1817           3 :                 return;
    1818             :         }
    1819             : 
    1820           1 :         RETURN_STRING(ldap_err2string(perrno), 1);
    1821             : }
    1822             : /* }}} */
    1823             : 
    1824             : /* {{{ proto string ldap_error(resource link)
    1825             :    Get the current ldap error string */
    1826           9 : PHP_FUNCTION(ldap_error) 
    1827             : {
    1828             :         zval *link;
    1829             :         ldap_linkdata *ld;
    1830             :         int ld_errno;
    1831             : 
    1832           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
    1833           2 :                 return;
    1834             :         }
    1835             : 
    1836           7 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1837             : 
    1838           7 :         ld_errno = _get_lderrno(ld->link);
    1839             : 
    1840           7 :         RETURN_STRING(ldap_err2string(ld_errno), 1);
    1841             : }
    1842             : /* }}} */
    1843             : 
    1844             : /* {{{ proto bool ldap_compare(resource link, string dn, string attr, string value)
    1845             :    Determine if an entry has a specific value for one of its attributes */
    1846           7 : PHP_FUNCTION(ldap_compare) 
    1847             : {
    1848             :         zval *link;
    1849             :         char *dn, *attr, *value;
    1850             :         int dn_len, attr_len, value_len;
    1851             :         ldap_linkdata *ld;
    1852             :         int errno;
    1853             : 
    1854           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss", &link, &dn, &dn_len, &attr, &attr_len, &value, &value_len) != SUCCESS) {
    1855           4 :                 return;
    1856             :         }
    1857             : 
    1858           3 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1859             : 
    1860           3 :         errno = ldap_compare_s(ld->link, dn, attr, value);
    1861             : 
    1862           3 :         switch (errno) {
    1863             :                 case LDAP_COMPARE_TRUE:
    1864           1 :                         RETURN_TRUE;
    1865             :                         break;
    1866             : 
    1867             :                 case LDAP_COMPARE_FALSE:
    1868           1 :                         RETURN_FALSE;
    1869             :                         break;
    1870             :         }
    1871             :         
    1872           1 :         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Compare: %s", ldap_err2string(errno));
    1873           1 :         RETURN_LONG(-1);
    1874             : }
    1875             : /* }}} */
    1876             : 
    1877             : /* {{{ proto bool ldap_sort(resource link, resource result, string sortfilter)
    1878             :    Sort LDAP result entries */
    1879           7 : PHP_FUNCTION(ldap_sort)
    1880             : {
    1881             :         zval *link, *result;
    1882             :         ldap_linkdata *ld;
    1883             :         char *sortfilter;
    1884             :         int sflen;
    1885             :         zend_rsrc_list_entry *le;
    1886             : 
    1887           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs", &link, &result, &sortfilter, &sflen) != SUCCESS) {
    1888           4 :                 RETURN_FALSE;
    1889             :         }
    1890             : 
    1891           3 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1892             : 
    1893           3 :         if (zend_hash_index_find(&EG(regular_list), Z_LVAL_P(result), (void **) &le) != SUCCESS || le->type != le_result) {
    1894           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied resource is not a valid ldap result resource");
    1895           1 :                 RETURN_FALSE;
    1896             :         }
    1897             : 
    1898           2 :         if (ldap_sort_entries(ld->link, (LDAPMessage **) &le->ptr, sflen ? sortfilter : NULL, strcmp) != LDAP_SUCCESS) {
    1899           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ldap_err2string(errno));
    1900           0 :                 RETURN_FALSE;
    1901             :         }
    1902             : 
    1903           2 :         RETURN_TRUE;
    1904             : }
    1905             : /* }}} */
    1906             : 
    1907             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10
    1908             : /* {{{ proto bool ldap_get_option(resource link, int option, mixed retval)
    1909             :    Get the current value of various session-wide parameters */
    1910          23 : PHP_FUNCTION(ldap_get_option) 
    1911             : {
    1912             :         zval *link, *retval;
    1913             :         ldap_linkdata *ld;
    1914             :         long option;
    1915             :         
    1916          23 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &link, &option, &retval) != SUCCESS) {
    1917           4 :                 return;
    1918             :         }
    1919             : 
    1920          19 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    1921             : 
    1922          19 :         switch (option) {
    1923             :         /* options with int value */
    1924             :         case LDAP_OPT_DEREF:
    1925             :         case LDAP_OPT_SIZELIMIT:
    1926             :         case LDAP_OPT_TIMELIMIT:
    1927             :         case LDAP_OPT_PROTOCOL_VERSION:
    1928             :         case LDAP_OPT_ERROR_NUMBER:
    1929             :         case LDAP_OPT_REFERRALS:
    1930             : #ifdef LDAP_OPT_RESTART
    1931             :         case LDAP_OPT_RESTART:
    1932             : #endif
    1933             :                 {
    1934             :                         int val;
    1935             : 
    1936          12 :                         if (ldap_get_option(ld->link, option, &val)) {
    1937           0 :                                 RETURN_FALSE;
    1938             :                         }
    1939          12 :                         zval_dtor(retval);
    1940          12 :                         ZVAL_LONG(retval, val);
    1941          12 :                 } break;
    1942             : #ifdef LDAP_OPT_NETWORK_TIMEOUT
    1943             :         case LDAP_OPT_NETWORK_TIMEOUT:
    1944             :                 {
    1945           2 :                         struct timeval *timeout = NULL;
    1946             : 
    1947           2 :                         if (ldap_get_option(ld->link, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
    1948           0 :                                 if (timeout) {
    1949           0 :                                         ldap_memfree(timeout);
    1950             :                                 }
    1951           0 :                                 RETURN_FALSE;
    1952             :                         }                   
    1953           2 :                         if (!timeout) {
    1954           0 :                                 RETURN_FALSE;
    1955             :                         }
    1956           2 :                         zval_dtor(retval);
    1957           2 :                         ZVAL_LONG(retval, timeout->tv_sec);
    1958           2 :                         ldap_memfree(timeout);
    1959           2 :                 } break;
    1960             : #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
    1961             :         case LDAP_X_OPT_CONNECT_TIMEOUT:
    1962             :                 {
    1963             :                         int timeout;
    1964             : 
    1965             :                         if (ldap_get_option(ld->link, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
    1966             :                                 RETURN_FALSE;
    1967             :                         }                       
    1968             :                         zval_dtor(retval);
    1969             :                         ZVAL_LONG(retval, (timeout / 1000));
    1970             :                 } break;
    1971             : #endif
    1972             :         /* options with string value */
    1973             :         case LDAP_OPT_ERROR_STRING:
    1974             : #ifdef LDAP_OPT_HOST_NAME
    1975             :         case LDAP_OPT_HOST_NAME:
    1976             : #endif
    1977             : #ifdef HAVE_LDAP_SASL
    1978             :         case LDAP_OPT_X_SASL_MECH:   
    1979             :         case LDAP_OPT_X_SASL_REALM:
    1980             :         case LDAP_OPT_X_SASL_AUTHCID:
    1981             :         case LDAP_OPT_X_SASL_AUTHZID:
    1982             : #endif
    1983             : #ifdef LDAP_OPT_MATCHED_DN
    1984             :         case LDAP_OPT_MATCHED_DN:
    1985             : #endif
    1986             :                 {
    1987           1 :                         char *val = NULL;
    1988             : 
    1989           1 :                         if (ldap_get_option(ld->link, option, &val) || val == NULL || *val == '\0') {
    1990           0 :                                 if (val) {
    1991           0 :                                         ldap_memfree(val);
    1992             :                                 }
    1993           0 :                                 RETURN_FALSE;
    1994             :                         }
    1995           1 :                         zval_dtor(retval);
    1996           1 :                         ZVAL_STRING(retval, val, 1);
    1997           1 :                         ldap_memfree(val);
    1998           1 :                 } break;
    1999             : /* options not implemented
    2000             :         case LDAP_OPT_SERVER_CONTROLS:
    2001             :         case LDAP_OPT_CLIENT_CONTROLS:
    2002             :         case LDAP_OPT_API_INFO:
    2003             :         case LDAP_OPT_API_FEATURE_INFO:
    2004             : */
    2005             :         default:
    2006           4 :                 RETURN_FALSE;
    2007             :         }
    2008          15 :         RETURN_TRUE;
    2009             : }
    2010             : /* }}} */
    2011             : 
    2012             : /* {{{ proto bool ldap_set_option(resource link, int option, mixed newval)
    2013             :    Set the value of various session-wide parameters */
    2014         209 : PHP_FUNCTION(ldap_set_option) 
    2015             : {
    2016             :         zval *link, **newval;
    2017             :         ldap_linkdata *ld;
    2018             :         LDAP *ldap;
    2019             :         long option;
    2020             :         
    2021         209 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zlZ", &link, &option, &newval) != SUCCESS) {
    2022           4 :                 return;
    2023             :         }
    2024             : 
    2025         205 :         if (Z_TYPE_P(link) == IS_NULL) {
    2026           0 :                 ldap = NULL;
    2027             :         } else {
    2028         205 :                 ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2029         205 :                 ldap = ld->link;
    2030             :         }
    2031             : 
    2032         205 :         switch (option) {
    2033             :         /* options with int value */
    2034             :         case LDAP_OPT_DEREF:
    2035             :         case LDAP_OPT_SIZELIMIT:
    2036             :         case LDAP_OPT_TIMELIMIT:
    2037             :         case LDAP_OPT_PROTOCOL_VERSION:
    2038             :         case LDAP_OPT_ERROR_NUMBER:
    2039             : #ifdef LDAP_OPT_DEBUG_LEVEL
    2040             :         case LDAP_OPT_DEBUG_LEVEL:
    2041             : #endif
    2042             :                 {
    2043             :                         int val;
    2044             : 
    2045         186 :                         convert_to_long_ex(newval);
    2046         186 :                         val = Z_LVAL_PP(newval);
    2047         186 :                         if (ldap_set_option(ldap, option, &val)) {
    2048           1 :                                 RETURN_FALSE;
    2049             :                         }
    2050         185 :                 } break;
    2051             : #ifdef LDAP_OPT_NETWORK_TIMEOUT
    2052             :         case LDAP_OPT_NETWORK_TIMEOUT:
    2053             :                 {
    2054             :                         struct timeval timeout;
    2055             : 
    2056           2 :                         convert_to_long_ex(newval);
    2057           2 :                         timeout.tv_sec = Z_LVAL_PP(newval);
    2058           2 :                         timeout.tv_usec = 0;
    2059           2 :                         if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
    2060           0 :                                 RETURN_FALSE;
    2061             :                         }                       
    2062           2 :                 } break;
    2063             : #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
    2064             :         case LDAP_X_OPT_CONNECT_TIMEOUT:
    2065             :                 {
    2066             :                         int timeout;
    2067             : 
    2068             :                         convert_to_long_ex(newval);
    2069             :                         timeout = 1000 * Z_LVAL_PP(newval); /* Convert to milliseconds */
    2070             :                         if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
    2071             :                                 RETURN_FALSE;
    2072             :                         }                       
    2073             :                 } break;
    2074             : #endif
    2075             :                 /* options with string value */
    2076             :         case LDAP_OPT_ERROR_STRING:
    2077             : #ifdef LDAP_OPT_HOST_NAME
    2078             :         case LDAP_OPT_HOST_NAME:
    2079             : #endif
    2080             : #ifdef HAVE_LDAP_SASL
    2081             :         case LDAP_OPT_X_SASL_MECH:   
    2082             :         case LDAP_OPT_X_SASL_REALM:
    2083             :         case LDAP_OPT_X_SASL_AUTHCID:
    2084             :         case LDAP_OPT_X_SASL_AUTHZID:
    2085             : #endif
    2086             : #ifdef LDAP_OPT_MATCHED_DN
    2087             :         case LDAP_OPT_MATCHED_DN:
    2088             : #endif
    2089             :                 {
    2090             :                         char *val;
    2091           1 :                         convert_to_string_ex(newval);
    2092           1 :                         val = Z_STRVAL_PP(newval);
    2093           1 :                         if (ldap_set_option(ldap, option, val)) {
    2094           0 :                                 RETURN_FALSE;
    2095             :                         }
    2096           1 :                 } break;
    2097             :                 /* options with boolean value */
    2098             :         case LDAP_OPT_REFERRALS:
    2099             : #ifdef LDAP_OPT_RESTART
    2100             :         case LDAP_OPT_RESTART:
    2101             : #endif
    2102             :                 {
    2103             :                         void *val;
    2104           4 :                         convert_to_boolean_ex(newval);
    2105           4 :                         val = Z_LVAL_PP(newval)
    2106             :                                 ? LDAP_OPT_ON : LDAP_OPT_OFF;
    2107           4 :                         if (ldap_set_option(ldap, option, val)) {
    2108           0 :                                 RETURN_FALSE;
    2109             :                         }
    2110           4 :                 } break;
    2111             :                 /* options with control list value */
    2112             :         case LDAP_OPT_SERVER_CONTROLS:
    2113             :         case LDAP_OPT_CLIENT_CONTROLS:
    2114             :                 {
    2115             :                         LDAPControl *ctrl, **ctrls, **ctrlp;
    2116             :                         zval **ctrlval, **val;
    2117             :                         int ncontrols;
    2118          11 :                         char error=0;
    2119             : 
    2120          11 :                         if ((Z_TYPE_PP(newval) != IS_ARRAY) || !(ncontrols = zend_hash_num_elements(Z_ARRVAL_PP(newval)))) {
    2121           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected non-empty array value for this option");
    2122           1 :                                 RETURN_FALSE;
    2123             :                         }
    2124          10 :                         ctrls = safe_emalloc((1 + ncontrols), sizeof(*ctrls), 0);
    2125          10 :                         *ctrls = NULL;
    2126          10 :                         ctrlp = ctrls;
    2127          10 :                         zend_hash_internal_pointer_reset(Z_ARRVAL_PP(newval));
    2128          34 :                         while (zend_hash_get_current_data(Z_ARRVAL_PP(newval), (void**)&ctrlval) == SUCCESS) {
    2129          16 :                                 if (Z_TYPE_PP(ctrlval) != IS_ARRAY) {
    2130           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array value must contain only arrays, where each array is a control");
    2131           1 :                                         error = 1;
    2132           1 :                                         break;
    2133             :                                 }
    2134          15 :                                 if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "oid", sizeof("oid"), (void **) &val) != SUCCESS) {
    2135           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Control must have an oid key");
    2136           1 :                                         error = 1;
    2137           1 :                                         break;
    2138             :                                 }
    2139          14 :                                 ctrl = *ctrlp = emalloc(sizeof(**ctrlp));
    2140          14 :                                 convert_to_string_ex(val);
    2141          14 :                                 ctrl->ldctl_oid = Z_STRVAL_PP(val);
    2142          14 :                                 if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "value", sizeof("value"), (void **) &val) == SUCCESS) {
    2143           5 :                                         convert_to_string_ex(val);
    2144           5 :                                         ctrl->ldctl_value.bv_val = Z_STRVAL_PP(val);
    2145           5 :                                         ctrl->ldctl_value.bv_len = Z_STRLEN_PP(val);
    2146             :                                 } else {
    2147           9 :                                         ctrl->ldctl_value.bv_val = NULL;
    2148           9 :                                         ctrl->ldctl_value.bv_len = 0;
    2149             :                                 }
    2150          14 :                                 if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "iscritical", sizeof("iscritical"), (void **) &val) == SUCCESS) {
    2151           5 :                                         convert_to_boolean_ex(val);
    2152           5 :                                         ctrl->ldctl_iscritical = Z_BVAL_PP(val);
    2153             :                                 } else {
    2154           9 :                                         ctrl->ldctl_iscritical = 0;
    2155             :                                 }
    2156             :                                 
    2157          14 :                                 ++ctrlp;
    2158          14 :                                 *ctrlp = NULL;
    2159          14 :                                 zend_hash_move_forward(Z_ARRVAL_PP(newval));
    2160             :                         }
    2161          10 :                         if (!error) {
    2162           8 :                                 error = ldap_set_option(ldap, option, ctrls);
    2163             :                         }
    2164          10 :                         ctrlp = ctrls;
    2165          34 :                         while (*ctrlp) {
    2166          14 :                                 efree(*ctrlp);
    2167          14 :                                 ctrlp++;
    2168             :                         }
    2169          10 :                         efree(ctrls);
    2170          10 :                         if (error) {
    2171           2 :                                 RETURN_FALSE;
    2172             :                         }
    2173           8 :                 } break;
    2174             :         default:
    2175           1 :                 RETURN_FALSE;
    2176             :         }
    2177         200 :         RETURN_TRUE;
    2178             : }
    2179             : /* }}} */
    2180             : 
    2181             : #ifdef HAVE_LDAP_PARSE_RESULT
    2182             : /* {{{ proto bool ldap_parse_result(resource link, resource result, int errcode, string matcheddn, string errmsg, array referrals)
    2183             :    Extract information from result */
    2184           2 : PHP_FUNCTION(ldap_parse_result) 
    2185             : {
    2186             :         zval *link, *result, *errcode, *matcheddn, *errmsg, *referrals;
    2187             :         ldap_linkdata *ld;
    2188             :         LDAPMessage *ldap_result;
    2189             :         char **lreferrals, **refp;
    2190             :         char *lmatcheddn, *lerrmsg;
    2191           2 :         int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
    2192             : 
    2193           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrz|zzz", &link, &result, &errcode, &matcheddn, &errmsg, &referrals) != SUCCESS) {
    2194           1 :                 return; 
    2195             :         }
    2196             : 
    2197           1 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2198           1 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
    2199             : 
    2200           1 :         rc = ldap_parse_result(ld->link, ldap_result, &lerrcode,
    2201             :                                 myargcount > 3 ? &lmatcheddn : NULL,
    2202             :                                 myargcount > 4 ? &lerrmsg : NULL,
    2203             :                                 myargcount > 5 ? &lreferrals : NULL,
    2204             :                                 NULL /* &serverctrls */,
    2205             :                                 0);
    2206           1 :         if (rc != LDAP_SUCCESS) {
    2207           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse result: %s", ldap_err2string(rc));
    2208           0 :                 RETURN_FALSE;
    2209             :         }
    2210             : 
    2211           1 :         zval_dtor(errcode);
    2212           1 :         ZVAL_LONG(errcode, lerrcode);
    2213             : 
    2214             :         /* Reverse -> fall through */
    2215           1 :         switch (myargcount) {
    2216             :                 case 6:
    2217           1 :                         zval_dtor(referrals);
    2218           1 :                         array_init(referrals);
    2219           1 :                         if (lreferrals != NULL) {
    2220           1 :                                 refp = lreferrals;
    2221           3 :                                 while (*refp) {
    2222           1 :                                         add_next_index_string(referrals, *refp, 1);
    2223           1 :                                         refp++;
    2224             :                                 }
    2225           1 :                                 ldap_value_free(lreferrals);
    2226             :                         }
    2227             :                 case 5:
    2228           1 :                         zval_dtor(errmsg);
    2229           1 :                         if (lerrmsg == NULL) {
    2230           1 :                                 ZVAL_EMPTY_STRING(errmsg);
    2231             :                         } else {
    2232           0 :                                 ZVAL_STRING(errmsg, lerrmsg, 1);
    2233           0 :                                 ldap_memfree(lerrmsg);
    2234             :                         }
    2235             :                 case 4: 
    2236           1 :                         zval_dtor(matcheddn);
    2237           1 :                         if (lmatcheddn == NULL) {
    2238           0 :                                 ZVAL_EMPTY_STRING(matcheddn);
    2239             :                         } else {
    2240           1 :                                 ZVAL_STRING(matcheddn, lmatcheddn, 1);
    2241           1 :                                 ldap_memfree(lmatcheddn);
    2242             :                         }
    2243             :         }
    2244           1 :         RETURN_TRUE;
    2245             : }
    2246             : /* }}} */
    2247             : #endif
    2248             : 
    2249             : /* {{{ proto resource ldap_first_reference(resource link, resource result)
    2250             :    Return first reference */
    2251           6 : PHP_FUNCTION(ldap_first_reference)
    2252             : {
    2253             :         zval *link, *result;
    2254             :         ldap_linkdata *ld;
    2255             :         ldap_resultentry *resultentry;
    2256             :         LDAPMessage *ldap_result, *entry;
    2257             : 
    2258           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
    2259           2 :                 return;
    2260             :         }
    2261             : 
    2262           4 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2263           4 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
    2264             : 
    2265           3 :         if ((entry = ldap_first_reference(ld->link, ldap_result)) == NULL) {
    2266           0 :                 RETVAL_FALSE;
    2267             :         } else {
    2268           3 :                 resultentry = emalloc(sizeof(ldap_resultentry));
    2269           3 :                 ZEND_REGISTER_RESOURCE(return_value, resultentry, le_result_entry);
    2270           3 :                 resultentry->id = Z_LVAL_P(result);
    2271           3 :                 zend_list_addref(resultentry->id);
    2272           3 :                 resultentry->data = entry;
    2273           3 :                 resultentry->ber = NULL;
    2274             :         }
    2275             : }
    2276             : /* }}} */
    2277             : 
    2278             : /* {{{ proto resource ldap_next_reference(resource link, resource reference_entry)
    2279             :    Get next reference */
    2280           4 : PHP_FUNCTION(ldap_next_reference)
    2281             : {
    2282             :         zval *link, *result_entry;
    2283             :         ldap_linkdata *ld;
    2284             :         ldap_resultentry *resultentry, *resultentry_next;
    2285             :         LDAPMessage *entry_next;
    2286             : 
    2287           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
    2288           2 :                 return;
    2289             :         }
    2290             : 
    2291           2 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2292           2 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    2293             : 
    2294           1 :         if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
    2295           0 :                 RETVAL_FALSE;
    2296             :         } else {
    2297           1 :                 resultentry_next = emalloc(sizeof(ldap_resultentry));
    2298           1 :                 ZEND_REGISTER_RESOURCE(return_value, resultentry_next, le_result_entry);
    2299           1 :                 resultentry_next->id = resultentry->id;
    2300           1 :                 zend_list_addref(resultentry->id);
    2301           1 :                 resultentry_next->data = entry_next;
    2302           1 :                 resultentry_next->ber = NULL;
    2303             :         }
    2304             : }
    2305             : /* }}} */
    2306             : 
    2307             : #ifdef HAVE_LDAP_PARSE_REFERENCE
    2308             : /* {{{ proto bool ldap_parse_reference(resource link, resource reference_entry, array referrals)
    2309             :    Extract information from reference entry */
    2310           6 : PHP_FUNCTION(ldap_parse_reference)
    2311             : {
    2312             :         zval *link, *result_entry, *referrals;
    2313             :         ldap_linkdata *ld;
    2314             :         ldap_resultentry *resultentry;
    2315             :         char **lreferrals, **refp;
    2316             : 
    2317           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrz", &link, &result_entry, &referrals) != SUCCESS) {
    2318           2 :                 return;
    2319             :         }
    2320             : 
    2321           4 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2322           4 :         ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
    2323             : 
    2324           3 :         if (ldap_parse_reference(ld->link, resultentry->data, &lreferrals, NULL /* &serverctrls */, 0) != LDAP_SUCCESS) {
    2325           0 :                 RETURN_FALSE;
    2326             :         }
    2327             : 
    2328           3 :         zval_dtor(referrals);
    2329           3 :         array_init(referrals);
    2330           3 :         if (lreferrals != NULL) {
    2331           3 :                 refp = lreferrals;
    2332           9 :                 while (*refp) {
    2333           3 :                         add_next_index_string(referrals, *refp, 1);
    2334           3 :                         refp++;
    2335             :                 }
    2336           3 :                 ldap_value_free(lreferrals);
    2337             :         }
    2338           3 :         RETURN_TRUE;
    2339             : }
    2340             : /* }}} */
    2341             : #endif
    2342             : 
    2343             : /* {{{ proto bool ldap_rename(resource link, string dn, string newrdn, string newparent, bool deleteoldrdn);
    2344             :    Modify the name of an entry */
    2345           4 : PHP_FUNCTION(ldap_rename)
    2346             : {
    2347             :         zval *link;
    2348             :         ldap_linkdata *ld;
    2349             :         int rc;
    2350             :         char *dn, *newrdn, *newparent;
    2351             :         int dn_len, newrdn_len, newparent_len;
    2352             :         zend_bool deleteoldrdn;
    2353             :         
    2354           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsssb", &link, &dn, &dn_len, &newrdn, &newrdn_len, &newparent, &newparent_len, &deleteoldrdn) != SUCCESS) {
    2355           1 :                 return;
    2356             :         }
    2357             : 
    2358           3 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2359             : 
    2360           3 :         if (newparent_len == 0) {
    2361           0 :                 newparent = NULL;
    2362             :         }
    2363             : 
    2364             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10
    2365           3 :         rc = ldap_rename_s(ld->link, dn, newrdn, newparent, deleteoldrdn, NULL, NULL);
    2366             : #else
    2367             :         if (newparent_len != 0) {
    2368             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "You are using old LDAP API, newparent must be the empty string, can only modify RDN");
    2369             :                 RETURN_FALSE;
    2370             :         }
    2371             : /* could support old APIs but need check for ldap_modrdn2()/ldap_modrdn() */
    2372             :         rc = ldap_modrdn2_s(ld->link, dn, newrdn, deleteoldrdn);
    2373             : #endif
    2374             : 
    2375           3 :         if (rc == LDAP_SUCCESS) {
    2376           2 :                 RETURN_TRUE;
    2377             :         }
    2378           1 :         RETURN_FALSE;
    2379             : }
    2380             : /* }}} */
    2381             : 
    2382             : #ifdef HAVE_LDAP_START_TLS_S
    2383             : /* {{{ proto bool ldap_start_tls(resource link)
    2384             :    Start TLS */
    2385           3 : PHP_FUNCTION(ldap_start_tls)
    2386             : {
    2387             :         zval *link;
    2388             :         ldap_linkdata *ld;
    2389           3 :         int rc, protocol = LDAP_VERSION3;
    2390             : 
    2391           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
    2392           2 :                 return;
    2393             :         }
    2394             : 
    2395           1 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2396             : 
    2397           2 :         if (((rc = ldap_set_option(ld->link, LDAP_OPT_PROTOCOL_VERSION, &protocol)) != LDAP_SUCCESS) ||
    2398           1 :                 ((rc = ldap_start_tls_s(ld->link, NULL, NULL)) != LDAP_SUCCESS)
    2399             :         ) {
    2400           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Unable to start TLS: %s", ldap_err2string(rc));
    2401           0 :                 RETURN_FALSE;
    2402             :         } else {
    2403           1 :                 RETURN_TRUE;
    2404             :         }
    2405             : }
    2406             : /* }}} */
    2407             : #endif
    2408             : #endif /* (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 */
    2409             : 
    2410             : #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
    2411             : /* {{{ _ldap_rebind_proc()
    2412             : */
    2413           0 : int _ldap_rebind_proc(LDAP *ldap, const char *url, ber_tag_t req, ber_int_t msgid, void *params)
    2414             : {
    2415             :         ldap_linkdata *ld;
    2416             :         int retval;
    2417             :         zval *cb_url;
    2418             :         zval **cb_args[2];
    2419             :         zval *cb_retval;
    2420           0 :         zval *cb_link = (zval *) params;
    2421             :         TSRMLS_FETCH();
    2422             : 
    2423           0 :         ld = (ldap_linkdata *) zend_fetch_resource(&cb_link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
    2424             : 
    2425             :         /* link exists and callback set? */
    2426           0 :         if (ld == NULL || ld->rebindproc == NULL) {
    2427           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link not found or no callback set");
    2428           0 :                 return LDAP_OTHER;
    2429             :         }
    2430             : 
    2431             :         /* callback */
    2432           0 :         MAKE_STD_ZVAL(cb_url);
    2433           0 :         ZVAL_STRING(cb_url, estrdup(url), 0);
    2434           0 :         cb_args[0] = &cb_link;
    2435           0 :         cb_args[1] = &cb_url;
    2436           0 :         if (call_user_function_ex(EG(function_table), NULL, ld->rebindproc, &cb_retval, 2, cb_args, 0, NULL TSRMLS_CC) == SUCCESS && cb_retval) {
    2437           0 :                 convert_to_long_ex(&cb_retval);
    2438           0 :                 retval = Z_LVAL_P(cb_retval);
    2439           0 :                 zval_ptr_dtor(&cb_retval);
    2440             :         } else {
    2441           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "rebind_proc PHP callback failed");
    2442           0 :                 retval = LDAP_OTHER;
    2443             :         }
    2444           0 :         zval_dtor(cb_url);
    2445           0 :         FREE_ZVAL(cb_url);
    2446           0 :         return retval;
    2447             : }
    2448             : /* }}} */
    2449             : 
    2450             : /* {{{ proto bool ldap_set_rebind_proc(resource link, string callback)
    2451             :    Set a callback function to do re-binds on referral chasing. */
    2452           6 : PHP_FUNCTION(ldap_set_rebind_proc)
    2453             : {
    2454             :         zval *link, *callback;
    2455             :         ldap_linkdata *ld;
    2456             :         char *callback_name;
    2457             : 
    2458           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &link, &callback) != SUCCESS) {
    2459           2 :                 RETURN_FALSE;
    2460             :         }
    2461             : 
    2462           4 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2463             : 
    2464           4 :         if (Z_TYPE_P(callback) == IS_STRING && Z_STRLEN_P(callback) == 0) {
    2465             :                 /* unregister rebind procedure */
    2466           1 :                 if (ld->rebindproc != NULL) {
    2467           1 :                         zval_dtor(ld->rebindproc);
    2468           1 :                         FREE_ZVAL(ld->rebindproc);
    2469           1 :                         ld->rebindproc = NULL;
    2470           1 :                         ldap_set_rebind_proc(ld->link, NULL, NULL);
    2471             :                 }
    2472           1 :                 RETURN_TRUE;
    2473             :         }
    2474             : 
    2475             :         /* callable? */
    2476           3 :         if (!zend_is_callable(callback, 0, &callback_name TSRMLS_CC)) {
    2477           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Two arguments expected for '%s' to be a valid callback", callback_name);
    2478           1 :                 efree(callback_name);
    2479           1 :                 RETURN_FALSE;
    2480             :         }
    2481           2 :         efree(callback_name);
    2482             : 
    2483             :         /* register rebind procedure */
    2484           2 :         if (ld->rebindproc == NULL) {
    2485           2 :                 ldap_set_rebind_proc(ld->link, _ldap_rebind_proc, (void *) link);
    2486             :         } else {
    2487           0 :                 zval_dtor(ld->rebindproc);
    2488             :         }
    2489             : 
    2490           2 :         ALLOC_ZVAL(ld->rebindproc);
    2491           2 :         *ld->rebindproc = *callback;
    2492           2 :         zval_copy_ctor(ld->rebindproc);
    2493           2 :         RETURN_TRUE;
    2494             : }
    2495             : /* }}} */
    2496             : #endif
    2497             : 
    2498             : #ifdef STR_TRANSLATION
    2499             : /* {{{ php_ldap_do_translate
    2500             :  */
    2501             : static void php_ldap_do_translate(INTERNAL_FUNCTION_PARAMETERS, int way) 
    2502             : {
    2503             :         char *value;
    2504             :         int result, ldap_len;
    2505             :                 
    2506             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) != SUCCESS) {
    2507             :                 return;
    2508             :         }
    2509             : 
    2510             :         if (value_len == 0) {
    2511             :                 RETURN_FALSE;
    2512             :         }
    2513             : 
    2514             :         if (way == 1) {
    2515             :                 result = ldap_8859_to_t61(&value, &value_len, 0);
    2516             :         } else {
    2517             :                 result = ldap_t61_to_8859(&value, &value_len, 0);
    2518             :         }
    2519             : 
    2520             :         if (result == LDAP_SUCCESS) {
    2521             :                 RETVAL_STRINGL(value, value_len, 1);
    2522             :                 free(value);
    2523             :         } else {
    2524             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Conversion from iso-8859-1 to t61 failed: %s", ldap_err2string(result));
    2525             :                 RETVAL_FALSE;
    2526             :         }
    2527             : }
    2528             : /* }}} */
    2529             : 
    2530             : /* {{{ proto string ldap_t61_to_8859(string value)
    2531             :    Translate t61 characters to 8859 characters */
    2532             : PHP_FUNCTION(ldap_t61_to_8859)
    2533             : {
    2534             :         php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    2535             : }
    2536             : /* }}} */
    2537             : 
    2538             : /* {{{ proto string ldap_8859_to_t61(string value)
    2539             :    Translate 8859 characters to t61 characters */
    2540             : PHP_FUNCTION(ldap_8859_to_t61)
    2541             : {
    2542             :         php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    2543             : }
    2544             : /* }}} */
    2545             : #endif
    2546             : 
    2547             : #ifdef LDAP_CONTROL_PAGEDRESULTS
    2548             : /* {{{ proto mixed ldap_control_paged_result(resource link, int pagesize [, bool iscritical [, string cookie]])
    2549             :    Inject paged results control*/
    2550           4 : PHP_FUNCTION(ldap_control_paged_result) 
    2551             : {
    2552             :         long pagesize;
    2553             :         zend_bool iscritical;
    2554             :         zval *link;
    2555           4 :         char *cookie = NULL;
    2556           4 :         int cookie_len = 0;
    2557           4 :         struct berval lcookie = { 0, NULL };
    2558             :         ldap_linkdata *ld;
    2559             :         LDAP *ldap;
    2560           4 :         BerElement *ber = NULL;
    2561             :         LDAPControl     ctrl, *ctrlsp[2];
    2562           4 :         int rc, myargcount = ZEND_NUM_ARGS();
    2563             : 
    2564           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|bs", &link, &pagesize, &iscritical, &cookie, &cookie_len) != SUCCESS) {
    2565           0 :                 return;
    2566             :         }
    2567             : 
    2568           4 :         if (Z_TYPE_P(link) == IS_NULL) {
    2569           0 :                 ldap = NULL;
    2570             :         } else {
    2571           4 :                 ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2572           4 :                 ldap = ld->link;
    2573             :         }
    2574             : 
    2575           4 :         ber = ber_alloc_t(LBER_USE_DER);
    2576           4 :         if (ber == NULL) {
    2577           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to alloc BER encoding resources for paged results control");
    2578           0 :                 RETURN_FALSE;
    2579             :         }
    2580             : 
    2581           4 :         ctrl.ldctl_iscritical = 0;
    2582             : 
    2583           4 :         switch (myargcount) {
    2584             :                 case 4:
    2585           2 :                         lcookie.bv_val = cookie;
    2586           2 :                         lcookie.bv_len = cookie_len;
    2587             :                         /* fallthru */
    2588             :                 case 3:
    2589           2 :                         ctrl.ldctl_iscritical = (int)iscritical;
    2590             :                         /* fallthru */
    2591             :         }
    2592             : 
    2593           4 :         if (ber_printf(ber, "{iO}", (int)pagesize, &lcookie) == LBER_ERROR) {
    2594           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to BER printf paged results control");
    2595           0 :                 RETVAL_FALSE;
    2596           0 :                 goto lcpr_error_out;
    2597             :         }
    2598           4 :         rc = ber_flatten2(ber, &ctrl.ldctl_value, 0);
    2599           4 :         if (rc == LBER_ERROR) {
    2600           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to BER encode paged results control");
    2601           0 :                 RETVAL_FALSE;
    2602           0 :                 goto lcpr_error_out;
    2603             :         }
    2604             : 
    2605           4 :         ctrl.ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
    2606             : 
    2607           4 :         if (ldap) {
    2608             :                 /* directly set the option */
    2609           4 :                 ctrlsp[0] = &ctrl;
    2610           4 :                 ctrlsp[1] = NULL;
    2611             : 
    2612           4 :                 rc = ldap_set_option(ldap, LDAP_OPT_SERVER_CONTROLS, ctrlsp);
    2613           4 :                 if (rc != LDAP_SUCCESS) {
    2614           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set paged results control: %s (%d)", ldap_err2string(rc), rc);
    2615           0 :                         RETVAL_FALSE;
    2616           0 :                         goto lcpr_error_out;
    2617             :                 }
    2618           4 :                 RETVAL_TRUE;
    2619             :         } else {
    2620             :                 /* return a PHP control object */
    2621           0 :                 array_init(return_value);
    2622             : 
    2623           0 :                 add_assoc_string(return_value, "oid", ctrl.ldctl_oid, 1);
    2624           0 :                 if (ctrl.ldctl_value.bv_len) {
    2625           0 :                         add_assoc_stringl(return_value, "value", ctrl.ldctl_value.bv_val, ctrl.ldctl_value.bv_len, 1);
    2626             :                 }
    2627           0 :                 if (ctrl.ldctl_iscritical) {
    2628           0 :                         add_assoc_bool(return_value, "iscritical", ctrl.ldctl_iscritical);
    2629             :                 }
    2630             :         }
    2631             : 
    2632             : lcpr_error_out:
    2633           4 :         if (ber != NULL) {
    2634           4 :                 ber_free(ber, 1);
    2635             :         }
    2636           4 :         return;
    2637             : }
    2638             : /* }}} */
    2639             : 
    2640             : /* {{{ proto bool ldap_control_paged_result_response(resource link, resource result [, string &cookie [, int &estimated]])
    2641             :    Extract paged results control response */
    2642           1 : PHP_FUNCTION(ldap_control_paged_result_response) 
    2643             : {
    2644             :         zval *link, *result, *cookie, *estimated;
    2645             :         struct berval lcookie;
    2646             :         int lestimated;
    2647             :         ldap_linkdata *ld;
    2648             :         LDAPMessage *ldap_result;
    2649             :         LDAPControl **lserverctrls, *lctrl;
    2650             :         BerElement *ber;
    2651             :         ber_tag_t tag;
    2652           1 :         int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
    2653             : 
    2654           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|zz", &link, &result, &cookie, &estimated) != SUCCESS) {
    2655           0 :                 return;
    2656             :         }
    2657             : 
    2658           1 :         ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
    2659           1 :         ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
    2660             : 
    2661           1 :         rc = ldap_parse_result(ld->link,
    2662             :                                 ldap_result,
    2663             :                                 &lerrcode,
    2664             :                                 NULL,           /* matcheddn */
    2665             :                                 NULL,           /* errmsg */
    2666             :                                 NULL,           /* referrals */
    2667             :                                 &lserverctrls,
    2668             :                                 0);
    2669             : 
    2670           1 :         if (rc != LDAP_SUCCESS) {
    2671           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse result: %s (%d)", ldap_err2string(rc), rc);
    2672           0 :                 RETURN_FALSE;
    2673             :         }
    2674             : 
    2675           1 :         if (lerrcode != LDAP_SUCCESS) {
    2676           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Result is: %s (%d)", ldap_err2string(lerrcode), lerrcode);
    2677           0 :                 RETURN_FALSE;
    2678             :         }
    2679             : 
    2680           1 :         if (lserverctrls == NULL) {
    2681           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No server controls in result");
    2682           0 :                 RETURN_FALSE;
    2683             :         }
    2684             : 
    2685           1 :         lctrl = ldap_find_control(LDAP_CONTROL_PAGEDRESULTS, lserverctrls);
    2686           1 :         if (lctrl == NULL) {
    2687           0 :                 ldap_controls_free(lserverctrls);
    2688           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No paged results control response in result");
    2689           0 :                 RETURN_FALSE;
    2690             :         }
    2691             : 
    2692           1 :         ber = ber_init(&lctrl->ldctl_value);
    2693           1 :         if (ber == NULL) {
    2694           0 :                 ldap_controls_free(lserverctrls);
    2695           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to alloc BER decoding resources for paged results control response");
    2696           0 :                 RETURN_FALSE;
    2697             :         }
    2698             : 
    2699           1 :         tag = ber_scanf(ber, "{io}", &lestimated, &lcookie);
    2700           1 :         (void)ber_free(ber, 1);
    2701             : 
    2702           1 :         if (tag == LBER_ERROR) {
    2703           0 :                 ldap_controls_free(lserverctrls);
    2704           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to decode paged results control response");
    2705           0 :                 RETURN_FALSE;
    2706             :         }
    2707             : 
    2708           1 :         if (lestimated < 0) {
    2709           0 :                 ldap_controls_free(lserverctrls);
    2710           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid paged results control response value");
    2711           0 :                 RETURN_FALSE;
    2712             :         }
    2713             : 
    2714           1 :         ldap_controls_free(lserverctrls);
    2715           1 :         if (myargcount == 4) {
    2716           0 :                 zval_dtor(estimated);
    2717           0 :                 ZVAL_LONG(estimated, lestimated);
    2718             :         }
    2719             : 
    2720           1 :         zval_dtor(cookie);
    2721           1 :         if (lcookie.bv_len == 0) {
    2722           0 :                 ZVAL_EMPTY_STRING(cookie);
    2723             :         } else {
    2724           1 :                 ZVAL_STRINGL(cookie, lcookie.bv_val, lcookie.bv_len, 1);
    2725             :         }
    2726           1 :         ldap_memfree(lcookie.bv_val);
    2727             : 
    2728           1 :         RETURN_TRUE;
    2729             : }
    2730             : /* }}} */
    2731             : #endif
    2732             : 
    2733             : /* {{{ arginfo */
    2734             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_connect, 0, 0, 0)
    2735             :         ZEND_ARG_INFO(0, hostname)
    2736             :         ZEND_ARG_INFO(0, port)
    2737             : #ifdef HAVE_ORALDAP
    2738             :         ZEND_ARG_INFO(0, wallet)
    2739             :         ZEND_ARG_INFO(0, wallet_passwd)
    2740             :         ZEND_ARG_INFO(0, authmode)
    2741             : #endif
    2742             : ZEND_END_ARG_INFO()
    2743             : 
    2744             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_resource, 0, 0, 1)
    2745             :         ZEND_ARG_INFO(0, link_identifier)
    2746             : ZEND_END_ARG_INFO()
    2747             : 
    2748             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_bind, 0, 0, 1)
    2749             :         ZEND_ARG_INFO(0, link_identifier)
    2750             :         ZEND_ARG_INFO(0, bind_rdn)
    2751             :         ZEND_ARG_INFO(0, bind_password)
    2752             : ZEND_END_ARG_INFO()
    2753             : 
    2754             : #ifdef HAVE_LDAP_SASL
    2755             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sasl_bind, 0, 0, 1)
    2756             :         ZEND_ARG_INFO(0, link)
    2757             :         ZEND_ARG_INFO(0, binddn)
    2758             :         ZEND_ARG_INFO(0, password)
    2759             :         ZEND_ARG_INFO(0, sasl_mech)
    2760             :         ZEND_ARG_INFO(0, sasl_realm)
    2761             :         ZEND_ARG_INFO(0, sasl_authz_id)
    2762             :         ZEND_ARG_INFO(0, props)
    2763             : ZEND_END_ARG_INFO()
    2764             : #endif
    2765             : 
    2766             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_read, 0, 0, 3)
    2767             :         ZEND_ARG_INFO(0, link_identifier)
    2768             :         ZEND_ARG_INFO(0, base_dn)
    2769             :         ZEND_ARG_INFO(0, filter)
    2770             :         ZEND_ARG_INFO(0, attributes)
    2771             :         ZEND_ARG_INFO(0, attrsonly)
    2772             :         ZEND_ARG_INFO(0, sizelimit)
    2773             :         ZEND_ARG_INFO(0, timelimit)
    2774             :         ZEND_ARG_INFO(0, deref)
    2775             : ZEND_END_ARG_INFO()
    2776             : 
    2777             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_list, 0, 0, 3)
    2778             :         ZEND_ARG_INFO(0, link_identifier)
    2779             :         ZEND_ARG_INFO(0, base_dn)
    2780             :         ZEND_ARG_INFO(0, filter)
    2781             :         ZEND_ARG_INFO(0, attributes)
    2782             :         ZEND_ARG_INFO(0, attrsonly)
    2783             :         ZEND_ARG_INFO(0, sizelimit)
    2784             :         ZEND_ARG_INFO(0, timelimit)
    2785             :         ZEND_ARG_INFO(0, deref)
    2786             : ZEND_END_ARG_INFO()
    2787             : 
    2788             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_search, 0, 0, 3)
    2789             :         ZEND_ARG_INFO(0, link_identifier)
    2790             :         ZEND_ARG_INFO(0, base_dn)
    2791             :         ZEND_ARG_INFO(0, filter)
    2792             :         ZEND_ARG_INFO(0, attributes)
    2793             :         ZEND_ARG_INFO(0, attrsonly)
    2794             :         ZEND_ARG_INFO(0, sizelimit)
    2795             :         ZEND_ARG_INFO(0, timelimit)
    2796             :         ZEND_ARG_INFO(0, deref)
    2797             : ZEND_END_ARG_INFO()
    2798             : 
    2799             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_count_entries, 0, 0, 2)
    2800             :         ZEND_ARG_INFO(0, link_identifier)
    2801             :         ZEND_ARG_INFO(0, result_identifier)
    2802             : ZEND_END_ARG_INFO()
    2803             : 
    2804             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_entry, 0, 0, 2)
    2805             :         ZEND_ARG_INFO(0, link_identifier)
    2806             :         ZEND_ARG_INFO(0, result_identifier)
    2807             : ZEND_END_ARG_INFO()
    2808             : 
    2809             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_entry, 0, 0, 2)
    2810             :         ZEND_ARG_INFO(0, link_identifier)
    2811             :         ZEND_ARG_INFO(0, result_identifier)
    2812             : ZEND_END_ARG_INFO()
    2813             : 
    2814             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_entries, 0, 0, 2)
    2815             :         ZEND_ARG_INFO(0, link_identifier)
    2816             :         ZEND_ARG_INFO(0, result_identifier)
    2817             : ZEND_END_ARG_INFO()
    2818             : 
    2819             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_attribute, 0, 0, 2)
    2820             :         ZEND_ARG_INFO(0, link_identifier)
    2821             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2822             : ZEND_END_ARG_INFO()
    2823             : 
    2824             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_attribute, 0, 0, 2)
    2825             :         ZEND_ARG_INFO(0, link_identifier)
    2826             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2827             : ZEND_END_ARG_INFO()
    2828             : 
    2829             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_attributes, 0, 0, 2)
    2830             :         ZEND_ARG_INFO(0, link_identifier)
    2831             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2832             : ZEND_END_ARG_INFO()
    2833             : 
    2834             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values, 0, 0, 3)
    2835             :         ZEND_ARG_INFO(0, link_identifier)
    2836             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2837             :         ZEND_ARG_INFO(0, attribute)
    2838             : ZEND_END_ARG_INFO()
    2839             : 
    2840             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values_len, 0, 0, 3)
    2841             :         ZEND_ARG_INFO(0, link_identifier)
    2842             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2843             :         ZEND_ARG_INFO(0, attribute)
    2844             : ZEND_END_ARG_INFO()
    2845             : 
    2846             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_dn, 0, 0, 2)
    2847             :         ZEND_ARG_INFO(0, link_identifier)
    2848             :         ZEND_ARG_INFO(0, result_entry_identifier)
    2849             : ZEND_END_ARG_INFO()
    2850             : 
    2851             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_explode_dn, 0, 0, 2)
    2852             :         ZEND_ARG_INFO(0, dn)
    2853             :         ZEND_ARG_INFO(0, with_attrib)
    2854             : ZEND_END_ARG_INFO()
    2855             : 
    2856             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_dn2ufn, 0, 0, 1)
    2857             :         ZEND_ARG_INFO(0, dn)
    2858             : ZEND_END_ARG_INFO()
    2859             : 
    2860             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_add, 0, 0, 3)
    2861             :         ZEND_ARG_INFO(0, link_identifier)
    2862             :         ZEND_ARG_INFO(0, dn)
    2863             :         ZEND_ARG_INFO(0, entry)
    2864             : ZEND_END_ARG_INFO()
    2865             : 
    2866             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_delete, 0, 0, 2)
    2867             :         ZEND_ARG_INFO(0, link_identifier)
    2868             :         ZEND_ARG_INFO(0, dn)
    2869             : ZEND_END_ARG_INFO()
    2870             : 
    2871             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify, 0, 0, 3)
    2872             :         ZEND_ARG_INFO(0, link_identifier)
    2873             :         ZEND_ARG_INFO(0, dn)
    2874             :         ZEND_ARG_INFO(0, entry)
    2875             : ZEND_END_ARG_INFO()
    2876             : 
    2877             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify_batch, 0, 0, 3)
    2878             :         ZEND_ARG_INFO(0, link_identifier)
    2879             :         ZEND_ARG_INFO(0, dn)
    2880             :         ZEND_ARG_ARRAY_INFO(0, modifications_info, 0)
    2881             : ZEND_END_ARG_INFO()
    2882             : 
    2883             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_add, 0, 0, 3)
    2884             :         ZEND_ARG_INFO(0, link_identifier)
    2885             :         ZEND_ARG_INFO(0, dn)
    2886             :         ZEND_ARG_INFO(0, entry)
    2887             : ZEND_END_ARG_INFO()
    2888             : 
    2889             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_replace, 0, 0, 3)
    2890             :         ZEND_ARG_INFO(0, link_identifier)
    2891             :         ZEND_ARG_INFO(0, dn)
    2892             :         ZEND_ARG_INFO(0, entry)
    2893             : ZEND_END_ARG_INFO()
    2894             : 
    2895             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_del, 0, 0, 3)
    2896             :         ZEND_ARG_INFO(0, link_identifier)
    2897             :         ZEND_ARG_INFO(0, dn)
    2898             :         ZEND_ARG_INFO(0, entry)
    2899             : ZEND_END_ARG_INFO()
    2900             : 
    2901             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_err2str, 0, 0, 1)
    2902             :         ZEND_ARG_INFO(0, errno)
    2903             : ZEND_END_ARG_INFO()
    2904             : 
    2905             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_compare, 0, 0, 4)
    2906             :         ZEND_ARG_INFO(0, link_identifier)
    2907             :         ZEND_ARG_INFO(0, dn)
    2908             :         ZEND_ARG_INFO(0, attribute)
    2909             :         ZEND_ARG_INFO(0, value)
    2910             : ZEND_END_ARG_INFO()
    2911             : 
    2912             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sort, 0, 0, 3)
    2913             :         ZEND_ARG_INFO(0, link)
    2914             :         ZEND_ARG_INFO(0, result)
    2915             :         ZEND_ARG_INFO(0, sortfilter)
    2916             : ZEND_END_ARG_INFO()
    2917             : 
    2918             : #ifdef LDAP_CONTROL_PAGEDRESULTS
    2919             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result, 0, 0, 2)
    2920             :         ZEND_ARG_INFO(0, link)
    2921             :         ZEND_ARG_INFO(0, pagesize)
    2922             :         ZEND_ARG_INFO(0, iscritical)
    2923             :         ZEND_ARG_INFO(0, cookie)
    2924             : ZEND_END_ARG_INFO();
    2925             : 
    2926             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result_response, 0, 0, 2)
    2927             :         ZEND_ARG_INFO(0, link)
    2928             :         ZEND_ARG_INFO(0, result)
    2929             :         ZEND_ARG_INFO(1, cookie)
    2930             :         ZEND_ARG_INFO(1, estimated)
    2931             : ZEND_END_ARG_INFO();
    2932             : #endif
    2933             : 
    2934             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10
    2935             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_rename, 0, 0, 5)
    2936             :         ZEND_ARG_INFO(0, link_identifier)
    2937             :         ZEND_ARG_INFO(0, dn)
    2938             :         ZEND_ARG_INFO(0, newrdn)
    2939             :         ZEND_ARG_INFO(0, newparent)
    2940             :         ZEND_ARG_INFO(0, deleteoldrdn)
    2941             : ZEND_END_ARG_INFO()
    2942             : 
    2943             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_option, 0, 0, 3)
    2944             :         ZEND_ARG_INFO(0, link_identifier)
    2945             :         ZEND_ARG_INFO(0, option)
    2946             :         ZEND_ARG_INFO(1, retval)
    2947             : ZEND_END_ARG_INFO()
    2948             : 
    2949             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_option, 0, 0, 3)
    2950             :         ZEND_ARG_INFO(0, link_identifier)
    2951             :         ZEND_ARG_INFO(0, option)
    2952             :         ZEND_ARG_INFO(0, newval)
    2953             : ZEND_END_ARG_INFO()
    2954             : 
    2955             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_reference, 0, 0, 2)
    2956             :         ZEND_ARG_INFO(0, link)
    2957             :         ZEND_ARG_INFO(0, result)
    2958             : ZEND_END_ARG_INFO()
    2959             : 
    2960             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_reference, 0, 0, 2)
    2961             :         ZEND_ARG_INFO(0, link)
    2962             :         ZEND_ARG_INFO(0, entry)
    2963             : ZEND_END_ARG_INFO()
    2964             : 
    2965             : #ifdef HAVE_LDAP_PARSE_REFERENCE
    2966             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_reference, 0, 0, 3)
    2967             :         ZEND_ARG_INFO(0, link)
    2968             :         ZEND_ARG_INFO(0, entry)
    2969             :         ZEND_ARG_INFO(1, referrals)
    2970             : ZEND_END_ARG_INFO()
    2971             : #endif
    2972             : 
    2973             : 
    2974             : #ifdef HAVE_LDAP_PARSE_RESULT
    2975             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_result, 0, 0, 3)
    2976             :         ZEND_ARG_INFO(0, link)
    2977             :         ZEND_ARG_INFO(0, result)
    2978             :         ZEND_ARG_INFO(1, errcode)
    2979             :         ZEND_ARG_INFO(1, matcheddn)
    2980             :         ZEND_ARG_INFO(1, errmsg)
    2981             :         ZEND_ARG_INFO(1, referrals)
    2982             : ZEND_END_ARG_INFO()
    2983             : #endif
    2984             : #endif
    2985             : 
    2986             : #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
    2987             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_rebind_proc, 0, 0, 2)
    2988             :         ZEND_ARG_INFO(0, link)
    2989             :         ZEND_ARG_INFO(0, callback)
    2990             : ZEND_END_ARG_INFO()
    2991             : #endif
    2992             : 
    2993             : #ifdef STR_TRANSLATION
    2994             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_t61_to_8859, 0, 0, 1)
    2995             :         ZEND_ARG_INFO(0, value)
    2996             : ZEND_END_ARG_INFO()
    2997             : 
    2998             : ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_8859_to_t61, 0, 0, 1)
    2999             :         ZEND_ARG_INFO(0, value)
    3000             : ZEND_END_ARG_INFO()
    3001             : #endif
    3002             : /* }}} */
    3003             :         
    3004             : /*
    3005             :         This is just a small subset of the functionality provided by the LDAP library. All the 
    3006             :         operations are synchronous. Referrals are not handled automatically.
    3007             : */
    3008             : /* {{{ ldap_functions[]
    3009             :  */
    3010             : const zend_function_entry ldap_functions[] = {
    3011             :         PHP_FE(ldap_connect,                                                            arginfo_ldap_connect)
    3012             :         PHP_FALIAS(ldap_close,          ldap_unbind,                    arginfo_ldap_resource)
    3013             :         PHP_FE(ldap_bind,                                                                       arginfo_ldap_bind)
    3014             : #ifdef HAVE_LDAP_SASL
    3015             :         PHP_FE(ldap_sasl_bind,                                                          arginfo_ldap_sasl_bind)
    3016             : #endif
    3017             :         PHP_FE(ldap_unbind,                                                                     arginfo_ldap_resource)
    3018             :         PHP_FE(ldap_read,                                                                       arginfo_ldap_read)
    3019             :         PHP_FE(ldap_list,                                                                       arginfo_ldap_list)
    3020             :         PHP_FE(ldap_search,                                                                     arginfo_ldap_search)
    3021             :         PHP_FE(ldap_free_result,                                                        arginfo_ldap_resource)
    3022             :         PHP_FE(ldap_count_entries,                                                      arginfo_ldap_count_entries)
    3023             :         PHP_FE(ldap_first_entry,                                                        arginfo_ldap_first_entry)
    3024             :         PHP_FE(ldap_next_entry,                                                         arginfo_ldap_next_entry)
    3025             :         PHP_FE(ldap_get_entries,                                                        arginfo_ldap_get_entries)
    3026             :         PHP_FE(ldap_first_attribute,                                            arginfo_ldap_first_attribute)
    3027             :         PHP_FE(ldap_next_attribute,                                                     arginfo_ldap_next_attribute)
    3028             :         PHP_FE(ldap_get_attributes,                                                     arginfo_ldap_get_attributes)
    3029             :         PHP_FALIAS(ldap_get_values,     ldap_get_values_len,    arginfo_ldap_get_values)
    3030             :         PHP_FE(ldap_get_values_len,                                                     arginfo_ldap_get_values_len)
    3031             :         PHP_FE(ldap_get_dn,                                                                     arginfo_ldap_get_dn)
    3032             :         PHP_FE(ldap_explode_dn,                                                         arginfo_ldap_explode_dn)
    3033             :         PHP_FE(ldap_dn2ufn,                                                                     arginfo_ldap_dn2ufn)
    3034             :         PHP_FE(ldap_add,                                                                        arginfo_ldap_add)
    3035             :         PHP_FE(ldap_delete,                                                                     arginfo_ldap_delete)
    3036             :         PHP_FE(ldap_modify_batch,                                                       arginfo_ldap_modify_batch)
    3037             :         PHP_FALIAS(ldap_modify,         ldap_mod_replace,               arginfo_ldap_modify)
    3038             : 
    3039             : /* additional functions for attribute based modifications, Gerrit Thomson */
    3040             :         PHP_FE(ldap_mod_add,                                                            arginfo_ldap_mod_add)
    3041             :         PHP_FE(ldap_mod_replace,                                                        arginfo_ldap_mod_replace)
    3042             :         PHP_FE(ldap_mod_del,                                                            arginfo_ldap_mod_del)
    3043             : /* end gjt mod */
    3044             : 
    3045             :         PHP_FE(ldap_errno,                                                                      arginfo_ldap_resource)
    3046             :         PHP_FE(ldap_err2str,                                                            arginfo_ldap_err2str)
    3047             :         PHP_FE(ldap_error,                                                                      arginfo_ldap_resource)
    3048             :         PHP_FE(ldap_compare,                                                            arginfo_ldap_compare)
    3049             :         PHP_FE(ldap_sort,                                                                       arginfo_ldap_sort)
    3050             : 
    3051             : #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10
    3052             :         PHP_FE(ldap_rename,                                                                     arginfo_ldap_rename)
    3053             :         PHP_FE(ldap_get_option,                                                         arginfo_ldap_get_option)
    3054             :         PHP_FE(ldap_set_option,                                                         arginfo_ldap_set_option)
    3055             :         PHP_FE(ldap_first_reference,                                            arginfo_ldap_first_reference)
    3056             :         PHP_FE(ldap_next_reference,                                                     arginfo_ldap_next_reference)
    3057             : #ifdef HAVE_LDAP_PARSE_REFERENCE
    3058             :         PHP_FE(ldap_parse_reference,                                            arginfo_ldap_parse_reference)
    3059             : #endif
    3060             : #ifdef HAVE_LDAP_PARSE_RESULT
    3061             :         PHP_FE(ldap_parse_result,                                                       arginfo_ldap_parse_result)
    3062             : #endif
    3063             : #ifdef HAVE_LDAP_START_TLS_S
    3064             :         PHP_FE(ldap_start_tls,                                                          arginfo_ldap_resource)
    3065             : #endif
    3066             : #endif
    3067             : 
    3068             : #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
    3069             :         PHP_FE(ldap_set_rebind_proc,                                            arginfo_ldap_set_rebind_proc)
    3070             : #endif
    3071             : 
    3072             : #ifdef STR_TRANSLATION
    3073             :         PHP_FE(ldap_t61_to_8859,                                                        arginfo_ldap_t61_to_8859)
    3074             :         PHP_FE(ldap_8859_to_t61,                                                        arginfo_ldap_8859_to_t61)
    3075             : #endif
    3076             : 
    3077             : #ifdef LDAP_CONTROL_PAGEDRESULTS
    3078             :         PHP_FE(ldap_control_paged_result,                                                       arginfo_ldap_control_paged_result)
    3079             :         PHP_FE(ldap_control_paged_result_response,              arginfo_ldap_control_paged_result_response)
    3080             : #endif
    3081             :         PHP_FE_END
    3082             : };
    3083             : /* }}} */
    3084             : 
    3085             : zend_module_entry ldap_module_entry = { /* {{{ */
    3086             :         STANDARD_MODULE_HEADER,
    3087             :         "ldap", 
    3088             :         ldap_functions, 
    3089             :         PHP_MINIT(ldap), 
    3090             :         PHP_MSHUTDOWN(ldap), 
    3091             :         NULL, 
    3092             :         NULL,
    3093             :         PHP_MINFO(ldap), 
    3094             :         NO_VERSION_YET,
    3095             :         PHP_MODULE_GLOBALS(ldap),
    3096             :         PHP_GINIT(ldap),
    3097             :         NULL,
    3098             :         NULL,
    3099             :         STANDARD_MODULE_PROPERTIES_EX
    3100             : };
    3101             : /* }}} */
    3102             : 
    3103             : /*
    3104             :  * Local variables:
    3105             :  * tab-width: 4
    3106             :  * c-basic-offset: 4
    3107             :  * End:
    3108             :  * vim600: sw=4 ts=4 fdm=marker
    3109             :  * vim<600: sw=4 ts=4
    3110             :  */

Generated by: LCOV version 1.10

Generated at Thu, 10 Apr 2014 08:53:51 +0000 (7 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.