Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

+58 -33
View File
@@ -11,7 +11,7 @@
namespace IPS\Api;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -50,7 +50,8 @@ abstract class _Controller
/**
* Constructor
*
* @param \IPS\Api\Key $apiKey The API key being used to access, if applicable
* @param \IPS\Api\Key $apiKey The API key being used to access, if applicable
* @param string $accessToken Access token for OAuth-authenticated requests
* @return void
*/
public function __construct( \IPS\Api\Key $apiKey = NULL, $accessToken = NULL )
@@ -122,12 +123,12 @@ abstract class _Controller
*/
public function execute( $pathBits, &$shouldLog )
{
$method = ( isset( $_SERVER['REQUEST_METHOD'] ) and in_array( mb_strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'POST', 'PUT', 'DELETE' ) ) ) ? mb_strtoupper( $_SERVER['REQUEST_METHOD'] ) : 'GET';
$method = ( isset( $_SERVER['REQUEST_METHOD'] ) and \in_array( mb_strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'POST', 'PUT', 'DELETE' ) ) ) ? mb_strtoupper( $_SERVER['REQUEST_METHOD'] ) : 'GET';
$params = array();
try
{
$endpointData = $this->_getEndpoint( $pathBits );
$endpointData = $this->_getEndpoint( $pathBits, $method );
}
catch ( \RuntimeException $e )
{
@@ -136,7 +137,7 @@ abstract class _Controller
if ( method_exists( $this, "{$method}{$endpointData['endpoint']}" ) )
{
preg_match( '/^IPS\\\(.+?)\\\api\\\(.+?)$/', get_called_class(), $matches );
preg_match( '/^IPS\\\(.+?)\\\api\\\(.+?)$/', \get_called_class(), $matches );
$shouldLog = $this->shouldLog( $matches[1], $matches[2], "{$method}{$endpointData['endpoint']}" );
if ( !$this->canAccess( $matches[1], $matches[2], "{$method}{$endpointData['endpoint']}" ) )
@@ -161,7 +162,10 @@ abstract class _Controller
}
}
return call_user_func_array( array( $this, "{$method}{$endpointData['endpoint']}" ), $endpointData['params'] );
$methodName = "{$method}{$endpointData['endpoint']}";
$parameters = $endpointData['params'];
return $this->$methodName( ...$parameters );
}
else
{
@@ -173,34 +177,50 @@ abstract class _Controller
* Get endpoint data
*
* @param array $pathBits The parts to the path called
* @param string $method HTTP method verb
* @return array
* @throws \RuntimeException
*/
protected function _getEndpoint( $pathBits )
protected function _getEndpoint( $pathBits, $method = 'GET' )
{
$endpoint = NULL;
$params = array();
if ( count( $pathBits ) === 0 )
if ( \count( $pathBits ) === 0 )
{
$endpoint = 'index';
}
elseif ( count( $pathBits ) === 1 )
elseif ( \count( $pathBits ) === 1 )
{
$params[] = array_shift( $pathBits );
$endpoint = 'item';
/* Sometimes we want to do something like /api/core/clubs/contenttypes but this is treated as an item */
if( \method_exists( $this, $method . $pathBits[0] ) )
{
$endpoint = array_shift( $pathBits );
}
else
{
$params[] = array_shift( $pathBits );
$endpoint = 'item';
}
}
elseif ( count( $pathBits ) === 2 )
elseif ( \count( $pathBits ) === 2 )
{
$params[] = array_shift( $pathBits );
$endpoint = 'item_' . array_shift( $pathBits );
}
elseif ( count( $pathBits ) === 3 )
elseif ( \count( $pathBits ) === 3 )
{
$params[] = array_shift( $pathBits );
$endpoint = 'item_' . array_shift( $pathBits );
$params[] = array_shift( $pathBits );
}
elseif ( \count( $pathBits ) === 4 )
{
$params[] = array_shift( $pathBits );
$endpoint = 'item_' . array_shift( $pathBits );
$params[] = array_shift( $pathBits );
$endpoint .= '_' . array_shift( $pathBits );
}
else
{
throw new \RuntimeException;
@@ -212,36 +232,40 @@ abstract class _Controller
/**
* Get all endpoints
*
* @param string|null $type If 'client' or 'member', will not include endpoints that are't applicable
* @param string|null $type If 'client' or 'member', will not include endpoints that are't applicable
* @param bool $includeAll Should also disabled applications be returned
* @return array
*/
public static function getAllEndpoints( $type = NULL )
public static function getAllEndpoints( $type = NULL, $includeAll = FALSE )
{
$return = array();
foreach ( \IPS\Application::applications() as $app )
{
$apiDir = \IPS\ROOT_PATH . '/applications/' . $app->directory . '/api';
if ( file_exists( $apiDir ) )
if ( $app->enabled OR $includeAll )
{
$directory = new \DirectoryIterator( $apiDir );
foreach ( $directory as $file )
$apiDir = \IPS\ROOT_PATH . '/applications/' . $app->directory . '/api';
if ( file_exists( $apiDir ) )
{
if ( !$file->isDot() and mb_substr( $file, 0, 1 ) != '.' )
$directory = new \DirectoryIterator( $apiDir );
foreach ( $directory as $file )
{
$controllerName = mb_substr( $file, 0, -4 );
$class = 'IPS\\' . $app->directory . '\\api\\' . $controllerName;
if( class_exists( $class ) )
if ( !$file->isDot() and mb_substr( $file, 0, 1 ) != '.' )
{
$reflection = new \ReflectionClass( $class );
foreach ( $reflection->getMethods() as $method )
$controllerName = mb_substr( $file, 0, -4 );
$class = 'IPS\\' . $app->directory . '\\api\\' . $controllerName;
if( class_exists( $class ) )
{
if ( $method->getName() != 'execute' and !$method->isStatic() and $method->isPublic() and mb_substr( $method->getName(), 0, 1 ) != '_' )
$reflection = new \ReflectionClass( $class );
foreach ( $reflection->getMethods() as $method )
{
$details = static::decodeDocblock( $method->getDocComment() );
if ( ( $type !== 'client' or !isset( $details['details']['apimemberonly'] ) ) and ( $type !== 'member' or !isset( $details['details']['apiclientonly'] ) ) )
if ( $method->getName() != 'execute' and !$method->isStatic() and $method->isPublic() and mb_substr( $method->getName(), 0, 1 ) != '_' )
{
$return[ $app->directory . '/' . $controllerName . '/' . $method->getName() ] = $details;
$details = static::decodeDocblock( $method->getDocComment() );
if ( ( $type !== 'client' or !isset( $details['details']['apimemberonly'] ) ) and ( $type !== 'member' or !isset( $details['details']['apiclientonly'] ) ) )
{
$return[ $app->directory . '/' . $controllerName . '/' . $method->getName() ] = $details;
}
}
}
}
@@ -277,7 +301,7 @@ abstract class _Controller
$params = array();
while ( $nextLine = array_shift( $comment ) )
{
if ( preg_match( '/^\s*\*\s*@([a-z]*)(\t+([^\t]*))?(\t+([^\t]*))?(\t+([^\t]*))?$/', $nextLine, $matches ) )
if ( preg_match( '/^\s*\*\s*@([a-z]*)(\t+([^\t]*))?(\t+([^\t]*))?(\t+([^\t]*))?$/', trim( $nextLine ), $matches ) )
{
$details = array();
foreach ( array( 3, 5, 7 ) as $k )
@@ -311,7 +335,8 @@ abstract class _Controller
*
* @param string $endpoint The endpoint (e.g: GET /core/members)
* @param string $size Size of badge to show
* @return array
* @param bool $includeBaseUrl Whether or not to include the URL base in the endpoint URL
* @return string
*/
public static function parseEndpointForDisplay( $endpoint, $size='small', $includeBaseUrl=FALSE )
{
@@ -323,7 +348,7 @@ abstract class _Controller
);
$pieces = explode( ' ', $endpoint );
if ( !in_array( $pieces[0], array_keys( $badgeStyles ) ) )
if ( !\in_array( $pieces[0], array_keys( $badgeStyles ) ) )
{
\IPS\Output::i()->error( \IPS\Member::loggedIn()->language()->addToStack( 'api_endpoint_phpdoc_error', FALSE, array( "sprintf" => array( $endpoint ) ) ), '3S291/4', 500 );
}