Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+80 -60
View File
@@ -11,42 +11,62 @@
namespace IPS\Api;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Application;
use IPS\core\Form\WebhookSelector;
use IPS\Data\Store;
use IPS\Db;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Select;
use IPS\Helpers\Form\Text;
use IPS\Helpers\Form\Url as FormUrl;
use IPS\Http\Url;
use IPS\Log;
use IPS\Member;
use IPS\Node\Model;
use IPS\Theme;
use function count;
use function defined;
use function in_array;
use function is_array;
use function is_object;
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;
}
/**
* Webhook
*/
class _Webhook extends \IPS\Node\Model
class Webhook extends Model
{
/**
* @brief [ActiveRecord] Multiton Store
*/
protected static $multitons;
protected static array $multitons;
/**
* @brief [ActiveRecord] Database Table
*/
public static $databaseTable = 'core_api_webhooks';
public static ?string $databaseTable = 'core_api_webhooks';
/**
* @brief cache for get_url()
*/
protected $_url;
protected mixed $_url = NULL;
/**
* @brief [ActiveRecord] Caches
* @note Defined cache keys will be cleared automatically as needed
*/
protected $caches = array('webhooks');
protected array $caches = array('webhooks');
/**
* @brief [Node] Node Title
*/
public static $nodeTitle = 'api_webhooks';
public static string $nodeTitle = 'api_webhooks';
/**
* @brief [Node] ACP Restrictions
@@ -64,7 +84,7 @@ class _Webhook extends \IPS\Node\Model
'prefix' => 'foo_', // [Optional] Rather than specifying each key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
* @endcode
*/
protected static $restrictions = array(
protected static ?array $restrictions = array(
'app' => 'core',
'module' => 'applications',
'prefix' => 'api_',
@@ -75,7 +95,7 @@ class _Webhook extends \IPS\Node\Model
*
* @return void
*/
public function setDefaultValues()
public function setDefaultValues() : void
{
$this->content_type = 'application/x-www-form-urlencoded';
$this->_data['filters'] = '{}';
@@ -86,14 +106,14 @@ class _Webhook extends \IPS\Node\Model
/**
* [Node] Add/Edit Form
*
* @param \IPS\Helpers\Form $form The form
* @param Form $form The form
* @return void
*/
public function form( &$form )
public function form( Form &$form ) : void
{
$form->add( new \IPS\Helpers\Form\Url( 'url', $this->url ?? "", TRUE ) );
$form->add( new FormUrl( 'url', $this->url ?? "", TRUE ) );
if( \in_array($this->content_type, ['application/x-www-form-urlencoded', 'application/json']))
if( in_array($this->content_type, ['application/x-www-form-urlencoded', 'application/json']))
{
$selectedHeader = $this->content_type;
$customHeader = '';
@@ -103,13 +123,13 @@ class _Webhook extends \IPS\Node\Model
$selectedHeader = 'custom';
$customHeader = $this->content_type;
}
$form->add( new \IPS\Helpers\Form\Select('webhook_content_type', $selectedHeader, TRUE , ['options' => ['application/x-www-form-urlencoded' => 'x-www-form-urlencoded', 'application/json' => 'application/json', 'custom' => 'Other'] , 'toggles'=> ['custom' => ['webhook_content_type_other'] ] ] ) );
$form->add( new \IPS\Helpers\Form\Text('webhook_content_type_other', $customHeader, FALSE , [], NULL, NULL, NULL, 'webhook_content_type_other' ) );
$form->add( new Select('webhook_content_type', $selectedHeader, TRUE , ['options' => ['application/x-www-form-urlencoded' => 'x-www-form-urlencoded', 'application/json' => 'application/json', 'custom' => 'Other'] , 'toggles'=> ['custom' => ['webhook_content_type_other'] ] ] ) );
$form->add( new Text('webhook_content_type_other', $customHeader, FALSE , [], NULL, NULL, NULL, 'webhook_content_type_other' ) );
$events = [];
foreach ( \IPS\Api\Webhook::getAvailableWebhooks() as $app => $hooks )
foreach (Webhook::getAvailableWebhooks() as $app => $hooks )
{
if( \count( $hooks ))
if( count( $hooks ))
{
foreach( $hooks as $hook => $params)
{
@@ -118,7 +138,7 @@ class _Webhook extends \IPS\Node\Model
}
}
$form->add( new \IPS\core\Form\WebhookSelector('webhook_events', $this->events ?? [], TRUE , [ 'options' => $events] ) );
$form->add( new WebhookSelector('webhook_events', $this->events ?? [], TRUE , [ 'options' => $events] ) );
}
/**
@@ -127,7 +147,7 @@ class _Webhook extends \IPS\Node\Model
* @param array $values Values from the form
* @return array
*/
public function formatFormValues( $values )
public function formatFormValues( array $values ): array
{
$values['events'] = $values['webhook_events'];
@@ -145,7 +165,7 @@ class _Webhook extends \IPS\Node\Model
return $values;
}
public function get__title()
public function get__title(): string
{
return (string) $this->url;
}
@@ -155,9 +175,9 @@ class _Webhook extends \IPS\Node\Model
*
* @return string|null
*/
protected function get__description()
protected function get__description(): ?string
{
return \IPS\Theme::i()->getTemplate( 'api', 'core' )->webhookDesc( $this );
return Theme::i()->getTemplate( 'api', 'core' )->webhookDesc( $this );
}
/**
@@ -168,16 +188,16 @@ class _Webhook extends \IPS\Node\Model
* @param array $filters Filters
* @return void
*/
public static function fire( $event, $data = NULL, $filters = array() )
public static function fire( string $event, mixed $data = NULL, array $filters = array() ) : void
{
/* This will intentionally not call $data::apiOutput to avoid unnecessary overhead here */
\IPS\Log::debug( print_r( array_merge(['event' => $event], ['payload' => $data], ['filters' => $filters ]) , TRUE ), 'webhook_fire_call');
Log::debug( print_r( array_merge(['event' => $event], ['payload' => $data], ['filters' => $filters ]) , TRUE ), 'webhook_fire_call');
/* Get our webhooks from cache */
if ( !isset( \IPS\Data\Store::i()->webhooks ) )
if ( !isset( Store::i()->webhooks ) )
{
$webhooks = array();
foreach ( \IPS\Db::i()->select( '*', 'core_api_webhooks', array('enabled=1') ) as $row )
foreach ( Db::i()->select( '*', 'core_api_webhooks', array('enabled=1') ) as $row )
{
foreach ( explode( ',', $row['events'] ) as $e )
{
@@ -194,24 +214,24 @@ class _Webhook extends \IPS\Node\Model
);
}
}
\IPS\Data\Store::i()->webhooks = $webhooks;
Store::i()->webhooks = $webhooks;
}
/* If we have webhooks for this event... */
$enable = FALSE;
if ( isset( \IPS\Data\Store::i()->webhooks[ $event ] ) )
if ( isset( Store::i()->webhooks[ $event ] ) )
{
/* Normalise data */
if ( \is_object( $data ) and method_exists( $data, 'apiOutput' ) )
if ( is_object( $data ) and method_exists( $data, 'apiOutput' ) )
{
$data = $data->apiOutput();
}
else if ( \is_array( $data ) )
else if ( is_array( $data ) )
{
foreach ( $data as $key => &$item )
{
/* Normalise data */
if ( \is_object( $item ) and method_exists( $item, 'apiOutput' ) )
if ( is_object( $item ) and method_exists( $item, 'apiOutput' ) )
{
$item = $item->apiOutput();
}
@@ -219,12 +239,12 @@ class _Webhook extends \IPS\Node\Model
}
/* We need to replace and langstring hashes ( like node names) */
\IPS\Member::loggedIn()->language()->parseOutputForDisplay( $data );
Member::loggedIn()->language()->parseOutputForDisplay( $data );
$data = $data ? json_encode( $data ) : NULL;
/* Loop through each one... */
foreach ( \IPS\Data\Store::i()->webhooks[ $event ] as $id => $webhook )
foreach ( Store::i()->webhooks[ $event ] as $id => $webhook )
{
/* Skip over it if the filters don't match */
if ( isset( $webhook['filters'][ $event ] ) )
@@ -233,9 +253,9 @@ class _Webhook extends \IPS\Node\Model
{
if ( isset( $filters[ $k ] ) )
{
if ( \is_array( $v ) and !\is_array( $filters[ $k ] ) )
if ( is_array( $v ) and !is_array( $filters[ $k ] ) )
{
if ( !\in_array( $filters[ $k ], $v ) )
if ( !in_array( $filters[ $k ], $v ) )
{
continue 2;
}
@@ -253,7 +273,7 @@ class _Webhook extends \IPS\Node\Model
/* Insert it */
$enable = TRUE;
\IPS\Db::i()->insert( 'core_api_webhook_fires', [
Db::i()->insert( 'core_api_webhook_fires', [
'webhook' => $id,
'event' => $event,
'data' => $data,
@@ -266,27 +286,27 @@ class _Webhook extends \IPS\Node\Model
/* Make sure the task is enabled */
if ( $enable )
{
\IPS\Db::i()->update( 'core_tasks', array( 'enabled' => 1 ), array( '`key`=?', 'webhooks' ) );
Db::i()->update( 'core_tasks', array( 'enabled' => 1 ), array( '`key`=?', 'webhooks' ) );
}
}
/**
* Get API Key
*
* @return \IPS\Api\Key
* @return Key
*/
public function get_api_key()
public function get_api_key() : Key
{
return \IPS\Api\Key::load( $this->_data['api_key'] );
return Key::load( $this->_data['api_key'] );
}
/**
* Set API Key
*
* @param \IPS\Api\Key $key The API Key
* @param Key $key The API Key
* @return void
*/
public function set_api_key( \IPS\Api\Key $key )
public function set_api_key(Key $key ) : void
{
$this->_data['api_key'] = $key->id;
}
@@ -296,7 +316,7 @@ class _Webhook extends \IPS\Node\Model
*
* @return array
*/
public function get_events()
public function get_events() : array
{
return explode( ',', $this->_data['events'] );
}
@@ -307,9 +327,9 @@ class _Webhook extends \IPS\Node\Model
* @param string|array $events List of events
* @return void
*/
public function set_events( $events )
public function set_events( string|array $events ) : void
{
if( !\is_array( $events ) )
if( !is_array( $events ) )
{
$events = [$events];
}
@@ -319,20 +339,20 @@ class _Webhook extends \IPS\Node\Model
/**
* Get URL
*
* @return
* @return Url|string
*/
public function get_url()
public function get_url() : Url|string
{
return $this->_data['url'] ? new \IPS\Http\Url( $this->_data['url'] ) : '';
return $this->_data['url'] ? new Url( $this->_data['url'] ) : '';
}
/**
* Set Url
*
* @param \IPS\Http\Url $url The URL
* @param Url $url The URL
* @return void
*/
public function set_url( \IPS\Http\Url $url )
public function set_url( Url $url ) : void
{
$this->_url = $url;
$this->_data['url'] = (string) $this->_url;
@@ -343,7 +363,7 @@ class _Webhook extends \IPS\Node\Model
*
* @return array
*/
public function get_filters()
public function get_filters() : array
{
return $this->_data['filters'] ? json_decode( $this->_data['filters'], TRUE ) : array();
}
@@ -354,7 +374,7 @@ class _Webhook extends \IPS\Node\Model
* @param array $filters Filter
* @return void
*/
public function set_filters( array $filters )
public function set_filters( array $filters ) : void
{
$this->_data['filters'] = json_encode( $filters );
}
@@ -362,13 +382,13 @@ class _Webhook extends \IPS\Node\Model
/**
* Get output for API
*
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
* @return array
* @apiresponse int id ID number
* @apiresponse array events List of events this hook is subscribed to
* @apiresponse string url URL to send webhook to
*/
public function apiOutput( \IPS\Member $authorizedMember = NULL )
public function apiOutput( Member $authorizedMember = NULL ): array
{
return array(
'id' => $this->id,
@@ -377,10 +397,10 @@ class _Webhook extends \IPS\Node\Model
);
}
public static function getAvailableWebhooks()
public static function getAvailableWebhooks() : array
{
$hooks = [];
foreach ( \IPS\Application::applications() as $app )
foreach ( Application::applications() as $app )
{
if( $app->enabled )
{
@@ -400,7 +420,7 @@ class _Webhook extends \IPS\Node\Model
* @param mixed $where Where clause
* @return array
*/
public static function search( $column, $query, $order=NULL, $where=array() )
public static function search( string $column, string $query, string $order=NULL, mixed $where=array() ): array
{
if ( $column === '_title' )
{
@@ -420,9 +440,9 @@ class _Webhook extends \IPS\Node\Model
*
* @return void
*/
public function delete()
public function delete(): void
{
\IPS\Db::i()->delete( 'core_api_webhook_fires', array( 'webhook=?', $this->id ) );
Db::i()->delete( 'core_api_webhook_fires', array( 'webhook=?', $this->id ) );
parent::delete();
}
}