<?php

/***************************************************************************
 *                           msgadm.php
 *
 *                           --------------------
 *   begin                : Thursday, 20 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.
 *
 ***************************************************************************/
session_set_cookie_params(600); // Session span. // Durata sesiunii.
session_start();

$submit=$_GET['submit'];
$userName=$_GET['username'];
$password=$_GET['password'];
$myUserName="username"; // Put your username here. // Pune numele de utlizator aici.
$myPassword="password"; // And here the password. // Iar aici parola.
$websiteURL="http://".$_SERVER['HTTP_HOST'];
$scriptURL=$websiteURL.$_SERVER['SCRIPT_NAME'];

if(!isset($messagesStoringFile))
	$messagesStoringFile = "msgrcv.txt"; // File of data storing. || Fisierul de stocare a datelor

switch($submit){
	case "Autentificare":
		if(empty($userName) || empty($password)){
			header("Location: ".$scriptURL);
			exit;
		}
		if($userName==$myUserName && $password==$myPassword){
			$_SESSION['user_is_logged_in'] = true;
			header("Location: ".$scriptURL);
		}
		else{
			echo "<!DOCTYPE HTML>";
			echo "<html>";
			echo "<head>";
			echo "</head>";
			echo "<body>";
			echo "<div style=\"text-align: center;\"><big style=\"color: rgb(255, 0, 0);\"><big>";
			echo "<br><br>Datele introduse nu sunt corecte.</big></big>";
			echo "<br><span style=\"color: rgb(255, 0, 0);\"><a size=\"-1\"style=\"font-family: Cursor;\"
				href=$scriptURL>Inapoi la autentificare</a></span>";
			echo "</big></big><br>";
			echo "</div>";
			echo "</body>";
			echo "</html>";
		}
		break;
	case "Iesire":
		session_destroy();
		header("Location: ".$websiteURL);
		break;
	case "Sterge":
		deleteMessages();
		header("Location: ".$scriptURL);
		break;
	default:
		displayAdmPage();
}
// Displaing Autentification page.
// Afisarea paginii de autentificare.
function displayAdmPage(){
	$msgNo=countMessages();
	if (isset($_SESSION['user_is_logged_in']) && $_SESSION['user_is_logged_in']==true){
		echo "<html><head></head><body style=\"color: rgb(0, 0, 0); background-color: rgb(16, 16, 153);\">
			<form method=\"Sterge\" action=\"msgadm.php\">
				<div style=\"text-align: right;\"><input name=\"submit\" value=\"Iesire\"
					align=\"middle\" type=\"submit\"></div>
			<table style=\"text-align: left; width: 100%;\" border=\"1\">
			  <tbody>
				<tr>
				  <td style=\"vertical-align: top; width: 33%; text-align: center; color: rgb(51, 204, 0);
				  	font-weight: bold; font-family: monospace;\"><big><big>Autor</big></big></td>
				  <td style=\"vertical-align: top; text-align: center;\"><big style=\"font-weight: bold;
				  	font-family: monospace;\"><big><span style=\"color: rgb(51, 204, 0);\">Mesaj</span></big></big><br>
				  </td>
				</tr>";
		readMessages();
		echo "</tbody>
			</table>
			<input name=\"submit\" value=\"Sterge\" align=\"middle\" type=\"submit\">
			</form>

			</body></html>";
	}
	else{
			echo "<html>
				<head>
				</head>
				<body style=\"color: rgb(0, 0, 0); background-color: rgb(16, 16, 153);\"
				 alink=\"#33cc00\" link=\"#33cc00\" vlink=\"#990099\">
				<div style=\"text-align: left;\">
				<big style=\"color: rgb(204, 204, 255);\">Aveti $msgNo mesaje primite.<br>
				Autentificare pentru citirea si administrarea mesajelor.<br>
				</big>
				</div>
				<form method=\"Trimite\" action=\"msgadm.php\">
				  <table border=\"0\">
					<tbody>
					  <tr>
						<td style=\"color: rgb(204, 255, 255);\">Utilizator <br>
						</td>
						<td style=\"color: rgb(0, 0, 0);\"><input maxlength=\"20\"
				 tabindex=\"1\" size=\"20\" name=\"username\"></td>
					  </tr>
					  <tr>
						<td style=\"color: rgb(204, 255, 255);\">Parola</td>
						<td><input maxlength=\"20\" tabindex=\"2\" size=\"20\" name=\"password\"
				 type=\"password\"></td>
					  </tr>
					  <tr>
						<td> <br>
						</td>
						<td style=\"text-align: left; vertical-align: middle;\"><input
						name=\"submit\" value=\"Autentificare\" align=\"middle\" type=\"submit\"></td>
					  </tr>
					</tbody>
				  </table>
				</form>
				</body>
				</html>";
	}
}

