<?php

/***************************************************************************
 *                           msgrcv.php
 *
 *                           --------------------
 *   begin                : Thursday, 18 sept, 2008
 *   owner                : Qumbetlian
 *   email                : qumbetlian@yahoo.com
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is a free script; 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 option) any later version.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   Acest program este un script liber; il puteti redistribui si/sau modifica
 *   sub termenii Licentei Publice Generale asa cum este publicata de
 *   Fundatia Free Software; fie in versiunea a doua a licentei, sau 
 *   (la alegerea dvs.) oricarei versiuni ulterioare.
 *
 ***************************************************************************/

// Maximum number of messages allowed to be send in one day.
// Numarul maxim de mesaje permise sa fie trimise intr-o zi.
$maxDailyMsg=2;
$cookieName="messagesSentToQumbetlian";
$websiteURL="http://".$_SERVER['HTTP_HOST'];
$localURL=$websiteURL.dirname($_SERVER['SCRIPT_NAME']);
$localURL=trim($localURL,"/");

// If storing file vas not set, then set storing file.
// Daca fisierul de stocare nu a fost precizat, atunci fixeaza fisierul de stocare.
if(!isset($messagesStoringFile))
	$messagesStoringFile = "msgrcv.txt"; // File of data storing. || Fisierul de stocare a datelor

echo "<!DOCTYPE HTML>
	<html>
	<head>
	</head>
	<body>
	<div style=\"text-align: center;\"><big style=\"color: rgb(255, 0, 0);\"><big>
	<br><br>";

// Check if provided data is not void
// Verificam daca datele furnizate nu sunt goale
if(!empty($nickname)&&!empty($contact)&&!empty($message)){
	// Check the number of send messages.
	// Verificam numarul de mesaje trimise.
	if(($msgNo=$_COOKIE[$cookieName])<$maxDailyMsg && userMessagesCount()<$maxDailyMsg){	
		$sFfp=fopen($messagesStoringFile, 'a');
		flock($sFfp, LOCK_EX);
		$buffer=$nickname." # ".$message." # ".$contact." # ".date("H:i:s")
			." # ".date("d/m/Y")." # ".$_SERVER['REMOTE_ADDR']."\n";
		fwrite($sFfp, $buffer);
		flock($sFfp, LOCK_UN);
		fclose($sFfp);
		// Setting the number of message sended in a cookie.
		// Setarea numarului de mesaje trimise intr-un cookie.
		setcookie($cookieName, "$msgNo"+1, time()+60*60*24);
		echo "Mesajul a fost trimis cu succes.";
	}
	else
		echo "Nu poti trimite mai mult de 2 mesaje intr-o zi.";
	echo "</big></big><br><br><span style=\"color: rgb(255, 0, 0);\">
		<a size=\"-1\"style=\"font-family: Cursor;\"href=$websiteURL>
		Prima pagina
		</a>";
}
else
	echo "Eroare la trimiterea mesajului.<br>Te rog verifica daca ai completat toate campurile!<br>
		</big></big><br><span style=\"color: rgb(255, 0, 0);\">
		<a size=\"-1\"style=\"font-family: Cursor;\"href=$localURL/msgsend.html>
		Compune un nou mesaj.
		</a>";

echo "</big></big><br>
	</div>
	</body>
	</html>";

// Function for counting messages sending by an user today.
// Functie pentru numararea mesajelor trimise de un utilizator astazi.
function userMessagesCount(){
	global $maxDailyMsg, $messagesStoringFile;
	$count=0;
	$sFfp=fopen($messagesStoringFile, 'r');
	while($buffer=fgets($sFfp)){
		list($nickname, $message, $contact, $time, $data, $IP)=split(" # ", $buffer);
		if(!strcmp($data.$IP, date("d/m/Y").$_SERVER['REMOTE_ADDR']."\n"))
			$count="$count"+1;
	}
	fclose($sFfp);
	return $count;
}
	  
?>
