Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+84 -78
View File
@@ -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 );
}
}