2015-12-02 15:38:40 +01:00
|
|
|
<?php
|
2016-02-03 23:22:56 +01:00
|
|
|
/**
|
|
|
|
* Holds the BBcode handler.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-03 23:22:56 +01:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-12-02 15:38:40 +01:00
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
2016-07-29 21:31:36 +02:00
|
|
|
* BBcode handler.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2015-12-02 15:38:40 +01:00
|
|
|
* @package Sakura
|
2016-02-02 22:04:15 +01:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-12-02 15:38:40 +01:00
|
|
|
*/
|
|
|
|
class BBcode
|
|
|
|
{
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
2016-07-29 21:31:36 +02:00
|
|
|
* BBcodes, also for backwards compatibility.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-07-29 21:31:36 +02:00
|
|
|
* @var array
|
2016-02-02 22:04:15 +01:00
|
|
|
*/
|
2016-07-29 21:31:36 +02:00
|
|
|
protected static $bbcodes = [];
|
2015-12-02 15:38:40 +01:00
|
|
|
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
|
|
|
* Initialiser.
|
|
|
|
*/
|
2015-12-03 19:41:14 +01:00
|
|
|
public static function init()
|
2015-12-02 09:40:28 -05:00
|
|
|
{
|
2015-12-02 15:38:40 +01:00
|
|
|
}
|
|
|
|
|
2016-02-13 14:36:21 +01: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 17:06:29 +01:00
|
|
|
$emotes = DB::table('emoticons')
|
|
|
|
->get();
|
2016-02-13 14:36:21 +01:00
|
|
|
|
|
|
|
// Parse all emoticons
|
2016-02-15 22:20:46 +01:00
|
|
|
foreach ($emotes as $emote) {
|
2016-07-29 21:31:36 +02:00
|
|
|
$image = "<img src='{$emote->emote_path}' alt='{$emote->emote_string}' class='emoticon'>";
|
2016-02-19 00:28:44 +01:00
|
|
|
$icon = preg_quote($emote->emote_string, '#');
|
2016-02-13 14:36:21 +01:00
|
|
|
$text = preg_replace("#$icon#", $image, $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the parsed text
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
|
|
|
* Set the text to parse.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @param string $text The text that should be parsed.
|
|
|
|
*/
|
2015-12-03 19:41:14 +01:00
|
|
|
public static function text($text)
|
2015-12-02 09:40:28 -05:00
|
|
|
{
|
2016-07-29 21:31:36 +02:00
|
|
|
return $text;
|
2015-12-02 15:38:40 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to HTML.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @return string The parsed HTML.
|
|
|
|
*/
|
2015-12-03 19:41:14 +01:00
|
|
|
public static function toHTML($text = null)
|
2015-12-02 09:40:28 -05:00
|
|
|
{
|
2016-07-29 21:31:36 +02:00
|
|
|
// // Check if text isn't null
|
|
|
|
// if ($text !== null) {
|
|
|
|
// self::text($text);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// $parsed = nl2br(self::$bbcode->getAsHtml());
|
2015-12-03 19:41:14 +01:00
|
|
|
|
2016-07-29 21:31:36 +02:00
|
|
|
// $parsed = self::fixCodeTags($parsed);
|
|
|
|
// $parsed = self::parseEmoticons($parsed);
|
2015-12-02 19:15:34 +01:00
|
|
|
|
2016-07-29 21:31:36 +02:00
|
|
|
// return $parsed;
|
2015-12-02 19:15:34 +01:00
|
|
|
|
2016-07-29 21:31:36 +02:00
|
|
|
return $text;
|
2015-12-02 15:38:40 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to BBCode.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @return string The converted bbcode.
|
|
|
|
*/
|
2015-12-03 19:41:14 +01:00
|
|
|
public static function toEditor($text = null)
|
2015-12-02 09:40:28 -05:00
|
|
|
{
|
2016-07-29 21:31:36 +02:00
|
|
|
// // Check if text isn't null
|
|
|
|
// if ($text !== null) {
|
|
|
|
// self::text($text);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return self::$bbcode->getAsBBCode();
|
2015-12-03 19:41:14 +01:00
|
|
|
|
2016-07-29 21:31:36 +02:00
|
|
|
return $text;
|
2015-12-02 15:38:40 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 22:04:15 +01:00
|
|
|
/**
|
|
|
|
* Convert the parsed text to plain.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @param string $text The text that should be parsed.
|
2016-03-08 18:07:58 -05:00
|
|
|
*
|
2016-02-02 22:04:15 +01:00
|
|
|
* @return string The converted plaintext.
|
|
|
|
*/
|
2015-12-03 19:41:14 +01:00
|
|
|
public static function toPlain($text = null)
|
2015-12-02 09:40:28 -05:00
|
|
|
{
|
2016-07-29 21:31:36 +02:00
|
|
|
// // Check if text isn't null
|
|
|
|
// if ($text !== null) {
|
|
|
|
// self::text($text);
|
|
|
|
// }
|
2015-12-03 19:41:14 +01:00
|
|
|
|
2016-07-29 21:31:36 +02:00
|
|
|
// return self::$bbcode->getAsText();
|
|
|
|
return $text;
|
2015-12-02 15:38:40 +01:00
|
|
|
}
|
2016-03-27 23:18:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up the contents of <code> tags.
|
2016-07-29 21:31:36 +02:00
|
|
|
* See if this can be deprecated with a custom implementation!
|
2016-03-27 23:18:57 +02:00
|
|
|
*
|
|
|
|
* @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 15:38:40 +01:00
|
|
|
}
|