<!DOCTYPE html>
<html>

<body>
    <head>
        <title>Darknet Lantern</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="icon" href="/img/logo-favicon.png">
    </head>
    <center >



<?php 
require("header.php");
	$min_length = 1;
	$max_length = 32;
	$instance= $_SERVER['SERVER_NAME'];
	$participantpath="participants/" . $instance . '/';
	$verifiedcsvpath="participants/" . $instance . '/verified.csv';
	$unverifiedcsvpath="participants/" . $instance . '/unverified.csv';

	// check if the verified and unverified csv files have links in them or not:
    $lines_uv = count(file($unverifiedcsvpath)); 
    $lines_v = count(file($verifiedcsvpath)); 
    //echo "<p>CSV LINES: " . $lines_uv . " " . $lines_v . "</p>"; 

?>

<form action="index.php" method="GET">
    <input type="text" name="query" />
    <input type="submit" value="Search" />	</br>
	<p>Sensitive Search: <input type="checkbox" name="sensitive" value="1"></p></br>
</form>


<?php
	$sensitive = $_GET['sensitive'];
	if ($sensitive == 1){
		//echo "<p>Sensitive ON " . $sensitive . "</p>";
		$sensitive=1;
	}else{
		$sensitive=0;
		//echo "<p>Sensitive OFF " . $sensitive . "</p>";
	}
	$query = $_GET['query']; 
	// gets value sent over search form
	

	if(strlen($query) >= $min_length and strlen($query) <= $max_length){ // if query length is more or equal minimum length then
		
		$query = htmlspecialchars($query); 
		// removes the risky characters
		// there needs to be regex checking, only allow [a-zA-Z0-9.:/] 
		$query=preg_replace("/[^a-zA-Z0-9:\/.\ -]/", "", $query);



		
		echo "<p>Search results for " . $query . " :</p>";	
		echo nl2br("\n");
		echo nl2br("\n");
		//echo $verifiedcsvpath;
		//echo nl2br("\n");
		//echo $unverifiedcsvpath;
		if ($lines_v >= 2){
			echo '<div class="verified"><h3>Verified Links</h3></br></div>';
			echo nl2br("\n");
			DisplayCSVNEW($verifiedcsvpath,$query,$sensitive);
		}else{
			echo "<p>Your verified.csv file is empty, check the documentation to know how to add links into it.</p>";
		}
		if ($lines_uv >= 2){
			echo '</br></br></br><div class="unverified"><h3>Unverified Links</h3></br></div>';
			echo nl2br("\n");
			DisplayCSVNEW($unverifiedcsvpath,$query,$sensitive);
		}else{
			echo "<p>Your unverified.csv file is empty, please check the documentation to know how to add links into it.</p>";
		}
		// display the results of verified.csv
		// only display the results of verified.csv that matches with the search term
		// display the results of unverified.csv 
		// only display the results of unverified.csv that matches with the search term
		
	}
	else{ // if query length is less than minimum or more than maximum
		echo '</br></br><img src="img/logo-large.png"><p>Search query must be between '.$min_length . " and ".$max_length . " characters.</p>";
		// TODO list the available categories
	}
?>
<?php 
require("footer.php");
?>



