This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/BBcode.php

161 lines
3.6 KiB
PHP
Raw Normal View History

<?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
*/
namespace Sakura;
/**
* BBcode handler.
2016-03-08 23:07:58 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class BBcode
{
2016-02-02 21:04:15 +00:00
/**
* BBcodes, also for backwards compatibility.
2016-03-08 23:07:58 +00:00
*
* @var array
2016-02-02 21:04:15 +00:00
*/
protected static $bbcodes = [];
2016-02-02 21:04:15 +00:00
/**
* Initialiser.
*/
public static function init()
2015-12-02 14:40:28 +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) {
$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-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
/**
* 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.
*/
public static function text($text)
2015-12-02 14:40:28 +00:00
{
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-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.
*/
public static function toHTML($text = null)
2015-12-02 14:40:28 +00:00
{
// // Check if text isn't null
// if ($text !== null) {
// self::text($text);
// }
// $parsed = nl2br(self::$bbcode->getAsHtml());
// $parsed = self::fixCodeTags($parsed);
// $parsed = self::parseEmoticons($parsed);
// return $parsed;
return $text;
}
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.
*/
public static function toEditor($text = null)
2015-12-02 14:40:28 +00:00
{
// // Check if text isn't null
// if ($text !== null) {
// self::text($text);
// }
// return self::$bbcode->getAsBBCode();
return $text;
}
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.
*/
public static function toPlain($text = null)
2015-12-02 14:40:28 +00:00
{
// // Check if text isn't null
// if ($text !== null) {
// self::text($text);
// }
// return self::$bbcode->getAsText();
return $text;
}
2016-03-27 21:18:57 +00:00
/**
* Clean up the contents of <code> tags.
* See if this can be deprecated with a custom implementation!
2016-03-27 21:18:57 +00: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('<', '&lt;', $code);
$newStr .= '<code>' . $code . '</code>';
$newStr .= $parts2[1];
} else {
$newStr .= $p;
}
}
} else {
$newStr = $text;
}
return $newStr;
}
}