<?php

/* Customized for LaituriSyli, do not use elsewhere
*
* OPTIONAL
*
* Create header.htm to be displayed at the top (XHTML 1.0 Strict).
*
* Put a background image in folder "background". It will be tiled.
*
* Put large versions of the images (with exactly the same filename)
* in folder "large". The current image will then be clickable to see the large version.
*
* Create commentfiles to the images in folder "comments".
* If such a file exists, the comment will be displayed above the current image.
* One file per comment for each image. The filename must be exactly the same as the image,
* except for the extension, which can be .php .htm or .txt XHTML 1.0 Strict code is ok.
*
* Edit colors immediately below.
*
*******************************************************************************/

if ( $_SERVER['SERVER_NAME'] == 'laiturisyli.fibresin.fi' ) {
	header ( 'HTTP/1.1 301 Moved Permanently' );
	header ( 'Location: https://fibresin.fi/laiturisyli/' );
	exit;
}

ob_start(); // Output buffering.
error_reporting ( E_ALL	);
//if ( $_SERVER["SERVER_NAME"] != "localhost" )	error_reporting ( 0		);
//else											error_reporting ( E_ALL	);

define ("BACKGROUND_COLOR", "rgb(48,48,48)");
define ("TEXT_COLOR", "rgb(250,250,250)");
define ("LINK_COLOR", "rgb(255,128,0)");
define ("FONT_NAME", "Verdana");
define ("FONT_SIZE", "9pt");

function fnBackgroundImage ()
{
if (! file_exists ("background")) return "";
if (! is_dir ("background")) return "";
if (! $dp = @opendir ("background")) return "";
while ($file = readdir ($dp)) {
	if (is_dir ($file)) continue;
	if (@GetImageSize ("background/$file") === FALSE) continue;
	else {
		@closedir ($dp);
		return "background: url('background/$file');\n";
	}
}
@closedir ($dp);
return "";
}



function fnImageArray ()
{
// GENERATE ARRAY OF IMAGES IN CURRENT DIR.
// Returns an array of arrays.

if (isset ($_GET["debug"])) $debug = TRUE;
else $debug = FALSE;
//$debug = TRUE;
$aaImages = array ();
if (!$dp = @opendir(".")) die ("Darn! Could not open directory for reading.");
while ($file = readdir ($dp)) {
	if ($debug) echo $file;
	if (is_dir ($file)) { if ($debug) { echo " (dir)<br />"; } continue; }
	if (!$aImg = GetImageSize ($file)) { if ($debug) { echo " (not image)<br />"; } continue; }
	list ($x, $y, $type) = $aImg;
	$aaImages[] = array ($file, $x, $y);
	if ($debug) echo " (". $aImg["mime"] .")<br />";
}
closedir ($dp);
sort ($aaImages);
return $aaImages;
}


function fnCreateCommentFiles (&$aaImages)
{
echo "<p><strong>Creating comment files.</strong><br />";
if (is_dir ("comments")) {
	echo "Directory \"comments\" exists.<br />";
} else {
	echo "<p>Creating directory \"comments\".<br />";
	if (!mkdir ("comments")) die ("Could not create directory \"comments\". Please create it manually.</p>");
}
foreach ($aaImages as $aImg) {
	$strCommentFile = "comments/". substr ($aImg[0], 0, strrpos ($aImg[0], ".")) .".txt";
	if (file_exists ($strCommentFile)) {
		echo "File \"$strCommentFile\" exists.<br />";
	} else {
		if (! $fp = fopen ($strCommentFile, "w")) die ("Could not create file.</p>");
		fwrite ($fp, "");
		fclose ($fp);
		echo "File \"$strCommentFile\" created.<br />";
	}
}
echo "</p>";
return;
}



function fnListOfImages (&$aaImages, $intImg)
{
// PRINT OUT LINK LIST OF IMAGES.

echo "<div id='images'>";
foreach ($aaImages as $id => $aImg) {
	$id++;
	if ($intImg + 1 <> $id) echo " <a href='?img=$id' name='i$id' id='i$id' title='". $aImg[0] ."'>$id</a> ";
	else echo "<span>&nbsp;$id&nbsp;</span> ";
}
echo "</div>";
return;
}



