<?php

$Path = str_replace('\\','/', __FILE__);
$Path = str_replace('contact-form/contact-form.php', '', $Path);

include_once($Path."includes/error_reporting.php");
include_once($Path.'gigkalender.ini.php');
include_once($Path.'functions/standard.php');
include_once($Path.'guestbook/functions.php');
include_once($Path."includes/frontend_language_selector.php");
foreach ($_POST as $key => $value) {
  $$key = trim($value); // ' -1\2'3"4<b>5&6; ' => '-1\2'3"4<b>5&6;'
  $$key = html_entity_decode($$key); // '&lt;b&gt;dog&lt;/b&gt;' => <b>dog</b>
  $$key = htmlspecialchars_decode($$key); // 'this -&gt; &quot;' => 'this -> "'
  $$key = stripslashes($$key); // O\'reilly? => O'reilly
  $$key = strip_tags($$key); } // 'Hi <b>there</b>' => 'Hi there'

$IPAddress = getRealIP();

// Prevent 'undefined index' notices
if (!isset($submit)) {
    $name = "";
    $email = "";
    $url = "";
    $message = "";
    $agreement = "";
    $email = "";
    $messageIsSpam = messageIsSpam('', '', $IPAddress, '', ''); }
else {
  $messageIsSpam = messageIsSpam($name, $message, $IPAddress, '', $url);
}

//echo "<br><br>".$messageIsSpam."<br><br>";
// Abort script
  if ($messageIsSpam=="blocked-ip") {
    http_response_code(404);
    die(); }
// Write spam log
  if ($messageIsSpam!='noSpam')
    writeSpamLog($name, '', $email, '', $url, '', $message, $IPAddress,
      $messageIsSpam, 'contact-form');

echo '<div class="GigKalender"><div id="Contact">';

