1
1
<?php
2
+
3
+
2
4
function escapeJsonString ($ value )
3
5
{
4
6
# list from www.json.org: (\b backspace, \f formfeed)
@@ -10,8 +12,8 @@ function escapeJsonString($value)
10
12
11
13
function ValidateCredentials ($ data )
12
14
{
13
- $ users = array ( " user " => " userpass " ) ;
14
-
15
+ require ' users.php ' ;
16
+
15
17
if (array_key_exists ("credentials " , $ data )==false )
16
18
{
17
19
die ('{"res":"need credentials"} ' );
@@ -29,8 +31,12 @@ function ValidateCredentials($data)
29
31
{
30
32
if ($ users [$ username ]!=$ password )
31
33
{
32
- die ('{"res":"bad login/passwd"} ' );
34
+ die ('{"res":"bad login/passwd"} ' );
33
35
}
36
+ else
37
+ {
38
+ return $ username ;
39
+ }
34
40
}
35
41
else
36
42
{
@@ -47,7 +53,8 @@ function ValidateCredentials($data)
47
53
48
54
date_default_timezone_set ("Europe/Brussels " );
49
55
50
- header ("Content-Type: text/plain; charset=us-ascii " );
56
+ header ("Content-Type: application/javascript; charset=us-ascii " );
57
+ //header("Content-Type: text/plain; charset=us-ascii");
51
58
header_remove ("Transfer-Encoding " );
52
59
header_remove ("Connection " );
53
60
@@ -63,12 +70,12 @@ function ValidateCredentials($data)
63
70
echo "Failed to connect to MySQL: " . $ conn ->lastErrorMsg ();
64
71
}
65
72
66
- if (!($ conn ->exec ("CREATE TABLE IF NOT EXISTS NODES (id TEXT UNIQUE, label TEXT, x FLOAT, y FLOAT) " )))
73
+ if (!($ conn ->exec ("CREATE TABLE IF NOT EXISTS NODES (id TEXT UNIQUE, label TEXT, x FLOAT, y FLOAT, username TEXT DEFAULT 'raul', timestamp TEXT DEFAULT CURRENT_TIMESTAMP ) " )))
67
74
{
68
75
die ( "Error creating NODES table " . $ conn ->lastErrorMsg ());
69
76
}
70
77
71
- if (!($ conn ->exec ("CREATE TABLE IF NOT EXISTS EDGES (id TEXT UNIQUE, source TEXT, target TEXT, answer TEXT) " )))
78
+ if (!($ conn ->exec ("CREATE TABLE IF NOT EXISTS EDGES (id TEXT UNIQUE, source TEXT, target TEXT, answer TEXT, username TEXT DEFAULT 'raul', timestamp TEXT DEFAULT CURRENT_TIMESTAMP ) " )))
72
79
{
73
80
die ( "Error creating EDGES table " . $ conn ->lastErrorMsg ());
74
81
}
@@ -79,32 +86,33 @@ function ValidateCredentials($data)
79
86
{
80
87
$ data = json_decode (file_get_contents ('php://input ' ), true );
81
88
82
- validateCredentials ($ data );
89
+ $ username = validateCredentials ($ data );
83
90
84
91
if (!($ conn ->exec ("BEGIN TRANSACTION " )))
85
92
{
86
- die ( $ conn ->lastErrorMsg ());
93
+ die ( ' {"res":" ' . $ conn ->lastErrorMsg () . ' "} ' );
87
94
}
88
95
89
96
if (array_key_exists ("nodes " , $ data )==true )
90
97
{
91
-
98
+
92
99
$ nodelist = $ data ["nodes " ];
93
100
foreach ($ nodelist as $ node )
94
101
{
95
102
$ id = SQLite3::escapeString ($ node ["id " ]);
96
103
$ l = SQLite3::escapeString ($ node ["l " ]);
97
104
$ x = SQLite3::escapeString ($ node ["x " ]);
98
105
$ y = SQLite3::escapeString ($ node ["y " ]);
99
-
100
- $ str = "insert or replace INTO NODES values( ' $ id', ' $ l', ' $ x', ' $ y' ) " ;
106
+ $ u = $ username ;
107
+
108
+ $ str = "insert or replace INTO NODES values( ' $ id', ' $ l', ' $ x', ' $ y', ' $ u', datetime('now') ) " ;
101
109
if (!$ conn ->exec ($ str ))
102
110
{
103
- die ( " Insert failed: $ str " . $ conn ->lastErrorMsg ());
111
+ die ( ' {"res":"Node Insert failed: ' . escapeJsonString ( $ str) . ' ' . $ conn ->lastErrorMsg () . ' "} ' );
104
112
}
105
113
}
106
114
}
107
-
115
+
108
116
if (array_key_exists ("edges " , $ data )==true )
109
117
{
110
118
$ edgelist = $ data ["edges " ];
@@ -114,20 +122,21 @@ function ValidateCredentials($data)
114
122
$ s = SQLite3::escapeString ($ edge ["s " ]);
115
123
$ t = SQLite3::escapeString ($ edge ["t " ]);
116
124
$ l = SQLite3::escapeString ($ edge ["l " ]);
117
-
118
- $ str = "insert or replace INTO EDGES values( ' $ id', ' $ s', ' $ t', ' $ l' ) " ;
125
+ $ u = $ username ;
126
+
127
+ $ str = "insert or replace INTO EDGES values( ' $ id', ' $ s', ' $ t', ' $ l', ' $ u', datetime('now') ) " ;
119
128
if (!$ conn ->exec ($ str ))
120
129
{
121
- die ( " Insert failed: $ str" . $ conn ->lastErrorMsg ());
130
+ die ( ' {"res":"Edge Insert failed: ' . escapeJsonString ( $ str) . ' ' . $ conn ->lastErrorMsg () . ' "} ' );
122
131
}
123
132
}
124
133
}
125
-
134
+
126
135
if (!($ conn ->exec ("END TRANSACTION " )))
127
136
{
128
- die ( $ conn ->lastErrorMsg ());
137
+ die ( ' {"res":" ' . $ conn ->lastErrorMsg () . ' "} ' );
129
138
}
130
-
139
+
131
140
echo '{"res":"OK"} ' ;
132
141
}
133
142
else if ($ method =="GET " )
@@ -144,7 +153,8 @@ function ValidateCredentials($data)
144
153
$ q = escapeJsonString ($ row ["label " ]);
145
154
$ x = $ row ["x " ];
146
155
$ y = $ row ["y " ];
147
- echo '{"id":" ' .$ id .'", "q":" ' .$ q .'", "x": ' .$ x .', "y": ' .$ y .'} ' ;
156
+ $ u = $ row ["username " ];
157
+ echo '{"id":" ' .$ id .'", "q":" ' .$ q .'", "x": ' .$ x .', "y": ' .$ y .', "u":" ' .$ u .'"} ' ;
148
158
$ counter ++;
149
159
}
150
160
@@ -162,13 +172,14 @@ function ValidateCredentials($data)
162
172
$ id = $ row ["id " ];
163
173
$ s = $ row ["source " ];
164
174
$ t = $ row ["target " ];
165
- $ l = escapeJsonString ($ row ["answer " ]);
166
- echo '{"id":" ' .$ id .'","s":" ' .$ s .'","t":" ' .$ t .'","l":" ' .$ l .'"} ' ;
175
+ $ l = escapeJsonString ($ row ["answer " ]);
176
+ $ u = $ row ["username " ];
177
+ echo '{"id":" ' .$ id .'","s":" ' .$ s .'","t":" ' .$ t .'","l":" ' .$ l .'","u":" ' .$ u .'"} ' ;
167
178
168
179
$ counter ++;
169
180
}
170
181
171
- echo " ] } " ;
182
+ echo ' ],"res":"OK" } ' ;
172
183
173
184
}
174
185
else if ($ method =="DELETE " )
@@ -179,7 +190,7 @@ function ValidateCredentials($data)
179
190
180
191
if (!($ conn ->exec ("BEGIN TRANSACTION " )))
181
192
{
182
- die ( $ conn ->lastErrorMsg ());
193
+ die ( ' {"res":" ' . $ conn ->lastErrorMsg () . ' "} ' );
183
194
}
184
195
185
196
if (array_key_exists ("nodes " , $ data )==true )
@@ -190,7 +201,7 @@ function ValidateCredentials($data)
190
201
$ str = "delete from NODES where id=' $ id' " ;
191
202
if (!$ conn ->exec ($ str ))
192
203
{
193
- die ( " Insert failed: $ str " . $ conn ->lastErrorMsg ());
204
+ die ( ' {"res":"Node Insert failed: ' . $ str . ' ' . $ conn ->lastErrorMsg () . ' "} ' );
194
205
}
195
206
}
196
207
}
@@ -203,14 +214,14 @@ function ValidateCredentials($data)
203
214
$ str = "delete from EDGES where id=' $ id' " ;
204
215
if (!$ conn ->exec ($ str ))
205
216
{
206
- die ( " Insert failed: $ str " . $ conn ->lastErrorMsg ());
217
+ die ( ' {"res":"Edge Insert failed: ' . $ str . ' ' . $ conn ->lastErrorMsg () . ' "} ' );
207
218
}
208
219
}
209
220
}
210
221
211
222
if (!($ conn ->exec ("END TRANSACTION " )))
212
223
{
213
- die ( $ conn ->lastErrorMsg ());
224
+ die ( ' {"res":" ' . $ conn ->lastErrorMsg () . ' "} ' );
214
225
}
215
226
216
227
echo '{"res":"OK"} ' ;
0 commit comments