Advertisement
firsttrue

kernel.php

May 25th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.43 KB | None | 0 0
  1. <?php
  2. /**
  3. *                 PHP APPLICATION BASE KERNEL
  4. *
  5. * DOES SOME GOOD THINGS SUCH AS MANAGE COMMUNICATION WITH DATABASE AND SECURE YOUR ASS
  6. *
  7. * WORKS ONLY IN >=PHP5.6
  8. *
  9. * @author MATHEUS HENRIQUE DE FREITAS (matheus.h.freitas@ufv.br)
  10. */
  11.  
  12.  
  13. /*
  14.   EXPECTED PATHS:
  15.   CONFIG PATH: *ACTUAL KERNEL PATH*../config
  16.   LOG PATH *ACTUAL KERNEL PATH*../logs
  17. */
  18.  
  19. class kernel {
  20.  
  21.   private $database;
  22.   private $kernel_config;
  23.   private $database_config;
  24.  
  25.   private $prepared_stmts = [];
  26.  
  27.   /* public calls */
  28.  
  29.   public function __construct()
  30.   {
  31.     date_default_timezone_set("America/Sao_Paulo");
  32.  
  33.     ob_start();
  34.  
  35.     /* files check */
  36.     if(!file_exists(dirname(__FILE__) . "/../logs/kernel.log")) $this->panic("0x10", true);
  37.     if(!file_exists(dirname(__FILE__) . "/../logs/security.log")) $this->panic("0x11");
  38.     if(!file_exists(dirname(__FILE__) . "/../logs/database.log")) $this->panic("0x12");
  39.  
  40.     //config
  41.     if(!file_exists(dirname(__FILE__) . "/../configs/kernel.json")) $this->panic("0x1");
  42.     if(!file_exists(dirname(__FILE__) . "/../configs/database.json")) $this->panic("0x0");
  43.  
  44.  
  45.     /* loading config */
  46.     //kernel
  47.     $kernel_jsoned_file = file_get_contents(dirname(__FILE__) . "/../configs/kernel.json");
  48.     $this->kernel_config = json_decode($kernel_jsoned_file, true);
  49.  
  50.     if(json_last_error() != JSON_ERROR_NONE) $this->panic("1x1");
  51.  
  52.     //db
  53.     $db_jsoned_file = file_get_contents(dirname(__FILE__) . "/../configs/database.json");
  54.     $this->database_config = json_decode($db_jsoned_file, true);
  55.  
  56.     if(json_last_error() != JSON_ERROR_NONE) $this->panic("1x0");
  57.  
  58.  
  59.     /* config added to be checked in kernel json */
  60.  
  61.     if(count($this->kernel_config["add_config_files"])>0){
  62.       foreach($this->kernel_config["add_config_files"] as $file){
  63.         if(!file_exists(dirname(__FILE__) . "/../configs/" . $file)) $this->panic("1x2");
  64.       }
  65.     }
  66.  
  67.  
  68.  
  69.     /* initing things that need it */
  70.     $this->database_init();
  71.     if($this->database===false) $this->panic("10x0");
  72.  
  73.     $this->security();
  74.  
  75.     session_start();
  76.   }
  77.  
  78.   public function DBQuery($sql)
  79.   {
  80.     $temp_result = $this->database->query($sql);
  81.     if($temp_result===false)
  82.     {
  83.       $additional_code = rand(100000, 9999999999);
  84.       $this->log(2, "<helper code: #$additional_code> " . $this->database->error);
  85.       $this->panic("10x1 / #$additional_code");
  86.     }
  87.     return $temp_result;
  88.   }
  89.  
  90.   public function DBQuery_ps_automatic($query, $vs, $vars, $affected=false, $insertid=false)
  91.   {
  92.     $temp = $this->DBQuery_ps_prepare($query);
  93.     $this->DBQuery_ps_bind($temp, $vs, $vars);
  94.     $this->DBQuery_ps_execute($temp);
  95.     if($affected && $insertid) $result = [$this->DBQuery_ps_affectedrows($temp),$this->DBQuery_ps_insertid($temp)];
  96.     elseif($affected) $result = $this->DBQuery_ps_affectedrows($temp);
  97.     elseif($insertid) $result = $this->DBQuery_ps_insertid($temp);
  98.     else $result = $this->DBQuery_ps_result($temp);
  99.     $this->DBQuery_ps_close($temp);
  100.     return $result;
  101.   }
  102.  
  103.   public function DBQuery_ps_prepare($query)
  104.   {
  105.     $temp_sm = $this->database->prepare($query);
  106.     if($temp_sm===false)
  107.     {
  108.       $additional_code = rand(100000, 9999999999);
  109.       $this->log(2, "PS_P <helper code: #$additional_code> " . $this->database->error);
  110.       $this->panic("10x2 / #$additional_code");
  111.     }
  112.  
  113.     $higher_index = $this->prepared_stmts===array()?0:(max(array_keys($this->prepared_stmts))+1);
  114.  
  115.     for($i=0;$i<=$higher_index;$i++){
  116.       if(!isset($this->prepared_stmts[$i])){
  117.         $this->prepared_stmts[$i] = $temp_sm;
  118.         return $i;
  119.       }
  120.     }
  121.     $this->panic("10x3");
  122.   }
  123.  
  124.   public function DBQuery_ps_bind($id, $vs, $vars)
  125.   {
  126.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  127.     if(strlen($vs)!=count($vars)) $this->panic("10x5");
  128.  
  129.     //$temp_bind = call_user_func_array(array($this->prepared_stmts[$id], 'bind_param'), $vars);
  130.  
  131.     //Using new PHP5.6 spread operator(...)
  132.     $temp_bind = $this->prepared_stmts[$id]->bind_param($vs, ...$vars);
  133.  
  134.     if($temp_bind===false) $this->panic("10x5");
  135.  
  136.     return true;
  137.   }
  138.  
  139.   public function DBQuery_ps_execute($id)
  140.   {
  141.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  142.  
  143.     $temp_exec = $this->prepared_stmts[$id]->execute();
  144.  
  145.     if($temp_exec===false) $this->panic("10x6");
  146.  
  147.     return true;
  148.   }
  149.  
  150.   public function DBQuery_ps_result($id)
  151.   {
  152.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  153.  
  154.     $temp_res = $this->prepared_stmts[$id]->get_result();
  155.  
  156.     if($temp_res===false) $this->panic("10x7");
  157.  
  158.     return $temp_res;
  159.   }
  160.  
  161.   public function DBQuery_ps_close($id)
  162.   {
  163.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  164.  
  165.     $this->prepared_stmts[$id]->close();
  166.     unset($this->prepared_stmts[$id]);
  167.  
  168.     return true;
  169.   }
  170.  
  171.   public function DBQuery_ps_affectedrows($id)
  172.   {
  173.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  174.  
  175.     return $this->prepared_stmts[$id]->affected_rows;
  176.   }
  177.  
  178.   public function DBQuery_ps_insertid($id)
  179.   {
  180.     if(!is_numeric($id) || !isset($this->prepared_stmts[$id])) $this->panic("10x4");
  181.  
  182.     return $this->prepared_stmts[$id]->insert_id;
  183.   }
  184.  
  185.  
  186.  
  187.  
  188.   /* private calls */
  189.  
  190.  
  191.   /* application-kernel related funcs */
  192.  
  193.   private function database_init()
  194.   {
  195.     $this->database = mysqli_init( );
  196.     $this->database->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
  197.     $this->database->real_connect($this->database_config["hostname"], $this->database_config["user"], $this->database_config["pass"], $this->database_config["database"]);
  198.  
  199.     if($this->database->connect_errno > 0){
  200.       $this->log(2, "<" . $this->database->connect_errno . "> " . $this->database->connect_error);
  201.       $this->database = false;
  202.     }
  203.     else {
  204.       $this->database->set_charset("utf8");
  205.     }
  206.   }
  207.  
  208.  
  209.   private function security()
  210.   {
  211.     /*
  212.     if(isset($_POST))
  213.     {
  214.         foreach($_POST as $key => $value)
  215.         {
  216.         if(!in_array($key, $this->kernel_config["html_parse_whitelist"]))
  217.           $_POST[$key] = htmlspecialchars($_POST[$key]);
  218.  
  219.         if(!in_array($key, $this->kernel_config["html_slashe_whitelist"]))
  220.           $_POST[$key] = addslashes($_POST[$key]);
  221.         }
  222.     }
  223.     if(isset($_GET))
  224.     {
  225.         foreach($_GET as $key => $value)
  226.         {
  227.         if(!in_array($key, $this->kernel_config["html_parse_whitelist"]))
  228.           $_GET[$key] = htmlspecialchars($_GET[$key]);
  229.  
  230.         if(!in_array($key, $this->kernel_config["html_slashe_whitelist"]))
  231.           $_GET[$key] = addslashes($_GET[$key]);
  232.         }
  233.     } */
  234.   }
  235.  
  236.  
  237.   /* kernel related funcs */
  238.  
  239.   private function panic($code,$supress_log=false)
  240.   {
  241.     ob_clean();
  242.     http_response_code(500);
  243.     echo "<h1>APPLICATION KERNEL PANIC</h1>";
  244.     echo "<p>Ocorreu um erro irrecuperável no núcleo da aplicação.</p>";
  245.     echo "<p><b>Identificação do erro:</b> $code</p>";
  246.     if(!$supress_log)
  247.       $this->log(0, "Kernel panic - error code: $code");
  248.     exit;
  249.   }
  250.  
  251.   private function log($type, $to_log)
  252.   {
  253.     if($type==0)
  254.       $file = "kernel";
  255.     elseif($type==1)
  256.       $file = "security";
  257.     elseif($type==2)
  258.       $file = "database";
  259.  
  260.     file_put_contents(dirname(__FILE__) . "/../logs/" . $file .".log", "[" . date("j/n/y G:i:s") . "] (" . $_SERVER['REMOTE_ADDR'] . ") $to_log" . PHP_EOL, FILE_APPEND);
  261.   }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement