diff --git a/hungng/HungNG_CI_Base_Controllers.php b/hungng/HungNG_CI_Base_Controllers.php index eea1119..fbfdeb2 100644 --- a/hungng/HungNG_CI_Base_Controllers.php +++ b/hungng/HungNG_CI_Base_Controllers.php @@ -15,50 +15,50 @@ * @author 713uk13m * @copyright 713uk13m * - * @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 { @@ -83,19 +83,19 @@ 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) ) ); @@ -103,6 +103,30 @@ protected function defaultJsonResponseInfo() exit; } + /** + * Function defaultJsonResponseInfoFromCLI + * + * User: 713uk13m + * Copyright: 713uk13m + * @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 * @@ -114,12 +138,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; @@ -136,12 +162,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; @@ -158,11 +186,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); } @@ -181,11 +211,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); } @@ -207,20 +239,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; } @@ -240,7 +270,6 @@ protected function errorResponse($message = '') $response['code'] = StatusCodes::HTTP_BAD_REQUEST; $response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST]; $response['errorMessage'] = $message; - return $response; } @@ -255,7 +284,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] ); } @@ -273,8 +302,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 ); } @@ -297,7 +326,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; } @@ -410,9 +438,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) ) ); @@ -428,9 +456,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)); diff --git a/hungng/HungNG_CI_Base_Module.php b/hungng/HungNG_CI_Base_Module.php index e6e646e..df1f8b3 100644 --- a/hungng/HungNG_CI_Base_Module.php +++ b/hungng/HungNG_CI_Base_Module.php @@ -15,50 +15,50 @@ * @author 713uk13m * @copyright 713uk13m * - * @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_Module extends MX_Controller { @@ -83,19 +83,19 @@ 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) ) ); @@ -103,6 +103,30 @@ protected function defaultJsonResponseInfo() exit; } + /** + * Function defaultJsonResponseInfoFromCLI + * + * User: 713uk13m + * Copyright: 713uk13m + * @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 * @@ -114,12 +138,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; @@ -136,12 +162,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; @@ -158,11 +186,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); } @@ -181,11 +211,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); } @@ -207,20 +239,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; } @@ -240,7 +270,6 @@ protected function errorResponse($message = '') $response['code'] = StatusCodes::HTTP_BAD_REQUEST; $response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST]; $response['errorMessage'] = $message; - return $response; } @@ -255,7 +284,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] ); } @@ -273,8 +302,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 ); } @@ -297,7 +326,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; } @@ -410,9 +438,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) ) ); @@ -427,9 +455,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));