<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isSet($_POST['op'])) { $op = inCode($_POST['op']); } else { $op = ""; }
if (isSet($_POST['email'])) { $email = inCode($_POST['email']); } else { $email = ""; }

function subscribe($email) {
	global $dblink,$visitorInfo,$siteConfig;
	
	if ($dblink->get_num("SELECT * FROM memht_newsletter WHERE email='$email'")>0) {
		echo "<div align='center' id='errorText'><b>"._EMAIL_ALREADY_PRESENT_."</b></div>";
	} else {
		if (validEmail($email)) {
			if (memRunHooks('AddEmailToNewsletter',array($email))) {
				$dblink->query("INSERT INTO memht_newsletter VALUES (null,'$email','".$visitorInfo['ip']."')");
				
				memRunHooks('AddEmailToNewsletterEnd',array($email));
			}
			
			$subject = "Welcome to ".$siteConfig['site_name']."!";
			$msg = "Welcome to ".$siteConfig['site_name']."!\n\n";
			$msg .= "You're email has been subscribed to our newsletter.\n";
			$msg .= "If the subscription is unwanted or done without you're agreement, you can unsubscribe from here:\n";
			$msg .= $siteConfig['site_url']."/index.php?page=newsletter\n\n";
			$msg .= "Subscriber IP address: ".$visitorInfo['ip']."\n";
			
			sendEmail($email,$subject,$msg,$siteConfig['site_name'],$siteConfig['webmaster_mail'],0);
			
			echo "<div align='center'><b>"._SUBSCRIBED_."</b></div>";
		} else {
			echo "<div align='center' id='errorText'><b>"._MAILNOTVALID_."</b></div>";
		}
	}
}

function unsubscribe($email) {
	global $dblink,$visitorInfo,$siteConfig;
	
	if ($dblink->get_num("SELECT * FROM memht_newsletter WHERE email='$email'")>0) {
		if (validEmail($email)) {
			$subject = "Unsubscribed from ".$siteConfig['site_name']."!";
			$msg = "You're email has been unsubscribed from our newsletter.\n";
			$msg .= "If the unsubscription is unwanted or done without you're agreement, you can subscribe again from here:\n";
			$msg .= $siteConfig['site_url']."/index.php?page=newsletter\n\n";
			$msg .= "Unsubscriber IP address: ".$visitorInfo['ip']."\n";
				
			sendEmail($email,$subject,$msg,$siteConfig['site_name'],$siteConfig['webmaster_mail'],0);
			
			if (memRunHooks('RemoveEmailFromNewsletter',array($email))) {
				$dblink->query("DELETE FROM memht_newsletter WHERE email='$email'");
				
				memRunHooks('RemoveEmailFromNewsletterEnd',array($email));
			}
			echo "<div align='center'><b>"._UNSUBSCRIBED_."</b></div>";
		} else {
			echo "<div align='center' id='errorText'><b>"._MAILNOTVALID_."</b></div>";
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._EMAIL_NOT_PRESENT_."</b></div>";
	}
}

function showNewsletter() {
	global $siteConfig;

	echo "<table align='center'><form name='newsletter_subscribe' method='post' action='index.php?page=newsletter'>\n";
		echo "<tr><th>"._EMAIL_."</th><td><input name='email' type='text' size='30' maxlength='100'></td></tr>\n";
		
		if (extension_loaded('gd') AND $siteConfig['graphic_check']==1) {
			echo "<tr><td></td><td><img src='".$siteConfig['site_url']."/imgCode.php?name=chk_newsletter&nc=".md5(time()+rand())."' title='header=["._SECURITY_CODE_."] body=["._SECURITY_CODE_TEXT_."]' alt='Captcha'></td></tr>\n";
			echo "<tr><th>"._CODE_."</th><td><input type='text' name='post_code_page' size='5' maxlength='255'>";
			echo " <select name='op'>\n";
				echo "<option value='subscribe' selected>"._SUBSCRIBE_."</option>\n";
				echo "<option value='unsubscribe'>"._UNSUBSCRIBE_."</option>\n";
			echo "</select> <input type='submit' name='Submit' value='"._OK_."'></td></tr>\n";
		} else {
			echo "<tr><th><b>"._OPERATION_."</b></th><td><select name='op'>\n";
				echo "<option value='subscribe' selected>"._SUBSCRIBE_."</option>\n";
				echo "<option value='unsubscribe'>"._UNSUBSCRIBE_."</option>\n";
			echo "</select> <input type='submit' name='Submit' value='"._OK_."'></td></tr>\n";
		}

	echo "</form></td></tr></table>\n";
}

	openTable();

	switch($op) {
		case "subscribe":
			@session_start();
			if ((isSet($_POST['post_code_page']) AND isSet($_SESSION['chk_newsletter']) AND strtolower($_POST['post_code_page'])==strtolower($_SESSION['chk_newsletter'])) OR !(extension_loaded('gd') AND $siteConfig['graphic_check']==1)) {
				subscribe($email); } else { echo "<div align='center' id='errorText'><b>"._WRONGSECCODE_."</b></div>"; }
		break;
		
		case "unsubscribe":
			@session_start();
			if ((isSet($_POST['post_code_page']) AND isSet($_SESSION['chk_newsletter']) AND strtolower($_POST['post_code_page'])==strtolower($_SESSION['chk_newsletter'])) OR !(extension_loaded('gd') AND $siteConfig['graphic_check']==1)) {
				unsubscribe($email); } else { echo "<div align='center' id='errorText'><b>"._WRONGSECCODE_."</b></div>"; }
		break;
				
		default:
			showNewsletter();
		break;
	}
	
	closeTable();

?>