<?php
require "../Pages/UserAdmin/super_secret_stuff.php";
require "CommonFunc.php";
if(!empty($_GET["auth"])){
  $auth = $_GET["auth"];
  $con = mysqli_connect($website,$mysqli_username,$pw,$dbase);
  $user_id = userid($con,$auth);
  if($user_id <= 0){
    echo ("ERROR: Bad authentication code provided!");
    mysqli_close($con);
    exit;
  }

  $response = array();
  $posts = array();
  $result = mysqli_query($con,"SELECT calproj_id,name FROM calibro_projects WHERE shared_between LIKE '%,{$user_id},%'");
  while($row = mysqli_fetch_array($result)){
    $id = $row["calproj_id"];
    $mname = $row["name"];
    $posts[] = array('id' => $id, 'mname' => $mname,);
  }
  $response['posts'] = $posts;

  // echo json data and exit
  //echo json_encode($response);exit;

  // Write json file on server and download
  $tmpfile = "{$user_id}.json";
  $handle = fopen($tmpfile,'w');
  ob_clean();
  fwrite($handle,json_encode($response));
  fclose($handle);
  header('Content-Type: application/octet-stream');
  header("Content-Transfer-Encoding: Binary");
  header("Content-disposition: attachment; filename=\"" . basename($tmpfile) . "\"");
  readfile($tmpfile);
  unlink($tmpfile);
  mysqli_close($con);
}else{
  echo ("ERROR: GetID.php no authentication code provided!");
}
exit;
?>
