2015-12-02 14:38:40 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the BBcode handler.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-12-02 14:38:40 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
use JBBCode\CodeDefinitionBuilder;
|
2016-03-20 16:37:59 +00:00
|
|
|
use JBBCode\DefaultCodeDefinitionSet;
|
|
|
|
use JBBCode\Parser;
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Sakura wrapper for JBBCode.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2015-12-02 14:38:40 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-12-02 14:38:40 +00:00
|
|
|
*/
|
|
|
|
class BBcode
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The container for JBBCode.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var Parser
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
private static $bbcode = null;
|
2015-12-02 14:38:40 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Initialiser.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function init()
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Create new parser class
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode = new Parser();
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Add the standard definitions
|
2015-12-03 18:41:14 +00:00
|
|
|
self::loadStandardCodes();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 13:36:21 +00:00
|
|
|
/**
|
|
|
|
* Parse the emoticons.
|
|
|
|
*
|
|
|
|
* @param string $text String to parse emoticons from.
|
|
|
|
*
|
|
|
|
* @return string Parsed text.
|
|
|
|
*/
|
|
|
|
public static function parseEmoticons($text)
|
|
|
|
{
|
|
|
|
// Get emoticons from the database
|
2016-02-25 16:06:29 +00:00
|
|
|
$emotes = DB::table('emoticons')
|
|
|
|
->get();
|
2016-02-13 13:36:21 +00:00
|
|
|
|
|
|
|
// Parse all emoticons
|
2016-02-15 21:20:46 +00:00
|
|
|
foreach ($emotes as $emote) {
|
2016-02-18 23:28:44 +00:00
|
|
|
$image = "<img src='{$emote->emote_path}' alt='{$emote->emote_string}' class='emoticon' />";
|
|
|
|
$icon = preg_quote($emote->emote_string, '#');
|
2016-02-13 13:36:21 +00:00
|
|
|
$text = preg_replace("#$icon#", $image, $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the parsed text
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Adds the standard BBcode.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function loadStandardCodes()
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Add the standard definitions
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinitionSet(new DefaultCodeDefinitionSet());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
2016-03-25 17:10:42 +00:00
|
|
|
$simpleCodes = [
|
|
|
|
['header', '<h1>{param}</h1>'],
|
|
|
|
['s', '<del>{param}</del>'],
|
|
|
|
['spoiler', '<span class="spoiler">{param}</span>'],
|
|
|
|
['box', '<div class="spoiler-box-container">
|
2016-03-26 16:36:58 +00:00
|
|
|
<div class="spoiler-box-title" onclick="Sakura.toggleClass(this.parentNode.children[1], \'hidden\');">'
|
|
|
|
. 'Click to open</div><div class="spoiler-box-content hidden">{param}</div></div>'],
|
|
|
|
['box', '<div class="spoiler-box-container"><div class="spoiler-box-title"'
|
|
|
|
. ' onclick="Sakura.toggleClass(this.parentNode.children[1], \'hidden\');">{option}</div>'
|
|
|
|
. '<div class="spoiler-box-content hidden">{param}</div></div>'],
|
2016-03-25 17:10:42 +00:00
|
|
|
['quote', '<blockquote><div class="quotee">Quote</div><div class="quote">{param}</div></blockquote>'],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($simpleCodes as $code) {
|
|
|
|
$builder = new CodeDefinitionBuilder($code[0], $code[1]);
|
|
|
|
|
|
|
|
if (strstr($code[1], '{option}')) {
|
|
|
|
$builder->setUseOption(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
|
|
|
}
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Add special definitions (PHP files MUST have the same name as the definition class
|
2015-12-03 19:40:01 +00:00
|
|
|
foreach (glob(ROOT . 'libraries/BBcodeDefinitions/*.php') as $ext) {
|
2015-12-02 14:38:40 +00:00
|
|
|
// Clean the file path
|
2015-12-03 19:40:01 +00:00
|
|
|
$ext = str_replace(ROOT . 'libraries/', '', $ext);
|
2015-12-02 14:38:40 +00:00
|
|
|
$ext = str_replace('.php', '', $ext);
|
|
|
|
$ext = str_replace('/', '\\', $ext);
|
|
|
|
|
|
|
|
// Build the classname
|
|
|
|
$className = __NAMESPACE__ . '\\' . $ext;
|
|
|
|
|
|
|
|
// Add the BBcode definition
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition(new $className);
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Set the text to parse.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $text The text that should be parsed.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function text($text)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if $bbcode is still null
|
2016-02-27 17:28:45 +00:00
|
|
|
if (!self::$bbcode) {
|
2015-12-03 18:41:14 +00:00
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$bbcode->parse($text);
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to HTML.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return string The parsed HTML.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toHTML($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsed = nl2br(self::$bbcode->getAsHtml());
|
2015-12-02 18:15:34 +00:00
|
|
|
|
2016-03-27 21:18:57 +00:00
|
|
|
$parsed = self::fixCodeTags($parsed);
|
2016-02-13 13:36:21 +00:00
|
|
|
$parsed = self::parseEmoticons($parsed);
|
2015-12-02 18:15:34 +00:00
|
|
|
|
|
|
|
return $parsed;
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to BBCode.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return string The converted bbcode.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toEditor($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$bbcode->getAsBBCode();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to plain.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return string The converted plaintext.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toPlain($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$bbcode->getAsText();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
2016-03-27 21:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up the contents of <code> tags.
|
|
|
|
*
|
|
|
|
* @param string $text Dirty
|
|
|
|
*
|
|
|
|
* @return string Clean
|
|
|
|
*/
|
|
|
|
public static function fixCodeTags($text)
|
|
|
|
{
|
|
|
|
$parts = explode('<code>', $text);
|
|
|
|
$newStr = '';
|
|
|
|
|
|
|
|
if (count($parts) > 1) {
|
|
|
|
foreach ($parts as $p) {
|
|
|
|
$parts2 = explode('</code>', $p);
|
|
|
|
if (count($parts2) > 1) {
|
|
|
|
$code = str_replace('<br />', '', $parts2[0]);
|
|
|
|
$code = str_replace('<br/>', '', $code);
|
|
|
|
$code = str_replace('<br>', '', $code);
|
|
|
|
$code = str_replace('<', '<', $code);
|
|
|
|
$newStr .= '<code>' . $code . '</code>';
|
|
|
|
$newStr .= $parts2[1];
|
|
|
|
} else {
|
|
|
|
$newStr .= $p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$newStr = $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newStr;
|
|
|
|
}
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|