/var/www/(Del)hsihk.com/wp-content/plugins/ninja-forms/includes/admin/ajax.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php

add_action
'wp_ajax_ninja_forms_save_metabox_state''ninja_forms_save_metabox_state' );
function 
ninja_forms_save_metabox_state(){
    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$plugin_settings nf_get_settings();
    
$page esc_html$_REQUEST['page'] );
    
$tab esc_html$_REQUEST['tab'] );
    
$slug esc_html$_REQUEST['slug'] );
    
$state esc_html$_REQUEST['state'] );
    
$plugin_settings['metabox_state'][$page][$tab][$slug] = $state;
    
update_option'ninja_forms_settings'$plugin_settings );
    die();
}

add_action('wp_ajax_ninja_forms_new_field''ninja_forms_new_field');
function 
ninja_forms_new_field(){
    global 
$wpdb$ninja_forms_fields;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
// Bail if we don't have proper permissions
    
if ( ! current_user_canapply_filters'nf_new_field_capabilities''manage_options' ) ) )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$type esc_html$_REQUEST['type'] );
    
$form_id absint$_REQUEST['form_id'] );

    if( isset( 
$ninja_forms_fields[$type]['name'] ) ){
        
$type_name $ninja_forms_fields[$type]['name'];
    }else{
        
$type_name '';
    }

    if( isset( 
$ninja_forms_fields[$type]['default_label'] ) ){
        
$default_label $ninja_forms_fields[$type]['default_label'];
    }else{
        
$default_label '';
    }

    if( isset( 
$ninja_forms_fields[$type]['edit_options'] ) ){
        
$edit_options $ninja_forms_fields[$type]['edit_options'];
    }else{
        
$edit_options '';
    }

    if ( 
$default_label != '' ) {
        
$label $default_label;
    } else {
        
$label $type_name;
    }

    
$input_limit_msg __'character(s) left''ninja-forms' );

    
$data serialize( array( 'label' => $label'input_limit_msg' => $input_limit_msg ) );

    
$order 999;

    if(
$form_id != AND $form_id != ''){
        
$args = array(
            
'type' => $type,
            
'data' => $data,
        );

        
$new_id ninja_forms_insert_field$form_id$args );
        
$new_html ninja_forms_return_echo('ninja_forms_edit_field'$new_id);
        
header("Content-type: application/json");
        
$array = array ('new_id' => $new_id'new_type' => $type_name'new_html' => $new_html'edit_options' => $edit_options'new_type_slug' => $type );
        echo 
json_encode($array);
        die();
    }
}

add_action('wp_ajax_ninja_forms_remove_field''ninja_forms_remove_field');
function 
ninja_forms_remove_field(){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
// Bail if we don't have proper permissions
    
if ( ! current_user_canapply_filters'nf_delete_field_capabilities''manage_options' ) ) )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_id absint$_REQUEST['field_id'] );
    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE id = %d"$field_id));
    die();
}

add_action('wp_ajax_ninja_forms_delete_form''ninja_forms_delete_form');
function 
ninja_forms_delete_form$form_id '' ){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
// Bail if we don't have proper permissions
    
if ( ! current_user_canapply_filters'nf_delete_form_capabilities''manage_options' ) ) )
        return 
false;

    if( 
$form_id == '' ){
        
$ajax true;
        
$form_id absint$_REQUEST['form_id'] );
        
check_ajax_referer'nf_ajax''nf_ajax_nonce' );
    }else{
        
$ajax false;
    }

    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_TABLE_NAME." WHERE id = %d"$form_id));
    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE form_id = %d"$form_id));

    
// Delete any notifications attached to this form.
    // Grab notifications.
    
$notifications nf_get_notifications_by_form_id$form_idfalse );
    foreach ( 
$notifications as $n_id ) {
        
nf_delete_object$n_id );
    }

    if( 
$ajax ){
        die();
    }

}

add_action('wp_ajax_ninja_forms_add_list_option''ninja_forms_add_list_options');
function 
ninja_forms_add_list_options(){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
// Bail if we don't have proper permissions
    
if ( ! current_user_canapply_filters'nf_new_field_capabilities''manage_options' ) ) )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );
    
    
$field_id absint$_REQUEST['field_id'] );
    
