<?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>Setup page for la.plume Micro CMS</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>setup</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 (!empty($GLOBALS['_SERVER'])) {
		$_SERVER_ARRAY = '_SERVER';
	} else if (!empty($GLOBALS['HTTP_SERVER_VARS'])) {
		$_SERVER_ARRAY = 'HTTP_SERVER_VARS'; // *HTTP_SERVER_VARS is deprecated
	} else {
		$_SERVER_ARRAY = 'GLOBALS';
	}

	$serverSoftware = ${$_SERVER_ARRAY}['SERVER_SOFTWARE'];
	$phpSelf = ${$_SERVER_ARRAY}['PHP_SELF'];
	$scriptFilename = ${$_SERVER_ARRAY}['SCRIPT_FILENAME'];
	$scriptName = ${$_SERVER_ARRAY}['SCRIPT_NAME'];
	$requestURI = ${$_SERVER_ARRAY}['REQUEST_URI'];
	$httpHost = ${$_SERVER_ARRAY}['HTTP_HOST'];
	$serverName = ${$_SERVER_ARRAY}['SERVER_NAME'];

	if (isset($serverSoftware)) {
		$apache_test = $serverSoftware;
		if (stristr($apache_test, 'Apache')) {
			$apache = TRUE;
		}
	} else if (in_array("Apache", $_SERVER) || in_array("apache", $_SERVER)) {
		$apache = TRUE;
	} else {
		$apache = 'notdetected';
	}

	$self = dirname(__FILE__); // Current folder (full server path)
	if (empty($self)) {
		if (!empty($phpSelf)) {
			$self = htmlspecialchars($phpSelf, ENT_QUOTES, 'utf-8');
		} else if (!empty($scriptFilename)) {
			$self = htmlspecialchars($scriptFilename, ENT_QUOTES, 'utf-8');
		} else if (!empty($scriptName)) {
			$self = htmlspecialchars($scriptName, ENT_QUOTES, 'utf-8');
		} else if (!empty($requestURI)) {
			$self = htmlspecialchars($requestURI, ENT_QUOTES, 'utf-8');
		} else {
			$response1 = "<em>Problem: server path not detected.</em>";
		}
	}

	if (isset($httpHost)) {
		$site = $httpHost;
	} else if (isset($serverName)) {
		$site = $serverName;
	} else {
		$response2 = "<em>Problem: site location not detected.</em>";
	}

	if (strstr($self, 'laplume')) {
		$site_location = $site . '/laplume/';
	} else {
		$site_location = $site . '/';
	}

	if (file_exists("../contact.php")) {
		$contact_page = TRUE;
	} else {
		$contact_page = FALSE;
	}

	if (file_exists("../inc/settings.php")) {
		$settings = TRUE;
		$setup = TRUE;
	} else {
		$settings = FALSE;
		$setup = FALSE;
	}

	if (file_exists("../index.txt")) {
		$index = TRUE;
	} else {
		$index = FALSE;
	}

	if ($setup == FALSE) {
		$response1 = "<em>No action requested. Setup does not yet exist.</em>";
	}

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

		$home_link = trim($_POST['home_link']);
		$home_link = stripAccents($home_link);
		$home_link = cleanText($home_link);
		$home_link = utf8_it($home_link);
		if (strlen($home_link) < 1) {
			$problem = TRUE;
			$home_link = FALSE;
		}

		$name = trim($_POST['name']);
		$name = stripAccents($name);
		$name = cleanText($name);
		$name = utf8_it($name);
		if (strlen($name) < 1) {
			$problem = TRUE;
			$name = FALSE;
		}

		$comments = trim($_POST['comments']);
		if (($comments != "YES") && ($comments != "NO")) {
			$problem = TRUE;
			$comments = FALSE;
		} else if ($comments == "YES") {
			$comment = "TRUE";
		} else if ($comments == "NO") {
			$comment = "FALSE";
		}

		if ($contact_page == TRUE) {

			$email = trim($_POST['email']);
			if (strlen($email) < 1) {
				$problem = TRUE;
				$email = FALSE;
			}
			if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
				$problem = TRUE;
				$email = FALSE;
				$invalid_email = TRUE;
			}

			$short_name = trim($_POST['short_name']);
			$short_name = stripAccents($short_name);
			$short_name = cleanText($short_name);
			$short_name = utf8_it($short_name);
			if (strlen($short_name) < 1) {
				$problem = TRUE;
				$short_name = FALSE;
			}

			$banner_text = trim($_POST['banner_text']);
			$banner_text = stripAccents($banner_text);
			$banner_text = cleanText($banner_text);
			$banner_text = utf8_it($banner_text);
			if (strlen($banner_text) < 1) {
				$problem = TRUE;
				$banner_text = FALSE;
			}

			$contact_text = trim($_POST['contact_text']);
			$contact_text = stripAccents($contact_text);
			$contact_text = cleanText($contact_text);
			$contact_text = utf8_it($contact_text);
			if (strlen($contact_text) < 1) {
				$problem = TRUE;
				$contact_text = FALSE;
			}

		}

		if ($problem) {
			$response1 = "<em>There was a problem with the settings you entered (see below).</em>";
		}

		if (!$problem) {

			if ((!file_exists("../../.htaccess")) && ($apache == TRUE)) {
				$htaccess = "../../.htaccess";
				@touch($htaccess);
				if (strstr($self, 'laplume')) {
					$htaccess_text = "
AddDefaultCharset UTF-8

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/laplume/cms/inc/menu\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/laplume/cms/inc/inmenu\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/laplume/cms/admin/list\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/laplume/cms/([A-Za-z0-9_-]+)\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/laplume/cms/comments/([A-Za-z0-9_-]+)\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/laplume/img/$
RewriteRule .* - [F]

RewriteCond %{REQUEST_URI} ^/laplume/cms/$
RewriteRule ^cms/$ /laplume/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /laplume/cms/([A-Za-z0-9_-]+)\.php\ HTTP/
RewriteRule ^cms/([A-Za-z0-9_-]+)\.php$ /laplume/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/laplume/$
RewriteRule ^$ /laplume/cms/index.php [L]

RewriteCond %{REQUEST_URI} !^/laplume/index$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /laplume/([A-Za-z0-9_-]+)\ HTTP/
RewriteRule ^([A-Za-z0-9_-]+)$ /laplume/cms/$1.php [L]
";
				} else {
					$htaccess_text = "
AddDefaultCharset UTF-8

Options +FollowSymLinks
RewriteEngine on

ErrorDocument 404 /error404.php

RewriteCond %{REQUEST_URI} ^/cms/inc/menu\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/inc/inmenu\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/admin/list\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/([A-Za-z0-9_-]+)\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/comments/([A-Za-z0-9_-]+)\.txt$ [OR]
RewriteCond %{REQUEST_URI} ^/img/$
RewriteRule .* - [F]

RewriteCond %{REQUEST_URI} ^/cms/$
RewriteRule ^cms/$ / [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cms/([A-Za-z0-9_-]+)\.php\ HTTP/
RewriteRule ^cms/([A-Za-z0-9_-]+)\.php$ /$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /cms/index.php [L]

RewriteCond %{REQUEST_URI} !^/index$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9_-]+)\ HTTP/
RewriteRule ^([A-Za-z0-9_-]+)$ /cms/$1.php [L]
";
				}
				$fp = fopen($htaccess, "wb") or die("Couldn't open $htaccess. Check your file permissions.");
				fwrite($fp, $htaccess_text);
				fclose($fp);
				$cms_dir = NULL;
			}

			if (file_exists("../inc/settings.php")) {
				$update = TRUE;
				$delete = "../inc/settings.php";
				unlink($delete);
			}

			$settings = "../inc/settings.php";
			@touch($settings);
			$settings_text = "<?php

