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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 |
<?php
/** * IRB_BBdecoder - class interpreter bb-tags * NOTE: Requires PHP version 5 or later and GD version 2.0.1 or later * @package irb_bbdecoder * @author IT studio IRBIS-team * @copyright © 2009 IRBIS-team * @version 0.1 * @license http://www.opensource.org/licenses/rpl1.5.txt */
class IRB_BBdecoder {
private $bb_open; private $bb_close; private $bb_single; private $html_open; private $html_close; private $html_single; private $tmp_open; private $tmp_close; private $tmp_single; private $max_len; private $links; private $images; private $video; private $formatters;
public function __construct() { include_once dirname(__FILE__) .'/config.php';
extract($configBBcode); $this->bb_open = array_keys($setup_bb); $this->bb_close = array_values($setup_bb); $this->html_open = array_keys($setup_html); $this->html_close = array_values($setup_html); $this->bb_single = array_keys($single_tags); $this->html_single = array_values($single_tags);
$this->tmp_open = $tmp_open; $this->tmp_close = $tmp_close; $this->tmp_single = $tmp_single; $this->max_len = $max_len; $this->links = $links; $this->images = $images; $this->video = $video; $this->formatters = $formatters; } /** * Основной метод интерпретатора * @param string $text //обрабатываемый текст * @return string */ public function createBBtags($text) {
$text = str_replace($this->tmp_open, '', $text); $text = str_replace($this->tmp_close, '', $text); $text = str_replace($this->tmp_single, '', $text); $text = str_replace("\r", "", $text); $text = str_replace("\t", " ", $text); $text = str_ireplace($this->bb_open, $this->tmp_open, $text); $text = str_ireplace($this->bb_close, $this->tmp_close, $text); $text = str_ireplace($this->bb_single, $this->tmp_single, $text);
$open_cnt = array(); foreach($this->tmp_open as $k => $v) { $text = preg_replace("#". $v ."\s*?". $this->tmp_close[$k] ."#us", "", $text); $cnt = substr_count($text, $v); if($cnt > 0) { $open_cnt[$v] = $cnt; $close_cnt[$v] = substr_count($text, $this->tmp_close[$k]); } } foreach($open_cnt as $k => $v) { if($v > $close_cnt[$k]) { for($i = 0; $i < $v - $close_cnt[$k]; ++$i) $text = preg_replace('#'. $k .'(?!.*'. $k .')#us', '', $text); } } $text = $this->mBwordwrap($text, $this->max_len); $text = htmlspecialchars($text); if(count($this->formatters)) $text = preg_replace_callback('#\[code=([^\] ]+?)\](.+?)\[/code=\\1\]#si', array($this, 'getFormat'), $text ); if($this->links) { $text = preg_replace_callback('#\[url=http(s*)://([^\] ]+?)\](.+?)\[/url\]#si', array($this, 'createLink1'), $text ); $text = preg_replace_callback('#\[url\]http(s*)://(.+?)\[/url\]#si', array($this, 'createLink2'), $text ); } if($this->images) $text = preg_replace_callback('#\[img\]http://([^\] \?]+?)\[/img\]#si', array($this, 'createImg'), $text ); if($this->video) $text = preg_replace_callback('#\[video\]http://([^\] \?]+?).flv\[/video\]#si', array($this, 'createSwf'), $text ); $text = str_replace($this->tmp_open, $this->html_open, $text); $text = str_replace($this->tmp_close, $this->html_close, $text); $text = str_replace($this->tmp_single, $this->html_single, $text); $text = str_replace(' ', ' ', $text); $text = nl2br($text); return $text; }
/** * Метод очистки текста от BB-тегов * @param string $text * @return string */ public function stripBBtags($text) { $text = str_replace($this->bb_open, '', $text); $text = str_replace($this->bb_close, '', $text); $text = str_replace($this->bb_single, '', $text);
$text = preg_replace('#\\[(code|url|img|video)[^\s]*?\].*?\[/\\1\]#usi', '', $text);
return $text; } /** * Аналог wordwrap() для кодировки UTF-8 * @param string $str //обрабатываеемая строка * @param int $width //максимальная длина слова * @param string $break //разделитель * @return string */ public function mBwordwrap($text, $width = 74, $break = "\n") { return preg_replace('#([^\s]{'. $width .'})#u', '$1'. $break , $text); } /** * Метод генерации ссылок * @param array $match * @return string */ private function createLink1($match) {
$match[2] = str_replace("\n", "", $match[2]);
return '<a href="http'. $match[1] .'://'. htmlspecialchars($match[2])
. '" target="_blanck" >'. htmlspecialchars($match[3]) .'</a>'; } private function createLink2($match) {
$match[2] = str_replace("\n", "", $match[2]);
return '<a href="http'. $match[1] .'://'. htmlspecialchars($match[2])
. '" target="_blanck" >'. htmlspecialchars($match[2]) .'</a>'; }
/** * Метод генерации картинок * @param array $match * @return string */ private function createImg($match) {
$match[1] = str_replace("\n", "", $match[1]);
return '<img src="http://'. htmlspecialchars($match[1]) .'" border="0" />'; }
/** * Метод генерации видеоролика * @param array $match * @return string */ private function createSwf($match) { $match[1] = str_replace("\n", "", $match[1]);
return '<center><object type="application/x-shockwave-flash" data="'
. IRB_BB_PATH .'/images/video.swf" height="300" width="400">'
. '<param name="bgcolor" value="#333333" /><param name="allowFullScreen" value="true" />'
. '<param name="allowScriptAccess" value="always" />'
. '<param name="movie" value="'
. IRB_BB_PATH .'/images/video.swf" />'
. '<param name="FlashVars" value="way=http://'
. htmlspecialchars($match[1]) .'.flv&swf='
. IRB_BB_PATH .'/images/video.swf&w=400&h=300&'
. 'pic=http://&autoplay=0&tools=1&'
. 'skin=white&volume=70&q=&comment=" />'
. '</object></center>'; }
/** * Метод подключения форматтеров. * Подключает нужный и вызывает функцию * @param array $match //массив значений * @return string */ private function getFormat($match) { $format = str_replace("\n", "", strtolower($match[1])); if(in_array($format, $this->formatters)) { include_once dirname(__FILE__) .'/formatters/'. $format .'.php'; return $format($match[2]); } return 'No formatter '. $match[1]; } }
|