<?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>");
}

$op = isset($_POST['op']) ? inCode($_POST['op']) : "" ;
$name = isset($_POST['name']) ? trim(inCode($_POST['name'])) : "" ;
$email = isset($_POST['email']) ? inCode($_POST['email']) : "" ;
$argument = isset($_POST['argument']) ? inCode($_POST['argument']) : "" ;
$subject = isset($_POST['subject']) ? trim(inCode($_POST['subject'])) : "" ;
$message = isset($_POST['message']) ? inCode($_POST['message']) : "" ;

$pagerow = $dblink->get_row("SELECT fullscreen FROM memht_pagine WHERE nome='$page'");
$fullScreen = intval($pagerow['fullscreen']);

require_once("inc/inc_header.php");
	if (isset($_GET['page'])) { page_title($page); }
	openTable();
	switch($op) {
		case "send":
			sendMail($name,$email,$argument,$subject,$message);
		break;		
		default:
			contForm();
		break;
	}
	closeTable();
require_once("inc/inc_footer.php");

function contForm() {
	global $dblink,$siteConfig,$visitorInfo,$userInfo,$userid;
	
	?>
	<form name="contact" id="contact" method="post" action="index.php?page=contact">
		<table  align="center">
		<?php
			if (isUser($userid)) {
				$user = $userInfo['user'];
				$email = $userInfo['email'];
			} else {
				$user = "";
				$email = "";	
			}
			echo "<tr><td align='right'><b>"._NAME_."</b></td><td><input type='text' name='name' size=30 value=\"$user\"></td></tr>\n";
			echo "<tr><td align='right'><b>"._EMAIL_."</b></td><td><input type='text' name='email' size=40 value=\"$email\"></td></tr>\n";
		?>			
		<tr><td align="right"><b><?php echo _ARGUMENT_; ?></b></td>
		<td><select name="argument">
		<option value="<?php echo _GENERALINFO_; ?>"><?php echo _GENERALINFO_; ?></option>
		<option value="<?php echo _PROBSITE_; ?>"><?php echo _PROBSITE_; ?></option>
		<option value="<?php echo _COMMERCIAL_; ?>"><?php echo _COMMERCIAL_; ?></option>
		<option value="<?php echo _PROGRAMMING_; ?>"><?php echo _PROGRAMMING_; ?></option>
		<option value="<?php echo _PERSONAL_; ?>"><?php echo _PERSONAL_; ?></option>
		<option value="<?php echo _OTHER_; ?>"><?php echo _OTHER_; ?></option>
		</select></td></tr>
		<tr><td align="right"><b><?php echo _OBJECT_; ?></b></td><td><input type="text" name="subject" size=40></td></tr>
		<tr><td align="right" valign="top"><b><?php echo _MESSAGE_; ?></b></td><td><textarea name="message" cols=30 rows=5></textarea></td></tr>
		<tr><td align="right"><b><?php echo _DATE_; ?></b></td><td><?php echo @date(Y.".".m.".".d)."-".@date(H.".".i); ?></td></tr>
		<tr><td align="right"><b><?php echo _IP_; ?></b></td><td><?php echo $visitorInfo['ip']; ?></td></tr>
		<?php
		if (extension_loaded('gd') AND $siteConfig['graphic_check']==1) {
			echo "<tr><td></td><td><img src='".$siteConfig['site_url']."/imgCode.php?name=chk_contact&nc=".md5(time()+rand())."' title='header=["._SECURITY_CODE_."] body=["._SECURITY_CODE_TEXT_."]' alt='Captcha'></td></tr>\n";
			echo "<tr><td align='right'><b>"._CODE_."</b></td><td><input type='text' name='post_code_page' size='8' maxlength='255'></td></tr>\n";
		}
		?>
		<tr><td colspan="2"><center><input type="submit" name="submit" value="<?php echo _SEND_; ?>"> <input type="reset" name="Submit" value="<?php echo _DELETE_; ?>"></center></td></tr>
		</table>
		<input type="hidden" name="op" value="send">
		<input type="hidden" name="ok" value=true>
	</form>
	<?php
}

function sendMail($name,$email,$argument,$subject,$message) {
	global $siteConfig,$visitorInfo;
	
	@session_start();
	if ((isset($_POST['post_code_page']) AND isset($_SESSION['chk_contact']) AND strtolower($_POST['post_code_page'])==strtolower($_SESSION['chk_contact'])) OR !(extension_loaded('gd') AND $siteConfig['graphic_check']==1)) {
	
		$send = true;
		if ($name=="" OR !validate($name)) { $send = false; $msg = "<b>"._FIELDINVALID_."</b>: "._NAME_; }
		if (!validEmail($email)) { $send = false; $msg = "<b>"._FIELDINVALID_."</b>: "._EMAIL_; }
		if ($subject=="" OR !validate($subject)) { $send = false; $msg = "<b>"._FIELDINVALID_."</b>: "._OBJECT_; }
		if ($message=="") { $send = false; $msg = "<b>"._EMPTY_."</b>: "._MESSAGE_; }
		
		if ($send) {
			$subject = $siteConfig['site_name'];
			$msg = "$subject\n\n";
		
			$msg .= _NAME_.": $name\n";
			$msg .= _EMAIL_.": $email\n";
			$msg .= _ARGUMENT_.": $argument\n";
			$msg .= _OBJECT_.": $subject\n";
			$msg .= _MESSAGE_.": $message\n";
			$msg .= _DATE_.": ".@date(Y.".".m.".".d)."-".@date(H.".".i)."\n";
			$msg .= _IP_.": ".$visitorInfo['ip']."\n\n";			
			
			sendEmail($siteConfig['webmaster_mail'],$subject,$msg,$name,$email,0);
			
			echo "<center><b>"._MAILSENT_."</b><br>";
			echo "<center>"._THANKFORCONTACT_."</center>";
		} else {
			echo "<div align='center' id='errorText'>$msg</div>";
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._WRONGSECCODE_."</b></div>";
	}
}

?>