function fnFlipButtons ($img, $last)
{
echo "<div id='flipButtons'>";
if ($img > 0) echo " <a href='?img=". ($img) ."'> &laquo; </a> ";
else echo " <span style='color:rgb(128,128,128)'> &laquo; </span> ";
if ($img < $last - 1) echo " <a href='?img=". ($img + 2) ."'> &raquo; </a> ";
else echo " <span style='color:rgb(128,128,128)'> &raquo; </span> ";
echo "</div>";
return;
}


function fnComment ($img, $width)
{
$strCommentFile = "comments/". substr ($img, 0, strrpos ($img, "."));
switch (TRUE) {
	case file_exists ("$strCommentFile.php"): $strCommentFile = "$strCommentFile.php"; break;
	case file_exists ("$strCommentFile.htm"): $strCommentFile = "$strCommentFile.htm"; break;
	case file_exists ("$strCommentFile.txt"): $strCommentFile = "$strCommentFile.txt"; break;
	default: echo "<div>"; return;
}
if (! filesize ($strCommentFile)) {
	echo "<div>";
	return;
}
if ($width < 550) {
	echo "<div style='width:748px; margin:auto;'>
	<div style='width:". (710 - $width) ."px; float:left; margin-top:10px; text-align:left;'>";
} else {
	echo "<div><div style='width:". ($width + 14) ."px; margin:auto; margin-top:3px; text-align:justify;'>";
}
readfile ($strCommentFile);
echo "</div>";
return;
}



function fnViewImage (&$aaImages, $intImg)
{
if (! isset ($aaImages[$intImg])) die ("<p>Sorry, no such image. Index $intImg is out of bounds.</p>");
list ($img, $x, $y) = $aaImages[$intImg];
if (! file_exists ($img)) die ("<p>Sorry, image \"$img]\" not found.</p>");
if (file_exists ("comments")) {
	if (is_dir ("comments")) fnComment ($img, $x);
}
if (file_exists ("large/$img")) {
	echo "<div><a href='large/$img' title='Click image to see large image'
	><img id='img' src='$img' width='$x' height='$y' alt='$img' /></a></div></div>";
} else {
	echo "<div><img id='img' src='$img' width='$x' height='$y' alt='$img' title='$img' /></div></div>";
}
return;
}



ob_start (); // Enable output buffering.
if ($_SERVER["SERVER_NAME"] == "localhost") error_reporting (E_ALL); // SHOW ALL ERRORS.
else error_reporting (0);	// SHOW NO ERRORS.
echo "<?xml version='1.0' encoding='iso-8859-1'". chr (63) .">
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>";

