ajax.php 17.5 KB
Newer Older
Kevin Stover's avatar
Kevin Stover committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
add_action( 'wp_ajax_ninja_forms_save_metabox_state', 'ninja_forms_save_metabox_state' );
function ninja_forms_save_metabox_state(){
	$plugin_settings = get_option( 'ninja_forms_settings' );
	$page = $_REQUEST['page'];
	$tab = $_REQUEST['tab'];
	$slug = $_REQUEST['slug'];
	$state = $_REQUEST['state'];
	$plugin_settings['metabox_state'][$page][$tab][$slug] = $state;
	update_option( 'ninja_forms_settings', $plugin_settings );
	//$plugin_settings = get_option( 'ninja_forms_settings' );
	//echo "SETTING: ".$plugin_settings['metabox_state'][$page][$tab][$slug];
	die();
}

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

	$type = $_REQUEST['type'];
	$form_id = $_REQUEST['form_id'];
22

Kevin Stover's avatar
Kevin Stover committed
23 24 25 26 27
	if( isset( $ninja_forms_fields[$type]['name'] ) ){
		$type_name = $ninja_forms_fields[$type]['name'];
	}else{
		$type_name = '';
	}
28

Kevin Stover's avatar
Kevin Stover committed
29 30 31 32 33
	if( isset( $ninja_forms_fields[$type]['edit_options'] ) ){
		$edit_options = $ninja_forms_fields[$type]['edit_options'];
	}else{
		$edit_options = '';
	}
34

Kevin Stover's avatar
Kevin Stover committed
35 36 37 38 39 40 41 42 43
	$data = serialize(array('label' => $type_name));

	$order = 999;

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

Kevin Stover's avatar
Kevin Stover committed
45 46 47
		$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");
48
		$array = array ('new_id' => $new_id, 'new_type' => $type_name, 'new_html' => $new_html, 'edit_options' => $edit_options, 'new_type_slug' => $type );
Kevin Stover's avatar
Kevin Stover committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		echo json_encode($array);
		die();
	}
}

add_action('wp_ajax_ninja_forms_remove_field', 'ninja_forms_remove_field');
function ninja_forms_remove_field(){
	global $wpdb;
	$field_id = $_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;
	if( $form_id == '' ){
		$ajax = true;
		$form_id = $_REQUEST['form_id'];
	}else{
		$ajax = false;
	}
71

Kevin Stover's avatar
Kevin Stover committed
72 73
	$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));
74

Kevin Stover's avatar
Kevin Stover committed
75 76 77
	if( $ajax ){
		die();
	}
78

Kevin Stover's avatar
Kevin Stover committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

