<?php
  header("Content-Type: text/html;charset=UTF-8");
  $recipientmail = 'info@cultureelerfgoed.nl'; //recipient
  $subject = 'Kontaktanfrage von technischemitteilungen.com'; //subject line of E-Mail
  $mailheader = 'Jemand hat das Kontaktformular auf technischemitteilungen.com ausgefüllt.'.PHP_EOL.'Bitten finden Sie die Anfrage und Daten nachfolgend:'.PHP_EOL.PHP_EOL; //header at the beginning of the E-Mail (not SMTP Headers)
  $mailfooter = PHP_EOL.PHP_EOL.'Viele Grüße,'.PHP_EOL.'Das Kontaktformular auf technischemitteilungen.com'; //footer at the end of the E-Mail
  $mailfieldseparator = PHP_EOL; //separator put between each field in E-Mail

  $fields = array(
    'name'  => array(
      'rule' => 'regex',
      'regex' => '/^.+$/',
      'label' => 'Ihr Name: ',
      'errormsg' => 'Bitte geben Sie einen Namen ein.'
    ),
    'email' => array( //name of field in html form
      'rule' => 'email', //validation rule
      'regex' => false, //regex (if 'rule' == 'regex')
      'label' => 'Ihre E-Mail Adresse: ', //label in E-Mail
      'errormsg' => 'Bitte geben Sie eine gültige E-Mail-Adresse ein.' //error message if validation failed
    ),
    'strasse'  => array(
      'rule' => 'none',
      'regex' => false,
      'label' => 'Ihre Straße: ',
      'errormsg' => 'Bitte geben Sie eine gültige Straße ein.'
    ),
    'hausnummer'  => array(
      'rule' => 'none',
      'regex' => false,
      'label' => 'Hausnummer: ',
      'errormsg' => 'Bitte geben Sie eine gültige Hausnummer ein.'
    ),
    'plz'  => array(
      'rule' => 'regex',
      'regex' => '/^[0-9]+$/',
      'label' => 'Ihre PLZ: ',
      'errormsg' => 'Bitte geben Sie eine gültige PLZ ein.'
    ),
    'ort'  => array(
      'rule' => 'none',
      'regex' => false,
      'label' => 'Ihr Ort: ',
      'errormsg' => 'Bitte geben Sie Ihren Ort ein.'
    ),
    'land'  => array(
      'rule' => 'none',
      'regex' => false,
      'label' => 'Ihr Land: ',
      'errormsg' => 'Bitte geben Sie Ihr Land ein.'
    ),
    'nachricht'  => array(
      'rule' => 'length',
      'regex' => 3,
      'label' => 'Ihre Nachricht: '.PHP_EOL,
      'errormsg' => 'Bitte geben Sie eine Nachricht ein, um das Formular abschicken zu können'
    ),
    //'phone' => array(
      //'rule' => 'regex',
      //'regex' => '/^[0-9]+$/',
      //'label' => 'Phone: ',
      //'errormsg' => 'Please enter a valid Phone Number.'
    //),
  );

  $tokenmismatchmsg = 'Entschuldigen Sie bitte, es ist leider ein Fehler beim Versenden des Kontaktformulars aufgetreten.'; //error message if user reloads browser and submits form again.
  $mailsendfailedmsg = 'Das Abschicken des Formulars ist Fehlgeschlagen. Bitte Entschuldigen Sie die Unannehmlichkeiten und versuchen Sie es später erneut.'; //error message if mail() failed.

  $successmsg = '<div class="success">Vielen Dank für Ihre Nachricht!<br />Wir werden Ihre Anfrage so bald als möglich bearbeiten!</div>'; //info message if contact form was sent successfully.
  $errormsg = '<div class="error"><ul><li>{{ content }}</li></ul></div>'; //template for error messages. {{ content }} is replaced with one or more error messages
  $errormsgseparator = '</li><li>'; //separator put between each error message.

  ////////////////////

  session_start();
  $errors = array();
  $emailfields = array();
  $formstatus = null;

  //set array to represent current form content
  //can be displayed later in HTML input fields
  foreach ($fields as $key => $value) {
    if(array_key_exists($key, $_POST)){
      $formContent[$key] = htmlentities(mb_eregi_replace('(^\\s+)|(\\s+$)/u', '', utf8_decode($_POST[$key])));
    }else{
      $formContent[$key] = '';
    }
  }

  if(array_key_exists('submit', $_POST)){
    $formstatus = true;
    if($_SESSION['formtoken'] === $_POST['token']){
      foreach($fields as $fieldName => $field){
        //clean input from leading/trailing spaces and stuff
        $fieldValue = mb_eregi_replace('(^\\s+)|(\\s+$)/u', '', $_POST[$fieldName]);
        
        //check if defined field exists in POST array
        if(!array_key_exists($fieldName, $_POST)){
          $formstatus = false;
          $errors[] = 'input field missing';
          continue;
        }
        
        //prepare each form field for email
        $emailfields[] = $field['label'] . $fieldValue;
        
        switch ($field['rule']) {
          case 'regex':
            //field has to match regex
            $regex = $field['regex'];
            if(preg_match($regex, $fieldValue) !== 1){
              $errors[] = $field['errormsg'];
              $formstatus = false;
            }
            break;
          case 'email':
            //field has to be a valid email address
            $regex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$/';
            if(preg_match($regex, $fieldValue) !== 1){
              $errors[] = $field['errormsg'];
              $formstatus = false;
            }
            break;
          case 'notempty':
            //field may not be empty
            if(empty($fieldValue)){
              $errors[] = $field['errormsg'];
              $formstatus = false;
            }
            break;
          case 'length':
            if(strlen($fieldValue) < intval($field['regex'])){
              $errors[] = $field['errormsg'];
              $formstatus = false;
            }
            break;
          case 'none':
            //field doesn't need to be validated. (e. g. optional field)
            break;
          default:
            //validation rule doesn't exist
            $formstatus = false;
            $errors[] = 'invalid rule '.$field['rule'];
            break;
        }
      }
    } else {
      //form token doesn't match
      $errors[] = $tokenmismatchmsg;
      $formstatus = false;
    }
    
    if($formstatus === true){
      //prepare mail content
      $mailcontent = $mailheader.implode(PHP_EOL, $emailfields).$mailfooter;
      
      //prepare additional SMTP Headers
      $headers =  'From: '.$_POST['email'].PHP_EOL;
      $headers .= 'Reply-To: '.$_POST['email'].PHP_EOL;
      
      //send email
      $result = mail($recipientmail, $subject, $mailcontent, $headers);
      if($result === false){
        $errors[] = $mailsendfailedmsg;
        $formstatus = false;
      }else{
        //empty formContent
        foreach($formContent as $key => $value){
          $formContent[$key] = '';
        }
      }
    }
    
    //prepare error message according to settings above
    $errors = implode($errormsgseparator, $errors);
    $errormsg = str_replace('{{ content }}', $errors, $errormsg);
  }
  $token = uniqid();
  $_SESSION['formtoken'] = $token;

