<?php
/*
  Return file name for json output file
*/
require "../Pages/UserAdmin/super_secret_stuff.php";
require "CommonFunc.php";
if(!empty($_GET["auth"]) && !empty($_GET["pid"]) && !empty($_GET["mode"])){
  $con = mysqli_connect($website,$mysqli_username,$pw,$dbase);

  // Input error checks
  $auth = $_GET["auth"];
  $user_id = userid($con,$auth);
  if($user_id <= 0)goto errormsg1;

  $projid = $_GET["pid"];
  $pid = projid($con,$projid,$user_id);
  if($pid <= 0)goto errormsg2;
  
  // Get project name (to add to output file)
  $res = mysqli_query($con,"SELECT name FROM calibro_projects WHERE calproj_id = {$pid}");
  $row = mysqli_fetch_array($res);
  $mname = str_replace(' ', '_', $row[0]);
  $now = date("ymdhis");
  $jsonfile = "calibro_report_{$mname}_{$now}.json";
  $pdffile  = "calibro_report_{$mname}_{$now}.pdf";

  $mode = $_GET["mode"];
  if($mode != "pdf" && $mode != "json")goto errormsg3;
  if($mode == "pdf")$namestr = basename($pdffile);
  if($mode == "json")$namestr = basename($jsonfile);
  ob_clean(); // clean output buffer
  echo $namestr;
  mysqli_close($con);

}else{
  echo ("ERROR in RunPAM.php: authentication code or project id or PAM or mode not provided!");
}
exit;

errormsg1: echo ("ERROR in AddModel.php: bad authentication code"); goto theend;
errormsg2: echo ("ERROR in AddModel.php: bad project id"); goto theend;
errormsg3: echo ("ERROR in AddModel.php: bad mode"); goto theend;
theend:
mysqli_close($con);
exit;
?>