define('LOCATION', 'http://$site_location');
define('HOME_LINK', '$home_link');
define('NAME', '$name');
define('COMMENTS', $comment);
define('EMAIL', '$email');
define('SHORT_NAME', '$short_name');
define('BANNER_TEXT', '$banner_text');
define('CONTACT_TEXT', '$contact_text');
define('VERSION', '6.0');

?>";
			$fp1 = fopen($settings, "wb") or die("Couldn't open $settings. Check your file permissions.");
			fwrite($fp1, $settings_text);
			fclose($fp1);

			if (!file_exists("../index.txt")) {
				$index = "../index.txt";
				@touch($index);
				$index_text = 'bannner text
Main <i>Heading</i>

[ <a href="http://www.mini-print.com/CMS-help">help page</a> ]

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Leave a blank line to create new paragraphs.';
				$fp2 = fopen($index, "wb") or die("Couldn't open $index. Check your file permissions.");
				fwrite($fp2, $index_text);
				fclose($fp2);
			}

			if (!file_exists("../inc/inmenu.txt")) {
				$inmenu = "../inc/inmenu.txt";
				@touch($inmenu);
				$fp3 = fopen($inmenu, "wb") or die("Couldn't open $inmenu. Check your file permissions.");
				fclose($fp3);
			}

			if ($update == TRUE) {
				$action = "updated";
			} else {
				$action = "entered";
			}

			$setup = TRUE;
			$response1 = "<em>Settings $action successfully.</em>";
		}
	} else {
		$response1 = "<em>No action requested. Example settings shown.</em>";
	}

	if (isset($_POST['submit2'])) {
		$response1 = "<em>Example settings shown. To create or change the settings, edit as required then <b>Submit Setup</b>.</em>";
	}

?>

