<?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("../$textfilename")) {
		$file_contents = stripslashes(file_get_contents("../$textfilename"));
		$lines = explode("\n",$file_contents);
		if (strlen($lines[0]) < 1) {
			$file_contents = "\n$file_contents";
		}
		$file_contents = htmlspecialchars($file_contents);
	} else {
		$response1 = "<em>No content. A page named <b>$filetitle</b> could not be found.</em>";
	}
}

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

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create a new page or edit an existing page</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>pages</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> ] [ Create/edit/delete pages ] [ <a href="images.php" title="Upload or delete images">Upload or delete images</a> ] [ <a href="comments.php" title="Create/edit/delete comments">Create/edit/delete comments</a> ] [ <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

function _uscore($string) {
	$first = $string[0];
	if (is_numeric($first)) {
		return substr_replace($string, '_', 0, 0);
	} else {
		return $string;
	}
}

/* ---------------------------------------------------------------------- */
/* New Page */

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

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

		if (preg_match("/[^A-Za-z0-9_-]/", $_POST['page_id'])) {
			$problem1 = true;
			$response1 = "<em>The page filename can contain only letters, numbers, hypens, and underscores.</em> ";
		}

		if (strlen($_POST['content']) < 1) {
			$problem1 = true;
			$response2 = "<em>You didn't enter any content.</em>";
		}

		if (!$problem1) {

			$filetitle = trim($_POST['page_id']);
			$filetitle = preg_replace("/^-/", "", $filetitle);
			$cleanfiletitle = str_replace("-", "", $filetitle);
			$cleanfiletitle = _uscore($cleanfiletitle);

			$textfilename = $filetitle . '.txt';
			if (file_exists("../$textfilename")) {
				$response1 = "<em>Sorry, this page name already exists. Try another.</em>";
			} else {

				$pagecontent = stripslashes($_POST['content']);
				$linesarray = explode("\r\n", $pagecontent);
				if (strstr($linesarray[0], "%")) {
					if (file_exists('../inc/inmenu.txt')) {
						$inmenu = "../inc/inmenu.txt";
						$fp = fopen($inmenu, "ab") or die("Couldn't open $inmenu.");
						$towrite = "$filetitle" . PHP_EOL;
						$towrite = removeEmptyLines($towrite);
						fwrite($fp, $towrite);
						fclose($fp);
					}
				}

				@touch("../$textfilename");
				@touch("../comments/$textfilename");
				$phpfilename = $filetitle . '.php';
				@touch("../$phpfilename");

				$template = '<?php

include (\'inc/page.class.php\');
$' . $cleanfiletitle . ' = new Page;
if (file_exists($textfilename)) {
	$file_contents = file_get_contents($textfilename);
	$output = $file_contents;
} else {
	$output = "No content. The associated text file could not be found.";
}
$' . $cleanfiletitle . '->Textfilename = "' . $textfilename . '";
$' . $cleanfiletitle . '->SetContent($output);
$' . $cleanfiletitle . '->Template();

?>';

				$filename = "../$phpfilename";
				$fp = fopen($filename, "wb") or die("Couldn't open $filename.");
				fwrite($fp, $template);
				fclose($fp);

				$textfilename = "../$textfilename";
				$fp = fopen($textfilename, "wb") or die("Couldn't open $textfilename.");
				fwrite($fp, $pagecontent);
				fclose($fp);

				$anchor = $filetitle;
				$filetitle = str_replace("index", "", $filetitle);
				if ($cms_dir) {
					$fileurl = LOCATION . $cms_dir . $filetitle . '.php';
				} else {
					$fileurl = LOCATION . $filetitle;
				}
				$response1 = "<em>Success. The page <b><a href=\"$fileurl\">$anchor</a></b> was created.</em>";
			}
		}
	}

/* ---------------------------------------------------------------------- */
/* Get Page Content*/

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

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

/* ---------------------------------------------------------------------- */
/* Prepare to Edit Page */

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

		$filetitle = trim($_POST['page_id']);
		$textfilename = $filetitle . '.txt';
		if (strlen($filetitle) < 1) {
			$response1 = "<em>You didn't enter a filename.</em>";
		} else if ($filetitle == 'stylesheet.css') {
			$response1 = "<em>You can't edit the stylesheet with this button.</em>";
		} else if (!file_exists("../$textfilename")) {
			$warning = "<em>Sorry, this page name does not exist. Try another or create the page.</em>";
		} else {
			$warning = "<em>You are about to edit</em> <b>$filetitle</b> &raquo; click 'Save page' button again, or [ <a href=\"index.php\" title=\"Abort\">abort</a> ]";
		}
	}

