<?
$spam_words = [ 'go.tazalus.com', 'porn', 'dating', 'hierbalimon.es', 'bodyandsoul', 'promo', 'puertobelenn', 'faiterde.tk', 'tygyguip', 'maritim-touristik', 'toolbarqueries', 'allnews.elk.pl', 'fuck', 'delicatedate.net', 'allcnews.xyz', 'allcryptonnews', 'hinebixi.com', 'obermatsa', 'hilkom-digital.de', 'monkeydigital.co', 'CrytoPRove', 'no-site.com', 'strictlydigital', 'youtu.be', 'wilhetiperbe', 'blogspot.com', 'suck', 'Cryptaxbot', 'arryspasp@gmail.com', 'stomathartov@gmail.com', 'nellyspasp@gmail.com' ];

function strposa( $haystack, $needles = array(), $offset = 0 ) {
  $chr = array();
  foreach ( $needles as $needle ) {
    $res = strpos($haystack, $needle, $offset);
    if ($res !== false) $chr[$needle] = $res;
  }
  if ( empty($chr) )
    return false;
  return min($chr);
}

function saveContactData($name, $email, $address, $zip, $phone, $cooperation, $message) {
  $contactHistoryFile = $_SERVER['DOCUMENT_ROOT'] . '/vidok_it/forms.csv';

  $contactData = array(
      'Name' => str_replace( [ '\'', '"' ], '', $name ),
      'Email' => str_replace( [ '\'', '"' ], '', $email ),
      'Address' => str_replace( [ '\'', '"' ], '', $address ),
      'Zip_code' => str_replace( [ '\'', '"' ], '', $zip ),
      'Phone' => str_replace( [ '\'', '"' ], '', $phone ),
      'Cooperation' => str_replace( [ '\'', '"' ], '', $cooperation ),
      'Message' => str_replace( [ "\n", "\r\n", "\r" ], ' ', str_replace( [ '\'', '"' ], '', $message ) ),
      'Timestamp' => date('Y-m-d H:i:s', time())
  );

  if (!file_exists($contactHistoryFile)) {
      $header = array_keys($contactData);
      $history = array($header);
  } else {
      $history = array_map('str_getcsv', file($contactHistoryFile));
  }
  $history[] = $contactData;
  $csvContent = fopen($contactHistoryFile, 'w');
  foreach ($history as $row) {
      fputcsv($csvContent, $row);
  }
  fclose($csvContent);
}

if ( \S::get( 'action' ) == 'footer_contact_mail_send' and \S::get( 'token' ) == \S::get_session( 'form-token' ) and !\S::get( 'website' ) )
{
  if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
  }
  if(!$captcha){
    \S::alert( "Verifica reCaptcha fallita. Per favore riprova." );
    header( 'Location: ' . $_SERVER['REQUEST_URI'] );
    exit;
  }
  $secretKey = "6LfaovglAAAAAAz84QLdbONodP1yJUpSOVTnFMt4";
  $ip = $_SERVER['REMOTE_ADDR'];
  // post request to server
  $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
  $response = file_get_contents($url);
  $responseKeys = json_decode($response,true);
  // should return JSON with success as true
  if($responseKeys["success"]) {
    if (
        \S::get( 'name' ) and \S::get( 'email' ) and \S::get( 'indirizzo' ) and \S::get( 'phone' ) and \S::get( 'altre-informazioni' ) and strpos( \S::get( 'altre-informazioni' ), 'http://' ) === false and strpos( \S::get( 'altre-informazioni' ), 'https://' ) === false
      and
        strposa( $_POST['address'], $spam_words ) === false
      and
        strposa( $_POST['information'], $spam_words ) === false
      and
        strposa( $_POST['name'], $spam_words ) === false )
    {
      $text = '<p><b>Imię:</b> ' . \S::get( 'name' ) . '</p>';
      $text .= '<p><b>Email:</b> ' . \S::get( 'email' ) . '</p>';
      $text .= '<p><b>Adres:</b> ' . \S::get( 'indirizzo' ) . '</p>';
      $text .= '<p><b>Kod pocztowy:</b> ' . \S::get( 'zip_code' ) . '</p>';
      $text .= '<p><b>Telefon:</b> ' . \S::get( 'phone' ) . '</p>';
      $text .= '<p><b>Rodzaj współpracy:</b> ' . \S::get( 'kind_of_cooperation' ) . '</p>';
      $text .= '<p><b>Wiadomość:</b> ' . nl2br( \S::get( 'altre-informazioni' ) ) . '</p>';
      $text .= '<p style="font-size: 13px; color: #ccc; margin-top: 50px;"><b>Wysłano:</b> ' . date( 'Y-m-d H:i:s' ) . '</p>';

      saveContactData(
        \S::get('name'),
        \S::get('email'),
        \S::get('indirizzo'),
        \S::get('zip_code'),
        \S::get('phone'),
        \S::get('kind_of_cooperation'),
        \S::get('altre-informazioni')
      );

      if ( \S::send_email( 'm.kawa@vidok.com', 'Wiadomość ze strony it.vidok.com', $text, \S::get( 'email' ) ) )
      {
        \S::alert( \S::lang( 'wiadomosc-zostala-wyslana' ) );
      }
      else
      {
        \S::alert( \S::lang( 'wiadomosc-niezostala-wyslana' ) );
      }
    }
    else
    {
      \S::alert( \S::lang( 'wiadomosc-niezostala-wyslana' ) );
    }
  } else {
    \S::alert( "Sei un robot!" );
  }
  header( 'Location: https://it.vidok.com/#scontainer-18' );
  exit;
}

