From 7a1935b0979f569b5ac0b6e9a2b2ad15a454f0bc Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Mon, 27 Mar 2023 14:56:40 +0900 Subject: [PATCH 1/4] Add option to enable/disable the feature --- config/df.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/df.php b/config/df.php index 646b92f0..3907567e 100644 --- a/config/df.php +++ b/config/df.php @@ -91,5 +91,6 @@ // list of allowed lookup modifying functions like urlencode, trim, etc. 'allowed_modifiers' => explode(',', env('DF_LOOKUP_MODIFIERS', 'strtoupper,strtolower,ucfirst,lcfirst,ucwords,urlencode,urldecode,rawurlencode,rawurldecode,base64_encode,base64_decode,trim')), - ] + ], + 'generate_report' => env('DF_GENERATE_REPORT', false), ]; From ab051f644e20fb6918b0295c4687ec86d26dfeca Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Mon, 27 Mar 2023 14:57:10 +0900 Subject: [PATCH 2/4] Add route to for generating the report --- routes/routes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/routes/routes.php b/routes/routes.php index 1a212d60..70ca9a1e 100644 --- a/routes/routes.php +++ b/routes/routes.php @@ -12,6 +12,8 @@ Route::prefix(config('df.api_route_prefix', 'api')) ->middleware('df.api') ->group(function () { + Route::post('report', [\DreamFactory\Core\Http\Controllers\ReportController::class, 'generate'])->name('report:generate'); + $versionPattern = 'v[0-9.]+'; $servicePattern = '[_0-9a-zA-Z-.]+'; $resourcePathPattern = '[0-9a-zA-ZÀ-ÿ-_@&\#\!=,:;\/\^\$\.\|\{\}\[\]\(\)\*\+\?\' ]+'; @@ -29,7 +31,7 @@ ['service' => $servicePattern, 'resource' => $resourcePathPattern] ); } -); + ); /* |-------------------------------------------------------------------------- From 51ab701bc92f96b7bf0b33b43478eb3a5bcb6478 Mon Sep 17 00:00:00 2001 From: anas-srikou Date: Mon, 27 Mar 2023 14:58:00 +0900 Subject: [PATCH 3/4] Create controller to process the request --- src/Http/Controllers/ReportController.php | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/Http/Controllers/ReportController.php diff --git a/src/Http/Controllers/ReportController.php b/src/Http/Controllers/ReportController.php new file mode 100644 index 00000000..22405ff1 --- /dev/null +++ b/src/Http/Controllers/ReportController.php @@ -0,0 +1,25 @@ + Date: Mon, 27 Mar 2023 14:58:51 +0900 Subject: [PATCH 4/4] Create event/listener to handle generate report request --- src/Events/GenerateReportEvent.php | 12 ++++ src/Handlers/Events/GenerateReportHandler.php | 71 +++++++++++++++++++ src/Handlers/Events/ServiceEventHandler.php | 7 ++ 3 files changed, 90 insertions(+) create mode 100644 src/Events/GenerateReportEvent.php create mode 100644 src/Handlers/Events/GenerateReportHandler.php diff --git a/src/Events/GenerateReportEvent.php b/src/Events/GenerateReportEvent.php new file mode 100644 index 00000000..d250a9ac --- /dev/null +++ b/src/Events/GenerateReportEvent.php @@ -0,0 +1,12 @@ + \Config::get('app.version'), + 'bitnami_demo' => EnvUtilities::isDemoApplication(), + 'is_hosted' => to_bool(env('DF_MANAGED', false)), + 'is_trial' => to_bool(env('DF_IS_TRIAL', false)), + ]; + + // administrator-only information + $dbDriver = \Config::get('database.default'); + $result['platform']['db_driver'] = $dbDriver; + if ($dbDriver === 'sqlite') { + $result['platform']['sqlite_storage'] = \Config::get('df.db.sqlite_storage'); + } + $result['platform']['install_path'] = base_path() . DIRECTORY_SEPARATOR; + $result['platform']['log_path'] = env('DF_MANAGED_LOG_PATH', + storage_path('logs')) . DIRECTORY_SEPARATOR; + $result['platform']['app_debug'] = env('APP_DEBUG', false); + $result['platform']['log_mode'] = \Config::get('logging.log'); + $result['platform']['log_level'] = \Config::get('logging.log_level'); + $result['platform']['cache_driver'] = \Config::get('cache.default'); + + if ($result['platform']['cache_driver'] === 'file') { + $result['platform']['cache_path'] = \Config::get('cache.stores.file.path') . DIRECTORY_SEPARATOR; + } + + // including information that helps users use the API or debug + $result['server'] = php_uname(); + + /* + * Most API calls return a resource array or a single resource, + * If an array, shall we wrap it?, With what shall we wrap it? + */ + $result['config'] = [ + 'always_wrap_resources' => \Config::get('df.always_wrap_resources'), + 'resources_wrapper' => \Config::get('df.resources_wrapper'), + 'db' => [ + /** The default number of records to return at once for database queries */ + 'max_records_returned' => \Config::get('database.max_records_returned'), + 'time_format' => \Config::get('df.db.time_format'), + 'date_format' => \Config::get('df.db.date_format'), + 'datetime_format' => \Config::get('df.db.datetime_format'), + 'timestamp_format' => \Config::get('df.db.timestamp_format'), + ], + ]; + + $connectors = DB::table('service')->whereNotNull('created_by_id') + ->get(['name', 'type', 'created_date', 'last_modified_date']); + $result['connectors'] = $connectors->toArray(); +// $result['php'] = EnvUtilities::getPhpInfo(); +// // Remove environment variables being kicked back to the client +// unset($result['php']['environment']); +// unset($result['php']['php_variables']); + + + Log::channel('report')->debug('environment', $result); + } +} diff --git a/src/Handlers/Events/ServiceEventHandler.php b/src/Handlers/Events/ServiceEventHandler.php index 4bd0f96c..542fd56c 100644 --- a/src/Handlers/Events/ServiceEventHandler.php +++ b/src/Handlers/Events/ServiceEventHandler.php @@ -4,6 +4,7 @@ use DreamFactory\Core\Events\ApiEvent; use DreamFactory\Core\Events\BaseServiceEvent; +use DreamFactory\Core\Events\GenerateReportEvent; use DreamFactory\Core\Events\ServiceDeletedEvent; use DreamFactory\Core\Events\ServiceAssignedEvent; use DreamFactory\Core\Events\ServiceModifiedEvent; @@ -48,6 +49,12 @@ public function subscribe($events) static::class . '@handleQueryExecutedEvent' ); } + if (config('df.generate_report')) { + $events->listen( + [GenerateReportEvent::class], + GenerateReportHandler::class + ); + } } /**