diff --git a/administrator/language/en-GB/plg_task_requests.ini b/administrator/language/en-GB/plg_task_requests.ini index 8b47db87f5543..578b16aa7f2dd 100644 --- a/administrator/language/en-GB/plg_task_requests.ini +++ b/administrator/language/en-GB/plg_task_requests.ini @@ -11,10 +11,14 @@ PLG_TASK_REQUESTS_LABEL_AUTH_HEADER="Authorization Header" PLG_TASK_REQUESTS_LABEL_AUTH_KEY="Authorization Key" PLG_TASK_REQUESTS_LABEL_REQUEST_TIMEOUT="Request Timeout" PLG_TASK_REQUESTS_LABEL_REQUEST_URL="Request URL" +PLG_TASK_REQUESTS_LABEL_SAVE_RESPONSE="Save Response" PLG_TASK_REQUESTS_TASK_GET_REQUEST_DESC="Make GET requests to a server. Supports a custom timeout and authorization headers." PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE="Request response code was: %1$d" PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT="GET request failed or timed out." PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT="Unable write output file!" +PLG_TASK_REQUESTS_TASK_GET_REQUEST_NOT_SAVED="NOT_SAVED" PLG_TASK_REQUESTS_TASK_GET_REQUEST_ROUTINE_END_LOG_MESSAGE="GET return code is: %1$d. Processing Time: %2$.2f seconds" +PLG_TASK_REQUESTS_TASK_GET_REQUEST_ROUTINE_OUTPUT="======= Task Output Body =======\n>URL: %1$s\n>Response Code: %2$d\n>Response: %3$s\n" +PLG_TASK_REQUESTS_TASK_GET_REQUEST_SAVED="SAVED" PLG_TASK_REQUESTS_TASK_GET_REQUEST_TITLE="GET Request" PLG_TASK_REQUESTS_XML_DESCRIPTION="Job plugin to make GET requests to a server." diff --git a/plugins/task/requests/forms/get_requests.xml b/plugins/task/requests/forms/get_requests.xml index a9a26ca012367..c1497833090d9 100644 --- a/plugins/task/requests/forms/get_requests.xml +++ b/plugins/task/requests/forms/get_requests.xml @@ -21,6 +21,18 @@ filter="int" validate="number" /> + + + + getTaskId(); $params = $event->getArgument('params'); - $url = $params->url; - $timeout = $params->timeout; - $auth = (string) $params->auth ?? 0; - $authType = (string) $params->authType ?? ''; - $authKey = (string) $params->authKey ?? ''; - $headers = []; + $url = $params->url; + $timeout = $params->timeout; + $auth = ($params->auth ?? 0) === 1; + $authType = (string) $params->authType ?? ''; + $authKey = (string) $params->authKey ?? ''; + $saveResponse = ($params->saveResponse ?? 0) === 1; + $headers = []; if ($auth && $authType && $authKey) { @@ -108,26 +116,21 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int $responseCode = $response->code; $responseBody = $response->body; - // @todo this handling must be rethought and made safe. stands as a good demo right now. - $responseFilename = Path::clean(JPATH_ROOT . "/tmp/task_{$id}_response.html"); + $responseSaved = false; - if (File::write($responseFilename, $responseBody)) - { - $this->snapshot['output_file'] = $responseFilename; - $responseStatus = 'SAVED'; - } - else + if ($saveResponse) { - $this->logTask('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT', 'error'); - $responseStatus = 'NOT_SAVED'; + $this->saveResponse($id, $url, $responseCode, $responseBody); + $responseSaved = true; } - $this->snapshot['output'] = <<< EOF -======= Task Output Body ======= -> URL: $url -> Response Code: $responseCode -> Response: $responseStatus -EOF; + $responseStatus = (function () use ($responseSaved, $saveResponse) { + $status = $saveResponse && $responseSaved ? 'SAVED' : 'NOT_SAVED'; + + return Text::_("PLG_TASK_REQUESTS_TASK_GET_REQUEST_${status}"); + })(); + + $this->snapshot['output'] = Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_OUTPUT', $url, $responseCode, $responseStatus); $this->logTask(Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE', $responseCode)); @@ -138,4 +141,34 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int return TaskStatus::OK; } + + /** + * Save request response to task snapshot. + * + * @param integer $taskId ID of the task spawning the request routine. + * @param string $url Request URL. + * @param integer $responseCode Request response code. + * @param string $responseBody Response body. + * + * @return boolean Returns false on failure to save the response. + * + * @since __DEPLOY_VERSION__ + * @throws Exception + */ + protected function saveResponse(int $taskId, string $url, int $responseCode, string $responseBody): bool + { + $tmpdir = $this->app->getConfig()->get('tmp_path'); + $responseFilename = Path::check("${tmpdir}/task_{$taskId}_GET_response.html"); + + if (File::write($responseFilename, $responseBody)) + { + $this->snapshot['output_file'] = $responseFilename; + + return true; + } + + $this->logTask('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT', 'error'); + + return false; + } }