Skip to content

Commit

Permalink
Merge pull request #43 from nguyenanhung/v3.2.0-develop
Browse files Browse the repository at this point in the history
Add action run via CLI
  • Loading branch information
nguyenanhung committed Feb 27, 2024
2 parents 6ed0ebc + 41365d8 commit 2374eeb
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 186 deletions.
214 changes: 121 additions & 93 deletions hungng/HungNG_CI_Base_Controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,50 @@
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*
* @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them. Memory consumption can also be displayed.
* @property CI_Calendar $calendar This class enables the creation of calendars
* @property CI_Cache $cache Caching Class
* @property CI_Cart $cart Shopping Cart Class
* @property CI_Config $config This class contains functions that enable config files to be managed
* @property CI_Controller $controller This class object is the super class that every library in CodeIgniter will be assigned to
* @property CI_DB_forge $dbforge Database Forge Class
* @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them. Memory consumption can also be displayed.
* @property CI_Calendar $calendar This class enables the creation of calendars
* @property CI_Cache $cache Caching Class
* @property CI_Cart $cart Shopping Cart Class
* @property CI_Config $config This class contains functions that enable config files to be managed
* @property CI_Controller $controller This class object is the super class that every library in CodeIgniter will be assigned to
* @property CI_DB_forge $dbforge Database Forge Class
* @property CI_DB_pdo_driver|CI_DB_query_builder|CI_DB_driver $db This is the platform-independent base Query Builder implementation class
* @property CI_DB_utility $dbutil Database Utility Class
* @property CI_Driver_Library $driver Driver Library Class
* @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP
* @property CI_Encrypt $encrypt Provides two-way keyed encoding using Mcrypt
* @property CI_Encryption $encryption Provides two-way keyed encryption via PHP's MCrypt and/or OpenSSL extensions
* @property CI_Exceptions $exceptions Exceptions Class
* @property CI_Form_validation $form_validation Form Validation Class
* @property CI_FTP $ftp FTP Class
* @property CI_Hooks $hooks Provides a mechanism to extend the base system without hacking
* @property CI_Image_lib $image_lib Image Manipulation class
* @property CI_Input $input Pre-processes global input data for security
* @property CI_Javascript $javascript Javascript Class
* @property CI_Jquery $jquery Jquery Class
* @property CI_Lang $lang Language Class
* @property CI_Loader $load Loads framework components
* @property CI_Log $log Logging Class
* @property CI_Migration $migration All migrations should implement this, forces up() and down() and gives access to the CI super-global
* @property CI_Model $model CodeIgniter Model Class
* @property CI_Output $output Responsible for sending final output to the browser
* @property CI_Pagination $pagination Pagination Class
* @property CI_Parser $parser Parser Class
* @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data in order to help with debugging and optimization.
* @property CI_Router $router Parses URIs and determines routing
* @property CI_Security $security Security Class
* @property CI_Session $session Session Class
* @property CI_Table $table Lets you create tables manually or from database result objects, or arrays
* @property CI_Trackback $trackback Trackback Sending/Receiving Class
* @property CI_Typography $typography Typography Class
* @property CI_Unit_test $unit Simple testing class
* @property CI_Upload $upload File Uploading Class
* @property CI_URI $uri Parses URIs and determines routing
* @property CI_User_agent $agent Identifies the platform, browser, robot, or mobile device of the browsing agent
* @property CI_Xmlrpc $xmlrpc XML-RPC request handler class
* @property CI_Xmlrpcs $xmlrpcs XML-RPC server class
* @property CI_Zip $zip Zip Compression Class
* @property CI_Utf8 $utf8 Provides support for UTF-8 environments
* @property CI_DB_utility $dbutil Database Utility Class
* @property CI_Driver_Library $driver Driver Library Class
* @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP
* @property CI_Encrypt $encrypt Provides two-way keyed encoding using Mcrypt
* @property CI_Encryption $encryption Provides two-way keyed encryption via PHP's MCrypt and/or OpenSSL extensions
* @property CI_Exceptions $exceptions Exceptions Class
* @property CI_Form_validation $form_validation Form Validation Class
* @property CI_FTP $ftp FTP Class
* @property CI_Hooks $hooks Provides a mechanism to extend the base system without hacking
* @property CI_Image_lib $image_lib Image Manipulation class
* @property CI_Input $input Pre-processes global input data for security
* @property CI_Javascript $javascript Javascript Class
* @property CI_Jquery $jquery Jquery Class
* @property CI_Lang $lang Language Class
* @property CI_Loader $load Loads framework components
* @property CI_Log $log Logging Class
* @property CI_Migration $migration All migrations should implement this, forces up() and down() and gives access to the CI super-global
* @property CI_Model $model CodeIgniter Model Class
* @property CI_Output $output Responsible for sending final output to the browser
* @property CI_Pagination $pagination Pagination Class
* @property CI_Parser $parser Parser Class
* @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data in order to help with debugging and optimization.
* @property CI_Router $router Parses URIs and determines routing
* @property CI_Security $security Security Class
* @property CI_Session $session Session Class
* @property CI_Table $table Lets you create tables manually or from database result objects, or arrays
* @property CI_Trackback $trackback Trackback Sending/Receiving Class
* @property CI_Typography $typography Typography Class
* @property CI_Unit_test $unit Simple testing class
* @property CI_Upload $upload File Uploading Class
* @property CI_URI $uri Parses URIs and determines routing
* @property CI_User_agent $agent Identifies the platform, browser, robot, or mobile device of the browsing agent
* @property CI_Xmlrpc $xmlrpc XML-RPC request handler class
* @property CI_Xmlrpcs $xmlrpcs XML-RPC server class
* @property CI_Zip $zip Zip Compression Class
* @property CI_Utf8 $utf8 Provides support for UTF-8 environments
*/
class HungNG_CI_Base_Controllers extends CI_Controller
{
Expand All @@ -85,26 +85,50 @@ public function __construct()
protected function defaultJsonResponseInfo()
{
$response = array(
'code' => StatusCodes::HTTP_OK,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
'info' => array(
'name' => 'Nguyen An Hung',
'email' => 'dev@nguyenanhung.com',
'web' => 'https://nguyenanhung.com',
'blog' => 'https://blog.nguyenanhung.com',
'code' => StatusCodes::HTTP_OK,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
'info' => array(
'name' => 'Nguyen An Hung',
'email' => 'dev@nguyenanhung.com',
'web' => 'https://nguyenanhung.com',
'blog' => 'https://blog.nguyenanhung.com',
'facebook' => 'https://facebook.com/nguyenanhung',
'github' => 'https://github.com/nguyenanhung'
'github' => 'https://github.com/nguyenanhung'
),
'request_data' => array(
'ip' => getIPAddress(),
'user_agent' => $this->input->user_agent(true),
'request_data' => is_cli() ? 'Run from CLI' : array(
'ip' => getIPAddress(),
'user_agent' => $this->input->user_agent(true),
'request_method' => $this->input->method(true)
)
);
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
exit;
}

/**
* Function defaultJsonResponseInfoFromCLI
*
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* @return void
*/
protected function defaultJsonResponseInfoFromCLI()
{
$response = array(
'code' => StatusCodes::HTTP_OK,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
'info' => array(
'name' => 'Nguyen An Hung',
'email' => 'dev@nguyenanhung.com',
'web' => 'https://nguyenanhung.com',
'github' => 'https://github.com/nguyenanhung'
),
'action' => 'Default Response from CLI'
);
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
exit;
}

/**
* Function renderOutput
*
Expand All @@ -116,12 +140,14 @@ protected function defaultJsonResponseInfo()
*/
protected function renderOutput($response)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
if (!is_cli()) {
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
}
}
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response))->_display();
exit;
Expand All @@ -138,12 +164,14 @@ protected function renderOutput($response)
*/
protected function renderOutputPretty($response)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
if (!is_cli()) {
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
}
}
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
exit;
Expand All @@ -160,11 +188,13 @@ protected function renderOutputPretty($response)
*/
protected function jsonResponse($response = array(), $status = 200)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
log_message('debug', $message);
if (!is_cli()) {
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
log_message('debug', $message);
}
if (is_array($response) || is_object($response)) {
$response = json_encode($response);
}
Expand All @@ -183,11 +213,13 @@ protected function jsonResponse($response = array(), $status = 200)
*/
protected function jsonResponsePretty($response = array(), $status = 200)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
log_message('debug', $message);
if (!is_cli()) {
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
log_message('debug', $message);
}
if (is_array($response) || is_object($response)) {
$response = json_encode($response, JSON_PRETTY_PRINT);
}
Expand All @@ -209,20 +241,18 @@ protected function errorExceptionResponse($exception)
{
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));

