<?php
/************************************************************************************
Kittenbunny CMS
Filename: textarea.class.php
Class: Validate Textarea
************************************************************************************/
class textarea
{
// Variables
public $post;
// Function to remove html tags //
private function sanitize_htmltags()
{
$value = $this->post;
// Sets the allowed taggs //
$value = preg_replace('/<\/?(?:\b(?!)[^>]+?)>/i', '', $value);
// Does the htmlspecialchars bit //
$value = htmlspecialchars($value, ENT_QUOTES, "UTF-8");
return $value;
}
// Adds paragraph tags to the html
private function build_paragraphs()
{
$value = $this->bbtags_html();
// Rebuilding the paragraphs //
$paragraphs = explode("\n", $value);
for ($i = 0; $i < count ($paragraphs); $i++)
{
$paragraphs[$i] = '<p>' . $paragraphs[$i] . '</p>';
}
$value = implode ('', $paragraphs);
// Making the paragraphs HTML
$value = preg_replace('/<(.p?)>/', '<$1>', $value);
// Return the value
return $value;
}
// Function to convert the bbtages to html. //
private function bbtags_html()
{
// Variables
$value = $this->sanitize_htmltags();
/*
Formating Tags
*/
// Bold //
$pattern[0] = "/\[b\](.*?)\[\/b\]/is";
$replace[0] = "<strong>$1</strong>";
// Italic //
$pattern[1] = "/\[i\](.*?)\[\/i\]/is";
$replace[1] = "<i>$1</i>";
// Underlined //
$pattern[2] = "/\[u\](.*?)\[\/u\]/is";
$replace[2] = "<u>$1</u>";
// url //
$pattern[3] = "/\[url\](.*?)\[\/url\]/is";
$replace[3] = "<a href=\"$1\">$1</a>";
// img //
$pattern[4] = "/\[img\](.*?)\[\/img\]/is";
$replace[4] = "<img src=\"$1\" alt=\"$1\" \>";
// quote //
$pattern[5] = "/\[quote\](.*?)\[\/quote\]/is";
$replace[5] = "<div class=\"quote\">$1</div>";
// code //
$pattern[6] = "/\[code\](.*?)\[\/code\]/is";
$replace[6] = "<div class=\"code\">$1</div>";
// list element //
$pattern[7] = '/\[\*\]([\w\W]+?)\n?(?=(?:(?:\[\*\])|(?:\[\/LIST\])))/is';
$replace[7] = "<li>$1</li>";
// Numbered list //
$pattern[8] = "/\[LIST=1\](.*?)\[\/LIST\]/is";
$replace[8] = "<ol>$1</ol>";
// unordered list //
$pattern[9] = "/\[LIST\](.*?)\[\/LIST\]/is";
$replace[9] = "<ul>$1</ul>";
/*
Image Tags
*/
// smile
$pattern[10] = '/\:\)/';
$replace[10] = '<img src="/include/class/bbeditor/images/smilies/emoticon_smile.png" alt=":)" />';
// tongue
$pattern[11] = '/\:p/';
$replace[11] = '<img src="/include/class/bbeditor/images/smilies/emoticon_tongue.png" alt=":p" />';
$pattern[12] = '/\:P/';
$replace[12] = '<img src="/include/class/bbeditor/images/smilies/emoticon_tongue.png" alt=":p" />';
// happy
$pattern[13] = '/\:d/';
$replace[13] = '<img src="/include/class/bbeditor/images/smilies/emoticon_happy.png" alt=":D" />';
$pattern[14] = '/\:D/';
$replace[14] = '<img src="/include/class/bbeditor/images/smilies/emoticon_happy.png" alt=":D" />';
// Kitty Smile
$pattern[15] = '/\:3/';
$replace[15] = '<img src="/include/class/bbeditor/images/smilies/emoticon_waii.png" alt=":3" />';
// grin
$pattern[16] = '/\:grin\:/';
$replace[16] = '<img src="/include/class/bbeditor/images/smilies/emoticon_grin.png" alt=":grin:" />';
$pattern[17] = '/\:GRIN\:/';
$replace[17] = '<img src="/include/class/bbeditor/images/smilies/emoticon_grin.png" alt=":grin:" />';
// wink
$pattern[18] = '/\;\)/';
$replace[18] = '<img src="/include/class/bbeditor/images/smilies/emoticon_wink.png" alt=";)" />';
// twisted
$pattern[19] = '/\:twisted\:/';
$replace[19] = '<img src="/include/class/bbeditor/images/smilies/emoticon_evilgrin.png" alt=":twisted:" />';
$pattern[20] = '/\:TWISTED\:/';
$replace[20] = '<img src="/include/class/bbeditor/images/smilies/emoticon_evilgrin.png" alt=":twisted:" />';
// surprised
$pattern[21] = '/\:o/';
$replace[21] = '<img src="/include/class/bbeditor/images/smilies/emoticon_surprised.png" alt=":o" />';
$pattern[22] = '/\:O/';
$replace[22] = '<img src="/include/class/bbeditor/images/smilies/emoticon_surprised.png" alt=":o" />';
// sad
$pattern[23] = '/\:\(/';
$replace[23] = '<img src="/include/class/bbeditor/images/smilies/emoticon_unhappy.png" alt=":(" />';
/*
Output
*/
$value = preg_replace($pattern, $replace, $value);
return $value;
}
// Function to check for errors. //
private function error_checking()
{
$value = $this->sanitize_htmltags();
// Trimming excess space from the value. //
if(!$value || strlen($value = trim($value)) == 0)
{
// If the value is empty. //
return "The post is empty.";
}
else if (preg_match('/[\w]{1,}/', $value))
{
// Checking if the value is a number. //
if (is_numeric($value))
{
// Sets the is numeric error. //
return "The post is numeric it must be alpha-numeric.";
}
// The value is not a number so lets proceed. //
else
{
// Now making sure the string is not to short to avoid laziness //
if (strlen($value) < 5)
{
// Sets the error as to short. //
return "The post is to short, It must be greater than 5 characters.";
}
else if (strlen($value) > 12500)
{
// Sets the error as to short. //
return "The post is to long, It must be less than 12500 characters.";
}
}
}
}
// Function to return any errors. //
public function error_return()
{
return $this->error_checking();
}
// Function to return the post with bbcode. html tags removed. //
public function return_bbcode()
{
return $this->sanitize_htmltags();
}
// Function to return the post in html format for display. //
public function return_formatedhtml()
{
return $this->build_paragraphs();
}
}
?>