Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,16 +11,29 @@
|
||||
namespace IPS\core\tasks;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use Exception;
|
||||
use IPS\Application;
|
||||
use IPS\Db;
|
||||
use IPS\Http\Url;
|
||||
use IPS\IPS;
|
||||
use IPS\Output;
|
||||
use IPS\Settings;
|
||||
use IPS\Task;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* webhooks Task
|
||||
*/
|
||||
class _webhooks extends \IPS\Task
|
||||
class webhooks extends Task
|
||||
{
|
||||
/**
|
||||
* Execute
|
||||
@@ -31,36 +44,35 @@ class _webhooks extends \IPS\Task
|
||||
* Tasks should execute within the time of a normal HTTP request.
|
||||
*
|
||||
* @return mixed Message to log or NULL
|
||||
* @throws \IPS\Task\Exception
|
||||
* @throws Task\Exception
|
||||
*/
|
||||
public function execute()
|
||||
public function execute() : mixed
|
||||
{
|
||||
/* Get our webhooks */
|
||||
$webhooks = iterator_to_array( \IPS\Db::i()->select( '*', 'core_api_webhooks', array('enabled=1') )->setKeyField('id') );
|
||||
$webhooks = iterator_to_array( Db::i()->select( '*', 'core_api_webhooks', array('enabled=1') )->setKeyField('id') );
|
||||
|
||||
/* Run through the queue */
|
||||
$this->runUntilTimeout( function() use ( $webhooks ) {
|
||||
/* Try and get a queue item */
|
||||
try
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER race condition in tasks */
|
||||
$fire = \IPS\Db::i()->select( '*', 'core_api_webhook_fires', array( "status='pending'" ), '`time` ASC', 1, NULL, NULL, \IPS\Db::SELECT_FROM_WRITE_SERVER )->first();
|
||||
$fire = Db::i()->select( '*', 'core_api_webhook_fires', array( "status='pending'" ), '`time` ASC', 1, NULL, NULL, Db::SELECT_FROM_WRITE_SERVER )->first();
|
||||
|
||||
try
|
||||
{
|
||||
if ( !isset( $webhooks[ $fire['webhook'] ] ) )
|
||||
{
|
||||
throw new \DomainException("Webhook Disabled");
|
||||
throw new DomainException("Webhook Disabled");
|
||||
}
|
||||
|
||||
\IPS\Output::i()->parseFileObjectUrls( $fire['data'] );
|
||||
$fire['data'] = \IPS\Output::i()->replaceEmojiWithImages( $fire['data'] );
|
||||
|
||||
$request = \IPS\Http\Url::external( $webhooks[ $fire['webhook'] ]['url'] )
|
||||
Output::i()->parseFileObjectUrls( $fire['data'] );
|
||||
$fire['data'] = Output::i()->replaceEmojiWithImages( $fire['data'] );
|
||||
|
||||
$request = Url::external( $webhooks[ $fire['webhook'] ]['url'] )
|
||||
->request()
|
||||
->setHeaders(
|
||||
[
|
||||
'User-Agent' => "InvisionCommunity/" . \IPS\Application::load('core')->long_version ,
|
||||
'User-Agent' => "InvisionCommunity/" . Application::load('core')->long_version ,
|
||||
'Webhook-Event' => $fire['event'],
|
||||
'Content-Type' => $webhooks[ $fire['webhook'] ]['content_type']
|
||||
]
|
||||
@@ -73,9 +85,9 @@ class _webhooks extends \IPS\Task
|
||||
|
||||
$response = $request->post( $fire['data'] );
|
||||
|
||||
if ( \IPS\Settings::i()->webhook_logs_success )
|
||||
if ( Settings::i()->webhook_logs_success )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_api_webhook_fires', [
|
||||
Db::i()->update( 'core_api_webhook_fires', [
|
||||
'status' => 'successful',
|
||||
'response_code' => $response->httpResponseCode,
|
||||
'response_body' => $response->content
|
||||
@@ -83,41 +95,43 @@ class _webhooks extends \IPS\Task
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_api_webhook_fires', [ 'id=?', $fire['id'] ] );
|
||||
Db::i()->delete( 'core_api_webhook_fires', [ 'id=?', $fire['id'] ] );
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
$update = [
|
||||
'response_code' => NULL,
|
||||
'response_body' => \IPS\IPS::getExceptionDetails( $e ),
|
||||
'response_body' => IPS::getExceptionDetails( $e ),
|
||||
'fails' => $fire['fails'] + 1,
|
||||
];
|
||||
if ( $fire['fails'] >= ( \IPS\Settings::i()->webhooks_allowed_fails - 1 ) )
|
||||
if ( $fire['fails'] >= ( Settings::i()->webhooks_allowed_fails - 1 ) )
|
||||
{
|
||||
$update['status'] = 'failed';
|
||||
}
|
||||
|
||||
if ( isset( $update['status'] ) and $update['status'] === 'failed' and !\IPS\Settings::i()->webhook_logs_fail )
|
||||
if ( isset( $update['status'] ) and $update['status'] === 'failed' and !Settings::i()->webhook_logs_fail )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_api_webhook_fires', [ 'id=?', $fire['id'] ] );
|
||||
Db::i()->delete( 'core_api_webhook_fires', [ 'id=?', $fire['id'] ] );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_api_webhook_fires', $update, [ 'id=?', $fire['id'] ] );
|
||||
Db::i()->update( 'core_api_webhook_fires', $update, [ 'id=?', $fire['id'] ] );
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
/* If there's no queue items left, disable this task and return */
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
$this->enabled = FALSE;
|
||||
$this->save();
|
||||
return FALSE;
|
||||
}
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user