// Function for riding messages file an display messages in html format.
// Functie pentru parcurgerea fisierului de mesaje si afisarea in format html.
function readMessages(){
	global $messagesStoringFile;
	$msgNo=1;
	$sFfp=fopen($messagesStoringFile, 'r');
		while($buffer=fgets($sFfp)){
			list($nickName, $message, $contact, $time, $data, $IP)=split(" # ", $buffer, 6);
			echo "<tr>
				<td style=\"vertical-align: top; text-align: center; font-family: monospace;\"><font size=\"+1\"><span style=\"
					color: rgb(255, 204, 0); font-weight: bold; font-family: monospace;\">$nickName</span><br>
				</font>
				<div style=\"margin-left: 40px; font-family: monospace;\"><font size=\"+1\"><span style=\"color: rgb(255, 204, 0);
					font-weight: bold;\"></span></font></div>
				<div style=\"text-align: right;\">
				<div style=\"text-align: right; font-family: monospace;\"><font size=\"+1\"><span style=\"
					color: rgb(204, 204, 204);\">E-mail: $contact</span><br>
				<span style=\"color: rgb(204, 204, 204);\">Data: $data</span><br>
				<span style=\"color: rgb(204, 204, 204);\">Ora: $time</span></font></div>
				<span style=\"font-family: mon;\"></span>
				<table style=\"text-align: right; width: 100%;\" border=\"0\">
				  <tbody><tr>
					<td style=\"vertical-align: top;\"><font size=\"+1\"><span style=\"color: rgb(204, 204, 204);
						font-family: monospace;\"><span style=\"font-weight: bold; color: rgb(255, 102, 102);\">
						</span></span></font><big><big><span style=\"color: rgb(255, 102, 102);\">Stergere
						</span></big></big><input name=\"message[]\" value=\"$msgNo\" align=\"middle\" type=\"checkbox\"></td>
				  </tr>
				</tbody></table>
				</div>
				</td>
				<td style=\"vertical-align: top; color: rgb(204, 204, 204); font-family: monospace;\"><font size=\"+1\">
					<span style=\"font-family: monospace;\">  $message</span></font> </td>
				</tr>";
			$msgNo++;
		}
		fclose($sFfp);
}

// Function for delete messages.
// Functie pentru stergerea mesajelor.
function deleteMessages(){
	global $messagesStoringFile;
	$msgCount=0; // Count the messages that was delete. // Numara mesajele care au fost sterse.
	$lineCount=0; // Count the rided message line. // Numara liniile de mesaj parcurse.
	$_POST['message'];
	$message=$_REQUEST['message'];
	$msgNo=count($message); // Number of messages to delete. // Numarul mesajelor de sters.
	if(!$msgNo) return;
	$sFfp=fopen($messagesStoringFile, 'r');
	while($rBuffer=fgets($sFfp)){
		if($message[$msgCount]!=$lineCount+1){
			$wBuffer[$lineCount-$msgCount]=$rBuffer; // Store the messages that must remain. // Stocheaza mesajele care trebuie sa ramana.
		}
		else
			$msgCount++;
		$lineCount++;
	}
	fclose($sFfp);
	$sFfp=fopen($messagesStoringFile, 'w');
	flock($sFfp, LOCK_EX);
	for($i=0; $i<$lineCount-$msgCount; $i++)
		fwrite($sFfp, $wBuffer[$i]); // Rewrite in file the remaining messages. Rescrie in fisier mesajele ramase
	flock($sFfp, LOCK_UN);
	fclose($sFfp);
}

function countMessages(){
	global $messagesStoringFile;
	$lineCount=0; // Count the rided message line. // Numara liniile de mesaj parcurse.
	$sFfp=fopen($messagesStoringFile, 'r');
	while($rBuffer=fgets($sFfp)) $lineCount++;
	fclose($sFfp);
	return $lineCount;
}
 ?>
