Skip to content

Commit

Permalink
Implements abstract SMF\Parser class & sub-classes
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Stovell <[email protected]>
  • Loading branch information
Sesquipedalian committed Dec 6, 2024
1 parent 5c8fe7e commit 8f5f02f
Show file tree
Hide file tree
Showing 57 changed files with 1,426 additions and 1,543 deletions.
6 changes: 3 additions & 3 deletions Sources/Actions/Admin/ACP.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use SMF\Actions\MessageIndex;
use SMF\Actions\Notify;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Cache\CacheApi;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
Expand All @@ -28,6 +27,7 @@
use SMF\Lang;
use SMF\Mail;
use SMF\Menu;
use SMF\Parser;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\Url;
Expand Down Expand Up @@ -976,7 +976,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
// What about any BBC selection boxes?
if (!empty($bbcChoice)) {
// What are the options, eh?
$temp = BBCodeParser::getCodes();
$temp = Parser::getBBCodes();
$bbcTags = [];

foreach ($temp as $tag) {
Expand Down Expand Up @@ -1351,7 +1351,7 @@ public static function saveDBSettings(array &$config_vars): void
elseif ($var[0] == 'bbc') {
$bbcTags = [];

foreach (BBCodeParser::getCodes() as $tag) {
foreach (Parser::getBBCodes() as $tag) {
$bbcTags[] = $tag['tag'];
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Actions/Admin/Boards.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use SMF\ActionInterface;
use SMF\Actions\BackwardCompatibility;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Board;
use SMF\Category;
use SMF\Config;
Expand All @@ -28,6 +27,7 @@
use SMF\IntegrationHook;
use SMF\Lang;
use SMF\Menu;
use SMF\Parser;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\Url;
Expand Down Expand Up @@ -366,7 +366,7 @@ public function editCategory2(): void

// Try to get any valid HTML to BBC first, add a naive attempt to strip it off, htmlspecialchars for the rest
$catOptions['cat_name'] = Utils::htmlspecialchars(strip_tags($_POST['cat_name']));
$catOptions['cat_desc'] = Utils::htmlspecialchars(strip_tags(BBCodeParser::load()->unparse($_POST['cat_desc'])));
$catOptions['cat_desc'] = Utils::htmlspecialchars(strip_tags(Parser::transform($_POST['cat_desc'], Parser::OUTPUT_BBC)));
$catOptions['is_collapsible'] = isset($_POST['collapse']);

if (isset($_POST['add'])) {
Expand Down Expand Up @@ -677,7 +677,7 @@ public function editBoard2(): void

// Try to get any valid HTML to BBC first, add a naive attempt to strip it off, htmlspecialchars for the rest
$boardOptions['board_name'] = Utils::htmlspecialchars(strip_tags($_POST['board_name']));
$boardOptions['board_description'] = Utils::htmlspecialchars(strip_tags(BBCodeParser::load()->unparse($_POST['desc'])));
$boardOptions['board_description'] = Utils::htmlspecialchars(strip_tags(Parser::transform($_POST['desc'], Parser::OUTPUT_BBC)));

$boardOptions['moderator_string'] = $_POST['moderators'];

Expand Down
4 changes: 2 additions & 2 deletions Sources/Actions/Admin/ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

use SMF\ActionInterface;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\ErrorHandler;
use SMF\IP;
use SMF\Lang;
use SMF\PageIndex;
use SMF\Parser;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\Time;
Expand Down Expand Up @@ -430,7 +430,7 @@ public function viewFile(): void
ErrorHandler::fatalLang('error_bad_line');
}

$file_data = explode('<br />', BBCodeParser::highlightPhpCode(Utils::htmlspecialchars(file_get_contents($file))));
$file_data = explode('<br />', Parser::highlightPhpCode(Utils::htmlspecialchars(file_get_contents($file))));

// We don't want to slice off too many so lets make sure we stop at the last one
$max = min($max, max(array_keys($file_data)));
Expand Down
14 changes: 7 additions & 7 deletions Sources/Actions/Admin/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
use SMF\Actions\BackwardCompatibility;
use SMF\Actions\Profile\Notification;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\ErrorHandler;
use SMF\Graphics\Image;
use SMF\IntegrationHook;
use SMF\ItemList;
use SMF\Lang;
use SMF\MarkdownParser;
use SMF\Menu;
use SMF\Parser;
use SMF\Parsers\MarkdownParser;
use SMF\Profile;
use SMF\Sapi;
use SMF\SecurityToken;
Expand Down Expand Up @@ -185,7 +185,7 @@ public function bbc(): void
$bbcTags = [];
$bbcTagsChildren = [];

foreach (BBCodeParser::getCodes() as $tag) {
foreach (Parser::getBBCodes() as $tag) {
$bbcTags[] = $tag['tag'];

if (isset($tag['require_children'])) {
Expand All @@ -194,8 +194,8 @@ public function bbc(): void
}

// Clean up tags with children
foreach($bbcTagsChildren as $parent_tag => $children) {
foreach($children as $index => $child_tag) {
foreach ($bbcTagsChildren as $parent_tag => $children) {
foreach ($children as $index => $child_tag) {
// Remove entries where parent and child tag is the same
if ($child_tag == $parent_tag) {
unset($bbcTagsChildren[$parent_tag][$index]);
Expand Down Expand Up @@ -592,7 +592,7 @@ public function signature(): void
// Clean up the tag stuff!
$bbcTags = [];

foreach (BBCodeParser::getCodes() as $tag) {
foreach (Parser::getBBCodes() as $tag) {
$bbcTags[] = $tag['tag'];
}

Expand Down Expand Up @@ -1035,7 +1035,7 @@ public function profileEdit(): void
[],
);

while($row = Db::$db->fetch_assoc($request)) {
while ($row = Db::$db->fetch_assoc($request)) {
$fields[] = $row['id_field'];
}
Db::$db->free_result($request);
Expand Down
6 changes: 3 additions & 3 deletions Sources/Actions/Admin/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use SMF\Actions\BackwardCompatibility;
use SMF\Actions\Notify;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\Editor;
Expand All @@ -31,6 +30,7 @@
use SMF\Mail;
use SMF\Menu;
use SMF\Msg;
use SMF\Parser;
use SMF\PersonalMessage\PM;
use SMF\SecurityToken;
use SMF\Theme;
Expand Down Expand Up @@ -1106,7 +1106,7 @@ public static function list_getNews(): array
$admin_current_news[$id] = [
'id' => $id,
'unparsed' => Msg::un_preparsecode($line),
'parsed' => preg_replace('~<([/]?)form[^>]*?[>]*>~i', '<em class="smalltext">&lt;$1form&gt;</em>', BBCodeParser::load()->parse($line)),
'parsed' => preg_replace('~<([/]?)form[^>]*?[>]*>~i', '<em class="smalltext">&lt;$1form&gt;</em>', Parser::transform($line)),
];
}

Expand Down Expand Up @@ -1193,7 +1193,7 @@ public static function prepareMailingForPreview(): void
if (!empty(Utils::$context['send_html'])) {
$enablePostHTML = Config::$modSettings['enablePostHTML'];
Config::$modSettings['enablePostHTML'] = Utils::$context['send_html'];
Utils::$context[$key] = BBCodeParser::load()->parse(Utils::$context[$key]);
Utils::$context[$key] = Parser::transform(Utils::$context[$key]);
Config::$modSettings['enablePostHTML'] = $enablePostHTML;
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Actions/Admin/Smileys.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use SMF\Actions\BackwardCompatibility;
use SMF\Actions\MessageIndex;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Cache\CacheApi;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
Expand All @@ -33,6 +32,7 @@
use SMF\Menu;
use SMF\Msg;
use SMF\PackageManager\SubsPackage;
use SMF\Parser;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\User;
Expand Down Expand Up @@ -1622,7 +1622,7 @@ public function install(): void

if (!empty($action['parse_bbc'])) {
Msg::preparsecode(Utils::$context[$type]);
Utils::$context[$type] = BBCodeParser::load()->parse(Utils::$context[$type]);
Utils::$context[$type] = Parser::transform(Utils::$context[$type]);
} else {
Utils::$context[$type] = nl2br(Utils::$context[$type]);
}
Expand Down
26 changes: 13 additions & 13 deletions Sources/Actions/Agreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

use SMF\ActionInterface;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Config;
use SMF\ErrorHandler;
use SMF\Lang;
use SMF\MarkdownParser;
use SMF\Parser;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -149,31 +148,32 @@ protected function prepareAgreementContext(): void

if (!empty(Utils::$context['agreement_file'])) {
$cache_id = strtr(Utils::$context['agreement_file'], [Config::$languagesdir => '', '.txt' => '', '.' => '_']);
Utils::$context['agreement'] = BBCodeParser::load()->parse(file_get_contents(Utils::$context['agreement_file']), true, $cache_id);
Utils::$context['agreement'] = Parser::transform(
string: file_get_contents(Utils::$context['agreement_file']),
options: ['cache_id' => $cache_id, 'hard_breaks' => 0],
);
} elseif (Utils::$context['can_accept_agreement']) {
ErrorHandler::fatalLang('error_no_agreement', false);
}

if (!empty(Config::$modSettings['enableMarkdown'])) {
Utils::$context['agreement'] = MarkdownParser::load(MarkdownParser::OUTPUT_HTML, 0)->parse(Utils::$context['agreement'], true);
}
}

if (!Utils::$context['accept_doc'] || Utils::$context['can_accept_privacy_policy']) {
// Have we got a localized policy?
if (!empty(Config::$modSettings['policy_' . User::$me->language])) {
Utils::$context['privacy_policy'] = BBCodeParser::load()->parse(Config::$modSettings['policy_' . User::$me->language]);
Utils::$context['privacy_policy'] = Parser::transform(
string: Config::$modSettings['policy_' . User::$me->language],
options: ['hard_breaks' => 0],
);
} elseif (!empty(Config::$modSettings['policy_' . Lang::$default])) {
Utils::$context['privacy_policy'] = BBCodeParser::load()->parse(Config::$modSettings['policy_' . Lang::$default]);
Utils::$context['privacy_policy'] = Parser::transform(
string: Config::$modSettings['policy_' . Lang::$default],
options: ['hard_breaks' => 0],
);
}
// Then I guess we've got nothing
elseif (Utils::$context['can_accept_privacy_policy']) {
ErrorHandler::fatalLang('error_no_privacy_policy', false);
}

if (!empty(Config::$modSettings['enableMarkdown'])) {
Utils::$context['privacy_policy'] = MarkdownParser::load(MarkdownParser::OUTPUT_HTML, 0)->parse(Utils::$context['privacy_policy'], true);
}
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions Sources/Actions/Announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use SMF\ActionInterface;
use SMF\ActionTrait;
use SMF\BBCodeParser;
use SMF\Board;
use SMF\BrowserDetector;
use SMF\Config;
Expand All @@ -27,7 +26,7 @@
use SMF\Lang;
use SMF\Logging;
use SMF\Mail;
use SMF\MarkdownParser;
use SMF\Parser;
use SMF\Theme;
use SMF\Topic;
use SMF\User;
Expand Down Expand Up @@ -168,11 +167,7 @@ public function send(): void
Lang::censorText(Utils::$context['topic_subject']);
Lang::censorText($message);

$message = BBCodeParser::load()->parse($message, false, $id_msg);

if (!empty(Config::$modSettings['enableMarkdown'])) {
$message = MarkdownParser::load()->parse($message, true);
}
$message = Parser::transform(string: $message, options: ['cache_id' => $id_msg]);

$message = trim(Utils::htmlspecialcharsDecode(strip_tags(strtr($message, ['<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '<p>' => '', '</p>' => "\n\n", '&#91;' => '[', '&#93;' => ']']))));

Expand Down
39 changes: 17 additions & 22 deletions Sources/Actions/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use SMF\ActionTrait;
use SMF\Attachment;
use SMF\Autolinker;
use SMF\BBCodeParser;
use SMF\Board;
use SMF\BrowserDetector;
use SMF\Cache\CacheApi;
Expand All @@ -29,7 +28,7 @@
use SMF\IntegrationHook;
use SMF\IP;
use SMF\Lang;
use SMF\MarkdownParser;
use SMF\Parser;
use SMF\Sapi;
use SMF\Theme;
use SMF\Time;
Expand Down Expand Up @@ -779,11 +778,11 @@ public function getXmlNews(): array
$row['body'] = strtr(Utils::entitySubstr(str_replace('<br>', "\n", $row['body']), 0, Config::$modSettings['xmlnews_maxlen'] - 3), ["\n" => '<br>']) . '...';
}

$row['body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);

if (!empty(Config::$modSettings['enableMarkdown'])) {
$row['body'] = MarkdownParser::load()->parse($row['body'], true);
}
$row['body'] = Parser::transform(
string: $row['body'],
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
options: ['cache_id' => (int) $row['id_msg']],
);

Lang::censorText($row['body']);
Lang::censorText($row['subject']);
Expand Down Expand Up @@ -1223,11 +1222,11 @@ public function getXmlRecent(): array
$row['body'] = strtr(Utils::entitySubstr(str_replace('<br>', "\n", $row['body']), 0, Config::$modSettings['xmlnews_maxlen'] - 3), ["\n" => '<br>']) . '...';
}

$row['body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);

if (!empty(Config::$modSettings['enableMarkdown'])) {
$row['body'] = MarkdownParser::load()->parse($row['body'], true);
}
$row['body'] = Parser::transform(
string: $row['body'],
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
options: ['cache_id' => (int) $row['id_msg']],
);

Lang::censorText($row['body']);
Lang::censorText($row['subject']);
Expand Down Expand Up @@ -1991,11 +1990,11 @@ public function getXmlPosts(): array
}

// If using our own format, we want both the raw and the parsed content.
$row[$this->format === 'smf' ? 'body_html' : 'body'] = BBCodeParser::load()->parse($row['body'], (bool) $row['smileys_enabled'], (int) $row['id_msg']);

if (!empty(Config::$modSettings['enableMarkdown'])) {
$row[$this->format === 'smf' ? 'body_html' : 'body'] = MarkdownParser::load()->parse($row[$this->format === 'smf' ? 'body_html' : 'body'], true);
}
$row[$this->format === 'smf' ? 'body_html' : 'body'] = Parser::transform(
string: $row['body'],
input_types: Parser::INPUT_BBC | Parser::INPUT_MARKDOWN | ((bool) $row['smileys_enabled'] ? Parser::INPUT_SMILEYS : 0),
options: ['cache_id' => (int) $row['id_msg']],
);

// Do we want to include any attachments?
if (!empty(Config::$modSettings['attachmentEnable']) && !empty(Config::$modSettings['xmlnews_attachments'])) {
Expand Down Expand Up @@ -2446,11 +2445,7 @@ public function getXmlPMs(): array
}

// If using our own format, we want both the raw and the parsed content.
$row[$this->format === 'smf' ? 'body_html' : 'body'] = BBCodeParser::load()->parse($row['body']);

if (!empty(Config::$modSettings['enableMarkdown'])) {
$row[$this->format === 'smf' ? 'body_html' : 'body'] = MarkdownParser::load()->parse($row[$this->format === 'smf' ? 'body_html' : 'body'], true);
}
$row[$this->format === 'smf' ? 'body_html' : 'body'] = Parser::transform($row['body']);

$recipients = array_combine(explode(',', $row['id_members_to']), explode($separator, $row['to_names']));

Expand Down
Loading

0 comments on commit 8f5f02f

Please sign in to comment.