-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathillustration.php
More file actions
115 lines (108 loc) · 3.9 KB
/
Copy pathillustration.php
File metadata and controls
115 lines (108 loc) · 3.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
include_once "connect.php";
include_once "checkuser.php";
try {
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
$cmd = 'list';
if (!empty($_REQUEST['request'])) {
if (preg_match('/^(\d+)(?:\/([^\/]+))?$/', $_REQUEST['request'], $matches)) {
$id = $matches[1];
$cmd = $matches[2];
}
elseif (preg_match('/^un\/(.+)$/', $_REQUEST['request'], $matches))
$un = $matches[1];
elseif (preg_match('/^ug\/(\d+)$/', $_REQUEST['request'], $matches))
$uid = $matches[1];
}
if ($cmd == 'illustration') {
$sql = 'SELECT format,illustration FROM tg_illustration';
} else {
$sql = 'SELECT tg_illustration.id,format,fk_phrase predecessor,clientaddress,name user, googleid, timestamp
FROM tg_illustration JOIN tg_user ON fk_user = tg_user.id';
if (isset($id))
$cmd = 'single';
}
if (isset($id))
$sql .= " WHERE tg_illustration.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 (isset($id))
$stmt->bindParam(1, $id);
elseif (isset($uid))
$stmt->bindParam(1, $uid);
elseif (isset($un))
$stmt->bindParam(1, $un);
$stmt->execute();
if ($cmd == 'illustration') {
if ($row = $stmt->fetch()) {
switch($row['format']) {
case 'GIF':
header('Content-Type: image/gif');
break;
case 'SVG':
header('Content-Type: image/svg+xml');
break;
default:
header('Content-Type: image/png');
break;
}
print $row['illustration'];
}
} else {
header('Content-Type: application/json');
$results = array();
while ($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'];
array_push($results, $result);
}
}
if ($cmd == 'list')
echo json_encode($results);
elseif ($cmd == 'single')
echo json_encode($results[0]);
break;
case 'POST':
CheckUser();
if (!($illustration = base64_decode($_REQUEST['illustration'])))
die("Failed to decode picture data");
$stmt = $conn->prepare('INSERT INTO tg_illustration(id,fk_phrase,format,illustration,clientaddress,fk_user)
VALUES(:id,:predecessor,:format,BINARY :illustration,BINARY :clientaddress,:user)');
$id = mt_rand() . mt_rand(0,999999999);
$stmt->execute(array(':id' => $id,
':predecessor' => $_REQUEST['predecessor'],
':format' => $_REQUEST['format'],
':illustration' => $illustration,
':clientaddress' => inet_pton($_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP'])),
':user' => $uid));
$stmt = $conn->prepare('SELECT i.id,i.fk_phrase predecessor,i.format,i.clientaddress,u.name,u.googleid,i.timestamp
FROM tg_illustration i JOIN tg_user u ON i.fk_user = u.id
WHERE i.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;
?>