Created
December 22, 2020 17:19
-
-
Save i0bs/6fe47c4c8676f650d7675296ced771f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
/* Created by James Walston, All rights are reserved. | |
HookDB.php acts as a local database class for scalable | |
webservers. Failure to comply to this notice rids the | |
legitimacy of this script. */ | |
include "Error.php"; | |
interface hookclass { | |
# Class setup | |
public function __construct($user, $pass, $key, $file); | |
# Internal checks | |
public function parse_error($value=0); | |
public function parse_conn($user, $pass, $key); | |
public function empty_var($list); | |
# Database checks | |
public function check_table($name); | |
public function check_col($table, $name); | |
public function check_query($table, $value); | |
# Database tools | |
public function new_table($name); | |
public function new_col($table, $name, $buffer_size=0); | |
public function new_query($table, $value); | |
public function get_db(); | |
public function open_db($file); | |
public function commit($table, $file); | |
# Class destruction | |
public function __destruct(); | |
} | |
final class HookDB implements hookclass { | |
# Sets up the class variables | |
public $CONN = 0; | |
private $KEY_VAL = 0; | |
private $ERROR_VAL; | |
private $file; | |
private $DB; | |
# Initializes the class construction | |
public function __construct($user, $pass, $key, $file) { | |
$this->empty_var([$user, $pass, $key, $file]); | |
// Checks for an existant configuration .INI file | |
if(file_exists("{$file}.ini")) { | |
$this->file = file_get_contents("{$file}.ini"); | |
$values = explode("|", $this->file); | |
$this->DB = [ | |
"username" => $values[0], | |
"password" => $values[1], | |
"key" => $values[2], | |
"table" => [] | |
]; | |
// Checks if the DB information are not empty values | |
if(!$this->empty_var($key)) { | |
if(!$this->empty_var($this->DB["username"]) | |
&& !$this->empty_var($this->DB["password"])) { | |
$this->parse_conn($user, $pass, $key); | |
} | |
else $this->parse_error(); | |
} | |
else $this->parse_error(); | |
} | |
else $this->parse_error(1); | |
} | |
# Gives back a switch/case error | |
public function parse_error($value=0) { | |
switch($value) { | |
case 0: | |
$this->ERROR_VAL = "Empty variable."; | |
break; | |
case 1: | |
$this->ERROR_VAL = "File non-existant."; | |
break; | |
case 2: | |
$this->ERROR_VAL = "Username/password check inequal."; | |
break; | |
case 3: | |
$this->ERROR_VAL = "Key check inequal."; | |
break; | |
case 4: | |
$this->ERROR_VAL = "Unsupported connection."; | |
break; | |
case 5: | |
$this->ERROR_VAL = "Table existant."; | |
break; | |
case 6: | |
$this->ERROR_VAL = "Column existant."; | |
break; | |
case 7: | |
$this->ERROR_VAL = "Query existant."; | |
break; | |
case 8: | |
$this->ERROR_VAL = "Table non-existant."; | |
break; | |
case 9: | |
$this->ERROR_VAL = "No columns exist."; | |
break; | |
case 10: | |
$this->ERROR_VAL = "Column non-existant."; | |
break; | |
default: | |
$this->ERROR_VAL = "Def Error"; | |
break; | |
} | |
die("[INTERNAL] [ERROR] {$this->ERROR_VAL}"); | |
} | |
# Checks the key and connection info to see if legitimate | |
public function parse_conn($user, $pass, $key) { | |
// If any of the variables are empty, give empty error value | |
if(empty_var([$user, $pass, $key])) { | |
$this->parse_error(); | |
} | |
// Checks if the username and password are the same in Base64 | |
// If so, the connection becomes allowed. | |
elseif($user == base64_encode($this->DB["username"])) { | |
if($pass == base64_encode($this->DB["password"])) { | |
$this->CONN = 1; | |
} | |
} | |
// Otherwise, disconnect and give output error. | |
else { | |
$this->CONN = 0; | |
$this->parse_error(2); | |
} | |
// Note: there is an error with splitting by the | character. This is due to non-truncating | |
// the \n after the 2nd |. This needs to be fixed in the future. For now, it has to | |
// manually be fixed in Notepad. Checks if the connection being existant | |
if($this->CONN) { | |
// If the key is equal to the Base64 encryption version, allow. | |
if($key === base64_encode($this->DB["key"])) { | |
$this->KEY_VAL = 1; | |
} | |
// Otherwise, disconnect and give output error. | |
else { | |
$this->KEY_VAL = 0; | |
$this->parse_error(3); | |
} | |
} | |
else $this->parse_error(4); | |
} | |
# Checks if a variable is empty (shorten function code checks) | |
public function empty_var($list) { | |
// This checks if the list variable is set as an array. | |
// If so, it will check each given in the array if empty. | |
// If any are, this will pass an empty output error. | |
if(is_array($list)) { | |
foreach($list as $var) { | |
if(empty($var)) { | |
$this->parse_error(); | |
} | |
} | |
} | |
// Same concept as before, but no array and a single variable | |
else { | |
if(empty($list)) { | |
$this->parse_error(); | |
} | |
} | |
} | |
# Checks if the table already exists within the database | |
public function check_table($name) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var($name); | |
// If the table exists within the Database, pass. | |
if(in_array($name, $this->DB["table"])) { | |
return true; | |
} | |
// Otherwise, fail. | |
else return false; | |
} | |
# Checks if the column already exists within the table of DB | |
public function check_col($table, $name) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var([$table, $name]); | |
// If the column exists within the specified table of the Database, | |
// allow it to pass. | |
if(in_array($name, $this->DB["table"][$table]["cols"])) { | |
return true; | |
} | |
// Otherwise, fail. | |
else return false; | |
} | |
# Checks if the query already exists within the content of DB | |
public function check_query($table, $value) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var([$table, $value]); | |
// Acting as some concept of check_col(), but with queries. | |
if(in_array($value, $this->DB["table"][$table]["content"])) { | |
return true; | |
} | |
// Otherwise, fail. | |
else return false; | |
} | |
# Creates a new table within the database | |
public function new_table($name) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var($name); | |
// If the table does not exist, create it. | |
if(!$this->check_table($name)) { | |
$this->DB["table"][$name] = [ | |
"cols" => [], | |
"content" => [], | |
"ROW_COUNT" => 0, | |
"COL_COUNT" => 0 | |
]; | |
} | |
// Otherwise, give existant table error. | |
else $this->parse_error(5); | |
} | |
# Creates a list of columns within the database | |
public function new_col($table, $name, $buffer_size=0) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var([$table, $name]); | |
// Creating database variable to help with longcodeitis. | |
$db = $this->DB["table"][$table]; | |
// If the column does not exist, create it. | |
if(!$this->check_col($table, $name)) { | |
$col_count = $db["COL_COUNT"]; | |
$this->DB["table"][$table]["COL_COUNT"]++; | |
$this->DB["table"][$table]["cols"][$col_count] = $name; | |
} | |
// Otherwise, give existant column error. | |
else $this->parse_error(6); | |
} | |
# Creates a new field/query in the specified column. (Appends row) | |
public function new_query($table, $value) { | |
// If variable(s) is/are empty, give empty output error. | |
$this->empty_var([$table, $value]); | |
// Creating database variable to help with longcodeitis. | |
$db = $this->DB["table"][$table]; | |
// Same concept as new_col(), except with queries. | |
if(!$this->check_query($table, $value)) { | |
$len = $db["ROW_COUNT"]; | |
$this->DB["table"][$table]["ROW_COUNT"]++; | |
$this->DB["table"][$table]["content"][$len] = $value; | |
} | |
// Otherwise, give existant query error. | |
else $this->parse_error(7); | |
} | |
# Gets the content of the tables from within the database | |
public function get_db() { | |
return $this->DB["table"]; | |
} | |
# Gets the content of the tables from within the database files | |
public function open_db($file) { | |
return parse_ini_file("database/{$file}.ini", true); | |
} | |
# Writes all of the database info to an .ini file for security | |
public function commit($table, $file) { | |
$db = $this->DB["table"][$table]; | |
$this->empty_var([$table, $file]); | |
if($this->check_table($table)) { | |
file_put_contents("database/{$file}.ini", serialize($db)); | |
} | |
else $this->parse_error(8); | |
} | |
# Destroys the class for security | |
public function __destruct() { | |
$this->file = null; | |
$this->DB = null; | |
$this->CONN = null; | |
$this->KEY_VAL = null; | |
$this->ERROR_VAL = null; | |
} | |
} | |
// Just a test script showing HookDB in action. | |
// Writing values for connection. | |
$user = base64_encode("admin"); | |
$pass = base64_encode("pass"); | |
$key = base64_encode("tokenkey"); | |
$file = "config"; | |
echo "Attempting connection...\n"; | |
$db = new HookDB($user, $pass, $key, $file); | |
// If we connect, and while connected, make our table. | |
if($db->CONN) { | |
echo "Connected! Opening fake table environment...\n"; | |
$db->new_table("test"); | |
$db->new_col("test", "col 1"); | |
$db->new_col("test", "col 2"); | |
$db->new_query("test", ["val 1", "val 2"]); | |
$content = $db->get_db(); | |
// Display it back. | |
echo "<pre>"; print_r($content["test"]); echo "</pre>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment