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
@@ -11,16 +11,44 @@
|
||||
namespace IPS\Content\Api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Filter;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Content\Reaction;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\core\Reports\Report;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use UnexpectedValueException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
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 Base API endpoint for Content Comments
|
||||
*/
|
||||
class _CommentController extends \IPS\Api\Controller
|
||||
class CommentController extends Controller
|
||||
{
|
||||
/**
|
||||
* List
|
||||
@@ -28,29 +56,30 @@ class _CommentController extends \IPS\Api\Controller
|
||||
* @param array $where Extra WHERE clause
|
||||
* @param string $containerParam The parameter which includes the container values
|
||||
* @param bool $byPassPerms If permissions should be ignored
|
||||
* @return \IPS\Api\PaginatedResponse
|
||||
* @return PaginatedResponse
|
||||
*/
|
||||
protected function _list( $where = array(), $containerParam = 'categories', $byPassPerms=FALSE )
|
||||
protected function _list( array $where = array(), string $containerParam = 'categories', bool $byPassPerms=FALSE ): PaginatedResponse
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
$class = $this->class;
|
||||
$itemClass = $class::$itemClass;
|
||||
|
||||
/* Containers */
|
||||
if ( isset( \IPS\Request::i()->$containerParam ) )
|
||||
if ( isset( Request::i()->$containerParam ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $itemClass::$databaseTable . '.' . $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->$containerParam ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $itemClass::$databaseTable . '.' . $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', Request::i()->$containerParam ) ) ) ) );
|
||||
}
|
||||
|
||||
/* Authors */
|
||||
if ( isset( \IPS\Request::i()->authors ) )
|
||||
if ( isset( Request::i()->authors ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->authors ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', Request::i()->authors ) ) ) ) );
|
||||
}
|
||||
|
||||
/* Pinned? */
|
||||
if ( isset( \IPS\Request::i()->pinned ) AND \in_array( 'IPS\Content\Pinnable', class_implements( $itemClass ) ) )
|
||||
if ( isset( Request::i()->pinned ) AND IPS::classUsesTrait( $itemClass, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->pinned )
|
||||
if ( Request::i()->pinned )
|
||||
{
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['pinned'] . "=1" );
|
||||
}
|
||||
@@ -61,9 +90,9 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Featured? */
|
||||
if ( isset( \IPS\Request::i()->featured ) AND \in_array( 'IPS\Content\Featurable', class_implements( $itemClass ) ) )
|
||||
if ( isset( Request::i()->featured ) AND IPS::classUsesTrait( $itemClass, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->featured )
|
||||
if ( Request::i()->featured )
|
||||
{
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['featured'] . "=1" );
|
||||
}
|
||||
@@ -74,22 +103,22 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Locked? */
|
||||
if ( isset( \IPS\Request::i()->locked ) AND \in_array( 'IPS\Content\Lockable', class_implements( $itemClass ) ) )
|
||||
if ( isset( Request::i()->locked ) AND IPS::classUsesTrait( $itemClass, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( isset( static::$databaseColumnMap['locked'] ) )
|
||||
if ( isset( $itemClass::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['locked'] . '=?', \intval( \IPS\Request::i()->locked ) );
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['locked'] . '=?', intval( Request::i()->locked ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['state'] . '=?', \IPS\Request::i()->locked ? 'closed' : 'open' );
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['state'] . '=?', Request::i()->locked ? 'closed' : 'open' );
|
||||
}
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( \IPS\Request::i()->hidden ) AND \in_array( 'IPS\Content\Hideable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->hidden ) AND IPS::classUsesTrait( $class, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hidden )
|
||||
if ( Request::i()->hidden )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['hidden'] ) )
|
||||
{
|
||||
@@ -114,9 +143,9 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Has poll? */
|
||||
if ( isset( \IPS\Request::i()->hasPoll ) AND \in_array( 'IPS\Content\Polls', class_implements( $itemClass ) ) )
|
||||
if ( isset( Request::i()->hasPoll ) AND IPS::classUsesTrait( $itemClass, 'IPS\Content\Polls' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hasPoll )
|
||||
if ( Request::i()->hasPoll )
|
||||
{
|
||||
$where[] = array( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['poll'] . ">0" );
|
||||
}
|
||||
@@ -127,25 +156,25 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Sort */
|
||||
if ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, array( 'date' ) ) )
|
||||
if ( isset( Request::i()->sortBy ) and Request::i()->sortBy == 'date' )
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ \IPS\Request::i()->sortBy ];
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ Request::i()->sortBy ];
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, array( 'title' ) ) )
|
||||
if ( isset( Request::i()->sortBy ) and Request::i()->sortBy == 'title' )
|
||||
{
|
||||
$sortBy = $itemClass::$databasePrefix . $itemClass::$databaseColumnMap[ \IPS\Request::i()->sortBy ];
|
||||
$sortBy = $itemClass::$databasePrefix . $itemClass::$databaseColumnMap[ Request::i()->sortBy ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnId;
|
||||
}
|
||||
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and \in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
|
||||
$sortDir = ( isset( Request::i()->sortDir ) and in_array( mb_strtolower( Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? Request::i()->sortDir : 'asc';
|
||||
|
||||
/* Get results */
|
||||
if ( $this->member and !$byPassPerms )
|
||||
{
|
||||
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'read', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
|
||||
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'read', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
|
||||
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'read', Filter::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
|
||||
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'read', Filter::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -164,7 +193,7 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* We also need to check the item for soft delete and post before register */
|
||||
if( \in_array( 'IPS\Content\Hideable', class_implements( $itemClass ) ) )
|
||||
if( IPS::classUsesTrait( $itemClass, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
/* No matter if we can or cannot view hidden items, we do not want these to show: -2 is queued for deletion and -3 is posted before register */
|
||||
if ( isset( $itemClass::$databaseColumnMap['hidden'] ) )
|
||||
@@ -179,95 +208,96 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
}
|
||||
|
||||
$query = \IPS\Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" )->join( $itemClass::$databaseTable, array_merge( array( array( $class::$databaseTable . "." . $class::$databasePrefix . $class::$databaseColumnMap['item'] . "=" . $itemClass::$databaseTable . "." . $itemClass::$databasePrefix . $itemClass::$databaseColumnId ) ), $itemWhere ), 'STRAIGHT_JOIN' );
|
||||
$count = \IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->join( $itemClass::$databaseTable, array_merge( array( array( $class::$databaseTable . "." . $class::$databasePrefix . $class::$databaseColumnMap['item'] . "=" . $itemClass::$databaseTable . "." . $itemClass::$databasePrefix . $itemClass::$databaseColumnId ) ), $itemWhere ), 'STRAIGHT_JOIN' )->first();
|
||||
$query = Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" )->join( $itemClass::$databaseTable, array_merge( array( array( $class::$databaseTable . "." . $class::$databasePrefix . $class::$databaseColumnMap['item'] . "=" . $itemClass::$databaseTable . "." . $itemClass::$databasePrefix . $itemClass::$databaseColumnId ) ), $itemWhere ), 'STRAIGHT_JOIN' );
|
||||
$count = Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->join( $itemClass::$databaseTable, array_merge( array( array( $class::$databaseTable . "." . $class::$databasePrefix . $class::$databaseColumnMap['item'] . "=" . $itemClass::$databaseTable . "." . $itemClass::$databasePrefix . $itemClass::$databaseColumnId ) ), $itemWhere ), 'STRAIGHT_JOIN' )->first();
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
$query,
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
$class,
|
||||
$count,
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\Content\Item $item Content Item
|
||||
* @param \IPS\Member $author Author
|
||||
* @param Item $item Content Item
|
||||
* @param Member $author Author
|
||||
* @param string $contentParam The parameter that contains the content body
|
||||
* @return \IPS\Api\Response
|
||||
* @return Response
|
||||
*/
|
||||
protected function _create( \IPS\Content\Item $item, \IPS\Member $author, $contentParam='content' )
|
||||
protected function _create( Item $item, Member $author, string $contentParam='content' ): Response
|
||||
{
|
||||
return new \IPS\Api\Response( 201, $this->_createComment( $item, $author, $contentParam )->apiOutput( $this->member ) );
|
||||
return new Response( 201, $this->_createComment( $item, $author, $contentParam )->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\Content\Item $item Content Item
|
||||
* @param \IPS\Member $author Author
|
||||
* @param Item $item Content Item
|
||||
* @param Member $author Author
|
||||
* @param string $contentParam The parameter that contains the content body
|
||||
* @return \IPS\Api\Response
|
||||
* @return Comment
|
||||
*/
|
||||
protected function _createComment( \IPS\Content\Item $item, \IPS\Member $author, $contentParam='content' )
|
||||
protected function _createComment( Item $item, Member $author, string $contentParam='content' ): Comment
|
||||
{
|
||||
/* Work out the date */
|
||||
$date = ( !$this->member and \IPS\Request::i()->date ) ? new \IPS\DateTime( \IPS\Request::i()->date ) : \IPS\DateTime::create();
|
||||
$date = ( !$this->member and Request::i()->date ) ? new DateTime( Request::i()->date ) : DateTime::create();
|
||||
|
||||
/* Is it hidden? */
|
||||
$hidden = NULL;
|
||||
if ( isset( \IPS\Request::i()->hidden ) and !$this->member )
|
||||
if ( isset( Request::i()->hidden ) and !$this->member )
|
||||
{
|
||||
$hidden = \IPS\Request::i()->hidden;
|
||||
$hidden = Request::i()->hidden;
|
||||
}
|
||||
|
||||
/* Parse */
|
||||
$content = \IPS\Request::i()->$contentParam;
|
||||
$content = Request::i()->$contentParam;
|
||||
if ( $this->member )
|
||||
{
|
||||
$content = \IPS\Text\Parser::parseStatic( $content, TRUE, NULL, $this->member, $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = Parser::parseStatic( $content, NULL, $this->member, $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
}
|
||||
|
||||
/* Create post */
|
||||
/* @var Comment $class */
|
||||
$class = $this->class;
|
||||
if ( \in_array( 'IPS\Content\Review', class_parents( $class ) ) )
|
||||
if ( in_array( 'IPS\Content\Review', class_parents( $class ) ) )
|
||||
{
|
||||
$comment = $class::create( $item, $content, FALSE, \intval( \IPS\Request::i()->rating ), $author->member_id ? NULL : $author->real_name, $author, $date, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), $hidden, ( isset( \IPS\Request::i()->anonymous ) ? (bool) \IPS\Request::i()->anonymous : NULL ) );
|
||||
$comment = $class::create( $item, $content, FALSE, intval( Request::i()->rating ), $author->member_id ? NULL : $author->real_name, $author, $date, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), $hidden, ( isset( Request::i()->anonymous ) ? (bool) Request::i()->anonymous : NULL ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$comment = $class::create( $item, $content, FALSE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), $hidden, ( isset( \IPS\Request::i()->anonymous ) ? (bool) \IPS\Request::i()->anonymous : NULL ) );
|
||||
$comment = $class::create( $item, $content, FALSE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), $hidden, ( isset( Request::i()->anonymous ) ? (bool) Request::i()->anonymous : NULL ) );
|
||||
}
|
||||
|
||||
/* Index */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
if ( $item::$firstCommentRequired and !$comment->isFirst() )
|
||||
{
|
||||
if ( \in_array( 'IPS\Content\Searchable', class_implements( $class ) ) )
|
||||
if( SearchContent::isSearchable( $class ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item->firstComment() );
|
||||
Index::i()->index( $item->firstComment() );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item );
|
||||
Index::i()->index( $item );
|
||||
}
|
||||
}
|
||||
if ( $comment instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $comment ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
}
|
||||
|
||||
/* Hide */
|
||||
if ( isset( \IPS\Request::i()->hidden ) and $this->member and \IPS\Request::i()->hidden and $comment->canHide( $this->member ) )
|
||||
if ( isset( Request::i()->hidden ) and $this->member and Request::i()->hidden and $comment->canHide( $this->member ) )
|
||||
{
|
||||
$comment->hide( $this->member );
|
||||
}
|
||||
@@ -279,17 +309,18 @@ class _CommentController extends \IPS\Api\Controller
|
||||
/**
|
||||
* Edit
|
||||
*
|
||||
* @param \IPS\Content\Comment $comment The comment
|
||||
* @param Comment $comment The comment
|
||||
* @param string $contentParam The parameter that contains the content body
|
||||
* @throws InvalidArgumentException Invalid author
|
||||
* @return \IPS\Api\Response
|
||||
* @return Response
|
||||
*/
|
||||
protected function _edit( $comment, $contentParam='content' )
|
||||
protected function _edit( Comment $comment, string $contentParam='content' ): Response
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
/* Hidden */
|
||||
if ( !$this->member and isset( \IPS\Request::i()->hidden ) )
|
||||
if ( !$this->member and isset( Request::i()->hidden ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hidden )
|
||||
if ( Request::i()->hidden )
|
||||
{
|
||||
$comment->hide( FALSE );
|
||||
}
|
||||
@@ -300,15 +331,15 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Change author */
|
||||
if ( !$this->member and isset( \IPS\Request::i()->author ) )
|
||||
if ( !$this->member and isset( Request::i()->author ) )
|
||||
{
|
||||
$authorIdColumn = $comment::$databaseColumnMap['author'];
|
||||
$authorNameColumn = $comment::$databaseColumnMap['author_name'];
|
||||
|
||||
/* Just renaming the guest */
|
||||
if ( !$comment->$authorIdColumn and ( !isset( \IPS\Request::i()->author ) or !\IPS\Request::i()->author ) and isset( \IPS\Request::i()->author_name ) )
|
||||
if ( !$comment->$authorIdColumn and ( !isset( Request::i()->author ) or !Request::i()->author ) and isset( Request::i()->author_name ) )
|
||||
{
|
||||
$comment->$authorNameColumn = \IPS\Request::i()->author_name;
|
||||
$comment->$authorNameColumn = Request::i()->author_name;
|
||||
}
|
||||
|
||||
/* Actually changing the author */
|
||||
@@ -316,42 +347,42 @@ class _CommentController extends \IPS\Api\Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
$member = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$member = Member::load( Request::i()->author );
|
||||
if ( !$member->member_id )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
|
||||
$comment->changeAuthor( $member );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Post value */
|
||||
if ( isset( \IPS\Request::i()->$contentParam ) )
|
||||
if ( isset( Request::i()->$contentParam ) )
|
||||
{
|
||||
$contentColumn = $comment::$databaseColumnMap['content'];
|
||||
|
||||
$content = \IPS\Request::i()->$contentParam;
|
||||
$content = Request::i()->$contentParam;
|
||||
if ( $this->member )
|
||||
{
|
||||
$item = $comment->item();
|
||||
$content = \IPS\Text\Parser::parseStatic( $content, TRUE, NULL, $this->member, $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = Parser::parseStatic( $content, NULL, $this->member, $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
}
|
||||
$comment->$contentColumn =$content;
|
||||
}
|
||||
|
||||
/* Rating */
|
||||
$ratingChanged = FALSE;
|
||||
if ( isset( \IPS\Request::i()->rating ) )
|
||||
if ( isset( Request::i()->rating ) )
|
||||
{
|
||||
$ratingChanged = TRUE;
|
||||
$ratingColumn = $comment::$databaseColumnMap['rating'];
|
||||
$comment->$ratingColumn = \intval( \IPS\Request::i()->rating );
|
||||
$comment->$ratingColumn = intval( Request::i()->rating );
|
||||
}
|
||||
|
||||
/* Save and return */
|
||||
@@ -368,25 +399,25 @@ class _CommentController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Api\Response( 200, $comment->apiOutput( $this->member ) );
|
||||
return new Response( 200, $comment->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a reaction to a comment
|
||||
*
|
||||
* @param $id
|
||||
* @return \IPS\Api\Response
|
||||
* @throws \IPS\Api\Exception
|
||||
* @param int $id
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _reactRemove( $id ): \IPS\Api\Response
|
||||
public function _reactRemove( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$member = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$member = Member::load( Request::i()->author );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1S425/6', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1S425/6', 404 );
|
||||
}
|
||||
|
||||
try
|
||||
@@ -403,43 +434,43 @@ class _CommentController extends \IPS\Api\Controller
|
||||
|
||||
$object->removeReaction( $member );
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
catch ( DomainException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( $e->getMessage(), '1S425/7', 403 );
|
||||
throw new Exception( $e->getMessage(), '1S425/7', 403 );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '1S425/8', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1S425/8', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* React to a comment
|
||||
*
|
||||
* @param $id
|
||||
* @return \IPS\Api\Response
|
||||
* @throws \IPS\Api\Exception
|
||||
* @param int $id
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _reactAdd( $id ): \IPS\Api\Response
|
||||
public function _reactAdd( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$reaction = \IPS\Content\Reaction::load( \IPS\Request::i()->id );
|
||||
$reaction = Reaction::load( Request::i()->id );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_REACTION', '1S425/2', 404 );
|
||||
throw new Exception( 'NO_REACTION', '1S425/2', 404 );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$member = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$member = Member::load( Request::i()->author );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1S425/3', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1S425/3', 404 );
|
||||
}
|
||||
|
||||
try
|
||||
@@ -456,15 +487,15 @@ class _CommentController extends \IPS\Api\Controller
|
||||
|
||||
$object->react( $reaction, $member );
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
catch ( DomainException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'REACT_ERROR_' . $e->getMessage(), '1S425/4', 403 );
|
||||
throw new Exception( 'REACT_ERROR_' . $e->getMessage(), '1S425/4', 403 );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '1S425/5', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1S425/5', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -472,19 +503,19 @@ class _CommentController extends \IPS\Api\Controller
|
||||
/**
|
||||
* Report a comment
|
||||
*
|
||||
* @param $id
|
||||
* @return \IPS\Api\Response
|
||||
* @throws \IPS\Api\Exception
|
||||
* @param int $id
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _report( $id ): \IPS\Api\Response
|
||||
public function _report( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$member = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$member = Member::load( Request::i()->author );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1S425/B', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1S425/B', 404 );
|
||||
}
|
||||
|
||||
$class = $this->class;
|
||||
@@ -501,11 +532,11 @@ class _CommentController extends \IPS\Api\Controller
|
||||
/* Has this member already reported this in the past 24 hours */
|
||||
try
|
||||
{
|
||||
$index = \IPS\core\Reports\Report::loadByClassAndId( \get_class( $object ), $object->$idColumn );
|
||||
$report = \IPS\Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, $member, time() - ( \IPS\Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
|
||||
$index = Report::loadByClassAndId( get_class( $object ), $object->$idColumn );
|
||||
$report = Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, $member, time() - ( Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
|
||||
|
||||
/* They have aleady reported, so do nothing */
|
||||
throw new \IPS\Api\Exception( 'REPORTED_ALREADY', '1S425/C', 404 );
|
||||
throw new Exception( 'REPORTED_ALREADY', '1S425/C', 404 );
|
||||
}
|
||||
catch( \Exception $e )
|
||||
{
|
||||
@@ -514,13 +545,13 @@ class _CommentController extends \IPS\Api\Controller
|
||||
|
||||
try
|
||||
{
|
||||
$object->report( ( isset( \IPS\Request::i()->message ) ? \IPS\Request::i()->message : '' ), ( isset(\IPS\Request::i()->report_type) ? \IPS\Request::i()->report_type : 0 ), $member );
|
||||
$object->report( ( isset( Request::i()->message ) ? Request::i()->message : '' ), ( isset(Request::i()->report_type) ? Request::i()->report_type : 0 ), $member );
|
||||
}
|
||||
catch( \UnexpectedValueException $e )
|
||||
catch( UnexpectedValueException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'REPORT_ERROR_' . $e->getMessage(), '1S425/B', 403 );
|
||||
throw new Exception( 'REPORT_ERROR_' . $e->getMessage(), '1S425/B', 403 );
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
}
|
||||
@@ -11,43 +11,72 @@
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use Exception;
|
||||
use IPS\Api\GraphQL\SafeException;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Content\Reaction;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\core\Reports\Report;
|
||||
use IPS\core\Reports\Types;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\File;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
use function in_array;
|
||||
|
||||
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 Base mutator class for comments
|
||||
*/
|
||||
abstract class _CommentMutator extends ContentMutator
|
||||
abstract class CommentMutator extends ContentMutator
|
||||
{
|
||||
|
||||
/**
|
||||
* Report comment
|
||||
* Revoke Comment Report
|
||||
*
|
||||
* @param array $args Arguments
|
||||
* @param Comment $comment Comment
|
||||
*
|
||||
* @return Comment
|
||||
*/
|
||||
protected function _revokeCommentReport( array $args, \IPS\Content\Comment $comment )
|
||||
protected function _revokeCommentReport( array $args, Comment $comment ): Comment
|
||||
{
|
||||
try
|
||||
{
|
||||
$report = \IPS\Db::i()->select( '*', 'core_rc_reports', array( 'id=? AND report_by=? AND date_reported > ?', $args['reportID'], \IPS\Member::loggedIn()->member_id, time() - ( \IPS\Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
|
||||
$report = Db::i()->select( '*', 'core_rc_reports', array( 'id=? AND report_by=? AND date_reported > ?', $args['reportID'], Member::loggedIn()->member_id, time() - ( Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
|
||||
}
|
||||
catch( \UnderflowException $e )
|
||||
catch( UnderflowException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NO_REPORT', '1F295/1', 403 );
|
||||
throw new SafeException( 'NO_REPORT', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$index = \IPS\core\Reports\Report::load( $report['rid'] );
|
||||
$index = Report::load( $report['rid'] );
|
||||
}
|
||||
catch( \OutofRangeException $e )
|
||||
catch( OutofRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NO_REPORT', '1F295/1', 403 );
|
||||
throw new SafeException( 'NO_REPORT', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
\IPS\Db::i()->delete( 'core_rc_reports', array( 'id=?', $args['reportID'] ) );
|
||||
Db::i()->delete( 'core_rc_reports', array( 'id=?', $args['reportID'] ) );
|
||||
|
||||
/* Recalculate, we may have dropped below the threshold needed to hide a thing */
|
||||
$index->runAutomaticModeration();
|
||||
@@ -61,42 +90,46 @@ abstract class _CommentMutator extends ContentMutator
|
||||
/**
|
||||
* Report comment
|
||||
*
|
||||
* @param array $args Arguments
|
||||
* @param Comment $comment Comment
|
||||
*
|
||||
* @return Comment
|
||||
*/
|
||||
protected function _reportComment( array $args, \IPS\Content\Comment $comment )
|
||||
protected function _reportComment( array $args, Comment $comment ): Comment
|
||||
{
|
||||
$class = $this->class;
|
||||
$canReport = $comment->canReport();
|
||||
|
||||
if ( $canReport !== TRUE AND !( $canReport == 'report_err_already_reported' AND \IPS\Settings::i()->automoderation_enabled ) )
|
||||
if ( $canReport !== TRUE AND !( $canReport == 'report_err_already_reported' AND Settings::i()->automoderation_enabled ) )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_REPORT', '1F295/1', 403 );
|
||||
throw new SafeException( 'CANNOT_REPORT', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
$itemIdColumn = $class::$databaseColumnId;
|
||||
$idColumn = $comment::$databaseColumnId;
|
||||
|
||||
if ( \IPS\Member::loggedIn()->member_id and \IPS\Settings::i()->automoderation_enabled )
|
||||
if ( Member::loggedIn()->member_id and Settings::i()->automoderation_enabled )
|
||||
{
|
||||
/* Has this member already reported this in the past 24 hours */
|
||||
try {
|
||||
$index = \IPS\core\Reports\Report::loadByClassAndId( \get_class( $comment ), $comment->$idColumn );
|
||||
$report = \IPS\Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, \IPS\Member::loggedIn()->member_id, time() - ( \IPS\Settings::i()->automoderation_report_again_mins * 60 ) ) );
|
||||
$index = Report::loadByClassAndId( get_class( $comment ), $comment->$idColumn );
|
||||
$report = Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, Member::loggedIn()->member_id, time() - ( Settings::i()->automoderation_report_again_mins * 60 ) ) );
|
||||
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'ALREADY_REPORTED', '1F295/1', 403 );
|
||||
throw new SafeException( 'ALREADY_REPORTED', '1F295/1', 403 );
|
||||
}
|
||||
catch( \Exception $e ) {
|
||||
if( $e instanceof \IPS\Api\GraphQL\SafeException ){
|
||||
catch( Exception $e ) {
|
||||
if( $e instanceof SafeException ){
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
if( !\in_array( $args['reason'], array_keys( \IPS\core\Reports\Types::roots() ) ) && $args['reason'] !== 0 )
|
||||
if( !in_array( $args['reason'], array_keys( Types::roots() ) ) && $args['reason'] !== 0 )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_REASON', '1F295/1', 403 );
|
||||
throw new SafeException( 'INVALID_REASON', '1F295/1', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
if( !\IPS\Settings::i()->automoderation_enabled )
|
||||
if( !Settings::i()->automoderation_enabled )
|
||||
{
|
||||
$args['reason'] = 0;
|
||||
}
|
||||
@@ -107,9 +140,9 @@ abstract class _CommentMutator extends ContentMutator
|
||||
{
|
||||
$comment->report( $args['additionalInfo'], $args['reason'] );
|
||||
}
|
||||
catch( \Exception $e )
|
||||
catch( Exception $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'REPORT_FAILED', '1F295/1', 403 );
|
||||
throw new SafeException( 'REPORT_FAILED', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
|
||||
@@ -120,18 +153,18 @@ abstract class _CommentMutator extends ContentMutator
|
||||
* Comment reactions
|
||||
*
|
||||
* @param int $reactionID ID of reaction to add
|
||||
* @param \IPS\Content\Comment $comment The comment to add a reaction on
|
||||
* @param Comment $comment The comment to add a reaction on
|
||||
* @return void
|
||||
*/
|
||||
protected function _reactComment( $reactionID, \IPS\Content\Comment $comment )
|
||||
protected function _reactComment( int $reactionID, Comment $comment ): void
|
||||
{
|
||||
try
|
||||
{
|
||||
$reaction = \IPS\Content\Reaction::load( $reactionID );
|
||||
$reaction = Reaction::load( $reactionID );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_REACTION', '1F295/1', 403 );
|
||||
throw new SafeException( 'INVALID_REACTION', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
$comment->react( $reaction );
|
||||
@@ -140,43 +173,44 @@ abstract class _CommentMutator extends ContentMutator
|
||||
/**
|
||||
* Remove comment reaction
|
||||
*
|
||||
* @param \IPS\Content\Comment $comment The comment to remove the reaction on
|
||||
* @param Comment $comment The comment to remove the reaction on
|
||||
* @return void
|
||||
*/
|
||||
protected function _unreactComment( \IPS\Content\Comment $comment )
|
||||
protected function _unreactComment( Comment $comment ): void
|
||||
{
|
||||
try {
|
||||
$comment->removeReaction();
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NO_REACTIONS', '1F295/1', 403 );
|
||||
throw new SafeException( 'NO_REACTIONS', '1F295/1', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param array $commentData Array of data used to generate comment
|
||||
* @param \IPS\Content\Item $item Content Item
|
||||
* @param string $postKey Post key
|
||||
* @return \IPS\Content\Comment
|
||||
* @param array $commentData Array of data used to generate comment
|
||||
* @param Item $item Content Item
|
||||
* @param string|null $postKey Post key
|
||||
* @param Comment|null $replyTo Reply To
|
||||
* @return Comment
|
||||
*/
|
||||
protected function _createComment( array $commentData, \IPS\Content\Item $item, string $postKey = NULL, $replyTo = NULL )
|
||||
protected function _createComment( array $commentData, Item $item, string|null $postKey = NULL, Comment|null $replyTo = NULL ): Comment
|
||||
{
|
||||
/* Work out the date */
|
||||
$date = \IPS\DateTime::create();
|
||||
$date = DateTime::create();
|
||||
$hidden = false;
|
||||
$quoteHtml = '';
|
||||
|
||||
if( $replyTo !== NULL && $replyTo instanceof \IPS\Content\Comment ){
|
||||
if( $replyTo instanceof Comment ){
|
||||
try {
|
||||
$idField = $replyTo::$databaseColumnId;
|
||||
$app = $replyTo::$application;
|
||||
$replyToItem = $replyTo->item();
|
||||
$contentType = $replyToItem::$module;
|
||||
$itemClassSafe = str_replace( '\\', '_', mb_substr( $replyTo::$itemClass, 4 ) );
|
||||
$citationLang = \IPS\Member::loggedIn()->language()->get('_date_just_now_c') . ', ' . $replyTo->mapped('author_name');
|
||||
$citationLang = Member::loggedIn()->language()->get('_date_just_now_c') . ', ' . $replyTo->mapped('author_name');
|
||||
|
||||
$quoteHtml = <<<HTML
|
||||
<blockquote class="ipsQuote" data-ipsquote="" data-ipsquote-contentapp="{$app}" data-ipsquote-contentclass="{$itemClassSafe}" data-ipsquote-contentcommentid="{$replyTo->mapped('id')}" data-ipsquote-contentid="{$replyTo->item()->mapped('id')}" data-ipsquote-contenttype="{$contentType}" data-ipsquote-timestamp="{$replyTo->mapped('date')}" data-ipsquote-userid="{$replyTo->author()->member_id}" data-ipsquote-username="{$replyTo->mapped('author_name')}">
|
||||
@@ -186,7 +220,7 @@ abstract class _CommentMutator extends ContentMutator
|
||||
<div class='ipsQuote_contents'>{$replyTo->mapped('content')}</div>
|
||||
</blockquote>
|
||||
HTML;
|
||||
} catch ( \Exception $err ) {
|
||||
} catch ( Exception $err ) {
|
||||
// If something goes wrong here, it isn't a big deal - just continue without the quote
|
||||
}
|
||||
}
|
||||
@@ -199,14 +233,14 @@ HTML;
|
||||
{
|
||||
$this->_addAttachmentsToContent( $postKey, $commentData['content'] );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
catch ( DomainException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S400/2', 403 );
|
||||
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S400/2', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
/* Parse */
|
||||
$content = $quoteHtml . \IPS\Text\Parser::parseStatic( $commentData['content'], TRUE, md5( $postKey . ':' ), \IPS\Member::loggedIn(), $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = $quoteHtml . Parser::parseStatic( $commentData['content'], array( md5( $postKey . ':' ) ), Member::loggedIn(), $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
|
||||
/* Create post */
|
||||
$class = $this->class;
|
||||
@@ -216,40 +250,40 @@ HTML;
|
||||
}
|
||||
else
|
||||
{*/
|
||||
$comment = $class::create( $item, $content, FALSE, \IPS\Member::loggedIn()->member_id ? NULL : \IPS\Member::loggedIn()->real_name, NULL, \IPS\Member::loggedIn(), $date, \IPS\Request::i()->ipAddress() );
|
||||
$comment = $class::create( $item, $content, FALSE, Member::loggedIn()->member_id ? NULL : Member::loggedIn()->real_name, NULL, Member::loggedIn(), $date, Request::i()->ipAddress() );
|
||||
/*}*/
|
||||
$itemIdColumn = $item::$databaseColumnId;
|
||||
$commentIdColumn = $comment::$databaseColumnId;
|
||||
\IPS\File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $comment->$commentIdColumn );
|
||||
File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $comment->$commentIdColumn );
|
||||
|
||||
/* Index */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
if ( $item::$firstCommentRequired and !$comment->isFirst() )
|
||||
{
|
||||
if ( \in_array( 'IPS\Content\Searchable', class_implements( $class ) ) )
|
||||
if( SearchContent::isSearchable( $class ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item->firstComment() );
|
||||
Index::i()->index( $item->firstComment() );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item );
|
||||
Index::i()->index( $item );
|
||||
}
|
||||
}
|
||||
if ( $comment instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $comment ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
}
|
||||
|
||||
/* Hide */
|
||||
if ( isset( $commentData['hidden'] ) and \IPS\Member::loggedIn()->member_id and $comment->canHide() )
|
||||
if ( isset( $commentData['hidden'] ) and Member::loggedIn()->member_id and $comment->canHide() )
|
||||
{
|
||||
$comment->hide( \IPS\Member::loggedIn() );
|
||||
$comment->hide( Member::loggedIn() );
|
||||
}
|
||||
|
||||
/* Mark it as read */
|
||||
if( $item instanceof \IPS\Content\ReadMarkers )
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
|
||||
{
|
||||
$item->markRead();
|
||||
}
|
||||
|
||||
@@ -11,33 +11,38 @@
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Member;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base mutator class for Content Comments
|
||||
*/
|
||||
class _CommentType extends ObjectType
|
||||
class CommentType extends ObjectType
|
||||
{
|
||||
/*
|
||||
* @brief The item classname we use for this type
|
||||
*/
|
||||
protected static $commentClass = '\IPS\Content\Comment';
|
||||
protected static string $commentClass = '\IPS\Content\Comment';
|
||||
|
||||
/*
|
||||
* @brief GraphQL type name
|
||||
*/
|
||||
protected static $typeName = 'core_Comment';
|
||||
protected static string $typeName = 'core_Comment';
|
||||
|
||||
/*
|
||||
* @brief GraphQL type description
|
||||
*/
|
||||
protected static $typeDescription = 'A generic content comment item';
|
||||
protected static string $typeDescription = 'A generic content comment item';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -57,7 +62,7 @@ class _CommentType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fields()
|
||||
public function fields(): array
|
||||
{
|
||||
return array(
|
||||
'id' => [
|
||||
@@ -161,13 +166,14 @@ class _CommentType extends ObjectType
|
||||
],
|
||||
],
|
||||
'resolveField' => function ($item, $args, $context, $info) {
|
||||
$className = \get_class( $item );
|
||||
$className = get_class( $item );
|
||||
|
||||
/* @var Item $className */
|
||||
switch( $info->fieldName )
|
||||
{
|
||||
case 'indefinite':
|
||||
return $className::_indefiniteArticle( NULL );
|
||||
break;
|
||||
return $className::_indefiniteArticle();
|
||||
|
||||
case 'definite':
|
||||
if( $args['withItem'] === FALSE )
|
||||
{
|
||||
@@ -177,9 +183,9 @@ class _CommentType extends ObjectType
|
||||
}
|
||||
|
||||
// Return normal definite article string handled by \IPS\Content
|
||||
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
|
||||
break;
|
||||
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
|
||||
}
|
||||
return '';
|
||||
}
|
||||
]),
|
||||
'resolve' => function ($item) {
|
||||
@@ -209,28 +215,28 @@ class _CommentType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getCommentPermissionFields()
|
||||
public static function getCommentPermissionFields(): array
|
||||
{
|
||||
return array(
|
||||
'canShare' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the user share this item?',
|
||||
'resolve' => function ($comment) {
|
||||
return $comment->canShare( \IPS\Member::loggedIn() );
|
||||
return $comment->canShare( Member::loggedIn() );
|
||||
}
|
||||
],
|
||||
'canReport' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the user report this item?',
|
||||
'resolve' => function ($comment) {
|
||||
return $comment->canReport( \IPS\Member::loggedIn() ) === TRUE;
|
||||
return $comment->canReport( Member::loggedIn() ) === TRUE;
|
||||
}
|
||||
],
|
||||
'canReportOrRevoke' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the user report (or revoke a report) on this comment?',
|
||||
'resolve' => function ($comment) {
|
||||
return $comment->canReportOrRevoke( \IPS\Member::loggedIn() ) === TRUE;
|
||||
return $comment->canReportOrRevoke( Member::loggedIn() ) === TRUE;
|
||||
}
|
||||
]
|
||||
);
|
||||
@@ -241,7 +247,7 @@ class _CommentType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function args()
|
||||
public static function args(): array
|
||||
{
|
||||
return array(
|
||||
'offsetPosition' => [
|
||||
@@ -301,7 +307,7 @@ class _CommentType extends ObjectType
|
||||
*
|
||||
* @return ObjectType
|
||||
*/
|
||||
public static function getItemType()
|
||||
public static function getItemType(): ObjectType
|
||||
{
|
||||
return \IPS\Content\Api\GraphQL\TypeRegistry::item();
|
||||
}
|
||||
@@ -309,10 +315,12 @@ class _CommentType extends ObjectType
|
||||
/**
|
||||
* Return the definite article, but without the item type
|
||||
*
|
||||
* @return string
|
||||
* @param Comment $post
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public static function definiteArticleNoItem($post, $options = array())
|
||||
public static function definiteArticleNoItem( Comment $post, array $options = array()): string
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( '__defart_comment', FALSE );
|
||||
return Member::loggedIn()->language()->addToStack( '__defart_comment', FALSE );
|
||||
}
|
||||
}
|
||||
@@ -11,16 +11,28 @@
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use IPS\Db;
|
||||
use IPS\File;
|
||||
use IPS\Helpers\Form\Editor;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\Theme;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
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 Base mutator class for comments
|
||||
*/
|
||||
abstract class _ContentMutator
|
||||
abstract class ContentMutator
|
||||
{
|
||||
/**
|
||||
* Add attachments to content
|
||||
@@ -28,29 +40,29 @@ abstract class _ContentMutator
|
||||
* @param string $postKey Post key
|
||||
* @param string $content Post content
|
||||
* @return void
|
||||
* @throws \DomainException Size is too large
|
||||
* @throws DomainException Size is too large
|
||||
*/
|
||||
protected function _addAttachmentsToContent( $postKey, &$content )
|
||||
protected function _addAttachmentsToContent( string $postKey, string &$content ): void
|
||||
{
|
||||
$maxTotalSize = \IPS\Helpers\Form\Editor::maxTotalAttachmentSize( \IPS\Member::loggedIn(), 0 ); // @todo Currently set to 0 because editing is not supported. Once editing is supported by GraphQL, that will need to set the correct value
|
||||
$maxTotalSize = Editor::maxTotalAttachmentSize( Member::loggedIn(), 0 ); // @todo Currently set to 0 because editing is not supported. Once editing is supported by GraphQL, that will need to set the correct value
|
||||
|
||||
$fileAttachments = array();
|
||||
$totalSize = 0;
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_attachments', array( 'attach_post_key=?', $postKey ) ) as $attachment )
|
||||
foreach ( Db::i()->select( '*', 'core_attachments', array( 'attach_post_key=?', $postKey ) ) as $attachment )
|
||||
{
|
||||
if ( $maxTotalSize !== NULL )
|
||||
{
|
||||
$totalSize += $attachment['attach_filesize'];
|
||||
if ( $totalSize > $maxTotalSize )
|
||||
{
|
||||
throw new \DomainException;
|
||||
throw new DomainException;
|
||||
}
|
||||
}
|
||||
|
||||
$ext = mb_substr( $attachment['attach_file'], mb_strrpos( $attachment['attach_file'], '.' ) + 1 );
|
||||
if ( \in_array( mb_strtolower( $ext ), \IPS\File::$videoExtensions ) )
|
||||
if ( in_array( mb_strtolower( $ext ), File::$videoExtensions ) )
|
||||
{
|
||||
$content .= \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedVideo( $attachment['attach_location'], \IPS\Http\Url::baseUrl( \IPS\Http\Url::PROTOCOL_RELATIVE ) . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'], $attachment['attach_file'], \IPS\File::getMimeType( $attachment['attach_file'] ), $attachment['attach_id'] );
|
||||
$content .= Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedVideo( $attachment['attach_location'], Url::baseUrl( Url::PROTOCOL_RELATIVE ) . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'], $attachment['attach_file'], File::getMimeType( $attachment['attach_file'] ), $attachment['attach_id'] );
|
||||
}
|
||||
elseif ( $attachment['attach_is_image'] )
|
||||
{
|
||||
@@ -65,19 +77,19 @@ abstract class _ContentMutator
|
||||
$width = $attachment['attach_img_width'];
|
||||
}
|
||||
|
||||
$content .= str_replace( '<fileStore.core_Attachment>', \IPS\File::getClass('core_Attachment')->baseUrl(), \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedImage( $attachment['attach_location'], $attachment['attach_thumb_location'] ? $attachment['attach_thumb_location'] : $attachment['attach_location'], $attachment['attach_file'], $attachment['attach_id'], $width, $ratio ) );
|
||||
$content .= str_replace( '<fileStore.core_Attachment>', File::getClass('core_Attachment')->baseUrl(), Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedImage( $attachment['attach_location'], $attachment['attach_thumb_location'] ?: $attachment['attach_location'], $attachment['attach_file'], $attachment['attach_id'], $width, $ratio ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$fileAttachments[] = \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedFile( \IPS\Http\Url::baseUrl() . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'] . ( $attachment['attach_security_key'] ? "&key={$attachment['attach_security_key']}" : '' ), $attachment['attach_file'], FALSE, $attachment['attach_ext'], $attachment['attach_id'], $attachment['attach_security_key'] );
|
||||
$fileAttachments[] = Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedFile( Url::baseUrl() . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'] . ( $attachment['attach_security_key'] ? "&key={$attachment['attach_security_key']}" : '' ), $attachment['attach_file'], FALSE, $attachment['attach_ext'], $attachment['attach_id'], $attachment['attach_security_key'] );
|
||||
}
|
||||
}
|
||||
|
||||
if( \count( $fileAttachments ) )
|
||||
if( count( $fileAttachments ) )
|
||||
{
|
||||
$content .= "<p>" . implode( ' ', $fileAttachments ) . "</p>";
|
||||
}
|
||||
|
||||
\IPS\Db::i()->update( 'core_attachments', array( 'attach_post_key' => '' ), array( 'attach_post_key=?', $postKey ) );
|
||||
Db::i()->update( 'core_attachments', array( 'attach_post_key' => '' ), array( 'attach_post_key=?', $postKey ) );
|
||||
}
|
||||
}
|
||||
@@ -10,19 +10,21 @@
|
||||
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
use GraphQL\Type\Definition\UnionType;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Item;
|
||||
use function defined;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base mutator class for Content Items
|
||||
*/
|
||||
class _ContentType extends UnionType
|
||||
class ContentType extends UnionType
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
@@ -30,18 +32,19 @@ class _ContentType extends UnionType
|
||||
'name' => 'core_Content',
|
||||
'description' => 'A union type that returns a CommentType, ItemType or @todo ReviewType depending on the object passed to it',
|
||||
'types' => [
|
||||
\IPS\Content\Api\GraphQL\TypeRegistry::comment(),
|
||||
\IPS\Content\Api\GraphQL\TypeRegistry::item()
|
||||
TypeRegistry::comment(),
|
||||
TypeRegistry::item()
|
||||
],
|
||||
'resolveType' => function ($content) {
|
||||
if ( $content instanceof \IPS\Content\Comment )
|
||||
if ( $content instanceof Comment )
|
||||
{
|
||||
return \IPS\Content\Api\GraphQL\TypeRegistry::comment();
|
||||
return TypeRegistry::comment();
|
||||
}
|
||||
elseif( $content instanceof \IPS\Content\Item )
|
||||
elseif( $content instanceof Item )
|
||||
{
|
||||
return \IPS\Content\Api\GraphQL\TypeRegistry::item();
|
||||
return TypeRegistry::item();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
@@ -10,18 +10,41 @@
|
||||
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
|
||||
use \IPS\Api\GraphQL\TypeRegistry;
|
||||
use DomainException;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\GraphQL\SafeException;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\DateTime;
|
||||
use IPS\File;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
use function intval;
|
||||
use function strlen;
|
||||
use function strpos;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base mutator class for Content Items
|
||||
*/
|
||||
abstract class _ItemMutator extends ContentMutator
|
||||
abstract class ItemMutator extends ContentMutator
|
||||
{
|
||||
public function args(): array
|
||||
{
|
||||
@@ -38,10 +61,10 @@ abstract class _ItemMutator extends ContentMutator
|
||||
/**
|
||||
* Mark as read
|
||||
*
|
||||
* @param \IPS\Content\Item $item Item to mark as read
|
||||
* @return void
|
||||
* @param Item $item Item to mark as read
|
||||
* @return Item
|
||||
*/
|
||||
protected function _markRead( $item )
|
||||
protected function _markRead( Item $item ): Item
|
||||
{
|
||||
$item->markRead();
|
||||
return $item;
|
||||
@@ -50,21 +73,21 @@ abstract class _ItemMutator extends ContentMutator
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\Node\Model $container Container
|
||||
* @param \IPS\Member $author Author
|
||||
* @param string $postKey Post key
|
||||
* @return \IPS\Content\Item
|
||||
* @param array $itemData Item Data
|
||||
* @param Model|null $container Container
|
||||
* @param string|null $postKey Post key
|
||||
* @return Item
|
||||
*/
|
||||
protected function _create( $itemData, \IPS\Node\Model $container = NULL, string $postKey = NULL )
|
||||
protected function _create( array $itemData, Model|null $container = NULL, string|null $postKey = NULL ): Item
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
/* Work out the date */
|
||||
$date = \IPS\DateTime::create();
|
||||
$date = DateTime::create();
|
||||
|
||||
/* Create item */
|
||||
$item = $class::createItem( \IPS\Member::loggedIn(), \IPS\Request::i()->ipAddress(), $date, $container );
|
||||
$this->_createOrUpdate( $item, $itemData, 'add' );
|
||||
$item = $class::createItem( Member::loggedIn(), Request::i()->ipAddress(), $date, $container );
|
||||
$this->_createOrUpdate( $item, $itemData );
|
||||
$item->save();
|
||||
|
||||
/* Create post */
|
||||
@@ -77,19 +100,19 @@ abstract class _ItemMutator extends ContentMutator
|
||||
{
|
||||
$this->_addAttachmentsToContent( $postKey, $itemData['content'] );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
catch ( DomainException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/1', 403 );
|
||||
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/1', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
$postContents = \IPS\Text\Parser::parseStatic( $itemData['content'], TRUE, md5( $postKey . ':' ), \IPS\Member::loggedIn(), $class::$application . '_' . mb_ucfirst( $class::$module ) );
|
||||
$postContents = Parser::parseStatic( $itemData['content'], array( md5( $postKey . ':' ) ), Member::loggedIn(), $class::$application . '_' . IPS::mb_ucfirst( $class::$module ) );
|
||||
|
||||
$commentClass = $item::$commentClass;
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, \IPS\Member::loggedIn()->member_id ? NULL : \IPS\Member::loggedIn()->real_name, NULL, \IPS\Member::loggedIn(), $date );
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, Member::loggedIn()->member_id ? NULL : Member::loggedIn()->real_name, NULL, Member::loggedIn(), $date );
|
||||
$itemIdColumn = $item::$databaseColumnId;
|
||||
$postIdColumn = $commentClass::$databaseColumnId;
|
||||
\IPS\File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $post->$postIdColumn );
|
||||
File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $post->$postIdColumn );
|
||||
|
||||
if ( isset( $class::$databaseColumnMap['first_comment_id'] ) )
|
||||
{
|
||||
@@ -101,13 +124,13 @@ abstract class _ItemMutator extends ContentMutator
|
||||
}
|
||||
|
||||
/* Index */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item );
|
||||
Index::i()->index( $item );
|
||||
}
|
||||
|
||||
/* Mark it as read */
|
||||
if( $item instanceof \IPS\Content\ReadMarkers )
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
|
||||
{
|
||||
$item->markRead();
|
||||
}
|
||||
@@ -116,7 +139,7 @@ abstract class _ItemMutator extends ContentMutator
|
||||
if ( !$item->hidden() )
|
||||
{
|
||||
$item->sendNotifications();
|
||||
\IPS\Member::loggedIn()->achievementAction( 'core', 'NewContentItem', $item );
|
||||
Member::loggedIn()->achievementAction( 'core', 'NewContentItem', $item );
|
||||
}
|
||||
elseif( $item->hidden() !== -1 )
|
||||
{
|
||||
@@ -130,13 +153,13 @@ abstract class _ItemMutator extends ContentMutator
|
||||
/**
|
||||
* Create or update item
|
||||
*
|
||||
* @param \IPS\Content\Item $item The item
|
||||
* @param array $itemData Item data
|
||||
* @param string $type add or edit
|
||||
* @param string $postKey Post key
|
||||
* @return \IPS\Content\Item
|
||||
* @param Item $item The item
|
||||
* @param array $itemData Item data
|
||||
* @param string $type add or edit
|
||||
* @param string|null $postKey Post key
|
||||
* @return Item
|
||||
*/
|
||||
protected function _createOrUpdate( \IPS\Content\Item $item, array $itemData=array(), $type='add', $postKey = NULL )
|
||||
protected function _createOrUpdate( Item $item, array $itemData=array(), string $type='add', string|null $postKey = NULL ): Item
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
@@ -148,40 +171,21 @@ abstract class _ItemMutator extends ContentMutator
|
||||
}
|
||||
|
||||
/* Tags */
|
||||
if ( ( isset( $itemData['prefix'] ) or isset( $itemData['tags'] ) ) and \in_array( 'IPS\Content\Tags', class_implements( \get_class( $item ) ) ) )
|
||||
if ( ( isset( $itemData['prefix'] ) or isset( $itemData['tags'] ) ) and IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
|
||||
{
|
||||
if ( \IPS\Member::loggedIn()->member_id && $item::canTag( NULL, $item->containerWrapper() ) )
|
||||
{
|
||||
$source = array();
|
||||
|
||||
if( !\IPS\Settings::i()->tags_open_system )
|
||||
{
|
||||
/* And get the defined tags */
|
||||
if( $class::definedTags( $item->containerWrapper() ) )
|
||||
{
|
||||
$source = array_map( 'trim', array_unique( array_merge( $source, $class::definedTags( $item->containerWrapper() ) ) ) );
|
||||
}
|
||||
}
|
||||
if ( Member::loggedIn()->member_id && $item::canTag( NULL, $item->containerWrapper() ) )
|
||||
{
|
||||
$source = array_map( 'trim', array_unique( $class::definedTags() ) );
|
||||
|
||||
/* Filter our provided tags and exclude any that are invalid */
|
||||
$validTags = array_filter( array_map( 'trim', array_unique( $itemData['tags'] ) ), function($tag) use ($source) {
|
||||
|
||||
if( !\IPS\Settings::i()->tags_open_system && !\in_array($tag, $source) )
|
||||
if( !in_array($tag, $source) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( \IPS\Settings::i()->tags_len_min && \strlen($tag) < \IPS\Settings::i()->tags_len_min )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( \IPS\Settings::i()->tags_len_max && \strlen($tag) < \IPS\Settings::i()->tags_len_max )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if( \strpos( $tag, '#' ) !== FALSE )
|
||||
if( strpos( $tag, '#' ) !== FALSE )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -189,14 +193,14 @@ abstract class _ItemMutator extends ContentMutator
|
||||
return TRUE;
|
||||
});
|
||||
|
||||
if( \IPS\Settings::i()->tags_min && \count( $validTags ) < \IPS\Settings::i()->tags_min )
|
||||
if( Settings::i()->tags_min && count( $validTags ) < Settings::i()->tags_min )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'TOO_FEW_TAGS', 'GQL/0011/1', 400 );
|
||||
throw new SafeException( 'TOO_FEW_TAGS', 'GQL/0011/1', 400 );
|
||||
}
|
||||
|
||||
if( \IPS\Settings::i()->tags_max && \count( $validTags ) > \IPS\Settings::i()->tags_max )
|
||||
if( Settings::i()->tags_max && count( $validTags ) > Settings::i()->tags_max )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'TOO_MANY_TAGS', 'GQL/0011/2', 400 );
|
||||
throw new SafeException( 'TOO_MANY_TAGS', 'GQL/0011/2', 400 );
|
||||
}
|
||||
|
||||
/* we need to save the item before we set the tags because setTags requires that the item exists */
|
||||
@@ -211,14 +215,15 @@ abstract class _ItemMutator extends ContentMutator
|
||||
}
|
||||
|
||||
/* Open/closed */
|
||||
if ( isset( $itemData['state']['locked'] ) and \in_array( 'IPS\Content\Lockable', class_implements( \get_class( $item ) ) ) )
|
||||
/* @var array $databaseColumnMap */
|
||||
if ( isset( $itemData['state']['locked'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['locked'] and $item->canLock() ) or ( !$itemData['state']['locked'] and $item->canUnlock() ) )
|
||||
if ( Member::loggedIn()->member_id && ( $itemData['state']['locked'] and $item->canLock() ) or ( !$itemData['state']['locked'] and $item->canUnlock() ) )
|
||||
{
|
||||
if ( isset( $item::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$lockedColumn = $item::$databaseColumnMap['locked'];
|
||||
$item->$lockedColumn = \intval( $itemData['state']['locked'] );
|
||||
$item->$lockedColumn = intval( $itemData['state']['locked'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -229,9 +234,9 @@ abstract class _ItemMutator extends ContentMutator
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( $itemData['state']['hidden'] ) and \in_array( 'IPS\Content\Hideable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( $itemData['state']['hidden'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['hidden'] and $item->canHide() ) or ( !$itemData['state']['hidden'] and $item->canUnhide() ) )
|
||||
if ( Member::loggedIn()->member_id && ( $itemData['state']['hidden'] and $item->canHide() ) or ( !$itemData['state']['hidden'] and $item->canUnhide() ) )
|
||||
{
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
if ( $itemData['state']['hidden'] )
|
||||
@@ -278,27 +283,27 @@ abstract class _ItemMutator extends ContentMutator
|
||||
}
|
||||
|
||||
/* Pinned */
|
||||
if ( isset( $itemData['state']['pinned'] ) and \in_array( 'IPS\Content\Pinnable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( $itemData['state']['pinned'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['pinned'] and $item->canPin() ) or ( !$itemData['state']['pinned'] and $item->canUnpin() ) )
|
||||
if ( Member::loggedIn()->member_id && ( $itemData['state']['pinned'] and $item->canPin() ) or ( !$itemData['state']['pinned'] and $item->canUnpin() ) )
|
||||
{
|
||||
$pinnedColumn = $item::$databaseColumnMap['pinned'];
|
||||
$item->$pinnedColumn = \intval( $itemData['state']['pinned'] );
|
||||
$item->$pinnedColumn = intval( $itemData['state']['pinned'] );
|
||||
}
|
||||
}
|
||||
|
||||
/* Featured */
|
||||
if ( isset( $itemData['state']['featured'] ) and \in_array( 'IPS\Content\Featurable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( $itemData['state']['featured'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['featured'] and $item->canFeature() ) or ( !$itemData['state']['featured'] and $item->canUnfeature() ) )
|
||||
if ( Member::loggedIn()->member_id && $item->canFeature() )
|
||||
{
|
||||
$featuredColumn = $item::$databaseColumnMap['featured'];
|
||||
$item->$featuredColumn = \intval( $itemData['state']['featured'] );
|
||||
$item->$featuredColumn = intval( $itemData['state']['featured'] );
|
||||
}
|
||||
}
|
||||
|
||||
/* Update first comment if required, and it's not a new item */
|
||||
$field = isset( $item::$databaseColumnMap['first_comment_id'] ) ? $item::$databaseColumnMap['first_comment_id'] : NULL;
|
||||
$field = $item::$databaseColumnMap['first_comment_id'] ?? NULL;
|
||||
$commentClass = $item::$commentClass;
|
||||
$contentField = $commentClass::$databaseColumnMap['content'];
|
||||
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( $itemData[ $contentField ] ) AND $type == 'edit' )
|
||||
@@ -310,30 +315,31 @@ abstract class _ItemMutator extends ContentMutator
|
||||
{
|
||||
$this->_addAttachmentsToContent( $postKey, $itemData[ $contentField ] );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
catch ( DomainException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/2', 403 );
|
||||
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/2', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
$content = \IPS\Text\Parser::parseStatic( $itemData[ $contentField ], TRUE, array( $item->_id, $item->$field ), \IPS\Member::loggedIn(), $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = Parser::parseStatic( $itemData[$contentField], array( $item->_id, $item->$field ), Member::loggedIn(), $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
|
||||
try
|
||||
{
|
||||
/* @var Comment $commentClass */
|
||||
$comment = $commentClass::load( $item->$field );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_FIRST_POST', '1S377/1', 400 );
|
||||
throw new Exception( 'NO_FIRST_POST', '1S377/1', 400 );
|
||||
}
|
||||
|
||||
$comment->$contentField = $content;
|
||||
$comment->save();
|
||||
|
||||
/* Update Search Index of the first item */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,35 +9,52 @@
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
use BadMethodCallException;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Item;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\File;
|
||||
use IPS\IPS;
|
||||
use IPS\Http\Url\Friendly;
|
||||
use IPS\Member;
|
||||
use IPS\Settings;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base mutator class for Content Items
|
||||
*/
|
||||
class _ItemType extends ObjectType
|
||||
class ItemType extends ObjectType
|
||||
{
|
||||
/*
|
||||
* @brief The item classname we use for this type
|
||||
*/
|
||||
protected static $itemClass = '\IPS\Content\Item';
|
||||
protected static string $itemClass = '\IPS\Content\Item';
|
||||
|
||||
/*
|
||||
* @brief GraphQL type name
|
||||
*/
|
||||
protected static $typeName = 'core_Item';
|
||||
protected static string $typeName = 'core_Item';
|
||||
|
||||
/*
|
||||
* @brief GraphQL type description
|
||||
*/
|
||||
protected static $typeDescription = 'A generic content item';
|
||||
protected static string $typeDescription = 'A generic content item';
|
||||
|
||||
|
||||
public function __construct()
|
||||
@@ -58,7 +75,7 @@ class _ItemType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fields()
|
||||
public function fields(): array
|
||||
{
|
||||
return array(
|
||||
'id' => [
|
||||
@@ -83,13 +100,13 @@ class _ItemType extends ObjectType
|
||||
'seoTitle' => [
|
||||
'type' => TypeRegistry::string(),
|
||||
'resolve' => function ($item) {
|
||||
return \IPS\Http\Url\Friendly::seoTitle( $item->mapped('title') );
|
||||
return Friendly::seoTitle( $item->mapped('title') );
|
||||
}
|
||||
],
|
||||
'views' => [
|
||||
'type' => TypeRegistry::int(),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\Views', class_implements( $item ) ) )
|
||||
if ( in_array( 'IPS\Content\ViewUpdates', class_uses( $item ) ) )
|
||||
{
|
||||
return $item->mapped('views');
|
||||
}
|
||||
@@ -117,7 +134,7 @@ class _ItemType extends ObjectType
|
||||
'isLocked' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\Lockable', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
return (bool) $item->locked();
|
||||
}
|
||||
@@ -128,7 +145,7 @@ class _ItemType extends ObjectType
|
||||
'isPinned' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\Pinnable', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
return (bool) $item->mapped('pinned');
|
||||
}
|
||||
@@ -139,7 +156,7 @@ class _ItemType extends ObjectType
|
||||
'isFeatured' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\Featurable', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
return (bool) $item->mapped('featured');
|
||||
}
|
||||
@@ -153,7 +170,7 @@ class _ItemType extends ObjectType
|
||||
'values' => ['HIDDEN', 'PENDING', 'DELETED']
|
||||
]),
|
||||
'resolve' => function ($item) {
|
||||
if ( !\in_array( 'IPS\Content\Hideable', class_implements( $item ) ) )
|
||||
if ( !IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
switch( $item->hidden() ){
|
||||
case -2:
|
||||
@@ -166,6 +183,7 @@ class _ItemType extends ObjectType
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
],
|
||||
'updated' => [
|
||||
@@ -183,7 +201,7 @@ class _ItemType extends ObjectType
|
||||
'isUnread' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\ReadMarkers', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
|
||||
{
|
||||
return $item->unread();
|
||||
}
|
||||
@@ -201,7 +219,7 @@ class _ItemType extends ObjectType
|
||||
'type' => TypeRegistry::int(),
|
||||
'description' => 'Returns the position of the comment that is the first unread in this topic',
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\ReadMarkers', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
|
||||
{
|
||||
return static::getUnreadPosition($item);
|
||||
}
|
||||
@@ -222,7 +240,8 @@ class _ItemType extends ObjectType
|
||||
'follow' => [
|
||||
'type' => TypeRegistry::follow(),
|
||||
'resolve' => function ($item) {
|
||||
if( \in_array( 'IPS\Content\Followable', class_implements( $item ) ) && isset( static::$followData ) && \is_array( static::$followData ) ){
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\Followable' ) && isset( static::$followData ) && is_array( static::$followData ) )
|
||||
{
|
||||
$idColumn = static::getIdColumn($item);
|
||||
return array_merge( static::$followData, array(
|
||||
'id' => $item->$idColumn,
|
||||
@@ -237,7 +256,7 @@ class _ItemType extends ObjectType
|
||||
'tags' => [
|
||||
'type' => TypeRegistry::listOf( \IPS\core\api\GraphQL\TypeRegistry::tag() ),
|
||||
'resolve' => function ($item) {
|
||||
if ( \in_array( 'IPS\Content\Tags', class_implements( $item ) ) )
|
||||
if ( IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
|
||||
{
|
||||
return static::tags($item);
|
||||
}
|
||||
@@ -273,13 +292,13 @@ class _ItemType extends ObjectType
|
||||
'hasPoll' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
return $item instanceof \IPS\Content\Polls && $item->poll_state;
|
||||
return IPS::classUsesTrait( $item, 'IPS\Content\Polls' ) AND $item->poll_state;
|
||||
}
|
||||
],
|
||||
'poll' => [
|
||||
'type' => \IPS\core\api\GraphQL\TypeRegistry::poll(),
|
||||
'resolve' => function ($item) {
|
||||
if( $item instanceof \IPS\Content\Polls && $item->poll_state )
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\Polls' ) AND $item->poll_state )
|
||||
{
|
||||
return $item->getPoll();
|
||||
}
|
||||
@@ -290,7 +309,9 @@ class _ItemType extends ObjectType
|
||||
'firstCommentRequired' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'resolve' => function ($item) {
|
||||
return static::$itemClass::$firstCommentRequired;
|
||||
/* @var Item $className */
|
||||
$className = get_class( $item );
|
||||
return $className::$firstCommentRequired;
|
||||
}
|
||||
],
|
||||
'articleLang' => [
|
||||
@@ -309,17 +330,19 @@ class _ItemType extends ObjectType
|
||||
]
|
||||
],
|
||||
'resolveField' => function ($item, $args, $context, $info) {
|
||||
$className = \get_class( $item );
|
||||
/* @var Item $className */
|
||||
$className = get_class( $item );
|
||||
|
||||
switch( $info->fieldName )
|
||||
{
|
||||
case 'indefinite':
|
||||
return $className::_indefiniteArticle( NULL );
|
||||
break;
|
||||
return $className::_indefiniteArticle();
|
||||
|
||||
case 'definite':
|
||||
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
|
||||
break;
|
||||
|
||||
}
|
||||
return '';
|
||||
}
|
||||
]),
|
||||
'resolve' => function ($item) {
|
||||
@@ -376,11 +399,12 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Return the ID column for the provided item
|
||||
*
|
||||
* @return array
|
||||
* @param Item $item
|
||||
* @return string
|
||||
*/
|
||||
protected function getIdColumn($item)
|
||||
protected function getIdColumn(Item $item): string
|
||||
{
|
||||
$className = \get_class( $item );
|
||||
$className = get_class( $item );
|
||||
return $className::$databaseColumnId;
|
||||
}
|
||||
|
||||
@@ -389,7 +413,7 @@ class _ItemType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function args()
|
||||
public static function args(): array
|
||||
{
|
||||
return array(
|
||||
'offset' => [
|
||||
@@ -428,7 +452,7 @@ class _ItemType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOrderByOptions()
|
||||
public static function getOrderByOptions(): array
|
||||
{
|
||||
return array('title', 'author_name', 'last_comment_name');
|
||||
}
|
||||
@@ -438,42 +462,42 @@ class _ItemType extends ObjectType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getItemPermissionFields()
|
||||
public static function getItemPermissionFields(): array
|
||||
{
|
||||
return array(
|
||||
'canComment' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the logged in user comment on this item?',
|
||||
'resolve' => function ($item, $args, $context) {
|
||||
return $item->canComment( \IPS\Member::loggedIn(), FALSE );
|
||||
return $item->canComment( Member::loggedIn(), FALSE );
|
||||
}
|
||||
],
|
||||
'commentInformation' => [
|
||||
'type' => TypeRegistry::string(),
|
||||
'description' => 'A message providing some information about the comment form availability',
|
||||
'resolve' => function ($item, $args, $context) {
|
||||
if( $item->canComment( \IPS\Member::loggedIn(), FALSE ) )
|
||||
if( $item->canComment( Member::loggedIn(), FALSE ) )
|
||||
{
|
||||
if( $item instanceof \IPS\Content\Lockable && $item->locked() )
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) AND $item->locked() )
|
||||
{
|
||||
return 'locked_can_comment';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( $item instanceof \IPS\Content\Lockable && $item->locked() )
|
||||
if( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) AND $item->locked() )
|
||||
{
|
||||
return 'locked_cannot_comment';
|
||||
}
|
||||
elseif( \IPS\Member::loggedIn()->restrict_post )
|
||||
elseif( Member::loggedIn()->restrict_post )
|
||||
{
|
||||
return 'restricted_cannot_comment';
|
||||
}
|
||||
elseif( \IPS\Member::loggedIn()->members_bitoptions['unacknowledged_warnings'] )
|
||||
elseif( Member::loggedIn()->members_bitoptions['unacknowledged_warnings'] )
|
||||
{
|
||||
return 'unacknowledged_warning_cannot_post';
|
||||
}
|
||||
elseif( !\IPS\Member::loggedIn()->checkPostsPerDay() )
|
||||
elseif( !Member::loggedIn()->checkPostsPerDay() )
|
||||
{
|
||||
return 'member_exceeded_posts_per_day';
|
||||
}
|
||||
@@ -485,11 +509,11 @@ class _ItemType extends ObjectType
|
||||
'canCommentIfSignedIn' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Returns boolean indicating whether a guest who signs in would be able to comment on this item. Returns NULL if already signed in.',
|
||||
'resolve' => function ($item, $args, $context) {
|
||||
if ( !\IPS\Member::loggedIn()->member_id )
|
||||
'resolve' => function ($item) {
|
||||
if ( !Member::loggedIn()->member_id )
|
||||
{
|
||||
$testUser = new \IPS\Member;
|
||||
$testUser->member_group_id = \IPS\Settings::i()->member_group;
|
||||
$testUser = new Member;
|
||||
$testUser->member_group_id = Settings::i()->member_group;
|
||||
|
||||
return $item->canComment( $testUser, FALSE );
|
||||
}
|
||||
@@ -501,21 +525,21 @@ class _ItemType extends ObjectType
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Boolean indicating whether this item supports read markers, and if so, if the user can mark as read',
|
||||
'resolve' => function ($item) {
|
||||
return $item instanceof \IPS\Content\ReadMarkers && \IPS\Member::loggedIn()->member_id;
|
||||
return IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) && Member::loggedIn()->member_id;
|
||||
}
|
||||
],
|
||||
'canReport' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the user report this item?',
|
||||
'resolve' => function ($item) {
|
||||
return $item->canReport( \IPS\Member::loggedIn() ) === TRUE;
|
||||
return $item->canReport( Member::loggedIn() ) === TRUE;
|
||||
}
|
||||
],
|
||||
'canReportOrRevoke' => [
|
||||
'type' => TypeRegistry::boolean(),
|
||||
'description' => 'Can the user report (or revoke a report) on this item?',
|
||||
'resolve' => function ($item) {
|
||||
return $item->canReportOrRevoke( \IPS\Member::loggedIn() ) === TRUE;
|
||||
return $item->canReportOrRevoke( Member::loggedIn() ) === TRUE;
|
||||
}
|
||||
],
|
||||
'canShare' => [
|
||||
@@ -533,7 +557,7 @@ class _ItemType extends ObjectType
|
||||
*
|
||||
* @return ObjectType
|
||||
*/
|
||||
protected static function getCommentType()
|
||||
protected static function getCommentType(): ObjectType
|
||||
{
|
||||
return \IPS\Content\Api\GraphQL\TypeRegistry::comment();
|
||||
}
|
||||
@@ -541,25 +565,27 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Return content images for the provided item
|
||||
*
|
||||
* @return array|null
|
||||
* @param Item $item
|
||||
* @return array|null
|
||||
*/
|
||||
protected static function contentImages($item)
|
||||
protected static function contentImages(Item $item): array|NULL
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( $images = $item->contentImages( 20 ) )
|
||||
{
|
||||
$toReturn = [];
|
||||
foreach( $images as $image )
|
||||
{
|
||||
foreach( $image as $extension => $file )
|
||||
{
|
||||
$toReturn[] = (string) \IPS\File::get( $extension, $file )->url;
|
||||
$toReturn[] = (string) File::get( $extension, $file )->url;
|
||||
}
|
||||
}
|
||||
return $toReturn;
|
||||
}
|
||||
}
|
||||
catch( \BadMethodCallException $e ) { }
|
||||
catch( BadMethodCallException $e ) { }
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -567,20 +593,25 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the findCommentPosition field
|
||||
*
|
||||
* @param \IPS\forums\Topic
|
||||
* @param array Arguments passed to this resolver
|
||||
* @return array
|
||||
* @param Item $item
|
||||
* @param array $args Arguments passed to this resolver
|
||||
* @return int|NULL
|
||||
*/
|
||||
protected static function findCommentPosition($item, $args)
|
||||
protected static function findCommentPosition(Item $item, array $args): int|NULL
|
||||
{
|
||||
if( $args['findComment'] === NULL )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* @var Item $itemClass */
|
||||
$itemClass = get_class( $item );
|
||||
|
||||
try
|
||||
{
|
||||
$comment = static::$itemClass::$commentClass::load( $args['findComment'] );
|
||||
/* @var Comment $commentClass */
|
||||
$commentClass = $itemClass::$commentClass;
|
||||
$comment = $commentClass::load( $args['findComment'] );
|
||||
|
||||
// Check this comment belongs to this topic
|
||||
if( $comment->item() !== $item )
|
||||
@@ -588,7 +619,7 @@ class _ItemType extends ObjectType
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
catch (\Exception $e)
|
||||
catch (Exception $e)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -599,12 +630,15 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the comments field
|
||||
*
|
||||
* @param \IPS\forums\Topic
|
||||
* @param array Arguments passed to this resolver
|
||||
* @return array
|
||||
* @param Item $item
|
||||
* @param array $args Arguments passed to this resolver
|
||||
* @return array
|
||||
*/
|
||||
protected static function comments($item, $args)
|
||||
protected static function comments(Item $item, array $args): array
|
||||
{
|
||||
/* @var Item $itemClass */
|
||||
$itemClass = get_class( $item );
|
||||
|
||||
$offset = 0;
|
||||
$limit = 25;
|
||||
|
||||
@@ -621,7 +655,7 @@ class _ItemType extends ObjectType
|
||||
case 'ID':
|
||||
if( !isset( $args['findComment'] ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
$offset = static::getCommentPosition($item, $args['findComment']) + $args['offsetAdjust'];
|
||||
@@ -637,7 +671,10 @@ class _ItemType extends ObjectType
|
||||
|
||||
if( $args['orderBy'] == 'DATE' )
|
||||
{
|
||||
$args['orderBy'] = static::$itemClass::$commentClass::$databaseColumnMap['date'];
|
||||
/* @var Comment $commentClass */
|
||||
/* @var array $databaseColumnMap */
|
||||
$commentClass = $itemClass::$commentClass;
|
||||
$args['orderBy'] = $commentClass::$databaseColumnMap['date'];
|
||||
}
|
||||
|
||||
|
||||
@@ -653,27 +690,35 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Get the position of a specific comment
|
||||
*
|
||||
* @param \IPS\Content\Item
|
||||
* @return int
|
||||
* @param Item $item
|
||||
* @param int $commentID
|
||||
* @return int|null
|
||||
*/
|
||||
protected static function getCommentPosition($item, $commentID)
|
||||
protected static function getCommentPosition(Item $item, int $commentID): ?int
|
||||
{
|
||||
$itemClass = get_class( $item );
|
||||
|
||||
/* @var Comment $commentClass */
|
||||
$commentClass = $itemClass::$commentClass;
|
||||
|
||||
try
|
||||
{
|
||||
$comment = static::$itemClass::$commentClass::load($commentID);
|
||||
$comment = $commentClass::load($commentID);
|
||||
return static::findComment($comment, $item);
|
||||
}
|
||||
catch(\Exception $error)
|
||||
catch(Exception $error)
|
||||
{}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position of the last comment
|
||||
*
|
||||
* @param \IPS\Content\Item
|
||||
* @param Item $item
|
||||
* @return int
|
||||
*/
|
||||
protected static function getEndPosition($item)
|
||||
protected static function getEndPosition(Item $item): int
|
||||
{
|
||||
$comment = $item->comments( 1, NULL, 'date', 'desc' );
|
||||
return static::findComment($comment, $item);
|
||||
@@ -682,20 +727,21 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Get the position of the first unread comment
|
||||
*
|
||||
* @param \IPS\Content\Item
|
||||
* @param Item $item
|
||||
* @return int
|
||||
*/
|
||||
protected static function getUnreadPosition($item)
|
||||
protected static function getUnreadPosition(Item $item): int
|
||||
{
|
||||
try
|
||||
{
|
||||
{
|
||||
/* @var Item $class */
|
||||
$class = static::$itemClass;
|
||||
$timeLastRead = $item->timeLastRead();
|
||||
|
||||
if ( $timeLastRead instanceof \IPS\DateTime )
|
||||
if ( $timeLastRead instanceof DateTime )
|
||||
{
|
||||
$comment = NULL;
|
||||
if( \IPS\DateTime::ts( $item->mapped('date') ) < $timeLastRead )
|
||||
if( DateTime::ts( $item->mapped('date') ) < $timeLastRead )
|
||||
{
|
||||
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, $timeLastRead );
|
||||
}
|
||||
@@ -724,11 +770,11 @@ class _ItemType extends ObjectType
|
||||
if ( $item->unread() )
|
||||
{
|
||||
/* If we do not have a time last read set for this content, fallback to the reset time */
|
||||
$resetTimes = \IPS\Member::loggedIn()->markersResetTimes( $class::$application );
|
||||
$resetTimes = Member::loggedIn()->markersResetTimes( $class::$application );
|
||||
|
||||
if ( array_key_exists( $item->container()->_id, $resetTimes ) and $item->mapped('date') < $resetTimes[ $item->container()->_id ] )
|
||||
{
|
||||
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, \IPS\DateTime::ts( $resetTimes[ $item->container()->_id ] ) );
|
||||
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, DateTime::ts( $resetTimes[ $item->container()->_id ] ) );
|
||||
|
||||
if ( !$comment || $class::$firstCommentRequired and $comment->isFirst() )
|
||||
{
|
||||
@@ -748,7 +794,7 @@ class _ItemType extends ObjectType
|
||||
|
||||
return static::findComment($comment, $item);
|
||||
}
|
||||
catch( \Exception $e )
|
||||
catch( Exception $e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -757,20 +803,21 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Find the position of a comment
|
||||
*
|
||||
* @param \IPS\Content\Comment
|
||||
* @param \IPS\Content\Item
|
||||
* @param Comment $comment
|
||||
* @param Item $item
|
||||
* @return int
|
||||
*/
|
||||
protected static function findComment($comment, $item)
|
||||
protected static function findComment(Comment $comment, Item $item): int
|
||||
{
|
||||
try
|
||||
{
|
||||
$commentClass = \get_class( $comment );
|
||||
/* @var array $databaseColumnMap */
|
||||
$commentClass = get_class( $comment );
|
||||
$idColumn = $commentClass::$databaseColumnId;
|
||||
$itemColumn = $commentClass::$databaseColumnMap['item'];
|
||||
|
||||
/* Work out where the comment is in the item */
|
||||
$directional = ( \in_array( 'IPS\Content\Review', class_parents( $commentClass ) ) ) ? '>=?' : '<=?';
|
||||
$directional = ( in_array( 'IPS\Content\Review', class_parents( $commentClass ) ) ) ? '>=?' : '<=?';
|
||||
$where = array(
|
||||
array( $commentClass::$databasePrefix . $itemColumn . '=?', $comment->$itemColumn ),
|
||||
array( $commentClass::$databasePrefix . $idColumn . $directional, $comment->$idColumn )
|
||||
@@ -804,15 +851,17 @@ class _ItemType extends ObjectType
|
||||
}
|
||||
}
|
||||
}
|
||||
$commentPosition = \IPS\Db::i()->select( 'COUNT(*) AS position', $commentClass::$databaseTable, $where )->first();
|
||||
$commentPosition = Db::i()->select( 'COUNT(*) AS position', $commentClass::$databaseTable, $where )->first();
|
||||
|
||||
if( static::$itemClass::$firstCommentRequired ){
|
||||
/* @var Item $itemClass */
|
||||
$itemClass = get_class( $item );
|
||||
if( $itemClass::$firstCommentRequired ){
|
||||
$commentPosition = $commentPosition - 1;
|
||||
}
|
||||
|
||||
return $commentPosition;
|
||||
}
|
||||
catch( \Exception $e )
|
||||
catch( Exception $e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -821,15 +870,15 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the tags field
|
||||
*
|
||||
* @param \IPS\Content\Item
|
||||
* @return int
|
||||
* @param Item $item
|
||||
* @return int|null
|
||||
*/
|
||||
protected static function timeLastRead($item)
|
||||
protected static function timeLastRead(Item $item): ?int
|
||||
{
|
||||
$time = $item->timeLastRead();
|
||||
if( $time instanceof \IPS\DateTime )
|
||||
if( $time instanceof DateTime )
|
||||
{
|
||||
return (int) $time->setTimezone( new \DateTimeZone( "UTC" ) )->format('U');
|
||||
return (int) $time->setTimezone( new DateTimeZone( "UTC" ) )->format('U');
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -837,23 +886,22 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the tags field
|
||||
*
|
||||
* @param \IPS\Content\Item
|
||||
* @param Item $item
|
||||
* @return array
|
||||
*/
|
||||
protected static function tags($item)
|
||||
protected static function tags(Item $item): array
|
||||
{
|
||||
$tags = $item->tags();
|
||||
return \is_array( $tags ) ? $tags : array();
|
||||
return $item->tags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the author field
|
||||
*
|
||||
* @param null
|
||||
* @param array Arguments passed to this resolver
|
||||
* @return \IPS\forums\Forum
|
||||
* @param Item $item
|
||||
* @param array $args Arguments passed to this resolver
|
||||
* @return Member
|
||||
*/
|
||||
protected static function author($item, $args)
|
||||
protected static function author(Item $item, array $args): Member
|
||||
{
|
||||
return $item->author();
|
||||
}
|
||||
@@ -861,11 +909,11 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the last comment author field
|
||||
*
|
||||
* @param \IPS\forums\Topic
|
||||
* @param array Arguments passed to this resolver
|
||||
* @return \IPS\Member
|
||||
* @param Item $item
|
||||
* @param array $args Arguments passed to this resolver
|
||||
* @return Member
|
||||
*/
|
||||
protected static function lastCommentAuthor($item, $args)
|
||||
protected static function lastCommentAuthor(Item $item, array $args): Member
|
||||
{
|
||||
if( $item->mapped('num_comments') )
|
||||
{
|
||||
@@ -878,11 +926,11 @@ class _ItemType extends ObjectType
|
||||
/**
|
||||
* Resolve the last comment date field
|
||||
*
|
||||
* @param \IPS\forums\Topic
|
||||
* @param array Arguments passed to this resolver
|
||||
* @param Item $item
|
||||
* @param array $args Arguments passed to this resolver
|
||||
* @return string
|
||||
*/
|
||||
protected static function lastCommentDate($item, $args)
|
||||
protected static function lastCommentDate(Item $item, array $args): string
|
||||
{
|
||||
if( $item->mapped('last_comment') )
|
||||
{
|
||||
|
||||
@@ -10,23 +10,23 @@
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Api\GraphQL;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use function defined;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* \IPS\Content base types
|
||||
*/
|
||||
class _TypeRegistry
|
||||
class TypeRegistry
|
||||
{
|
||||
protected static $comment;
|
||||
protected static $content;
|
||||
protected static $item;
|
||||
protected static CommentType $comment;
|
||||
protected static ContentType $content;
|
||||
protected static ItemType $item;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -39,24 +39,24 @@ class _TypeRegistry
|
||||
/**
|
||||
* @return CommentType
|
||||
*/
|
||||
public static function comment()
|
||||
public static function comment(): CommentType
|
||||
{
|
||||
return self::$comment ?: (self::$comment = new \IPS\Content\Api\GraphQL\CommentType());
|
||||
return self::$comment ?? (self::$comment = new CommentType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ItemType
|
||||
*/
|
||||
public static function item()
|
||||
public static function item(): ItemType
|
||||
{
|
||||
return self::$item ?: (self::$item = new \IPS\Content\Api\GraphQL\ItemType());
|
||||
return self::$item ?? (self::$item = new ItemType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ContentType
|
||||
*/
|
||||
public static function content()
|
||||
public static function content(): ContentType
|
||||
{
|
||||
return self::$content ?: (self::$content = new \IPS\Content\Api\GraphQL\ContentType());
|
||||
return self::$content ?? (self::$content = new ContentType());
|
||||
}
|
||||
}
|
||||
@@ -12,18 +12,44 @@ namespace IPS\Content\Api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
|
||||
use BadMethodCallException;
|
||||
use DateInterval;
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Api\Webhook;
|
||||
use IPS\Content\Comment;
|
||||
use IPS\Content\Filter;
|
||||
use IPS\Content\Item;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Content\Search\SearchContent;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Poll;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
use function in_array;
|
||||
use function intval;
|
||||
use function substr;
|
||||
|
||||
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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base API endpoint for Content Items
|
||||
*/
|
||||
class _ItemController extends \IPS\Api\Controller
|
||||
class ItemController extends Controller
|
||||
{
|
||||
/**
|
||||
* List
|
||||
@@ -31,36 +57,37 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* @param array $where Extra WHERE clause
|
||||
* @param string $containerParam The parameter which includes the container values
|
||||
* @param bool $byPassPerms If permissions should be ignored
|
||||
* @param string $customSort Custom sort by parameter to use
|
||||
* @return \IPS\Api\PaginatedResponse
|
||||
* @param string|null $customSort Custom sort by parameter to use
|
||||
* @return PaginatedResponse
|
||||
*/
|
||||
protected function _list( $where = array(), $containerParam = 'categories', $byPassPerms = FALSE, $customSort = NULL )
|
||||
protected function _list( array $where = array(), string $containerParam = 'categories', bool $byPassPerms = FALSE, string|null $customSort = NULL ): PaginatedResponse
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
$class = $this->class;
|
||||
|
||||
/* Containers */
|
||||
if ( $containerParam and isset( \IPS\Request::i()->$containerParam ) )
|
||||
if ( $containerParam and isset( Request::i()->$containerParam ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->$containerParam ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnMap['container'], array_map( 'intval', array_filter( explode( ',', Request::i()->$containerParam ) ) ) ) );
|
||||
}
|
||||
|
||||
/* IDs */
|
||||
if ( isset( \IPS\Request::i()->ids ) )
|
||||
if ( isset( Request::i()->ids ) )
|
||||
{
|
||||
$idField = $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnId;
|
||||
$where[] = array(\IPS\Db::i()->in( $idField, array_map( 'intval', explode(',', \IPS\Request::i()->ids ) ) ) );
|
||||
$where[] = array(Db::i()->in( $idField, array_map( 'intval', explode(',', Request::i()->ids ) ) ) );
|
||||
}
|
||||
|
||||
/* Authors */
|
||||
if ( isset( \IPS\Request::i()->authors ) )
|
||||
if ( isset( Request::i()->authors ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', \IPS\Request::i()->authors ) ) ) ) );
|
||||
$where[] = array( Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['author'], array_map( 'intval', array_filter( explode( ',', Request::i()->authors ) ) ) ) );
|
||||
}
|
||||
|
||||
/* Pinned? */
|
||||
if ( isset( \IPS\Request::i()->pinned ) AND \in_array( 'IPS\Content\Pinnable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->pinned ) AND IPS::classUsesTrait( $class, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->pinned )
|
||||
if ( Request::i()->pinned )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['pinned'] . "=1" );
|
||||
}
|
||||
@@ -71,9 +98,9 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Featured? */
|
||||
if ( isset( \IPS\Request::i()->featured ) AND \in_array( 'IPS\Content\Featurable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->featured ) AND IPS::classUsesTrait( $class, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->featured )
|
||||
if ( Request::i()->featured )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['featured'] . "=1" );
|
||||
}
|
||||
@@ -84,22 +111,22 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Locked? */
|
||||
if ( isset( \IPS\Request::i()->locked ) AND \in_array( 'IPS\Content\Lockable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->locked ) AND IPS::classUsesTrait( $class, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['locked'] . '=?', \intval( \IPS\Request::i()->locked ) );
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['locked'] . '=?', intval( Request::i()->locked ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['state'] . '=?', \IPS\Request::i()->locked ? 'closed' : 'open' );
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['state'] . '=?', Request::i()->locked ? 'closed' : 'open' );
|
||||
}
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( \IPS\Request::i()->hidden ) AND \in_array( 'IPS\Content\Hideable', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->hidden ) AND IPS::classUSesTrait( $class, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hidden )
|
||||
if ( Request::i()->hidden )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['hidden'] ) )
|
||||
{
|
||||
@@ -124,9 +151,9 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Has poll? */
|
||||
if ( isset( \IPS\Request::i()->hasPoll ) AND \in_array( 'IPS\Content\Polls', class_implements( $class ) ) )
|
||||
if ( isset( Request::i()->hasPoll ) AND IPS::classUsesTrait( $class, 'IPS\Content\Polls' ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hasPoll )
|
||||
if ( Request::i()->hasPoll )
|
||||
{
|
||||
$where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['poll'] . ">0" );
|
||||
}
|
||||
@@ -147,21 +174,21 @@ class _ItemController extends \IPS\Api\Controller
|
||||
{
|
||||
$sortBy = $customSort;
|
||||
}
|
||||
elseif ( isset( \IPS\Request::i()->sortBy ) and \in_array( \IPS\Request::i()->sortBy, $supportedSortBy ) )
|
||||
elseif ( isset( Request::i()->sortBy ) and in_array( Request::i()->sortBy, $supportedSortBy ) )
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ \IPS\Request::i()->sortBy ];
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnMap[ Request::i()->sortBy ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$sortBy = $class::$databasePrefix . $class::$databaseColumnId;
|
||||
}
|
||||
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and \in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
|
||||
$sortDir = ( isset( Request::i()->sortDir ) and in_array( mb_strtolower( Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? Request::i()->sortDir : 'asc';
|
||||
|
||||
/* Get results */
|
||||
if ( $this->member and !$byPassPerms )
|
||||
{
|
||||
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
|
||||
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', \IPS\Content\Hideable::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
|
||||
$query = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', Filter::FILTER_AUTOMATIC, 0, $this->member )->getInnerIterator();
|
||||
$count = $class::getItemsWithPermission( $where, "{$sortBy} {$sortDir}", NULL, 'view', Filter::FILTER_AUTOMATIC, 0, $this->member, FALSE, FALSE, FALSE, TRUE );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -177,19 +204,19 @@ class _ItemController extends \IPS\Api\Controller
|
||||
$where[] = array( "{$col}!=-2 AND {$col}!=-3" );
|
||||
}
|
||||
|
||||
$query = \IPS\Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" );
|
||||
$count = \IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first();
|
||||
$query = Db::i()->select( '*', $class::$databaseTable, $where, "{$sortBy} {$sortDir}" );
|
||||
$count = Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first();
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
$query,
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
$class,
|
||||
$count,
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
@@ -197,58 +224,52 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* View
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @return \IPS\Api\Response
|
||||
* @return Response
|
||||
*/
|
||||
protected function _view( $id )
|
||||
protected function _view( int $id ): Response
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
$item = $class::load( $id );
|
||||
if( $this->member )
|
||||
if ( $this->member and !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
if ( method_exists($item, 'canView') and !$item->canView( $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
else if ( !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $item->apiOutput( $this->member ) );
|
||||
return new Response( 200, $item->apiOutput( $this->member ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update item
|
||||
*
|
||||
* @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
|
||||
{
|
||||
/* @var array $databaseColumnMap */
|
||||
/* Title */
|
||||
if ( isset( \IPS\Request::i()->title ) and isset( $item::$databaseColumnMap['title'] ) )
|
||||
if ( isset( Request::i()->title ) and isset( $item::$databaseColumnMap['title'] ) )
|
||||
{
|
||||
$titleColumn = $item::$databaseColumnMap['title'];
|
||||
$item->$titleColumn = \IPS\Request::i()->title;
|
||||
$item->$titleColumn = Request::i()->title;
|
||||
}
|
||||
|
||||
/* Tags */
|
||||
if ( ( isset( \IPS\Request::i()->prefix ) or isset( \IPS\Request::i()->tags ) ) and \in_array( 'IPS\Content\Tags', class_implements( \get_class( $item ) ) ) )
|
||||
if ( ( isset( Request::i()->prefix ) or isset( Request::i()->tags ) ) and IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
|
||||
{
|
||||
if ( !$this->member or $item::canTag( $this->member, $item->containerWrapper() ) )
|
||||
{
|
||||
$tags = isset( \IPS\Request::i()->tags ) ? array_filter( explode( ',', \IPS\Request::i()->tags ) ) : $item->tags();
|
||||
$tags = isset( Request::i()->tags ) ? array_filter( explode( ',', Request::i()->tags ) ) : $item->tags();
|
||||
|
||||
if ( !$this->member or $item::canPrefix( $this->member, $item->containerWrapper() ) )
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->prefix ) )
|
||||
if ( isset( Request::i()->prefix ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->prefix )
|
||||
if ( Request::i()->prefix )
|
||||
{
|
||||
$tags['prefix'] = \IPS\Request::i()->prefix;
|
||||
$tags['prefix'] = Request::i()->prefix;
|
||||
}
|
||||
}
|
||||
elseif ( $existingPrefix = $item->prefix() )
|
||||
@@ -269,30 +290,30 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Open/closed */
|
||||
if ( isset( \IPS\Request::i()->locked ) and \in_array( 'IPS\Content\Lockable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->locked ) and IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->locked and $item->canLock( $this->member ) ) or ( !\IPS\Request::i()->locked and $item->canUnlock( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->locked and $item->canLock( $this->member ) ) or ( !Request::i()->locked and $item->canUnlock( $this->member ) ) )
|
||||
{
|
||||
if ( isset( $item::$databaseColumnMap['locked'] ) )
|
||||
{
|
||||
$lockedColumn = $item::$databaseColumnMap['locked'];
|
||||
$item->$lockedColumn = \intval( \IPS\Request::i()->locked );
|
||||
$item->$lockedColumn = intval( Request::i()->locked );
|
||||
}
|
||||
else
|
||||
{
|
||||
$stateColumn = $item::$databaseColumnMap['status'];
|
||||
$item->$stateColumn = \IPS\Request::i()->locked ? 'closed' : 'open';
|
||||
$item->$stateColumn = Request::i()->locked ? 'closed' : 'open';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Hidden */
|
||||
if ( isset( \IPS\Request::i()->hidden ) and \in_array( 'IPS\Content\Hideable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->hidden ) and IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->hidden and $item->canHide( $this->member ) ) or ( !\IPS\Request::i()->hidden and $item->canUnhide( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->hidden and $item->canHide( $this->member ) ) or ( !Request::i()->hidden and $item->canUnhide( $this->member ) ) )
|
||||
{
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
if ( \IPS\Request::i()->hidden AND $item->hidden() != -1 )
|
||||
if ( Request::i()->hidden AND $item->hidden() != -1 )
|
||||
{
|
||||
if ( $item->$idColumn )
|
||||
{
|
||||
@@ -303,17 +324,17 @@ class _ItemController extends \IPS\Api\Controller
|
||||
if ( isset( $item::$databaseColumnMap['hidden'] ) )
|
||||
{
|
||||
$hiddenColumn = $item::$databaseColumnMap['hidden'];
|
||||
$item->$hiddenColumn = \IPS\Request::i()->hidden;
|
||||
$item->$hiddenColumn = Request::i()->hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
$approvedColumn = $item::$databaseColumnMap['approved'];
|
||||
$item->$approvedColumn = ( \IPS\Request::i()->hidden == -1 ) ? -1 : 0;
|
||||
$item->$approvedColumn = ( Request::i()->hidden == -1 ) ? -1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !\IPS\Request::i()->hidden AND $item->hidden() == -1 )
|
||||
if ( !Request::i()->hidden AND $item->hidden() == -1 )
|
||||
{
|
||||
if ( $item->$idColumn )
|
||||
{
|
||||
@@ -337,27 +358,27 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Pinned */
|
||||
if ( isset( \IPS\Request::i()->pinned ) and \in_array( 'IPS\Content\Pinnable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->pinned ) and IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->pinned and $item->canPin( $this->member ) ) or ( !\IPS\Request::i()->pinned and $item->canUnpin( $this->member ) ) )
|
||||
if ( !$this->member or ( Request::i()->pinned and $item->canPin( $this->member ) ) or ( !Request::i()->pinned and $item->canUnpin( $this->member ) ) )
|
||||
{
|
||||
$pinnedColumn = $item::$databaseColumnMap['pinned'];
|
||||
$item->$pinnedColumn = \intval( \IPS\Request::i()->pinned );
|
||||
$item->$pinnedColumn = intval( Request::i()->pinned );
|
||||
}
|
||||
}
|
||||
|
||||
/* Featured */
|
||||
if ( isset( \IPS\Request::i()->featured ) and \in_array( 'IPS\Content\Featurable', class_implements( \get_class( $item ) ) ) )
|
||||
if ( isset( Request::i()->featured ) and IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
|
||||
{
|
||||
if ( !$this->member or ( \IPS\Request::i()->featured and $item->canFeature( $this->member ) ) or ( !\IPS\Request::i()->featured and $item->canUnfeature( $this->member ) ) )
|
||||
if ( !$this->member or $item->canFeature( $this->member ) )
|
||||
{
|
||||
$featuredColumn = $item::$databaseColumnMap['featured'];
|
||||
$item->$featuredColumn = \intval( \IPS\Request::i()->featured );
|
||||
$item->$featuredColumn = intval( Request::i()->featured );
|
||||
}
|
||||
}
|
||||
|
||||
/** We intentionally allow anonymous content in nodes where it's possible (but where it's disabled via the settings */
|
||||
if( isset( \IPS\Request::i()->anonymous ) AND \in_array( 'IPS\Content\Anonymous', class_implements( \get_class( $item ) ) ) )
|
||||
if( isset( Request::i()->anonymous ) AND IPS::classUsesTrait( $item, 'IPS\Content\Anonymous' ) )
|
||||
{
|
||||
/* we need to save the item before we set the anonymous data */
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
@@ -367,40 +388,41 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
try
|
||||
{
|
||||
$item->setAnonymous( (bool) \IPS\Request::i()->anonymous, $item->author() );
|
||||
$item->setAnonymous( (bool) Request::i()->anonymous, $item->author() );
|
||||
}
|
||||
catch ( \BadMethodCallException $e ){}
|
||||
catch ( BadMethodCallException $e ){}
|
||||
|
||||
}
|
||||
|
||||
/* Update first comment if required, and it's not a new item */
|
||||
$field = isset( $item::$databaseColumnMap['first_comment_id'] ) ? $item::$databaseColumnMap['first_comment_id'] : NULL;
|
||||
$field = $item::$databaseColumnMap['first_comment_id'] ?? NULL;
|
||||
$commentClass = $item::$commentClass;
|
||||
$contentField = $commentClass::$databaseColumnMap['content'];
|
||||
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( \IPS\Request::i()->$contentField ) AND $type == 'edit' )
|
||||
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( Request::i()->$contentField ) AND $type == 'edit' )
|
||||
{
|
||||
$content = \IPS\Request::i()->$contentField;
|
||||
$content = Request::i()->$contentField;
|
||||
if ( $this->member )
|
||||
{
|
||||
$content = \IPS\Text\Parser::parseStatic( $content, TRUE, NULL, $this->member, $item::$application . '_' . mb_ucfirst( $item::$module ) );
|
||||
$content = Parser::parseStatic( $content, NULL, $this->member, $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
/* @var Comment $commentClass */
|
||||
$comment = $commentClass::load( $item->$field );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_FIRST_POST', '1S377/1', 400 );
|
||||
throw new Exception( 'NO_FIRST_POST', '1S377/1', 400 );
|
||||
}
|
||||
|
||||
$comment->$contentField = $content;
|
||||
$comment->save();
|
||||
|
||||
/* Update Search Index of the first item */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,40 +430,40 @@ class _ItemController extends \IPS\Api\Controller
|
||||
return $item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\Node\Model $container Container
|
||||
* @param \IPS\Member $author Author
|
||||
* @param string $firstPostParam The parameter which contains the body for the first comment
|
||||
* @return \IPS\Content\Item
|
||||
* @param Model|null $container Container
|
||||
* @param Member $author Author
|
||||
* @param string $firstPostParam The parameter which contains the body for the first comment
|
||||
* @return Item
|
||||
*/
|
||||
protected function _create( ?\IPS\Node\Model $container, \IPS\Member $author, $firstPostParam = 'post' )
|
||||
protected function _create( Model|null $container, Member $author, string $firstPostParam = 'post' ): Item
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
/* Work out the date */
|
||||
$date = ( !$this->member and \IPS\Request::i()->date ) ? new \IPS\DateTime( \IPS\Request::i()->date ) : \IPS\DateTime::create();
|
||||
$date = ( !$this->member and Request::i()->date ) ? new DateTime( Request::i()->date ) : DateTime::create();
|
||||
|
||||
/* Create item */
|
||||
$item = $class::createItem( $author, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), $date, $container );
|
||||
$item = $class::createItem( $author, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), $date, $container );
|
||||
|
||||
$this->_createOrUpdate( $item, 'add' );
|
||||
$this->_createOrUpdate( $item );
|
||||
$item->save();
|
||||
|
||||
/* Create post */
|
||||
if ( $class::$firstCommentRequired )
|
||||
{
|
||||
$postContents = \IPS\Request::i()->$firstPostParam;
|
||||
$postContents = Request::i()->$firstPostParam;
|
||||
|
||||
if ( $this->member )
|
||||
{
|
||||
$postContents = \IPS\Text\Parser::parseStatic( $postContents, TRUE, NULL, $this->member, $class::$application . '_' . mb_ucfirst( $class::$module ) );
|
||||
$postContents = Parser::parseStatic( $postContents, NULL, $this->member, $class::$application . '_' . IPS::mb_ucfirst( $class::$module ) );
|
||||
}
|
||||
|
||||
$commentClass = $item::$commentClass;
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and \IPS\Request::i()->ip_address ) ? \IPS\Request::i()->ip_address : \IPS\Request::i()->ipAddress(), NULL, ( isset( \IPS\Request::i()->anonymous ) ? (bool) \IPS\Request::i()->anonymous : NULL ) );
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, $author->member_id ? NULL : $author->real_name, NULL, $author, $date, ( !$this->member and Request::i()->ip_address ) ? Request::i()->ip_address : Request::i()->ipAddress(), NULL, ( isset( Request::i()->anonymous ) ? (bool) Request::i()->anonymous : NULL ) );
|
||||
|
||||
if ( isset( $class::$databaseColumnMap['first_comment_id'] ) )
|
||||
{
|
||||
@@ -453,15 +475,15 @@ class _ItemController extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Index */
|
||||
if ( $item instanceof \IPS\Content\Searchable )
|
||||
if( SearchContent::isSearchable( $item ) )
|
||||
{
|
||||
\IPS\Content\Search\Index::i()->index( $item );
|
||||
Index::i()->index( $item );
|
||||
}
|
||||
|
||||
/* Send webhooks */
|
||||
if ( \in_array( $item->hidden(), array( -1, 0, 1 ) ) ) // i.e. not post before register or pending deletion
|
||||
if ( in_array( $item->hidden(), array( -1, 0, 1 ) ) ) // i.e. not post before register or pending deletion
|
||||
{
|
||||
\IPS\Api\Webhook::fire( str_replace( '\\', '', \substr( \get_class( $item ), 3 ) ) . '_create', $item, $item->webhookFilters() );
|
||||
Webhook::fire( str_replace( '\\', '', substr( get_class( $item ), 3 ) ) . '_create', $item, $item->webhookFilters() );
|
||||
}
|
||||
|
||||
/* Send notifications and dish out points */
|
||||
@@ -470,7 +492,7 @@ class _ItemController extends \IPS\Api\Controller
|
||||
$item->sendNotifications();
|
||||
$author->achievementAction( 'core', 'NewContentItem', $item );
|
||||
}
|
||||
elseif( !\in_array( $item->hidden(), array( -1, -3 ) ) )
|
||||
elseif( !in_array( $item->hidden(), array( -1, -3 ) ) )
|
||||
{
|
||||
$item->sendUnapprovedNotification();
|
||||
}
|
||||
@@ -482,43 +504,43 @@ class _ItemController extends \IPS\Api\Controller
|
||||
/**
|
||||
* Create or update poll
|
||||
*
|
||||
* @param \IPS\Content\Item $item The content item to attach the poll to
|
||||
* @param Item $item The content item to attach the poll to
|
||||
* @param string $type Whether we are adding or editing
|
||||
* @return void
|
||||
*/
|
||||
protected function _createOrUpdatePoll( $item, $type )
|
||||
protected function _createOrUpdatePoll( Item $item, string $type ): void
|
||||
{
|
||||
/* Are we creating or updating a poll? */
|
||||
if( isset( \IPS\Request::i()->poll_title ) AND isset( \IPS\Request::i()->poll_options ) AND \IPS\Request::i()->poll_title AND \IPS\Request::i()->poll_options AND ( !$this->member OR $item::canCreatePoll( $this->member, $item->container() ) ) )
|
||||
if( isset( Request::i()->poll_title ) AND isset( Request::i()->poll_options ) AND Request::i()->poll_title AND Request::i()->poll_options AND ( !$this->member OR $item::canCreatePoll( $this->member, $item->container() ) ) )
|
||||
{
|
||||
$canCreatePoll = TRUE;
|
||||
|
||||
if( $type == 'edit' AND !$item->getPoll() )
|
||||
{
|
||||
$canCreatePoll = (bool) ( \IPS\Settings::i()->startpoll_cutoff == -1 or \IPS\DateTime::create()->sub( new \DateInterval( 'PT' . \IPS\Settings::i()->startpoll_cutoff . 'H' ) )->getTimestamp() < $item->mapped('date') );
|
||||
$canCreatePoll = ( Settings::i()->startpoll_cutoff == -1 or DateTime::create()->sub( new DateInterval( 'PT' . Settings::i()->startpoll_cutoff . 'H' ) )->getTimestamp() < $item->mapped('date') );
|
||||
}
|
||||
|
||||
if( $canCreatePoll === TRUE )
|
||||
{
|
||||
$poll = $item->getPoll() ?: new \IPS\Poll;
|
||||
$poll = $item->getPoll() ?: new Poll;
|
||||
|
||||
if( !$item->getPoll() )
|
||||
{
|
||||
$poll->starter_id = $item->author()->member_id;
|
||||
$poll->poll_item_class = \get_class( $item );
|
||||
$poll->poll_item_class = get_class( $item );
|
||||
}
|
||||
|
||||
$formatted = array(
|
||||
'title' => \IPS\Request::i()->poll_title,
|
||||
'questions' => \IPS\Request::i()->poll_options
|
||||
'title' => Request::i()->poll_title,
|
||||
'questions' => Request::i()->poll_options
|
||||
);
|
||||
|
||||
if( isset( \IPS\Request::i()->poll_only ) AND \IPS\Request::i()->poll_only )
|
||||
if( isset( Request::i()->poll_only ) AND Request::i()->poll_only )
|
||||
{
|
||||
$formatted['poll_only'] = 1;
|
||||
}
|
||||
|
||||
if( isset( \IPS\Request::i()->poll_public ) AND \IPS\Request::i()->poll_public )
|
||||
if( isset( Request::i()->poll_public ) AND Request::i()->poll_public )
|
||||
{
|
||||
$formatted['public'] = 1;
|
||||
}
|
||||
@@ -539,32 +561,28 @@ class _ItemController extends \IPS\Api\Controller
|
||||
* @param int $id ID Number
|
||||
* @param string $commentClass The class
|
||||
* @param array $where Base where clause
|
||||
* @return \IPS\Api\PaginatedResponse
|
||||
* @return PaginatedResponse
|
||||
*/
|
||||
protected function _comments( $id, $commentClass, $where = array() )
|
||||
protected function _comments( int $id, string $commentClass, array $where = array() ): PaginatedResponse
|
||||
{
|
||||
/* Init */
|
||||
/* @var array $databaseColumnMap
|
||||
* @var Item $itemClass
|
||||
* @var Comment $commentClass */
|
||||
$itemClass = $this->class;
|
||||
$item = $itemClass::load( $id );
|
||||
if( $this->member )
|
||||
if ( $this->member and !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
if ( method_exists($item, 'canView') and !$item->canView( $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
else if ( !$item->can( 'read', $this->member ) )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
$itemIdColumn = $itemClass::$databaseColumnId;
|
||||
$where [] = array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'] . '=?', $item->$itemIdColumn );
|
||||
|
||||
/* Hideable? */
|
||||
if ( \in_array( 'IPS\Content\Hideable', class_implements( $commentClass ) ) )
|
||||
if ( IPS::classUsesTrait( $commentClass, 'IPS\Content\Hideable' ) )
|
||||
{
|
||||
/* If request for hidden comments, only show if request is via API key or by a moderator */
|
||||
if ( isset( \IPS\Request::i()->hidden ) AND \IPS\Request::i()->hidden
|
||||
if ( isset( Request::i()->hidden ) AND Request::i()->hidden
|
||||
AND ( $this->member === NULL OR $commentClass::modPermission( 'view_hidden', $this->member ) ) )
|
||||
{
|
||||
if ( isset( $commentClass::$databaseColumnMap['hidden'] ) )
|
||||
@@ -597,16 +615,16 @@ class _ItemController extends \IPS\Api\Controller
|
||||
|
||||
/* Sort */
|
||||
$sortBy = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['date'];
|
||||
$sortDir = ( isset( \IPS\Request::i()->sortDir ) and \in_array( mb_strtolower( \IPS\Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? \IPS\Request::i()->sortDir : 'asc';
|
||||
$sortDir = ( isset( Request::i()->sortDir ) and in_array( mb_strtolower( Request::i()->sortDir ), array( 'asc', 'desc' ) ) ) ? Request::i()->sortDir : 'asc';
|
||||
|
||||
return new \IPS\Api\PaginatedResponse(
|
||||
return new PaginatedResponse(
|
||||
200,
|
||||
\IPS\Db::i()->select( '*', $commentClass::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
Db::i()->select( '*', $commentClass::$databaseTable, $where, "{$sortBy} {$sortDir}" ),
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
$commentClass,
|
||||
\IPS\Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first(),
|
||||
Db::i()->select( 'COUNT(*)', $commentClass::$databaseTable, $where )->first(),
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user