summary refs log tree commit diff stats
path: root/includes/bbcode.php
blob: b116e7abc8066e85b37902201d06f09a87c2efb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
       444444444  
      4::::::::4  
     4:::::::::4  
    4::::44::::4  
   4::::4 4::::4   Four Island
  4::::4  4::::4  
 4::::4   4::::4   Written and maintained by Starla Insigna
4::::444444::::444
4::::::::::::::::4  includes/bbcode.php
4444444444:::::444
          4::::4   Please do not use, reproduce or steal the
          4::::4   contents of this file without explicit
          4::::4   permission from Hatkirby.
        44::::::44
        4::::::::4
        4444444444
*/

if (!defined('S_INCLUDE_FILE')) {define('S_INCLUDE_FILE',1);}

require('headerproc.php');

class BBCode
{
	var $init = false;
	var $bbcodes;
	var $bbcodes2;

	function init()
	{
		$this->bbcodes['b'] = '<strong>{CONTENT}</strong>';
		$this->bbcodes['i'] = '<em>{CONTENT}</em>';
		$this->bbcodes['u'] = '<u>{CONTENT}</u>';
		$this->bbcodes['url'] = '<a href="{CONTENT}">{CONTENT}</a>';
		$this->bbcodes2['url'] = '<a href="{PARAM}">{CONTENT}</a>';
		$this->bbcodes['img'] = '<img src="{CONTENT}" alt="Image" />';
		$this->bbcodes2['img'] = '<img src="{CONTENT}" alt="{PARAM}" title="{PARAM}" />';
		$this->bbcodes['big'] = '<big>{CONTENT}</big>';
		$this->bbcodes['small'] = '<small>{CONTENT}</small>';
		$this->bbcodes['ul'] = '<ul>{CONTENT}</ul>';
		$this->bbcodes['ol'] = '<ol>{CONTENT}</ol>';
		$this->bbcodes['li'] = '<li>{CONTENT}</li>';
		$this->bbcodes['code'] = '<code>{CONTENT}</code>';
		$this->bbcodes['pre'] = '<pre><code>{CONTENT}</code></pre>';
		$this->bbcodes['pref'] = '<pre>{CONTENT}</pre>';
		$this->bbcodes2['blog'] = '<a href="/blog/{PARAM}/">{CONTENT}</a>';
		$this->bbcodes['quote'] = '<a href="/quotes/{CONTENT}.php">#{CONTENT}</a>';
		$this->bbcodes2['quote'] = '<a href="/quotes/{PARAM}.php">{CONTENT}</a>';
		$this->bbcodes['ins'] = '<ins>{CONTENT}</ins>';
		$this->bbcodes['del'] = '<del>{CONTENT}</del>';
		$this->bbcodes['bquote'] = '<div class="bquote module unrounded"><blockquote>{CONTENT}</blockquote></div><cite><strong>Anonymous</strong></cite><div class="cleardiv"></div>';
		$this->bbcodes2['bquote'] = '<div class="bquote module unrounded"><blockquote>{CONTENT}</blockquote></div><cite><strong>{PARAM}</strong></cite><div class="cleardiv"></div>';
		$this->bbcodes2['abbr'] = '<abbr title="{PARAM}">{CONTENT}</abbr>';
		$this->bbcodes['hidden'] = '<span style="display: none">{CONTENT}</span>';
		$this->bbcodes['thumb'] = '<a href="/images/{CONTENT}"><img src="http://fourisland.com/thumb.php?file=images/{CONTENT}&amp;mode=scale&amp;by=521&amp;side=0" alt="Image" /></a>';
		$this->bbcodes['thumb2'] = '<a href="/images/{CONTENT}"><img src="http://fourisland.com/thumb.php?file=images/{CONTENT}&amp;mode=scale&amp;by=260&amp;side=0" align="right" alt="Image" /></a>';
		$this->bbcodes['project'] = '<a href="http://projects.fourisland.com/projects/show/{CONTENT}">{CONTENT}</a>';

		$this->init = true;
	}

	function parseBBCode($text)
	{
		if (!$this->init)
		{
			$this->init();
		}

		$to_parse = str_replace("\n",'[br]',htmlentities($text));
		
		foreach ($this->bbcodes as $name => $value)
		{
			while (strpos($to_parse, '[' . $name . ']') !== FALSE)
			{
				$bbcode_uid = unique_id();
				$bbpos = strpos($to_parse, '[' . $name . ']');
				$otag = '[' . $name . ':' . $bbcode_uid . ']';
				$ctag = '[/' . $name . ':' . $bbcode_uid . ']';
				$to_parse = substr_replace($to_parse, $otag, $bbpos, strlen($name) + 2);
				$to_parse = substr_replace($to_parse, $ctag, strpos(substr($to_parse, $bbpos), '[/' . $name . ']') + $bbpos, strlen($name) + 3);

				if (strpos($this->bbcodes[$name], '<pre>') !== -1)
				{
					$to_parse = substr_replace($to_parse, str_replace('[br]', '', substr($to_parse, strpos($to_parse, $otag) + strlen($otag), strpos($to_parse, $ctag) - (strpos($to_parse, $otag) + strlen($otag)))), strpos($to_parse, $otag) + strlen($otag), strpos($to_parse, $ctag) - (strpos($to_parse, $otag) + strlen($otag)));
				}

				$value = str_replace('{CONTENT}', '\1', $value);
				$to_parse = preg_replace('/\[' . $name . ':' . $bbcode_uid . '\](.*)\[\/' . $name . ':' . $bbcode_uid . '\]/', $value, $to_parse);
			}
		}

		foreach ($this->bbcodes2 as $name => $value)
		{
			while (strpos($to_parse, '[' . $name . '=') !== FALSE)
			{
				$bbcode_uid = unique_id();
				$bbpos = strpos($to_parse, '[' . $name . '=');
				$to_parse = substr_replace($to_parse, '[' . $name . ':' . $bbcode_uid . '=', $bbpos, strlen($name) + 2);
				$to_parse = substr_replace($to_parse, '[/' . $name . ':' . $bbcode_uid . ']', strpos(substr($to_parse, $bbpos), '[/' . $name . ']') + $bbpos, strlen($name) + 3);

				$value = str_replace('{PARAM}', '\1', $value);
				$value = str_replace('{CONTENT}', '\2', $value);
				$to_parse = preg_replace('/\[' . $name . ':' . $bbcode_uid . '=([^\]]*)\](.*)\[\/' . $name . ':' . $bbcode_uid . '\]/', $value, $to_parse);
			}
		}

		return str_replace('[br]','<br />',$to_parse);
	}
}

function parseBBCode($text)
{
	static $bbcode;
	if (!isset($bbcode))
	{
		$bbcode = new BBCode();
	}

	return $bbcode->parseBBCode($text);
}

?>