?>
<html>
  <head>
    <meta charset="utf-8">
    <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">-->
    <meta name="viewport" content="width=device-width; initial-scale=1.0;" />
    <link rel="icon" href="images/favicon.ico" type="image/x-icon" />
    <title>Technische Mitteilungen | KEIMFARBEN</title>
    <meta name="description" content="Hier finden Sie die Technischen Mitteilungen der Firma Keimfarben. Ausserdem gibt es viele interessante Informationen hierzu!" />
    <meta name="keywords" content="Keimfarben, Keim, mineralisch, Farben, Technisch, Technische Mitteilungen, historisch, Mitteilung technische" />
    <link type="text/css" rel="stylesheet" media="screen" href="css/style.css">
    <link type="text/css" rel="stylesheet" media="only screen and (max-width: 1220px)" href="css/normal.css" />
    <link type="text/css" rel="stylesheet" media="only screen and (max-width: 980px)" href="css/narrow.css" />
    <link type="text/css" rel="stylesheet" media="only screen and (max-width: 740px)" href="css/mobile.css" />
  </head>
  <body>
    <div class="page clearfix" id="page">
      <header id="section-header" class="section section-header">
        <div id="zone-branding" class="zone zone-branding clearfix container-12">
          <div id="flag">
            <a href="en" id="logo" title="englische Sprachversion" class="active">
              <img typeof="foaf:Image" src="images/en.gif" alt="englische Sprachversion" />
            </a>
          </div>
          <div class="titletext">
            Technische Mitteilungen für Malerei
          </div>
          <div class="logo-img">
            <span class="additionallogo">Mit Unterstützung von</span>
            <!--<a href="http://www.culturalheritageagency.nl/en" target="_blank" id="logo" title="Logo RCE" class="active"> 
              <img typeof="foaf:Image" src="images/logo_rce.png" alt="RCE" />
            </a>-->
            <a href="http://www.keimfarben.de/" id="logo" target="_blank" title="Logo Keimfarben" class="active">
              <img typeof="foaf:Image" src="images/logo.png" alt="Logo Keimfarben" />
            </a>
          </div>
        </div>
        <div id="zone-menu-wrapper" class="zone-wrapper zone-menu-wrapper clearfix">  
          <div id="zone-menu" class="zone zone-menu clearfix container-12">
            <div class="grid-12 region region-menu" id="region-menu">
              <ul class="nice-menu nice-menu-down nice-menu-main-menu" id="nice-menu-1">
                <li class="menuparent menu-path-taxonomy-term-2 first even">
                  <a href="index.html" title="Home">
                    Startseite
                  </a>
                </li>
                <li class="menu-487 menuparent  menu-path-taxonomy-term-3 odd ">
                  <a href="https://www.technischemitteilungen.com/book/" target="_blank" title="Zu den Technischen Mitteilungen für Malerei">
                    Zu den Technischen Mitteilungen für Malerei
                  </a>
                </li>
                <li class="menu-488 menuparent  menu-path-taxonomy-term-4  even ">
                  <a href="pdf/tmfm_jahrgaenge_1884-1942.pdf" target="_blank" title="Index TMM">
                    Index TMM
                  </a>
                </li>
                <li class="menu-1118 menu-path-node-168  even ">
                  <a href="contact.php">Kontakt</a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </header>    
    <section id="section-content" class="section section-content">
      <div id="zone-content-wrapper" class="zone-wrapper zone-content-wrapper clearfix">  
        <div id="zone-content" class="zone zone-content clearfix container-12">    
          <div class="grid-12 region region-content" id="region-content">
            <div class="region-inner region-content-inner">
              <a id="main-content"></a>
              <h1 class="title" id="page-title">Kontakt</h1>
              <div class="block block-system block-main block-system-main odd block-without-title" id="block-system-main">
                <div class="block-inner clearfix">           
                  <div class="content clearfix">
                    <article about="/kreatives-gestalten/gestalten-mit-farbe" typeof="sioc:Item foaf:Document" class="node node-webform node-published node-not-promoted node-not-sticky author-sascha odd clearfix" id="node-webform-159">
                      <div class="content clearfix">
                        <div class="field field-name-body field-type-text-with-summary field-label-hidden">
                          <div class="field-items">
                            <div class="field-item even" property="content:encoded"> 
                              <div class="field field-name-body field-type-text-with-summary field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                      <img alt="" src="images/kontakt.png" style="width: 630px; height: 273px;" />
                                    <p>
                                      <span class="intext">*Pflichtfeld</span>
                                    </p>
                                  </div>
                                </div>
                              </div>
                              <?php
                                if($formstatus === true){
                                  echo $successmsg;
                                }elseif($formstatus === false){
                                  echo $errormsg;
                                }
                              ?>
                              <form class="webform-client-form" action="" method="post" id="webform-client-form-159" accept-charset="UTF-8">
                                <div>
                                  <div class="form-item webform-component webform-component-email" id="webform-component-ihre-e-mail-adresse">
                                    <label for="edit-submitted-ihre-e-mail-adresse">
                                      Ihre E-Mail Adresse
                                      <span class="form-required" title="Diese Angabe wird benötigt.">*</span>
                                    </label>
                                    <input class="email form-text form-email required" type="email" id="edit-submitted-ihre-e-mail-adresse" name="email" size="60" value="<?php echo $formContent['email']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-ihr-name">
                                    <label for="edit-submitted-ihr-name">
                                      Ihr Name 
                                      <span class="form-required" title="Diese Angabe wird benötigt.">*</span>
                                    </label>
                                    <input type="text" id="edit-submitted-ihr-name" name="name" size="60" maxlength="128" class="form-text required" value="<?php echo $formContent['name']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-stra-e">
                                    <label for="edit-submitted-stra-e">
                                      Ihre Straße
                                    </label>
                                    <input type="text" id="edit-submitted-stra-e" name="strasse" size="60" maxlength="128" class="form-text" value="<?php echo $formContent['strasse']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-hausnummer">
                                    <label for="edit-submitted-hausnummer">
                                      Ihre Hausnummer
                                    </label>
                                    <input type="text" id="edit-submitted-hausnummer" name="hausnummer" size="60" maxlength="128" class="form-text" value="<?php echo $formContent['hausnummer']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-plz">
                                    <label for="edit-submitted-plz">
                                      Ihre PLZ
                                      <span class="form-required" title="Diese Angabe wird benötigt.">*</span>
                                    </label>
                                    <input type="text" id="edit-submitted-plz" name="plz" size="60" maxlength="128" class="form-text required" value="<?php echo $formContent['plz']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-ihr-ort">
                                    <label for="edit-submitted-ihr-ort">
                                      Ihr Ort
                                    </label>
                                    <input type="text" id="edit-submitted-ihr-ort" name="ort" size="60" maxlength="128" class="form-text" value="<?php echo $formContent['ort']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textfield" id="webform-component-land">
                                    <label for="edit-submitted-land">
                                      Ihr Land
                                    </label>
                                    <input type="text" id="edit-submitted-land" name="land" size="60" maxlength="128" class="form-text" value="<?php echo $formContent['land']; ?>" />
                                  </div>
                                  <div class="form-item webform-component webform-component-textarea" id="webform-component-ihre-nachricht">
                                    <label for="edit-submitted-ihre-nachricht">
                                      Ihre Nachricht
                                      <span class="form-required" title="Diese Angabe wird benötigt.">*</span>
                                    </label>
                                    <div class="form-textarea-wrapper resizable">
                                      <textarea id="edit-submitted-ihre-nachricht" name="nachricht" cols="60" rows="14" class="form-textarea"><?php echo $formContent['nachricht']; ?></textarea>
                                    </div>
                                  </div>
                                  <div class="form-actions form-wrapper" id="edit-actions">
                                    <input type="hidden" name="token" value="<?php echo $token; ?>">
                                    <input type="submit" id="edit-submit" name="submit" value="Nachricht abschicken" class="form-submit" />
                                  </div>
                                </div>
                              </form>
                            </div>
                          </div>
                        </div>
                      </div>
                    </article>    
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>  
    <footer id="section-footer" class="section section-footer">
      <div id="zone-footer-wrapper" class="zone-wrapper zone-footer-wrapper clearfix">  
        <div id="zone-footer" class="zone zone-footer clearfix container-12">
          <div class="grid-12 region region-footer-first" id="region-footer-first">
            <div class="region-inner region-footer-first-inner">
              <div class="block block-menu block-menu-footer-menu block-menu-menu-footer-menu odd block-without-title" id="block-menu-menu-footer-menu">
                <div class="block-inner clearfix">          
                  <div class="content clearfix">
                    <ul class="menu">
                      <li class="first leaf">
                        <a href="impressum.html">
                          Impressum
                        </a>
                      </li>
                      <li class="leaf">
                        <a href="datenschutz.html">
                          Datenschutz
                        </a>
                      </li>
                      <li class="last leaf">
                        <a href="contact.php">
                          Kontakt
                        </a>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
<div class="grid-4 region region-footer-second" id="region-footer-second">
  <div class="teaser-text">Die Technischen Mitteilungen für Malerei sind ein Gemeinschaftsprojekt der <b><a href="http://www.culturalheritageagency.nl/en" target="_blank">Cultural Heritage Agency of the Netherlands</a></b> und <b><a href="http://keimfarben.de/" target="_blank">KEIMFARBEN.</a></b> </div>
            &nbsp;
          </div>
          <div class="grid-8 region region-footer-third" id="region-footer-third">
            &nbsp;
          </div>
        </div>
      </div>
    </footer>
  </div>
  </body>
</html>
