<?php
// Return user id given auth code
// Parameters
$sleeptime = 30; // seconds between each request to simulation server to take up simulation
                 // (by attempting to write to results table)
$maxiter = 240;  // number of attempts made to write new job to results table

function userid($con,$auth){
  $res = mysqli_query($con,"SELECT COUNT(user_id) FROM User WHERE auth_code = '{$auth}'");
  $row = mysqli_fetch_array($res);
  $i = 0; $i = $row[0];
  if($i <= 0){
    return -1;
  }else{
    $res = mysqli_query($con,"SELECT user_id FROM User WHERE auth_code = '{$auth}'");
    $row = mysqli_fetch_array($res);
    return $row[0];
  }
}

// Check if valid project id
// Return project id if given project id is valid else return -1
// Return -1 also if incorrect user id
function projid($con,$pid,$user_id){
  $res = mysqli_query($con,"SELECT COUNT(calproj_id) FROM calibro_projects WHERE calproj_id = '{$pid}' AND shared_between LIKE '%,{$user_id},%'");
echo ("SELECT COUNT(calproj_id) FROM calibro_projects WHERE calproj_id = '{$pid}' AND shared_between LIKE '%,{$user_id},%'");
    $row = mysqli_fetch_array($res);
  $i = 0; $i = $row[0];
  if($i <= 0){
    return -1;
  }else{
    return $pid;
  }
}

// Get result report
// If report not available then wait for it INDEFINITELY
function get_result($path,$mode){
  set_time_limit(0);
  if($mode = "json"){
    $tmpfile = $path . "calibro_report.json";
	$errfile = $path . "data.json";
  }elseif($mode = "pdf"){
    $tmpfile = $path . "calibro_report.pdf";
    $errfile = $path . "report.pdf";
  }
  lookforit:
    // echo("looking for ")
  if(file_exists($tmpfile)){
	ob_clean();
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"" . basename($tmpfile) . "\"");
    readfile($tmpfile);
  }elseif(file_exists($errfile)){
	ob_clean();
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"" . basename($errfile) . "\"");
    readfile($errfile);
  }else{
    sleep(30);
    goto lookforit;
  }
}





















?>
