<?php
#############################################################################
#           SUPERMAILER SUBSCRIBE/UNSUBSCRIBE SCRIPT                        #
#           Copyright © 2001-2009 Mirko Boeer                               #
#           http://www.supermailer.de/                                      #
#                                                                           #
# Dieses Script kann kostenlos eingesetzt werden, jedoch muss dieser Header #
# im Script enthalten bleiben!                                              #
# This script can be used for free. The header must be contained in script! #
#                                                                           #
# Systemvoraussetzungen: PHP 4 und Windows/Unix                             #
# 14.04.2009                                                                #
#############################################################################

# Geben Sie die E-Mail-Adresse Ihres POP3/IMAP Postfachs an
# Enter the email address of your POP3/IMAP inbox
$Recipient="anabmeldung@up-to-date.ch";

# Geben Sie die Seite an, die angezeigt werden soll, wenn eine ANmeldung erfolgte
# Angabe mit http://
# Enter the page that should be shown if a recipient will bei added to your recipients list
# With http://
$SubscribeOKPage="";

# Geben Sie die Seite an, die angezeigt werden soll, wenn eine ABmeldung erfolgte
# Angabe mit http://
# Enter the page that should be shown of a recipients will be removed from you recipients list
# With http://
$UnsubscribeOKPage="https://energieallianz-linth.ch/abmeldung-vom-newsletter-erfolgreich";

# Geben Sie die Seite an, die angezeigt werden soll, wenn die E-Mail-Adresse aeusserlich nicht korrekt ist = Fehlerseite
# Angabe mit http://
# Enter the page that should be shown if an error ocurrs = email address not correct
# With http://
$ErrorPage="https://energieallianz-linth.ch/newsletter-fehler";

########### Ab hier nichts mehr aendern #####################
########### Don't change anything at this position #####################
$CRLF = "\r\n";

# FALLS register_globals off
if (ini_get('register_globals') == 0) {
  
  if ( isset($_GET['Action']) )
     $Action=$_GET['Action'];

  if ( isset($_GET['EMail']) )
     $EMail=$_GET['EMail'];

  if ( isset($_POST['EMail']) )
     $EMail=$_POST['EMail'];
     
  if ( isset($_POST['Action']) )
     $Action=$_POST['Action'];
     
  if ( isset($_GET['Format']) )
     $Format=$_GET['Format'];

  if ( isset($_POST['Format']) )
     $Format=$_POST['Format'];
     
  if ( isset($_POST['RG']) )
     $RG=$_POST['RG'];

  if ( isset($_GET['RG']) )
     $RG=$_GET['RG'];

  if ( (!isset($REMOTE_ADDR)) || ($REMOTE_ADDR == "") )
     $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
}

function CheckEMail($email) {
 if (strpos($email, "@") === False)
   return 0;
 $s = substr($email, strpos($email, "@"), strlen($email));
 if (count(explode(".", $s)) < 2)
   return 0;

 if (!(strpos($email, "\n") === False))
   return 0;

 if (!(strpos($email, "\r") === False))
   return 0;

 if (!(strpos($email, ",") === False))
   return 0;

 if (!(strpos($email, ";") === False))
   return 0;

 if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email)){
        return 1;
   }else {
        return 0;
   }

}

function CheckForSpam($str) {
  if ( eregi("from:",$str) || eregi("to:",$str) || eregi("multipart",$str) || eregi("cc:",$str) || eregi("bcc:",$str) )
    return 1;
  return 0;
}


if ( (!isset($EMail)) || ($EMail == "") || ( !CheckEMail($EMail) ) ) {
  if ($ErrorPage != "") 
     header("Location: $ErrorPage");
  else
   print "<b>Es wurde keine E-Mail-Adresse angegeben oder die E-Mail-Adresse liegt nicht im korrekten Format vor!</b>";
  exit; 
}

if ( (!isset($Action)) || ($Action == "") ) {
  print "Es wurde das Feld Action nicht übermittelt!";
  exit;
}


##################################### Spam test
$teststring="";
reset ($_GET);
while (list ($key, $val) = each ($_GET)) {
 $teststring .= "$key=$val";
}

reset ($_POST);
while (list ($key, $val) = each ($_POST)) {
 $teststring .= "$key=$val";
}

if (CheckForSpam($teststring) == 1) {
  print "Error processing form data";
  exit;
}
#####################################

$headers = "From: $EMail".$CRLF;
$headers .= "Return-Path: <$EMail>".$CRLF;
  

# $Action als Betreff
# message als Text der Mail
# Format des Mailtextes: 
#        FeldName: <feldinhalt>\n
$message = "EMail: $EMail\n";  
if (isset($Format)) 
  $message .= "Format: $Format\n";  

# Fuer weitere Felder, diesen muessen im HTML-Formular und im SuperMailer angelegt werden!
  #Fuer weitere Felder, diesen muessen im HTML-Formular und im SuperMailer angelegt werden!

reset ($_GET);
while (list ($key, $val) = each ($_GET)) {
  if ($key == "Action") continue;
  if ($key == "EMail") continue;
  if ($key == "Format") continue;
  if ($key == "SubmitBtn") continue;

  $pos = strpos ($key, "[]");
  if ($pos !== false) {
    $key = substr($key, 0, $pos - 1);
  }
  if (is_array($val))  # RG as array?
    $message .= "$key: ".join(";", $val)."\n";
    else
    $message .= "$key: ".$val."\n";
}

reset ($_POST);
while (list ($key, $val) = each ($_POST)) {
  if ($key == "Action") continue;
  if ($key == "EMail") continue;
  if ($key == "Format") continue;
  if ($key == "SubmitBtn") continue;

  $pos = strpos ($key, "[]");
  if ($pos !== false) {
    $key = substr($key, 0, $pos - 1);
  }
  if (is_array($val))  # RG as array?
    $message .= "$key: ".join(";", $val)."\n";
    else
    $message .= "$key: ".$val."\n";
}


$message .= "IP: ".$REMOTE_ADDR."\n";

# Versenden der Mail
if(!@mail($Recipient, $Action, $message, $headers))
   print "Can't send email.";

if ($Action == "subscribe")
   if ($SubscribeOKPage != "")
     header("Location: $SubscribeOKPage");
     else
     print "<b>Die E-Mail Adresse wurde zu unserer Newsletterliste hinzugefügt!</b>";
 else
   if ($UnsubscribeOKPage != "")
     header("Location: $UnsubscribeOKPage");
     else
     print "<b>Die E-Mail Adresse wurde aus unserer Newsletterliste entfernt!</b>";
?>