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,40 @@
|
||||
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;
|
||||
use IPS\cms\Records\Review;
|
||||
use IPS\Content\Api\CommentController;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use OutOfRangeException;
|
||||
use RuntimeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
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 Reviews API
|
||||
*/
|
||||
class _reviews extends \IPS\Content\Api\CommentController
|
||||
class reviews extends CommentController
|
||||
{
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = NULL;
|
||||
protected string $class = '';
|
||||
|
||||
/**
|
||||
* Get endpoint data
|
||||
@@ -34,25 +53,25 @@ class _reviews 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 +81,7 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException;
|
||||
throw new RuntimeException;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,27 +100,28 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @apiparam int page Page number
|
||||
* @apiparam int perPage Number of results per page - defaults to 25
|
||||
* @throws 2T312/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\Review>
|
||||
* @apireturn PaginatedResponse<IPS\cms\Records\Review>
|
||||
* @return PaginatedResponse<Review>
|
||||
*/
|
||||
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\Review' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T312/1', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/1', 404 );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return $this->_list( array( array( 'review_database_id=?', $database->id ) ), 'categories' );
|
||||
return $this->_list( array( array( 'review_database_id=?', $database->id ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,27 +132,29 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @param int $review Comment ID
|
||||
* @throws 2T312/2 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\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $database, $review )
|
||||
public function GETitem( int $database, int $review ): 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', '2T312/2', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/2', 404 );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
try
|
||||
{
|
||||
/* @var Review $class */
|
||||
$class = 'IPS\cms\Records\Review' . $database->id;
|
||||
if ( $this->member )
|
||||
{
|
||||
@@ -143,11 +165,11 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
$object = $class::load( $review );
|
||||
}
|
||||
|
||||
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', '2T312/3', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T312/3', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,34 +194,36 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @throws 1T312/7 NO_CONTENT No content was supplied
|
||||
* @throws 1T312/8 INVALID_RATING The rating is not a valid number up to the maximum rating
|
||||
* @throws 2T312/E NO_PERMISSION The authorized user does not have permission to review that record
|
||||
* @return \IPS\cms\Records\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @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\Review' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T312/4', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/4', 404 );
|
||||
}
|
||||
|
||||
/* Get record */
|
||||
try
|
||||
{
|
||||
/* @var Records $recordClass */
|
||||
$recordClass = 'IPS\cms\Records' . $database->id;
|
||||
$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', '2T312/5', 403 );
|
||||
throw new Exception( 'INVALID_ID', '2T312/5', 403 );
|
||||
}
|
||||
|
||||
/* Get author */
|
||||
@@ -207,44 +231,44 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
if ( !$record->canReview( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T312/E', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T312/E', 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', '1T312/6', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T312/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', '1T312/6', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T312/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 );
|
||||
}
|
||||
|
||||
/* Check we have a rating */
|
||||
if ( !\IPS\Request::i()->rating or !\in_array( (int) \IPS\Request::i()->rating, range( 1, \IPS\Settings::i()->reviews_rating_out_of ) ) )
|
||||
if ( !Request::i()->rating or !in_array( (int) Request::i()->rating, range( 1, Settings::i()->reviews_rating_out_of ) ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_RATING', '1T312/8', 403 );
|
||||
throw new Exception( 'INVALID_RATING', '1T312/8', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -268,38 +292,40 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @throws 2T312/A INVALID_ID The comment ID does not exist or the authorized user does not have permission to view it
|
||||
* @throws 1T312/B NO_AUTHOR The author ID does not exist
|
||||
* @throws 2T312/F NO_PERMISSION The authorized user does not have permission to edit the review
|
||||
* @return \IPS\cms\Records\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $database, $review )
|
||||
public function POSTitem( int $database, int $review ): 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\Review' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T312/9', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/9', 404 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
try
|
||||
{
|
||||
/* Load */
|
||||
/* @var Review $className */
|
||||
$className = $this->class;
|
||||
$review = $className::load( $review );
|
||||
if ( $this->member and !$review->canView( $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
if ( $this->member and !$review->canEdit( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T312/F', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T312/F', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -307,14 +333,14 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
return $this->_edit( $review );
|
||||
}
|
||||
catch ( \InvalidArgumentException $e )
|
||||
catch ( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1T312/B', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1T312/B', 400 );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2T312/A', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T312/A', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,41 +353,43 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @throws 2T312/C INVALID_DATABASE The database ID does not exist or the authorized user does not have permission to view it
|
||||
* @throws 2T312/D INVALID_ID The comment ID does not exist
|
||||
* @throws 2T312/G NO_PERMISSION The authorized user does not have permission to delete the review
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $database, $review )
|
||||
public function DELETEitem( int $database, int $review ): 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\Review' . $database->id;
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_DATABASE', '2T312/C', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/C', 404 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
try
|
||||
{
|
||||
{
|
||||
/* @var Review $class */
|
||||
$class = $this->class;
|
||||
$object = $class::load( $review );
|
||||
if ( $this->member and !$object->canDelete( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2T312/G', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2T312/G', 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', '2T312/D', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2T312/D', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,35 +398,36 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* Add a reaction
|
||||
*
|
||||
* @param int $database Database ID
|
||||
* @param int $comment Comment ID
|
||||
* @param int $review Review ID
|
||||
* @apiparam int id ID of the reaction to add
|
||||
* @apiparam int author ID of the member reacting
|
||||
* @return \IPS\cms\Records\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @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 2T312/E 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 $review ): 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', '2T312/E', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/E', 404 );
|
||||
}
|
||||
|
||||
return $this->_reactAdd( $comment );
|
||||
return $this->_reactAdd( $review );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -406,33 +435,34 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* Delete a reaction
|
||||
*
|
||||
* @param int $database Database ID
|
||||
* @param int $comment Comment ID
|
||||
* @param int $review Review ID
|
||||
* @apiparam int author ID of the member who reacted
|
||||
* @return \IPS\cms\Records\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @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 2T312/F 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 $review ): 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', '2T312/F', 404 );
|
||||
throw new Exception( 'INVALID_DATABASE', '2T312/F', 404 );
|
||||
}
|
||||
|
||||
return $this->_reactRemove( $comment );
|
||||
return $this->_reactRemove( $review );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -440,31 +470,32 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* Reports a review
|
||||
*
|
||||
* @param int $database Database ID
|
||||
* @param int $comment Comment ID
|
||||
* @param int $review Review ID
|
||||
* @apiparam int author ID of the member reporting
|
||||
* @apiparam int report_type Report type (0 is default and is for letting CMGR team know, more options via core_automatic_moderation_types)
|
||||
* @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\Review
|
||||
* @apireturn \IPS\cms\Records\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem_report( $database, $comment )
|
||||
public function POSTitem_report( int $database, int $review ): 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 );
|
||||
return $this->_report( $review );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user