if (
    \S::get( 'action' ) == 'send-contact-modal' and \S::get( 'token' ) == \S::get_session( 'modal-token' ) and !\S::get( 'website' )
  and
    strposa( $_POST['address'], $spam_words ) === false
  and
    strposa( $_POST['information'], $spam_words ) === false
  and
    strposa( $_POST['name'], $spam_words ) === false )
{
  if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
  }
  if(!$captcha){
    \S::alert( "Verifica reCaptcha fallita. Per favore riprova." );
    header( 'Location: ' . $_SERVER['REQUEST_URI'] );
    exit;
  }
  $secretKey = "6LfaovglAAAAAAz84QLdbONodP1yJUpSOVTnFMt4";
  $ip = $_SERVER['REMOTE_ADDR'];
  // post request to server
  $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
  $response = file_get_contents($url);
  $responseKeys = json_decode($response,true);
  // should return JSON with success as true
  if($responseKeys["success"]) {

    $countfiles = count( $_FILES['files']['name'] );

    for ( $i = 0; $i < $countfiles; $i++ )
    {
      $filename = $_FILES['files']['name'][$i];
      if ( $filename )
      {
        move_uploaded_file( $_FILES['files']['tmp_name'][$i], 'temp/' . $filename );
        $files_to_send[] = 'temp/' . $filename;
      }
    }

    if ( \S::get( 'name' ) and \S::get( 'email' ) and \S::get( 'address' ) and \S::get( 'phone' ) and \S::get( 'information' ) and strpos( \S::get( 'information' ), 'http://' ) === false and strpos( \S::get( 'information' ), 'https://' ) === false )
    {
      $text = '<p><b>Imię:</b> ' . \S::get( 'name' ) . '</p>';
      $text .= '<p><b>Email:</b> ' . \S::get( 'email' ) . '</p>';
      $text .= '<p><b>Adres:</b> ' . \S::get( 'address' ) . '</p>';
      $text .= '<p><b>Kod pocztowy:</b> ' . \S::get( 'zip_code' ) . '</p>';
      $text .= '<p><b>Telefon:</b> ' . \S::get( 'phone' ) . '</p>';
      $text .= '<p><b>Rodzaj współpracy:</b> ' . \S::get( 'kind_of_cooperation' ) . '</p>';
      $text .= '<p><b>Wiadomość:</b> ' . nl2br( \S::get( 'information' ) ) . '</p>';
      $text .= '<p style="font-size: 13px; color: #ccc; margin-top: 50px;"><b>Wysłano:</b> ' . date( 'Y-m-d H:i:s' ) . '</p>';

      saveContactData(
        \S::get('name'),
        \S::get('email'),
        \S::get('address'),
        \S::get('zip_code'),
        \S::get('phone'),
        \S::get('kind_of_cooperation'),
        \S::get('information')
      );

      if ( \S::send_email( 'm.kawa@vidok.com', 'Wiadomość ze strony it.vidok.com', $text, \S::get( 'email' ), $files_to_send ) )
      {
        \S::alert( \S::lang( 'wiadomosc-zostala-wyslana' ) );
      }
      else
      {
        \S::alert( \S::lang( 'wiadomosc-niezostala-wyslana' ) );
      }
    }
    else
    {
      \S::alert( \S::lang( 'wiadomosc-niezostala-wyslana' ) );
    }
  }
  else
  {
    \S::alert( "Sei un robot!" );
  }

  header( 'Location: https://it.vidok.com/#scontainer-18' );
  exit;
}