<?php
/**
 * la.plume Micro CMS
 * ==================
 * COPYRIGHT Patrick Taylor http://www.mini-print.com/
 */

require_once ('top.php');

$page = cleanText($_GET['page']);
if ($page) {
	$filetitle = trim($page);
	$textfilename = $filetitle . '.txt';
	if (file_exists("../comments/$textfilename")) {
		$file_contents = stripslashes(file_get_contents("../comments/$textfilename"));
		$file_contents = htmlspecialchars($file_contents);
		if (strlen($file_contents) < 1 ) {
			$response1 = "<em>No action requested. The comments file is currently empty.</em>";
		}
	} else {
		$response1 = "<em>Sorry, this comments file does not exist.</em>";
	}
}

?>
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit comments</title>
<meta name="robots" content="noindex,nofollow">
<link rel="stylesheet" href="stylesheet.css" type="text/css" media="screen">
</head>
<body>

<div id="wrap">

<h1>la.plume Admin: <i>comments</i></h1>

<?php

include_once('password.php');
if (($word != trim($enc_password)) || (strlen(trim($enc_password)) < 1)) {

?>

<form id="pw" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>" method="post">
<label><b>Enter password:</b></label><br>
<input type="hidden" name="form" value="login">
<input type="password" name="password" size="25" maxlength="25"><br>
<input type="submit" name="submit0" value="Submit Password">
</form>

<?php

	if (PWFORM_SUBMITTED) {
		if (md5($_POST['password']) != trim($enc_password)) {
			echo '<p><em>Sorry. Wrong password.</em></p>';
		}
	}

	if (($status == 'logout') || ($user == 'disappointed')) {
		echo '<p><a href="' . LOCATION . $cms_dir . '">' . $_SERVER['HTTP_HOST'] . '</a> (logged out)</p>';
	}

} else if ((COOKIE_EXISTS && ($_COOKIE['textadmin'] == trim($enc_password))) || (PWFORM_SUBMITTED && (md5($_POST['password']) == trim($enc_password)))) {

	if (file_exists("../checkup.txt")) {
		$checkup = "../checkup.txt";
		unlink($checkup);
	}

?>

<p>[ <a href="<?php echo LOCATION . $cms_dir; ?>">Site</a> ] [ <a href="index.php" title="Create/edit/delete pages">Create/edit/delete pages</a> ] [ <a href="images.php" title="Upload or delete images">Upload or delete images</a> ] [ Create/edit/delete comments ] [ <a href="setup.php" title="Setup">Setup</a> ] [ <a href="<?php echo $_SERVER['PHP_SELF']; ?>?status=logout" title="Logout">Logout</a> ] <a href="http://www.mini-print.com/CMS-help" title="Help" target="_blank">Help&nbsp;&raquo;</a></p>

<?php

/* ---------------------------------------------------------------------- */

	if (isset($_POST['submit1'])) {

		if (strlen($_POST['comments_id']) < 1) {
			$filetitle = FALSE;
			$response1 = "<em>You didn't enter a page name.</em>";
		} else {
			$filetitle = trim($_POST['comments_id']);
			$textfilename = $filetitle . '.txt';
			if (!file_exists("../comments/$textfilename")) {
				$response1 = "<em>Sorry, this comments file does not exist.</em>";
			} else if (strlen(stripslashes(file_get_contents("../comments/$textfilename"))) < 1 ) {
				$response1 = "<em>The comments file is currently empty.</em>";
			} else {
				$file_contents = stripslashes(file_get_contents("../comments/$textfilename"));
				$file_contents = htmlspecialchars($file_contents);
			}
		}
	}

/* ---------------------------------------------------------------------- */

	if (isset($_POST['pre-submit2'])) {

		$overwrite = stripslashes($_POST['content']);
		$filetitle = trim($_POST['comments_id']);
		if (strlen($overwrite) < 1) {
			$response1 = "<em>You didn't enter any comments.</em>";
		} else {
			$warning = "<em>You are about to edit</em> comments for <b>$filetitle</b> &raquo; click 'Save comments' button again, or [ <a href=\"comments.php\" title=\"Abort\">abort</a> ]";
		}
	}

/* ---------------------------------------------------------------------- */

	if (isset($_POST['submit2'])) {

		$filetitle = trim($_POST['comments_id']);
		if (strlen($filetitle) < 1) {
			$problem = true;
			$response1 = "<em>You didn't enter a filename.</em>";
		}

		if (!$problem) {

			$textfilename = $filetitle . '.txt';
			if (!file_exists("../comments/$textfilename")) {
				$response1 = "<em>Sorry, this comments file does not exist. Try another.</em>";
			} else {
				$textfilename = "../comments/$textfilename";
				$commentscontent = stripslashes($_POST['content']);
				$fp = fopen($textfilename, "wb") or die("Couldn't open $textfilename.");
				fwrite($fp, $commentscontent);
				fclose($fp);

				$anchor = $filetitle;
				if ($cms_dir) {
					$fileurl = LOCATION . $cms_dir . $filetitle . '.php';
				} else {
					$fileurl = LOCATION . $filetitle;
				}
				$response1 = "<em>The comments on <b><a href=\"$fileurl\">$anchor</a></b> were successfully edited.</em>";
			}
		}
	}

?>

<h3>Create/edit/delete comments<?php
	if (COMMENTS) {
		echo ' is <em>enabled</em>';
	} else {
		echo ' is <em>disabled</em>';
	}
?></h3>

	<div id="response">

<?php if (!$warning && !$response1) { echo "<em>No action requested.</em>"; } ?>
<?php echo $warning; ?>
<?php echo $response1; ?>

	</div>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="UTF-8">

	<div id="boxes">

<label>Enter page name (see existing pages below):</label><br>
<input type="text" name="comments_id" size="60" style="font-weight: bold; color: #c63;" value="<?php

	if (isset($_POST['submit3'])) {
		$filetitle == FALSE;
		$page == FALSE;
		echo "";
	} else if ((isset($_POST['submit1'])) || (isset($_POST['pre-submit2'])) || (isset($_POST['submit2']))) {
		echo $filetitle;
	} else {
		echo $page;
	}

?>
" maxlength="60" /><br>
<label>Edit comments - for horizontal line between comments add &lt;hr&gt; (blank line above and below):</label><br>
<textarea name="content" cols="90" rows="20"><?php

	if (isset($_POST['submit3'])) {
		$file_contents == FALSE;
		$overwrite == FALSE;
		$commentscontent == FALSE;
		echo "";
	} else if ((isset($_POST['submit1'])) || ($page == TRUE)) {
		echo $file_contents;
	} else if (isset($_POST['pre-submit2'])) {
		echo htmlspecialchars($overwrite);
	} else if (isset($_POST['submit2'])) {
		echo htmlspecialchars($commentscontent);
	} else {
		echo "";
	}

?>
</textarea>

	</div>
	<div id="buttons">

<?php if (COMMENTS) { ?>
<input type="submit" name="submit1" value="Get comments">
<input type="submit" name="<?php

	if (!isset($_POST['pre-submit2'])) {
		echo 'pre-submit2';
	} else {
		echo 'submit2';
	}

?>" value="Save comments">
<input type="submit" name="submit3" value="Clear all">

<?php } ?>
	</div>

</form>

	<div id="list">

<?php include_once('list.php'); ?>

	</div>

<p style="padding-top: 5px; border-top: 3px solid #ebebeb;">la.plume Micro CMS version <?php echo VERSION; ?> | latest version: <em><?php

	$getversion = "http://www.mini-print.com/version.php";
	if ($getversion) {
		$version = @fopen($getversion, "r");
		if ($version) {
			while(!feof($version)) {
				print fgets($version, 1024);
			}
			@fclose($version);
		}
	}

?></em></p>

<?php

} else {
	echo '<p>Login could not be verified.</p>';
}

?>

</div>

</body>
</html>