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;
|
|
|
|
|
|
|
|
/**
|
2016-07-29 19:31:36 +00:00
|
|
|
* BBcode handler.
|
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
|
|
|
/**
|
2016-08-04 21:24:08 +00:00
|
|
|
* Holds the bbcode parsers
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-07-29 19:31:36 +00:00
|
|
|
* @var array
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-04 21:24:08 +00:00
|
|
|
public static $parsers = [
|
|
|
|
'bold' => [
|
|
|
|
'pattern' => '/\[b\](.*?)\[\/b\]/s',
|
|
|
|
'replace' => '<b>$1</b>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'italic' => [
|
|
|
|
'pattern' => '/\[i\](.*?)\[\/i\]/s',
|
|
|
|
'replace' => '<i>$1</i>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'underline' => [
|
|
|
|
'pattern' => '/\[u\](.*?)\[\/u\]/s',
|
|
|
|
'replace' => '<u>$1</u>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'linethrough' => [
|
|
|
|
'pattern' => '/\[s\](.*?)\[\/s\]/s',
|
|
|
|
'replace' => '<del>$1</del>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'header' => [
|
|
|
|
'pattern' => '/\[header\](.*?)\[\/header\]/s',
|
|
|
|
'replace' => '<h1>$1</h1>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'size' => [
|
|
|
|
'pattern' => '/\[size\=([1-7])\](.*?)\[\/size\]/s',
|
|
|
|
'replace' => '<font size="$1">$2</font>',
|
|
|
|
'content' => '$2',
|
|
|
|
],
|
|
|
|
'color' => [
|
|
|
|
'pattern' => '/\[color\=(#[A-f0-9]{6}|#[A-f0-9]{3})\](.*?)\[\/color\]/s',
|
|
|
|
'replace' => '<span style="color: $1">$2</span>',
|
|
|
|
'content' => '$2',
|
|
|
|
],
|
|
|
|
'center' => [
|
|
|
|
'pattern' => '/\[center\](.*?)\[\/center\]/s',
|
|
|
|
'replace' => '<div style="text-align: center;">$1</div>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'left' => [
|
|
|
|
'pattern' => '/\[left\](.*?)\[\/left\]/s',
|
|
|
|
'replace' => '<div style="text-align: left;">$1</div>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'right' => [
|
|
|
|
'pattern' => '/\[right\](.*?)\[\/right\]/s',
|
|
|
|
'replace' => '<div style="text-align: right;">$1</div>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'align' => [
|
|
|
|
'pattern' => '/\[align\=(left|center|right)\](.*?)\[\/align\]/s',
|
|
|
|
'replace' => '<div style="text-align: $1;">$2</div>',
|
|
|
|
'content' => '$2',
|
|
|
|
],
|
|
|
|
'quote' => [
|
|
|
|
'pattern' => '/\[quote\](.*?)\[\/quote\]/s',
|
|
|
|
'replace' => '<blockquote>$1</blockquote>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'namedquote' => [
|
|
|
|
'pattern' => '/\[quote\=(.*?)\](.*)\[\/quote\]/s',
|
|
|
|
'replace' => '<blockquote><small>$1</small>$2</blockquote>',
|
|
|
|
'content' => '$2',
|
|
|
|
],
|
|
|
|
'link' => [
|
|
|
|
'pattern' => '/\[url\](.*?)\[\/url\]/s',
|
|
|
|
'replace' => '<a href="$1">$1</a>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'namedlink' => [
|
|
|
|
'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s',
|
|
|
|
'replace' => '<a href="$1">$2</a>',
|
|
|
|
'content' => '$2',
|
|
|
|
],
|
|
|
|
'image' => [
|
|
|
|
'pattern' => '/\[img\](.*?)\[\/img\]/s',
|
|
|
|
'replace' => '<img src="$1" alt="$1">',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'orderedlistnumerical' => [
|
|
|
|
'pattern' => '/\[list=1\](.*?)\[\/list\]/s',
|
|
|
|
'replace' => '<ol>$1</ol>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'orderedlistalpha' => [
|
|
|
|
'pattern' => '/\[list=a\](.*?)\[\/list\]/s',
|
|
|
|
'replace' => '<ol type="a">$1</ol>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'unorderedlist' => [
|
|
|
|
'pattern' => '/\[list\](.*?)\[\/list\]/s',
|
|
|
|
'replace' => '<ul>$1</ul>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'listitem' => [
|
|
|
|
'pattern' => '/\[\*\](.*)/',
|
|
|
|
'replace' => '<li>$1</li>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'code' => [
|
|
|
|
'pattern' => '/\[code\](.*?)\[\/code\]/s',
|
|
|
|
'replace' => '<pre><code>$1</code></pre>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'youtube' => [
|
|
|
|
'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s',
|
|
|
|
'replace' => '<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/$1" frameborder="0" allowfullscreen></iframe>',
|
|
|
|
'content' => '$1',
|
|
|
|
],
|
|
|
|
'linebreak' => [
|
|
|
|
'pattern' => '/\r\n|\r|\n/',
|
|
|
|
'replace' => '<br>',
|
|
|
|
'content' => '',
|
|
|
|
],
|
|
|
|
];
|
2015-12-02 14:38:40 +00:00
|
|
|
|
2016-02-13 13:36:21 +00:00
|
|
|
/**
|
|
|
|
* Parse the emoticons.
|
|
|
|
*
|
2016-08-04 21:24:08 +00:00
|
|
|
* @param string $text
|
|
|
|
* @return string
|
2016-02-13 13:36:21 +00:00
|
|
|
*/
|
|
|
|
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-07-29 19:31:36 +00:00
|
|
|
$image = "<img src='{$emote->emote_path}' alt='{$emote->emote_string}' class='emoticon'>";
|
2016-02-18 23:28:44 +00:00
|
|
|
$icon = preg_quote($emote->emote_string, '#');
|
2016-08-04 21:24:08 +00:00
|
|
|
$text = preg_replace("#{$icon}#", $image, $text);
|
2016-02-13 13:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the parsed text
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to HTML.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-08-04 21:24:08 +00:00
|
|
|
* @param string $text
|
|
|
|
* @return string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-04 21:24:08 +00:00
|
|
|
public static function toHTML($text)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2016-08-04 21:24:08 +00:00
|
|
|
$text = self::parseEmoticons($text);
|
2015-12-02 14:38:40 +00:00
|
|
|
|
2016-08-04 21:24:08 +00:00
|
|
|
foreach (self::$parsers as $parser) {
|
|
|
|
$text = preg_replace($parser['pattern'], $parser['replace'], $text);
|
2016-03-27 21:18:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 21:24:08 +00:00
|
|
|
return $text;
|
2016-03-27 21:18:57 +00:00
|
|
|
}
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|