<p><?php if ($setup == TRUE) { ?>[ <a href="<?php echo 'http://' . $site_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> ] [ <a href="comments.php" title="Create/edit/delete comments">Create/edit/delete comments</a> ] <?php } ?> [ Setup ] [ <a href="?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 (file_exists('checkup.php')) {
	include ('checkup.php');
}

?>

<h3>Settings for la.plume Micro CMS</h3>

	<div id="response">

<?php echo $response1 . ' ' . $response2; ?>

	</div>

<h3>Enter as required</h3>

<p>Use only alphanumeric characters in all fields<?php if ($contact_page == TRUE) { ?> except email address<?php } ?>.</p>

<form action="" method="post" accept-charset="UTF-8">

	<div id="boxes2">

<label>For menu top item text<?php if (isset($_POST['submit1']) && (!$home_link) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="home_link" size="60" style="font-weight: bold; color: #c63;" value="<?php

	if (isset($_POST['submit1'])) {
		echo stripslashes($home_link);
	} else {
		echo "Home Page";
	}

?>
" maxlength="60"><br>

<label>For name in footer<?php if (isset($_POST['submit1']) && (!$name) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="name" size="60" style="font-weight: bold; color: #c63;" value="<?php

	if (isset($_POST['submit1'])) {
		echo stripslashes($name);
	} else {
		echo "Joe Bloggs";
	}

?>
" maxlength="60"><br>

<label>Enable visitor comments (YES or NO)<?php if (isset($_POST['submit1']) && (!$comments) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="comments" size="60" style="font-weight: bold; color: #c63;" value="<?php

	if (isset($_POST['submit1'])) {
		echo $comments;
	} else {
		echo "YES";
	}

?>
" maxlength="5"><br>

<?php if ($contact_page == TRUE) { ?>

<label>For contact page (your email address)<?php if (isset($_POST['submit1']) && (!$email) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="email" size="60" style="font-weight: bold; color: #c63;" value="<?php

		if (isset($_POST['submit1'])) {
			if ($invalid_email) {
				echo trim($_POST['email']);
			} else {
				echo $email;
			}
		} else {
			echo "mail@mydomain.com";
		}

?>
" maxlength="60"><br>

<label>For contact page (name beneath form)<?php if (isset($_POST['submit1']) && (!$short_name) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="short_name" size="60" style="font-weight: bold; color: #c63;" value="<?php

		if (isset($_POST['submit1'])) {
			echo stripslashes($short_name);
		} else {
			echo "Joe";
		}

?>
" maxlength="60" /><br>

<label>For contact page (banner text)<?php if (isset($_POST['submit1']) && (!$banner_text) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="banner_text" size="60" style="font-weight: bold; color: #c63;" value="<?php

		if (isset($_POST['submit1'])) {
			echo stripslashes($banner_text);
		} else {
			echo "contact page";
		}

?>
" maxlength="100"><br>

<label>For contact page (message above form)<?php if (isset($_POST['submit1']) && (!$contact_text) && ($problem == TRUE)) { echo ' [ <strong>incorrect value</strong> ]'; } ?>:</label><br>
<input type="text" name="contact_text" size="60" style="font-weight: bold; color: #c63;" value="<?php

		if (isset($_POST['submit1'])) {
			echo stripslashes($contact_text);
		} else {
			echo "To get in touch, feel free to use the following contact form (all fields required). The form will send me an email. Your privacy will be respected.";
		}

?>
" maxlength="255"><br>

<?php } ?>

	</div>
	<div id="buttons2">

<input type="submit" name="submit1" value="Submit Setup"><br>
<input type="submit" name="submit2" value="Reset Form"><br>

<?php

	if ($apache == TRUE) {
		echo '<p class="notice">Apache Web Server detected. The system should run.</p>';
	} else if ($apache == 'notdetected') {
		echo '<p class="notice"><em>Note:</em> Apache Web Server <strong>not detected</strong>. The system will run in the subdirectory <strong>/cms/</strong>.</p>';
	}

?>
<?php echo '<p class="break_word">Site location = <strong>http://' . $site_location . '</strong></p>'; ?>
<p>'Reset Form' restores the examples (does not submit values).</p>
<p>Characters with international accents are replaced with non-accented characters to ensure the settings script is valid. If you really want to use international characters in settings, enter them manually in <strong>/cms/inc/settings-sample.php</strong>, save the file as <strong>settings.php</strong> and upload it via FTP.</p>
<p>See also <a href="http://www.mini-print.com/-laplume-setup" target="_blank">about setup&nbsp;&raquo;</a></p>
<p><strong>Note:</strong> The settings and .htaccess files can also be set up manually. See /cms/inc/settings-sample.php | /htaccess.txt | /htaccess-laplume.txt.</p>

	</div>

</form>

<p id="version">la.plume Micro CMS 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>