-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Requests.php
166 lines (143 loc) · 4.8 KB
/
Requests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @package Joomla.Plugins
* @subpackage Task.Requests
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\Task\Requests\Extension;
use Exception;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\Http\HttpFactory;
/**
* Task plugin with routines to make HTTP requests.
* At the moment, offers a single routine for GET requests.
*
* @since 4.1.0
*/
final class Requests extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;
/**
* @var string[]
* @since 4.1.0
*/
protected const TASKS_MAP = [
'plg_task_requests_task_get' => [
'langConstPrefix' => 'PLG_TASK_REQUESTS_TASK_GET_REQUEST',
'form' => 'get_requests',
'method' => 'makeGetRequest',
],
];
/**
* Returns an array of events this subscriber will listen to.
*
* @return string[]
*
* @since 4.1.0
*/
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* @var boolean
* @since 4.1.0
*/
protected $autoloadLanguage = true;
/**
* The http factory
*
* @var HttpFactory
* @since 4.2.0
*/
private $httpFactory;
/**
* The root directory
*
* @var string
* @since 4.2.0
*/
private $rootDirectory;
/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param HttpFactory $httpFactory The http factory
* @param string $rootDirectory The root directory to store the output file in
*
* @since 4.2.0
*/
public function __construct(DispatcherInterface $dispatcher, array $config, HttpFactory $httpFactory, string $rootDirectory)
{
parent::__construct($dispatcher, $config);
$this->httpFactory = $httpFactory;
$this->rootDirectory = $rootDirectory;
}
/**
* Standard routine method for the get request routine.
*
* @param ExecuteTaskEvent $event The onExecuteTask event
*
* @return integer The exit code
*
* @since 4.1.0
* @throws Exception
*/
protected function makeGetRequest(ExecuteTaskEvent $event): int
{
$id = $event->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 = [];
if ($auth && $authType && $authKey) {
$headers = [$authType => $authKey];
}
try {
$response = $this->httpFactory->getHttp([])->get($url, $headers, $timeout);
} catch (Exception $e) {
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));
return TaskStatus::TIMEOUT;
}
$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($this->rootDirectory . "/task_{$id}_response.html");
try {
File::write($responseFilename, $responseBody);
$this->snapshot['output_file'] = $responseFilename;
$responseStatus = 'SAVED';
} catch (Exception $e) {
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT'), 'error');
$responseStatus = 'NOT_SAVED';
}
$this->snapshot['output'] = <<< EOF
======= Task Output Body =======
> URL: $url
> Response Code: $responseCode
> Response: $responseStatus
EOF;
$this->logTask(sprintf($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE'), $responseCode));
if ($response->code !== 200) {
return TaskStatus::KNOCKOUT;
}
return TaskStatus::OK;
}
}