-
Notifications
You must be signed in to change notification settings - Fork 0
/
place_marker.php
55 lines (47 loc) · 1.76 KB
/
place_marker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
Usage: POST request with well-formed lat, long, msg, and hash.
Requires: msg not null and <=200 characters, lat, long well-formed
Effects: If succesful, adds the provided marker to the db and prints the string "Success".
Prints a string beginning with "Error" on failure.
*/
// Ensure necessary inputs are present and well-formed
if (!isset($_POST["lat"]) || !is_numeric($_POST["lat"])) {
die("Error: lat not set or non-numeric.");
}
if (!isset($_POST["long"]) || !is_numeric($_POST["long"])) {
die("Error: long not set or non-numeric.");
}
if (!isset($_POST["msg"]) || trim($_POST["msg"]) == "" || strlen(trim($_POST["msg"])) > 200) {
die("Error: msg not set or malformed.");
}
if (!isset($_POST["hash"])) {
die("Error: hash not set.");
}
// If we reached this point, this is safe:
$lat = $_POST["lat"];
$long = $_POST["long"];
$msg = trim($_POST["msg"]);
$hash = $_POST["hash"];
// Establish DB connection
$conn = new mysqli(ini_get("mysqli.default_host"),
ini_get("mysqli.default_user"),
ini_get("mysqli.default_pw"),
"memomap");
if ($conn->connect_error) {
die("Error: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4"); // Fix for broken diacritics (é, etc.) and emojis
// Prepare statement
if ($hash == "nofingerprint") {
// User has blocked fingerprinting. Hash should be NULL.
$sql = "INSERT INTO markers (latitude, longitude, message) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("dds", $lat, $long, $msg);
} else {
$sql = "INSERT INTO markers (latitude, longitude, message, author_hash) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ddss", $lat, $long, $msg, $hash);
}
$stmt->execute();
echo("Success");