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
+49
-31
@@ -11,36 +11,53 @@
|
||||
namespace IPS\Api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use IPS\Db;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Helpers\Form\Custom;
|
||||
use IPS\Helpers\Form\Radio;
|
||||
use IPS\Helpers\Form\Stack;
|
||||
use IPS\Helpers\Form\Translatable;
|
||||
use IPS\Lang;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Request;
|
||||
use IPS\Theme;
|
||||
use function defined;
|
||||
use function intval;
|
||||
use function is_array;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* API Key
|
||||
*/
|
||||
class _Key extends \IPS\Node\Model
|
||||
class Key extends Model
|
||||
{
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_api_keys';
|
||||
public static ?string $databaseTable = 'core_api_keys';
|
||||
|
||||
/**
|
||||
* @brief Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 'api_';
|
||||
public static string $databasePrefix = 'api_';
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'api_keys';
|
||||
public static string $nodeTitle = 'api_keys';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
@@ -58,7 +75,7 @@ class _Key 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_',
|
||||
@@ -67,16 +84,16 @@ class _Key extends \IPS\Node\Model
|
||||
/**
|
||||
* @brief [Node] Title prefix. If specified, will look for a language key with "{$key}_title" as the key
|
||||
*/
|
||||
public static $titleLangPrefix = 'core_api_name_';
|
||||
public static ?string $titleLangPrefix = 'core_api_name_';
|
||||
|
||||
/**
|
||||
* Get description
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function get__description()
|
||||
public function get__description(): ?string
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate('api')->apiKey( $this->id );
|
||||
return Theme::i()->getTemplate('api')->apiKey( $this->id );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +104,7 @@ class _Key extends \IPS\Node\Model
|
||||
* @param string $method Method
|
||||
* @return bool
|
||||
*/
|
||||
public function canAccess( $app, $controller, $method )
|
||||
public function canAccess( string $app, string $controller, string $method ) : bool
|
||||
{
|
||||
$permissions = json_decode( $this->permissions, TRUE );
|
||||
return isset( $permissions["{$app}/{$controller}/{$method}"] ) and $permissions["{$app}/{$controller}/{$method}"]['access'] == TRUE;
|
||||
@@ -101,7 +118,7 @@ class _Key extends \IPS\Node\Model
|
||||
* @param string $method Method
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldLog( $app, $controller, $method )
|
||||
public function shouldLog( string $app, string $controller, string $method ) : bool
|
||||
{
|
||||
$permissions = json_decode( $this->permissions, TRUE );
|
||||
return isset( $permissions["{$app}/{$controller}/{$method}"] ) and isset( $permissions["{$app}/{$controller}/{$method}"]['log'] ) and $permissions["{$app}/{$controller}/{$method}"]['log'] == TRUE;
|
||||
@@ -110,25 +127,25 @@ class _Key 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
|
||||
{
|
||||
/* API Key */
|
||||
$form->add( new \IPS\Helpers\Form\Custom( 'api_id', $this->id ?: md5( mt_rand() ), TRUE, array(
|
||||
$form->add( new Custom( 'api_id', $this->id ?: md5( mt_rand() ), TRUE, array(
|
||||
'getHtml' => function( $field )
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate('api')->apiKeyField( $field->name, $field->value );
|
||||
return Theme::i()->getTemplate('api')->apiKeyField( $field->name, $field->value );
|
||||
},
|
||||
'disableCopy' => TRUE
|
||||
) ) );
|
||||
|
||||
/* Description */
|
||||
$form->add( new \IPS\Helpers\Form\Translatable( 'api_name', NULL, TRUE, array( 'app' => 'core', 'key' => ( $this->id ? "core_api_name_{$this->id}" : NULL ) ) ) );
|
||||
$form->add( new Translatable( 'api_name', NULL, TRUE, array( 'app' => 'core', 'key' => ( $this->id ? "core_api_name_{$this->id}" : NULL ) ) ) );
|
||||
|
||||
/* Allowed IPs */
|
||||
$form->add( new \IPS\Helpers\Form\Radio( 'api_enable_ip_restriction', \intval( $this->allowed_ips !== NULL ), FALSE, array(
|
||||
$form->add( new Radio( 'api_enable_ip_restriction', intval( $this->allowed_ips !== NULL ), FALSE, array(
|
||||
'options' => array(
|
||||
0 => 'api_enable_ip_restriction_off',
|
||||
1 => 'api_enable_ip_restriction_on',
|
||||
@@ -138,37 +155,38 @@ class _Key extends \IPS\Node\Model
|
||||
)
|
||||
) ) );
|
||||
|
||||
if ( \IPS\Request::i()->isCgi() )
|
||||
if ( Request::i()->isCgi() )
|
||||
{
|
||||
\IPS\Member::loggedIn()->language()->words['api_enable_ip_restriction_warning'] = \IPS\Member::loggedIn()->language()->addToStack('api_enable_ip_restriction__warning');
|
||||
Member::loggedIn()->language()->words['api_enable_ip_restriction_warning'] = Member::loggedIn()->language()->addToStack('api_enable_ip_restriction__warning');
|
||||
}
|
||||
|
||||
$form->add( new \IPS\Helpers\Form\Stack( 'api_allowed_ips', explode( ',', $this->allowed_ips ), NULL, array(), function( $val )
|
||||
$form->add( new Stack( 'api_allowed_ips', explode( ',', $this->allowed_ips ), NULL, array(), function( $val )
|
||||
{
|
||||
if ( $val )
|
||||
{
|
||||
foreach ( array_filter( \is_array( $val ) ? $val : array( $val ) ) as $ip )
|
||||
foreach ( array_filter( is_array( $val ) ? $val : array( $val ) ) as $ip )
|
||||
{
|
||||
if ( filter_var( $ip, FILTER_VALIDATE_IP ) === FALSE )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'api_allowed_ips_err', FALSE, array( 'sprintf' => array( $ip ) ) ) );
|
||||
throw new DomainException( Member::loggedIn()->language()->addToStack( 'api_allowed_ips_err', FALSE, array( 'sprintf' => array( $ip ) ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}, NULL, NULL, 'api_allowed_ips' ) );
|
||||
|
||||
/* Permissions */
|
||||
$form->add( new \IPS\Helpers\Form\Custom( 'api_permissions', $this->permissions ? json_decode( $this->permissions, TRUE ) : array(), FALSE, array(
|
||||
$form->add( new Custom( 'api_permissions', $this->permissions ? json_decode( $this->permissions, TRUE ) : array(), FALSE, array(
|
||||
'rowHtml' => function( $field )
|
||||
{
|
||||
$endpoints = \IPS\Api\Controller::getAllEndpoints('client');
|
||||
$endpoints = Controller::getAllEndpoints('client');
|
||||
$endpointTree = [];
|
||||
foreach ( $endpoints as $key => $endpoint )
|
||||
{
|
||||
$pieces = explode('/', $key);
|
||||
$endpointTree[ $pieces[0] ][ $pieces[1] ][ $key ] = $endpoint;
|
||||
}
|
||||
|
||||
return \IPS\Theme::i()->getTemplate( 'api' )->permissionsField( $endpointTree, $field->name, $field->value );
|
||||
return Theme::i()->getTemplate( 'api' )->permissionsField( $endpointTree, $field->name, $field->value );
|
||||
}
|
||||
) ) );
|
||||
|
||||
@@ -180,7 +198,7 @@ class _Key extends \IPS\Node\Model
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
public function formatFormValues( array $values ): array
|
||||
{
|
||||
$values['api_permissions'] = json_encode( $values['api_permissions'] );
|
||||
|
||||
@@ -196,7 +214,7 @@ class _Key extends \IPS\Node\Model
|
||||
$this->save();
|
||||
}
|
||||
|
||||
\IPS\Lang::saveCustom( 'core', "core_api_name_{$this->id}", $values['api_name'] );
|
||||
Lang::saveCustom( 'core', "core_api_name_{$this->id}", $values['api_name'] );
|
||||
unset( $values['api_name'] );
|
||||
|
||||
return $values;
|
||||
@@ -221,6 +239,6 @@ class _Key extends \IPS\Node\Model
|
||||
$this->save();
|
||||
|
||||
/* ...and language */
|
||||
\IPS\Lang::saveCustom( 'core', 'core_api_name_' . $this->id, iterator_to_array( \IPS\Db::i()->select( 'word_custom, lang_id', 'core_sys_lang_words', array( 'word_key=?', 'core_api_name_' . $oldId ) )->setKeyField( 'lang_id' )->setValueField( 'word_custom' ) ) );
|
||||
Lang::saveCustom( 'core', 'core_api_name_' . $this->id, iterator_to_array( Db::i()->select( 'word_custom, lang_id', 'core_sys_lang_words', array( 'word_key=?', 'core_api_name_' . $oldId ) )->setKeyField( 'lang_id' )->setValueField( 'word_custom' ) ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user