-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql-injection.php
70 lines (65 loc) · 2.79 KB
/
sql-injection.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
63
64
65
66
67
68
69
70
<?php
function json_response($value)
{
header('Content-type: application/json');
echo json_encode($value);
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Expose-Headers: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
if ($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$options = null;
foreach ($_REQUEST as $key => $value) {
if (str_starts_with($key, 'PDO::')) {
$options[constant($key)] = $value;
}
}
$dbh = new PDO($_REQUEST['dsn'], $_REQUEST['username'], $_REQUEST['password'], $options);
if (isset($_REQUEST['params'])) {
$stmt = $dbh->prepare($_REQUEST['query']);
$stmt->execute(json_decode($_REQUEST['params']));
} elseif (isset($_REQUEST['query'])) {
$stmt = $dbh->query($_REQUEST['query']);
}
if (isset($stmt)) {
if (isset($_REQUEST['lastInsertId'])) {
if (isset($_REQUEST['lastInsertId_name'])) {
json_response($dbh->lastInsertId($_REQUEST['lastInsertId_name']));
} else {
json_response($dbh->lastInsertId());
}
} else if (isset($_REQUEST['rowCount'])) {
json_response($stmt->rowCount());
} else if (isset($_REQUEST['columnCount'])) {
json_response($stmt->columnCount());
} else if (isset($_REQUEST['fetchColumn'])) {
if (isset($_REQUEST['fetchColumn_column']) && is_numeric($_REQUEST['fetchColumn_column'])) {
json_response($stmt->fetchColumn((int)$_REQUEST['fetchColumn_column']));
} else {
json_response($stmt->fetchColumn());
}
} else if (isset($_REQUEST['fetch'])) {
if (isset($_REQUEST['fetch_mode'])) {
$mode = array_sum(array_map(fn ($name) => constant($name), explode('|', $_REQUEST['fetch_mode'])));
json_response($stmt->fetch($mode));
} else {
json_response($stmt->fetch(PDO::FETCH_ASSOC));
}
} else {
if (isset($_REQUEST['fetchAll_mode'])) {
$mode = array_sum(array_map(fn ($name) => constant($name), explode('|', $_REQUEST['fetchAll_mode'])));
json_response($stmt->fetchAll($mode));
} else {
json_response($stmt->fetchAll(PDO::FETCH_ASSOC));
}
}
}
} catch (PDOException $e) {
http_response_code(400);
json_response($e->getMessage());
}
}