if (isset($submit)
  && isset($name)
  && isset($email)
  && !empty($message)
  && isset($agreement)
  && $agreement=='checked'
  && checkEmail ($email)
  && $messageIsSpam=="noSpam" // in newer GigKalender it returned "noSpam", not "" anymore
  )
{
  // test for time spent to fill the form
  $diff = microtime(TRUE)-$AntiSpamTimestamp;
  $diff = number_format($diff, 2, '.', '');
  if ($diff<2.5) {
    writeSpamLog($name, '', $email, '', '', '', $message, $IPAddress,
      'form-completion-time: '.$diff.' s', 'contact-form');
    http_response_code(404);
    die(); }
  else {
  // replace multiple blank lines with one blank line
  $message = preg_replace("/(\r\s*){2,}/", "\n\n", $message); // Bug fix: v1.2.83_065
  //$message = str_replace("\r\n", "\n", $message);
  //$message = preg_replace("/(\R|\n){2,}/", "\n\n", $text, $message); // Bug fix: v1.2.83_065
  //$message = str_replace("\n", "\r\n", $message);
  $message = $L_114.":
".$name."

".$L_115.":
".$email."

".$L_119.":
".$message."

".$L_134.":
".$IPAddress."

".$L_Form_Completion_Time.":
".$diff." s

".$L_Block_IP_Address.":
".getCurrentURL()."?ip=".$IPAddress;

$message = wordwrap($message, 70, "\n");
  // Mail mit Header definieren
    $name = sprintf('=?%s?B?%s?=', strtoupper('UTF-8'),
      base64_encode($name)); // Bugfix v1.2.80_165
    $optheader[] = "From: ".$name." <".$email.">";
    $optheader[] = "Reply-To: ".$name." <".$email.">";
    $optheader[] = "User-Agent: GigKalender ".$GigKalenderVersion;
    $optheader[] = "MIME-Version: 1.0";
    $optheader[] = "Content-Type: text/plain; Charset=utf-8";
    $header = implode("\r\n", $optheader);
  // Mail versenden
    if (@mail(getSetup('SetupGuestbookMail'), $L_New_message_via_contact_form, $message, $header)) {
      echo '<p>'.$L_142.'</p>';
      unset($submit);
    }
    else
      echo '<p class="ErrorMessage">'.$L_ERROR_Email_could_not_be_sent.'</p>'; }
}
else
{
  if (isset($_GET["ip"])) {
    echo "<span class='ErrorMessage'>
      Blocking IP address: ".$_GET["ip"]."  ... ";
      $IPArray = explode(', ', getSetup('SetupGuestbookSpamIPs'));
      $IPArray[] = $_GET["ip"];
      $IPArray = array_unique($IPArray);
      $newIPs = implode(', ', $IPArray);
      $SQLQuery = "UPDATE ".$prefix."setup SET SetupGuestbookSpamIPs = '$newIPs'";
      $Result = mysqli_query($db, $SQLQuery);
      if ($Result) {
        echo 'IP address blocked!</span><br><br>'; }
      else echo 'Error 97<br>';
  }
  if (!isset($agreement)) $agreement = "";
  echo '<div id="ContactForm">
      <form action="'.getFrontendURL('SetupContactURL').'" method="post" accept-charset="UTF-8">
        <table>';
  // Name
  if (isset($submit)) {
    if (empty($name))
      echo '<tr><td></td><td class="ErrorMessage">'.$L_121.'</td></tr>';
      // Spam test
      elseif (preg_match('|word-in-name:|', $messageIsSpam))
        echo '<tr><td></td><td class="ErrorMessage">'.$L_124.'</td><td></td></tr>'; }
  echo '<tr>
          <td>'.$L_114.':</td>
          <td><input type="text" name="name" maxlength="40" value="'.$name.'" /></td>
          <td valign="top">*</td>
        </tr>';
  // Email
  if (isset($submit) && empty($email))
    echo '<tr><td></td><td class="ErrorMessage">'.$L_216.'</td></tr>';
  if (isset($submit) && !empty($email) && !checkEmail ($email))
    echo '<tr><td></td><td class="ErrorMessage">'.$L_122.'</td></tr>';
  echo '<tr>
          <td>'.$L_115.':</td>
          <td><input type="text" name="email" maxlength="80" value="'.$email.'" /></td>
          <td valign="top">*</td>
         </tr>';
  // Fake URL as spam trap
  echo '<tr style="display:none;">
          <td align="right" valign="top">'.$L_117.':</td>
          <td><input type="text" name="url" value="'.$url.'" /></td>
          <td></td>
        </tr>';
  // Message
  if (isset($submit)) {
    // Spam test
    if (preg_match('|word-in-message:|', $messageIsSpam))
      echo '<tr><td></td><td class="ErrorMessage">'.$L_124.'</td><td></td></tr>';
    elseif (preg_match ('|blocked-character:|', $messageIsSpam))
      echo '<tr><td></td><td class="ErrorMessage">'.$L_ERROR_Message_contains_invalide_characters.'</td><td></td></tr>';
    // empty message
    elseif (empty($message))
      echo '<tr><td></td><td class="ErrorMessage">'.$L_125.'</td><td></td></tr>'; }
  echo '<tr>
          <td>'.$L_119.':</td>
          <td><textarea name="message" rows="8" cols="" >'.$message.'</textarea></td>
          <td valign="top">*</td>
         </tr>';
  if (isset($submit) && $agreement!="checked")
    echo '<tr><td></td><td class="ErrorMessage">'.$L_ERROR_Missing_agreement.'</td></tr>';
  // Define string with link to privacy policy
    // Check if a Link can be set
      if ($SetupFrontendPrivacyPolicyURL) $Link = '<a href="'.$SetupFrontendPrivacyPolicyURL.'">'.$L_Privacy_Policy.'</a>';
        else $Link = $L_Privacy_Policy;
    // replace variable %1 in language string with the link
      $L_Field_Agreement = str_ireplace("%1", $Link, $L_Field_Agreement);
  // AntiSpamTimestamp
    if (!isset($submit)) {
      $AntiSpamTimestamp = microtime(TRUE); }
  // Button
  echo '<tr>
          <td></td>
          <td>
            <input style="width:auto" type="checkbox" name="agreement" value="checked" '.$agreement.'/>
            '.$L_Field_Agreement.'
          </td>
          <td valign="top">*</td>
         </tr>
         <tr>
          <td></td>
          <td><input type="submit" name="submit" value="'.$L_Button42.'" class="button" /></td>
          <input type="hidden" name="AntiSpamTimestamp" value="'.$AntiSpamTimestamp.'" />
         </tr>
      </table>
    </form></div>';
}
echo '</div></div>';
?>