first commit
This commit is contained in:
53
plugins/task/requests/forms/get_requests.xml
Normal file
53
plugins/task/requests/forms/get_requests.xml
Normal file
@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form>
|
||||
<fields name="params">
|
||||
<fieldset name="task_params">
|
||||
<field
|
||||
name="url"
|
||||
type="url"
|
||||
label="PLG_TASK_REQUESTS_LABEL_REQUEST_URL"
|
||||
required="true"
|
||||
validate="url"
|
||||
filter="url"
|
||||
/>
|
||||
<field
|
||||
name="timeout"
|
||||
type="number"
|
||||
label="PLG_TASK_REQUESTS_LABEL_REQUEST_TIMEOUT"
|
||||
min="1"
|
||||
step="1"
|
||||
default="120"
|
||||
required="true"
|
||||
filter="int"
|
||||
validate="number"
|
||||
/>
|
||||
<field
|
||||
name="auth"
|
||||
type="radio"
|
||||
label="PLG_TASK_REQUESTS_LABEL_AUTH"
|
||||
layout="joomla.form.field.radio.switcher"
|
||||
default="0"
|
||||
required="true"
|
||||
filter="integer"
|
||||
>
|
||||
<option value="0">JDISABLED</option>
|
||||
<option value="1">JENABLED</option>
|
||||
</field>
|
||||
<field
|
||||
name="authType"
|
||||
type="list"
|
||||
label="PLG_TASK_REQUESTS_LABEL_AUTH_HEADER"
|
||||
showon="auth:1"
|
||||
>
|
||||
<option value="Bearer">PLG_TASK_REQUESTS_BEARER</option>
|
||||
<option value="X-Joomla-Token">PLG_TASK_REQUESTS_JOOMLA_TOKEN</option>
|
||||
</field>
|
||||
<field
|
||||
name="authKey"
|
||||
type="text"
|
||||
label="PLG_TASK_REQUESTS_LABEL_AUTH_KEY"
|
||||
showon="auth:1"
|
||||
/>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</form>
|
||||
22
plugins/task/requests/requests.xml
Normal file
22
plugins/task/requests/requests.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="task" method="upgrade">
|
||||
<name>plg_task_requests</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2021-08</creationDate>
|
||||
<copyright>(C) 2021 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>4.1</version>
|
||||
<description>PLG_TASK_REQUESTS_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\Task\Requests</namespace>
|
||||
<files>
|
||||
<folder plugin="requests">services</folder>
|
||||
<folder>src</folder>
|
||||
<folder>forms</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_task_requests.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_task_requests.sys.ini</language>
|
||||
</languages>
|
||||
</extension>
|
||||
49
plugins/task/requests/services/provider.php
Normal file
49
plugins/task/requests/services/provider.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Task.requests
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Http\HttpFactory;
|
||||
use Joomla\Plugin\Task\Requests\Extension\Requests;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.2.0
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new Requests(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('task', 'requests'),
|
||||
new HttpFactory(),
|
||||
JPATH_ROOT . '/tmp'
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
169
plugins/task/requests/src/Extension/Requests.php
Normal file
169
plugins/task/requests/src/Extension/Requests.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?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 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;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* 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 = ['Authorization' => $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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user