Browse Source

Create server endpoint

Antony Male 8 years ago
parent
commit
df3d4a8c8f
2 changed files with 60 additions and 7 deletions
  1. 9 7
      installer/x64/installer-x64.iss
  2. 51 0
      server/survey.php

+ 9 - 7
installer/x64/installer-x64.iss

@@ -16,7 +16,7 @@
 #define RunRegKey "Software\Microsoft\Windows\CurrentVersion\Run"
 #define DotNetInstallerExe "dotNet451Setup.exe"
 #define DonateUrl "https://synctrayzor.antonymale.co.uk/donate"
-#define SurveyUrl "https://requestb.in/yueovtyu"
+#define SurveyUrl "http://localhost:8080/survey.php"
 
 [Setup]
 AppId={{#AppId}
@@ -416,12 +416,14 @@ begin
     WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
     WinHttpReq.Open('POST', '{#SurveyUrl}', false);
     WinHttpReq.Send('{' +
-      ' "wontWork": '+ SerializeBool(Checklist.Checked[0]) + 
-      ', "notWhatINeed": '+ SerializeBool(Checklist.Checked[1]) +
-      ', "preferResilio": '+ SerializeBool(Checklist.Checked[2]) +
-      ', "dontLikeSyncTrayzor": '+ SerializeBool(Checklist.Checked[3]) +
-      ', "other": '+ SerializeBool(Checklist.Checked[4]) +
-      ', "comments": "' + EscapeJsonString(CommentsBox.Text) + '"' +
+      ' "version": "{#AppVersion}", "comment": "' + EscapeJsonString(CommentsBox.Text) + '"' +
+      ', "checklist": {' +
+        ' "wontWork": '+ SerializeBool(Checklist.Checked[0]) + 
+        ', "notWhatINeed": '+ SerializeBool(Checklist.Checked[1]) +
+        ', "preferResilio": '+ SerializeBool(Checklist.Checked[2]) +
+        ', "dontLikeSyncTrayzor": '+ SerializeBool(Checklist.Checked[3]) +
+        ', "other": '+ SerializeBool(Checklist.Checked[4]) +
+      ' }' +
       ' }');
 
     Abort;

+ 51 - 0
server/survey.php

@@ -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);
+}