add_action('wp_ajax_ninja_forms_add_list_option', 'ninja_forms_add_list_options');
function ninja_forms_add_list_options(){
	global $wpdb;
	$field_id = $_REQUEST['field_id'];
	$x = $_REQUEST['x'];
	$hidden_value = $_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;
	$fav_id = $_REQUEST['fav_id'];
	$form_id = $_REQUEST['form_id'];
96

Kevin Stover's avatar
Kevin Stover committed
97 98 99 100 101
	$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'];
102

Kevin Stover's avatar
Kevin Stover committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	if($form_id != 0 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;
	$def_id = $_REQUEST['def_id'];
	$form_id = $_REQUEST['form_id'];
123

Kevin Stover's avatar
Kevin Stover committed
124
	$def_row = ninja_forms_get_def_by_id($def_id);
125

Kevin Stover's avatar
Kevin Stover committed
126 127 128
	$data = serialize($def_row['data']);
	$type = $def_row['type'];
	$type_name = $ninja_forms_fields[$type]['name'];
129

Kevin Stover's avatar
Kevin Stover committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	if($form_id != 0 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;

	$field_data = $_REQUEST['field_data'];
	$field_id = $_REQUEST['field_id'];

	$field_row = ninja_forms_get_field_by_id($field_id);
153

Kevin Stover's avatar
Kevin Stover committed
154 155
	$field_type = $field_row['type'];
	$form_id = 1;
156

Kevin Stover's avatar
Kevin Stover committed
157
	$data = array();
158

Kevin Stover's avatar
Kevin Stover committed
159 160
	foreach($field_data as $key => $val){
		$key = stripslashes( $key );
161
		$key = str_replace('"', '', $key);
Kevin Stover's avatar
Kevin Stover committed
162 163 164
		if(strpos($key, '[')){
			$key = str_replace(']', '', $key);
			$key = explode('[', $key);
165 166
			$multi = array();
			$temp  =& $multi;
Kevin Stover's avatar
Kevin Stover committed
167 168
			$x = 0;
			$count = count($key) - 1;
169 170
			foreach ($key as $item){
				$temp[$item] = array();
Kevin Stover's avatar
Kevin Stover committed
171 172 173 174 175 176 177
				if($x < $count){
					$temp =& $temp[$item];
				}else{
					$temp[$item] = $val;
				}
				$x++;
			}
178
			$data = ninja_forms_array_merge_recursive($data, $multi);
Kevin Stover's avatar
Kevin Stover committed
179 180 181 182
		}else{
			$data[$key] = $val;
		}
	}
183

Kevin Stover's avatar
Kevin Stover committed
184 185 186 187 188 189 190 191
	$name = stripslashes($_REQUEST['fav_name']);
	$data['label'] = $name;

	$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->update( NINJA_FORMS_FIELDS_TABLE_NAME, $update_array, array( 'id' => $field_id ));
192

Kevin Stover's avatar
Kevin Stover committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	$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;
	$field_data = $_REQUEST['field_data'];
	$field_id = $_REQUEST['field_id'];

	$field_row = ninja_forms_get_field_by_id($field_id);
211

Kevin Stover's avatar
Kevin Stover committed
212 213
	$field_type = $field_row['type'];
	$row_type = 0;
214

Kevin Stover's avatar
Kevin Stover committed
215
	$data = array();
216

Kevin Stover's avatar
Kevin Stover committed
217
	foreach($field_data as $key => $val){
218
		$key = str_replace('"', '', $key);
Kevin Stover's avatar
Kevin Stover committed
219 220 221
		if(strpos($key, '[')){
			$key = str_replace(']', '', $key);
			$key = explode('[', $key);
222 223
			$multi = array();
			$temp  =& $multi;
Kevin Stover's avatar
Kevin Stover committed
224 225
			$x = 0;
			$count = count($key) - 1;
226 227
			foreach ($key as $item){
				$temp[$item] = array();
Kevin Stover's avatar
Kevin Stover committed
228 229 230 231 232 233 234
				if($x < $count){
					$temp =& $temp[$item];
				}else{
					$temp[$item] = $val;
				}
				$x++;
			}
235
			$data = ninja_forms_array_merge_recursive($data, $multi);
Kevin Stover's avatar
Kevin Stover committed
236 237 238 239
		}else{
			$data[$key] = $val;
		}
	}
240

Kevin Stover's avatar
Kevin Stover committed
241 242 243 244 245 246 247
	$name = stripslashes($_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->update( NINJA_FORMS_FIELDS_TABLE_NAME, $update_array, array( 'id' => $field_id ));
248

Kevin Stover's avatar
Kevin Stover committed
249 250 251 252 253 254
	$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);
255

Kevin Stover's avatar
Kevin Stover committed
256 257 258 259 260 261
	die();
}

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

Kevin Stover's avatar
Kevin Stover committed
263 264 265 266 267 268 269 270 271 272
	$field_id = $_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);
273

Kevin Stover's avatar
Kevin Stover committed
274 275 276 277 278 279
	die();
}

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

Kevin Stover's avatar
Kevin Stover committed
281 282 283 284 285 286 287 288 289 290
	$field_id = $_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);
291

Kevin Stover's avatar
Kevin Stover committed
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
	die();
}

add_action( 'wp_ajax_ninja_forms_side_sortable', 'ninja_forms_side_sortable' );
function ninja_forms_side_sortable(){
	$plugin_settings = get_option( 'ninja_forms_settings' );
	$page = $_REQUEST['page'];
	$tab = $_REQUEST['tab'];
	$order = $_REQUEST['order'];

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

	die();
}

