<?php
namespace MMO\Hide\BbCode\Tag;
use XF\BbCode\Renderer\EmailHtml;
use XF\PreEscaped;
abstract class AbstractTag
{
/**
* @var \XF\App
*/
protected $app;
/**
* @var \XF\BbCode\Renderer\AbstractRenderer
*/
protected $renderer;
/**
* @var \XF\Template\Templater
*/
protected $templater;
/**
* @param \XF\App $app
* @param \XF\BbCode\Renderer\AbstractRenderer $renderer
*/
public function __construct(\XF\App $app, \XF\BbCode\Renderer\AbstractRenderer $renderer = null)
{
$this->app = $app;
$this->renderer = $renderer;
$this->templater = $app->templater();
}
/**
* @param array $tagChildren
* @param string $tagOption
* @param array $tag
* @param array $options
* @return string
*/
abstract public function render($content, array $tagChildren, $tagOption, array $tag, array $options);
public function renderTemplate($content, $title, array $hide)
{
return $this->templater->renderTemplate('public:mh_bb_code_hide', [
'visible' => $hide['canView'] || $this->isCreator($hide['options']) || $this->canBypass() || $this->visitor()->canBypassHideTag(ucfirst($hide['tag'])),
'content' => new PreEscaped($content),
'title' => $title ? new PreEscaped($title) : ''
]);
}
/**
* @param $tagChildren
* @param $tagOption
* @param $tag
* @param array $options
* @param \XF\BbCode\Renderer\AbstractRenderer $renderer
*
* @return string
*/
public static function renderHide($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
{
$tagRenderer = new static(\XF::app(), $renderer);
/** @var \XF\BbCode\Renderer\AbstractRenderer $content */
$content = $renderer instanceof EmailHtml ? false : $renderer->renderSubTree($tagChildren, $options);
if ($content === '')
{
return '';
}
if(!\XF::visitor()->user_id && !in_array($tag['tag'], ['hide', 'groups', 'users']))
{
return $renderer->getTemplater()->renderTemplate('public:mh_hide_auth_or_register');
}
return $tagRenderer->render($content, $tagChildren, $tagOption, $tag, $options);
}
/**
* @param $options
* @return bool
*/
public function isCreator($options)
{
$visitor = \XF::visitor();
if (isset($options['user_id']))
{
return $options['user_id'] === $visitor->user_id;
}
if (!empty($options['entity']['user_id']))
{
return $options['entity']['user_id'] === $visitor->user_id;
}
else
{
return false;
}
}
/**
* @return bool
*/
public function canBypass()
{
$visitor = \XF::visitor();
if ($visitor->is_super_admin)
{
return true;
}
if($visitor->hasPermission('forum', 'editAnyPost')
|| $visitor->hasPermission('forum', 'deleteAnyPost')
)
{
return true;
}
if($visitor->hasPermission('forum', 'mh_bypass'))
{
return true;
}
return false;
}
/**
* @param $options
* @return bool
*/
public function canReactionView($options)
{
if (isset($options['likes']))
{
$reactionIds = array_map(function ($v)
{
return $v['user_id'];
}, $options['likes']);
}
elseif (isset($options['entity']) && isset($options['entity']['reaction_users']))
{
$reactionIds = array_map(function ($v)
{
return $v['user_id'];
}, $options['entity']['reaction_users']);
}
if (isset($reactionIds))
{
return in_array(\XF::visitor()->user_id, $reactionIds);
}
else
{
return false;
}
}
/**
* @param $options
* @return bool
*/
public function canReplyView($options)
{
$threadId = 0;
if (isset($options['thread_id']))
{
$threadId = $options['thread_id'];
}
elseif (isset($options['entity']) && isset($options['entity']['thread_id']))
{
$threadId = $options['entity']['thread_id'];
}
if (!$threadId)
{
return false;
}
/** @var \XF\Finder\Post $finder */
$finder = $this->finder('XF:Post');
$posts = $finder->where([
['thread_id', $threadId],
['user_id', \XF::visitor()->user_id],
['message_state', 'visible']
])->limit(1)->fetch();
return $posts->count();
}
/**
* @return \XF\Entity\User
*/
public function visitor()
{
return \XF::visitor();
}
/**
* @return \XF\Mvc\Entity\Manager
*/
public function em()
{
return $this->app->em();
}
/**
* @param string $repository
*
* @return \XF\Mvc\Entity\Repository
*/
public function repository($repository)
{
return $this->app->repository($repository);
}
/**
* @param $finder
*
* @return \XF\Mvc\Entity\Finder
*/
public function finder($finder)
{
return $this->app->finder($finder);
}
public function find($shortName, $id, $with = null)
{
return $this->app->em()->find($shortName, $id, $with);
}
/**
* @param string $finder
* @param array $where
* @param string|array $with
*
* @return null|\XF\Mvc\Entity\Entity
*/
public function findOne($finder, array $where, $with = null)
{
return $this->app->em()->findOne($finder, $where, $with);
}
}