<?php 
error_reporting(E_ALL);
// change
$map = '.';	// dir for images
$col = 4;		// collumns per page
$row = 5;	// rows per page
$img = array('jpg'); // possible extentions
$tab = array(); // empty
if(isset($_GET['actie'])){
	$_GET['actie'] = $_GET['actie'];
}
else{
	$_GET['actie'] = '';
}
if(isset($_GET['dir'])){
	$_GET['dir'] = $_GET['dir'];
}
else{
	$_GET['dir'] = '';
}
$dir = $_GET['dir'];
if(!isset($_GET['p'])){
	$p = 1;
}
else{
	$p = $_GET['p'];
}
function img_resize($a, $b = '', $c = ''){
	if(file_exists($a)){
		if($b == '' || !is_numeric($b)){
			$b = 100;
		}
		if($c == '' || !is_numeric($c)){
			$c = 100;
		}
		$f = getimagesize($a);
		$g = array();
		if($f[1] > $c || $f[0] > $b){
			if($f[0] > $c){
				$d = $f[0] - $b;
				$e = $d / $f[0];
				$g[0] = $b;
				$g[1] = $f[1] - ($f[1] * $e);
			}
			elseif($f[1] > $c){
				$d = $f[1] - $c;
				$e = $d / $f[1];
				$g[0] = $f[0] - ($f[0] * $e);
				$g[1] = $c;
			}
			else{
				$g[1] = $c;
				$g[0] = $b;
			}
		}
                else{
			$g[0] = $f[0];
			$g[2] = $f[1];
                }
		return 'src="'.$a.'" width="'.$g[0].'" height="'.$g[1].'"';
	}
	else{
		return false;
	}
}
function dir_array($a, $b){ 
	$g = array();
    if($c = opendir($a)){ 
        while(false !== ($d = readdir($c))){ 
            if($d != "." && $d != ".."){ 
                $e = explode('.', $d); 
                $f = $e[count($e)-1]; 
                if(is_dir($a."/".$d)){ 
                    $g = array_merge($g, dir_array($a."/".$d, $b)); 
                } 
                elseif(in_array(strtolower($f), $b)){ 
                    $g[] = $a."/".$d; 
                } 
            } 
        } 
        closedir($c);         
    } 
    return $g; 
} 
function map_array($a){
	$g = array();
    if($c = opendir($a)){ 
        while(false !== ($d = readdir($c))){ 
            if($d != "." && $d != ".."){ 
                if(is_dir($a."/".$d)){ 
                    $g[$d] = $a."/".$d; 
                } 
            } 
        } 
        closedir($c);         
    } 
    return $g; 
}
function sort_table($a, $b, $e){
	global $p;
	$f = $b*$e;
	$f = count($a)/$f;
	$f = ceil($f);
	if(isset($_GET['actie'])){
		$actie = '&actie='.$_GET['actie'];
	}
	else{
		$actie = '';
	}
	if($_GET['dir'] != ''){
		$actie .= '&dir='.$_GET['dir'];
	}
	if($b > count($a)){
		$b = count($a);
	}
	if($p < 1 || $p > $f){
		$p = 1;
	}
	$c = '<table border="1" width="'.(150*$b).'" style="border-collapse: collapse">';
	$d = 1;
	for($i = $b*$e*($p-1); $i < ($b*$e*$p); $i++){
		if($d == 1){
			$c .= '<tr align="center" valign="middle">';
		}
		$c .= '<td width="150" height="150">';
		if(isset($a[$i])){
			$c .= $a[$i];
		}
//		else{
//			$c .= '&nbsp;';
//		}
		$c .= '</td>';
		if($d == $b){
			$d = 1;
			$c .= '</tr>';
		}
		else{
			$d++;
		}
	}
	$c .= '<tr align="center" valign="middle"><td colspan="'.$b.'">';
	if($p > 1){
		$c .= '<a href="?p='.($p-1).$actie.'">< Vorige</a>';
	}
	else{
		$c .= '< Vorige';
	}
	$c .= ' | ';
	for($j = 1; $j <= $f; $j++){
		if($p != $j){
			$c .= '<a href="?p='.$j.$actie.'">'.$j.'</a>';
		}
		else{
			$c .= '<b>'.$j.'</b>';
		}
		$c .= ' | ';
	}
	if($p < $f){
		$c .= '<a href="?p='.($p+1).$actie.'">Volgende ></a>';
	}
	else{
		$c .= 'Volgende >';
	}
	if($_GET['actie'] == 2){
		$c .= '<br /><a href="?actie=1&dir='.$_GET['dir'].'">Overzicht</a>';
	}
	elseif($_GET['actie'] == 1){
		$c .= '<br /><a href="?dir=">Overzicht</a>';
	}
	$c .= '</td></tr>';
	$c .= '</table>';
	return $c;
}
?>
<html>
<head>
<title>Foto's Cm 2004 Schwarszee</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<?php
$i = 1;
foreach(dir_array($dir, $img) as $a){
	if(file_exists($a) == true){
		if($_GET['actie'] == 1){
			$tab[] = '<a href="?p='.$i.'&actie=2&dir='.$_GET['dir'].'"><img '.img_resize($a).' border="0" /></a>';
		}
		else{
			$tab[] = '<a href="'.$a.'"><img '.img_resize($a, 700, 700).' border="0" /></a>';
		}
		$i++;
	}
}
if($_GET['actie'] == '' || $_GET['dir'] == '' || !is_dir($_GET['dir'])){
	echo '<b>Fotoalbums:</b><br />';
	if(count(map_array($map)) != 0){
		foreach(map_array($map) as $a => $b){
			echo '- <a href="?actie=1&dir='.$b.'">'.$a.'</a><br />';
		}
	}
	else{
		echo '- <a href="?actie=1&dir='.$map.'">'.$map.'</a><br />';
	}
	echo '- <a href="?actie=1&dir='.$map.'">Allemaal</a><br />';
}
elseif($_GET['actie'] == 1){
	echo sort_table($tab, $col, $row);
}
elseif($_GET['actie'] == 2){
	echo sort_table($tab, 1, 1);
}
?> 
</div>
</body>
</html>