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

require_once ('top.php');

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

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload or delete images</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>images</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> ] [ Upload or delete images ] [ <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

	$imgfolder = LOCATION . 'img/';

	if (array_key_exists('submit1', $_POST)) {

		$filename = trim($_POST['filename']);

		$filetype = exif_imagetype($_FILES['upload']['tmp_name']);
		$allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);

		if (in_array($filetype, $allowed)) {
			$pieces = explode('.', $_FILES['upload']['name']);
			$extension = strtolower(array_pop($pieces));
			$extensions = array("jpg", "jpeg", "gif", "png");
			if (in_array($extension, $extensions)) {
				$filename = $filename . '.' . $extension;
				if (move_uploaded_file($_FILES['upload']['tmp_name'], "../../img/$filename")) {
					$response1 = '<em>The file named <b>' . $filename . '</b> has been uploaded.</em><br>';
					$display = '<img src="../../img/' . $filename . '" />';
				} else {
					$response1 = '<em>The file could not be moved.</em><br>';
				}
			} else {
				$response1 = '<em>The file type must be .jpg, .jpeg, .gif, or .png.</em><br>';
			}
		} else {
			$response1 = '<em>Not uploaded. The file you renamed <b>' . $filename . '</b> was not actually an image.</em><br>';
		}
	}

	if (array_key_exists('submit2', $_POST)) {

		if (strlen($_POST['delete']) < 1) {
			$problem = TRUE;
			$response2 = '<em>No image filename was entered.</em>';
		} else if (!$problem) {
			$imagefile = '../../img/' . $_POST['delete'];
			if (file_exists($imagefile)) {
				unlink($imagefile);
				$response2 = '<em>Success. <b>' . $_POST['delete'] . '</b> was deleted.</em>';
			} else {
				$response2 = '<em>Sorry, <b>' . $_POST['delete'] . '</b> does not exist. Try another.</em>';
			}
		}
	}

?>

<h3>Upload an image</h3>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Select image (max width 640 pixels):</label><br>
<input type="file" name="upload"><br>
<label>Name the file (omit file extension - eg image1):</label><br>
<input type="text" size="40" name="filename" maxlength="40"><input type="hidden" name="MAX_FILE_SIZE" value="20000" /><input type="submit" name="submit1" value="Upload">
</form>

<?php echo $response1; ?>
<?php echo $display; ?>

<hr>

<h3>Delete an image</h3>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Image to be deleted (include file extension - eg image1.jpg):</label><br>
<input type="text" name="delete" size="40" value="<?php if (isset($_POST['submit2'])) echo $_POST['delete']; ?>" maxlength="40" /><input type="submit" name="submit2" value="Delete image">
</form>

<?php echo $response2; ?>

	<div id="list">

<?php

	$view = LOCATION . 'img/view.php?img=';

	echo '<ul>';
	echo "<li>Existing image files <em>(copy grey 'markup' to paste in page)</em>:</li>";
	$dirname = "../../img";
	if ($folder = opendir($dirname)) {
		$filesArray = array();
		while (false !== ($file = readdir($folder))) {
			if ((strstr($file, '.jpg')) || (strstr($file, '.jpeg')) || (strstr($file, '.gif')) || (strstr($file, '.png'))) {
				$filesArray[] = $file;
			}
		}
		natcasesort($filesArray);
		foreach ($filesArray as $file) {
			print  '<li><a href="' . $view . $file . '" title="View">' . $file . '</a>&nbsp;&nbsp; <span>&lt;img src="img/' . $file . '" alt=""&gt;<span></li>';
		}
		closedir($folder);
		echo "</ul>\n";
	}

?>

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