Jump to content


Problem auditing queries in Codeigniter with Grocerycrud

audit grocerycrud codeigniter mysql hook

  • Please log in to reply
4 replies to this topic

#1 Pedro Bernal

Pedro Bernal

    Member

  • Members
  • PipPip
  • 12 posts

Posted 24 September 2014 - 03:45 PM

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."')");
    }
  }
}
?>
 
  

 

 



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 25 September 2014 - 03:48 AM

i am sorry ... dont know how you achiving it .. but i have much simpler way (of course u can call it a hack) ... is to hook to the grocery_crud_model.php in that .. where ever the queries are getting executed.. post that u can just add logquery($this->db->last_query()) ... make sure u write the logquery function in helper and include it in autoload.php so it is available globally.

 

Hope this works out for u

 

Happy Hacking GC :)



#3 Lucas

Lucas

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationMedellin - Colombia

Posted 30 April 2015 - 07:23 PM

anyone have a different answer ?



#4 Nitts S Chand

Nitts S Chand

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 13 June 2016 - 12:16 PM

I have to perform to click delete icon in crud table to perform update operation and database table IS_DELETED =YES change flag. Any solution to replace delete query to Update Query....



#5 Nitts S Chand

Nitts S Chand

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 13 June 2016 - 12:20 PM

any one to solve  Above problem...! :-







Also tagged with one or more of these keywords: audit, grocerycrud, codeigniter, mysql, hook

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users