// Display source, if requested.
if (@strtoupper ($_GET["display"]) == "SOURCE") {
	die ("<html><head><title>Source code for Simple Image Flipper</title></head><body><pre>".
		htmlentities (file_get_contents (substr ($_SERVER["PHP_SELF"], strrpos ($_SERVER["PHP_SELF"], "/") + 1))) .
		"</pre></body></html>");
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<meta http-equiv='Content-Type'			content='text/html; charset=ISO-8859-1' />
<meta http-equiv='Content-Style-Type'	content='text/css' />
<meta http-equiv='Content-Script-Type'	content='text/javascript' />
<meta name='Author'						content='Björn Törnroth' lang='sv' />
<meta name='verify-v1'					content='epRcmG+zw9chqRmJ4Gof/l+xydLf5Np4IzitdqGrxFA=' />
<title>LaituriSyli - Kiinnity laituriin nopeasti, helposti ja turvallisesti - myös yksin</title>
<style type="text/css">
body {
	margin: 0;
	background-color: <?php echo BACKGROUND_COLOR ?>;
	<?php echo fnBackgroundImage (); ?>
	color: <?php echo TEXT_COLOR ?>;
	font-family: <?php echo FONT_NAME ?>, Verdana, Arial, Helvetica, sans-serif;
	text-align: center;
}
#fibresin {
	background-color: Black;
	padding: 1em 0;
	border-bottom: 1px solid Orange;
	font-weight: bold;
	margin-bottom: 1em;
}
h1, h2 ,h3 {
	font-weight: bold;
	margin: 0.8em 0px 0.5em 0px;
}
h1 {
	font-size: 155%;
	margin-top: 0px;
	letter-spacing: .1em;
}
h2 {
	font-size: 133%;
	letter-spacing: 6px;
}
h3 {
	font-size: 111%;
	letter-spacing: 3px;
}
#info span {
	display: inline-block;
}
#images {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	margin: 1em 0;
	font-weight: bold;
}
#images span {
	color: <?php echo BACKGROUND_COLOR ?>;
	background-color: <?php echo LINK_COLOR ?>;
}
#flipButtons {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14pt;
	font-weight: bold;
	margin-top: -10px;
}
a {
	color: <?php echo LINK_COLOR ?>;
	cursor: pointer;
	text-decoration: none;
}
a:hover {
	color: <?php echo BACKGROUND_COLOR ?>;
	background-color: <?php echo LINK_COLOR ?>;
}
#img {
	font-size: 8pt;
	margin: 10px 0;
	background-color: rgb(0,0,0);
	/*padding: 6px;*/
	/*border: 1px solid rgb(160,160,160);*/
	max-width: 100%;
	height: auto;
}
#footer {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	margin-top: 50px;
	border-top: 1px solid rgb(128,128,128);
	font-size: 8pt;
	text-align: right;
	line-height: 2.5em;
}
</style>
</head>
<body>
<?php

// SELECTED IMAGE ID TO DISPLAY.
if (isset ($_GET["img"])) $intImg = (int) $_GET["img"] - 1;
else $intImg = 0;

$aaImages = fnImageArray ();

if (isset ($_GET["createcommentfiles"])) fnCreateCommentFiles ($aaImages);

?>

<div id='fibresin'>
	<a href='https://www.fibresin.fi/'>www.fibresin.fi <big>&larr;</big> takaisin &ndash; tillbaka &ndash; back</a>
</div>
<h1><span style="font-size: smaller;">Bryggfamnen</span>
	<img style="vertical-align: middle; margin-left: 30px; margin-right: 30px; border: 8px solid white;" src="img/Laiturisyli_logo.png" alt="Laituri Syli" width="177" height="72" />
	<span style="font-size: smaller;">Mooring Antlers</span>
</h1>
<div id='info'>
	<span>Tietoja <a href="LaituriSyli_2012_FI.pdf">PDF esitteess&auml;</a></span> &ndash; 
	<span>Information i&nbsp;<a href="LaituriSyli_2012_SV.pdf">PDF broschyren</a></span> &ndash; 
	<span>Information in <a href="LaituriSyli_2012_EN.pdf">PDF flyer</a></span>
</div>

<?php

fnListOfImages ($aaImages, $intImg);

fnFlipButtons ($intImg, count ($aaImages));
	
fnViewImage ($aaImages, $intImg);
	
?>
<p id='footer'>
SimpleImageFlipper is utterly freeware and available at
<a href='http://nettitonttu.fi/pub/'>www.nettitonttu.fi/pub</a>
[<a href='<?php echo $_SERVER["PHP_SELF"] ?>?display=source' title='View source'>View source code</a>]
<br />
<a href='http://validator.w3.org/check?uri=referer'
 ><img style='border:0;width:88px;height:31px' src='http://www.w3.org/Icons/valid-xhtml10' alt='XHTML' title='Valid XHTML 1.0' /></a>
<a href='http://jigsaw.w3.org/css-validator/check/referer'
 ><img style='border:0;width:88px;height:31px' src='http://jigsaw.w3.org/css-validator/images/vcss' alt='CSS' title='Valid CSS' /></a>
<a href='http://www.nettitonttu.fi/'
 ><img style='border:0;width:88px;height:31px' src='http://nettitonttu.fi/img/webgnome.png' alt='WebGnome' title='Proud Craftsmanship by WebGnome' /></a>
</p>
</body>
</html>