/* ---------------------------------------------------------------------- */
/* Edit Page */

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

		$filetitle = trim($_POST['page_id']);
		if (strlen($filetitle) < 1) {
			$problem3 = true;
			$response1 = "<em>You didn't enter a filename.</em>";
		} else if ($filetitle == 'stylesheet.css') {
			$problem3 = true;
			$response1 = "<em>You can't edit the stylesheet with this button.</em>";
		}

		if (!$problem3) {

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

				$anchor = $filetitle;
				$filetitle = str_replace("index", "", $filetitle);
				if ($cms_dir) {
					$fileurl = LOCATION . $cms_dir . $filetitle . '.php';
				} else {
					$fileurl = LOCATION . $filetitle;
				}
				$response1 = "<em>The page <b><a href=\"$fileurl\">$anchor</a></b> was successfully edited.</em>";

				if (file_exists('../inc/inmenu.txt')) {
					$delete = $filetitle;
					$inmenu = file_get_contents('../inc/inmenu.txt');
					$result = preg_replace('/\b' . $delete . '\b
/', '', $inmenu, 1);
					file_put_contents('../inc/inmenu.txt', $result);
					unset($inmenu);

					$linesarray = explode("\n", $pagecontent);
					if (strstr($linesarray[0], "%")) {
						$inmenu = "../inc/inmenu.txt";
						$fp = fopen($inmenu, "ab") or die("Couldn't open $inmenu.");
						$towrite = "$filetitle" . PHP_EOL;
						$towrite = removeEmptyLines($towrite);
						fwrite($fp, $towrite);
						fclose($fp);
					}
				} else {
					$response2 = "<em>The file <b>inmenu.txt</b> does not exist.</em>";
				}
			}
		}
	}

/* ---------------------------------------------------------------------- */
/* Prepare to Delete Page */

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

		$delete = trim($_POST['page_id']);
		$to_delete = '../' . $delete . '.php';
		if (strlen($delete) < 1) {
			$response1 = "<em>You didn't enter a filename.</em>";
		} else if (!file_exists($to_delete)) {
			$response1 = "<em>Sorry, the page <b>$delete</b> does not exist.</em>";
		} else if ($delete == 'index') {
			$response1 = "<em>You can't delete <b>index</b> (the home page).</em>";
		} else if ($delete == 'stylesheet.css') {
			$response1 = "<em>You can't delete the stylesheet with this button.</em>";
		} else {
			$warning = "<em>You are about to delete</em> <b>$delete</b> &raquo; click 'Delete page' button again, or [ <a href=\"index.php\" title=\"Abort\">abort</a> ]";
		}
	}

/* ---------------------------------------------------------------------- */
/* Delete Page */

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

		$delete = trim($_POST['page_id']);
		$phpfilename = '../' . $delete . '.php';
		$phpfilename_comments = '../comments/' . $delete . '.php';
		$textfilename = '../' . $delete . '.txt';
		$textfilename_comments = '../comments/' . $delete . '.txt';
		if ($delete == 'index') {
			$response1 = "<em>You can't delete <b>index</b> (the home page).</em>";
		} else {
			if (file_exists($phpfilename)) {
				unlink($phpfilename);
				$response1 = '<em>Success. <b>' . $delete . '</b> was deleted.</em> ';
			} else {
				$response1 = '<em>Sorry, the page <b>' . $delete . '</b> does not exist.</em> ';
			}
			if (file_exists($textfilename)) {
				unlink($textfilename);
				if (file_exists($textfilename_comments)) {
					unlink($textfilename_comments);
				}
			}

			if (file_exists('../inc/inmenu.txt')) {
				$inmenu = file_get_contents('../inc/inmenu.txt');
				$result = preg_replace('/\b' . $delete . '\b
/', '', $inmenu, 1);
				$result = removeEmptyLines($result);
				file_put_contents('../inc/inmenu.txt', $result);
			}
		}
	}

