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

+103 -80
View File
@@ -12,21 +12,37 @@
namespace IPS\forums\api;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Api\Exception;
use IPS\Api\PaginatedResponse;
use IPS\Api\Response;
use IPS\Content\Api\ItemController;
use IPS\Content\Item;
use IPS\DateTime;
use IPS\Db;
use IPS\forums\Forum;
use IPS\forums\Topic;
use IPS\forums\Topic\Post;
use IPS\Member;
use IPS\Request;
use OutOfRangeException;
use function defined;
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 Topics API
*/
class _topics extends \IPS\Content\Api\ItemController
class topics extends ItemController
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic';
protected string $class = 'IPS\forums\Topic';
/**
* GET /forums/topics
@@ -47,17 +63,18 @@ class _topics extends \IPS\Content\Api\ItemController
* @apiparam string sortDir Sort direction. Can be 'asc' or 'desc' - defaults to 'asc'
* @apiparam int page Page number
* @apiparam int perPage Number of results per page - defaults to 25
* @return \IPS\Api\PaginatedResponse<IPS\forums\Topic>
* @apireturn PaginatedResponse<IPS\forums\Topic>
* @return PaginatedResponse<Topic>
*/
public function GETindex()
public function GETindex(): PaginatedResponse
{
/* Init */
$where = array();
/* Has best answer */
if ( isset( \IPS\Request::i()->hasBestAnswer ) )
if ( isset( Request::i()->hasBestAnswer ) )
{
if ( \IPS\Request::i()->hasBestAnswer )
if ( Request::i()->hasBestAnswer )
{
$where[] = array( "topic_answered_pid>0" );
}
@@ -68,15 +85,15 @@ class _topics extends \IPS\Content\Api\ItemController
}
/* Archived */
if ( isset( \IPS\Request::i()->archived ) )
if ( isset( Request::i()->archived ) )
{
if ( \IPS\Request::i()->archived )
if ( Request::i()->archived )
{
$where[] = array( \IPS\Db::i()->in( 'topic_archive_status', array( \IPS\forums\Topic::ARCHIVE_DONE, \IPS\forums\Topic::ARCHIVE_WORKING, \IPS\forums\Topic::ARCHIVE_RESTORE ) ) );
$where[] = array( Db::i()->in( 'topic_archive_status', array( Topic::ARCHIVE_DONE, Topic::ARCHIVE_WORKING, Topic::ARCHIVE_RESTORE ) ) );
}
else
{
$where[] = array( \IPS\Db::i()->in( 'topic_archive_status', array( \IPS\forums\Topic::ARCHIVE_NOT, \IPS\forums\Topic::ARCHIVE_EXCLUDE ) ) );
$where[] = array( Db::i()->in( 'topic_archive_status', array( Topic::ARCHIVE_NOT, Topic::ARCHIVE_EXCLUDE ) ) );
}
}
@@ -90,17 +107,18 @@ class _topics extends \IPS\Content\Api\ItemController
*
* @param int $id ID Number
* @throws 2F294/9 INVALID_ID The topic ID does not exist
* @return \IPS\forums\Topic
* @apireturn \IPS\forums\Topic
* @return Response
*/
public function GETitem( $id )
public function GETitem( int $id ): Response
{
try
{
return $this->_view( $id );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'INVALID_ID', '2F294/9', 404 );
throw new Exception( 'INVALID_ID', '2F294/9', 404 );
}
}
@@ -114,37 +132,38 @@ class _topics extends \IPS\Content\Api\ItemController
* @apiparam int page Page number
* @apiparam int perPage Number of results per page - defaults to 25
* @throws 1F294/1 INVALID_ID The topic ID does not exist or the authorized user does not have permission to view it
* @return \IPS\Api\PaginatedResponse<IPS\forums\Topic\Post>
* @apireturn PaginatedResponse<IPS\forums\Topic\Post>
* @return PaginatedResponse<Post>
*/
public function GETitem_posts( $id )
public function GETitem_posts( int $id ): PaginatedResponse
{
try
{
return $this->_comments( $id, 'IPS\forums\Topic\Post' );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'INVALID_ID', '1F294/1', 404 );
throw new Exception( 'INVALID_ID', '1F294/1', 404 );
}
}
/**
* Create or update topic
*
* @param \IPS\Content\Item $item The item
* @param Item $item The item
* @param string $type add or edit
* @return \IPS\Content\Item
* @return Item
*/
protected function _createOrUpdate( \IPS\Content\Item $item, $type='add' )
protected function _createOrUpdate( Item $item, string $type='add' ): Item
{
/* Open/Close time */
if ( \IPS\Request::i()->open_time )
if ( Request::i()->open_time )
{
$item->topic_open_time = ( new \DateTime( \IPS\Request::i()->open_time ) )->getTimestamp();
$item->topic_open_time = ( new \DateTime( Request::i()->open_time ) )->getTimestamp();
}
if ( \IPS\Request::i()->close_time )
if ( Request::i()->close_time )
{
$item->topic_close_time = ( new \DateTime( \IPS\Request::i()->close_time ) )->getTimestamp();
$item->topic_close_time = ( new \DateTime( Request::i()->close_time ) )->getTimestamp();
}
/* Do we have a poll to attach? */
@@ -184,22 +203,23 @@ class _topics extends \IPS\Content\Api\ItemController
* @throws 1F294/4 NO_POST No post was supplied
* @throws 2F294/C NO_PERMISSION The authorized user does not have permission to create a topic in that forum
* @throws 3F294/D NO_ANON_PERMISSION The topic is set for anonymous posting, but the author does not have permission to post anonymously
* @return \IPS\forums\Topic
* @apireturn \IPS\forums\Topic
* @return Response
*/
public function POSTindex()
public function POSTindex(): Response
{
/* Get forum */
try
{
$forum = \IPS\forums\Forum::load( \IPS\Request::i()->forum );
$forum = Forum::load( Request::i()->forum );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'NO_FORUM', '1F294/2', 400 );
throw new Exception( 'NO_FORUM', '1F294/2', 400 );
}
if ( !$forum->sub_can_post )
{
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F294/C', 403 );
throw new Exception( 'NO_PERMISSION', '2F294/C', 403 );
}
/* Get author */
@@ -207,55 +227,55 @@ class _topics extends \IPS\Content\Api\ItemController
{
if ( !$forum->can( 'add', $this->member ) )
{
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F294/C', 403 );
throw new Exception( 'NO_PERMISSION', '2F294/C', 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', '1F294/3', 400 );
throw new Exception( 'NO_AUTHOR', '1F294/3', 400 );
}
}
else
{
if ( \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', '1F294/3', 400 );
throw new Exception( 'NO_AUTHOR', '1F294/3', 400 );
}
}
}
/* Check anonymous posting */
if ( isset( \IPS\Request::i()->anonymous ) and $author->member_id )
if ( isset( Request::i()->anonymous ) and $author->member_id )
{
if ( ! $forum->canPostAnonymously( 0, $author ) )
{
throw new \IPS\Api\Exception( 'NO_ANON_PERMISSION', '3F294/D', 403 );
throw new Exception( 'NO_ANON_PERMISSION', '3F294/D', 403 );
}
}
/* Check we have a title and a post */
if ( !\IPS\Request::i()->title )
if ( !Request::i()->title )
{
throw new \IPS\Api\Exception( 'NO_TITLE', '1F294/5', 400 );
throw new Exception( 'NO_TITLE', '1F294/5', 400 );
}
if ( !\IPS\Request::i()->post )
if ( !Request::i()->post )
{
throw new \IPS\Api\Exception( 'NO_POST', '1F294/4', 400 );
throw new Exception( 'NO_POST', '1F294/4', 400 );
}
/* Do it */
return new \IPS\Api\Response( 201, $this->_create( $forum, $author )->apiOutput( $this->member ) );
return new Response( 201, $this->_create( $forum, $author )->apiOutput( $this->member ) );
}
/**
@@ -288,52 +308,53 @@ class _topics extends \IPS\Content\Api\ItemController
* @throws 1F294/8 NO_AUTHOR The author ID does not exist
* @throws 2F294/A NO_PERMISSION The authorized user does not have permission to edit the topic
* @throws 1F294/E INVALID_DATE The date is invalid
* @return \IPS\forums\Topic
* @apireturn \IPS\forums\Topic
* @return Response
*/
public function POSTitem( $id )
public function POSTitem( int $id ): Response
{
try
{
$topic = \IPS\forums\Topic::load( $id );
$topic = Topic::load( $id );
if ( $this->member and !$topic->can( 'read', $this->member ) )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
if ( $this->member and !$topic->canEdit( $this->member ) )
{
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F294/A', 403 );
throw new Exception( 'NO_PERMISSION', '2F294/A', 403 );
}
/* New forum */
if ( isset( \IPS\Request::i()->forum ) and \IPS\Request::i()->forum != $topic->forum_id and ( !$this->member or $topic->canMove( $this->member ) ) )
if ( isset( Request::i()->forum ) and Request::i()->forum != $topic->forum_id and ( !$this->member or $topic->canMove( $this->member ) ) )
{
try
{
$newForum = \IPS\forums\Forum::load( \IPS\Request::i()->forum );
$newForum = Forum::load( Request::i()->forum );
if ( $this->member and !$newForum->can( 'add', $this->member ) )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
$topic->move( $newForum );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'NO_FORUM', '1F294/7', 400 );
throw new Exception( 'NO_FORUM', '1F294/7', 400 );
}
}
/* New author */
if ( !$this->member and isset( \IPS\Request::i()->author ) )
if ( !$this->member and isset( Request::i()->author ) )
{
/* Just renaming the guest */
if ( !$topic->starter_id and ( !isset( \IPS\Request::i()->author ) or !\IPS\Request::i()->author ) and isset( \IPS\Request::i()->author_name ) )
if ( !$topic->starter_id and ( !isset( Request::i()->author ) or !Request::i()->author ) and isset( Request::i()->author_name ) )
{
$topic->starter_name = \IPS\Request::i()->author_name;
$topic->starter_name = Request::i()->author_name;
if ( $firstPost = $this->comments( 1, 0, 'date', 'asc' ) )
if ( $firstPost = $topic->comments( 1, 0, 'date', 'asc' ) )
{
$firstPost->author_name = \IPS\Request::i()->author_name;
$firstPost->author_name = Request::i()->author_name;
}
}
@@ -342,31 +363,31 @@ class _topics extends \IPS\Content\Api\ItemController
{
try
{
$member = \IPS\Member::load( \IPS\Request::i()->author );
$member = Member::load( Request::i()->author );
if ( !$member->member_id )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
$topic->changeAuthor( $member );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1F294/8', 400 );
throw new Exception( 'NO_AUTHOR', '1F294/8', 400 );
}
}
}
/* Do we have a date? */
if ( isset( \IPS\Request::i()->date ) ) {
if ( isset( Request::i()->date ) ) {
try
{
$date = new \IPS\DateTime( \IPS\Request::i()->date );
$date = new DateTime( Request::i()->date );
}
catch( \Exception $e )
{
throw new \IPS\Api\Exception( 'INVALID_DATE', '1F294/E', 400 );
throw new Exception( 'INVALID_DATE', '1F294/E', 400 );
}
/* Do we have a first comment? */
@@ -374,6 +395,7 @@ class _topics extends \IPS\Content\Api\ItemController
{
try
{
/* @var array $databaseColumnMap */
$commentClass = $topic::$commentClass;
$field = $commentClass::$databaseColumnMap['date'];
@@ -390,11 +412,11 @@ class _topics extends \IPS\Content\Api\ItemController
/* Save and return */
$topic->save();
return new \IPS\Api\Response( 200, $topic->apiOutput( $this->member ) );
return new Response( 200, $topic->apiOutput( $this->member ) );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'INVALID_ID', '2F294/6', 404 );
throw new Exception( 'INVALID_ID', '2F294/6', 404 );
}
}
@@ -405,25 +427,26 @@ class _topics extends \IPS\Content\Api\ItemController
* @param int $id ID Number
* @throws 1F294/5 INVALID_ID The topic ID does not exist
* @throws 2F294/B NO_PERMISSION The authorized user does not have permission to delete the topic
* @return void
* @apireturn void
* @return Response
*/
public function DELETEitem( $id )
public function DELETEitem( int $id ): Response
{
try
{
$item = \IPS\forums\Topic::load( $id );
$item = Topic::load( $id );
if ( $this->member and !$item->canDelete( $this->member ) )
{
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F294/B', 404 );
throw new Exception( 'NO_PERMISSION', '2F294/B', 404 );
}
$item->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', '1F294/5', 404 );
throw new Exception( 'INVALID_ID', '1F294/5', 404 );
}
}
}