<?php
  define("CHUNK_SIZE", 1024*1024); // Size (in bytes) of tiles chunk

  // Read a file and display its content chunk by chunk
  function readfile_chunked($filename, $retbytes = TRUE) {
    $buffer = "";
    $cnt =0;
    // $handle = fopen($filename, "rb");
    $handle = fopen($filename, "rb");
    if ($handle === false) {
      return false;
    }
    while (!feof($handle)) {
      $buffer = fread($handle, CHUNK_SIZE);
      echo $buffer;
      ob_flush();
      flush();
      if ($retbytes) {
        $cnt += strlen($buffer);
      }
    }
    $status = fclose($handle);
    if ($retbytes && $status) {
      return $cnt; // return num. bytes delivered like readfile() does.
    }
    return $status;
  }

  // Here goes your code for checking that the user is logged in
  // ...
  // ...
  
  $data = json_decode(file_get_contents("data.json"),true)[$_GET["sharingid"]];
  
  if(isset($_GET["chkpassword"])){
	 if($_GET["chkpassword"] == $data["password"]){
		echo true;
	 }else{
		echo false;
	 }
  }else{
	  $logged_in = false;
	  if($_GET["password"] == $data["password"]){
		$logged_in = true;
	  }
	  
	  if ($logged_in) {
		$filename = $data["path"];
		//$mimetype = "mime/type";
		$mimetype = mime_content_type($filename);
		header("Content-length:".filesize($filename));
		header("Content-Type: ".$mimetype );
		readfile_chunked($filename);
	  }
	  else {
		echo "Tabatha says you haven\"t paid.";
	  }
  }
?>