$x absint$_REQUEST['x'] );
    
$hidden_value esc_html$_REQUEST['hidden_value'] );
    
ninja_forms_field_list_option_output($field_id$x''$hidden_value);
    die();
}

add_action('wp_ajax_ninja_forms_insert_fav''ninja_forms_insert_fav');
function 
ninja_forms_insert_fav(){
    global 
$wpdb$ninja_forms_fields;
    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$fav_id absint$_REQUEST['fav_id'] );
    
$form_id absint$_REQUEST['form_id'] );

    
$fav_row ninja_forms_get_fav_by_id($fav_id);

    
$data serialize($fav_row['data']);
    
$type $fav_row['type'];
    
$type_name $ninja_forms_fields[$type]['name'];

    if(
$form_id != AND $form_id != ''){
        
$args = array(
            
'type' => $type,
            
'data' => $data,
            
'fav_id' => $fav_id,
        );
        
$new_id ninja_forms_insert_field$form_id$args );
        
$new_html ninja_forms_return_echo('ninja_forms_edit_field'$new_id);
        
header("Content-type: application/json");
        
$array = array ('new_id' => $new_id'new_type' => $type_name'new_html' => $new_html);
        echo 
json_encode($array);
    }
    die();
}

add_action('wp_ajax_ninja_forms_insert_def''ninja_forms_insert_def');
function 
ninja_forms_insert_def(){
    global 
$wpdb$ninja_forms_fields;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$def_id absint$_REQUEST['def_id'] );
    
$form_id absint$_REQUEST['form_id'] );

    
$def_row ninja_forms_get_def_by_id($def_id);

    
$data serialize($def_row['data']);
    
$type $def_row['type'];
    
$type_name $ninja_forms_fields[$type]['name'];

    if(
$form_id != AND $form_id != ''){
        
$args = array(
            
'type' => $type,
            
'data' => $data,
            
'def_id' => $def_id,
        );
        
$new_id ninja_forms_insert_field$form_id$args );
        
$new_html ninja_forms_return_echo('ninja_forms_edit_field'$new_id);
        
header("Content-type: application/json");
        
$array = array ('new_id' => $new_id'new_type' => $type_name'new_html' => $new_html);
        echo 
json_encode($array);
    }
    die();
}

add_action('wp_ajax_ninja_forms_add_fav''ninja_forms_add_fav');
function 
ninja_forms_add_fav(){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_data $_REQUEST['field_data'];
    
$field_id absint$_REQUEST['field_id'] );

    
$field_row ninja_forms_get_field_by_id($field_id);

    
$field_type $field_row['type'];
    
$form_id 1;

    
$data = array();

    foreach(
$field_data as $key => $val){
        
$key stripslashes$key );
        
$key str_replace('"'''$key);
        if(
strpos($key'[')){
            
$key str_replace(']'''$key);
            
$key explode('['$key);
            
$multi = array();
            
$temp  =& $multi;
            
$x 0;
            
$count count($key) - 1;
            foreach (
$key as $item){
                
$temp[$item] = array();
                if(
$x $count){
                    
$temp =& $temp[$item];
                }else{
                    
$temp[$item] = $val;
                }
                
$x++;
            }
            
$data ninja_forms_array_merge_recursive($data$multi);
        }else{
            
$data[$key] = $val;
        }
    }

    
$name stripslashesesc_html$_REQUEST['fav_name'] ) );
    if ( !isset ( 
$data['label'] ) or empty ( $data['label'] ) ) {
        
$data['label'] = $name;        
    }

    
$data ninja_forms_stripslashes_deep$data );

    
$data serialize($data);
    
$wpdb->insert(NINJA_FORMS_FAV_FIELDS_TABLE_NAME, array('row_type' => 1'type' => $field_type'order' => 0'data' => $data'name' => $name));
    
$fav_id $wpdb->insert_id;
    
$update_array = array('fav_id' => $fav_id);
    
