Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * PHP APPLICATION BASE KERNEL
- *
- * DOES SOME GOOD THINGS SUCH AS MANAGE COMMUNICATION WITH DATABASE AND SECURE YOUR ASS
- *
- * WORKS ONLY IN >=PHP5.6
- *
- * @author MATHEUS HENRIQUE DE FREITAS (matheus.h.freitas@ufv.br)
- */
- /*
- EXPECTED PATHS:
- CONFIG PATH: *ACTUAL KERNEL PATH*../config
- LOG PATH *ACTUAL KERNEL PATH*../logs
- */
- class kernel {
- private $database;
- private $kernel_config;
- private $database_config;
- private $prepared_stmts = [];
- /* public calls */
- public function __construct()
- {
- date_default_timezone_set("America/Sao_Paulo");
- ob_start();
- /* files check */
- if(!file_exists(dirname(__FILE__) . "/../logs/kernel.log")) $this->panic("0x10", true);
- if(!file_exists(dirname(__FILE__) . "/../logs/security.log")) $this->panic("0x11");
- if(!file_exists(dirname(__FILE__) . "/../logs/database.log")) $this->panic("0x12");
- //config
- if(!file_exists(dirname(__FILE__) . "/../configs/kernel.json")) $this->panic("0x1");
- if(!file_exists(dirname(__FILE__) . "/../configs/database.json")) $this->panic("0x0");
- /* loading config */
- //kernel
- $kernel_jsoned_file = file_get_contents(dirname(__FILE__) . "/../configs/kernel.json");
- $this->kernel_config = json_decode($kernel_jsoned_file, true);
- if(json_last_error() != JSON_ERROR_NONE) $this->panic("1x1");
- //db
- $db_jsoned_file = file_get_contents(dirname(__FILE__) . "/../configs/database.json");
- $this->database_config = json_decode($db_jsoned_file, true);
- if(json_last_error() != JSON_ERROR_NONE) $this->panic("1x0");
- /* config added to be checked in kernel json */
- if(count($this->kernel_config["add_config_files"])>0){
- foreach($this->kernel_config["add_config_files"] as $file){
- if(!file_exists(dirname(__FILE__) . "/../configs/" . $file)) $this->panic("1x2");
- }
- }
- /* initing things that need it */
- $this->database_init();
- if($this->database===false) $this->panic("10x0");
- $this->security();
- session_start();
- }
- public function DBQuery($sql)
- {
- $temp_result = $this->database->query($sql);
- if($temp_result===false)
- {
- $additional_code = rand(100000, 9999999999);
- $this->log(2, "<helper code: #$additional_code> " . $this->database->error);
- $this->panic("10x1 / #$additional_code");
- }
- return $temp_result;
- }
- public function DBQuery_ps_automatic($query, $vs, $vars, $affected=false, $insertid=false)
- {
- $temp = $this->DBQuery_ps_prepare($query);
- $this->DBQuery_ps_bind($temp, $vs, $vars);
- $this->DBQuery_ps_execute($temp);
- if($affected && $insertid) $result = [$this->DBQuery_ps_affectedrows($temp),$this->DBQuery_ps_insertid($temp)];
- elseif($affected) $result = $this->DBQuery_ps_affectedrows($temp);
- elseif($insertid) $result = $this->DBQuery_ps_insertid($temp);
- else $result = $this->DBQuery_ps_result($temp);
- $this->DBQuery_ps_close($temp);
- return $result;
- }
- public function DBQuery_ps_prepare($query)
- {
- $temp_sm = $this->database->prepare($query);
- if($temp_sm===false)
- {
- $additional_code = rand(100000, 9999999999);
- $this->log(2, "PS_P <helper code: #$additional_code> " . $this->database->error);
- $this->panic("10x2 / #$additional_code");
- }
- $higher_index = $this->prepared_stmts===array()?0:(max(array_keys($this->prepared_stmts))+1);
- for($i=0;$i<=$higher_index;$i++){
- if(!isset($this->prepared_stmts[$i])){
- $this->prepared_stmts[$i] = $temp_sm;
- return $i;
- }
- }
- $this->panic("10x3");
- }
- public function DBQuery_ps_bind($id, $vs, $vars)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- if(strlen($vs)!=count($vars)) $this->panic("10x5");
- //$temp_bind = call_user_func_array(array($this->prepared_stmts[$id], 'bind_param'), $vars);
- //Using new PHP5.6 spread operator(...)
- $temp_bind = $this->prepared_stmts[$id]->bind_param($vs, ...$vars);
- if($temp_bind===false) $this->panic("10x5");
- return true;
- }
- public function DBQuery_ps_execute($id)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- $temp_exec = $this->prepared_stmts[$id]->execute();
- if($temp_exec===false) $this->panic("10x6");
- return true;
- }
- public function DBQuery_ps_result($id)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- $temp_res = $this->prepared_stmts[$id]->get_result();
- if($temp_res===false) $this->panic("10x7");
- return $temp_res;
- }
- public function DBQuery_ps_close($id)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- $this->prepared_stmts[$id]->close();
- unset($this->prepared_stmts[$id]);
- return true;
- }
- public function DBQuery_ps_affectedrows($id)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- return $this->prepared_stmts[$id]->affected_rows;
- }
- public function DBQuery_ps_insertid($id)
- {
- if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
- return $this->prepared_stmts[$id]->insert_id;
- }
- /* private calls */
- /* application-kernel related funcs */
- private function database_init()
- {
- $this->database = mysqli_init( );
- $this->database->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
- $this->database->real_connect($this->database_config["hostname"], $this->database_config["user"], $this->database_config["pass"], $this->database_config["database"]);
- if($this->database->connect_errno > 0){
- $this->log(2, "<" . $this->database->connect_errno . "> " . $this->database->connect_error);
- $this->database = false;
- }
- else {
- $this->database->set_charset("utf8");
- }
- }
- private function security()
- {
- /*
- if(isset($_POST))
- {
- foreach($_POST as $key => $value)
- {
- if(!in_array($key, $this->kernel_config["html_parse_whitelist"]))
- $_POST[$key] = htmlspecialchars($_POST[$key]);
- if(!in_array($key, $this->kernel_config["html_slashe_whitelist"]))
- $_POST[$key] = addslashes($_POST[$key]);
- }
- }
- if(isset($_GET))
- {
- foreach($_GET as $key => $value)
- {
- if(!in_array($key, $this->kernel_config["html_parse_whitelist"]))
- $_GET[$key] = htmlspecialchars($_GET[$key]);
- if(!in_array($key, $this->kernel_config["html_slashe_whitelist"]))
- $_GET[$key] = addslashes($_GET[$key]);
- }
- } */
- }
- /* kernel related funcs */
- private function panic($code,$supress_log=false)
- {
- ob_clean();
- http_response_code(500);
- echo "<h1>APPLICATION KERNEL PANIC</h1>";
- echo "<p>Ocorreu um erro irrecuperável no núcleo da aplicação.</p>";
- echo "<p><b>Identificação do erro:</b> $code</p>";
- if(!$supress_log)
- $this->log(0, "Kernel panic - error code: $code");
- exit;
- }
- private function log($type, $to_log)
- {
- if($type==0)
- $file = "kernel";
- elseif($type==1)
- $file = "security";
- elseif($type==2)
- $file = "database";
- file_put_contents(dirname(__FILE__) . "/../logs/" . $file .".log", "[" . date("j/n/y G:i:s") . "] (" . $_SERVER['REMOTE_ADDR'] . ") $to_log" . PHP_EOL, FILE_APPEND);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement