-
Notifications
You must be signed in to change notification settings - Fork 23
/
ubb.php
150 lines (144 loc) · 5.11 KB
/
ubb.php
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
<?php
/**
* UBB类,暂时仅支持UBB转HTML
* @作者 qinggan <[email protected]>
* @主页 https://www.phpok.com
* @版本 6.x
* @授权 MIT License <https://www.phpok.com/mit.html>
* @时间 2015年2月3日
* @更新 2023年7月20日
**/
/**
* 安全限制,防止直接访问
**/
if(!defined("PHPOK_SET")){
exit("<h1>Access Denied</h1>");
}
class ubb_lib extends _init_lib
{
public function __construct()
{
//
}
public function to_html($info,$nl2br=true)
{
if(is_array($info))
{
foreach($info AS $key=>$value)
{
$value = $this->to_html($value,$nl2br);
$info[$key] = $value;
}
return $info;
}
$info=trim($info);
if($nl2br)
{
$info=str_replace("\n","<br />",$info);
}
$info=preg_replace("/\[hr\]/is","<hr />",$info);
$info=preg_replace("/\[separator\]/is","<br/>",$info);
$info=preg_replace("/\[h([1-6])\](.+?)\[\/h([1-6])\]/is","<h\\1>\\2</h\\1>",$info);
$info=preg_replace("/\[center\](.+?)\[\/center\]/is","<div style='text-align:center'>\\1</div>",$info);
$info=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"\\1\" target='_blank'>\\1</a>",$info);
$info=preg_replace("/\[url=(http:\/\/.+?)\](.+?)\[\/url\]/is","<a href='\\1' target='_blank' title='\\2'>\\2</a>",$info);
$info=preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is","<a href='\\1' target='_blank' title='\\2'>\\2</a>",$info);
$info=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src='\\1'>",$info);
$info=preg_replace("/\[img\s(.+?)\](.+?)\[\/img\]/is","<img \\1 src='\\2'>",$info);
$info=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<span style='color:\\1'>\\2</span>",$info);
$info=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<span style='font-size:\\1'>\\2</span>",$info);
$info=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$info);
$info=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$info);
$info=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$info);
$info=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$info);
$info=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$info);
$info=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$info);
$info=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$info);
$info=preg_replace("/\[del\](.+?)\[\/del\]/is","<del>\\1</del>",$info);
$info=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote><div style='border:1px solid silver;background:#EFFFDF;color:#393939;padding:5px' >\\1</div></blockquote>", $info);
$info = $this->_download($info);
$info = $this->_title($info);
$info = $this->_video($info);
return $info;
}
//PHPOK扩展格式化附件下载
private function _download($Text)
{
//UBB下载格式化
preg_match_all("/\[download[:|:|=]*([0-9]*)\](.*)\[\/download\]/isU",$Text,$list);
if($list && count($list)>0)
{
$dlist = '';
foreach($list[0] AS $key=>$value)
{
$tmpid = $list[1][$key] ? $list[1][$key] : intval($list[2][$key]);
if(!$tmpid)
{
continue;
}
if($list[2][$key] && intval($list[2][$key]) == $tmpid)
{
$resinfo = $GLOBALS['app']->model('res')->get_one($tmpid);
if(!$resinfo)
{
continue;
}
$list[2][$key] = $resinfo['title'];
}
$string = '<a class="download" href="'.$GLOBALS['app']->url('download','','id='.$tmpid).'" title="'.strip_tags($list[2][$key]).'">'.$list[2][$key].'</a>';
$Text = str_replace($value,$string,$Text);
}
}
$list = '';
//格式化旧版的UBB附件下载
preg_match_all("/\[download[:|:]*([0-9]+)\]/isU",$Text,$list);
if($list && count($list)>0)
{
foreach($list[0] AS $key=>$value)
{
if(!$list[1][$key])
{
continue;
}
$rs = $GLOBALS['app']->model('res')->get_one($list[1][$key]);
if(!$rs)
{
continue;
}
$string = '<a class="download" href="'.$GLOBALS['app']->url('download','','id='.$rs['id']).'" title="'.$rs['title'].'">'.$rs['title'].'</a>';
$Text = str_replace($value,$string,$Text);
}
}
return $Text;
}
private function _title($Text)
{
//格式化主题列表
$list = '';
preg_match_all("/\[title[:|:]*([0-9a-zA-Z\_\-]+)\](.+)\[\/title\]/isU",$Text,$list);
if($list && count($list)>0)
{
$dlist = '';
foreach($list[0] AS $key=>$value)
{
if(!$list[1][$key] || !$list[2][$key]) continue;
$string = '<a href="'.$GLOBALS['app']->url($list[1][$key]).'" title="'.strip_tags($list[2][$key]).'" target="_blank">'.$list[2][$key].'</a>';
$Text = str_replace($value,$string,$Text);
}
}
return $Text;
}
//格式化视频,将 embed 插入的视频统一用 iframe 访问
private function _video($Text)
{
//格式化视频链接地址,主要是格式化FLV格式的转换
preg_match_all("/<embed(.+)src=[\"|'](.+)[\"|'](.*)>/isU",$Text,$list);
if($list && $list[2] && $list[0]){
foreach($list[2] as $key=>$value){
$string = '<iframe'.$list[1][$key].' src="'.$value.'"'.$list[3][$key].' frameborder=0 allowfullscreen></iframe>';
$Text = str_replace($list[0][$key],$string,$Text);
}
}
return $Text;
}
}