survey.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Survey endpoint for SyncTrayzor
  4. */
  5. set_error_handler('error_handler');
  6. date_default_timezone_set('UCT');
  7. function error_handler($severity, $message, $filename, $lineno)
  8. {
  9. throw new ErrorException($message, 0, $severity, $filename, $lineno);
  10. }
  11. define('DATABASE', 'survey.sqlite');
  12. try
  13. {
  14. $exists = file_exists(DATABASE);
  15. $db = new PDO('sqlite:'.DATABASE);
  16. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $db->exec('PRAGMA foreign_keys = ON;');
  18. if (!$exists)
  19. {
  20. $db->exec("CREATE TABLE IF NOT EXISTS responses (
  21. id INTEGER PRIMARY KEY,
  22. date TEXT NOT NULL,
  23. version TEXT NOT NULL,
  24. ip TEXT NOT NULL,
  25. comment TEXT
  26. );");
  27. $db->exec("CREATE TABLE IF NOT EXISTS checklist (
  28. id INTEGER PRIMARY KEY,
  29. response_id INTEGER NOT NULL REFERENCES responses(id),
  30. key TEXT NOT NULL
  31. );");
  32. }
  33. $data = json_decode(file_get_contents('php://input'), true);
  34. $stmt = $db->prepare("INSERT INTO responses(date, ip, version, comment) VALUES (CURRENT_TIMESTAMP, :ip, :version, :comment);");
  35. $stmt->execute(array(
  36. 'ip' => $_SERVER['REMOTE_ADDR'],
  37. 'version' => $data['version'],
  38. 'comment' => $data['comment']));
  39. $responseId = $db->lastInsertId();
  40. $stmt = $db->prepare("INSERT INTO CHECKLIST (response_id, key) VALUES (:response_id, :key);");
  41. foreach ($data['checklist'] as $key => $value)
  42. {
  43. if ($value)
  44. {
  45. $stmt->execute(array('response_id' => $responseId, 'key' => $key));
  46. }
  47. }
  48. }
  49. catch (Exception $e)
  50. {
  51. $loggable_error = $e->getMessage() . "\n" . $e->getTraceAsString();
  52. file_put_contents("survey_errors.txt", $loggable_error, FILE_APPEND);
  53. }