<?php

//upload.php

session_start();

$error = '';

$html = '';

$check_mailbox = '';

$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];

if($file_name != '')
{
 $file_array = explode(".", $file_name);

 $extension = end($file_array);

 if($extension == 'csv')
 {
	
/*
  if($file_size <= 2097152) {
		 $error = 'File size must be excately 2 MB';
  }
  */
	  
  // $file_ext = strtolower(end(explode('.',$file_name)));

	if(empty($error) == true) {
		$tmploc = realpath(dirname(dirname(__FILE__))).'/tmp/csv/';
		move_uploaded_file($file_tmp,"$tmploc/$file_name");
		$file_tmp = "$tmploc/$file_name";
	} else {
	   exit();
	}
	
  $file_data = fopen($file_tmp, 'r');

  $file_header = fgetcsv($file_data);

  $cc_data = unserialize($_POST['cc_data']);
  $data = '';
  if(!empty($cc_data)) {
  foreach($cc_data as $val){
	  $Uval = ucwords(str_replace('_', ' ', $val));
	  $data .= "<option value='$val'>$Uval</option>";
  }
  }
  
  $html .= '<table class="table table-bordered"><tr>';
  


  for($count = 0; $count < count($file_header); $count++)
  {
   $html .= '
   <th>
    <select name="set_column_data" class="form-control set_column_data" data-column_number="'.$count.'">
     <option value="">Set Data</option>'
     .$data.
     '<option value="email_name">Email</option>
    </select>
   </th>
   ';
  }

  $html .= '</tr>';

  $limit = 0;

  while(($row = fgetcsv($file_data)) !== FALSE)
  {
   $limit++;

   if($limit < 2)
   {
    $html .= '<tr>';

    for($count = 0; $count < count($row); $count++)
    {
     $html .= '<td>'.$row[$count].'</td>';
    }

    $html .= '</tr>';
   }/* else {
	  break;
   }
  */
   $temp_data[] = $row;
  }
  
  // var_dump($temp_data);

  $_SESSION['file_data'] = $temp_data;
   $_SESSION['file_name'] = $file_name;

if($_POST['inbox_checker'] == 'true') {
	$check_mailbox = '<div align="left">
  					  <div class="form-group row">
						<label class="col-sm-4 col-form-label">Confirm Mailbox <i class="icon-copy dw dw-question-1" data-container="addList" data-toggle="tooltip" data-placement="top" data-original-title="Send actual dummy emails to this list for second level validation. Make sure your dummy imap & smtp email account already configured!"></i><p style="font-size: 14px;line-height: 1em;width: 80%;"><small>
								Fully validate mailbox by sending actual dummy emails to this list. Make sure your dummy imap &amp; smtp email account already configured!
							</small></p></label>
						<div class="col-sm-8">
							<label class="btn_switch">
								<input type="checkbox" name="check_mailbox" id="check_mailbox" value="false">
								<span class="btn_slider round"></span>
							</label>
						</div>
					  </div>
					  
					</div>';
}

  $html .='</table><br />'.$check_mailbox.'<div align="right"><button type="button" name="import" id="import" class="btn btn-success" disabled>Import</button></div><br />';
 }
 else
 {
  $error = 'Only <b>.csv</b> file allowed';
 }
}
else
{
 $error = 'Please Select CSV File';
}

$output = array(
 'error'  => $error,
 'output' => $html
);

echo json_encode($output);


?>
