📄 ldap.c
字号:
{ zval **link, **result_entry, **attr; ldap_linkdata *ld; ldap_resultentry *resultentry; char *attribute; char **ldap_value; int i, num_values; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &result_entry, &attr) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry); convert_to_string_ex(attr); attribute = Z_STRVAL_PP(attr); if ((ldap_value = ldap_get_values(ld->link, resultentry->data, attribute)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link))); RETURN_FALSE; } num_values = ldap_count_values(ldap_value); array_init(return_value); for (i = 0; i<num_values; i++) { add_next_index_string(return_value, ldap_value[i], 1); } add_assoc_long(return_value, "count", num_values); ldap_value_free(ldap_value);}/* }}} *//* {{{ proto array ldap_get_values_len(resource link, resource result_entry, string attribute) Get all values with lengths from a result entry */PHP_FUNCTION(ldap_get_values_len){ zval **link, **result_entry, **attr; ldap_linkdata *ld; ldap_resultentry *resultentry; char* attribute; struct berval **ldap_value_len; int i, num_values; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &result_entry, &attr) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry); convert_to_string_ex(attr); attribute = Z_STRVAL_PP(attr); if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attribute)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link))); RETURN_FALSE; } num_values = ldap_count_values_len(ldap_value_len); array_init(return_value); for (i=0; i<num_values; i++) { add_next_index_stringl(return_value, ldap_value_len[i]->bv_val, ldap_value_len[i]->bv_len, 1); } add_assoc_long(return_value, "count", num_values); ldap_value_free_len(ldap_value_len);}/* }}} *//* {{{ proto string ldap_get_dn(resource link, resource result_entry) Get the DN of a result entry */PHP_FUNCTION(ldap_get_dn) { zval **link, **result_entry; ldap_linkdata *ld; ldap_resultentry *resultentry; char *text; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result_entry) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry); text = ldap_get_dn(ld->link, resultentry->data); if (text != NULL) { RETVAL_STRING(text, 1);#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS ldap_memfree(text);#else free(text);#endif } else { RETURN_FALSE; }}/* }}} *//* {{{ proto array ldap_explode_dn(string dn, int with_attrib) Splits DN into its component parts */PHP_FUNCTION(ldap_explode_dn){ zval **dn, **with_attrib; char **ldap_value; int i, count; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &dn, &with_attrib) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(dn); convert_to_long_ex(with_attrib); 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; } i=0; while (ldap_value[i] != NULL) i++; count = i; array_init(return_value); add_assoc_long(return_value, "count", count); for (i = 0; i<count; i++) { 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 */PHP_FUNCTION(ldap_dn2ufn){ zval **dn; char *ufn; if (ZEND_NUM_ARGS() !=1 || zend_get_parameters_ex(1, &dn)==FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(dn); ufn = ldap_dn2ufn(Z_STRVAL_PP(dn)); if (ufn !=NULL) { RETVAL_STRING(ufn, 1);#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10 || WINDOWS ldap_memfree(ufn);#endif } else { RETURN_FALSE; }}/* }}} *//* added to fix use of ldap_modify_add for doing an ldap_add, gerrit thomson. */#define PHP_LD_FULL_ADD 0xff/* {{{ php_ldap_do_modify */static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper){ zval **link, **dn, **entry, **value, **ivalue; ldap_linkdata *ld; char *ldap_dn; LDAPMod **ldap_mods; int i, j, num_attribs, num_values; int *num_berval; 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 */ if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &dn, &entry) == FAILURE) { WRONG_PARAM_COUNT; } if (Z_TYPE_PP(entry) != IS_ARRAY) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected Array as the last element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); convert_to_string_ex(dn); ldap_dn = Z_STRVAL_PP(dn); num_attribs = zend_hash_num_elements(Z_ARRVAL_PP(entry)); ldap_mods = safe_emalloc((num_attribs+1), sizeof(LDAPMod *), 0); num_berval = safe_emalloc(num_attribs, sizeof(int), 0); zend_hash_internal_pointer_reset(Z_ARRVAL_PP(entry)); /* added by gerrit thomson to fix ldap_add using ldap_mod_add */ if (oper == PHP_LD_FULL_ADD) { oper = LDAP_MOD_ADD; is_full_add = 1; } /* end additional , gerrit thomson */ for (i = 0; i < num_attribs; i++) { ldap_mods[i] = emalloc(sizeof(LDAPMod)); ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES; if (zend_hash_get_current_key(Z_ARRVAL_PP(entry), &attribute, &index, 0) == HASH_KEY_IS_STRING) { ldap_mods[i]->mod_type = estrdup(attribute); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown attribute in the data"); /* Free allocated memory */ while (i >= 0) { efree(ldap_mods[i--]); } efree(num_berval); efree(ldap_mods); RETURN_FALSE; } zend_hash_get_current_data(Z_ARRVAL_PP(entry), (void **)&value); if (Z_TYPE_PP(value) != IS_ARRAY) { num_values = 1; } else { num_values = zend_hash_num_elements(Z_ARRVAL_PP(value)); } num_berval[i] = num_values; ldap_mods[i]->mod_bvalues = safe_emalloc((num_values + 1), sizeof(struct berval *), 0);/* allow for arrays with one element, no allowance for arrays with none but probably not required, gerrit thomson. */ if ((num_values == 1) && (Z_TYPE_PP(value) != IS_ARRAY)) { convert_to_string_ex(value); ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval)); ldap_mods[i]->mod_bvalues[0]->bv_len = Z_STRLEN_PP(value); ldap_mods[i]->mod_bvalues[0]->bv_val = Z_STRVAL_PP(value); } else { for (j = 0; j < num_values; j++) { if (zend_hash_index_find(Z_ARRVAL_PP(value), j, (void **) &ivalue) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value array must have consecutive indices 0, 1, ..."); num_berval[i] = j; num_attribs = i + 1; RETVAL_FALSE; goto errexit; } convert_to_string_ex(ivalue); ldap_mods[i]->mod_bvalues[j] = (struct berval *) emalloc (sizeof(struct berval)); ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_PP(ivalue); ldap_mods[i]->mod_bvalues[j]->bv_val = Z_STRVAL_PP(ivalue); } } ldap_mods[i]->mod_bvalues[num_values] = NULL; zend_hash_move_forward(Z_ARRVAL_PP(entry)); } ldap_mods[num_attribs] = NULL;/* check flag to see if do_mod was called to perform full add , gerrit thomson */ if (is_full_add == 1) { if ((i = ldap_add_s(ld->link, ldap_dn, ldap_mods)) != LDAP_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Add: %s", ldap_err2string(i)); RETVAL_FALSE; } else RETVAL_TRUE; } else { if ((i = ldap_modify_s(ld->link, ldap_dn, ldap_mods)) != LDAP_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modify: %s", ldap_err2string(i)); RETVAL_FALSE; } else RETVAL_TRUE; }errexit: for (i = 0; i < num_attribs; i++) { efree(ldap_mods[i]->mod_type); for (j = 0; j < num_berval[i]; j++) { efree(ldap_mods[i]->mod_bvalues[j]); } efree(ldap_mods[i]->mod_bvalues); efree(ldap_mods[i]); } efree(num_berval); efree(ldap_mods); return;}/* }}} *//* {{{ proto bool ldap_add(resource link, string dn, array entry) Add entries to LDAP directory */PHP_FUNCTION(ldap_add){ /* 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 */ php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_LD_FULL_ADD);}/* }}} *//* three functions for attribute base modifications, gerrit Thomson *//* {{{ proto bool ldap_mod_replace(resource link, string dn, array entry) Replace attribute values with new ones */PHP_FUNCTION(ldap_mod_replace){ php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_REPLACE);}/* }}} *//* {{{ proto bool ldap_mod_add(resource link, string dn, array entry) Add attribute values to current */PHP_FUNCTION(ldap_mod_add){ php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_ADD);}/* }}} *//* {{{ proto bool ldap_mod_del(resource link, string dn, array entry) Delete attribute values */PHP_FUNCTION(ldap_mod_del){ php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_DELETE);}/* }}} *//* {{{ proto bool ldap_delete(resource link, string dn) Delete an entry from a directory */PHP_FUNCTION(ldap_delete){ zval **link, **dn; ldap_linkdata *ld; char *ldap_dn; int rc; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &dn) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); convert_to_string_ex(dn); ldap_dn = Z_STRVAL_PP(dn); if ((rc = ldap_delete_s(ld->link, ldap_dn)) != LDAP_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Delete: %s", ldap_err2string(rc)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto int ldap_errno(resource link) Get the current ldap error number */PHP_FUNCTION(ldap_errno){ zval **link; ldap_linkdata *ld; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ht, &link) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); RETURN_LONG(_get_lderrno(ld->link));}/* }}} *//* {{{ proto string ldap_err2str(int errno) Convert error number to error string */PHP_FUNCTION(ldap_err2str){ zval **perrno; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ht, &perrno) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(perrno); RETURN_STRING(ldap_err2string(Z_LVAL_PP(perrno)), 1);}/* }}} *//* {{{ proto string ldap_error(resource link) Get the current ldap error string */PHP_FUNCTION(ldap_error) { zval **link; ldap_linkdata *ld; int ld_errno; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ht, &link) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); ld_errno = _get_lderrno(ld->link); RETURN_STRING(ldap_err2string(ld_errno), 1);}/* }}} *//* {{{ proto bool ldap_compare(resource link, string dn, string attr, string value) Determine if an entry has a specific value for one of its attributes */PHP_FUNCTION(ldap_compare) { zval **link, **dn, **attr, **value; char *ldap_dn, *ldap_attr, *ldap_value; ldap_linkdata *ld; int errno; if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &link, &dn, &attr, &value) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); convert_to_string_ex(dn); convert_to_string_ex(attr); convert_to_string_ex(value); ldap_dn = Z_STRVAL_PP(dn); ldap_attr = Z_STRVAL_PP(attr); ldap_value = Z_STRVAL_PP(value); errno = ldap_compare_s(ld->link, ldap_dn, ldap_attr, ldap_value); switch (errno) { case LDAP_COMPARE_TRUE: RETURN_TRUE; break; case LDAP_COMPARE_FALSE: RETURN_FALSE; break; } php_error_docref(NULL TSRMLS_CC, E_WARNING, "Compare: %s", ldap_err2string(errno)); RETURN_LONG(-1);}/* }}} *//* {{{ proto bool ldap_sort(resource link, resource result, string sortfilter) Sort LDAP result entries */PHP_FUNCTION(ldap_sort){ zval *link, *result; ldap_linkdata *ld; 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; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link); if (zend_hash_index_find(&EG(regular_list), Z_LVAL_P(result), (void **) &le) == FAILURE || le->type != le_result) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied resource is not a valid ldap result resource"); RETURN_FALSE; } if (ldap_sort_entries(ld->link, (LDAPMessage **) &le->ptr, sflen ? sortfilter : NULL, strcmp) != LDAP_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ldap_err2string(errno)); RETURN_FALSE; } RETURN_TRUE;}/* }}} */#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP_10/* {{{ proto bool ldap_get_option(resource link, int option, mixed retval) Get the current value of various session-wide parameters */PHP_FUNCTION(ldap_get_option) { zval **link, **option, **retval; ldap_linkdata *ld; int opt; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &option, &retval) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link); convert_to_long_ex(option); opt = Z_LVAL_PP(option); switch (opt) { /* 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: case LDAP_OPT_REFERRALS:#ifdef LDAP_OPT_RESTART case LDAP_OPT_RESTART:#endif { int val; if (ldap_get_option(ld->link, opt, &val)) { RETURN_FALSE; } zval_dtor(*retval); ZVAL_LONG(*retval, val); } break; /* options with string value */ case LDAP_OPT_ERROR_STRING:#ifdef LDAP_OPT_HOST_NAME case LDAP_OPT_HOST_NAME:#endif#ifdef LDAP_OPT_MATCHED_DN case LDAP_OPT_MATCHED_DN:#endif { char *val = NULL; if (ldap_get_option(ld->link, opt, &val) || val == NULL || *val == '\0') { if (val) { ldap_memfree(val); } RETURN_FALSE; } zval_dtor(*retval); ZVAL_STRING(*retval, val, 1); ldap_memfree(val);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -