forked from nothingisover/smartideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartideo.php
410 lines (345 loc) · 18.2 KB
/
smartideo.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/*
Plugin Name: Smartideo
Plugin URI: https://www.rifuyiri.net/t/3639
Description: Smartideo 是为 WordPress 添加对在线视频支持的一款插件(支持手机、平板等设备HTML5播放)。 目前支持优酷、搜狐视频、腾讯视频、爱奇艺、哔哩哔哩,酷6、华数、乐视、YouTube 等网站。
Version: 2.7.0
Author: Fens Liu
Author URI: https://www.rifuyiri.net/t/3639
*/
define('SMARTIDEO_VERSION', '2.7.0');
define('SMARTIDEO_URL', plugins_url('', __FILE__));
define('SMARTIDEO_PATH', dirname( __FILE__ ));
$smartideo = new smartideo();
class smartideo{
private $edit = false;
private $width = '100%';
private $height = '500px';
private $youku_client_id = 'd0b1b77a17cded3b';
private $bilibili_pc_player = 0;
private $option = array();
public function __construct(){
if(is_admin()){
add_action('admin_menu', array($this, 'admin_menu'));
$this->edit = true;
}
$option = get_option('smartideo_option');
if(!empty($option)){
$option = json_decode($option, true);
}else{
$option = array();
}
$this->option = $option;
extract($option);
if(!empty($youku_client_id) && strlen($youku_client_id) == 16){
$this->youku_client_id = $youku_client_id;
}
$this->bilibili_pc_player = isset($bilibili_pc_player) ? $bilibili_pc_player : 0;
add_action('wp_enqueue_scripts', array($this, 'smartideo_scripts'));
wp_embed_unregister_handler('youku');
wp_embed_unregister_handler('tudou');
wp_embed_unregister_handler('56com');
wp_embed_unregister_handler('youtube');
// video
wp_embed_register_handler( 'smartideo_youku',
'#https?://(?:[v|vo]+)\.youku\.com/v_show/id_(?<video_id>[a-z0-9_=\-]+)#i',
array($this, 'smartideo_embed_handler_youku') );
wp_embed_register_handler( 'smartideo_qq',
'#https?://v\.qq\.com/(?:[a-z0-9_\./]+\?vid=(?<video_id1>[a-z0-9_=\-]+)|(?:[a-z0-9/]+)/(?<video_id2>[a-z0-9_=\-]+))#i',
array($this, 'smartideo_embed_handler_qq') );
wp_embed_register_handler( 'smartideo_sohu',
'#https?://my\.tv\.sohu\.com/(?:pl|us)/(?:\d+)/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_sohu') );
wp_embed_register_handler( 'smartideo_wasu',
'#https?://www\.wasu\.cn/play/show/id/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_wasu') );
wp_embed_register_handler( 'smartideo_youtube',
'#https?://www\.youtube\.com/watch\?v=(?<video_id>[a-zA-Z0-9_=\-]+)#i',
array($this, 'smartideo_embed_handler_youtube') );
wp_embed_register_handler( 'smartideo_acfun',
'#https?://www\.acfun\.(?:[tv|cn]+)/v/ac(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_acfun') );
wp_embed_register_handler( 'smartideo_bilibili',
'#https?://www\.bilibili\.com/video/av(?:(?<video_id1>\d+)/(?:index_|\#page=)(?<video_id2>\d+)|(?<video_id>\d+))#i',
array($this, 'smartideo_embed_handler_bilibili') );
wp_embed_register_handler( 'smartideo_miaopai',
'#https?://www\.miaopai\.com/show/(?<video_id>[a-z0-9_~\-]+)#i',
array($this, 'smartideo_embed_handler_miaopai') );
wp_embed_register_handler( 'smartideo_weibo',
'#https?://weibo\.com/tv/v/(?:[a-z0-9_\./]+\?fid=1034:(?<video_id>[a-z0-9_=\-]+))#i',
array($this, 'smartideo_embed_handler_weibo') );
wp_embed_register_handler( 'smartideo_iqiyi',
'#https?://www\.iqiyi\.com/(?:[a-zA-Z]+)_(?<video_id>[a-z0-9_~\-]+)#i',
array($this, 'smartideo_embed_handler_iqiyi') );
wp_embed_register_handler( 'smartideo_56',
'#https?://(?:www\.)?56\.com/[a-z0-9]+/(?:play_album\-aid\-[0-9]+_vid\-(?<video_id1>[a-z0-9_=\-]+)|v_(?<video_id2>[a-z0-9_=\-]+))#i',
array($this, 'smartideo_embed_handler_56') );
// Not supported HTML5
wp_embed_register_handler( 'smartideo_yinyuetai',
'#https?://(?:[www|v]+)\.yinyuetai\.com/video/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_yinyuetai') );
wp_embed_register_handler( 'smartideo_ku6',
'#https?://v\.ku6\.com/show/(?<video_id>[a-z0-9\-_\.]+).html#i',
array($this, 'smartideo_embed_handler_ku6') );
wp_embed_register_handler( 'smartideo_letv',
'#https?://(?:[a-z0-9/]+\.)?(?:[letv|le]+)\.com/(?:[a-z0-9/]+)/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_letv') );
wp_embed_register_handler( 'smartideo_hunantv',
'#https?://www\.(?:[hunantv|mgtv]+)\.com/(?:[a-z0-9/]+)/(?<video_id>\d+)\.html#i',
array($this, 'smartideo_embed_handler_hunantv') );
wp_embed_register_handler( 'smartideo_meipai',
'#https?://(?:www\.)?meipai\.com/media/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_meipai') );
wp_embed_register_handler( 'smartideo_tudou',
'#https?://(?:www\.)?tudou\.com/(?:programs/view|listplay/(?<list_id>[a-z0-9_=\-]+))/(?<video_id>[a-z0-9_=\-]+)#i',
array($this, 'smartideo_embed_handler_tudou') );
// music
wp_embed_register_handler( 'smartideo_music163',
'#https?://music\.163\.com/\#/song\?id=(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_music163') );
wp_embed_register_handler( 'smartideo_musicqq',
'#https?://y\.qq\.com/n/yqq/song/(?<video_id>\w+)\.html#i',
array($this, 'smartideo_embed_handler_musicqq') );
wp_embed_register_handler( 'smartideo_xiami',
'#https?://www\.xiami\.com/song/(?<video_id>\d+)#i',
array($this, 'smartideo_embed_handler_xiami') );
}
# video
public function smartideo_embed_handler_youku( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("//player.youku.com/embed/{$matches['video_id']}?client_id={$this->youku_client_id}", $url);
return apply_filters( 'embed_youku', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_qq( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = $matches['video_id1'] == '' ? $matches['video_id2'] : $matches['video_id1'];
$embed = $this->get_iframe("//v.qq.com/iframe/player.html?vid={$matches['video_id']}&auto=0", $url);
return apply_filters( 'embed_qq', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_sohu( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("//tv.sohu.com/upload/static/share/share_play.html#{$matches['video_id']}_0_0_9001_0", $url);
return apply_filters( 'embed_sohu', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_wasu( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("http://www.wasu.cn/Play/iframe/id/{$matches['video_id']}", $url);
return apply_filters( 'embed_wasu', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("//www.youtube.com/embed/{$matches['video_id']}", $url);
return apply_filters( 'embed_youtube', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_bilibili( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
$cid = '';
/*
try{
$request = new WP_Http();
$url = "https://api.bilibili.com/view?type=jsonp&appkey=8e9fc618fbd41e28&id=" . $matches['video_id'];
$data = (array)$request->request($url, array('timeout' => 3));
$json = json_decode($data['body'], true);
$cid = $json['cid'];
}catch(Exception $e){}
*/
if(wp_is_mobile() || $this->bilibili_pc_player == 1){
$embed = $this->get_iframe("//player.bilibili.com/player.html?aid={$matches['video_id']}&cid={$cid}&page={$page}", $url);
}else{
$embed = $this->get_link($url);
}
return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_meipai( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_link($url);
return apply_filters( 'embed_meipai', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_miaopai( $matches, $attr, $url, $rawattr ) {
if(wp_is_mobile()){
$embed = $this->get_iframe("//gslb.miaopai.com/stream/{$matches['video_id']}.mp4", $url);
}else{
$embed = $this->get_embed("//wscdn.miaopai.com/splayer2.2.0.swf?scid={$matches['video_id']}&token=&autopause=true", $url);
}
return apply_filters( 'embed_miaopai', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_iqiyi( $matches, $attr, $url, $rawattr ) {
$embed = '';
try{
$request = new WP_Http();
$data = (array)$request->request($url, array('timeout' => 3));
if(!isset($data['body'])){
$data['data'] = '';
}
preg_match('/"vid":"(\w+)"/i', (string)$data['body'], $match);
$vid = $match[1];
preg_match('/"tvId":(\d+)/i', (string)$data['body'], $match);
$tvid = $match[1];
if ($tvid > 0 && !empty($vid)) {
$embed = $this->get_iframe("//open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid={$vid}&tvId={$tvid}&height=100%&width=100%&autoplay=0", $url);
}
}catch(Exception $e){}
if(empty($embed)){
$embed = '解析失败,请刷新页面重试';
}
return apply_filters( 'embed_iqiyi', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_56( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = $matches['video_id1'] == '' ? $matches['video_id2'] : $matches['video_id1'];
$embed = $this->get_iframe("http://www.56.com/iframe/{$matches['video_id']}", $url);
return apply_filters( 'embed_56', $embed, $matches, $attr, $url, $rawattr );
}
# video widthout h5
public function smartideo_embed_handler_weibo( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_embed("http://video.weibo.com/player/1034:{$matches['video_id']}/v.swf", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_weibo', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_yinyuetai( $matches, $attr, $url, $rawattr ){
$embed = $this->get_embed("http://player.yinyuetai.com/video/player/{$matches['video_id']}/v_0.swf", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_yinyuetai', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_ku6( $matches, $attr, $url, $rawattr ){
$embed = $this->get_embed("http://player.ku6.com/refer/{$matches['video_id']}/v.swf", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_ku6', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_letv($matches, $attr, $url, $rawattr){
$embed = $this->get_embed("http://img1.c0.letv.com/ptv/player/swfPlayer.swf?id={$matches['video_id']}&autoplay=0", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_letv', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_hunantv( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_embed("//i1.hunantv.com/ui/swf/share/player.swf?video_id={$matches['video_id']}&autoplay=0", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_hunantv', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_acfun( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_embed("http://cdn.aixifan.com/player/ACFlashPlayer.out.swf?type=page&url=http://www.acfun.cn/v/ac{$matches['video_id']}", $url);
if(wp_is_mobile()){
$embed = $this->get_link($url);
}
return apply_filters( 'embed_acfun', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_tudou( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_link($url);
return apply_filters( 'embed_tudou', $embed, $matches, $attr, $url, $rawattr );
}
# music
public function smartideo_embed_handler_music163( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("//music.163.com/outchain/player?type=2&id={$matches['video_id']}&auto=0&height=90", '', '100%', '110px');
return apply_filters( 'embed_music163', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_musicqq( $matches, $attr, $url, $rawattr ) {
$embed = $this->get_iframe("//cc.stream.qqmusic.qq.com/C100{$matches['video_id']}.m4a?fromtag=52", '', '100%', '110px');
return apply_filters( 'embed_musicqq', $embed, $matches, $attr, $url, $rawattr );
}
public function smartideo_embed_handler_xiami( $matches, $attr, $url, $rawattr ) {
$embed =
'<div class="smartideo" style="background: transparent;">
<script src="http://www.xiami.com/widget/player-single?uid=0&sid='.$matches['video_id'].'&autoplay=0&mode=js" type="text/javascript"></script>
</div>';
return apply_filters( 'embed_xiami', $embed, $matches, $attr, $url, $rawattr );
}
private function get_embed($url = '', $source = '', $width = '', $height = ''){
$html = '';
$html .=
'<div class="smartideo">
<div class="player"' . $this->get_size_style($width, $height) . '>
<embed src="' . $url . '" allowFullScreen="true" quality="high" width="100%" height="100%" allowScriptAccess="always" type="application/x-shockwave-flash" wmode="transparent"></embed>
</div>';
if($this->option['tips_status'] == 1 && !$this->edit){
if(empty($source)){
$source = 'javascript:void(0);';
}
$html .=
'<div class="tips">
<a href="' . $source . '" target="_blank" smartideo-title="' . $this->option['tips_content'] . '" smartideo-title-mobile="' . $this->option['tips_content_mobile'] . '" title="' . $this->option['tips_content'] . '" class="smartideo-tips" rel="nofollow"></a>
</div>';
}
$html .= '</div>';
return $html;
}
private function get_iframe($url = '', $source = '', $width = '', $height = ''){
$html = '';
$html .=
'<div class="smartideo">
<div class="player"' . $this->get_size_style($width, $height) . '>
<iframe src="' . $url . '" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>
</div>';
if(isset($this->option['tips_status']) && $this->option['tips_status'] == 1 && !$this->edit){
if(empty($source)){
$source = 'javascript:void(0);';
}
$html .=
'<div class="tips">
<a href="' . $source . '" target="_blank" smartideo-title="' . $this->option['tips_content'] . '" smartideo-title-mobile="' . $this->option['tips_content_mobile'] . '" title="' . $this->option['tips_content'] . '" class="smartideo-tips" rel="nofollow"></a>
</div>';
}
$html .= '</div>';
return $html;
}
private function get_link($url){
$html = '';
$html .=
'<div class="smartideo">
<div class="player"' . $this->get_size_style(0, 0) . '>
<a href="' . $url . '" target="_blank" class="smartideo-play-link"><div class="smartideo-play-button"></div></a>
<p style="color: #999;margin-top: 50px;">暂时无法播放,可回源网站播放</p>
</div>
</div>';
return $html;
}
public function smartideo_scripts(){
wp_enqueue_style('smartideo_css', SMARTIDEO_URL . '/static/smartideo.css', array(), SMARTIDEO_VERSION, 'screen');
wp_enqueue_script('smartideo_js', SMARTIDEO_URL . '/static/smartideo.js', array(), SMARTIDEO_VERSION, true);
}
public function admin_menu(){
add_plugins_page('Smartideo 设置', 'Smartideo 设置', 'manage_options', 'smartideo_settings', array($this, 'admin_settings'));
}
public function admin_settings(){
require_once 'smartideo-admin.php';
new smartideo_admin();
}
private function is_https(){
if(strtolower($_SERVER['HTTPS']) == 'on'){
return true;
}else{
return false;
}
}
private function get_size_style($width, $height){
if(empty($width) || empty($height)){
$width = $height = 0;
if($this->edit){
$width = $this->width;
$height = $this->height;
}else if($this->option['response'] == 0){
if(wp_is_mobile()){
$width = $this->option['width_mobile'];
$height = $this->option['height_mobile'];
}else{
$width = $this->option['width'];
$height = $this->option['height'];
}
}
}
$style = '';
if(!empty($width)){
$style .= "width: {$width};";
}
if(!empty($height)){
$style .= "height: {$height};";
}
if(!empty($style)){
$style = ' style="' . $style . '"';
}
return $style;
}
}