Skip to content

Commit 388feb4

Browse files
author
raul
committed
adding support for multiple users
database stores name and timestap of the user making changes
1 parent e86da35 commit 388feb4

File tree

6 files changed

+86
-47
lines changed

6 files changed

+86
-47
lines changed

BeliefExplorer.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
}
2424

2525
function Process(sectionName, itemChosen)
26-
{
26+
{
2727
gtag('event', 'page', {'id':sectionName});
28-
28+
2929
variables[sectionName] = itemChosen;
3030
item = Database[sectionName];
3131

@@ -38,11 +38,10 @@
3838
var match = key.match(/\$(.*)\$/);
3939
if (match!=null && match.length==2)
4040
res = match[1]
41-
41+
4242
var answer = key.replace(/(\$)/g,"");
4343
var nextSectionName = item.answers[key];
44-
45-
44+
4645
var onClick = 'ProcessWithPush("'+nextSectionName+'", "'+res+'");return false;';
4746

4847
a+="<li>";

SEMap.html

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
url: "SEMapDatabase.php",
4242
dataType: "json",
4343
data: JSON.stringify({"credentials":credentials}),
44-
success: successFunction
44+
success: function(data)
45+
{
46+
if (data.res!="OK") alert(data.res);
47+
if (successFunction!=undefined)
48+
successFunction(data);
49+
}
4550
});
4651
}
4752

@@ -50,9 +55,14 @@
5055
$.ajax({
5156
type: "POST",
5257
url: "./SEMapDatabase.php",
53-
dataType: "text",
58+
dataType: "json",
5459
data: JSON.stringify({nodes:nodes, edges:edges, credentials:credentials }),
55-
success: successFunction
60+
success: function(data)
61+
{
62+
if (data.res!="OK") alert(data.res);
63+
if (successFunction!=undefined)
64+
successFunction(data);
65+
}
5666
});
5767
}
5868

@@ -64,16 +74,28 @@
6474
dataType: "json",
6575
contentType: "application/json; charset=utf-8",
6676
data: JSON.stringify({nodes:nodes, edges:edges, credentials:credentials }),
67-
success : successFunction
77+
success: function(data)
78+
{
79+
if (data.res!="OK") alert(data.res);
80+
if (successFunction!=undefined)
81+
successFunction(data);
82+
}
6883
});
6984
}
7085

71-
this.LoadDataFromDatabase = function (callback)
86+
this.LoadDataFromDatabase = function (successFunction)
7287
{
73-
$.get("./SEMapDatabase.php", function(data)
74-
{
75-
callback(data);
76-
});
88+
$.ajax({
89+
type: "GET",
90+
url: "./SEMapDatabase.php",
91+
dataType: "json",
92+
success: function(data)
93+
{
94+
if (data.res!="OK") alert(data.res);
95+
if (successFunction!=undefined)
96+
successFunction(data);
97+
}
98+
});
7799
}
78100
}
79101

@@ -90,8 +112,8 @@
90112
credentials.username = user;
91113
credentials.password = pass;
92114

93-
backend.setCredentials(credentials, callback)
94-
}
115+
backend.setCredentials(credentials, callback)
116+
}
95117