add_action('wp_ajax_ninja_forms_view_sub', 'ninja_forms_view_sub');
function ninja_forms_view_sub(){
	global $ninja_forms_fields;
	/*
	$plugin_settings = get_option("ninja_forms_settings");
	if(isset($plugin_settings['date_format'])){
		$date_format = $plugin_settings['date_format'];
	}else{
		$date_format = 'm/d/Y';
	}
	$sub_id = $_REQUEST['sub_id'];
	$sub_row = ninja_forms_get_sub_by_id($sub_id);
	$data = $sub_row['data'];
321

Kevin Stover's avatar
Kevin Stover committed
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
	$date_updated = strtotime($sub_row['date_updated']);
	$date_updated = date($date_format, $date_updated);
	*/
	$new_html = '<input type="hidden" id="ninja_forms_sub_id" value="'.$sub_id.'">';
	/*
	foreach($data as $field_id => $user_value){
		$new_html .= '<div id="" name="" class="description description-wide">';
		$field_row = ninja_forms_get_field_by_id($field_id);
		if(isset($field_row['data']['label'])){
			$field_label = $field_row['data']['label'];
		}else{
			$field_label = '';
		}
		$field_type = $field_row['type'];
		if($ninja_forms_fields[$field_type]['process_field']){
			$sub_edit = $ninja_forms_fields[$field_type]['sub_edit'];
			$user_value2 = '';
			if(is_array($user_value)){
				$x = 1;
				foreach($user_value as $val){
					$user_value2 .= esc_html(stripslashes($val));
					if($x != count($user_value)){
						$user_value2 .= ",";
					}
					$x++;
				}
			}else{
				$user_value2 = esc_html(stripslashes($user_value));
			}
			$new_html .= '<label for="ninja_forms_field_'.$field_id.'">'.$field_label;
			if($sub_edit == 'text'){
				$new_html .= '<input type="text" id="ninja_forms_field_'.$field_id.'" name="" value="'.$user_value2.'" class="code widefat">';
			}else if($sub_edit == 'textarea'){
355

Kevin Stover's avatar
Kevin Stover committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
			}
			$new_html .= '</label>';
			$new_html .= '</div>';
		}
	}
	*/
	header("Content-type: application/json");
	$array = array('new_html' => $new_html);
	echo json_encode($array);
	//echo "hello world";
	die();
}

add_action('wp_ajax_ninja_forms_edit_sub', 'ninja_forms_edit_sub');
function ninja_forms_edit_sub(){
	global $wpdb;
	$sub_id = $_REQUEST['sub_id'];
	$sub_data = $_REQUEST['sub_data'];

	$args = array(
		'sub_id' => $sub_id,
		'data' => $sub_data,
	);
379

Kevin Stover's avatar
Kevin Stover committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393
	ninja_forms_update_sub($args);
	die();
}

add_action('wp_ajax_ninja_forms_delete_sub', 'ninja_forms_delete_sub');
add_action('wp_ajax_nopriv_ninja_forms_delete_sub', 'ninja_forms_delete_sub');
function ninja_forms_delete_sub($sub_id = ''){
	global $wpdb;
	if($sub_id == ''){
		$ajax = true;
		$sub_id = $_REQUEST['sub_id'];
	}else{
		$ajax = false;
	}
394

Kevin Stover's avatar
Kevin Stover committed
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
	$wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_SUBS_TABLE_NAME." WHERE id = %d", $sub_id));
	if( $ajax ){
		die();
	}
}

add_action('wp_ajax_ninja_forms_ajax_submit', 'ninja_forms_ajax_submit');
add_action('wp_ajax_nopriv_ninja_forms_ajax_submit', 'ninja_forms_ajax_submit');
function ninja_forms_ajax_submit(){
	global $ninja_forms_processing;
	//add_action( 'init', 'test' );
	//add_action( 'init', 'ninja_forms_setup_processing_class', 5 );
	//add_action( 'init', 'ninja_forms_pre_process', 999 );
	//ninja_forms_setup_processing_class();
	//ninja_forms_pre_process();
	//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;
Kevin Stover's avatar
Kevin Stover committed
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
}

function ninja_forms_import_list_options(){
	$options = $_REQUEST['options'];
	$field_id = $_REQUEST['field_id'];

	$options = csv_explode( $options );

	if( is_array( $options ) ){
		$tmp_array = array();
		$x = 0;
		foreach( $options as $option ){
			$label = stripslashes( $option[0] );
			$value = stripslashes( $option[1] );
			$label = str_replace( "''", "", $label );
			$value = str_replace( "''", "", $value );
			$tmp_array[$x]['label'] = $label;
			$tmp_array[$x]['value'] = $value;
			$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' );

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
/*
 *
 * 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 = '' ){
	if ( $field_id == '' AND isset ( $_POST['field_id'] ) ) {
		$field_id = $_POST['field_id'];
	}	

	if ( $tax_name == '' AND isset ( $_POST['tax_name'] ) ) {
		$tax_name = $_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 checked( in_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 $_POST['from_ajax'] == 1 ) {
		die();
	}
}

add_action( 'wp_ajax_ninja_forms_list_terms_checkboxes', 'ninja_forms_list_terms_checkboxes' );

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
/*
 *
 * Function that outputs a calculation row
 *
 * @since 2.2.28
 * @returns void
 */

function ninja_forms_add_calc_row(){
	$field_id = $_REQUEST['field_id'];
	$c = array( 'calc' => '', 'operator' => 'add', 'value' => '', 'when' => '' );
	$x = $_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' );

Kevin Stover's avatar
Kevin Stover committed
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
/**
 * 
 * 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 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, $ms, PREG_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;

Kevin Stover's avatar
Kevin Stover committed
592
}