1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | function set_bbcode_bar() |
---|
5 | { |
---|
6 | global $template, $conf, $pwg_loaded_plugins, $page; |
---|
7 | |
---|
8 | load_language('plugin.lang', dirname(__FILE__) . '/'); |
---|
9 | $conf_bbcode_bar = unserialize($conf['bbcode_bar']); |
---|
10 | |
---|
11 | // buttons |
---|
12 | foreach(unserialize(BBcode_codes) as $key) |
---|
13 | { |
---|
14 | if ($conf_bbcode_bar[$key]) $template->assign('BBC_'.$key, true); |
---|
15 | } |
---|
16 | |
---|
17 | // edit field has different id |
---|
18 | // if ( |
---|
19 | // (isset($_GET['action']) AND $_GET['action'] == 'edit_comment') |
---|
20 | // OR (isset($page['body_id']) AND $page['body_id'] == 'theCommentsPage') |
---|
21 | // ) { |
---|
22 | // $template->assign('bbcode_texarea', 'contenteditid'); |
---|
23 | // } else { |
---|
24 | // $template->assign('bbcode_texarea', 'contentid'); |
---|
25 | // } |
---|
26 | $template->assign('bbcode_texarea', 'contentid'); |
---|
27 | |
---|
28 | // smilies |
---|
29 | if (isset($pwg_loaded_plugins['SmiliesSupport'])) |
---|
30 | { |
---|
31 | $template->assign('BBC_smilies', true); |
---|
32 | } |
---|
33 | |
---|
34 | $template->assign('BBCODE_PATH', BBcode_PATH); |
---|
35 | $template->set_prefilter('picture', 'set_bbcode_bar_prefilter'); |
---|
36 | |
---|
37 | // smilies support > 2.2.f ## must be parsed after bbcode_bar, because the javascript must be after bbc's one |
---|
38 | if (isset($pwg_loaded_plugins['SmiliesSupport']) AND strcmp($pwg_loaded_plugins['SmiliesSupport']['version'], '2.2.f') != -1) { |
---|
39 | set_smiliessupport(); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | function set_bbcode_bar_prefilter($content, &$smarty) |
---|
44 | { |
---|
45 | $search = "<label>{'Comment'|@translate}"; |
---|
46 | $replace = file_get_contents(BBcode_PATH.'/template/bbcode_bar.tpl').$search; |
---|
47 | return str_replace($search, $replace, $content); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | //Check tags and eventually close malformed tags, return BBCoded String |
---|
52 | function CheckTags($str) |
---|
53 | { |
---|
54 | //storage stack |
---|
55 | $tags = array(); |
---|
56 | |
---|
57 | for ($pos = 0; $pos<strlen($str); $pos++) |
---|
58 | { |
---|
59 | if ($str{$pos} == '[') |
---|
60 | { |
---|
61 | $end_pos = strpos($str, ']', $pos); |
---|
62 | $tag = substr($str, ++$pos, $end_pos-$pos); |
---|
63 | //deals with tags which contains arguments (ie quote) |
---|
64 | if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE) |
---|
65 | $tag = substr($tag, 0, $equal_pos); |
---|
66 | //check whether we have a defined tag or not. |
---|
67 | if (in_array(strtolower($tag),unserialize(BBcode_codes)) || in_array(strtolower(substr($tag,1)),unserialize(BBcode_codes))) |
---|
68 | { |
---|
69 | //closing tag |
---|
70 | if ($tag{0} == '/') |
---|
71 | { |
---|
72 | //cleaned tag |
---|
73 | $tag = substr($tag, 1); |
---|
74 | $before_tag = substr($str, 0, $pos-1); |
---|
75 | $after_tag = substr($str, $end_pos+1); |
---|
76 | //pop stack |
---|
77 | while (($temp = array_pop($tags))) |
---|
78 | { |
---|
79 | if ($temp != $tag) { |
---|
80 | $before_tag.='[/'.$temp.']'; |
---|
81 | } else { |
---|
82 | $before_tag.='[/'.$tag.']'; |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str); |
---|
87 | $str = $before_tag.$after_tag; |
---|
88 | } else { // push stack |
---|
89 | array_push($tags,$tag); |
---|
90 | } |
---|
91 | } |
---|
92 | $pos = $end_pos; |
---|
93 | } |
---|
94 | } |
---|
95 | // empty stack and closing tags |
---|
96 | while ($temp = array_pop($tags)) |
---|
97 | { |
---|
98 | $str.='[/'.$temp.']'; |
---|
99 | } |
---|
100 | return $str; |
---|
101 | } |
---|
102 | |
---|
103 | // return string, HTML version of BBCoded $str |
---|
104 | function BBCodeParse($str) |
---|
105 | { |
---|
106 | global $conf; |
---|
107 | |
---|
108 | $conf_bbcode_bar = unserialize($conf['bbcode_bar']); |
---|
109 | $str = CheckTags(nl2br($str)); |
---|
110 | |
---|
111 | $patterns = array(); |
---|
112 | $replacements = array(); |
---|
113 | |
---|
114 | if ($conf_bbcode_bar['p']) |
---|
115 | { |
---|
116 | //Paragraph |
---|
117 | $patterns[] = '#\[p\](.*?)\[/p\]#is'; |
---|
118 | $replacements[] = '<p>\\1</p>'; |
---|
119 | } |
---|
120 | if ($conf_bbcode_bar['b']) |
---|
121 | { |
---|
122 | // Bold |
---|
123 | $patterns[] = '#\[b\](.*?)\[/b\]#is'; |
---|
124 | $replacements[] = '<b>\\1</b>'; |
---|
125 | } |
---|
126 | if ($conf_bbcode_bar['i']) |
---|
127 | { |
---|
128 | //Italic |
---|
129 | $patterns[] = '#\[i\](.*?)\[/i\]#is'; |
---|
130 | $replacements[] = '<i>\\1</i>'; |
---|
131 | } |
---|
132 | if ($conf_bbcode_bar['u']) |
---|
133 | { |
---|
134 | //Underline |
---|
135 | $patterns[] = '#\[u\](.*?)\[\/u\]#is'; |
---|
136 | $replacements[] = '<u>\\1</u>'; |
---|
137 | } |
---|
138 | if ($conf_bbcode_bar['s']) |
---|
139 | { |
---|
140 | //Strikethrough |
---|
141 | $patterns[] = '#\[s\](.*?)\[/s\]#is'; |
---|
142 | $replacements[] = '<s>\\1</s>'; |
---|
143 | } |
---|
144 | if ($conf_bbcode_bar['center']) |
---|
145 | { |
---|
146 | //Center |
---|
147 | $patterns[] = '#\[center\](.*?)\[/center\]#is'; |
---|
148 | $replacements[] = '<div align="center"><p>\\1</p></div>'; |
---|
149 | } |
---|
150 | if ($conf_bbcode_bar['right']) |
---|
151 | { |
---|
152 | //Right |
---|
153 | $patterns[] = '#\[right\](.*?)\[/right\]#is'; |
---|
154 | $replacements[] = '<div align="right"><p>\\1</p></div>'; |
---|
155 | } |
---|
156 | if ($conf_bbcode_bar['ol']) |
---|
157 | { |
---|
158 | //Olist |
---|
159 | $patterns[] = '#\[ol\](.*?)\[/ol\]#is'; |
---|
160 | $replacements[] = '<ol>\\1</ol>'; |
---|
161 | } |
---|
162 | if ($conf_bbcode_bar['ul']) |
---|
163 | { |
---|
164 | //Ulist |
---|
165 | $patterns[] = '#\[ul\](.*?)\[/ul\]#is'; |
---|
166 | $replacements[] = '<ul>\\1</ul>'; |
---|
167 | } |
---|
168 | if ($conf_bbcode_bar['ol'] || $conf_bbcode_bar['ul']) |
---|
169 | { |
---|
170 | //List |
---|
171 | $patterns[] = '#\[li\](.*?)\[/li\]#is'; |
---|
172 | $replacements[] = '<li>\\1</li>'; |
---|
173 | } |
---|
174 | if ($conf_bbcode_bar['quote']) |
---|
175 | { |
---|
176 | // Quotes |
---|
177 | $patterns[] = "#\[quote\](.*?)\[/quote\]#is"; |
---|
178 | $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal">\\1</span></blockquote>'; |
---|
179 | |
---|
180 | //Quotes with "user" |
---|
181 | $patterns[] = "#\[quote="(.*?)"\](.*?)\[/quote\]#is"; |
---|
182 | $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal"><b>\\1 : </b><br/>\\2</span></blockquote>'; |
---|
183 | |
---|
184 | //Quotes with user |
---|
185 | $patterns[] = "#\[quote=(.*?)\](.*?)\[/quote\]#is"; |
---|
186 | $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal"><b>\\1 : </b><br/>\\2</span></blockquote>'; |
---|
187 | } |
---|
188 | if ($conf_bbcode_bar['img']) |
---|
189 | { |
---|
190 | //Images |
---|
191 | $patterns[] = "#\[img\](.*?)\[/img\]#si"; |
---|
192 | $replacements[] = '<img src="\\1" />'; |
---|
193 | } |
---|
194 | if ($conf_bbcode_bar['url']) |
---|
195 | { |
---|
196 | //[url]xxxx://www.zzzz.yyy[/url] |
---|
197 | $patterns[] = "#\[url\]([\w]+?://[^ \"\n\r\t<]*?)\[/url\]#is"; |
---|
198 | $replacements[] = '<a href="\\1" target="_blank">\\1</a>'; |
---|
199 | |
---|
200 | //[url]www.zzzzz.yyy[/url] |
---|
201 | $patterns[] = "#\[url\]((www|ftp)\.[^ \"\n\r\t<]*?)\[/url\]#is"; |
---|
202 | $replacements[] = '<a href="http://\\1" target="_blank">\\1</a>'; |
---|
203 | |
---|
204 | //[url=xxxx://www.zzzzz.yyy]ZzZzZ[/url] |
---|
205 | $patterns[] = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; |
---|
206 | $replacements[] = '<a href="\\1" target="_blank">\\2</a>'; |
---|
207 | |
---|
208 | //[url=www.zzzzz.yyy]zZzZz[/url] |
---|
209 | $patterns[] = "#\[url=((www|ftp)\.[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; |
---|
210 | $replacements[] = '<a href="http://\\1" target="_blank">\\2</a>'; |
---|
211 | |
---|
212 | //[url="www.zzzzz.yyy"]zZzZz[/url] |
---|
213 | $patterns[] = "#\[url="((www|ftp)\.[^ \n\r\t<]*?)"\](.*?)\[/url\]#is"; |
---|
214 | $replacements[] = '<a href="http://\\1" target="_blank">\\3</a>'; |
---|
215 | |
---|
216 | //[url="http://www.zzzzz.yyy"]zZzZz[/url] |
---|
217 | $patterns[] = "#\[url="([\w]+?://[^ \n\r\t<]*?)"\](.*?)\[/url\]#is"; |
---|
218 | $replacements[] = '<a href="\\1" target="_blank">\\2</a>'; |
---|
219 | } |
---|
220 | if ($conf_bbcode_bar['email']) |
---|
221 | { |
---|
222 | //[email]samvure@gmail.com[/email] |
---|
223 | $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#is"; |
---|
224 | $replacements[] = '<a href="mailto:\\1">\\1</a>'; |
---|
225 | } |
---|
226 | if ($conf_bbcode_bar['size']) |
---|
227 | { |
---|
228 | //Size |
---|
229 | $patterns[] = "#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si"; |
---|
230 | $replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>'; |
---|
231 | } |
---|
232 | if ($conf_bbcode_bar['color']) |
---|
233 | { |
---|
234 | //Colours |
---|
235 | $patterns[] = "#\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*?)\[/color\]#si"; |
---|
236 | $replacements[] = '<span style="color: \\1">\\2</span>'; |
---|
237 | } |
---|
238 | |
---|
239 | return preg_replace($patterns, $replacements, $str); |
---|
240 | } |
---|
241 | |
---|
242 | ?> |
---|