<html>
<head>
<?php
//This folder is used to store all the important information needed for multiple web system booting
$infoFolder = "msbinfo";
if (file_exists($infoFolder . "/") == false){
	die("SYSTEM FOLDER NOT FOUND.");
}
echo '<link rel="stylesheet" href="'.$infoFolder.'/tocas.css">';
?>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta charset="UTF-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
<link rel="stylesheet" href="<?php echo $infoFolder; ?>/tocas.css">
<script src="<?php echo $infoFolder; ?>/jquery.min.js"></script>
<title>IMSB Interface</title>
</head>
<body>
<?php
$save = fopen("$infoFolder/bootinfo.txt", "w") or die("Permission denied! PHP cannot write file to this directory.");
$txt = "last_act=" . time() . "\nrmt_addr=" . $_SERVER['REMOTE_ADDR'] . "\n";
fwrite($save, $txt);
fclose($save);
?>
<div class="ts centered big menu">
    <div class="item">IMUS Multiple System Booting Interface</div>
</div><br>
<div class="ts container">
	<div class="ts cards">
		<?php
			$templateNormal = '<a class="ts card" href="%REDIRECT%">
				<div class="image">
					<img src="%MODULE_PIC%">
					<div class="header">
						%MODULE_NAME%
						<div class="sub header">%MODULE_DESC%</div>
					</div>
				</div>
			</a>';
			
			$templateVideo = '<a class="ts card" href="%REDIRECT%">
					<div class="video">
						<div class="ts 1:1 active embed">
							<video src="%VIDEO_PATH%" autoplay muted loop></video>
						</div>
					<div class="header">
						%MODULE_NAME%
						<div class="sub header">%MODULE_DESC%</div>
					</div>
				</div>
			</a>';
			
			//Building non-system directories list
			$skippingDirectory = [];
			array_push($skippingDirectory,$infoFolder);
			$skipItems = file_get_contents($infoFolder."/non-system_list.txt") or die("Missing $infoFolder/non-system_list.txt");
			$skipItems = explode("\n",$skipItems);
			foreach ($skipItems as $dir){
				if (substr($dir,0,2) != "//" && $dir != ""){
					//Not commented items
					array_push($skippingDirectory,trim($dir));
				}
			}
			//Generating System List
			$systems = array_filter(glob('*'), 'is_dir');
			foreach ($systems as $system){
				if (in_array($system,$skippingDirectory) == false){
					if (file_exists($system . "/msb/card.mp4") == false){
						$box = str_replace("%REDIRECT%",$system . "/",$templateNormal);
						if (file_exists($system . "/msb/card.png") == true){
							//Module msb supporting profile pic found
							$box = str_replace("%MODULE_PIC%",$system . "/msb/card.png",$box);
						}else{
							//Module msb supporting profile pic not found, use default
							$box = str_replace("%MODULE_PIC%","$infoFolder/unknown_module.png",$box);
						}
					}else{
						//Video found, use video as profile pic
						$box = str_replace("%REDIRECT%",$system . "/",$templateVideo);
						$box = str_replace("%VIDEO_PATH%",$system . "/msb/card.mp4",$box);
					}
					if (file_exists($system . "/msb/") == true){
						//This system support multiple system booting
						if (file_exists($system . "/msb/bootConfig.txt")){
							//Config exists, use config's data as template
							$content = file_get_contents($system . "/msb/bootConfig.txt");
							$content = explode("\n",$content);
							$btarget = "index.php";
							$displayName = $system;
							$desc = "N/A";
							foreach ($content as $setting){
								if (strpos($setting,"bootTarget=") !== false){
									$btarget = $system . "/" . str_replace("bootTarget=","",$setting);
								}else if (strpos($setting,"displayName=") !== false){
									$displayName = str_replace("displayName=","",$setting);
								}else if (strpos($setting,"description=") !== false){
									$desc  = str_replace("description=","",$setting);
								}
							}
							$box = str_replace('href="'.$system.'/"','href="'.$btarget.'"',$box);
							$box = str_replace("%MODULE_NAME%",$displayName,$box);
							$box = str_replace("%MODULE_DESC%",$desc,$box);
							echo $box;
							
						}else{
							//Config not exists, use default values
							$box = str_replace("%MODULE_NAME%","$system",$box);
							$box = str_replace("%MODULE_DESC%","This module has no information readable by Multi System Booting Interface Utilities.",$box);
							echo $box;
						}
					}else{
						//This system do not support multiple system booting
						$box = str_replace("%REDIRECT%",$system . "/",$templateNormal);
						$box = str_replace("%MODULE_PIC%","$infoFolder/unknown_module.png",$box);
						$box = str_replace("%MODULE_NAME%","$system",$box);
						$box = str_replace("%MODULE_DESC%","This module has no information readable by Multi System Booting Interface Utilities.",$box);
						echo $box;
					}
				}
				
			}
			
		?>
		
	</div>
</div>
<div style=" position: fixed;
    z-index: 100; 
    bottom: 0; 
    left: 0;
    width: 100%;
	background-color:#828282;color:white;" align="right">
	CopyRight IMUS Laboratory 2017-2018
</div>
<script>
/*
var AOB = <?php echo file_exists("AOB/");?>;
var cancel = false;
if (AOB == true){
	setTimeout(function() {
		if (cancel == false){
			window.location.href = "AOB/index.php";
		}
	}, 1000);
}

$(document).keypress(function(e) {
  if(e.which == 13) {
    cancel = true;
	console.log("Auto-boot canceled");
  }
});
*/

</script>
</body>
</html>