$wpdb->updateNINJA_FORMS_FIELDS_TABLE_NAME$update_array, array( 'id' => $field_id ));

    
$new_html '<p class="button-controls" id="ninja_forms_insert_fav_field_'.$fav_id.'_p">
                <a class="button add-new-h2 ninja-forms-insert-fav-field" id="ninja_forms_insert_fav_field_'
.$fav_id.'" name=""  href="#">'.__($name'ninja-forms').'</a>
            </p>'
;

    
header("Content-type: application/json");
    
$array = array ('fav_id' => $fav_id'fav_name' => $name'link_html' => $new_html);
    echo 
json_encode($array);

    die();
}

add_action('wp_ajax_ninja_forms_add_def''ninja_forms_add_def');
function 
ninja_forms_add_def(){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_data $_REQUEST['field_data'];
    
$field_id absint$_REQUEST['field_id'] );

    
$field_row ninja_forms_get_field_by_id($field_id);

    
$field_type $field_row['type'];
    
$row_type 0;

    
$data = array();

    foreach(
$field_data as $key => $val){
        
$key str_replace('"'''$key);
        if(
strpos($key'[')){
            
$key str_replace(']'''$key);
            
$key explode('['$key);
            
$multi = array();
            
$temp  =& $multi;
            
$x 0;
            
$count count($key) - 1;
            foreach (
$key as $item){
                
$temp[$item] = array();
                if(
$x $count){
                    
$temp =& $temp[$item];
                }else{
                    
$temp[$item] = $val;
                }
                
$x++;
            }
            
$data ninja_forms_array_merge_recursive($data$multi);
        }else{
            
$data[$key] = $val;
        }
    }

    
$name stripslashesesc_html$_REQUEST['def_name'] ) );
    
$data['label'] = $name;
    
$data serialize($data);
    
$wpdb->insert(NINJA_FORMS_FAV_FIELDS_TABLE_NAME, array('row_type' => $row_type'type' => $field_type'data' => $data'name' => $name));
    
$def_id $wpdb->insert_id;
    
$update_array = array('def_id' => $def_id);
    
$wpdb->updateNINJA_FORMS_FIELDS_TABLE_NAME$update_array, array( 'id' => $field_id ));

    
$new_html '<p class="button-controls" id="ninja_forms_insert_def_field_'.$def_id.'_p">
                <a class="button add-new-h2 ninja-forms-insert-def-field" id="ninja_forms_insert_def_field_'
.$def_id.'" name=""  href="#">'.__($name'ninja-forms').'</a>
            </p>'
;
    
header("Content-type: application/json");
    
$array = array ('def_id' => $def_id'def_name' => $name'link_html' => $new_html);
    echo 
json_encode($array);

    die();
}

add_action('wp_ajax_ninja_forms_remove_fav''ninja_forms_remove_fav');
function 
ninja_forms_remove_fav(){
    global 
$wpdb$ninja_forms_fields;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_id absint$_REQUEST['field_id'] );
    
$field_row ninja_forms_get_field_by_id($field_id);
    
$field_type $field_row['type'];
    
$fav_id $field_row['fav_id'];
    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d"$fav_id));
    
$wpdb->update(NINJA_FORMS_FIELDS_TABLE_NAME, array('fav_id' => '' ), array('fav_id' => $fav_id));
    
$type_name $ninja_forms_fields[$field_type]['name'];
    
header("Content-type: application/json");
    
$array = array ('fav_id' => $fav_id'type_name' => $type_name);
    echo 
json_encode($array);

    die();
}

add_action('wp_ajax_ninja_forms_remove_def''ninja_forms_remove_def');
function 
ninja_forms_remove_def(){
    global 
$wpdb$ninja_forms_fields;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_id absint$_REQUEST['field_id'] );
    
$field_row ninja_forms_get_field_by_id($field_id);
    
$field_type $field_row['type'];
    
$def_id $field_row['def_id'];
    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d"$def_id));
    
$wpdb->update(NINJA_FORMS_FIELDS_TABLE_NAME, array('def_id' => '' ), array('def_id' => $def_id));
    
$type_name $ninja_forms_fields[$field_type]['name'];
    
