|
|
@@ -0,0 +1,51 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/**
|
|
|
+ * Survey endpoint for SyncTrayzor
|
|
|
+ */
|
|
|
+
|
|
|
+define('DATABASE', 'survey.sqlite');
|
|
|
+
|
|
|
+try
|
|
|
+{
|
|
|
+ $exists = file_exists(DATABASE);
|
|
|
+
|
|
|
+ $db = new PDO('sqlite:'.DATABASE);
|
|
|
+ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
+ $db->exec('PRAGMA foreign_keys = ON;');
|
|
|
+
|
|
|
+ if (!$exists)
|
|
|
+ {
|
|
|
+ $db->exec("CREATE TABLE IF NOT EXISTS responses (
|
|
|
+ id INTEGER PRIMARY KEY,
|
|
|
+ version TEXT,
|
|
|
+ ip TEXT,
|
|
|
+ comment TEXT
|
|
|
+ );");
|
|
|
+ $db->exec("CREATE TABLE IF NOT EXISTS checklist (
|
|
|
+ id INTEGER PRIMARY KEY,
|
|
|
+ response_id INTEGER REFERENCES responses(id),
|
|
|
+ key TEXT
|
|
|
+ );");
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = json_decode(file_get_contents('php://input'), true);
|
|
|
+ $stmt = $db->prepare("INSERT INTO responses(ip, version, comment) VALUES (:ip, :version, :comment);");
|
|
|
+ $stmt->execute(array('ip' => $_SERVER['REMOTE_ADDR'], 'version' => $data['version'], 'comment' => $data['comment']));
|
|
|
+ $responseId = $db->lastInsertId();
|
|
|
+
|
|
|
+ $stmt = $db->prepare("INSERT INTO CHECKLIST (response_id, key) VALUES (:response_id, :key);");
|
|
|
+
|
|
|
+ foreach ($data['checklist'] as $key => $value)
|
|
|
+ {
|
|
|
+ if ($value)
|
|
|
+ {
|
|
|
+ $stmt->execute(array('response_id' => $responseId, 'key' => $key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+catch (Exception $e)
|
|
|
+{
|
|
|
+ $loggable_error = $e->getMessage() . "\n" . $e->getTraceAsString();
|
|
|
+ file_put_contents("survey_errors.txt", $loggable_error, FILE_APPEND);
|
|
|
+}
|