2024-03-15 09:13:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class LonaDB {
|
2024-09-25 16:39:56 +00:00
|
|
|
//Create all needed variables
|
2024-03-15 09:13:20 +00:00
|
|
|
private $host;
|
|
|
|
private $port;
|
|
|
|
private $name;
|
|
|
|
private $password;
|
|
|
|
|
|
|
|
public function __construct($host, $port, $name, $password) {
|
2024-09-25 16:39:56 +00:00
|
|
|
//Import all connection details
|
2024-03-15 09:13:20 +00:00
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->password = $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function makeid($length) {
|
|
|
|
$result = "";
|
|
|
|
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
|
|
|
|
$counter = 0;
|
2024-09-25 16:39:56 +00:00
|
|
|
//Add random characters from $characters to $result until it has reached the dired lenght
|
2024-03-15 09:13:20 +00:00
|
|
|
while ($counter < $length) {
|
|
|
|
$result .= $characters[mt_rand(0, strlen($characters) - 1)];
|
|
|
|
$counter += 1;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function encryptPassword($password, $key) {
|
2024-09-25 16:39:56 +00:00
|
|
|
//Hash our encryption key (ProcessID)
|
2024-03-15 09:13:20 +00:00
|
|
|
$keyBuffer = hash('sha256', $key, true);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Generate IV
|
2024-03-15 09:13:20 +00:00
|
|
|
$iv = openssl_random_pseudo_bytes(16);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Encrypt
|
2024-03-15 09:13:20 +00:00
|
|
|
$encrypted = openssl_encrypt($password, 'aes-256-cbc', $keyBuffer, OPENSSL_RAW_DATA, $iv);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Add the IV to the encrypted value
|
2024-03-15 09:13:20 +00:00
|
|
|
$encryptedString = bin2hex($iv) . ':' . bin2hex($encrypted);
|
|
|
|
return $encryptedString;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sendRequest($action, $data) {
|
2024-09-25 16:39:56 +00:00
|
|
|
//Create socket
|
2024-03-15 09:13:20 +00:00
|
|
|
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Check if socket was created successfully
|
|
|
|
if (!$socket) return ["err" => socket_strerror(socket_last_error())];
|
|
|
|
//Try connecting to the database
|
2024-03-15 09:13:20 +00:00
|
|
|
$result = socket_connect($socket, $this->host, $this->port);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Check if connection was successfull
|
|
|
|
if (!$result) return ["err" => socket_strerror(socket_last_error($socket))];
|
|
|
|
//Generate ProcessID
|
2024-03-15 09:13:20 +00:00
|
|
|
$processID = $this->makeid(5);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Check if we need to encrypt something other than our own password
|
2024-03-15 10:45:30 +00:00
|
|
|
switch($action){
|
|
|
|
case "create_user":
|
|
|
|
$data['user']['password'] = $this->encryptPassword($data['user']['password'], $processID);
|
|
|
|
break;
|
|
|
|
case "check_password":
|
|
|
|
$data['checkPass']['pass'] = $this->encryptPassword($data['checkPass']['pass'], $processID);
|
|
|
|
break;
|
|
|
|
}
|
2024-09-25 16:39:56 +00:00
|
|
|
//Encrypt password
|
2024-03-15 09:13:20 +00:00
|
|
|
$encryptedPassword = $this->encryptPassword($this->password, $processID);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Generate request array
|
2024-03-15 09:13:20 +00:00
|
|
|
$request = json_encode([
|
|
|
|
"action" => $action,
|
|
|
|
"login" => [
|
|
|
|
"name" => $this->name,
|
|
|
|
"password" => $encryptedPassword
|
|
|
|
],
|
|
|
|
"process" => $processID
|
|
|
|
] + $data);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Send request to database
|
2024-03-15 09:13:20 +00:00
|
|
|
socket_write($socket, $request, strlen($request));
|
2024-09-25 16:39:56 +00:00
|
|
|
//Read response form database
|
2024-03-15 09:13:20 +00:00
|
|
|
$response = socket_read($socket, 2048);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Close socket
|
2024-03-15 09:13:20 +00:00
|
|
|
socket_close($socket);
|
2024-09-25 16:39:56 +00:00
|
|
|
//Give back the response
|
2024-03-15 09:13:20 +00:00
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
2024-09-25 16:39:56 +00:00
|
|
|
|
|
|
|
//All other functions work the same
|
2024-03-15 09:13:20 +00:00
|
|
|
public function createFunction($name, $content) {
|
2024-09-25 16:39:56 +00:00
|
|
|
//Generate request to send to the database
|
2024-03-15 09:13:20 +00:00
|
|
|
$data = [
|
|
|
|
"function" => [
|
|
|
|
"name" => $name,
|
|
|
|
"content" => $content
|
|
|
|
]
|
|
|
|
];
|
2024-09-25 16:39:56 +00:00
|
|
|
//Send request to the database and return the response
|
2024-03-15 09:13:20 +00:00
|
|
|
return $this->sendRequest("add_function", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function executeFunction($name) {
|
|
|
|
$data = [
|
|
|
|
"name" => $name
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("execute_function", $data);
|
|
|
|
}
|
|
|
|
|
2024-10-08 08:07:17 +00:00
|
|
|
public function deleteFunction($name) {
|
|
|
|
$data = [
|
|
|
|
"function" => [
|
|
|
|
"name" => $name,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("delete_function", $data);
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:13:20 +00:00
|
|
|
public function getTables($user) {
|
|
|
|
$data = [
|
|
|
|
"user" => $user
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("get_tables", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTableData($table) {
|
|
|
|
$data = [
|
|
|
|
"table" => $table
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("get_table_data", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteTable($table) {
|
|
|
|
$data = [
|
|
|
|
"table" => ["name" => $table]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("delete_table", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createTable($table) {
|
|
|
|
$data = [
|
|
|
|
"table" => ["name" => $table]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("create_table", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set($table, $name, $value) {
|
|
|
|
$data = [
|
|
|
|
"table" => ["name" => $table],
|
|
|
|
"variable" => [
|
|
|
|
"name" => $name,
|
|
|
|
"value" => $value
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("set_variable", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($table, $name){
|
|
|
|
$data = [
|
|
|
|
"table" => ["name" => $table],
|
|
|
|
"variable" => ["name" => $name]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("remove_variable", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($table, $name){
|
|
|
|
$data = [
|
|
|
|
"table" => ["name" => $table],
|
|
|
|
"variable" => [
|
|
|
|
"name" => $name
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("get_variable", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsers(){
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
return $this->sendRequest("get_users", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createUser($name, $pass){
|
|
|
|
$data = [
|
|
|
|
"user" => [
|
|
|
|
"name" => $name,
|
|
|
|
"password" => $pass
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("create_user", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteUser($name){
|
|
|
|
$data = [
|
|
|
|
"user" => [
|
|
|
|
"name" => $name
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("delete_user", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPassword($name, $pass){
|
|
|
|
$data = [
|
|
|
|
"checkPass" => [
|
|
|
|
"name" => $name,
|
|
|
|
"pass" => $pass
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("check_password", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPermission($name, $permission){
|
|
|
|
$data = [
|
|
|
|
"permission" => [
|
|
|
|
"user" => $name,
|
|
|
|
"name" => $permission
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("check_permission", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removePermission($name, $permission){
|
|
|
|
$data = [
|
|
|
|
"permission" => [
|
|
|
|
"user" => $name,
|
|
|
|
"name" => $permission
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("remove_permission", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPermissionsRaw($name){
|
|
|
|
$data = [
|
|
|
|
"user" => $name
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("get_permissions_raw", $data);
|
|
|
|
}
|
|
|
|
|
2024-10-08 08:07:17 +00:00
|
|
|
public function addPermission($name, $permission){
|
2024-03-15 09:13:20 +00:00
|
|
|
$data = [
|
|
|
|
"permission" => [
|
|
|
|
"user" => $name,
|
|
|
|
"name" => $permission
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("add_permission", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function eval($func) {
|
|
|
|
$data = [
|
|
|
|
"function" => $func
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->sendRequest("eval", $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|