-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdblibserver.php
More file actions
98 lines (75 loc) · 2.23 KB
/
dblibserver.php
File metadata and controls
98 lines (75 loc) · 2.23 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
<?php
/*
Remote DB Lib Server
*/
// Override the data below with your defaults
$encryption_key = "FFFFFFFFFFFFDDCCFFFFFFFFFFFFDDCC";
$user = "test_dblib";
$password = "supersecret";
$dbname = "agarzia";
$server = "localhost";
$database_type = "mysql";
$cipher = "AES-256-CTR"; // do not change cipher unless you know what you're doing
// Disable errors display
ini_set('display_errors', 1); // set to false == disable display.
require_once("idiorm.php");
/* Auxiliary function */
function debug($msg) {
$debug = true;
if ($debug) {
error_log("[DB LIB] $msg");
}
}
// decrypt the post
$post = file_get_contents('php://input');
$post = openssl_decrypt($post, $cipher, $encryption_key);
if (!$post) {
debug("error on decrypt");
debug(openssl_error_string());
}
debug("Post: $post");
$req = json_decode($post, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$error = json_last_error();
}
if (isset($req["user"])) {
$user = $req["user"];
}
if (isset($req["password"])) {
$password = $req["password"];
}
if (isset($req["db"])) {
$dbname = $req["db"];
}
if (isset($req["database_type"])) {
$database_type = $req["database_type"];
}
ORM::configure("${database_type}:host=${server};dbname=${dbname}");
ORM::configure('username', $user);
ORM::configure('password', $password);
$sql = $req["sql"];
$type = $req["type"];
$retVal = [];
switch($type) {
case "query":
if (isset($req["placeholders"])) {
$retVal = ORM::for_table($req["table"])->raw_query($sql, $req["placeholders"])->find_array();
} else {
$retVal = ORM::for_table($req["table"])->raw_query($sql)->find_array();
}
break;
case "execute":
if (isset($req["placeholders"])) {
$retVal = ORM::raw_execute($sql, $req["placeholders"]);
} else {
$retVal = ORM::raw_execute($sql);
}
break;
default:
break;
}
$retVal = json_encode($retVal);
debug("Response: ${retVal}");
header('Content-Type: text/plain+dblib');
$retVal = openssl_encrypt($retVal, $cipher, $encryption_key);
echo $retVal;