$response = array();
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
$response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST];
if ((defined('ENVIRONMENT') && (ENVIRONMENT === 'development' || ENVIRONMENT === 'staging' || ENVIRONMENT === 'testing')) || in_array(getIPAddress(), config_item('whitelist_ip'), true)) {
$response['error'] = array(
'Code' => $exception->getCode(),
'File' => $exception->getFile(),
'Line' => $exception->getLine(),
'Message' => $exception->getMessage(),
'Code' => $exception->getCode(),
'File' => $exception->getFile(),
'Line' => $exception->getLine(),
'Message' => $exception->getMessage(),
'TraceAsString' => $exception->getTraceAsString(),
);
}

return $response;
}

Expand All @@ -242,7 +272,6 @@ protected function errorResponse($message = '')
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
$response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST];
$response['errorMessage'] = $message;

return $response;
}

Expand All @@ -257,7 +286,7 @@ protected function errorResponse($message = '')
protected function errorMethodResponse()
{
return array(
'code' => StatusCodes::HTTP_METHOD_NOT_ALLOWED,
'code' => StatusCodes::HTTP_METHOD_NOT_ALLOWED,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_METHOD_NOT_ALLOWED]
);
}
Expand All @@ -275,8 +304,8 @@ protected function errorMethodResponse()
protected function errorCredentialsResponse($message = '')
{
return array(
'code' => StatusCodes::HTTP_FORBIDDEN,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_FORBIDDEN],
'code' => StatusCodes::HTTP_FORBIDDEN,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_FORBIDDEN],
'errorMessage' => $message
);
}
Expand All @@ -299,7 +328,6 @@ protected function errorSignatureResponse($validSignature = array())
if ((defined('_PROCESS_TEST_') && _PROCESS_TEST_ === true) || in_array(getIPAddress(), config_item('whitelist_ip'), true)) {
$response['validSignature'] = $validSignature;
}

return $response;
}

Expand Down Expand Up @@ -412,9 +440,9 @@ protected function default_base_flush_logs()
}
$response = array(
'status' => 'OK',
'time' => date('Y-m-d H:i:s'),
'data' => array(
'logs' => $file->cleanLog($applicationPath . 'logs', 7),
'time' => date('Y-m-d H:i:s'),
'data' => array(
'logs' => $file->cleanLog($applicationPath . 'logs', 7),
'logs-data' => $file->cleanLog($applicationPath . 'logs-data', 7)
)
);
Expand All @@ -430,9 +458,9 @@ protected function default_base_flush_logs()
}
} else {
$info = array(
'method' => $this->input->method(true),
'ip_address' => $this->input->ip_address(),
'user_agent' => $this->input->user_agent(true),
'method' => $this->input->method(true),
'ip_address' => $this->input->ip_address(),
'user_agent' => $this->input->user_agent(true),
'request_headers' => $this->input->request_headers(true)
);
log_message('error', json_encode($info));
Expand Down
Loading

0 comments on commit 2374eeb

Please sign in to comment.