Files
2024-04-19 14:04:41 +07:00

212 lines
5.8 KiB
PHP

<?php
require_once _DOCROOT_.'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
$dotenv = new Dotenv\Dotenv(_DOCROOT_);
$dotenv->load();
class db {
protected $connection;
protected $stmt;
public $insertid;
public $num_rows;
public $result_metadata;
public $result_fetch;
public $fetch_field;
public $query_count = 0;
public $debugging = FALSE;
public $error;
public function __construct($dbhost = '', $dbuser = '', $dbpass = '', $dbname = '', $charset = 'utf8') {
if($dbhost == '' || $dbuser == '' || $dbpass == '' || $dbname == '')
{
$dbhost = 'p:'.$_ENV['DATABASE_HOST'];
$dbuser = $_ENV['DATABASE_USER'];
$dbpass = $_ENV['DATABASE_PASS'];
$dbname = $_ENV['DATABASE_NAME'];
}
$this->connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($this->connection->connect_error) {
$this->debug('Failed to connect to MySQL - ' . $this->connection->connect_error);
}
$this->connection->set_charset($charset);
}
private function makeDir($new_path, $mode) {
return is_dir($new_path) || mkdir($new_path, $mode, true);
}
public function debug($args,$type = 'debug') {
global $logdir;
// create a log channel
$logger = new Logger('query');
$daily_log = date('d-m-Y').'.log';
$year_dir = self::makeDir($logdir.'activity/'.date('Y'),0777);
$month_dir = self::makeDir($logdir.'activity/'.date('Y').'/'.date('m'),0777);
$dir_log = $logdir.'activity/'.date('Y').'/'.date('m').'/'.$daily_log;
$logger->pushHandler(new StreamHandler($dir_log, Logger::DEBUG));
$uri = $_SERVER['REQUEST_URI'];
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$query = $_SERVER['QUERY_STRING'];
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip=$_SERVER['HTTP_CLIENT_IP']; // share internet
} elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; // pass from proxy
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
if($type == 'debug'){
$logger->addDebug($args,[$_SESSION['NAMA_PEGAWAI'],$url,$query,$ip]);
}
elseif($type == 'info'){
$logger->addInfo($args,[$_SESSION['NAMA_PEGAWAI'],$url,$query,$ip]);
}
}
public function query($query) {
if ($this->stmt = $this->connection->prepare($query)) {
if (func_num_args() > 1) {
$x = func_get_args();
$args = array_slice($x, 1);
$types = '';
$args_ref = array();
foreach ($args as $k => &$arg) {
if (is_array($args[$k])) {
foreach ($args[$k] as $j => &$a) {
$types .= $this->_gettype($args[$k][$j]);
$args_ref[] = &$a;
}
} else {
$types .= $this->_gettype($args[$k]);
$args_ref[] = &$arg;
}
}
array_unshift($args_ref, $types);
call_user_func_array(array($this->stmt, 'bind_param'), $args_ref);
}
if($this->debugging == TRUE)
{
$this->debug($query,'info');
}
$this->stmt->execute();
$this->insertid = $this->stmt->insert_id;
if($this->stmt->result_metadata()){
$this->stmt->store_result();
$this->num_rows = $this->stmt->num_rows;
$this->result_metadata = $this->stmt->result_metadata();
$params = array();
$meta = $this->result_metadata;
$this->fetch_field = [];
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
$this->fetch_field[] = (array) $field;
}
call_user_func_array(array($this->stmt, 'bind_result'), $params);
$result = [];
while ($this->stmt->fetch()) {
$r = [];
foreach ($row as $key => $val) {
$r[$key] = $val;
}
array_push($result, $r);
}
$this->result_fetch = $result;
}
$this->stmt->close();
if ($this->connection->errno) {
$this->debug('Unable to process MySQL query (check your params) - ' . $this->connection->error);
$this->error = $this->connection->error;
}
$this->query_count++;
} else {
$this->debug('Unable to prepare statement (check your syntax) - ' . $query);
$this->error = 'Unable to prepare statement';
}
return $this;
}
public function fetchAll() {
return $this->result_fetch;
}
public function fetchFirst() {
$result = $this->result_fetch[0];
return $result;
}
public function fetchLast() {
$jml_data = count($this->result_fetch);
$result = $this->result_fetch[$jml_data-1];
return $result;
}
public function fetchArray() {
$params = array();
$meta = $this->result_metadata;
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($this->stmt, 'bind_result'), $params);
$result = array();
while ($this->stmt->fetch()) {
foreach ($row as $key => $val) {
$result[$key] = $val;
}
}
$this->stmt->close();
return $result;
}
public function fetchField()
{
return $meta->fetch_field();
}
public function numRows() {
return $this->num_rows;
}
public function close() {
return $this->connection->close();
}
public function affectedRows() {
return $this->stmt->affected_rows;
}
private function _gettype($var) {
if(is_string($var)) return 's';
if(is_float($var)) return 'd';
if(is_int($var)) return 'i';
return 'b';
}
public function escape($string)
{
return $this->connection->escape_string($string);
}
public function getError()
{
return $this->error;
}
}
?>