<?php
$instagram = array('Name','Country','Email Address','Number of Followers','How would you describe your instagram profile? (e.g. tech, gamer, travel, etc.)','Please enter your instagram page/profile link below.','Please provide links to any other relevant media or social media channels (if applicable).','Please briefly describe what type of collaboration you have in mind and what you will require from Innx.');

if (session_id() == '') session_start();

require "formfiles/contact-config.php";

$error_message = '';

if (!isset($_POST['submit'])) {

  showForm();

} else { //form submitted

  $error = 0;
  
  if(!empty($_POST['name'])) {
  	$txt[0] = clean_var($_POST['name']);
  }
  else {
    $error = 1;
  }
  
    if(!empty($_POST['country'])) {
  	$txt[1] = clean_var($_POST['country']);
  }
  else {
    $error = 1;
  }
  
  if(!empty($_POST['email'])) {
  	$email[2] = clean_var($_POST['email']);
  	if (!validEmail($email[2])) {
  	  $error = 1;
  	  $email[3] = 'color:#FF0000;';
  	  $email[4] = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
	  }
  }
  else {
    $error = 1;
    $email[3] = 'color:#FF0000;';
  }

    if(!empty($_POST['subs'])) {
  	$txt[3] = clean_var($_POST['subs']);
  }
  else {
    $error = 1;
  } 

    if(!empty($_POST['describe'])) {
  	$txt[4] = clean_var($_POST['describe']);
  }
  else {
    $error = 1;
  } 
    if(!empty($_POST['link1'])) {
  	$txt[5] = clean_var($_POST['link1']);
  }
  else {
    $error = 1;
  } 

    if(!empty($_POST['link2'])) {
  	$txt[6] = clean_var($_POST['link2']);
  }
  else {
    $error = 1;
  } 
    if(!empty($_POST['mind'])) {
  	$txt[7] = clean_var($_POST['mind']);
  }
  else {
    $error = 1;
  } 

  if(empty($_POST['captcha_code'])) {
    $error = 1;
    $code[3] = 'color:#FF0000;';
  } else {
  	include_once "formfiles/contact-securimage.php";
		$securimage = new Securimage();
    $valid = $securimage->check($_POST['captcha_code']);

    if(!$valid) {
      $error = 1;
      $code[3] = 'color:#FF0000;';   
      $code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
    }
  }

  if ($error == 1) {
    $error_message = '<div style="font-weight:bold;font-size:90%;margin-bottom:5px;color:#ff0000;}">Please complete all the fields in this form.</div>';

    showForm();

  } else {
  	
  	if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
  	if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);  	
  	
    $body = "$instagram[0]: $txt[0]\r\n\r\n";
    $body .= "$instagram[1]: $txt[1]\r\n\r\n";
    $body .= "$instagram[2]: $email[2]\r\n\r\n";
    $body .= "$instagram[3]: $txt[3]\r\n\r\n";
    $body .= "$instagram[4]: $txt[4]\r\n\r\n";
    $body .= "$instagram[5]: $txt[5]\r\n\r\n";
    $body .= "$instagram[6]: $txt[6]\r\n\r\n";
    $body .= "$instagram[7]: $txt[7]\r\n\r\n";
    $body .= "$instagram[8]: $txt[8]\r\n\r\n";
    
   
    if (!$from) $from_value = $email[2];
    else $from_value = $from;
    
    require_once('formfiles/class.phpmailer.php');
    
    $mail = new PHPMailer();
    
    $mail->SetFrom($from_value);  
    $mail->AddReplyTo($email[2]);
    $mail->Subject = "$txt[0]给你发了一封influencers邮件";
    $mail->Body = $body;
    $mail->AddAddress($to);
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    }
    
    if (!$thank_you_url) {    
      if ($use_header_footer) {
				include $header_file;
				$form_width = '100%';
			}
      echo '<a name="cform"><!--Form--></a>'."\n";
      echo '<div id="formContainer" style="width: '.$form_width.';height: '.$form_height.';text-align: left; vertical-align: top;">'."\n";
      echo $GLOBALS['thank_you_message']."\n";
      echo '</div>'."\n";
      if ($use_header_footer) include $footer_file;
	  }
	  else {
	  	header("Location: $thank_you_url");
	  }
	  
	  session_unset();
    session_destroy();	  
       	
  }

} //else submitted



function showForm()

{
global $name, $email, $tel, $subject, $message, $instagram, $code, $txt;
global $where_included, $use_header_footer, $header_file, $footer_file;
global $form_width, $form_height, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width; 	

if ($use_header_footer) {
	include $header_file;
	$form_width = '100%';
}

echo <<<EOD
<a name="cform"><!--Form--></a>
<link rel="stylesheet" href="/css/style.css" type="text/css"  media="all">
<div id="formContainer" style="width:100%;">
{$GLOBALS['error_message']}


<form method="post" id="cForm" action="{$where_included}#cform">

{$instagram[0]}
<div>
  <input name="name" type="text" id="name">
</div>
{$instagram[1]}
<div>
  <input name="country" type="text" id="country">
</div>
{$instagram[2]}({$email[4]})
<div>
  <input name="email" type="email" id="email">
</div>
<div>
{$instagram[3]}
  <div>
    <input name="subs" type="text" id="subs">
  </div>
</div>
<div>
<div>
 {$instagram[4]}
  <div>
    <input name="describe" type="text" id="describe">
  </div>
</div>
<div>
  {$instagram[5]}
  <div>
    <input name="link1" type="text" id="link1">
  </div>
</div>
<div>
  {$instagram[6]}
  <div>
    <input name="link2" type="text" id="link2">
  </div>
</div>
<div>
  {$instagram[7]}
  <div>
    <input name="mind" type="text" id="mind">
  </div>
</div>
<span style="{$code[3]}">{$code[0]}</span><span style="color:#ff0000">*</span>
<img id="captcha" src="formfiles/contact-securimage_show.php" alt="CAPTCHA Image" onclick="document.getElementById('captcha').src = 'formfiles/contact-securimage_show.php?' + Math.random(); return false"/><input type="text" name="{$code[1]}" size="10" maxlength="5" id="{$code[1]}" /> 
(Please enter the text in the image above. Text is not case sensitive.)<br />
<a href="#" onclick="document.getElementById('captcha').src = 'formfiles/contact-securimage_show.php?' + Math.random(); return false">Click here if you cannot recognize the code.</a>
<br>
<center><input type="submit" name="submit" value="Submit" style="border:1px solid #999;margin-top:5px;" id="submit_button" /></center>

</form>

</div>
</div>
EOD;

if ($use_header_footer) include $footer_file;
}

function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
  return $variable;
}

/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && function_exists('checkdnsrr'))
      {
      	if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
         // domain not found in DNS
         $isValid = false;
       }
      }
   }
   return $isValid;
}


?>