', $level);
$list .= "\n\n";
return $list;
}
}
/**
* Generates the list items and proper depth
*
* First check if the item should be higher than the current level
* - Close the list and previous lists
*
* Then check if the item should go lower than the current level
* - If the list doesn't open on level one, use the Offset
* - Open appropriate sub lists
*
* Otherwise the item is on the same as level as the previous item
*
* @param int $ItemLevel Current item level
* @param int $Level Current list level
* @param str $List reference to an XHTML string
* @param int $i Iterator digit
* @param int $Offset If the list doesn't start at level 1
*/
private static function headline_level(&$ItemLevel, &$Level, &$List, $i, &$Offset)
{
if ($ItemLevel < $Level) {
$diff = $Level - $ItemLevel;
$List .= '' . str_repeat('', $diff);
} elseif ($ItemLevel > $Level) {
$diff = $ItemLevel - $Level;
if ($Offset > 0) {
$List .= str_repeat('
';
}
}
/*
* Hax prevention
*
* That's the original comment on this.
* Most likely this was implemented to avoid anyone nesting enough elements
* to reach PHP's memory limit as nested elements are solved recursively.
* Original value of 10, it is now replaced in favor of $MaximumNests.
*
* If this line is ever executed then something is, in fact, being haxed.
* The if before the block type switch for different tags should always be limiting ahead of this line.
*/
private static function to_html($Array)
{
self::$Levels++;
// Hax prevention, breaks upon exceeding nests
if (self::$Levels > self::$MaximumNests) {
return $Block['Val'];
}
$Str = '';
foreach ($Array as $Block) {
if (is_string($Block)) {
$Str .= self::smileys($Block);
continue;
}
if (self::$Levels < self::$MaximumNests) {
switch ($Block['Type']) {
case 'b':
$Str .= ''.self::to_html($Block['Val']).'';
break;
case 'u':
$Str .= ''.self::to_html($Block['Val']).'';
break;
case 'i':
$Str .= ''.self::to_html($Block['Val'])."";
break;
case 's':
$Str .= ''.self::to_html($Block['Val']).'';
break;
case 'important':
$Str .= ''.self::to_html($Block['Val']).'';
break;
case 'user':
$Str .= ''.$Block['Val'].'';
break;
case 'artist':
$Str .= ''.$Block['Val'].'';
break;
case 'rule':
$Rule = trim(strtolower($Block['Val']));
if ($Rule[0] !== 'r' && $Rule[0] !== 'h') {
$Rule = 'r'.$Rule;
}
$Str .= ''.preg_replace('/[aA-zZ]/', '', $Block['Val']).'';
break;
case 'torrent':
$Pattern = '/('.SITE_DOMAIN.'\/torrents\.php.*[\?&]id=)?(\d+)($|&|\#).*/i';
$Matches = [];
if (preg_match($Pattern, $Block['Val'], $Matches)) {
if (isset($Matches[2])) {
$GroupID = $Matches[2];
$Groups = Torrents::get_groups(array($GroupID), true, true, false);
if ($Groups[$GroupID]) {
$Group = $Groups[$GroupID];
$Str .= Artists::display_artists($Group['Artists']).'';
} else {
$Str .= '[torrent]'.str_replace('[inlineurl]', '', $Block['Val']).'[/torrent]';
}
}
} else {
$Str .= '[torrent]'.str_replace('[inlineurl]', '', $Block['Val']).'[/torrent]';
}
break;
case 'wiki':
$Str .= ''.$Block['Val'].'';
break;
case 'tex':
$Str .= '';
break;
case 'plain':
$Str .= $Block['Val'];
break;
case 'pre':
$Str .= '
'.$Block['Val'].'
';
break;
case 'code':
$Str .= ''.$Block['Val'].'';
break;
/*
case 'ch':
$Str .= '';
break;
*/
/*
case 'uch':
$Str .= '';
break;
*/
case 'list':
$Str .= "<$Block[ListType] class=\"postlist\">";
foreach ($Block['Val'] as $Line) {
$Str .= '
';
}
break;
case 'color':
case 'colour':
$ValidAttribs = array('aqua', 'black', 'blue', 'fuchsia', 'green', 'grey', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'white', 'yellow');
if (!in_array($Block['Attr'], $ValidAttribs) && !preg_match('/^#[0-9a-f]{6}$/', $Block['Attr'])) {
$Str .= '[color='.$Block['Attr'].']'.self::to_html($Block['Val']).'[/color]';
} else {
$Str .= ''.self::to_html($Block['Val']).'';
}
break;
case 'headline':
$text = self::to_html($Block['Val']);
$raw = self::raw_text($Block['Val']);
if (!in_array($Block['Attr'], self::$HeadlineLevels)) {
$Str .= sprintf('%1$s%2$s%1$s', str_repeat('=', $Block['Attr'] + 1), $text);
} else {
$id = '_' . crc32($raw . self::$HeadlineID);
if (self::$InQuotes === 0) {
self::$Headlines[] = array($Block['Attr'], $raw, $id);
}
$Str .= sprintf('%2$s', ($Block['Attr'] + 2), $text, $id);
self::$HeadlineID++;
}
break;
case 'inlinesize':
case 'size':
$ValidAttribs = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
if (!in_array($Block['Attr'], $ValidAttribs)) {
$Str .= '[size='.$Block['Attr'].']'.self::to_html($Block['Val']).'[/size]';
} else {
$Str .= ''.self::to_html($Block['Val']).'';
}
break;
case 'quote':
self::$NoImg++; // No images inside quote tags
self::$InQuotes++;
if (self::$InQuotes === self::$NestsBeforeHide) { // Put quotes that are nested beyond the specified limit in [hide] tags.
$Str .= 'Older quotes: Show';
$Str .= '
';
}
if (!empty($Block['Attr'])) {
$Exploded = explode('|', self::to_html($Block['Attr']));
if (isset($Exploded[1]) && (is_numeric($Exploded[1]) || (in_array($Exploded[1][0], array('a', 't', 'c', 'r')) && is_numeric(substr($Exploded[1], 1))))) {
// The part after | is either a number or starts with a, t, c or r, followed by a number (forum post, artist comment, torrent comment, collage comment or request comment, respectively)
$PostID = trim($Exploded[1]);
$Str .= ''.$Exploded[0].' wrote: ';
} else {
$Str .= ''.$Exploded[0].' wrote: ';
}
}
$Str .= '
'.self::to_html($Block['Val']).'
';
if (self::$InQuotes === self::$NestsBeforeHide) { //Close quote the deeply nested quote [hide].
$Str .= '
'; // Ensure new line after quote train hiding
}
self::$NoImg--;
self::$InQuotes--;
break;
case 'hide':
$Str .= ''.(($Block['Attr']) ? $Block['Attr'] : 'Hidden text').': Show';
$Str .= '
'.self::to_html($Block['Val']).'
';
break;
case 'img':
if (self::$NoImg > 0 && self::valid_url($Block['Val'])) {
$Str .= ''.$Block['Val'].' (image)';
break;
}
if (!self::valid_url($Block['Val'], '\.(jpe?g|gif|png|bmp|tiff)')) {
$Str .= '[img]'.$Block['Val'].'[/img]';
} else {
$LocalURL = self::local_url($Block['Val']);
if ($LocalURL) {
$Str .= '';
} else {
$Str .= '';
}
}
break;
case 'aud':
if (self::$NoImg > 0 && self::valid_url($Block['Val'])) {
$Str .= ''.$Block['Val'].' (audio)';
break;
}
if (!self::valid_url($Block['Val'], '\.(mp3|ogg|wav)')) {
$Str .= '[aud]'.$Block['Val'].'[/aud]';
} else {
// todo: Proxy this for staff?
$Str .= '';
}
break;
case 'url':
// Make sure the URL has a label
if (empty($Block['Val'])) {
$Block['Val'] = $Block['Attr'];
$NoName = true; // If there isn't a Val for this
} else {
$Block['Val'] = self::to_html($Block['Val']);
$NoName = false;
}
if (!self::valid_url($Block['Attr'])) {
$Str .= '[url='.$Block['Attr'].']'.$Block['Val'].'[/url]';
} else {
$LocalURL = self::local_url($Block['Attr']);
if ($LocalURL) {
if ($NoName) {
$Block['Val'] = substr($LocalURL, 1);
}
$Str .= ''.$Block['Val'].'';
} else {
$Str .= ''.$Block['Val'].'';
}
}
break;
case 'inlineurl':
if (!self::valid_url($Block['Attr'], '', true)) {
$Array = self::parse($Block['Attr']);
$Block['Attr'] = $Array;
$Str .= self::to_html($Block['Attr']);
} else {
$LocalURL = self::local_url($Block['Attr']);
if ($LocalURL) {
$Str .= ''.substr($LocalURL, 1).'';
} else {
$Str .= ''.$Block['Attr'].'';
}
}
break;
case 'embed':
$Val = str_replace(' ', '', $Block['Val']);
if (self::valid_url($Val) && substr($Val, -4) === 'webm') {
$Str .= '';
}
break;
}
}
}
self::$Levels--;
return $Str;
}
/**
* raw_text()
*/
private static function raw_text($Array)
{
$Str = '';
foreach ($Array as $Block) {
if (is_string($Block)) {
$Str .= $Block;
continue;
}
switch ($Block['Type']) {
case 'headline':
break;
case 'b':
case 'u':
case 'i':
case 's':
case 'color':
case 'size':
case 'quote':
case 'align':
$Str .= self::raw_text($Block['Val']);
break;
case 'tex': // Since this will never strip cleanly, just remove it
break;
case 'artist':
case 'user':
case 'wiki':
case 'pre':
case 'code':
case 'aud':
case 'img':
$Str .= $Block['Val'];
break;
case 'list':
foreach ($Block['Val'] as $Line) {
$Str .= $Block['Tag'].self::raw_text($Line);
}
break;
case 'url':
// Make sure the URL has a label
if (empty($Block['Val'])) {
$Block['Val'] = $Block['Attr'];
} else {
$Block['Val'] = self::raw_text($Block['Val']);
}
$Str .= $Block['Val'];
break;
case 'inlineurl':
if (!self::valid_url($Block['Attr'], '', true)) {
$Array = self::parse($Block['Attr']);
$Block['Attr'] = $Array;
$Str .= self::raw_text($Block['Attr']);
} else {
$Str .= $Block['Attr'];
}
break;
}
}
return $Str;
}
/**
* smileys()
*/
private static function smileys($Str)
{
return $Str;
/*
if (!empty(G::$LoggedUser['DisableSmileys'])) {
return $Str;
}
if (count(self::$ProcessedSmileys) === 0 && count(self::$Smileys) > 0) {
foreach (self::$Smileys as $Key => $Val) {
self::$ProcessedSmileys[$Key] = '';
}
reset(self::$ProcessedSmileys);
}
$Str = strtr($Str, self::$ProcessedSmileys);
return $Str;
*/
}
}
/*
// Uncomment this part to test the class via command line:
function display_str($Str) {
return $Str;
}
function check_perms($Perm) {
return true;
}
$Str = "hello
[pre]http://anonym.to/?http://whatshirts.portmerch.com/
====hi====
===hi===
==hi==[/pre]
====hi====
hi";
echo Text::full_format($Str);
echo "\n"
*/