@@ -235,6 +235,22 @@ static void ldap_result_entry_free_obj(zend_object *obj)
235
235
} \
236
236
}
237
237
238
+ static bool php_ldap_is_numerically_indexed_array (zend_array * arr )
239
+ {
240
+ if (zend_hash_num_elements (arr ) == 0 || HT_IS_PACKED (arr )) {
241
+ return true;
242
+ }
243
+
244
+ zend_string * str_key ;
245
+ ZEND_HASH_MAP_FOREACH_STR_KEY (arr , str_key ) {
246
+ if (str_key ) {
247
+ return false;
248
+ }
249
+ } ZEND_HASH_FOREACH_END ();
250
+
251
+ return false;
252
+ }
253
+
238
254
/* {{{ Parse controls from and to arrays */
239
255
static void _php_ldap_control_to_array (LDAP * ld , LDAPControl * ctrl , zval * array , int request )
240
256
{
@@ -1471,20 +1487,22 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
1471
1487
num_attribs = zend_hash_num_elements (Z_ARRVAL_P (attrs ));
1472
1488
ldap_attrs = safe_emalloc ((num_attribs + 1 ), sizeof (char * ), 0 );
1473
1489
1474
- for (i = 0 ; i < num_attribs ; i ++ ) {
1475
- if ((attr = zend_hash_index_find (Z_ARRVAL_P (attrs ), i )) == NULL ) {
1476
- php_error_docref (NULL , E_WARNING , "Array initialization wrong" );
1477
- ret = 0 ;
1478
- goto cleanup ;
1479
- }
1490
+ if (!php_ldap_is_numerically_indexed_array (Z_ARRVAL_P (attrs ))) {
1491
+ php_error_docref (NULL , E_WARNING , "Argument #4 ($attributes) must be an array with numeric keys" );
1492
+ ret = 0 ;
1493
+ goto cleanup ;
1494
+ }
1480
1495
1496
+ i = 0 ;
1497
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (attrs ), attr ) {
1481
1498
convert_to_string (attr );
1482
1499
if (EG (exception )) {
1483
1500
ret = 0 ;
1484
1501
goto cleanup ;
1485
1502
}
1486
- ldap_attrs [i ] = Z_STRVAL_P (attr );
1487
- }
1503
+ ldap_attrs [i ++ ] = Z_STRVAL_P (attr );
1504
+ } ZEND_HASH_FOREACH_END ();
1505
+
1488
1506
ldap_attrs [num_attribs ] = NULL ;
1489
1507
ZEND_FALLTHROUGH ;
1490
1508
default :
@@ -2257,14 +2275,16 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
2257
2275
ldap_mods [i ]-> mod_bvalues [0 ]-> bv_val = Z_STRVAL_P (value );
2258
2276
ldap_mods [i ]-> mod_bvalues [0 ]-> bv_len = Z_STRLEN_P (value );
2259
2277
} else {
2260
- for (j = 0 ; j < num_values ; j ++ ) {
2261
- if ((ivalue = zend_hash_index_find (Z_ARRVAL_P (value ), j )) == NULL ) {
2262
- zend_argument_value_error (3 , "must contain arrays with consecutive integer indices starting from 0" );
2263
- num_berval [i ] = j ;
2264
- num_attribs = i + 1 ;
2265
- RETVAL_FALSE ;
2266
- goto cleanup ;
2267
- }
2278
+ if (!php_ldap_is_numerically_indexed_array (Z_ARRVAL_P (value ))) {
2279
+ zend_argument_value_error (3 , "must be an array with numeric keys" );
2280
+ RETVAL_FALSE ;
2281
+ num_berval [i ] = 0 ;
2282
+ num_attribs = i + 1 ;
2283
+ goto cleanup ;
2284
+ }
2285
+
2286
+ j = 0 ;
2287
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (value ), ivalue ) {
2268
2288
convert_to_string (ivalue );
2269
2289
if (EG (exception )) {
2270
2290
num_berval [i ] = j ;
@@ -2275,7 +2295,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
2275
2295
ldap_mods [i ]-> mod_bvalues [j ] = (struct berval * ) emalloc (sizeof (struct berval ));
2276
2296
ldap_mods [i ]-> mod_bvalues [j ]-> bv_val = Z_STRVAL_P (ivalue );
2277
2297
ldap_mods [i ]-> mod_bvalues [j ]-> bv_len = Z_STRLEN_P (ivalue );
2278
- }
2298
+ j ++ ;
2299
+ } ZEND_HASH_FOREACH_END ();
2279
2300
}
2280
2301
ldap_mods [i ]-> mod_bvalues [num_values ] = NULL ;
2281
2302
zend_hash_move_forward (Z_ARRVAL_P (entry ));
@@ -2543,7 +2564,7 @@ PHP_FUNCTION(ldap_modify_batch)
2543
2564
zval * fetched ;
2544
2565
char * dn ;
2545
2566
size_t dn_len ;
2546
- int i , j , k ;
2567
+ int i , j ;
2547
2568
int num_mods , num_modprops , num_modvals ;
2548
2569
LDAPMod * * ldap_mods ;
2549
2570
LDAPControl * * lserverctrls = NULL ;
@@ -2603,12 +2624,14 @@ PHP_FUNCTION(ldap_modify_batch)
2603
2624
2604
2625
num_mods = zend_hash_num_elements (Z_ARRVAL_P (mods ));
2605
2626
2606
- for (i = 0 ; i < num_mods ; i ++ ) {
2607
- /* is the numbering consecutive? */
2608
- if ((fetched = zend_hash_index_find (Z_ARRVAL_P (mods ), i )) == NULL ) {
2609
- zend_argument_value_error (3 , "must have consecutive integer indices starting from 0" );
2610
- RETURN_THROWS ();
2611
- }
2627
+ if (!php_ldap_is_numerically_indexed_array (Z_ARRVAL_P (mods ))) {
2628
+ zend_argument_value_error (3 , "must be an array with numeric keys" );
2629
+ RETURN_THROWS ();
2630
+ }
2631
+
2632
+ i = 0 ;
2633
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (mods ), fetched ) {
2634
+ ZVAL_DEREF (fetched );
2612
2635
mod = fetched ;
2613
2636
2614
2637
/* is it an array? */
@@ -2706,19 +2729,10 @@ PHP_FUNCTION(ldap_modify_batch)
2706
2729
RETURN_THROWS ();
2707
2730
}
2708
2731
2709
- /* are its keys integers? */
2710
- if (zend_hash_get_current_key_type (Z_ARRVAL_P (modinfo )) != HASH_KEY_IS_LONG ) {
2711
- zend_value_error ("%s(): Option \"" LDAP_MODIFY_BATCH_VALUES "\" must be integer-indexed" , get_active_function_name ());
2732
+ if (!php_ldap_is_numerically_indexed_array (Z_ARRVAL_P (modinfo ))) {
2733
+ zend_value_error ("%s(): Option \"" LDAP_MODIFY_BATCH_VALUES "\" must be an array with numeric keys" , get_active_function_name ());
2712
2734
RETURN_THROWS ();
2713
2735
}
2714
-
2715
- /* are the keys consecutive? */
2716
- for (k = 0 ; k < num_modvals ; k ++ ) {
2717
- if ((fetched = zend_hash_index_find (Z_ARRVAL_P (modinfo ), k )) == NULL ) {
2718
- zend_value_error ("%s(): Option \"" LDAP_MODIFY_BATCH_VALUES "\" must have consecutive integer indices starting from 0" , get_active_function_name ());
2719
- RETURN_THROWS ();
2720
- }
2721
- }
2722
2736
}
2723
2737
2724
2738
zend_hash_move_forward (Z_ARRVAL_P (mod ));
@@ -2732,7 +2746,9 @@ PHP_FUNCTION(ldap_modify_batch)
2732
2746
zend_value_error ("%s(): Required option \"" LDAP_MODIFY_BATCH_MODTYPE "\" is missing" , get_active_function_name ());
2733
2747
RETURN_THROWS ();
2734
2748
}
2735
- }
2749
+
2750
+ i ++ ;
2751
+ } ZEND_HASH_FOREACH_END ();
2736
2752
}
2737
2753
/* validation was successful */
2738
2754
@@ -2786,9 +2802,9 @@ PHP_FUNCTION(ldap_modify_batch)
2786
2802
ldap_mods [i ]-> mod_bvalues = safe_emalloc ((num_modvals + 1 ), sizeof (struct berval * ), 0 );
2787
2803
2788
2804
/* for each value */
2789
- for (j = 0 ; j < num_modvals ; j ++ ) {
2805
+ j = 0 ;
2806
+ ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (vals ), fetched ) {
2790
2807
/* fetch it */
2791
- fetched = zend_hash_index_find (Z_ARRVAL_P (vals ), j );
2792
2808
modval = zval_get_string (fetched );
2793
2809
if (EG (exception )) {
2794
2810
RETVAL_FALSE ;
@@ -2804,7 +2820,8 @@ PHP_FUNCTION(ldap_modify_batch)
2804
2820
ldap_mods [i ]-> mod_bvalues [j ]-> bv_len = ZSTR_LEN (modval );
2805
2821
ldap_mods [i ]-> mod_bvalues [j ]-> bv_val = estrndup (ZSTR_VAL (modval ), ZSTR_LEN (modval ));
2806
2822
zend_string_release (modval );
2807
- }
2823
+ j ++ ;
2824
+ } ZEND_HASH_FOREACH_END ();
2808
2825
2809
2826
/* NULL-terminate values */
2810
2827
ldap_mods [i ]-> mod_bvalues [num_modvals ] = NULL ;
0 commit comments