forked from jcubic/jquery.terminal-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-rpc-demo.php
46 lines (39 loc) · 1.21 KB
/
mysql-rpc-demo.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
<?php
require('json-rpc.php');
if (function_exists('xdebug_disable')) {
xdebug_disable();
}
class MysqlDemo {
public function query($query) {
$link = new mysqli('localhost', 'user', 'password', 'db_name');
if (mysqli_connect_errno()) {
throw new Exception("MySQL Connection: " . mysqli_connect_error());
}
if (preg_match("/create|drop/", $query)) {
throw new Exception("Sorry you are not allowed to execute '" .
$query . "'");
}
if (!preg_match("/^\s*(select.*from *test|insert *into *test.*|delete *from *test|update *test)\s*$/", $query)) {
throw new Exception("Sorry you can't execute '" . $query .
"' you are only allowed to select, insert, delete " .
"or update 'test' table");
}
if ($res = $link->query($query)) {
if ($res === true) {
return true;
}
if ($res->num_rows > 0) {
while ($row = $res->fetch_array(MYSQLI_NUM)) {
$result[] = $row;
}
return $result;
} else {
return array();
}
} else {
throw new Exception("MySQL Error: " . mysqli_error($link));
}
}
}
handle_json_rpc(new MysqlDemo());
?>