/* ---------------------------------------------------------------------- */
/* Get Current Stylesheet */

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

		$cssfilename = "../inc/stylesheet.css";
		if (!file_exists($cssfilename)) {
			$response1 = "<em>Sorry, the stylesheet does not exist. Get default styles then save styles.</em>";
		} else {
			$file_contents = file_get_contents($cssfilename);
		}
	}

/* ---------------------------------------------------------------------- */
/* Prepare to Edit Stylesheet */

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

		$warning = "<em>You are about to edit the stylesheet</em> &raquo; click 'Save styles' button again, or [ <a href=\"index.php\" title=\"Abort\">abort</a> ]";
	}

/* ---------------------------------------------------------------------- */
/* Edit Stylesheet */

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

		$cssfilename = "../inc/stylesheet.css";
		if (!file_exists($cssfilename)) {
			$response1 = "<em>Sorry, the stylesheet does not exist. Get default styles then save styles.</em>";
		} else {
			$csscontent = stripslashes($_POST['content']);
			$fp = fopen($cssfilename, "wb") or die("Couldn't open $cssfilename.");
			fwrite($fp, $csscontent);
			fclose($fp);
			$response1 = "<em>The stylesheet was successfully edited.</em>";
		}
	}

/* ---------------------------------------------------------------------- */
/* Get Default Stylesheet */

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

		$cssfilename = "../inc/default.css";
		if (!file_exists($cssfilename)) {
			$response1 = "<em>Sorry, this file does not exist.</em>";
		} else {
			$file_contents = file_get_contents($cssfilename);
		}
	}

?>

<h3>Create/edit/delete a page</h3>

	<div id="response">

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

	</div>

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

	<div id="boxes">

<label>Page filename (no spaces, dots or file suffix; -hyphens and _underscores ok)</label><br>
<input type="text" name="page_id" size="60" style="font-weight: bold; color: #c63;" value="<?php

	if (isset($_POST['submit8'])) {
		$filetitle == FALSE;
		$page == FALSE;
		$cssfilename == FALSE;
		echo "";
	} else if ((isset($_POST['submit5'])) || (isset($_POST['pre-submit6'])) || (isset($_POST['submit7']))) {
		echo "stylesheet.css";
	} else if ($page == TRUE) {
		echo $page;
	} else {
		$page = trim(stripslashes($_POST['page_id']));
		$page = preg_replace("/^-/", "", $page);
		echo $page;
	}

?>
" maxlength="60" /> <strong>&laquo;&nbsp;page filename</strong><br>
<label>1st line = banner text, 2nd line = heading, leave 3rd line blank, 4th line onwards = content<br>
1st line options: <em>#</em> undisplays banner text, <em>%</em> adds page to menu, <em>~</em> password-protects page</label><br>
<textarea name="content" cols="90" rows="20"><?php

	if (isset($_POST['submit8'])) {
		$file_contents == FALSE;
		echo "";
	} else if ((isset($_POST['submit1'])) || (isset($_POST['pre-submit3'])) || (isset($_POST['submit3'])) || (isset($_POST['pre-submit4'])) || (isset($_POST['submit4']))) {
		echo htmlspecialchars(stripslashes($_POST['content']));
	} else if ((isset($_POST['submit2'])) || (isset($_POST['submit5'])) || (isset($_POST['submit7']))) {
		echo stripslashes($file_contents);
	} else if ((isset($_POST['pre-submit6'])) || (isset($_POST['submit6']))) {
		echo stripslashes($_POST['content']);
	} else if ($page == TRUE) {
		echo stripslashes($file_contents);
	} else {
		echo '#%~ example banner text
Page Title

Content...';
	}

?>
</textarea>

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

<input type="submit" name="submit1" value="Create page">
<input type="submit" name="submit2" value="Get page content">
<input type="submit" name="<?php

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

?>" value="Save page">
<input type="submit" name="<?php

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

?>" style="color: red;" value="Delete page">
<input type="submit" name="submit8" value="Clear all">

<p>Stylesheet:</p>

<input type="submit" name="submit5" value="Get current styles">
<input type="submit" name="<?php

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

?>" value="Save styles">
<input type="submit" name="submit7" value="Get default styles">

	</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>