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
@@ -12,21 +12,37 @@
|
||||
namespace IPS\cms\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use InvalidArgumentException;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\cms\Databases;
|
||||
use IPS\cms\Records\Comment;
|
||||
use IPS\Content\Api\CommentController;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use OutOfRangeException;
|
||||
use RuntimeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function intval;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Pages Database Comments API
|
||||
*/
|
||||
class _comments extends \IPS\Content\Api\CommentController
|
||||
class comments extends CommentController
|
||||
{
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = NULL;
|
||||
protected string $class = '';
|
||||
|
||||
/**
|
||||
* Get endpoint data
|
||||
@@ -34,25 +50,25 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @param array $pathBits The parts to the path called
|
||||
* @param string $method HTTP method verb
|
||||
* @return array
|
||||
* @throws \RuntimeException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function _getEndpoint( $pathBits, $method = 'GET' )
|
||||
protected function _getEndpoint( array $pathBits, string $method = 'GET' ): array
|
||||
{
|
||||
if ( !\count( $pathBits ) )
|
||||
if ( !count( $pathBits ) )
|
||||
{
|
||||
throw new \RuntimeException;
|
||||
throw new RuntimeException;
|
||||
}
|
||||
|
||||
$database = array_shift( $pathBits );
|
||||
if ( !\count( $pathBits ) )
|
||||
if ( !count( $pathBits ) )
|
||||
{
|
||||
return array( 'endpoint' => 'index', 'params' => array( $database ) );
|
||||
}
|
||||
|
||||
$nextBit = array_shift( $pathBits );
|
||||
if ( \intval( $nextBit ) != 0 )
|
||||
if ( intval( $nextBit ) != 0 )
|
||||
{
|
||||
if ( \count( $pathBits ) )
|
||||
if ( count( $pathBits ) )
|
||||
{
|
||||
return array( 'endpoint' => 'item_' . array_shift( $pathBits ), 'params' => array( $database, $nextBit ) );
|
||||
}
|
||||
@@ -62,7 +78,7 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException;
|
||||
throw new RuntimeException;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,27 +97,28 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @apiparam int page Page number
|
||||
* @apiparam int perPage Number of results per page - defaults to 25
|
||||
* @throws 2T311/1 INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\Api\PaginatedResponse<IPS\cms\Records\Comment>
|
||||
* @apireturn PaginatedResponse<IPS\cms\Records\Comment>
|
||||
* @return PaginatedResponse<Comment>
|
||||
*/
|
||||
public function GETindex( $database )
|
||||
public function GETindex( int $database ): PaginatedResponse
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/1', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/1', 404 );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return $this->_list( array( array( 'comment_database_id=?', $database->id ) ), 'categories' );
|
||||
return $this->_list( array( array( 'comment_database_id=?', $database->id ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,27 +129,29 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @param int $comment Comment ID
|
||||
* @throws 2T311/1 INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @throws 2T311/3 INVALID_ID The comment ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $database, $comment )
|
||||
public function GETitem( int $database,int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/2', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/2', 404 );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
try
|
||||
{
|
||||
/* @var Comment $class */
|
||||
$class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
if ( $this->member )
|
||||
{
|
||||
@@ -143,11 +162,11 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
$object = $class::load( $comment );
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2T311/3', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T311/3', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,44 +189,36 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @throws 1T311/6 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1T311/7 NO_CONTENT No content was supplied
|
||||
* @throws 2T311/D NO_PERMISSION The authorized user does not have permission to comment on that record
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex( $database )
|
||||
public function POSTindex( int $database ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/4', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/4', 404 );
|
||||
}
|
||||
|
||||
/* Get record */
|
||||
try
|
||||
{
|
||||
/* @var \IPS\cms\Records $recordClass */
|
||||
$recordClass = 'IPS\cms\Records' . $database->id;
|
||||
/** @var \IPS\cms\Records $record */
|
||||
$record = $recordClass::load( \IPS\Request::i()->record );
|
||||
$record = $recordClass::load( Request::i()->record );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2T311/5', 403 );
|
||||
}
|
||||
|
||||
if( $record->useForumComments() )
|
||||
{
|
||||
$this->class = 'IPS\cms\Records\CommentTopicSync' . $database->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
throw new Exception( 'INVALID_ID', '2T311/5', 403 );
|
||||
}
|
||||
|
||||
/* Get author */
|
||||
@@ -215,38 +226,38 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
if ( !$record->canComment( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T311/D', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T311/D', 403 );
|
||||
}
|
||||
$author = $this->member;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( \IPS\Request::i()->author )
|
||||
if ( Request::i()->author )
|
||||
{
|
||||
$author = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$author = Member::load( Request::i()->author );
|
||||
if ( !$author->member_id )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1T311/6', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T311/6', 404 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( (int) \IPS\Request::i()->author === 0 )
|
||||
if ( Request::i()->author === 0 )
|
||||
{
|
||||
$author = new \IPS\Member;
|
||||
$author->name = \IPS\Request::i()->author_name;
|
||||
$author = new Member;
|
||||
$author->name = Request::i()->author_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1T311/6', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T311/6', 400 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check we have a post */
|
||||
if ( !\IPS\Request::i()->content )
|
||||
if ( !Request::i()->content )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_CONTENT', '1T311/7', 403 );
|
||||
throw new Exception( 'NO_CONTENT', '1T311/7', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -269,38 +280,40 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @throws 2T311/8 INVALID_ID The comment ID does not exist or the authorized user does not have permission to view it
|
||||
* @throws 1T311/9 NO_AUTHOR The author ID does not exist
|
||||
* @throws 2T311/E NO_PERMISSION The authorized user does not have permission to edit the comment
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $database, $comment )
|
||||
public function POSTitem( int $database, int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/7', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/7', 404 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
try
|
||||
{
|
||||
/* Load */
|
||||
/* @var Comment $className */
|
||||
$className = $this->class;
|
||||
$comment = $className::load( $comment );
|
||||
if ( $this->member and !$comment->canView( $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
if ( $this->member and !$comment->canEdit( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T311/E', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T311/E', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -308,14 +321,14 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
return $this->_edit( $comment );
|
||||
}
|
||||
catch ( \InvalidArgumentException $e )
|
||||
catch ( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1T311/9', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T311/9', 400 );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2T311/8', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T311/8', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,41 +341,43 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @throws 2T311/A INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @throws 2T311/B INVALID_ID The comment ID does not exist
|
||||
* @throws 2T311/F NO_PERMISSION The authorized user does not have permission to delete the comment
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $database, $comment )
|
||||
public function DELETEitem( int $database, int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/A', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/A', 404 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
try
|
||||
{
|
||||
{
|
||||
/* @var Comment $class */
|
||||
$class = $this->class;
|
||||
$object = $class::load( $comment );
|
||||
if ( $this->member and !$object->canDelete( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T311/F', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T311/F', 403 );
|
||||
}
|
||||
$object->delete();
|
||||
|
||||
return new \IPS\Api\Response( 200, NULL );
|
||||
return new Response( 200, NULL );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2T311/B', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T311/B', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,29 +389,30 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @param int $comment Comment ID
|
||||
* @apiparam int id ID of the reaction to add
|
||||
* @apiparam int author ID of the member reacting
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @throws 1S425/2 NO_REACTION The reaction ID does not exist
|
||||
* @throws 1S425/3 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/4 REACT_ERROR Error adding the reaction
|
||||
* @throws 1S425/5 INVALID_ID Object ID does not exist
|
||||
* @throws 2T311/C INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @note If the author has already reacted to this content, any existing reaction will be removed first
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem_react( $database, $comment )
|
||||
public function POSTitem_react( int $database, int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/C', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/C', 404 );
|
||||
}
|
||||
|
||||
return $this->_reactAdd( $comment );
|
||||
@@ -409,28 +425,29 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @param int $database Database ID
|
||||
* @param int $comment Comment ID
|
||||
* @apiparam int author ID of the member who reacted
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @throws 1S425/6 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/7 REACT_ERROR Error adding the reaction
|
||||
* @throws 1S425/8 INVALID_ID Object ID does not exist
|
||||
* @throws 2T311/D INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @note If the author has already reacted to this content, any existing reaction will be removed first
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem_react( $database, $comment )
|
||||
public function DELETEitem_react( int $database, int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/D', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/D', 404 );
|
||||
}
|
||||
|
||||
return $this->_reactRemove( $comment );
|
||||
@@ -447,23 +464,24 @@ class _comments extends \IPS\Content\Api\CommentController
|
||||
* @apiparam string message Optional message
|
||||
* @throws 1S425/B NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/C REPORTED_ALREADY The member has reported this item in the past 24 hours
|
||||
* @return \IPS\cms\Records\Comment
|
||||
* @apireturn \IPS\cms\Records\Comment
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem_report( $database, $comment )
|
||||
public function POSTitem_report( int $database, int $comment ): Response
|
||||
{
|
||||
/* Load database */
|
||||
try
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $database );
|
||||
$database = Databases::load( $database );
|
||||
if ( $this->member and !$database->can( 'view', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$this->class = 'IPS\cms\Records\Comment' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T311/D', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T311/D', 404 );
|
||||
}
|
||||
|
||||
return $this->_report( $comment );
|
||||
|
||||
Reference in new issue
Block a user