96118
this.POST = function(eles, successFunction)
97119
{
@@ -138,8 +160,6 @@
138160
// load elements from database and build diagram
139161
backend.LoadDataFromDatabase( function(data)
140162
{
141-
var data = JSON.parse(data);
142-
143163
var elms = []
144164

145165
var nodes = data["nodes"];
@@ -195,8 +215,8 @@
195215
function logOut()
196216
{
197217
$("#loginlogout").text("LogIn");
198-
credentials["username"] = "";
199-
credentials["password"] = "";
218+
$("#username").val("")
219+
$("#password").val("")
200220
$(".admin").hide();
201221
}
202222

SEMapDatabase.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
24
function escapeJsonString($value)
35
{
46
# list from www.json.org: (\b backspace, \f formfeed)
@@ -10,8 +12,8 @@ function escapeJsonString($value)
1012

1113
function ValidateCredentials($data)
1214
{
13-
$users = array("user" => "userpass");
14-
15+
require 'users.php';
16+
1517
if (array_key_exists("credentials", $data)==false)
1618
{
1719
die('{"res":"need credentials"}');
@@ -29,8 +31,12 @@ function ValidateCredentials($data)
2931
{
3032
if ($users[$username]!=$password)
3133
{
32-
die('{"res":"bad login/passwd"}');
34+
die('{"res":"bad login/passwd"}');
3335
}
36+
else
37+
{
38+
return $username;
39+
}
3440
}
3541
else
3642
{
@@ -47,7 +53,8 @@ function ValidateCredentials($data)
4753

4854
date_default_timezone_set("Europe/Brussels");
4955

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");
5158
header_remove("Transfer-Encoding");
5259
header_remove("Connection");
5360

@@ -63,12 +70,12 @@ function ValidateCredentials($data)
6370
echo "Failed to connect to MySQL: " . $conn->lastErrorMsg();
6471
}
6572

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)")))
6774
{
6875
die( "Error creating NODES table" . $conn->lastErrorMsg());
6976
}
7077

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)")))
7279
{
7380
die( "Error creating EDGES table" . $conn->lastErrorMsg());
7481
}
@@ -79,32 +86,33 @@ function ValidateCredentials($data)
7986
{
8087
$data = json_decode(file_get_contents('php://input'), true);
8188

82-
validateCredentials($data);
89+
$username = validateCredentials($data);
8390

8491
if (!($conn->exec("BEGIN TRANSACTION")))
8592
{
86-
die( $conn->lastErrorMsg());
93+
die( '{"res":"' . $conn->lastErrorMsg() . '"}');
8794
}
8895

8996
if (array_key_exists("nodes", $data)==true)
9097
{
91-
98+
9299
$nodelist = $data["nodes"];
93100
foreach ($nodelist as $node)
94101
{
95102
$id = SQLite3::escapeString($node["id"]);
96103
$l = SQLite3::escapeString($node["l"]);
97104
$x = SQLite3::escapeString($node["x"]);
98105
$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') )";
101109
if (!$conn->exec($str))
102110
{
103-
die( "Insert failed: $str " . $conn->lastErrorMsg());
111+
die( '{"res":"Node Insert failed: ' . escapeJsonString($str) . ' ' . $conn->lastErrorMsg() . '"}');
104112
}
105113
}
106114
}
107-
115+
108116
if (array_key_exists("edges", $data)==true)
109117
{
110118
$edgelist = $data["edges"];
@@ -114,20 +122,21 @@ function ValidateCredentials($data)
114122
$s = SQLite3::escapeString($edge["s"]);
115123
$t = SQLite3::escapeString($edge["t"]);
116124
$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') )";
119128
if (!$conn->exec($str))
120129
{
121-
die( "Insert failed: $str" . $conn->lastErrorMsg());
130+
die( '{"res":"Edge Insert failed: ' . escapeJsonString($str) . ' ' . $conn->lastErrorMsg() . '"}');
122131
}
123132
}
124133
}
125-
134+
126135
if (!($conn->exec("END TRANSACTION")))
127136
{
128-
die( $conn->lastErrorMsg());
137+
die( '{"res":"' . $conn->lastErrorMsg() . '"}');
129138
}
130-
139+
131140
echo '{"res":"OK"}';
132141
}
133142
else if ($method=="GET")
@@ -144,7 +153,8 @@ function ValidateCredentials($data)
144153
$q = escapeJsonString($row["label"]);
145154
$x = $row["x"];
146155
$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.'"}';
148158
$counter++;
149159
}
150160

@@ -162,13 +172,14 @@ function ValidateCredentials($data)
162172
$id = $row["id"];
163173
$s = $row["source"];
164174
$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.'"}';
167178

168179
$counter++;
169180
}
170181

171-
echo "] }";
182+
echo '],"res":"OK" }';
172183

173184
}
174185
else if ($method=="DELETE")
@@ -179,7 +190,7 @@ function ValidateCredentials($data)
179190

180191
if (!($conn->exec("BEGIN TRANSACTION")))
181192
{
182-
die( $conn->lastErrorMsg());
193+
die( '{"res":"' . $conn->lastErrorMsg() . '"}');
183194
}
184195

185196
if (array_key_exists("nodes", $data)==true)
@@ -190,7 +201,7 @@ function ValidateCredentials($data)
190201
$str = "delete from NODES where id='$id'";
191202
if (!$conn->exec($str))
192203
{
193-
die( "Insert failed: $str " . $conn->lastErrorMsg());
204+
die( '{"res":"Node Insert failed: ' . $str . ' ' . $conn->lastErrorMsg() . '"}');
194205
}
195206
}
196207
}
@@ -203,14 +214,14 @@ function ValidateCredentials($data)
203214
$str = "delete from EDGES where id='$id'";
204215
if (!$conn->exec($str))
205216
{
206-
die( "Insert failed: $str " . $conn->lastErrorMsg());
217+
die( '{"res":"Edge Insert failed: ' . $str . ' ' . $conn->lastErrorMsg() . '"}');
207218
}
208219
}
209220
}
210221

211222
if (!($conn->exec("END TRANSACTION")))
212223
{
213-
die( $conn->lastErrorMsg());
224+
die( '{"res":"' . $conn->lastErrorMsg() . '"}');
214225
}
215226

216227
echo '{"res":"OK"}';

SEMap_en.sqlite

1 KB
Binary file not shown.

SEMap_es.sqlite

-2 KB
Binary file not shown.

users.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// set the users here, unhashed passwds... I know!
4+
5+
$users = array(
6+
"user" => "userpass",
7+
);
8+
9+
?>

0 commit comments

Comments
 (0)