1999-04-22 08:45:14 +08:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2001-12-11 23:32:16 +08:00
| PHP Version 4 |
1999-04-22 08:45:14 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2003-01-01 00:08:15 +08:00
| Copyright ( c ) 1997 - 2003 The PHP Group |
1999-04-22 08:45:14 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2003-06-11 04:04:29 +08:00
| This source file is subject to version 3.0 of the PHP license , |
1999-07-16 21:13:16 +08:00
| that is bundled with this package in the file LICENSE , and is |
2003-06-11 04:04:29 +08:00
| available through the world - wide - web at the following url : |
| http : //www.php.net/license/3_0.txt. |
1999-07-16 21:13:16 +08:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
1999-04-22 08:45:14 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Authors : Amitay Isaacs < amitay @ w - o - i . com > |
| Eric Warnke < ericw @ albany . edu > |
2002-02-28 16:29:35 +08:00
| Rasmus Lerdorf < rasmus @ php . net > |
1999-05-21 18:06:25 +08:00
| Gerrit Thomson < 334647 @ swin . edu . au > |
2000-07-25 23:34:28 +08:00
| Jani Taskinen < sniper @ iki . fi > |
2000-08-27 00:20:37 +08:00
| Stig Venaas < venaas @ uninett . no > |
2000-04-02 00:23:39 +08:00
| PHP 4.0 updates : Zeev Suraski < zeev @ zend . com > |
1999-04-22 08:45:14 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
# define IS_EXT_MODULE
2001-05-24 18:07:29 +08:00
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
2002-09-05 19:22:20 +08:00
/* Additional headers for NetWare */
# if defined(NETWARE) && (NEW_LIBC)
# include <sys/select.h>
# include <sys/timeval.h>
# endif
1999-04-22 08:45:14 +08:00
# include "php.h"
2000-04-02 00:23:39 +08:00
# include "php_ini.h"
1999-04-22 08:45:14 +08:00
2002-11-05 22:18:20 +08:00
# include <stddef.h>
1999-04-23 11:28:47 +08:00
# include "ext/standard/dl.h"
1999-12-05 03:19:57 +08:00
# include "php_ldap.h"
1999-04-22 08:45:14 +08:00
2000-02-16 17:33:19 +08:00
# ifdef PHP_WIN32
1999-04-22 08:45:14 +08:00
# include <string.h>
# if HAVE_NSLDAP
2003-02-16 11:48:49 +08:00
# include <winsock2.h>
1999-04-22 08:45:14 +08:00
# endif
# define strdup _strdup
# undef WINDOWS
# undef strcasecmp
# undef strncasecmp
# define WINSOCK 1
# define __STDC__ 1
# endif
1999-12-05 03:19:57 +08:00
# include "ext/standard/php_string.h"
2000-04-07 00:57:33 +08:00
# include "ext/standard/info.h"
1999-04-22 08:45:14 +08:00
2003-06-13 21:30:46 +08:00
# ifdef HAVE_LDAP_SASL_H
# include <sasl.h>
# elif defined(HAVE_LDAP_SASL_SASL_H)
# include <sasl/sasl.h>
# endif
2001-11-30 04:17:17 +08:00
typedef struct {
LDAP * link ;
2001-12-24 00:32:06 +08:00
# if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
2001-11-30 04:26:20 +08:00
zval * rebindproc ;
# endif
2001-11-30 04:17:17 +08:00
} ldap_linkdata ;
2002-05-04 22:27:48 +08:00
typedef struct {
LDAPMessage * data ;
int id ;
} ldap_resultentry ;
2000-04-02 00:23:39 +08:00
ZEND_DECLARE_MODULE_GLOBALS ( ldap )
1999-04-22 08:45:14 +08:00
2003-08-04 01:44:39 +08:00
static
ZEND_BEGIN_ARG_INFO ( arg3to6of6_force_ref , 0 )
ZEND_ARG_PASS_INFO ( 0 )
ZEND_ARG_PASS_INFO ( 0 )
2003-08-05 19:54:01 +08:00
ZEND_ARG_PASS_INFO ( 1 )
2003-08-04 01:44:39 +08:00
ZEND_ARG_PASS_INFO ( 1 )
ZEND_ARG_PASS_INFO ( 1 )
ZEND_ARG_PASS_INFO ( 1 )
ZEND_END_ARG_INFO ( ) ;
2001-01-07 21:03:11 +08:00
2001-06-01 05:09:30 +08:00
static int le_link , le_result , le_result_entry , le_ber_entry ;
1999-04-22 08:45:14 +08:00
/*
This is just a small subset of the functionality provided by the LDAP library . All the
operations are synchronous . Referrals are not handled automatically .
*/
2001-06-05 21:12:10 +08:00
/* {{{ ldap_functions[]
*/
1999-04-22 08:45:14 +08:00
function_entry ldap_functions [ ] = {
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_connect , NULL )
PHP_FALIAS ( ldap_close , ldap_unbind , NULL )
PHP_FE ( ldap_bind , NULL )
2003-06-13 21:30:46 +08:00
# ifdef HAVE_LDAP_SASL
2003-06-09 07:44:01 +08:00
PHP_FE ( ldap_sasl_bind , NULL )
# endif
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_unbind , NULL )
PHP_FE ( ldap_read , NULL )
PHP_FE ( ldap_list , NULL )
PHP_FE ( ldap_search , NULL )
PHP_FE ( ldap_free_result , NULL )
PHP_FE ( ldap_count_entries , NULL )
PHP_FE ( ldap_first_entry , NULL )
PHP_FE ( ldap_next_entry , NULL )
PHP_FE ( ldap_get_entries , NULL )
2003-08-04 01:44:39 +08:00
PHP_FE ( ldap_first_attribute , third_arg_force_ref )
PHP_FE ( ldap_next_attribute , third_arg_force_ref )
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_get_attributes , NULL )
PHP_FE ( ldap_get_values , NULL )
PHP_FE ( ldap_get_values_len , NULL )
PHP_FE ( ldap_get_dn , NULL )
PHP_FE ( ldap_explode_dn , NULL )
PHP_FE ( ldap_dn2ufn , NULL )
PHP_FE ( ldap_add , NULL )
PHP_FE ( ldap_delete , NULL )
2002-01-05 04:56:03 +08:00
PHP_FALIAS ( ldap_modify , ldap_mod_replace , NULL )
2001-06-01 05:09:30 +08:00
1999-05-21 18:06:25 +08:00
/* additional functions for attribute based modifications, Gerrit Thomson */
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_mod_add , NULL )
PHP_FE ( ldap_mod_replace , NULL )
PHP_FE ( ldap_mod_del , NULL )
1999-05-21 18:06:25 +08:00
/* end gjt mod */
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_errno , NULL )
PHP_FE ( ldap_err2str , NULL )
PHP_FE ( ldap_error , NULL )
PHP_FE ( ldap_compare , NULL )
2001-11-22 04:14:17 +08:00
PHP_FE ( ldap_sort , NULL )
2000-08-02 21:11:46 +08:00
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP
2003-08-04 01:44:39 +08:00
PHP_FE ( ldap_get_option , third_arg_force_ref )
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_set_option , NULL )
PHP_FE ( ldap_parse_result , arg3to6of6_force_ref )
PHP_FE ( ldap_first_reference , NULL )
PHP_FE ( ldap_next_reference , NULL )
2002-06-22 23:41:39 +08:00
# ifdef HAVE_LDAP_PARSE_REFERENCE
2003-08-04 01:44:39 +08:00
PHP_FE ( ldap_parse_reference , third_arg_force_ref )
2000-10-18 00:06:04 +08:00
# endif
2002-06-22 23:41:39 +08:00
PHP_FE ( ldap_rename , NULL )
# ifdef HAVE_LDAP_START_TLS_S
2001-10-10 05:14:14 +08:00
PHP_FE ( ldap_start_tls , NULL )
# endif
2002-06-22 23:41:39 +08:00
# endif
2001-10-10 05:14:14 +08:00
2001-12-24 00:32:06 +08:00
# if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
2001-11-30 04:26:20 +08:00
PHP_FE ( ldap_set_rebind_proc , NULL )
# endif
2000-08-02 21:11:46 +08:00
# ifdef STR_TRANSLATION
2001-06-01 05:09:30 +08:00
PHP_FE ( ldap_t61_to_8859 , NULL )
PHP_FE ( ldap_8859_to_t61 , NULL )
2000-08-02 21:11:46 +08:00
# endif
1999-04-22 08:45:14 +08:00
{ NULL , NULL , NULL }
} ;
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 08:45:14 +08:00
1999-12-18 04:55:31 +08:00
zend_module_entry ldap_module_entry = {
2001-10-12 07:33:59 +08:00
STANDARD_MODULE_HEADER ,
2001-06-01 05:09:30 +08:00
" ldap " ,
ldap_functions ,
PHP_MINIT ( ldap ) ,
PHP_MSHUTDOWN ( ldap ) ,
NULL ,
NULL ,
PHP_MINFO ( ldap ) ,
2003-06-09 07:34:51 +08:00
NO_VERSION_YET ,
2001-06-01 05:09:30 +08:00
STANDARD_MODULE_PROPERTIES
1999-04-22 08:45:14 +08:00
} ;
2000-05-23 17:33:51 +08:00
# ifdef COMPILE_DL_LDAP
2000-05-02 08:30:36 +08:00
ZEND_GET_MODULE ( ldap )
2003-08-31 20:41:53 +08:00
# ifdef PHP_WIN32
# include "zend_arg_defs.c"
# endif
1999-04-22 08:45:14 +08:00
# endif
2001-07-31 13:44:11 +08:00
static void _close_ldap_link ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
1999-04-22 08:45:14 +08:00
{
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld = ( ldap_linkdata * ) rsrc - > ptr ;
1999-04-22 08:45:14 +08:00
2001-11-30 04:17:17 +08:00
ldap_unbind_s ( ld - > link ) ;
2001-12-24 00:32:06 +08:00
# if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
2001-11-30 04:26:20 +08:00
if ( ld - > rebindproc ! = NULL ) {
zval_dtor ( ld - > rebindproc ) ;
FREE_ZVAL ( ld - > rebindproc ) ;
}
# endif
2001-11-30 04:17:17 +08:00
efree ( ld ) ;
2000-04-02 00:23:39 +08:00
LDAPG ( num_links ) - - ;
1999-04-22 08:45:14 +08:00
}
2001-07-31 13:44:11 +08:00
static void _free_ldap_result ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
1999-04-22 08:45:14 +08:00
{
2000-10-21 02:25:16 +08:00
LDAPMessage * result = ( LDAPMessage * ) rsrc - > ptr ;
2000-04-02 00:23:39 +08:00
ldap_msgfree ( result ) ;
1999-04-22 08:45:14 +08:00
}
2002-05-04 22:27:48 +08:00
static void _free_ldap_result_entry ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
{
ldap_resultentry * entry = ( ldap_resultentry * ) rsrc - > ptr ;
zend_list_delete ( entry - > id ) ;
efree ( entry ) ;
}
2001-06-05 21:12:10 +08:00
/* {{{ PHP_INI_BEGIN
*/
2000-04-02 00:23:39 +08:00
PHP_INI_BEGIN ( )
2003-06-09 07:34:51 +08:00
STD_PHP_INI_ENTRY_EX ( " ldap.max_links " , " -1 " , PHP_INI_SYSTEM , OnUpdateLong , max_links , zend_ldap_globals , ldap_globals , display_link_numbers )
2000-04-02 00:23:39 +08:00
PHP_INI_END ( )
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 08:45:14 +08:00
2001-06-05 21:12:10 +08:00
/* {{{ php_ldap_init_globals
*/
2000-04-02 00:23:39 +08:00
static void php_ldap_init_globals ( zend_ldap_globals * ldap_globals )
1999-04-22 08:45:14 +08:00
{
2000-04-02 00:23:39 +08:00
ldap_globals - > num_links = 0 ;
1999-04-22 08:45:14 +08:00
}
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 08:45:14 +08:00
2001-06-05 21:12:10 +08:00
/* {{{ PHP_MINIT_FUNCTION
*/
1999-08-02 23:02:52 +08:00
PHP_MINIT_FUNCTION ( ldap )
1999-04-22 08:45:14 +08:00
{
2000-04-02 00:23:39 +08:00
ZEND_INIT_MODULE_GLOBALS ( ldap , php_ldap_init_globals , NULL ) ;
1999-04-22 08:45:14 +08:00
2000-04-02 00:23:39 +08:00
REGISTER_INI_ENTRIES ( ) ;
1999-04-22 08:45:14 +08:00
2000-07-24 05:32:02 +08:00
/* Constants to be used with deref-parameter in php_ldap_do_search() */
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_DEREF_NEVER " , LDAP_DEREF_NEVER , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_DEREF_SEARCHING " , LDAP_DEREF_SEARCHING , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_DEREF_FINDING " , LDAP_DEREF_FINDING , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_DEREF_ALWAYS " , LDAP_DEREF_ALWAYS , CONST_PERSISTENT | CONST_CS ) ;
2000-10-18 00:06:04 +08:00
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP
2000-10-18 00:06:04 +08:00
/* LDAP options */
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_OPT_DEREF " , LDAP_OPT_DEREF , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_SIZELIMIT " , LDAP_OPT_SIZELIMIT , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_TIMELIMIT " , LDAP_OPT_TIMELIMIT , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_PROTOCOL_VERSION " , LDAP_OPT_PROTOCOL_VERSION , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_ERROR_NUMBER " , LDAP_OPT_ERROR_NUMBER , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_REFERRALS " , LDAP_OPT_REFERRALS , CONST_PERSISTENT | CONST_CS ) ;
2002-07-10 04:54:49 +08:00
# ifdef LDAP_OPT_RESTART
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_OPT_RESTART " , LDAP_OPT_RESTART , CONST_PERSISTENT | CONST_CS ) ;
2002-07-10 04:54:49 +08:00
# endif
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_OPT_HOST_NAME " , LDAP_OPT_HOST_NAME , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_ERROR_STRING " , LDAP_OPT_ERROR_STRING , CONST_PERSISTENT | CONST_CS ) ;
2001-01-08 05:18:02 +08:00
# ifdef LDAP_OPT_MATCHED_DN
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_OPT_MATCHED_DN " , LDAP_OPT_MATCHED_DN , CONST_PERSISTENT | CONST_CS ) ;
2000-10-18 00:06:04 +08:00
# endif
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " LDAP_OPT_SERVER_CONTROLS " , LDAP_OPT_SERVER_CONTROLS , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " LDAP_OPT_CLIENT_CONTROLS " , LDAP_OPT_CLIENT_CONTROLS , CONST_PERSISTENT | CONST_CS ) ;
2000-11-04 06:53:49 +08:00
# endif
2001-12-24 00:32:06 +08:00
# ifdef LDAP_OPT_DEBUG_LEVEL
REGISTER_LONG_CONSTANT ( " LDAP_OPT_DEBUG_LEVEL " , LDAP_OPT_DEBUG_LEVEL , CONST_PERSISTENT | CONST_CS ) ;
# endif
2000-10-18 00:06:04 +08:00
2000-08-21 02:47:14 +08:00
# ifdef ORALDAP
2001-12-04 04:54:23 +08:00
REGISTER_LONG_CONSTANT ( " GSLC_SSL_NO_AUTH " , GSLC_SSL_NO_AUTH , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " GSLC_SSL_ONEWAY_AUTH " , GSLC_SSL_ONEWAY_AUTH , CONST_PERSISTENT | CONST_CS ) ;
REGISTER_LONG_CONSTANT ( " GSLC_SSL_TWOWAY_AUTH " , GSLC_SSL_TWOWAY_AUTH , CONST_PERSISTENT | CONST_CS ) ;
2000-08-21 02:47:14 +08:00
# endif
2000-07-22 08:34:22 +08:00
2000-10-26 01:44:02 +08:00
le_result = zend_register_list_destructors_ex ( _free_ldap_result , NULL , " ldap result " , module_number ) ;
le_link = zend_register_list_destructors_ex ( _close_ldap_link , NULL , " ldap link " , module_number ) ;
2002-05-04 22:27:48 +08:00
le_result_entry = zend_register_list_destructors_ex ( _free_ldap_result_entry , NULL , " ldap result entry " , module_number ) ;
2001-12-01 07:37:43 +08:00
le_ber_entry = zend_register_list_destructors_ex ( NULL , NULL , " ldap ber entry " , module_number ) ;
1999-04-22 08:45:14 +08:00
2001-09-26 05:58:48 +08:00
Z_TYPE ( ldap_module_entry ) = type ;
1999-04-22 08:45:14 +08:00
return SUCCESS ;
}
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 08:45:14 +08:00
2001-06-05 21:12:10 +08:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
1999-08-02 23:02:52 +08:00
PHP_MSHUTDOWN_FUNCTION ( ldap )
{
1999-04-22 08:45:14 +08:00
return SUCCESS ;
}
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 08:45:14 +08:00
2001-06-05 21:12:10 +08:00
/* {{{ PHP_MINFO_FUNCTION
*/
1999-08-02 23:02:52 +08:00
PHP_MINFO_FUNCTION ( ldap )
1999-04-22 08:45:14 +08:00
{
2000-06-05 23:17:25 +08:00
char tmp [ 32 ] ;
2001-12-25 22:36:06 +08:00
# if HAVE_NSLDAP
1999-04-22 08:45:14 +08:00
LDAPVersion ver ;
double SDKVersion ;
# endif
2000-04-04 06:00:18 +08:00
2001-12-25 22:36:06 +08:00
php_info_print_table_start ( ) ;
2003-06-09 07:34:51 +08:00
php_info_print_table_row ( 2 , " LDAP Support " , " enabled " ) ;
php_info_print_table_row ( 2 , " RCS Version " , " $Id$ " ) ;
2000-04-04 06:00:18 +08:00
2000-04-02 00:23:39 +08:00
if ( LDAPG ( max_links ) = = - 1 ) {
2001-12-25 22:36:06 +08:00
snprintf ( tmp , 31 , " %ld/unlimited " , LDAPG ( num_links ) ) ;
1999-04-22 08:45:14 +08:00
} else {
2001-12-25 22:36:06 +08:00
snprintf ( tmp , 31 , " %ld/%ld " , LDAPG ( num_links ) , LDAPG ( max_links ) ) ;
1999-04-22 08:45:14 +08:00
}
2001-12-25 22:36:06 +08:00
tmp [ 31 ] = ' \0 ' ;
php_info_print_table_row ( 2 , " Total Links " , tmp ) ;
2001-06-01 05:09:30 +08:00
2000-08-07 11:18:00 +08:00
# ifdef LDAP_API_VERSION
2001-12-25 22:36:06 +08:00
snprintf ( tmp , 31 , " %d " , LDAP_API_VERSION ) ;
tmp [ 31 ] = ' \0 ' ;
php_info_print_table_row ( 2 , " API Version " , tmp ) ;
# endif
# ifdef LDAP_VENDOR_NAME
php_info_print_table_row ( 2 , " Vendor Name " , LDAP_VENDOR_NAME ) ;
# endif
# ifdef LDAP_VENDOR_VERSION
snprintf ( tmp , 31 , " %d " , LDAP_VENDOR_VERSION ) ;
tmp [ 31 ] = ' \0 ' ;
php_info_print_table_row ( 2 , " Vendor Version " , tmp ) ;
2000-08-07 11:18:00 +08:00
# endif
2000-04-07 05:17:50 +08:00
2001-06-01 05:09:30 +08:00
# if HAVE_NSLDAP
2003-06-09 07:34:51 +08:00
SDKVersion = ldap_version ( & ver ) ;
snprintf ( tmp , 31 , " %f " , SDKVersion / 100.0 ) ;
2001-12-25 22:36:06 +08:00
tmp [ 31 ] = ' \0 ' ;
2003-06-09 07:34:51 +08:00
php_info_print_table_row ( 2 , " SDK Version " , tmp ) ;
2000-04-07 05:17:50 +08:00
2003-06-09 07:34:51 +08:00
snprintf ( tmp , 31 , " %f " , ver . protocol_version / 100.0 ) ;
2001-12-25 22:36:06 +08:00
tmp [ 31 ] = ' \0 ' ;
2003-06-09 07:34:51 +08:00
php_info_print_table_row ( 2 , " Highest LDAP Protocol Supported " , tmp ) ;
2000-04-07 05:17:50 +08:00
2003-06-09 07:34:51 +08:00
snprintf ( tmp , 31 , " %f " , ver . SSL_version / 100.0 ) ;
2001-12-25 22:36:06 +08:00
tmp [ 31 ] = ' \0 ' ;
2003-06-09 07:34:51 +08:00
php_info_print_table_row ( 2 , " SSL Level Supported " , tmp ) ;
2000-04-07 05:17:50 +08:00
2003-06-09 07:34:51 +08:00
if ( ver . security_level ! = LDAP_SECURITY_NONE ) {
snprintf ( tmp , 31 , " %d " , ver . security_level ) ;
2001-12-25 22:36:06 +08:00
tmp [ 31 ] = ' \0 ' ;
1999-04-22 08:45:14 +08:00
} else {
2003-06-09 07:34:51 +08:00
strcpy ( tmp , " SSL not enabled " ) ;
1999-04-22 08:45:14 +08:00
}
2003-06-09 07:34:51 +08:00
php_info_print_table_row ( 2 , " Level of Encryption " , tmp ) ;
1999-04-22 08:45:14 +08:00
# endif
2000-04-07 05:17:50 +08:00
2003-06-13 21:30:46 +08:00
# ifdef HAVE_LDAP_SASL
php_info_print_table_row ( 2 , " SASL Support " , " Enabled " ) ;
# endif
2000-04-07 05:17:50 +08:00
php_info_print_table_end ( ) ;
1999-04-22 08:45:14 +08:00
}
2001-06-05 21:12:10 +08:00
/* }}} */
2000-07-24 05:32:02 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_connect([string host [, int port]])
1999-04-22 08:45:14 +08:00
Connect to an LDAP server */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_connect )
1999-04-22 08:45:14 +08:00
{
2001-11-14 02:05:47 +08:00
char * host = NULL ;
2002-08-06 05:14:14 +08:00
int hostlen ;
2003-03-07 07:07:28 +08:00
long port = 389 ; /* Default port */
2000-08-21 02:47:14 +08:00
# ifdef HAVE_ORALDAP
char * wallet , * walletpasswd ;
2002-08-06 05:14:14 +08:00
int walletlen , walletpasswdlen ;
2003-03-07 07:07:28 +08:00
long authmode ;
2000-08-21 02:47:14 +08:00
int ssl = 0 ;
# endif
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-04-22 08:45:14 +08:00
LDAP * ldap ;
2000-08-21 02:47:14 +08:00
# ifdef HAVE_ORALDAP
2002-08-06 05:14:14 +08:00
if ( ZEND_NUM_ARGS ( ) = = 3 | | ZEND_NUM_ARGS ( ) = = 4 ) {
WRONG_PARAM_COUNT ;
}
2000-08-21 02:47:14 +08:00
2002-08-06 05:14:14 +08:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " |slssl " , & host , & hostlen , & port , & wallet , & walletlen , & walletpasswd , & walletpasswdlen , & authmode ) = = FAILURE ) {
RETURN_FALSE ;
}
1999-04-22 08:45:14 +08:00
2003-06-09 07:34:51 +08:00
if ( ZEND_NUM_ARGS ( ) = = 5 ) {
2002-08-06 05:14:14 +08:00
ssl = 1 ;
1999-04-22 08:45:14 +08:00
}
2002-08-06 05:14:14 +08:00
# else
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " |sl " , & host , & hostlen , & port ) = = FAILURE ) {
RETURN_FALSE ;
}
# endif
1999-04-22 08:45:14 +08:00
2001-06-01 05:09:30 +08:00
if ( LDAPG ( max_links ) ! = - 1 & & LDAPG ( num_links ) > = LDAPG ( max_links ) ) {
2003-09-01 04:45:51 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Too many open links (%ld) " , LDAPG ( num_links ) ) ;
2000-04-02 00:23:39 +08:00
RETURN_FALSE ;
1999-04-22 08:45:14 +08:00
}
2001-11-30 04:17:17 +08:00
ld = ecalloc ( 1 , sizeof ( ldap_linkdata ) ) ;
if ( ld = = NULL ) {
RETURN_FALSE ;
}
2000-10-21 04:22:59 +08:00
# ifdef LDAP_API_FEATURE_X_OPENLDAP
2001-11-14 02:05:47 +08:00
if ( host ! = NULL & & strchr ( host , ' / ' ) ) {
2000-10-21 04:22:59 +08:00
int rc ;
rc = ldap_initialize ( & ldap , host ) ;
if ( rc ! = LDAP_SUCCESS ) {
2002-10-31 02:12:34 +08:00
efree ( ld ) ;
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Could not create session handle: %s " , ldap_err2string ( rc ) ) ;
2000-10-21 04:22:59 +08:00
RETURN_FALSE ;
}
2001-11-14 02:05:47 +08:00
} else {
ldap = ldap_init ( host , port ) ;
2000-10-21 04:22:59 +08:00
}
2001-11-14 02:05:47 +08:00
# else
ldap = ldap_open ( host , port ) ;
# endif
2000-10-21 04:22:59 +08:00
2003-06-09 07:34:51 +08:00
if ( ldap = = NULL ) {
2002-10-31 02:12:34 +08:00
efree ( ld ) ;
2000-04-02 00:23:39 +08:00
RETURN_FALSE ;
1999-04-22 08:45:14 +08:00
} else {
2000-08-21 02:47:14 +08:00
# ifdef HAVE_ORALDAP
if ( ssl ) {
2001-06-01 05:09:30 +08:00
if ( ldap_init_SSL ( & ldap - > ld_sb , wallet , walletpasswd , authmode ) ) {
2002-10-31 02:12:34 +08:00
efree ( ld ) ;
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " SSL init failed " ) ;
2000-08-21 02:47:14 +08:00
RETURN_FALSE ;
}
}
# endif
2001-06-01 05:09:30 +08:00
LDAPG ( num_links ) + + ;
2001-11-30 04:17:17 +08:00
ld - > link = ldap ;
ZEND_REGISTER_RESOURCE ( return_value , ld , le_link ) ;
1999-04-22 08:45:14 +08:00
}
}
/* }}} */
2001-06-05 21:12:10 +08:00
/* {{{ _get_lderrno
*/
2000-08-27 00:20:37 +08:00
static int _get_lderrno ( LDAP * ldap )
{
# if !HAVE_NSLDAP
# if LDAP_API_VERSION > 2000
int lderr ;
/* New versions of OpenLDAP do it this way */
ldap_get_option ( ldap , LDAP_OPT_ERROR_NUMBER , & lderr ) ;
return lderr ;
# else
return ldap - > ld_errno ;
# endif
# else
2001-08-12 00:39:07 +08:00
return ldap_get_lderrno ( ldap , NULL , NULL ) ;
2000-08-27 00:20:37 +08:00
# endif
}
2001-06-05 21:12:10 +08:00
/* }}} */
2000-08-27 00:20:37 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_bind(resource link [, string dn, string password])
1999-04-22 08:45:14 +08:00
Bind to LDAP directory */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_bind )
1999-04-22 08:45:14 +08:00
{
2002-08-06 05:14:14 +08:00
zval * link ;
char * ldap_bind_dn = NULL , * ldap_bind_pw = NULL ;
int ldap_bind_dnlen , ldap_bind_pwlen ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-06-26 14:49:25 +08:00
int rc ;
1999-04-22 08:45:14 +08:00
2002-08-06 05:14:14 +08:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " r|ss " , & link , & ldap_bind_dn , & ldap_bind_dnlen , & ldap_bind_pw , & ldap_bind_pwlen ) = = FAILURE ) {
RETURN_FALSE ;
}
1999-04-22 08:45:14 +08:00
2002-08-06 05:14:14 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , & link , - 1 , " ldap link " , le_link ) ;
1999-04-22 08:45:14 +08:00
2002-08-06 05:14:14 +08:00
if ( ( rc = ldap_bind_s ( ld - > link , ldap_bind_dn , ldap_bind_pw , LDAP_AUTH_SIMPLE ) ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unable to bind to server: %s " , ldap_err2string ( rc ) ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
}
/* }}} */
2003-06-13 21:30:46 +08:00
# ifdef HAVE_LDAP_SASL
2003-06-09 07:44:01 +08:00
/* {{{ _php_sasl_interact
Interact function for SASL */
static int _php_sasl_interact ( LDAP * ld , unsigned flags , void * defaults , void * in )
{
sasl_interact_t * interact = in ;
while ( interact - > id ! = SASL_CB_LIST_END ) {
const char * dflt = interact - > defresult ;
interact - > result = strdup ( ( dflt & & * dflt ) ? dflt : " " ) ;
interact - > len = interact - > result ? strlen ( interact - > result ) : 0 ;
interact + + ;
} ;
return LDAP_SUCCESS ;
}
/* {{{ proto bool ldap_sasl_bind(resource link)
Bind to LDAP directory using SASL */
PHP_FUNCTION ( ldap_sasl_bind )
{
zval * link ;
ldap_linkdata * ld ;
int rc ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " r " , & link ) = = FAILURE ) {
RETURN_FALSE ;
}
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , & link , - 1 , " ldap link " , le_link ) ;
if ( ( rc = ldap_sasl_interactive_bind_s ( ld - > link , NULL , NULL , NULL , NULL , LDAP_SASL_QUIET , _php_sasl_interact , NULL ) ) ! = LDAP_SUCCESS ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unable to bind to server: %s " , ldap_err2string ( rc ) ) ;
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
}
/* }}} */
2003-06-13 21:30:46 +08:00
# endif /* HAVE_LDAP_SASL */
2003-06-09 07:44:01 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_unbind(resource link)
1999-04-22 08:45:14 +08:00
Unbind from LDAP directory */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_unbind )
1999-04-22 08:45:14 +08:00
{
2002-08-06 05:14:14 +08:00
zval * link ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-04-22 08:45:14 +08:00
2002-08-06 05:14:14 +08:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " r " , & link ) = = FAILURE ) {
RETURN_FALSE ;
1999-04-22 08:45:14 +08:00
}
2002-08-06 05:14:14 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , & link , - 1 , " ldap link " , le_link ) ;
1999-04-22 08:45:14 +08:00
2002-08-06 05:14:14 +08:00
zend_list_delete ( Z_LVAL_P ( link ) ) ;
1999-04-22 08:45:14 +08:00
RETURN_TRUE ;
}
/* }}} */
2001-11-14 01:10:08 +08:00
/* {{{ php_set_opts
2001-06-05 21:12:10 +08:00
*/
2001-01-13 06:08:26 +08:00
static void php_set_opts ( LDAP * ldap , int sizelimit , int timelimit , int deref )
{
/* sizelimit */
if ( sizelimit > - 1 ) {
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP
2001-01-13 06:08:26 +08:00
ldap_set_option ( ldap , LDAP_OPT_SIZELIMIT , & sizelimit ) ;
# else
ldap - > ld_sizelimit = sizelimit ;
# endif
}
/* timelimit */
if ( timelimit > - 1 ) {
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP
2001-01-13 06:08:26 +08:00
ldap_set_option ( ldap , LDAP_OPT_TIMELIMIT , & timelimit ) ;
# else
ldap - > ld_timelimit = timelimit ;
# endif
}
/* deref */
if ( deref > - 1 ) {
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP
2001-01-13 06:08:26 +08:00
ldap_set_option ( ldap , LDAP_OPT_DEREF , & deref ) ;
# else
ldap - > ld_deref = deref ;
# endif
}
}
2001-06-05 21:12:10 +08:00
/* }}} */
2001-01-13 06:08:26 +08:00
2001-06-05 21:12:10 +08:00
/* {{{ php_ldap_do_search
*/
1999-12-18 19:49:31 +08:00
static void php_ldap_do_search ( INTERNAL_FUNCTION_PARAMETERS , int scope )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * base_dn , * * filter , * * attrs , * * attr , * * attrsonly , * * sizelimit , * * timelimit , * * deref ;
2001-11-14 01:10:08 +08:00
char * ldap_base_dn = NULL ;
char * ldap_filter = NULL ;
1999-04-22 08:45:14 +08:00
char * * ldap_attrs = NULL ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2001-06-01 05:09:30 +08:00
LDAPMessage * ldap_res ;
2003-06-09 07:34:51 +08:00
int ldap_attrsonly = 0 ;
2000-07-24 05:32:02 +08:00
int ldap_sizelimit = - 1 ;
int ldap_timelimit = - 1 ;
int ldap_deref = - 1 ;
2000-07-22 03:00:21 +08:00
int num_attribs = 0 ;
int i , errno ;
int myargcount = ZEND_NUM_ARGS ( ) ;
2003-06-09 07:34:51 +08:00
2000-07-22 03:00:21 +08:00
if ( myargcount < 3 | | myargcount > 8 | | zend_get_parameters_ex ( myargcount , & link , & base_dn , & filter , & attrs , & attrsonly , & sizelimit , & timelimit , & deref ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-04-22 08:45:14 +08:00
2000-07-22 03:00:21 +08:00
/* Reverse -> fall through */
2003-06-09 07:34:51 +08:00
switch ( myargcount ) {
2000-07-22 03:00:21 +08:00
case 8 :
convert_to_long_ex ( deref ) ;
2001-09-26 05:58:48 +08:00
ldap_deref = Z_LVAL_PP ( deref ) ;
1999-04-22 08:45:14 +08:00
2000-07-22 03:00:21 +08:00
case 7 :
convert_to_long_ex ( timelimit ) ;
2001-09-26 05:58:48 +08:00
ldap_timelimit = Z_LVAL_PP ( timelimit ) ;
1999-04-22 08:45:14 +08:00
2000-07-22 03:00:21 +08:00
case 6 :
convert_to_long_ex ( sizelimit ) ;
2001-09-26 05:58:48 +08:00
ldap_sizelimit = Z_LVAL_PP ( sizelimit ) ;
1999-04-22 08:45:14 +08:00
2000-07-22 03:00:21 +08:00
case 5 :
convert_to_long_ex ( attrsonly ) ;
2001-09-26 05:58:48 +08:00
ldap_attrsonly = Z_LVAL_PP ( attrsonly ) ;
1999-04-22 08:45:14 +08:00
case 4 :
2001-09-26 05:58:48 +08:00
if ( Z_TYPE_PP ( attrs ) ! = IS_ARRAY ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Expected Array as last element " ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
2001-09-26 05:58:48 +08:00
num_attribs = zend_hash_num_elements ( Z_ARRVAL_PP ( attrs ) ) ;
2003-05-01 05:53:53 +08:00
if ( ( ldap_attrs = safe_emalloc ( ( num_attribs + 1 ) , sizeof ( char * ) , 0 ) ) = = NULL ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Could not allocate memory " ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_attribs ; i + + ) {
if ( zend_hash_index_find ( Z_ARRVAL_PP ( attrs ) , i , ( void * * ) & attr ) = = FAILURE ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Array initialization wrong " ) ;
2001-01-13 18:00:27 +08:00
efree ( ldap_attrs ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
2000-07-22 03:00:21 +08:00
1999-08-08 00:32:50 +08:00
SEPARATE_ZVAL ( attr ) ;
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( attr ) ;
2001-09-26 05:58:48 +08:00
ldap_attrs [ i ] = Z_STRVAL_PP ( attr ) ;
1999-04-22 08:45:14 +08:00
}
ldap_attrs [ num_attribs ] = NULL ;
2000-07-22 03:00:21 +08:00
case 3 :
2001-01-13 06:08:26 +08:00
/* parallel search? */
if ( Z_TYPE_PP ( link ) ! = IS_ARRAY ) {
2001-01-17 21:43:39 +08:00
convert_to_string_ex ( filter ) ;
ldap_filter = Z_STRVAL_PP ( filter ) ;
2001-01-13 06:08:26 +08:00
convert_to_string_ex ( base_dn ) ;
ldap_base_dn = Z_STRVAL_PP ( base_dn ) ;
}
2000-07-22 03:00:21 +08:00
break ;
1999-04-22 08:45:14 +08:00
default :
WRONG_PARAM_COUNT ;
2000-07-22 03:00:21 +08:00
break ;
1999-04-22 08:45:14 +08:00
}
2001-01-13 06:08:26 +08:00
/* parallel search? */
if ( Z_TYPE_PP ( link ) = = IS_ARRAY ) {
2001-01-17 21:43:39 +08:00
int i , nlinks , nbases , nfilters , * rcs ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * * lds ;
2001-12-27 02:40:57 +08:00
zval * * entry , * resource ;
2001-01-13 06:08:26 +08:00
nlinks = zend_hash_num_elements ( Z_ARRVAL_PP ( link ) ) ;
if ( nlinks = = 0 ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " No links in link array " ) ;
2001-01-13 06:08:26 +08:00
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
RETURN_FALSE ;
}
if ( Z_TYPE_PP ( base_dn ) = = IS_ARRAY ) {
nbases = zend_hash_num_elements ( Z_ARRVAL_PP ( base_dn ) ) ;
if ( nbases ! = nlinks ) {
2003-01-19 05:31:11 +08:00
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 " ) ;
2001-01-13 06:08:26 +08:00
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
RETURN_FALSE ;
}
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( base_dn ) ) ;
} else {
nbases = 0 ; /* this means string, not array */
convert_to_string_ex ( base_dn ) ;
ldap_base_dn = Z_STRLEN_PP ( base_dn ) < 1 ? NULL : Z_STRVAL_PP ( base_dn ) ;
}
2001-01-17 21:43:39 +08:00
if ( Z_TYPE_PP ( filter ) = = IS_ARRAY ) {
nfilters = zend_hash_num_elements ( Z_ARRVAL_PP ( filter ) ) ;
if ( nfilters ! = nlinks ) {
2003-01-19 05:31:11 +08:00
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 " ) ;
2001-01-17 21:43:39 +08:00
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
RETURN_FALSE ;
}
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( filter ) ) ;
} else {
nfilters = 0 ; /* this means string, not array */
convert_to_string_ex ( filter ) ;
ldap_filter = Z_STRVAL_PP ( filter ) ;
}
2003-05-01 05:53:53 +08:00
lds = safe_emalloc ( nlinks , sizeof ( ldap_linkdata ) , 0 ) ;
rcs = safe_emalloc ( nlinks , sizeof ( * rcs ) , 0 ) ;
2001-01-13 06:08:26 +08:00
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( link ) ) ;
for ( i = 0 ; i < nlinks ; i + + ) {
zend_hash_get_current_data ( Z_ARRVAL_PP ( link ) , ( void * * ) & entry ) ;
2001-06-01 05:09:30 +08:00
2001-11-30 04:17:17 +08:00
ld = ( ldap_linkdata * ) zend_fetch_resource ( entry TSRMLS_CC , - 1 , " ldap link " , NULL , 1 , le_link ) ;
if ( ld = = NULL ) {
efree ( lds ) ;
2001-01-13 06:08:26 +08:00
efree ( rcs ) ;
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
RETURN_FALSE ;
}
if ( nbases ! = 0 ) { /* base_dn an array? */
zend_hash_get_current_data ( Z_ARRVAL_PP ( base_dn ) , ( void * * ) & entry ) ;
zend_hash_move_forward ( Z_ARRVAL_PP ( base_dn ) ) ;
convert_to_string_ex ( entry ) ;
ldap_base_dn = Z_STRLEN_PP ( entry ) < 1 ? NULL : Z_STRVAL_PP ( entry ) ;
}
2001-01-17 21:43:39 +08:00
if ( nfilters ! = 0 ) { /* filter an array? */
zend_hash_get_current_data ( Z_ARRVAL_PP ( filter ) , ( void * * ) & entry ) ;
zend_hash_move_forward ( Z_ARRVAL_PP ( filter ) ) ;
convert_to_string_ex ( entry ) ;
ldap_filter = Z_STRVAL_PP ( entry ) ;
}
2001-01-13 06:08:26 +08:00
2001-11-30 04:17:17 +08:00
php_set_opts ( ld - > link , ldap_sizelimit , ldap_timelimit , ldap_deref ) ;
2001-01-13 06:08:26 +08:00
/* Run the actual search */
2001-11-30 04:17:17 +08:00
rcs [ i ] = ldap_search ( ld - > link , ldap_base_dn , scope , ldap_filter , ldap_attrs , ldap_attrsonly ) ;
lds [ i ] = ld ;
2001-01-13 06:08:26 +08:00
zend_hash_move_forward ( Z_ARRVAL_PP ( link ) ) ;
}
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
2003-01-19 04:10:23 +08:00
array_init ( return_value ) ;
2001-01-13 06:08:26 +08:00
/* Collect results from the searches */
for ( i = 0 ; i < nlinks ; i + + ) {
2001-12-27 02:40:57 +08:00
MAKE_STD_ZVAL ( resource ) ;
2001-01-13 06:08:26 +08:00
if ( rcs [ i ] ! = - 1 ) {
2001-11-30 04:17:17 +08:00
rcs [ i ] = ldap_result ( lds [ i ] - > link , LDAP_RES_ANY , 1 /* LDAP_MSG_ALL */ , NULL , & ldap_res ) ;
2001-01-13 06:08:26 +08:00
}
if ( rcs [ i ] ! = - 1 ) {
2001-12-27 02:40:57 +08:00
ZEND_REGISTER_RESOURCE ( resource , ldap_res , le_result ) ;
add_next_index_zval ( return_value , resource ) ;
2001-01-13 06:08:26 +08:00
} else {
add_next_index_bool ( return_value , 0 ) ;
}
2001-12-27 02:40:57 +08:00
}
2001-11-30 04:17:17 +08:00
efree ( lds ) ;
2001-01-13 06:08:26 +08:00
efree ( rcs ) ;
return ;
}
1999-04-22 08:45:14 +08:00
/* fix to make null base_dn's work */
2003-06-09 07:34:51 +08:00
if ( strlen ( ldap_base_dn ) < 1 ) {
ldap_base_dn = NULL ;
1999-04-22 08:45:14 +08:00
}
2001-11-30 04:17:17 +08:00
ld = ( ldap_linkdata * ) zend_fetch_resource ( link TSRMLS_CC , - 1 , " ldap link " , NULL , 1 , le_link ) ;
if ( ld = = NULL ) {
2001-01-13 18:00:27 +08:00
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
RETURN_FALSE ;
}
1999-04-22 08:45:14 +08:00
2001-11-30 04:17:17 +08:00
php_set_opts ( ld - > link , ldap_sizelimit , ldap_timelimit , ldap_deref ) ;
2000-07-22 03:00:21 +08:00
/* Run the actual search */
2001-11-30 04:17:17 +08:00
errno = ldap_search_s ( ld - > link , ldap_base_dn , scope , ldap_filter , ldap_attrs , ldap_attrsonly , & ldap_res ) ;
2000-07-22 03:00:21 +08:00
if ( ldap_attrs ! = NULL ) {
efree ( ldap_attrs ) ;
}
2001-01-07 21:03:11 +08:00
if ( errno ! = LDAP_SUCCESS
2003-06-09 07:34:51 +08:00
& & errno ! = LDAP_SIZELIMIT_EXCEEDED
2001-01-07 21:03:11 +08:00
# ifdef LDAP_ADMINLIMIT_EXCEEDED
2003-06-09 07:34:51 +08:00
& & errno ! = LDAP_ADMINLIMIT_EXCEEDED
2001-01-07 21:03:11 +08:00
# endif
# ifdef LDAP_REFERRAL
2003-06-09 07:34:51 +08:00
& & errno ! = LDAP_REFERRAL
2001-01-07 21:03:11 +08:00
# endif
2003-06-09 07:34:51 +08:00
) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Search: %s " , ldap_err2string ( errno ) ) ;
2000-07-22 03:00:21 +08:00
RETVAL_FALSE ;
} else {
2003-06-09 07:34:51 +08:00
if ( errno = = LDAP_SIZELIMIT_EXCEEDED ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Partial search results returned: Sizelimit exceeded. " ) ;
2000-07-22 03:00:21 +08:00
}
2001-01-07 21:03:11 +08:00
# ifdef LDAP_ADMINLIMIT_EXCEEDED
else if ( errno = = LDAP_ADMINLIMIT_EXCEEDED ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Partial search results returned: Adminlimit exceeded. " ) ;
2001-01-07 21:03:11 +08:00
}
# endif
2001-06-01 05:09:30 +08:00
ZEND_REGISTER_RESOURCE ( return_value , ldap_res , le_result ) ;
1999-04-22 08:45:14 +08:00
}
}
2001-06-05 21:12:10 +08:00
/* }}} */
2000-07-22 03:00:21 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_read(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
1999-04-22 08:45:14 +08:00
Read an entry */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_read )
1999-04-22 08:45:14 +08:00
{
1999-12-18 19:49:31 +08:00
php_ldap_do_search ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_SCOPE_BASE ) ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_list(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
1999-04-22 08:45:14 +08:00
Single - level search */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_list )
1999-04-22 08:45:14 +08:00
{
1999-12-18 19:49:31 +08:00
php_ldap_do_search ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_SCOPE_ONELEVEL ) ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_search(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
1999-04-22 08:45:14 +08:00
Search LDAP tree under base_dn */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_search )
1999-04-22 08:45:14 +08:00
{
1999-12-18 19:49:31 +08:00
php_ldap_do_search ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_SCOPE_SUBTREE ) ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_free_result(resource result)
1999-04-22 08:45:14 +08:00
Free result memory */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_free_result )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * result ;
1999-07-17 02:01:55 +08:00
LDAPMessage * ldap_result ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & result ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
2001-11-15 06:05:33 +08:00
zend_list_delete ( Z_LVAL_PP ( result ) ) ; /* Delete list entry and call registered destructor function */
RETVAL_TRUE ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto int ldap_count_entries(resource link, resource result)
1999-04-22 08:45:14 +08:00
Count the number of entries in a search result */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_count_entries )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-07-17 02:01:55 +08:00
LDAPMessage * ldap_result ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
1999-04-22 08:45:14 +08:00
2001-11-30 04:17:17 +08:00
RETURN_LONG ( ldap_count_entries ( ld - > link , ldap_result ) ) ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_first_entry(resource link, resource result)
1999-04-22 08:45:14 +08:00
Return first result id */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_first_entry )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result ;
2002-05-04 22:27:48 +08:00
ldap_linkdata * ld ;
ldap_resultentry * resultentry ;
LDAPMessage * ldap_result , * entry ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
if ( ( entry = ldap_first_entry ( ld - > link , ldap_result ) ) = = NULL ) {
2001-06-01 05:09:30 +08:00
RETVAL_FALSE ;
1999-04-22 08:45:14 +08:00
} else {
2002-05-04 22:27:48 +08:00
resultentry = emalloc ( sizeof ( ldap_resultentry ) ) ;
ZEND_REGISTER_RESOURCE ( return_value , resultentry , le_result_entry ) ;
resultentry - > id = Z_LVAL_PP ( result ) ;
zend_list_addref ( resultentry - > id ) ;
resultentry - > data = entry ;
1999-04-22 08:45:14 +08:00
}
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto resource ldap_next_entry(resource link, resource result_entry)
1999-04-22 08:45:14 +08:00
Get next result entry */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_next_entry )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry , * resultentry_next ;
LDAPMessage * entry_next ;
1999-04-22 08:45:14 +08:00
2001-06-01 05:09:30 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result_entry ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
if ( ( entry_next = ldap_next_entry ( ld - > link , resultentry - > data ) ) = = NULL ) {
2001-06-01 05:09:30 +08:00
RETVAL_FALSE ;
1999-04-22 08:45:14 +08:00
} else {
2002-05-04 22:27:48 +08:00
resultentry_next = emalloc ( sizeof ( ldap_resultentry ) ) ;
ZEND_REGISTER_RESOURCE ( return_value , resultentry_next , le_result_entry ) ;
resultentry_next - > id = resultentry - > id ;
zend_list_addref ( resultentry - > id ) ;
resultentry_next - > data = entry_next ;
1999-04-22 08:45:14 +08:00
}
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto array ldap_get_entries(resource link, resource result)
1999-04-22 08:45:14 +08:00
Get all result entries */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_get_entries )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result ;
1999-07-17 02:01:55 +08:00
LDAPMessage * ldap_result , * ldap_result_entry ;
2003-06-09 07:34:51 +08:00
zval * tmp1 , * tmp2 ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-07-17 02:01:55 +08:00
LDAP * ldap ;
int num_entries , num_attrib , num_values , i ;
BerElement * ber ;
char * attribute ;
1999-12-10 07:45:30 +08:00
size_t attr_len ;
1999-07-17 02:01:55 +08:00
char * * ldap_value ;
char * dn ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
1999-04-22 08:45:14 +08:00
2001-11-30 04:17:17 +08:00
ldap = ld - > link ;
1999-04-22 08:45:14 +08:00
num_entries = ldap_count_entries ( ldap , ldap_result ) ;
array_init ( return_value ) ;
2000-12-05 01:07:27 +08:00
add_assoc_long ( return_value , " count " , num_entries ) ;
if ( num_entries = = 0 ) return ;
2000-07-25 23:34:28 +08:00
num_entries = 0 ;
2000-12-05 01:07:27 +08:00
1999-04-22 08:45:14 +08:00
ldap_result_entry = ldap_first_entry ( ldap , ldap_result ) ;
if ( ldap_result_entry = = NULL ) RETURN_FALSE ;
2003-06-09 07:34:51 +08:00
while ( ldap_result_entry ! = NULL ) {
1999-04-22 08:45:14 +08:00
1999-07-17 05:53:17 +08:00
MAKE_STD_ZVAL ( tmp1 ) ;
1999-07-17 02:01:55 +08:00
array_init ( tmp1 ) ;
1999-04-22 08:45:14 +08:00
2000-07-25 23:34:28 +08:00
num_attrib = 0 ;
1999-04-22 08:45:14 +08:00
attribute = ldap_first_attribute ( ldap , ldap_result_entry , & ber ) ;
2000-07-25 23:34:28 +08:00
1999-04-22 08:45:14 +08:00
while ( attribute ! = NULL ) {
ldap_value = ldap_get_values ( ldap , ldap_result_entry , attribute ) ;
num_values = ldap_count_values ( ldap_value ) ;
1999-07-17 05:53:17 +08:00
MAKE_STD_ZVAL ( tmp2 ) ;
1999-07-17 02:01:55 +08:00
array_init ( tmp2 ) ;
add_assoc_long ( tmp2 , " count " , num_values ) ;
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_values ; i + + ) {
1999-07-17 02:01:55 +08:00
add_index_string ( tmp2 , i , ldap_value [ i ] , 1 ) ;
1999-04-22 08:45:14 +08:00
}
ldap_value_free ( ldap_value ) ;
1999-12-10 07:45:30 +08:00
attr_len = strlen ( attribute ) ;
2003-06-09 07:34:51 +08:00
zend_hash_update ( Z_ARRVAL_P ( tmp1 ) , php_strtolower ( attribute , attr_len ) , attr_len + 1 , ( void * ) & tmp2 , sizeof ( zval * ) , NULL ) ;
2000-07-25 23:34:28 +08:00
add_index_string ( tmp1 , num_attrib , attribute , 1 ) ;
1999-04-22 08:45:14 +08:00
2000-07-25 23:34:28 +08:00
num_attrib + + ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
2001-06-21 05:12:55 +08:00
ldap_memfree ( attribute ) ;
# endif
1999-04-22 08:45:14 +08:00
attribute = ldap_next_attribute ( ldap , ldap_result_entry , ber ) ;
}
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
2001-06-21 05:12:55 +08:00
if ( ber ! = NULL )
ber_free ( ber , 0 ) ;
# endif
1999-04-22 08:45:14 +08:00
1999-07-17 02:01:55 +08:00
add_assoc_long ( tmp1 , " count " , num_attrib ) ;
1999-04-22 08:45:14 +08:00
dn = ldap_get_dn ( ldap , ldap_result_entry ) ;
1999-07-17 02:01:55 +08:00
add_assoc_string ( tmp1 , " dn " , dn , 1 ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
2001-06-21 05:12:55 +08:00
ldap_memfree ( dn ) ;
2001-07-15 19:12:28 +08:00
# else
free ( dn ) ;
2001-06-21 05:12:55 +08:00
# endif
1999-04-22 08:45:14 +08:00
2003-06-09 07:34:51 +08:00
zend_hash_index_update ( Z_ARRVAL_P ( return_value ) , num_entries , ( void * ) & tmp1 , sizeof ( zval * ) , NULL ) ;
1999-04-22 08:45:14 +08:00
2000-07-25 23:34:28 +08:00
num_entries + + ;
1999-04-22 08:45:14 +08:00
ldap_result_entry = ldap_next_entry ( ldap , ldap_result_entry ) ;
}
add_assoc_long ( return_value , " count " , num_entries ) ;
2000-07-25 23:34:28 +08:00
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto string ldap_first_attribute(resource link, resource result_entry, int ber)
1999-04-22 08:45:14 +08:00
Return first attribute */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_first_attribute )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry , * * berp ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
1999-04-22 08:45:14 +08:00
BerElement * ber ;
char * attribute ;
2002-05-04 22:27:48 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & result_entry , & berp ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
if ( ( attribute = ldap_first_attribute ( ld - > link , resultentry - > data , & ber ) ) = = NULL ) {
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
} else {
2001-12-01 07:37:43 +08:00
ZEND_REGISTER_RESOURCE ( * berp , ber , le_ber_entry ) ;
1999-04-22 08:45:14 +08:00
2001-08-12 00:39:07 +08:00
RETVAL_STRING ( attribute , 1 ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
1999-04-22 08:45:14 +08:00
ldap_memfree ( attribute ) ;
# endif
}
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto string ldap_next_attribute(resource link, resource result_entry, resource ber)
1999-04-22 08:45:14 +08:00
Get the next attribute in result */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_next_attribute )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry , * * berp ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
1999-07-17 02:01:55 +08:00
BerElement * ber ;
char * attribute ;
1999-04-22 08:45:14 +08:00
2003-06-09 07:34:51 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & result_entry , & berp ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ber , BerElement * , berp , - 1 , " ldap ber entry " , le_ber_entry ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
if ( ( attribute = ldap_next_attribute ( ld - > link , resultentry - > data , ber ) ) = = NULL ) {
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
} else {
2001-12-01 07:37:43 +08:00
ZEND_REGISTER_RESOURCE ( * berp , ber , le_ber_entry ) ;
2000-08-05 01:18:54 +08:00
2001-08-12 00:39:07 +08:00
RETVAL_STRING ( attribute , 1 ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
1999-04-22 08:45:14 +08:00
ldap_memfree ( attribute ) ;
# endif
}
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto array ldap_get_attributes(resource link, resource result_entry)
1999-04-22 08:45:14 +08:00
Get attributes from a search result entry */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_get_attributes )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry ;
zval * tmp ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
1999-07-17 02:01:55 +08:00
char * attribute ;
char * * ldap_value ;
2000-07-25 23:34:28 +08:00
int i , num_values , num_attrib ;
1999-07-17 02:01:55 +08:00
BerElement * ber ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result_entry ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
1999-04-22 08:45:14 +08:00
2000-07-25 23:34:28 +08:00
array_init ( return_value ) ;
1999-04-22 08:45:14 +08:00
num_attrib = 0 ;
2002-05-04 22:27:48 +08:00
attribute = ldap_first_attribute ( ld - > link , resultentry - > data , & ber ) ;
1999-04-22 08:45:14 +08:00
while ( attribute ! = NULL ) {
2002-05-04 22:27:48 +08:00
ldap_value = ldap_get_values ( ld - > link , resultentry - > data , attribute ) ;
1999-04-22 08:45:14 +08:00
num_values = ldap_count_values ( ldap_value ) ;
1999-07-17 05:53:17 +08:00
MAKE_STD_ZVAL ( tmp ) ;
1999-07-17 02:01:55 +08:00
array_init ( tmp ) ;
add_assoc_long ( tmp , " count " , num_values ) ;
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_values ; i + + ) {
1999-07-17 02:01:55 +08:00
add_index_string ( tmp , i , ldap_value [ i ] , 1 ) ;
1999-04-22 08:45:14 +08:00
}
ldap_value_free ( ldap_value ) ;
2003-06-09 07:34:51 +08:00
zend_hash_update ( Z_ARRVAL_P ( return_value ) , attribute , strlen ( attribute ) + 1 , ( void * ) & tmp , sizeof ( zval * ) , NULL ) ;
2000-07-25 23:34:28 +08:00
add_index_string ( return_value , num_attrib , attribute , 1 ) ;
1999-04-22 08:45:14 +08:00
2000-07-25 23:34:28 +08:00
num_attrib + + ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
2001-06-23 21:43:20 +08:00
ldap_memfree ( attribute ) ;
# endif
2002-05-04 22:27:48 +08:00
attribute = ldap_next_attribute ( ld - > link , resultentry - > data , ber ) ;
1999-04-22 08:45:14 +08:00
}
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
if ( ber ! = NULL ) {
2001-06-23 21:43:20 +08:00
ber_free ( ber , 0 ) ;
2003-06-09 07:34:51 +08:00
}
2001-06-23 21:43:20 +08:00
# endif
1999-04-22 08:45:14 +08:00
add_assoc_long ( return_value , " count " , num_attrib ) ;
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto array ldap_get_values(resource link, resource result_entry, string attribute)
1999-04-22 08:45:14 +08:00
Get all values from a result entry */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_get_values )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry , * * attr ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
1999-07-17 02:01:55 +08:00
char * attribute ;
char * * ldap_value ;
int i , num_values ;
1999-04-22 08:45:14 +08:00
2001-08-12 00:39:07 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & result_entry , & attr ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
1999-04-22 08:45:14 +08:00
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( attr ) ;
2001-09-26 05:58:48 +08:00
attribute = Z_STRVAL_PP ( attr ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
if ( ( ldap_value = ldap_get_values ( ld - > link , resultentry - > data , attribute ) ) = = NULL ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Cannot get the value(s) of attribute %s " , ldap_err2string ( _get_lderrno ( ld - > link ) ) ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
num_values = ldap_count_values ( ldap_value ) ;
2003-01-19 04:10:23 +08:00
array_init ( return_value ) ;
1999-04-22 08:45:14 +08:00
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_values ; i + + ) {
1999-04-22 08:45:14 +08:00
add_next_index_string ( return_value , ldap_value [ i ] , 1 ) ;
}
add_assoc_long ( return_value , " count " , num_values ) ;
ldap_value_free ( ldap_value ) ;
2000-07-28 08:09:33 +08:00
1999-04-22 08:45:14 +08:00
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto array ldap_get_values_len(resource link, resource result_entry, string attribute)
2000-07-28 08:09:33 +08:00
Get all values with lengths from a result entry */
2000-04-08 07:20:22 +08:00
PHP_FUNCTION ( ldap_get_values_len )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry , * * attr ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
2000-04-08 07:20:22 +08:00
char * attribute ;
struct berval * * ldap_value_len ;
int i , num_values ;
2000-07-28 08:09:33 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & result_entry , & attr ) = = FAILURE ) {
2000-04-08 07:20:22 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
2000-07-29 06:39:22 +08:00
2000-04-08 07:20:22 +08:00
convert_to_string_ex ( attr ) ;
2001-09-26 05:58:48 +08:00
attribute = Z_STRVAL_PP ( attr ) ;
2000-04-08 07:20:22 +08:00
2002-05-04 22:27:48 +08:00
if ( ( ldap_value_len = ldap_get_values_len ( ld - > link , resultentry - > data , attribute ) ) = = NULL ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Cannot get the value(s) of attribute %s " , ldap_err2string ( _get_lderrno ( ld - > link ) ) ) ;
2000-04-08 07:20:22 +08:00
RETURN_FALSE ;
}
num_values = ldap_count_values_len ( ldap_value_len ) ;
2003-01-19 04:10:23 +08:00
array_init ( return_value ) ;
2000-04-08 07:20:22 +08:00
for ( i = 0 ; i < num_values ; i + + ) {
2000-07-29 05:48:16 +08:00
add_next_index_stringl ( return_value , ldap_value_len [ i ] - > bv_val , ldap_value_len [ i ] - > bv_len , 1 ) ;
2000-04-08 07:20:22 +08:00
}
add_assoc_long ( return_value , " count " , num_values ) ;
2000-07-28 08:09:33 +08:00
ldap_value_free_len ( ldap_value_len ) ;
2000-04-08 07:20:22 +08:00
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto string ldap_get_dn(resource link, resource result_entry)
1999-04-22 08:45:14 +08:00
Get the DN of a result entry */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_get_dn )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
1999-04-22 08:45:14 +08:00
char * text ;
2001-06-01 05:09:30 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result_entry ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
1999-04-22 08:45:14 +08:00
2002-05-04 22:27:48 +08:00
text = ldap_get_dn ( ld - > link , resultentry - > data ) ;
2003-06-09 07:34:51 +08:00
if ( text ! = NULL ) {
2001-08-12 00:39:07 +08:00
RETVAL_STRING ( text , 1 ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
1999-04-22 08:45:14 +08:00
ldap_memfree ( text ) ;
2001-07-15 19:12:28 +08:00
# else
free ( text ) ;
1999-04-22 08:45:14 +08:00
# endif
} else {
RETURN_FALSE ;
}
}
/* }}} */
/* {{{ proto array ldap_explode_dn(string dn, int with_attrib)
Splits DN into its component parts */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_explode_dn )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * dn , * * with_attrib ;
1999-07-17 02:01:55 +08:00
char * * ldap_value ;
int i , count ;
1999-04-22 08:45:14 +08:00
2001-08-12 00:39:07 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & dn , & with_attrib ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( dn ) ;
convert_to_long_ex ( with_attrib ) ;
1999-04-22 08:45:14 +08:00
2003-09-14 01:31:07 +08:00
if ( ! ( ldap_value = ldap_explode_dn ( Z_STRVAL_PP ( dn ) , Z_LVAL_PP ( with_attrib ) ) ) ) {
/* Invalid parameters were passed to ldap_explode_dn */
RETURN_FALSE ;
}
1999-04-22 08:45:14 +08:00
i = 0 ;
2003-06-09 07:34:51 +08:00
while ( ldap_value [ i ] ! = NULL ) i + + ;
1999-04-22 08:45:14 +08:00
count = i ;
2003-01-19 04:10:23 +08:00
array_init ( return_value ) ;
1999-04-22 08:45:14 +08:00
add_assoc_long ( return_value , " count " , count ) ;
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < count ; i + + ) {
1999-04-22 08:45:14 +08:00
add_index_string ( return_value , i , ldap_value [ i ] , 1 ) ;
}
ldap_value_free ( ldap_value ) ;
}
/* }}} */
/* {{{ proto string ldap_dn2ufn(string dn)
Convert DN to User Friendly Naming format */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_dn2ufn )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * dn ;
1999-04-22 08:45:14 +08:00
char * ufn ;
2001-08-12 00:39:07 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & dn ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( dn ) ;
1999-04-22 08:45:14 +08:00
2001-09-26 05:58:48 +08:00
ufn = ldap_dn2ufn ( Z_STRVAL_PP ( dn ) ) ;
1999-04-22 08:45:14 +08:00
if ( ufn ! = NULL ) {
2001-08-12 00:39:07 +08:00
RETVAL_STRING ( ufn , 1 ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || WINDOWS
1999-04-22 08:45:14 +08:00
ldap_memfree ( ufn ) ;
# endif
} else {
RETURN_FALSE ;
}
}
/* }}} */
1999-05-21 18:06:25 +08:00
2000-07-24 05:32:02 +08:00
2003-06-09 07:34:51 +08:00
/* added to fix use of ldap_modify_add for doing an ldap_add, gerrit thomson. */
1999-05-21 18:06:25 +08:00
# define PHP_LD_FULL_ADD 0xff
2001-06-05 21:12:10 +08:00
/* {{{ php_ldap_do_modify
*/
1999-12-18 19:49:31 +08:00
static void php_ldap_do_modify ( INTERNAL_FUNCTION_PARAMETERS , int oper )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * dn , * * entry , * * value , * * ivalue ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-07-17 02:01:55 +08:00
char * ldap_dn ;
LDAPMod * * ldap_mods ;
int i , j , num_attribs , num_values ;
2000-07-29 05:31:02 +08:00
int * num_berval ;
1999-07-17 02:01:55 +08:00
char * attribute ;
ulong index ;
int is_full_add = 0 ; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit THomson */
1999-05-21 18:06:25 +08:00
2001-06-01 05:09:30 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & dn , & entry ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-09-26 05:58:48 +08:00
if ( Z_TYPE_PP ( entry ) ! = IS_ARRAY ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Expected Array as the last element " ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
1999-04-22 08:45:14 +08:00
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( dn ) ;
2001-09-26 05:58:48 +08:00
ldap_dn = Z_STRVAL_PP ( dn ) ;
1999-04-22 08:45:14 +08:00
2001-09-26 05:58:48 +08:00
num_attribs = zend_hash_num_elements ( Z_ARRVAL_PP ( entry ) ) ;
2003-05-01 05:53:53 +08:00
ldap_mods = safe_emalloc ( ( num_attribs + 1 ) , sizeof ( LDAPMod * ) , 0 ) ;
num_berval = safe_emalloc ( num_attribs , sizeof ( int ) , 0 ) ;
2001-09-26 05:58:48 +08:00
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( entry ) ) ;
2000-07-29 05:31:02 +08:00
/* added by gerrit thomson to fix ldap_add using ldap_mod_add */
2003-06-09 07:34:51 +08:00
if ( oper = = PHP_LD_FULL_ADD ) {
2000-07-29 05:31:02 +08:00
oper = LDAP_MOD_ADD ;
is_full_add = 1 ;
}
1999-05-21 18:06:25 +08:00
/* end additional , gerrit thomson */
1999-04-22 08:45:14 +08:00
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_attribs ; i + + ) {
1999-04-22 08:45:14 +08:00
ldap_mods [ i ] = emalloc ( sizeof ( LDAPMod ) ) ;
2000-07-29 05:31:02 +08:00
ldap_mods [ i ] - > mod_op = oper | LDAP_MOD_BVALUES ;
1999-04-22 08:45:14 +08:00
2001-09-26 05:58:48 +08:00
if ( zend_hash_get_current_key ( Z_ARRVAL_PP ( entry ) , & attribute , & index , 0 ) = = HASH_KEY_IS_STRING ) {
1999-04-22 08:45:14 +08:00
ldap_mods [ i ] - > mod_type = estrdup ( attribute ) ;
} else {
2003-03-07 22:03:53 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown attribute in the data " ) ;
2001-01-13 18:00:27 +08:00
/* Free allocated memory */
while ( i > = 0 ) {
efree ( ldap_mods [ i - - ] ) ;
}
efree ( num_berval ) ;
efree ( ldap_mods ) ;
2000-11-13 10:02:40 +08:00
RETURN_FALSE ;
1999-04-22 08:45:14 +08:00
}
2001-09-26 05:58:48 +08:00
zend_hash_get_current_data ( Z_ARRVAL_PP ( entry ) , ( void * * ) & value ) ;
1999-04-22 08:45:14 +08:00
2001-09-26 05:58:48 +08:00
if ( Z_TYPE_PP ( value ) ! = IS_ARRAY ) {
1999-04-22 08:45:14 +08:00
num_values = 1 ;
} else {
2001-09-26 05:58:48 +08:00
num_values = zend_hash_num_elements ( Z_ARRVAL_PP ( value ) ) ;
1999-04-22 08:45:14 +08:00
}
2000-07-29 05:31:02 +08:00
num_berval [ i ] = num_values ;
2003-05-01 05:53:53 +08:00
ldap_mods [ i ] - > mod_bvalues = safe_emalloc ( ( num_values + 1 ) , sizeof ( struct berval * ) , 0 ) ;
1999-05-21 18:06:25 +08:00
/* allow for arrays with one element, no allowance for arrays with none but probably not required, gerrit thomson. */
2001-09-26 05:58:48 +08:00
if ( ( num_values = = 1 ) & & ( Z_TYPE_PP ( value ) ! = IS_ARRAY ) ) {
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( value ) ;
2000-07-29 05:31:02 +08:00
ldap_mods [ i ] - > mod_bvalues [ 0 ] = ( struct berval * ) emalloc ( sizeof ( struct berval ) ) ;
2001-09-26 05:58:48 +08:00
ldap_mods [ i ] - > mod_bvalues [ 0 ] - > bv_len = Z_STRLEN_PP ( value ) ;
ldap_mods [ i ] - > mod_bvalues [ 0 ] - > bv_val = Z_STRVAL_PP ( value ) ;
1999-04-22 08:45:14 +08:00
} else {
2003-06-09 07:34:51 +08:00
for ( j = 0 ; j < num_values ; j + + ) {
2001-11-05 19:50:52 +08:00
if ( zend_hash_index_find ( Z_ARRVAL_PP ( value ) , j , ( void * * ) & ivalue ) = = FAILURE ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Value array must have consecutive indices 0, 1, ... " ) ;
2001-11-05 19:50:52 +08:00
num_berval [ i ] = j ;
num_attribs = i + 1 ;
RETVAL_FALSE ;
goto errexit ;
}
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( ivalue ) ;
2000-07-29 05:31:02 +08:00
ldap_mods [ i ] - > mod_bvalues [ j ] = ( struct berval * ) emalloc ( sizeof ( struct berval ) ) ;
2001-09-26 05:58:48 +08:00
ldap_mods [ i ] - > mod_bvalues [ j ] - > bv_len = Z_STRLEN_PP ( ivalue ) ;
ldap_mods [ i ] - > mod_bvalues [ j ] - > bv_val = Z_STRVAL_PP ( ivalue ) ;
1999-04-22 08:45:14 +08:00
}
}
2000-07-29 05:31:02 +08:00
ldap_mods [ i ] - > mod_bvalues [ num_values ] = NULL ;
2001-09-26 05:58:48 +08:00
zend_hash_move_forward ( Z_ARRVAL_PP ( entry ) ) ;
1999-04-22 08:45:14 +08:00
}
ldap_mods [ num_attribs ] = NULL ;
1999-05-21 18:06:25 +08:00
/* check flag to see if do_mod was called to perform full add , gerrit thomson */
2001-11-14 01:10:08 +08:00
if ( is_full_add = = 1 ) {
2002-06-26 14:49:25 +08:00
if ( ( i = ldap_add_s ( ld - > link , ldap_dn , ldap_mods ) ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Add: %s " , ldap_err2string ( i ) ) ;
1999-04-22 08:45:14 +08:00
RETVAL_FALSE ;
} else RETVAL_TRUE ;
} else {
2002-06-26 14:49:25 +08:00
if ( ( i = ldap_modify_s ( ld - > link , ldap_dn , ldap_mods ) ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Modify: %s " , ldap_err2string ( i ) ) ;
1999-04-22 08:45:14 +08:00
RETVAL_FALSE ;
} else RETVAL_TRUE ;
}
2001-11-05 19:50:52 +08:00
errexit :
2003-06-09 07:34:51 +08:00
for ( i = 0 ; i < num_attribs ; i + + ) {
1999-04-22 08:45:14 +08:00
efree ( ldap_mods [ i ] - > mod_type ) ;
2003-06-09 07:34:51 +08:00
for ( j = 0 ; j < num_berval [ i ] ; j + + ) {
2000-07-29 05:31:02 +08:00
efree ( ldap_mods [ i ] - > mod_bvalues [ j ] ) ;
}
efree ( ldap_mods [ i ] - > mod_bvalues ) ;
1999-04-22 08:45:14 +08:00
efree ( ldap_mods [ i ] ) ;
}
2000-07-29 05:31:02 +08:00
efree ( num_berval ) ;
1999-04-22 08:45:14 +08:00
efree ( ldap_mods ) ;
return ;
}
2001-06-05 21:12:10 +08:00
/* }}} */
2000-07-24 05:32:02 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_add(resource link, string dn, array entry)
1999-04-22 08:45:14 +08:00
Add entries to LDAP directory */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_add )
1999-04-22 08:45:14 +08:00
{
1999-05-21 18:06:25 +08:00
/* 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 */
1999-12-18 19:49:31 +08:00
php_ldap_do_modify ( INTERNAL_FUNCTION_PARAM_PASSTHRU , PHP_LD_FULL_ADD ) ;
1999-04-22 08:45:14 +08:00
}
/* }}} */
1999-05-21 18:06:25 +08:00
/* three functions for attribute base modifications, gerrit Thomson */
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_mod_replace(resource link, string dn, array entry)
1999-05-21 18:06:25 +08:00
Replace attribute values with new ones */
PHP_FUNCTION ( ldap_mod_replace )
{
2001-06-01 05:09:30 +08:00
php_ldap_do_modify ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_MOD_REPLACE ) ;
}
1999-05-21 18:06:25 +08:00
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_mod_add(resource link, string dn, array entry)
2000-02-23 23:04:36 +08:00
Add attribute values to current */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_mod_add )
{
2001-06-01 05:09:30 +08:00
php_ldap_do_modify ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_MOD_ADD ) ;
1999-05-21 18:06:25 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_mod_del(resource link, string dn, array entry)
1999-05-21 18:06:25 +08:00
Delete attribute values */
PHP_FUNCTION ( ldap_mod_del )
{
2003-06-09 07:34:51 +08:00
php_ldap_do_modify ( INTERNAL_FUNCTION_PARAM_PASSTHRU , LDAP_MOD_DELETE ) ;
1999-05-21 18:06:25 +08:00
}
2000-07-24 05:32:02 +08:00
/* }}} */
1999-05-21 18:06:25 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_delete(resource link, string dn)
1999-04-22 08:45:14 +08:00
Delete an entry from a directory */
1999-05-21 18:06:25 +08:00
PHP_FUNCTION ( ldap_delete )
1999-04-22 08:45:14 +08:00
{
2003-06-09 07:34:51 +08:00
zval * * link , * * dn ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
1999-07-17 02:01:55 +08:00
char * ldap_dn ;
2002-06-26 14:49:25 +08:00
int rc ;
1999-04-22 08:45:14 +08:00
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & dn ) = = FAILURE ) {
1999-04-22 08:45:14 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
1999-04-22 08:45:14 +08:00
1999-11-22 10:31:32 +08:00
convert_to_string_ex ( dn ) ;
2001-09-26 05:58:48 +08:00
ldap_dn = Z_STRVAL_PP ( dn ) ;
1999-04-22 08:45:14 +08:00
2002-06-26 14:49:25 +08:00
if ( ( rc = ldap_delete_s ( ld - > link , ldap_dn ) ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Delete: %s " , ldap_err2string ( rc ) ) ;
1999-04-22 08:45:14 +08:00
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto int ldap_errno(resource link)
2000-04-06 20:36:47 +08:00
Get the current ldap error number */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_errno )
{
2003-06-09 07:34:51 +08:00
zval * * link ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2000-04-06 20:36:47 +08:00
2001-06-01 05:09:30 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( ht , & link ) = = FAILURE ) {
2000-04-06 20:36:47 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
2003-06-09 07:34:51 +08:00
RETURN_LONG ( _get_lderrno ( ld - > link ) ) ;
2000-04-06 20:36:47 +08:00
}
/* }}} */
/* {{{ proto string ldap_err2str(int errno)
Convert error number to error string */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_err2str )
{
zval * * perrno ;
2000-04-06 20:36:47 +08:00
2003-06-09 07:34:51 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( ht , & perrno ) = = FAILURE ) {
2000-04-06 20:36:47 +08:00
WRONG_PARAM_COUNT ;
}
2001-06-01 05:09:30 +08:00
2000-04-06 20:36:47 +08:00
convert_to_long_ex ( perrno ) ;
2001-09-26 05:58:48 +08:00
RETURN_STRING ( ldap_err2string ( Z_LVAL_PP ( perrno ) ) , 1 ) ;
2000-04-06 20:36:47 +08:00
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto string ldap_error(resource link)
2000-04-06 20:36:47 +08:00
Get the current ldap error string */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_error )
{
2003-06-09 07:34:51 +08:00
zval * * link ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2000-04-06 20:36:47 +08:00
int ld_errno ;
2000-06-06 03:47:54 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( ht , & link ) = = FAILURE ) {
2000-04-06 20:36:47 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2000-04-06 20:36:47 +08:00
2001-11-30 04:17:17 +08:00
ld_errno = _get_lderrno ( ld - > link ) ;
2000-04-06 20:36:47 +08:00
RETURN_STRING ( ldap_err2string ( ld_errno ) , 1 ) ;
}
2000-04-07 00:57:33 +08:00
/* }}} */
2000-07-26 19:01:44 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_compare(resource link, string dn, string attr, string value)
2000-07-26 19:28:14 +08:00
Determine if an entry has a specific value for one of its attributes */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_compare )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * dn , * * attr , * * value ;
2000-07-26 19:01:44 +08:00
char * ldap_dn , * ldap_attr , * ldap_value ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2000-07-26 19:01:44 +08:00
int errno ;
if ( ZEND_NUM_ARGS ( ) ! = 4 | | zend_get_parameters_ex ( 4 , & link , & dn , & attr , & value ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
2000-07-26 19:01:44 +08:00
convert_to_string_ex ( dn ) ;
convert_to_string_ex ( attr ) ;
convert_to_string_ex ( value ) ;
2001-09-26 05:58:48 +08:00
ldap_dn = Z_STRVAL_PP ( dn ) ;
ldap_attr = Z_STRVAL_PP ( attr ) ;
ldap_value = Z_STRVAL_PP ( value ) ;
2000-07-26 19:01:44 +08:00
2001-11-30 04:17:17 +08:00
errno = ldap_compare_s ( ld - > link , ldap_dn , ldap_attr , ldap_value ) ;
2000-07-26 19:01:44 +08:00
2003-06-09 07:34:51 +08:00
switch ( errno ) {
case LDAP_COMPARE_TRUE :
2000-07-26 19:01:44 +08:00
RETURN_TRUE ;
2003-06-09 07:34:51 +08:00
break ;
2000-07-26 19:01:44 +08:00
2003-06-09 07:34:51 +08:00
case LDAP_COMPARE_FALSE :
2000-07-26 19:01:44 +08:00
RETURN_FALSE ;
2003-06-09 07:34:51 +08:00
break ;
2000-07-26 19:01:44 +08:00
}
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Compare: %s " , ldap_err2string ( errno ) ) ;
2000-07-26 19:01:44 +08:00
RETURN_LONG ( - 1 ) ;
}
/* }}} */
2000-10-17 23:51:04 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_sort(resource link, resource result, string sortfilter)
2001-11-22 04:14:17 +08:00
Sort LDAP result entries */
PHP_FUNCTION ( ldap_sort )
{
zval * link , * result ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2001-11-22 04:14:17 +08:00
char * sortfilter ;
int sflen ;
zend_rsrc_list_entry * le ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " rrs " , & link , & result , & sortfilter , & sflen ) = = FAILURE ) {
RETURN_FALSE ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , & link , - 1 , " ldap link " , le_link ) ;
2001-11-22 04:14:17 +08:00
if ( zend_hash_index_find ( & EG ( regular_list ) , Z_LVAL_P ( result ) , ( void * * ) & le ) = = FAILURE | | le - > type ! = le_result ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Supplied resource is not a valid ldap result resource " ) ;
2001-11-22 04:14:17 +08:00
RETURN_FALSE ;
}
2001-11-30 04:17:17 +08:00
if ( ldap_sort_entries ( ld - > link , ( LDAPMessage * * ) & le - > ptr , sflen ? sortfilter : NULL , strcmp ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %s " , ldap_err2string ( errno ) ) ;
2001-11-22 04:14:17 +08:00
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2000-08-02 21:11:46 +08:00
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_get_option(resource link, int option, mixed retval)
2000-10-18 00:06:04 +08:00
Get the current value of various session - wide parameters */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_get_option )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * option , * * retval ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2000-10-18 00:06:04 +08:00
int opt ;
2003-06-09 07:34:51 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & option , & retval ) = = FAILURE ) {
2000-10-18 00:06:04 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2000-10-18 00:06:04 +08:00
convert_to_long_ex ( option ) ;
2001-09-26 05:58:48 +08:00
opt = Z_LVAL_PP ( option ) ;
2000-10-18 00:06:04 +08:00
2003-06-09 07:34:51 +08:00
switch ( opt ) {
/* options with int value */
2000-10-18 00:06:04 +08:00
case LDAP_OPT_DEREF :
case LDAP_OPT_SIZELIMIT :
case LDAP_OPT_TIMELIMIT :
case LDAP_OPT_PROTOCOL_VERSION :
case LDAP_OPT_ERROR_NUMBER :
case LDAP_OPT_REFERRALS :
2002-07-10 04:54:49 +08:00
# ifdef LDAP_OPT_RESTART
2000-10-18 00:06:04 +08:00
case LDAP_OPT_RESTART :
2002-07-10 04:54:49 +08:00
# endif
2000-10-18 00:06:04 +08:00
{
int val ;
2001-11-30 04:17:17 +08:00
if ( ldap_get_option ( ld - > link , opt , & val ) ) {
2000-10-18 00:06:04 +08:00
RETURN_FALSE ;
}
2000-10-18 15:47:51 +08:00
zval_dtor ( * retval ) ;
2003-06-09 07:34:51 +08:00
ZVAL_LONG ( * retval , val ) ;
2000-10-18 00:06:04 +08:00
} break ;
2003-06-09 07:34:51 +08:00
/* options with string value */
2000-10-18 00:06:04 +08:00
case LDAP_OPT_HOST_NAME :
case LDAP_OPT_ERROR_STRING :
2001-01-08 05:18:02 +08:00
# ifdef LDAP_OPT_MATCHED_DN
2000-10-18 00:06:04 +08:00
case LDAP_OPT_MATCHED_DN :
2000-11-04 06:53:49 +08:00
# endif
2003-06-09 07:34:51 +08:00
{
2003-10-07 07:33:00 +08:00
char * val = NULL ;
2003-10-07 08:36:31 +08:00
if ( ldap_get_option ( ld - > link , opt , & val ) | | val = = NULL | | * val = = ' \0 ' ) {
if ( val ) {
ldap_memfree ( val ) ;
2003-10-07 07:33:00 +08:00
}
RETURN_FALSE ;
}
2003-10-07 08:36:31 +08:00
zval_dtor ( * retval ) ;
ZVAL_STRING ( * retval , val , 1 ) ;
ldap_memfree ( val ) ;
2000-10-18 00:06:04 +08:00
} break ;
2000-10-29 03:22:46 +08:00
/* options not implemented
2000-10-18 00:06:04 +08:00
case LDAP_OPT_SERVER_CONTROLS :
case LDAP_OPT_CLIENT_CONTROLS :
case LDAP_OPT_API_INFO :
case LDAP_OPT_API_FEATURE_INFO :
2000-10-29 03:22:46 +08:00
*/
2000-10-18 00:06:04 +08:00
default :
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_set_option(resource link, int option, mixed newval)
2000-10-18 00:06:04 +08:00
Set the value of various session - wide parameters */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_set_option )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * option , * * newval ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2001-12-24 00:32:06 +08:00
LDAP * ldap ;
2000-10-18 00:06:04 +08:00
int opt ;
2003-06-09 07:34:51 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & option , & newval ) = = FAILURE ) {
2000-10-18 00:06:04 +08:00
WRONG_PARAM_COUNT ;
}
2001-12-24 00:32:06 +08:00
if ( Z_TYPE_PP ( link ) = = IS_NULL ) {
ldap = NULL ;
} else {
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
ldap = ld - > link ;
}
2001-06-01 05:09:30 +08:00
2000-10-18 00:06:04 +08:00
convert_to_long_ex ( option ) ;
2001-09-26 05:58:48 +08:00
opt = Z_LVAL_PP ( option ) ;
2000-10-18 00:06:04 +08:00
2003-06-09 07:34:51 +08:00
switch ( opt ) {
2000-10-18 00:06:04 +08:00
/* options with int value */
case LDAP_OPT_DEREF :
case LDAP_OPT_SIZELIMIT :
case LDAP_OPT_TIMELIMIT :
case LDAP_OPT_PROTOCOL_VERSION :
case LDAP_OPT_ERROR_NUMBER :
2001-12-24 00:32:06 +08:00
# ifdef LDAP_OPT_DEBUG_LEVEL
case LDAP_OPT_DEBUG_LEVEL :
# endif
2000-10-18 00:06:04 +08:00
{
int val ;
convert_to_long_ex ( newval ) ;
2001-09-26 05:58:48 +08:00
val = Z_LVAL_PP ( newval ) ;
2001-12-24 00:32:06 +08:00
if ( ldap_set_option ( ldap , opt , & val ) ) {
2000-10-18 00:06:04 +08:00
RETURN_FALSE ;
}
} break ;
/* options with string value */
case LDAP_OPT_HOST_NAME :
case LDAP_OPT_ERROR_STRING :
2001-01-09 02:13:08 +08:00
# ifdef LDAP_OPT_MATCHED_DN
2000-10-18 00:06:04 +08:00
case LDAP_OPT_MATCHED_DN :
2000-11-04 06:53:49 +08:00
# endif
2000-10-18 00:06:04 +08:00
{
char * val ;
convert_to_string_ex ( newval ) ;
2001-09-26 05:58:48 +08:00
val = Z_STRVAL_PP ( newval ) ;
2001-12-24 00:32:06 +08:00
if ( ldap_set_option ( ldap , opt , val ) ) {
2000-10-18 00:06:04 +08:00
RETURN_FALSE ;
}
} break ;
/* options with boolean value */
case LDAP_OPT_REFERRALS :
2002-07-10 04:54:49 +08:00
# ifdef LDAP_OPT_RESTART
2000-10-18 00:06:04 +08:00
case LDAP_OPT_RESTART :
2002-07-10 04:54:49 +08:00
# endif
2000-10-18 00:06:04 +08:00
{
void * val ;
convert_to_boolean_ex ( newval ) ;
2001-09-26 05:58:48 +08:00
val = Z_LVAL_PP ( newval )
2000-10-18 00:06:04 +08:00
? LDAP_OPT_ON : LDAP_OPT_OFF ;
2001-12-24 00:32:06 +08:00
if ( ldap_set_option ( ldap , opt , val ) ) {
2000-10-18 00:06:04 +08:00
RETURN_FALSE ;
}
} break ;
2001-01-08 05:18:02 +08:00
/* options with control list value */
2000-10-18 00:06:04 +08:00
case LDAP_OPT_SERVER_CONTROLS :
case LDAP_OPT_CLIENT_CONTROLS :
2001-01-08 05:18:02 +08:00
{
LDAPControl * ctrl , * * ctrls , * * ctrlp ;
zval * * ctrlval , * * val ;
int ncontrols ;
char error = 0 ;
if ( ( Z_TYPE_PP ( newval ) ! = IS_ARRAY ) | | ! ( ncontrols = zend_hash_num_elements ( Z_ARRVAL_PP ( newval ) ) ) ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Expected non-empty array value for this option " ) ;
2003-06-09 07:34:51 +08:00
RETURN_FALSE ;
}
2003-05-01 05:53:53 +08:00
ctrls = safe_emalloc ( ( 1 + ncontrols ) , sizeof ( * ctrls ) , 0 ) ;
2001-01-08 05:18:02 +08:00
* ctrls = NULL ;
ctrlp = ctrls ;
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( newval ) ) ;
while ( zend_hash_get_current_data ( Z_ARRVAL_PP ( newval ) , ( void * * ) & ctrlval ) = = SUCCESS ) {
if ( Z_TYPE_PP ( ctrlval ) ! = IS_ARRAY ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " The array value must contain only arrays, where each array is a control " ) ;
2001-01-08 05:18:02 +08:00
error = 1 ;
break ;
}
if ( zend_hash_find ( Z_ARRVAL_PP ( ctrlval ) , " oid " , sizeof ( " oid " ) , ( void * * ) & val ) = = FAILURE ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Control must have an oid key " ) ;
2001-01-08 05:18:02 +08:00
error = 1 ;
break ;
}
ctrl = * ctrlp = emalloc ( sizeof ( * * ctrlp ) ) ;
convert_to_string_ex ( val ) ;
ctrl - > ldctl_oid = Z_STRVAL_PP ( val ) ;
if ( zend_hash_find ( Z_ARRVAL_PP ( ctrlval ) , " value " , sizeof ( " value " ) , ( void * * ) & val ) = = SUCCESS ) {
convert_to_string_ex ( val ) ;
ctrl - > ldctl_value . bv_val = Z_STRVAL_PP ( val ) ;
ctrl - > ldctl_value . bv_len = Z_STRLEN_PP ( val ) ;
} else {
ctrl - > ldctl_value . bv_val = NULL ;
ctrl - > ldctl_value . bv_len = 0 ;
}
if ( zend_hash_find ( Z_ARRVAL_PP ( ctrlval ) , " iscritical " , sizeof ( " iscritical " ) , ( void * * ) & val ) = = SUCCESS ) {
convert_to_boolean_ex ( val ) ;
ctrl - > ldctl_iscritical = Z_BVAL_PP ( val ) ;
} else {
ctrl - > ldctl_iscritical = 0 ;
}
+ + ctrlp ;
* ctrlp = NULL ;
zend_hash_move_forward ( Z_ARRVAL_PP ( newval ) ) ;
}
if ( ! error ) {
2001-12-24 00:32:06 +08:00
error = ldap_set_option ( ldap , opt , ctrls ) ;
2001-01-08 05:18:02 +08:00
}
ctrlp = ctrls ;
2003-06-09 07:34:51 +08:00
while ( * ctrlp ) {
2001-01-08 05:18:02 +08:00
efree ( * ctrlp ) ;
ctrlp + + ;
}
efree ( ctrls ) ;
if ( error ) {
RETURN_FALSE ;
}
} break ;
2000-10-18 00:06:04 +08:00
default :
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2001-01-07 21:03:11 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_parse_result(resource link, resource result, int errcode, string matcheddn, string errmsg, array referrals)
2001-01-07 21:03:11 +08:00
Extract information from result */
2001-06-01 05:09:30 +08:00
PHP_FUNCTION ( ldap_parse_result )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result , * * errcode , * * matcheddn , * * errmsg , * * referrals ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2001-01-07 21:03:11 +08:00
LDAPMessage * ldap_result ;
char * * lreferrals , * * refp ;
char * lmatcheddn , * lerrmsg ;
2001-06-01 05:09:30 +08:00
int rc , lerrcode , myargcount = ZEND_NUM_ARGS ( ) ;
2003-06-09 07:34:51 +08:00
2001-01-07 21:03:11 +08:00
if ( myargcount < 3 | | myargcount > 6 | | zend_get_parameters_ex ( myargcount , & link , & result , & errcode , & matcheddn , & errmsg , & referrals ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
2001-01-07 21:03:11 +08:00
2003-06-09 07:34:51 +08:00
rc = ldap_parse_result ( ld - > link , ldap_result , & lerrcode ,
2001-01-07 21:03:11 +08:00
myargcount > 3 ? & lmatcheddn : NULL ,
myargcount > 4 ? & lerrmsg : NULL ,
myargcount > 5 ? & lreferrals : NULL ,
NULL /* &serverctrls */ ,
2003-06-09 07:34:51 +08:00
0 ) ;
if ( rc ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unable to parse result: %s " , ldap_err2string ( rc ) ) ;
2001-01-07 21:03:11 +08:00
RETURN_FALSE ;
}
zval_dtor ( * errcode ) ;
ZVAL_LONG ( * errcode , lerrcode ) ;
/* Reverse -> fall through */
2003-06-09 07:34:51 +08:00
switch ( myargcount ) {
case 6 :
2001-01-07 21:03:11 +08:00
zval_dtor ( * referrals ) ;
2003-01-19 04:10:23 +08:00
array_init ( * referrals ) ;
2001-01-07 21:03:11 +08:00
if ( lreferrals ! = NULL ) {
refp = lreferrals ;
while ( * refp ) {
add_next_index_string ( * referrals , * refp , 1 ) ;
refp + + ;
}
ldap_value_free ( lreferrals ) ;
}
2003-06-09 07:34:51 +08:00
case 5 :
2001-01-07 21:03:11 +08:00
zval_dtor ( * errmsg ) ;
if ( lerrmsg = = NULL ) {
ZVAL_EMPTY_STRING ( * errmsg ) ;
} else {
ZVAL_STRING ( * errmsg , lerrmsg , 1 ) ;
ldap_memfree ( lerrmsg ) ;
}
2003-06-09 07:34:51 +08:00
case 4 :
2001-01-07 21:03:11 +08:00
zval_dtor ( * matcheddn ) ;
if ( lmatcheddn = = NULL ) {
ZVAL_EMPTY_STRING ( * matcheddn ) ;
} else {
ZVAL_STRING ( * matcheddn , lmatcheddn , 1 ) ;
ldap_memfree ( lmatcheddn ) ;
}
}
RETURN_TRUE ;
}
/* }}} */
2002-02-11 10:48:49 +08:00
/* {{{ proto resource ldap_first_reference(resource link, resource result)
2001-01-07 21:03:11 +08:00
Return first reference */
PHP_FUNCTION ( ldap_first_reference )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
LDAPMessage * ldap_result , * entry ;
2001-01-07 21:03:11 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-06-01 05:09:30 +08:00
ZEND_FETCH_RESOURCE ( ldap_result , LDAPMessage * , result , - 1 , " ldap result " , le_result ) ;
2001-01-07 21:03:11 +08:00
2002-05-04 22:27:48 +08:00
if ( ( entry = ldap_first_reference ( ld - > link , ldap_result ) ) = = NULL ) {
2001-06-01 05:09:30 +08:00
RETVAL_FALSE ;
2001-01-07 21:03:11 +08:00
} else {
2002-05-04 22:27:48 +08:00
resultentry = emalloc ( sizeof ( ldap_resultentry ) ) ;
ZEND_REGISTER_RESOURCE ( return_value , resultentry , le_result_entry ) ;
resultentry - > id = Z_LVAL_PP ( result ) ;
zend_list_addref ( resultentry - > id ) ;
resultentry - > data = entry ;
2001-01-07 21:03:11 +08:00
}
}
/* }}} */
2002-05-04 22:27:48 +08:00
/* {{{ proto resource ldap_next_reference(resource link, resource reference_entry)
2001-01-07 21:03:11 +08:00
Get next reference */
PHP_FUNCTION ( ldap_next_reference )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry , * resultentry_next ;
LDAPMessage * entry_next ;
2001-01-07 21:03:11 +08:00
2001-06-01 05:09:30 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & link , & result_entry ) = = FAILURE ) {
2001-01-07 21:03:11 +08:00
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
2001-01-07 21:03:11 +08:00
2002-05-04 22:27:48 +08:00
if ( ( entry_next = ldap_next_reference ( ld - > link , resultentry - > data ) ) = = NULL ) {
2001-06-01 05:09:30 +08:00
RETVAL_FALSE ;
2001-01-07 21:03:11 +08:00
} else {
2002-05-04 22:27:48 +08:00
resultentry_next = emalloc ( sizeof ( ldap_resultentry ) ) ;
ZEND_REGISTER_RESOURCE ( return_value , resultentry_next , le_result_entry ) ;
resultentry_next - > id = resultentry - > id ;
zend_list_addref ( resultentry - > id ) ;
resultentry_next - > data = entry_next ;
2001-01-07 21:03:11 +08:00
}
}
/* }}} */
2002-06-22 23:41:39 +08:00
# ifdef HAVE_LDAP_PARSE_REFERENCE
2002-05-04 22:27:48 +08:00
/* {{{ proto bool ldap_parse_reference(resource link, resource reference_entry, array referrals)
2001-01-07 21:03:11 +08:00
Extract information from reference entry */
PHP_FUNCTION ( ldap_parse_reference )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * result_entry , * * referrals ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-05-04 22:27:48 +08:00
ldap_resultentry * resultentry ;
2001-01-07 21:03:11 +08:00
char * * lreferrals , * * refp ;
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & link , & result_entry , & referrals ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2002-05-04 22:27:48 +08:00
ZEND_FETCH_RESOURCE ( resultentry , ldap_resultentry * , result_entry , - 1 , " ldap result entry " , le_result_entry ) ;
2001-01-07 21:03:11 +08:00
2002-05-04 22:27:48 +08:00
if ( ldap_parse_reference ( ld - > link , resultentry - > data , & lreferrals , NULL /* &serverctrls */ , 0 ) ! = LDAP_SUCCESS ) {
2001-01-07 21:03:11 +08:00
RETURN_FALSE ;
}
zval_dtor ( * referrals ) ;
2003-01-19 04:10:23 +08:00
array_init ( * referrals ) ;
2001-01-07 21:03:11 +08:00
if ( lreferrals ! = NULL ) {
refp = lreferrals ;
while ( * refp ) {
add_next_index_string ( * referrals , * refp , 1 ) ;
refp + + ;
}
ldap_value_free ( lreferrals ) ;
}
RETURN_TRUE ;
}
/* }}} */
2002-06-22 23:41:39 +08:00
# endif
2001-01-09 02:13:08 +08:00
2002-08-13 12:44:45 +08:00
/* {{{ proto bool ldap_rename(resource link, string dn, string newrdn, string newparent, bool deleteoldrdn);
2001-01-09 02:13:08 +08:00
Modify the name of an entry */
PHP_FUNCTION ( ldap_rename )
{
2003-06-09 07:34:51 +08:00
zval * * link , * * dn , * * newrdn , * * newparent , * * deleteoldrdn ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2001-01-09 02:13:08 +08:00
int rc ;
if ( ZEND_NUM_ARGS ( ) ! = 5 | | zend_get_parameters_ex ( 5 , & link , & dn , & newrdn , & newparent , & deleteoldrdn ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-01-09 02:13:08 +08:00
convert_to_string_ex ( dn ) ;
convert_to_string_ex ( newrdn ) ;
convert_to_string_ex ( newparent ) ;
convert_to_boolean_ex ( deleteoldrdn ) ;
2003-06-09 07:34:51 +08:00
# if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP
2001-11-30 04:17:17 +08:00
rc = ldap_rename_s ( ld - > link , Z_STRVAL_PP ( dn ) , Z_STRVAL_PP ( newrdn ) , Z_STRVAL_PP ( newparent ) , Z_BVAL_PP ( deleteoldrdn ) , NULL , NULL ) ;
2001-01-09 02:13:08 +08:00
# else
if ( Z_STRLEN_PP ( newparent ) ! = 0 ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " You are using old LDAP API, newparent must be the empty string, can only modify RDN " ) ;
2001-01-09 02:13:08 +08:00
RETURN_FALSE ;
}
/* could support old APIs but need check for ldap_modrdn2()/ldap_modrdn() */
2001-11-30 04:17:17 +08:00
rc = ldap_modrdn2_s ( ld - > link , Z_STRVAL_PP ( dn ) , Z_STRVAL_PP ( newrdn ) , Z_BVAL_PP ( deleteoldrdn ) ) ;
2001-01-09 02:13:08 +08:00
# endif
if ( rc = = LDAP_SUCCESS ) {
RETURN_TRUE ;
}
RETURN_FALSE ;
}
/* }}} */
2000-10-18 00:06:04 +08:00
2002-06-22 23:41:39 +08:00
# ifdef HAVE_LDAP_START_TLS_S
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_start_tls(resource link)
2001-10-10 05:14:14 +08:00
Start TLS */
PHP_FUNCTION ( ldap_start_tls )
{
2003-06-09 07:34:51 +08:00
zval * * link ;
2001-11-30 04:17:17 +08:00
ldap_linkdata * ld ;
2002-06-26 14:49:25 +08:00
int rc ;
2001-10-10 05:14:14 +08:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & link ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
2001-11-30 04:17:17 +08:00
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , link , - 1 , " ldap link " , le_link ) ;
2001-10-10 05:14:14 +08:00
2002-06-26 14:49:25 +08:00
if ( ( rc = ldap_start_tls_s ( ld - > link , NULL , NULL ) ) ! = LDAP_SUCCESS ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unable to start TLS: %s " , ldap_err2string ( rc ) ) ;
2001-10-10 05:14:14 +08:00
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
}
/* }}} */
# endif
2003-06-09 07:34:51 +08:00
# endif /* (LDAP_API_VERSION > 2000) || HAVE_NSLDAP */
2001-10-10 05:14:14 +08:00
2001-12-24 00:32:06 +08:00
# if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
2003-06-09 07:34:51 +08:00
/* {{{ proto _ldap_rebind_proc()
*/
int _ldap_rebind_proc ( LDAP * ldap , const char * url , ber_tag_t req , ber_int_t msgid , void * params )
{
2001-11-30 04:26:20 +08:00
ldap_linkdata * ld ;
int retval ;
zval * cb_url ;
zval * * cb_args [ 2 ] ;
zval * cb_retval ;
zval * cb_link = ( zval * ) params ;
2002-06-22 06:22:46 +08:00
TSRMLS_FETCH ( ) ;
2001-11-30 04:26:20 +08:00
ld = ( ldap_linkdata * ) zend_fetch_resource ( & cb_link TSRMLS_CC , - 1 , " ldap link " , NULL , 1 , le_link ) ;
/* link exists and callback set? */
if ( ld = = NULL | | ld - > rebindproc = = NULL ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Link not found or no callback set " ) ;
2001-11-30 04:26:20 +08:00
return LDAP_OTHER ;
}
/* callback */
MAKE_STD_ZVAL ( cb_url ) ;
2003-06-09 07:34:51 +08:00
ZVAL_STRING ( cb_url , estrdup ( url ) , 0 ) ;
2001-11-30 04:26:20 +08:00
cb_args [ 0 ] = & cb_link ;
cb_args [ 1 ] = & cb_url ;
2002-04-24 03:01:31 +08:00
if ( call_user_function_ex ( EG ( function_table ) , NULL , ld - > rebindproc , & cb_retval , 2 , cb_args , 0 , NULL TSRMLS_CC ) = = SUCCESS & & cb_retval ) {
2001-11-30 04:26:20 +08:00
convert_to_long_ex ( & cb_retval ) ;
retval = Z_LVAL_P ( cb_retval ) ;
zval_ptr_dtor ( & cb_retval ) ;
} else {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " rebind_proc PHP callback failed " ) ;
2001-11-30 04:26:20 +08:00
retval = LDAP_OTHER ;
}
zval_dtor ( cb_url ) ;
FREE_ZVAL ( cb_url ) ;
return retval ;
}
2003-06-09 07:34:51 +08:00
/* }}} */
2001-11-30 04:26:20 +08:00
2002-02-11 10:48:49 +08:00
/* {{{ proto bool ldap_set_rebind_proc(resource link, string callback)
2001-11-30 04:26:20 +08:00
Set a callback function to do re - binds on referral chasing . */
PHP_FUNCTION ( ldap_set_rebind_proc )
{
zval * link , * callback ;
ldap_linkdata * ld ;
char * callback_name ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " rz " , & link , & callback ) = = FAILURE ) {
RETURN_FALSE ;
}
ZEND_FETCH_RESOURCE ( ld , ldap_linkdata * , & link , - 1 , " ldap link " , le_link ) ;
if ( Z_TYPE_P ( callback ) = = IS_STRING & & Z_STRLEN_P ( callback ) = = 0 ) {
/* unregister rebind procedure */
if ( ld - > rebindproc ! = NULL ) {
zval_dtor ( ld - > rebindproc ) ;
ld - > rebindproc = NULL ;
ldap_set_rebind_proc ( ld - > link , NULL , NULL ) ;
}
RETURN_TRUE ;
}
/* callable? */
if ( ! zend_is_callable ( callback , 0 , & callback_name ) ) {
2003-01-19 05:31:11 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Two arguments expected for '%s' to be a valid callback " , callback_name ) ;
2001-11-30 04:26:20 +08:00
efree ( callback_name ) ;
RETURN_FALSE ;
}
efree ( callback_name ) ;
/* register rebind procedure */
if ( ld - > rebindproc = = NULL ) {
ldap_set_rebind_proc ( ld - > link , _ldap_rebind_proc , ( void * ) link ) ;
} else {
zval_dtor ( ld - > rebindproc ) ;
}
ALLOC_ZVAL ( ld - > rebindproc ) ;
* ld - > rebindproc = * callback ;
zval_copy_ctor ( ld - > rebindproc ) ;
RETURN_TRUE ;
}
2003-06-09 07:34:51 +08:00
/* }}} */
2001-11-30 04:26:20 +08:00
# endif
2000-08-02 21:11:46 +08:00
# ifdef STR_TRANSLATION
2001-06-05 21:12:10 +08:00
/* {{{ php_ldap_do_translate
*/
2001-06-01 05:09:30 +08:00
static void php_ldap_do_translate ( INTERNAL_FUNCTION_PARAMETERS , int way )
{
2000-08-02 21:11:46 +08:00
zval * * value ;
char * ldap_buf ;
unsigned long ldap_len ;
int result ;
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & value ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( value ) ;
2001-09-26 05:58:48 +08:00
ldap_buf = Z_STRVAL_PP ( value ) ;
ldap_len = Z_STRLEN_PP ( value ) ;
2000-08-02 21:11:46 +08:00
2003-06-09 07:34:51 +08:00
if ( ldap_len = = 0 ) {
2000-08-02 21:11:46 +08:00
RETURN_FALSE ;
}
2003-06-09 07:34:51 +08:00
if ( way = = 1 ) {
2000-08-02 21:11:46 +08:00
result = ldap_8859_to_t61 ( & ldap_buf , & ldap_len , 0 ) ;
} else {
result = ldap_t61_to_8859 ( & ldap_buf , & ldap_len , 0 ) ;
}
if ( result = = LDAP_SUCCESS ) {
2001-08-12 00:39:07 +08:00
RETVAL_STRINGL ( ldap_buf , ldap_len , 1 ) ;
2000-08-02 21:11:46 +08:00
free ( ldap_buf ) ;
} else {
2003-03-07 22:03:53 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Conversion from iso-8859-1 to t61 failed: %s " , ldap_err2string ( result ) ) ;
2000-08-02 21:11:46 +08:00
RETVAL_FALSE ;
}
return ;
}
2001-06-05 21:12:10 +08:00
/* }}} */
2000-08-02 21:11:46 +08:00
/* {{{ proto string ldap_t61_to_8859(string value)
Translate t61 characters to 8859 characters */
2001-08-05 09:43:02 +08:00
PHP_FUNCTION ( ldap_t61_to_8859 )
{
2000-08-02 21:11:46 +08:00
php_ldap_do_translate ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
}
/* }}} */
/* {{{ proto string ldap_8859_to_t61(string value)
Translate 8859 characters to t61 characters */
2001-08-05 09:43:02 +08:00
PHP_FUNCTION ( ldap_8859_to_t61 )
{
2000-08-02 21:11:46 +08:00
php_ldap_do_translate ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
}
/* }}} */
# endif
2001-06-05 21:12:10 +08:00
/*
* Local variables :
* tab - width : 4
* c - basic - offset : 4
* End :
2001-09-09 21:29:31 +08:00
* vim600 : sw = 4 ts = 4 fdm = marker
* vim < 600 : sw = 4 ts = 4
2001-06-05 21:12:10 +08:00
*/