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,36 @@
|
||||
namespace IPS\blog\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\blog\Blog;
|
||||
use IPS\blog\Entry;
|
||||
use IPS\blog\Entry\Comment;
|
||||
use IPS\Content\Api\ItemController;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Text\Parser;
|
||||
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 Blog Entries API
|
||||
*/
|
||||
class _entries extends \IPS\Content\Api\ItemController
|
||||
class entries extends ItemController
|
||||
{
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = 'IPS\blog\Entry';
|
||||
protected string $class = 'IPS\blog\Entry';
|
||||
|
||||
/**
|
||||
* GET /blog/entries
|
||||
@@ -45,9 +60,10 @@ class _entries 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\blog\Entry>
|
||||
* @apireturn PaginatedResponse<IPS\blog\Entry>
|
||||
* @return PaginatedResponse<Entry>
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): PaginatedResponse
|
||||
{
|
||||
/* Where clause */
|
||||
$where = array();
|
||||
@@ -57,9 +73,9 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
{
|
||||
$where[] = array( 'entry_status=? AND blog_disabled=0 AND blog_social_group IS NULL AND blog_club_id IS NULL', 'published' );
|
||||
}
|
||||
elseif ( isset( \IPS\Request::i()->draft ) )
|
||||
elseif ( isset( Request::i()->draft ) )
|
||||
{
|
||||
$where[] = array( 'entry_status=?', \IPS\Request::i()->draft ? 'draft' : 'published' );
|
||||
$where[] = array( 'entry_status=?', Request::i()->draft ? 'draft' : 'published' );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
@@ -72,17 +88,18 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @throws 2B300/A INVALID_ID The entry ID does not exist
|
||||
* @return \IPS\blog\Entry
|
||||
* @apireturn \IPS\blog\Entry
|
||||
* @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', '2B300/A', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2B300/A', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,17 +111,18 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
* @apiparam int page Page number
|
||||
* @apiparam int perPage Number of results per page - defaults to 25
|
||||
* @throws 2B300/1 INVALID_ID The entry ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\Api\PaginatedResponse<IPS\blog\Entry\Comment>
|
||||
* @apireturn PaginatedResponse<IPS\blog\Entry\Comment>
|
||||
* @return PaginatedResponse<Comment>
|
||||
*/
|
||||
public function GETitem_comments( $id )
|
||||
public function GETitem_comments( int $id ): PaginatedResponse
|
||||
{
|
||||
try
|
||||
{
|
||||
return $this->_comments( $id, 'IPS\blog\Entry\Comment' );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2B300/1', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2B300/1', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,18 +155,19 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
* @throws 1B300/4 NO_TITLE No title was supplied
|
||||
* @throws 1B300/5 NO_CONTENT No content was supplied
|
||||
* @throws 1B300/A NO_PERMISSION The authorized user does not have permission to create an entry in that blog
|
||||
* @return \IPS\blog\Entry
|
||||
* @apireturn \IPS\blog\Entry
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex()
|
||||
public function POSTindex(): Response
|
||||
{
|
||||
/* Get blog */
|
||||
try
|
||||
{
|
||||
$blog = \IPS\blog\Blog::load( \IPS\Request::i()->blog );
|
||||
$blog = Blog::load( Request::i()->blog );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_BLOG', '1B300/2', 400 );
|
||||
throw new Exception( 'NO_BLOG', '1B300/2', 400 );
|
||||
}
|
||||
|
||||
/* Get author */
|
||||
@@ -156,45 +175,45 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
{
|
||||
if ( !$blog->can( 'add', $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '1B300/A', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '1B300/A', 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', '1B300/3', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1B300/3', 400 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( (int) \IPS\Request::i()->author === 0 )
|
||||
if ( Request::i()->author === 0 )
|
||||
{
|
||||
$author = new \IPS\Member;
|
||||
$author = new Member;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1B300/3', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1B300/3', 400 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check we have a title and a description */
|
||||
if ( !\IPS\Request::i()->title )
|
||||
if ( !Request::i()->title )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_TITLE', '1B300/4', 400 );
|
||||
throw new Exception( 'NO_TITLE', '1B300/4', 400 );
|
||||
}
|
||||
if ( !\IPS\Request::i()->entry )
|
||||
if ( !Request::i()->entry )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_CONTENT', '1B300/5', 400 );
|
||||
throw new Exception( 'NO_CONTENT', '1B300/5', 400 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
return new \IPS\Api\Response( 201, $this->_create( $blog, $author )->apiOutput( $this->member ) );
|
||||
return new Response( 201, $this->_create( $blog, $author )->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,57 +243,58 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
* @throws 1B300/7 NO_BLOG The blog ID does not exist or the authorized user does not have permission to post in it
|
||||
* @throws 1B300/8 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1B300/B NO_PERMISSION The authorized user does not have permission to edit that blog entry
|
||||
* @return \IPS\blog\Entry
|
||||
* @apireturn \IPS\blog\Entry
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$entry = \IPS\blog\Entry::load( $id );
|
||||
$entry = Entry::load( $id );
|
||||
if ( $this->member and !$entry->can( 'read', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
if ( $this->member and !$entry->canEdit( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '1B300/B', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '1B300/B', 403 );
|
||||
}
|
||||
|
||||
/* New blog */
|
||||
if ( isset( \IPS\Request::i()->blog ) and \IPS\Request::i()->blog != $entry->blog_id and ( !$this->member or $entry->canMove( $this->member ) ) )
|
||||
if ( isset( Request::i()->blog ) and Request::i()->blog != $entry->blog_id and ( !$this->member or $entry->canMove( $this->member ) ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
$newBlog = \IPS\blog\Blog::load( \IPS\Request::i()->blog );
|
||||
$newBlog = Blog::load( Request::i()->blog );
|
||||
if ( $this->member and !$newBlog->can( 'add', $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
$entry->move( $newBlog );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_BLOG', '1B300/7', 400 );
|
||||
throw new Exception( 'NO_BLOG', '1B300/7', 400 );
|
||||
}
|
||||
}
|
||||
|
||||
/* New author */
|
||||
if ( !$this->member and isset( \IPS\Request::i()->author ) )
|
||||
if ( !$this->member and isset( Request::i()->author ) )
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
$entry->changeAuthor( $member );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1B300/8', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1B300/8', 400 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,36 +303,36 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
|
||||
/* Save and return */
|
||||
$entry->save();
|
||||
return new \IPS\Api\Response( 200, $entry->apiOutput( $this->member ) );
|
||||
return new Response( 200, $entry->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2B300/6', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2B300/6', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update entry
|
||||
*
|
||||
* @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
|
||||
{
|
||||
/* Is draft */
|
||||
if ( isset( \IPS\Request::i()->draft ) )
|
||||
if ( isset( Request::i()->draft ) )
|
||||
{
|
||||
$item->status = \IPS\Request::i()->draft ? 'draft' : 'published';
|
||||
$item->status = Request::i()->draft ? 'draft' : 'published';
|
||||
}
|
||||
|
||||
/* Content */
|
||||
if ( isset( \IPS\Request::i()->entry ) )
|
||||
if ( isset( Request::i()->entry ) )
|
||||
{
|
||||
$entryContents = \IPS\Request::i()->entry;
|
||||
$entryContents = Request::i()->entry;
|
||||
if ( $this->member )
|
||||
{
|
||||
$entryContents = \IPS\Text\Parser::parseStatic( $entryContents, TRUE, NULL, $this->member, 'blog_Entries' );
|
||||
$entryContents = Parser::parseStatic( $entryContents, NULL, $this->member, 'blog_Entries' );
|
||||
}
|
||||
$item->content = $entryContents;
|
||||
}
|
||||
@@ -321,9 +341,9 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
$this->_createOrUpdatePoll( $item, $type );
|
||||
|
||||
/* Category */
|
||||
if ( isset( \IPS\Request::i()->category ) )
|
||||
if ( isset( Request::i()->category ) )
|
||||
{
|
||||
$item->category_id = \IPS\Request::i()->category;
|
||||
$item->category_id = Request::i()->category;
|
||||
}
|
||||
|
||||
/* Pass up */
|
||||
@@ -337,25 +357,26 @@ class _entries extends \IPS\Content\Api\ItemController
|
||||
* @param int $id ID Number
|
||||
* @throws 2B300/9 INVALID_ID The entry ID does not exist
|
||||
* @throws 2B300/C NO_PERMISSION The authorized user does not have permission to delete the entry
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $id )
|
||||
public function DELETEitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$item = \IPS\blog\Entry::load( $id );
|
||||
$item = Entry::load( $id );
|
||||
if ( $this->member and !$item->canDelete( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2B300/C', 404 );
|
||||
throw new Exception( 'NO_PERMISSION', '2B300/C', 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', '2B300/9', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2B300/9', 404 );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user