-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphrase.php
More file actions
81 lines (80 loc) · 3.01 KB
/
Copy pathphrase.php
File metadata and controls
81 lines (80 loc) · 3.01 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
include_once "connect.php";
include_once "checkuser.php";
try {
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (!empty($_REQUEST['request'])) {
if (preg_match('/^\d+$/', $_REQUEST['request'], $matches))
$id = $matches[0];
elseif (preg_match('/^un\/(.+)$/', $_REQUEST['request'], $matches))
$un = $matches[1];
elseif (preg_match('/^ug\/(\d+)$/', $_REQUEST['request'], $matches))
$uid = $matches[1];
}
$sql = 'SELECT tg_phrase.id,fk_illustration predecessor,phrase,clientaddress,name user,googleid,timestamp
FROM tg_phrase JOIN tg_user ON fk_user = tg_user.id';
$single = false;
if (isset($id)) {
$single = true;
$sql .= " WHERE tg_phrase.id=?";
} elseif (isset($uid))
$sql .= " WHERE tg_user.googleid=?";
elseif (isset($un))
$sql .= " WHERE tg_user.name=? and tg_user.googleid is null";
$stmt = $conn->prepare($sql);
if ($single)
$stmt->bindParam(1, $id);
elseif (isset($uid))
$stmt->bindParam(1, $uid);
elseif (isset($un))
$stmt->bindParam(1, $un);
$stmt->execute();
$results = array();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result['clientaddress'] = !empty($result['clientaddress']) ? inet_ntop($result['clientaddress']) : '';
// javascript can't handle bigint
$result['id'] = (string)$result['id'];
$result['predecessor'] = (string)$result['predecessor'];
array_push($results, $result);
}
header('Content-Type: application/json');
if ($single)
echo json_encode($results[0]);
else
echo json_encode($results);
break;
case 'POST':
CheckUser();
$stmt = $conn->prepare('INSERT INTO tg_phrase(id,fk_illustration,phrase,clientaddress,fk_user)
VALUES(:id,:predecessor,:phrase,BINARY :clientaddress,:user)');
$id = mt_rand() . mt_rand(0,999999999);
$stmt->execute(array(':id' => $id,
':predecessor' => $_REQUEST['predecessor'],
':phrase' => $_REQUEST['phrase'],
':clientaddress' => inet_pton($_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP'])),
':user' => $uid));
$stmt = $conn->prepare("SELECT p.id, p.fk_illustration predecessor, p.phrase, p.clientaddress, u.name user, u.googleid, p.timestamp
FROM tg_phrase p JOIN tg_user u on p.fk_user = u.id WHERE p.id=?");
$stmt->execute(array($id));
if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result['clientaddress'] = $result['clientaddress'] ? inet_ntop($result['clientaddress']) : '';
// javascript can't handle bigint
$result['id'] = (string)$result['id'];
$result['predecessor'] = (string)$result['predecessor'];
header('Content-Type: application/json');
echo json_encode($result);
} else {
http_response_code(500);
header('Content-Type: text/plain');
echo "Failed to retrieve inserted row $id user $uid";
}
break;
}
} catch (PDOException $e) {
http_response_code(500);
header('Content-Type: text/plain');
echo $e->getMessage();
}
$conn = null;
?>