<?

/////////////////////////////////////////////////////////////////
// Yuba RTC Browser
/////////////////////////////////////////////////////////////////

$icon_size = 64;
$pad = 28;

?>

<html>
<head>
<style>

div.container { display: flex; flex-flow: row wrap; justify-content: center; }
div.item { width: <?=$icon_size+$pad;?>px; height: <?=$icon_size+$pad;?>px; padding: <?=$pad;?>px; word-wrap: break-word; }
div.item { font-family: Helvetica; font-size: 11px; }
div.flexfill { width: <?=$icon_size+($pad*3);?>px; height: 1px; }
img.thumb { padding: 6px; border: 1px solid gainsboro; }
img { margin-bottom: 8px; }

</style>
</head>

<body>

<?

// Functions

function breadcrumbs() { }

function shortlabel($filename, $max = 40) {
	$suffix = "(...).".pathinfo($filename)['extension'];
	if (strlen($filename) > $max) {
		$return = substr($filename, 0, ($max-strlen($suffix))).$suffix;
		} else {
		$return = $filename;
		}
	return $return;
	}

function findicon($filename) {
	$ext = pathinfo($filename)['extension'];
	if (!$ext) {
		$good = "icons/directory.png";
		} else {
		$good = "icons/null.png";
		}	
	foreach (glob("icons/*.png") as $file) {
		if (pathinfo($file)['filename'] == $ext) {
			$good = $file;
			}
		}
	return $good;
	}

/////////////////////////////////////////////////////////////////

$db_file = $_GET['db'];
$pid = $_GET['pid'];

if ($db_file) {
	
	// View
	
	if (!is_readable($db_file)) { echo "can't read db file"; die; }
	
	echo "<a href='?db='>db list</a>";

	echo "<hr>";
	
	$dbo = new PDO("sqlite:".$db_file);
	$dbo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
	
	if (!$pid) {
		$array = $dbo->query("SELECT children FROM family WHERE (rowid=2)")->fetch()['children'];
		} else {
		$array = $dbo->query("SELECT children FROM family WHERE (pid='".$pid."')")->fetch()['children'];
		}
	
	$children = unserialize($array);
	
	//echo "<pre>"; print_r($array); echo "</pre>";
	
	
	echo "<hr>";
	
	echo "\n<div class='container'>";
	dsf
	print_r($children);
	
	if (count($children) < 1) { echo "dir is empty."; die; }
	
	foreach ($children as $item) {
		
		echo "<div class='item'>";
		
		$row_a = $dbo->query("SELECT * FROM files WHERE (pid='".$item."')")->fetchAll()[0];
		
		//$row_b = $dbo->query("SELECT * FROM mdls WHERE (pid='".$item."')")->fetchAll()[0];
		//$row_c = $dbo->query("SELECT * FROM milk WHERE (pid='".$item."')")->fetchAll()[0];
		
		if ($row_a['thumb_filename']) {
			$aspect = $row_a['thumb_width']/$row_a['thumb_height'];
			if ($aspect > 1) {
				$width = $icon_size;
				$height = $icon_size/$aspect;
				} else {
				$width = $icon_size*$aspect;
				$height = $icon_size;
				}
			$realfile = dirname($db_file).$row_a['thumb_filename'];
			$icon = "<img class='thumb' src='".$realfile."' width='".$width."' height='".$height."'>";
			} else {
			$icon = "<img class='icon' src='".findicon($row_a['Filename'])."' width='".$icon_size."' height='".$icon_size."'>";
			}
		
		$name = htmlentities(shortlabel($row_a['Filename']));
		
		if ($row_a['Type'] == "dir") {
			echo "\n<a href='?db=".$db_file."&pid=".$row_a['pid']."'>".$icon."</a><br>".$name;
			} else {
			echo $icon."<br>".$name;
			}
		
		echo "</div>";
		echo "<br>";
		
		}
		
	echo str_repeat("<div class='flexfill'></div>", 100);
	echo "\n</div>";
	
	} else {

/////////////////////////////////////////////////////////////////

	// DB List
	
	$bundles = glob("skim/*.bundle");
	foreach ($bundles as $bundle) {
		echo "<h2>".pathinfo($bundle)['filename']."</h2>";
		$dbs = glob($bundle."/*.sqlite3");
		foreach ($dbs as $db_file) {
			if (!strpos($db_file,"pool")) {
				echo "<a href='?db=".$db_file."'>".pathinfo($db_file)['filename']."</a>&nbsp;";
				$dbo = new PDO("sqlite:".$db_file);
				echo $dbo->query("SELECT type FROM _skim WHERE (rowid=1)")->fetch()['type'].", ";
				echo $dbo->query("SELECT passed_total FROM _skim WHERE (rowid=1)")->fetch()['passed_total']." files, ";
				echo $dbo->query("SELECT status FROM _skim WHERE (rowid=1)")->fetch()['status'];
				echo "<br>";
				}
			}
		}
	}

/////////////////////////////////////////////////////////////////

echo "<hr><br>".round($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"],2)." seconds";

?>

</body>
</html>