header("Content-type: application/json");
    
$array = array ('def_id' => $def_id'type_name' => $type_name);
    echo 
json_encode($array);

    die();
}

add_action'wp_ajax_ninja_forms_side_sortable''ninja_forms_side_sortable' );
function 
ninja_forms_side_sortable(){

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$plugin_settings nf_get_settings();
    
$page esc_html$_REQUEST['page'] );
    
$tab esc_html$_REQUEST['tab'] );
    
$order ninja_forms_esc_html_deep$_REQUEST['order'] );

    
$plugin_settings['sidebars'][$page][$tab] = $order;
    
update_option'ninja_forms_settings'$plugin_settings );

    die();
}

add_action('wp_ajax_ninja_forms_delete_sub''ninja_forms_delete_sub');
function 
ninja_forms_delete_sub($sub_id ''){
    global 
$wpdb;

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    if(
$sub_id == ''){
        
$ajax true;
        
$sub_id absint$_REQUEST['sub_id'] );
    }else{
        
$ajax false;
    }

    
$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_SUBS_TABLE_NAME." WHERE id = %d"$sub_id));
    if( 
$ajax ){
        die();
    }
}

function 
ninja_forms_array_merge_recursive() {
    
$arrays func_get_args();
    
$base array_shift($arrays);

    foreach (
$arrays as $array) {
        
reset($base); //important
        
while (list($key$value) = @each($array)) {
            if (
is_array($value) && @is_array($base[$key])) {
                
$base[$key] = ninja_forms_array_merge_recursive($base[$key], $value);
            } else {
                
$base[$key] = $value;
            }
        }
    }

    return 
$base;
}

function 
ninja_forms_import_list_options(){
    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$options $_REQUEST['options'];
    
$field_id absint$_REQUEST['field_id'] );
    
$options str_replace('\,''-comma-replace-placeholder-'$options );
    
$options ninja_forms_csv_explode$options );

    if( 
is_array$options ) ){
        
$tmp_array = array();
        
$x 0;
        foreach( 
$options as $option ){
            
$label stripslashes$option[0] );
            
$value stripslashes$option[1] );
            
$calc stripslashes$option[2] );
            
$label str_replace"''"""$label );
            
$label str_replace"-comma-replace-placeholder-"","$label );
            
$value str_replace"''"""$value );
            
$value str_replace"-comma-replace-placeholder-"","$value );
            
$calc str_replace"''"""$calc );
            
$calc str_replace"-comma-replace-placeholder-"","$calc );
            
$tmp_array[$x]['label'] = $label;
            
$tmp_array[$x]['value'] = $value;
            
$tmp_array[$x]['calc'] = $calc;
            
$x++;
        }
        
$x 0;
        foreach( 
$tmp_array as $option ){
            
$hidden 0;
            
ninja_forms_field_list_option_output($field_id$x$option$hidden);
            
$x++;
        }
    }

    die();
}

add_action'wp_ajax_ninja_forms_import_list_options''ninja_forms_import_list_options' );

/*
 *
 * Function that outputs a list of terms so that the user can exclude terms from a list selector.
 *
 * @since 2.2.51
 * @return void
 */

function ninja_forms_list_terms_checkboxes$field_id ''$tax_name '' ){

    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    if ( 
$field_id == '' AND isset ( $_POST['field_id'] ) ) {
        
$field_id absint$_POST['field_id'] );
    }    

    if ( 
$tax_name == '' AND isset ( $_POST['tax_name'] ) ) {
        
$tax_name esc_html$_POST['tax_name'] );
    }

    if ( 
$field_id != '' AND $tax_name != '' ) {
        
$field ninja_forms_get_field_by_id$field_id );
        if ( isset ( 
$field['data']['exclude_terms'] ) ) {
            
$exclude_terms $field['data']['exclude_terms'];
        } else {
            
$exclude_terms '';
        }

        
$terms get_terms$tax_name, array( 'hide_empty' => false ) );
        if ( 
is_array $terms ) AND !empty ( $terms ) ) {
            
?>
            <h4><?php _e'Do not show these terms''ninja-forms' );?>:</h4>
            <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>[exclude_terms]" value="">
            <?php
            
