test_file.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var string $name
  5. */
  6. $test = Test::get($id);
  7. if(!$test) {
  8. throw new RuntimeException(__("Test not found."));
  9. }
  10. $user = Core::$user;
  11. $is_pdf = ($name === "pdf");
  12. if($is_pdf) {
  13. $project = $test->getProject();
  14. $customers = $user->getCustomers();
  15. $customer_ids = array_column($customers, "id");
  16. if(!in_array($project->customer_id, $customer_ids)) {
  17. throw new RuntimeException(__("You do not have permission to access this resource."));
  18. }
  19. }
  20. if(!$is_pdf && !$user->canVerifyTests() && $user->is($test->tester_id)) {
  21. throw new RuntimeException(__("You do not have permission to access this resource."));
  22. }
  23. $directory = $test->getDirectory();
  24. $path_jpg = $directory . "/" . $name . ".jpg";
  25. $path_pdf = $directory . "/" . $name . ".pdf";
  26. if(file_exists($path_jpg)) {
  27. header("Content-Type: image/jpeg");
  28. readfile($path_jpg);
  29. }
  30. else if(file_exists($path_pdf)) {
  31. header("Content-Type: application/pdf");
  32. readfile($path_pdf);
  33. }
  34. else {
  35. header("Content-Type: image/png");
  36. readfile("public/img/blank.png");
  37. }