-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
62 lines (54 loc) · 1.87 KB
/
api.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
56
57
58
59
60
61
62
<?php
include('conf.php');
session_start();
if(!isset($_SESSION['secret'])) {
header('Location: login.php');
exit();
}
function printError($msg){
$json=['error'=>$msg];
echo json_encode($json);
exit();
}
if(!isset($_POST['action'])){
printError("No action specified");
}
$con = new PDO('mysql:host='.$server.';dbname='.$table.';charset=utf8', $username, $password);
if($_POST['action']=='get_comments'){
if(!isset($_POST['feature'])){
printError("No feature specified");
}
$stmt = $con->prepare("SELECT * FROM content WHERE feature_id=:fid");
$bind = ['fid' => $_POST['feature']];
$res = $stmt->execute($bind);
$row=$stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($row);
}
if($_POST['action']=='get_status'){
if(!isset($_POST['features'])){
printError("No feature specified");
}
$features=explode(",",filter_var($_POST['features'],FILTER_SANITIZE_ENCODED));
$question=array_fill(0,count($features),'?');
$stmt = $con->prepare("SELECT * FROM status WHERE feature_id IN (".implode(', ',$question).")");
$res = $stmt->execute($features);
$row=$stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($row);
}
if($_POST['action']=='comment'){
if(!isset($_POST['feature']) || !isset($_POST['comment']) || !isset($_POST['status'])){
printError("No feature specified or no comment or status");
}
$stmt = $con->prepare("INSERT INTO content VALUES (:fid, :time, :username, :comment)");
$bind = ['fid' => $_POST['feature'],
'time' => time(),
'username' => $_SESSION['osm_user'],
'comment' => $_POST['comment']
];
$res = $stmt->execute($bind);
$stmt = $con->prepare("INSERT INTO status VALUES (:fid, :status) ON DUPLICATE KEY UPDATE status = :status");
$bind = ['fid' => $_POST['feature'],
'status' => $_POST['status']
];
$res = $stmt->execute($bind);
}