foreach ( $terms as $term ) {
                
?>
                <div>
                    <label>
                        <input type="checkbox" <?php checkedin_array $term->term_id$exclude_terms ), true );?> name="ninja_forms_field_<?php echo $field_id;?>[exclude_terms][]" value="<?php echo $term->term_id;?>">
                        <?php echo $term->name;?>
                    </label>
                </div>
                <?php
            
}
        }
    }

    if ( isset ( 
$_POST['from_ajax'] ) AND absint$_POST['from_ajax'] ) == ) {
        die();
    }
}

add_action'wp_ajax_ninja_forms_list_terms_checkboxes''ninja_forms_list_terms_checkboxes' );

/*
 *
 * Function that outputs a calculation row
 *
 * @since 2.2.28
 * @returns void
 */

function ninja_forms_add_calc_row(){
    
// Bail if we aren't in the admin
    
if ( ! is_admin() )
        return 
false;

    
check_ajax_referer'nf_ajax''nf_ajax_nonce' );

    
$field_id absint$_REQUEST['field_id'] );
    
$c = array( 'calc' => '''operator' => 'add''value' => '''when' => '' );
    
$x absint$_REQUEST['x'] );

    
ninja_forms_output_field_calc_row$field_id$c$x );
    die();
}

add_action'wp_ajax_ninja_forms_add_calc_row''ninja_forms_add_calc_row' );

/**
 * 
 * Covert a multi-line CSV string into a 2d array. Follows RFC 4180, allows
 * "cells with ""escaped delimiters""" and multi-line enclosed cells
 * It assumes the CSV file is properly formatted, and doesn't check for errors
 * in CSV format.
 * @param string $str The CSV string
 * @param string $d The delimiter between values
 * @param string $e The enclosing character
 * @param bool $crlf Set to true if your CSV file should return carriage return
 *                         and line feed (CRLF should be returned according to RFC 4180
 * @return array 
 */
function ninja_forms_csv_explode$str$d=','$e='"'$crlf=TRUE ) {
    
// Convert CRLF to LF, easier to work with in regex
    
if( $crlf $str str_replace("\r\n","\n",$str);
    
// Get rid of trailing linebreaks that RFC4180 allows
    
$str trim($str);
    
// Do the dirty work
    
if ( preg_match_all(
        
'/(?:
            '
.$e.'((?:[^'.$e.']|'.$e.$e.')*+)'.$e.'(?:'.$d.'|\n|$)
                # match enclose, then match either non-enclose or double-enclose
                # zero to infinity times (possesive), then match another enclose,
                # followed by a comma, linebreak, or string end
            |    ####### OR #######
            ([^'
.$d.'\n]*+)(?:['.$d.'\n]|$)
                # match anything thats not a comma or linebreak zero to infinity
                # times (possesive), then match either a comma or a linebreak or
                # string end
        )/x'

        
$str$msPREG_SET_ORDER
    
) === FALSE ) return FALSE;
    
// Initialize vars, $r will hold our return data, $i will track which line we're on
    
$r = array(); $i 0;
    
// Loop through results
    
foreach( $ms as $m ) {
        
// If the first group of matches is empty, the cell has no quotes
        
if( empty($m[1]) )
            
// Put the CRLF back in if needed
            
$r[$i][] = ($crlf == TRUE) ? str_replace("\n","\r\n",$m[2]) : $m[2];
        else {
            
// The cell was quoted, so we want to convert any "" back to " and
            // any LF back to CRLF, if needed
            
$r[$i][] = ($crlf == TRUE) ?
                
str_replace(
                    array(
"\n",$e.$e),
                    array(
"\r\n",$e),
                    
$m[1]) :
                
str_replace($e.$e$e$m[1]);
        }
        
// If the raw match doesn't have a delimiter, it must be the last in the
        // row, so we increment our line count.
        
if( substr($m[0],-1) != $d )
            
$i++;
    }
    
// An empty array will exist due to $ being a zero-length match, so remove it
    
array_pop$r );
    return 
$r;

}