-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse_video_filter.module
255 lines (223 loc) · 8.14 KB
/
sse_video_filter.module
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
<?php
/**
* Implements hook_filter_info().
*/
function sse_video_filter_filter_info() {
$filters = array();
$filters['sse_video_filter'] = array(
'title' => t('优酷土豆视频过滤'),
'description' => t('将 [video:URL] 替换成嵌入的视频'),
'process callback' => '_sse_video_filter_process',
'settings callback' => '_sse_video_filter_settings',
'default settings' => array(
'sse_video_filter_width' => '400',
'sse_video_filter_height' => '400',
),
'tips callback' => '_sse_video_filter_tips',
// See http://drupal.org/node/1061244.
'weight' => -1,
);
return $filters;
}
function _sse_video_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$settings['sse_video_filter_width'] = array(
'#type' => 'textfield',
'#title' => t('默认视频宽度'),
'#default_value' => isset($filter->settings['sse_video_filter_width']) ? $filter->settings['sse_video_filter_width'] : $defaults['sse_video_filter_width'],
'#maxlength' => 4,
);
$settings['sse_video_filter_height'] = array(
'#type' => 'textfield',
'#title' => t('默认视频高度'),
'#default_value' => isset($filter->settings['sse_video_filter_height']) ? $filter->settings['sse_video_filter_height'] : $defaults['sse_video_filter_height'],
'#maxlength' => 4,
);
return $settings;
}
function _sse_video_filter_tips($filter, $format, $long = FALSE) {
if ($long) {
return t('
<p>插入视频:<code>[video:url]</code></p>
<p>基本用法:<code>[video:http://v.youku.com/v_show/xxx.html]</code></p>
<p>设置长宽:<code>[video:http://v.youku.com/v_show/xxx.html width:800 height:600]</code></p>
<p>设置画面比:<code>[video:http://v.youku.com/v_show/xxx.html width:800 height:600 ratio:4/3]</code></p>
');
}
else {
return t('您可以通过输入 [video:URL] 来插入优酷或土豆的视频。');
}
}
function _sse_video_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$video = array(
'source' => $matches_code[2][$ci],
);
$video['source'] = _sse_video_filter_get_swf($video['source']);
if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
$ratio = 1;
if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio)) {
// Validate given ratio parameter.
$ratio = $tratio[1] / $tratio[2];
}
// First, check if user has set a width.
if (isset($video['width']) && !isset($video['height'])) {
$video['height'] = $filter->settings['sse_video_filter_height'];
}
// Else, if user has set height.
elseif (isset($video['height']) && !isset($video['width'])) {
$video['width'] = $video['height'] * $ratio;
}
// Maybe both?
elseif (isset($video['height']) && isset($video['width'])) {
$video['width'] = $video['width'];
$video['height'] = $video['height'];
}
// Fall back to defaults.
elseif (!isset($video['height']) && !isset($video['width'])) {
$video['width'] = $filter->settings['sse_video_filter_width'] != '' ? $filter->settings['sse_video_filter_width'] : 400;
$video['height'] = $filter->settings['sse_video_filter_height'] != '' ? $filter->settings['sse_video_filter_height'] : 400;
}
// Default value for control bar height.
$control_bar_height = 0;
if (isset($video['control_bar_height'])) {
// Respect control_bar_height option if present.
$control_bar_height = $video['control_bar_height'];
}
// Resize to fit within width and height repecting aspect ratio.
if ($ratio) {
$scale_factor = min(array(
($video['height'] - $control_bar_height),
$video['width'] / $ratio,
));
$video['height'] = round($scale_factor + $control_bar_height);
$video['width'] = round($scale_factor * $ratio);
}
$video['align'] = (isset($video['align']) && in_array($video['align'], array(
'left',
'right',
'center',
))) ? $video['align'] : NULL;
// Let modules have final say on video parameters.
drupal_alter('sse_video_filter_video', $video);
$replacement = sse_video_filter_flash($video);
$text = str_replace($code, $replacement, $text);
}
}
return $text;
}
/**
* Wrapper that calls the theme function.
*/
function sse_video_filter_flash($video, $params = array()) {
return theme('sse_video_filter_flash', array('video' => $video, 'params' => $params));
}
/**
* Function that outputs the <object> element.
*
* @ingroup themeable
*/
function theme_sse_video_filter_flash($variables) {
$output = '';
$video = $variables['video'];
$params = isset($variables['params']) ? $variables['params'] : array();
$output .= '<object type="application/x-shockwave-flash" ';
$output .= 'width="' . $video['width'] . '" height="' . $video['height'] . '" data="' . $video['source'] . '">' . "\n";
$defaults = array(
'movie' => $video['source'],
'wmode' => 'transparent',
'allowFullScreen' => 'true',
);
$params = array_merge($defaults, (is_array($params) && count($params)) ? $params : array());
foreach ($params as $name => $value) {
$output .= ' <param name="' . $name . '" value="' . $value . '" />' . "\n";
}
$output .= '</object>' . "\n";
return $output;
}
/**
* Implements hook_theme().
*/
function sse_video_filter_theme($existing, $type, $theme, $path) {
return array(
'sse_video_filter_flash' => array(
'variables' => array('video' => NULL, 'params' => NULL),
),
);
}
/**
* Implements hook_element_info_alter().
*/
function sse_video_filter_element_info_alter(&$types) {
if (isset($types['text_format']['#pre_render']) && is_array($types['text_format']['#pre_render'])) {
if (in_array('ckeditor_pre_render_text_format', $types['text_format']['#pre_render'])) {
_sse_video_filter_add_settings('ckeditor');
}
}
}
/*
* 根据用户提交的(swf/html)地址,获取优酷,土豆的swf播放地址
* */
function _sse_video_filter_get_swf($url = '') {
if (isset($url) && !empty($url)) {
preg_match_all('/http:\/\/(.*?)?\.(.*?)?\.com\/(.*)/', $url, $types);
} else {
return false;
}
$type = $types[2][0];
$domain = $types[1][0];
$isswf = strpos($types[3][0], 'v.swf') !== false;
$method = substr($types[3][0],0,1);
switch ($type) {
case 'youku' :
if ($domain == 'player') {
$swf = $url;
} else if ($domain == 'v') {
preg_match_all('/http:\/\/v\.youku\.com\/v_show\/id_(.*)?\.html/', $url, $url_array);
$swf = 'http://player.youku.com/player.php/sid/'.str_replace('/', '', $url_array[1][0]).'/v.swf';
} else {
$swf = $url;
}
break;
case 'tudou' :
if ($isswf) {
$swf = $url;
} else {
$method = $method == 'p' ? 'v' : $method ;
preg_match_all('/http:\/\/www.tudou\.com\/(.*)?\/(.*)?/', $url, $url_array);
$str_arr = explode('/',$url_array[1][0]);
$count = count($str_arr);
if ($count == 1) {
$id = explode('.', $url_array[2][0])[0];
} else if ($count == 2){
$id = $str_arr[1];
} else if ($count == 3){
$id = $str_arr[2];
}
$swf = 'http://www.tudou.com/'.$method.'/'.$id.'/v.swf';
}
break;
default:
$swf = $url;
break;
}
return $swf;
}
function _sse_video_filter_add_settings($editor) {
static $editor_settings_added = array();
static $global_settings_added = FALSE;
if (!$global_settings_added) {
$global_settings_added = TRUE;
// Add global settings for sse_video_filter.
$settings = array(
'sse_video_filter' => array(
'modulepath' => drupal_get_path('module', 'sse_video_filter'),
),
);
drupal_add_js($settings, 'setting');
}
}