forkbb/app/Core/Parser.php

175 lines
5.5 KiB
PHP
Raw Normal View History

2017-11-30 03:08:32 +00:00
<?php
2020-12-21 10:40:19 +00:00
/**
* This file is part of the ForkBB <https://github.com/forkbb>.
*
* @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
* @license The MIT License (MIT)
*/
2017-11-30 03:08:32 +00:00
2020-10-14 13:01:43 +00:00
declare(strict_types=1);
2017-11-30 03:08:32 +00:00
namespace ForkBB\Core;
use Parserus;
2017-12-07 12:21:51 +00:00
use ForkBB\Core\Container;
2017-11-30 03:08:32 +00:00
class Parser extends Parserus
{
2023-04-27 12:36:15 +00:00
public function __construct(int $flag, protected Container $c)
2017-11-30 03:08:32 +00:00
{
parent::__construct($flag);
$this->init();
}
/**
* Инициализация данных
*/
protected function init(): void
2017-11-30 03:08:32 +00:00
{
2020-06-28 05:10:59 +00:00
if (
2021-12-19 08:17:29 +00:00
1 === $this->c->config->b_message_bbcode
|| 1 === $this->c->config->b_sig_bbcode
2020-06-28 05:10:59 +00:00
) {
$this->setBBCodes($this->c->bbcode->list);
2017-12-03 15:50:06 +00:00
}
2017-11-30 03:08:32 +00:00
2020-06-28 05:10:59 +00:00
if (
2021-12-19 08:17:29 +00:00
1 === $this->c->user->show_smilies
2020-06-28 05:10:59 +00:00
&& (
2021-12-19 08:17:29 +00:00
1 === $this->c->config->b_smilies_sig
|| 1 === $this->c->config->b_smilies
2020-06-28 05:10:59 +00:00
)
2017-11-30 03:08:32 +00:00
) {
$smilies = [];
2017-11-30 03:08:32 +00:00
foreach ($this->c->smilies->list as $cur) {
2020-09-12 07:26:42 +00:00
$smilies[$cur['sm_code']] = $this->c->PUBLIC_URL . '/img/sm/' . $cur['sm_image'];
2017-11-30 03:08:32 +00:00
}
$info = $this->c->BBCODE_INFO;
$this->setSmilies($smilies)->setSmTpl($info['smTpl'], $info['smTplTag'], $info['smTplBl']);
}
2017-12-11 16:34:36 +00:00
$this->setAttr('baseUrl', $this->c->BASE_URL);
2021-12-19 08:31:27 +00:00
$this->setAttr('showImg', 1 === $this->c->user->show_img);
$this->setAttr('showImgSign', 1 === $this->c->user->show_img_sig);
$this->setAttr(
'hashtagLink',
1 === $this->c->user->g_search ? $this->c->Router->link('Search', ['keywords' => 'HASHTAG']) : null
);
2017-11-30 03:08:32 +00:00
}
2017-12-07 12:21:51 +00:00
/**
* Проверяет разметку сообщения с бб-кодами
* Пытается исправить неточности разметки
* Генерирует ошибки разметки
*/
public function prepare(string $text, bool $isSignature = false): string
2017-12-07 12:21:51 +00:00
{
if ($isSignature) {
2021-12-19 08:17:29 +00:00
$whiteList = 1 === $this->c->config->b_sig_bbcode
2020-09-21 04:31:55 +00:00
? (empty($this->c->config->a_bb_white_sig) && empty($this->c->config->a_bb_black_sig)
? null
: $this->c->config->a_bb_white_sig
)
: [];
//$blackList = null;
2017-12-07 12:21:51 +00:00
} else {
2021-12-19 08:17:29 +00:00
$whiteList = 1 === $this->c->config->b_message_bbcode
2020-09-21 04:31:55 +00:00
? (empty($this->c->config->a_bb_white_mes) && empty($this->c->config->a_bb_black_mes)
? null
: $this->c->config->a_bb_white_mes
)
: [];
//$blackList = null;
2017-12-07 12:21:51 +00:00
}
$blackList = 1 === $this->c->user->g_post_links ? null : ['email', 'url', 'img'];
2017-12-07 12:21:51 +00:00
$this->setAttr('isSign', $isSignature)
->setWhiteList($whiteList)
->setBlackList($blackList)
->parse($text, ['strict' => true])
->stripEmptyTags(" \n\t\r\v", true);
2021-12-19 08:17:29 +00:00
if (1 === $this->c->config->b_make_links) {
2017-12-07 12:21:51 +00:00
$this->detectUrls();
}
// создание хэштегов
$this->detect('hashtag', '%(?<=^|\s|\n|\r)#(?=[\p{L}\p{N}_]{3})[\p{L}\p{N}]+(?:_+[\p{L}\p{N}]+)*(?=$|\s|\n|\r|\.|,)%u', true);
2018-03-08 12:39:54 +00:00
return \preg_replace('%^(\x20*\n)+|(\n\x20*)+$%D', '', $this->getCode());
2017-12-07 12:21:51 +00:00
}
/**
* Преобразует бб-коды в html в сообщениях
*/
public function parseMessage(string $text = null, bool $hideSmilies = false): string
2017-12-07 12:21:51 +00:00
{
// при null предполагается брать данные после prepare()
if (null !== $text) {
2021-12-19 08:17:29 +00:00
$whiteList = 1 === $this->c->config->b_message_bbcode ? null : [];
2020-09-21 04:31:55 +00:00
$blackList = $this->c->config->a_bb_black_mes;
2018-03-08 12:39:54 +00:00
2017-12-07 12:21:51 +00:00
$this->setAttr('isSign', false)
->setWhiteList($whiteList)
->setBlackList($blackList)
->parse($text);
}
2020-06-28 05:10:59 +00:00
if (
! $hideSmilies
2021-12-19 08:17:29 +00:00
&& 1 === $this->c->config->b_smilies
2020-06-28 05:10:59 +00:00
) {
2017-12-07 12:21:51 +00:00
$this->detectSmilies();
}
return $this->getHtml();
}
/**
* Преобразует бб-коды в html в подписях пользователей
*/
public function parseSignature(string $text = null): string
2017-12-07 12:21:51 +00:00
{
// при null предполагается брать данные после prepare()
if (null !== $text) {
2021-12-19 08:17:29 +00:00
$whiteList = 1 === $this->c->config->b_sig_bbcode ? null : [];
2020-09-21 04:31:55 +00:00
$blackList = $this->c->config->a_bb_black_sig;
2018-03-08 12:39:54 +00:00
2017-12-07 12:21:51 +00:00
$this->setAttr('isSign', true)
->setWhiteList($whiteList)
->setBlackList($blackList)
->parse($text);
}
2021-12-19 08:17:29 +00:00
if (1 === $this->c->config->b_smilies_sig) {
2017-12-07 12:21:51 +00:00
$this->detectSmilies();
}
return $this->getHtml();
}
/**
* Флаг использования встроенных стилей
*/
protected bool $flagInlneStyle = false;
/**
* Устанавливает/возвращает флаг использования встроенных стилей в ббкодах
* (обработчик ббкода должен вызвать этот метод со значением true)
*/
public function inlineStyle(bool $flag = null): bool
{
$prev = $this->flagInlneStyle;
if (true === $flag) {
$this->flagInlneStyle = $flag;
}
return $prev;
}
2017-11-30 03:08:32 +00:00
}