I need to audit the insert, edit and delete queries from my application, I tried using a hook that calls a model that save the query into a table in my database, however it is just saving the SHOW, DESCRIBE and SELECT statements, but the update, adding and deleting actions that I perform on the GroceryCrud views are not being saved.
Can someone help me to figure it out why, Thanks!
Hook code:
<?php
class Db_log {
function __construct()
{
}
function logQueries()
{
$CI = & get_instance();
$CI->load->model("model_log");
$filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php'; // Creating Query Log file with today's date in application/logs folder
$handle = fopen($filepath, "a+"); // Opening file with pointer at the end of the file
$times = $CI->db->query_times; // Get execution time of all the queries executed by controller
foreach ($CI->db->queries as $key => $query)
{
$sql = $query . " \n Execution Time:" . $times[$key]; // Generating SQL file alongwith execution time
fwrite($handle, $sql . "\n\n"); // Writing it in the log file
$CI->model_log->log($sql); // Calling the model
}
fclose($handle); // Close the file
}
}
?>
Model code:
<?php
Class Model_log extends CI_Model
{
public function log($sql)
{
if($_SESSION<>null)
{
$this-> db ->select('*');
$this-> db ->from('usuarios');
$this -> db -> where('nombre', $_SESSION["usuario"]);
$query = $this->db->get();
$user_data= get_object_vars($query->row());
$sql = str_replace("'", "", $sql);
date_default_timezone_set('America/Bogota');
$date = date('Y-m-d H:i:s', time());
$this->db->query("
INSERT INTO `log`(`query`, `usuario`, `fecha`)
VALUES
('".$sql."',".$user_data["id"].",'".$date."')");
}
}
}
?>
