FILES_LOGGER_HELPER_ADDED
This commit is contained in:
parent
de758a8918
commit
bbc035fea3
@ -47,6 +47,7 @@ class Autoload extends AutoloadConfig
|
||||
public $psr4 = [
|
||||
APP_NAMESPACE => APPPATH, // For custom app namespace
|
||||
'Config' => APPPATH . 'Config',
|
||||
'Helpers' => APPPATH . 'Helpers'
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@ -7,9 +7,9 @@ use CodeIgniter\Router\RouteCollection;
|
||||
*/
|
||||
// $routes->get('/', 'LoginController::index');
|
||||
$routes->get('/test', 'Home::test');
|
||||
$routes->get('/login', 'LoginController::index');
|
||||
$routes->get('/login', 'LoginController::index');///auth/google
|
||||
$routes->get('/logout', 'LoginController::logout');
|
||||
$routes->get('/LoginController/receiveGoogleOAuthResponse', 'LoginController::receiveGoogleOAuthResponse');
|
||||
$routes->get('/oauth2callback', 'LoginController::receiveGoogleOAuthResponse');
|
||||
|
||||
|
||||
$routes->group("/user", ["filter" => "authMVC"], function($routes){
|
||||
|
||||
@ -4,6 +4,8 @@ namespace Config;
|
||||
|
||||
use CodeIgniter\Config\BaseService;
|
||||
use App\Libraries\Slug;
|
||||
use App\Libraries\MyLogger;
|
||||
use App\Libraries\DataServiceSqlite;
|
||||
use App\Controllers\Home;
|
||||
|
||||
/**
|
||||
@ -39,5 +41,24 @@ class Services extends BaseService
|
||||
|
||||
return new Home();
|
||||
}
|
||||
|
||||
public static function mylogger($getShared = true)
|
||||
{
|
||||
if ($getShared) {
|
||||
return static::getSharedInstance('mylogger');
|
||||
}
|
||||
|
||||
return new MyLogger();
|
||||
}
|
||||
|
||||
|
||||
public static function dataServiceSqlite($getShared = true)
|
||||
{
|
||||
if ($getShared) {
|
||||
return static::getSharedInstance('dataServiceSqlite');
|
||||
}
|
||||
|
||||
return new DataServiceSqlite();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -46,6 +46,7 @@ abstract class BaseController extends Controller
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected $session;
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
// Do Not Edit This Line
|
||||
|
||||
@ -14,7 +14,9 @@ class LoginController extends BaseController
|
||||
|
||||
public function index(){
|
||||
|
||||
$isLoggedIn = session()->get('isLoggedIn');
|
||||
// $session_data = ['isLoggedIn' => True ,'userid' => '4'];
|
||||
// set_session_data($session_data);
|
||||
$isLoggedIn = check_session();
|
||||
|
||||
if ($isLoggedIn) {
|
||||
return redirect()->to(base_url('/dashboard/view'));
|
||||
@ -25,6 +27,7 @@ class LoginController extends BaseController
|
||||
|
||||
public function receiveGoogleOAuthResponse()
|
||||
{
|
||||
//echo 'DONE';die();
|
||||
$UserModel = new UserModal();
|
||||
if ($this->request->getGet('code')) {
|
||||
$code = (string) $this->request->getGet('code');
|
||||
|
||||
@ -15,9 +15,18 @@ use App\Models\TeamsModal;
|
||||
|
||||
class UserController extends AdminController
|
||||
{
|
||||
|
||||
protected $myLogger;
|
||||
public function __construct()
|
||||
{
|
||||
// helper('utility');
|
||||
set_session_context('User');
|
||||
$this->myLogger = \Config\Services::mylogger();
|
||||
}
|
||||
|
||||
public function list()
|
||||
{
|
||||
$this->myLogger->logme('error','list {data} called',['data' => '9638527410']);
|
||||
$model = new UserModal();
|
||||
$data['UserList'] = $model->where('is_active', 1)->orderBy('id', 'DESC')->findAll();
|
||||
echo view('layout/header');
|
||||
|
||||
@ -9,26 +9,20 @@ class HttpRequestLog implements FilterInterface
|
||||
{
|
||||
public function before(RequestInterface $request, $arguments = null)
|
||||
{
|
||||
log_message('info', 'HttpRequestLog before');
|
||||
// log_message('info', 'HttpRequestLog before');
|
||||
$uuid = generate_uuid();
|
||||
// echo $uuid;//die();
|
||||
set_session_uuid($uuid);
|
||||
// echo $uuid.'<br>';
|
||||
// $slug = \Config\Services::slug();
|
||||
// $home = \Config\Services::home();
|
||||
// echo $home->testService();
|
||||
// // echo !check_session();// print_r($slug);die();
|
||||
// echo $slug->slugify('www.googl.com article no 5');die();
|
||||
|
||||
// echo 'before HttpRequestLog'.'<br>'
|
||||
$context = 'HTTP REQUEST';
|
||||
$log = \Config\Services::mylogger();
|
||||
$message = '{ip} - {platform} - {browser} - {method} - {endpoint} - {getparams} - {postparams} - REQ RECEIVED';
|
||||
$log->logme('ERROR',$message,['context' => $context],1);
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
|
||||
{
|
||||
// Do something here
|
||||
// log_message('info', 'HttpRequestLog after');
|
||||
// echo 'last HttpRequestLog'.'<br>';
|
||||
// $this->session = \Config\Services::session();
|
||||
// echo $this->session->get('uuid');
|
||||
|
||||
}
|
||||
}
|
||||
25
app/Helpers/HttpRequestHelper.php
Normal file
25
app/Helpers/HttpRequestHelper.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
class HttpRequestHelper
|
||||
{
|
||||
public static function getRequestInfo()
|
||||
{
|
||||
$request = service('request');
|
||||
$data = [
|
||||
'ip' => $request->getIPAddress(),
|
||||
'platform' => $request->getUserAgent()->getPlatform(),
|
||||
'browser' => ($request->getUserAgent()->getBrowser().' '.$request->getUserAgent()->getVersion()),
|
||||
'method' => $request->getMethod(),
|
||||
'endpoint' => $request->uri->getPath(),
|
||||
'getparams' => $request->uri->getSegments(),
|
||||
'postparams' => $request->getPost()
|
||||
];
|
||||
|
||||
$data['method'] = (isset($data['method']) ? strtoupper($data['method']) : $data['method']);
|
||||
$data['getparams'] = is_array($data['getparams']) ? json_encode($data['getparams']) : $data['getparams'];
|
||||
$data['postparams'] = is_array($data['postparams']) ? json_encode($data['postparams']) : $data['postparams'];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@ -50,7 +50,6 @@ if (!function_exists('check_permission')) {
|
||||
if (!function_exists('set_session_uuid')) {
|
||||
function set_session_uuid($uuid)
|
||||
{
|
||||
// $CI =& get_instance();
|
||||
$session = \Config\Services::session();
|
||||
$session->set('uuid', $uuid);
|
||||
}
|
||||
@ -59,9 +58,8 @@ if (!function_exists('set_session_uuid')) {
|
||||
if (!function_exists('get_session_uuid')) {
|
||||
function get_session_uuid()
|
||||
{
|
||||
// $CI =& get_instance();
|
||||
$session = \Config\Services::session();
|
||||
$session->get('uuid');
|
||||
return $session->get('uuid');
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,3 +71,22 @@ if (!function_exists('set_session')) {
|
||||
$session->set($data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('set_session_context')) {
|
||||
function set_session_context($context)
|
||||
{
|
||||
// $CI =& get_instance();
|
||||
$session = \Config\Services::session();
|
||||
$session->set('context',$context);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('get_session_context')) {
|
||||
function get_session_context()
|
||||
{
|
||||
// $CI =& get_instance();
|
||||
$session = \Config\Services::session();
|
||||
return $session->get('context');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,18 +2,18 @@
|
||||
|
||||
// File: app/Helpers/Uuid_helper.php
|
||||
|
||||
if (!function_exists('generate_uuid')) {
|
||||
function generate_uuid($version = 4, $format = 'hex')
|
||||
{
|
||||
$data = random_bytes(16);
|
||||
// echo $data.'<br>';
|
||||
// Set version to 0100
|
||||
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
||||
// Set bits 6-7 to 10
|
||||
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
||||
// if (!function_exists('generate_uuid')) {
|
||||
// function generate_uuid($version = 4, $format = 'hex')
|
||||
// {
|
||||
// $data = random_bytes(16);
|
||||
// // echo $data.'<br>';
|
||||
// // Set version to 0100
|
||||
// $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
||||
// // Set bits 6-7 to 10
|
||||
// $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
||||
|
||||
// Output the 36 character UUID.
|
||||
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
return $uuid;
|
||||
}
|
||||
}
|
||||
// // Output the 36 character UUID.
|
||||
// $uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
// return $uuid;
|
||||
// }
|
||||
// }
|
||||
|
||||
89
app/Libraries/DataServiceSqlite.php
Normal file
89
app/Libraries/DataServiceSqlite.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries;
|
||||
|
||||
use PDO;
|
||||
|
||||
class DataServiceSqlite
|
||||
{
|
||||
protected $sqliteDb;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$dbPath = WRITEPATH . 'logs/sqlite.db';
|
||||
|
||||
try {
|
||||
|
||||
$this->sqliteDb = new PDO('sqlite:' . $dbPath);
|
||||
$this->sqliteDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->createDefaultTable();
|
||||
} catch (\PDOException $e) {
|
||||
// Database doesn't exist, create it
|
||||
log_message('error', 'SQLite DB not found: ' . $e->getMessage());
|
||||
try {
|
||||
log_message('error', 'Creating SQLite DB: ');
|
||||
$this->sqliteDb = new PDO('sqlite:' . $dbPath);
|
||||
$this->sqliteDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Create default table
|
||||
$this->createDefaultTable();
|
||||
log_message('error', 'Creating SQLite DB doné');
|
||||
} catch (\PDOException $e) {
|
||||
log_message('error', 'Failed to create SQLite database: ' . $e->getMessage());
|
||||
throw $e; // Re-throw to allow for further handling
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ... other methods ...
|
||||
|
||||
protected function createDefaultTable()
|
||||
{
|
||||
$sql = "CREATE TABLE IF NOT EXISTS http (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
uuid TEXT,
|
||||
ip TEXT,
|
||||
platform TEXT,
|
||||
browser TEXT,
|
||||
method TEXT,
|
||||
endpoint TEXT,
|
||||
getparams TEXT,
|
||||
postparams TEXT,
|
||||
createdon DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)";
|
||||
|
||||
$this->sqliteDb->exec($sql);
|
||||
|
||||
$sql = "CREATE TABLE IF NOT EXISTS log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
context TEXT,
|
||||
uuid TEXT,
|
||||
msg TEXT,
|
||||
createdon DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)";
|
||||
|
||||
$this->sqliteDb->exec($sql);
|
||||
}
|
||||
|
||||
public function insertData(array $data, string $tableName): bool
|
||||
{
|
||||
log_message('error', 'SQLite insert called: ');
|
||||
unset($data['context']);
|
||||
$columns = implode(', ', array_keys($data));
|
||||
// print_r($columns);
|
||||
$placeholders = implode(', ', array_fill(0, count($data), '?'));
|
||||
|
||||
$sql = "INSERT INTO $tableName ($columns) VALUES ($placeholders)";
|
||||
|
||||
try {
|
||||
$stmt = $this->sqliteDb->prepare($sql);
|
||||
$stmt->execute(array_values($data));
|
||||
return true;
|
||||
} catch (\PDOException $e) {
|
||||
log_message('error', 'SQLite insert failed: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
53
app/Libraries/MyLogger.php
Normal file
53
app/Libraries/MyLogger.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php namespace App\Libraries;
|
||||
|
||||
use CodeIgniter\Log\Logger;
|
||||
use Config\Services;
|
||||
use CodeIgniter\Log\Handlers\FileHandler;
|
||||
use App\Helpers\HttpRequestHelper;
|
||||
|
||||
class MyLogger extends Logger
|
||||
{
|
||||
protected $db = null;
|
||||
protected $redis = null;
|
||||
|
||||
public function __construct($config = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function logme($level, $message, $context = [],$type = 0)
|
||||
{
|
||||
|
||||
|
||||
$message = '{context} - {uuid} -' . $message;
|
||||
// /echo $message;//die();
|
||||
$context = array_merge($context,['uuid' => get_session_uuid()]);
|
||||
// print_r($context);die();
|
||||
// echo isset($context['context']) ? $context['context'] : $this->context;die();
|
||||
$context['context'] = isset($context['context']) ? $context['context'] : get_session_context();
|
||||
|
||||
if($type == 1) //http log only
|
||||
{
|
||||
$path = WRITEPATH . 'logs/http';
|
||||
$fileHandler = new FileHandler(['path' => $path]);
|
||||
//get current request info
|
||||
$requestInfo = HttpRequestHelper::getRequestInfo();
|
||||
$context = array_merge($context,$requestInfo);
|
||||
// print_r($context);//die();
|
||||
$message = preg_replace_callback('/\{(.*?)\}/', function($matches) use ($context) { return $context[$matches[1]]; }, $message);
|
||||
// echo $message;die();
|
||||
$fileHandler->handle($level,$message);
|
||||
|
||||
|
||||
}
|
||||
|
||||
if($type == 0) // default CI log
|
||||
{
|
||||
log_message($level,$message,$context);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
writable/cache/.gitkeep
vendored
Normal file
11
writable/cache/.gitkeep
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
11
writable/logs/.gitkeep
Normal file
11
writable/logs/.gitkeep
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
11
writable/session/.gitkeep
Normal file
11
writable/session/.gitkeep
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
11
writable/uploads/.gitkeep
Normal file
11
writable/uploads/.gitkeep
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user