forked from k0a1a/hotglue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_video.inc.php
323 lines (280 loc) · 7.97 KB
/
module_video.inc.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
<?php
/*
* module_video.inc.php
* Module for embedding video elements on a page
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('html.inc.php');
require_once('html_parse.inc.php');
require_once('modules.inc.php');
// module glue gets loaded on demand
require_once('util.inc.php');
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
function video_alter_save($args)
{
$elem = $args['elem'];
$obj = &$args['obj'];
if (!elem_has_class($elem, 'video')) {
return false;
}
// parse children elements to find video
$childs = html_parse(elem_val($elem));
$v = false;
foreach ($childs as $c) {
if (elem_tag($c) == 'video') {
$v = $c;
break;
}
}
if (!$v) {
log_msg('warn', 'video_alter_save: no video element found, inner html is '.var_dump_inl($childs));
return false;
}
// autoplay
if (elem_attr($v, 'autoplay') !== NULL) {
$obj['video-autoplay'] = 'autoplay';
} else {
$obj['video-autoplay'] = '';
}
// loop
if (elem_attr($v, 'loop') !== NULL) {
$obj['video-loop'] = 'loop';
} else {
unset($obj['video-loop']);
}
// controls
if (elem_attr($v, 'controls') !== NULL) {
$obj['video-controls'] = 'controls';
} else {
unset($obj['video-controls']);
}
// volume
if (elem_attr($v, 'audio') == 'muted') {
$obj['video-volume'] = '0';
} else {
unset($obj['video-volume']);
}
}
function video_delete_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'video') {
return false;
}
load_modules('glue');
if (!empty($obj['video-file'])) {
$pn = array_shift(expl('.', $obj['name']));
delete_upload(array('pagename'=>$pn, 'file'=>$obj['video-file'], 'max_cnt'=>1));
}
}
function video_has_reference($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'video') {
return false;
}
// symlinks have their referenced files in a different page that's why
// they are not relevant here
if (@is_link(CONTENT_DIR.'/'.str_replace('.', '/', $obj['name']))) {
return false;
}
if (!empty($obj['video-file']) && $obj['video-file'] == $args['file']) {
return true;
} else {
return false;
}
}
function video_alter_render_early($args)
{
$elem = &$args['elem'];
$obj = $args['obj'];
if (!elem_has_class($elem, 'video')) {
return false;
}
// add a css (for viewing as well as editing)
html_add_css(base_url().'modules/video/video.css');
$v = elem('video');
if (empty($obj['video-file'])) {
elem_attr($v, 'src', '');
} else {
// TODO (later): support URLs as well
if (SHORT_URLS) {
elem_attr($v, 'src', base_url().urlencode($obj['name']));
} else {
elem_attr($v, 'src', base_url().'?'.urlencode($obj['name']));
}
}
elem_css($v, 'width', '100%');
elem_css($v, 'height', '100%');
// we're currently not preloading the video due to some troubles on
// Firefox
//elem_css($v, 'preload', 'preload');
// set some fallback text
if (!empty($obj['video-file']) && !empty($obj['video-file-mime'])) {
elem_val($v, '<div class="video-fallback">You are not seeing the video because your browser does not support '.htmlspecialchars($obj['video-file-mime'], ENT_NOQUOTES, 'UTF-8').'. Consider using a contemporary web browser.</div>');
} else {
elem_val($v, '<div class="video-fallback">You are not seeing the video because your browser does not support it. Consider using a contemporary web browser.</div>');
}
// autoplay
if (!isset($obj['video-autoplay']) || $obj['video-autoplay'] == 'autoplay') {
// autoplay is the default
elem_attr($v, 'autoplay', 'autoplay');
} else {
if (VIDEO_START_ON_CLICK) {
elem_attr($v, 'onclick', 'this.play()');
}
}
// loop
if (!empty($obj['video-loop'])) {
elem_attr($v, 'loop', 'loop');
}
// controls
if (!empty($obj['video-controls'])) {
elem_attr($v, 'controls', 'controls');
}
// volume
if (isset($obj['video-volume']) && $obj['video-volume'] == '0') {
elem_attr($v, 'audio', 'muted');
}
elem_append($elem, $v);
return true;
}
function video_render_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'video') {
return false;
}
$e = elem('div');
elem_attr($e, 'id', $obj['name']);
elem_add_class($e, 'video');
elem_add_class($e, 'resizable');
elem_add_class($e, 'object');
// hooks
invoke_hook_first('alter_render_early', 'video', array('obj'=>$obj, 'elem'=>&$e, 'edit'=>$args['edit']));
$html = elem_finalize($e);
invoke_hook_last('alter_render_late', 'video', array('obj'=>$obj, 'html'=>&$html, 'elem'=>$e, 'edit'=>$args['edit']));
return $html;
}
function video_render_page_early($args)
{
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url().'modules/video/video-edit.min.js');
} else {
html_add_js(base_url().'modules/video/video-edit.js');
}
html_add_css(base_url().'modules/video/video-edit.css');
}
}
function video_save_state($args)
{
$elem = $args['elem'];
$obj = $args['obj'];
if (array_shift(elem_classes($elem)) != 'video') {
return false;
}
// make sure the type is set
$obj['type'] = 'video';
$obj['module'] = 'video';
// hook
invoke_hook('alter_save', array('obj'=>&$obj, 'elem'=>$elem));
load_modules('glue');
$ret = save_object($obj);
if ($ret['#error']) {
log_msg('error', 'video_save_state: save_object returned '.quot($ret['#data']));
return false;
} else {
return true;
}
}
function video_serve_resource($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'video') {
return false;
}
if (!empty($obj['video-file'])) {
$pn = array_shift(expl('.', $obj['name']));
if (empty($obj['video-file-mime'])) {
$obj['video-file-mime'] = '';
}
serve_file(CONTENT_DIR.'/'.$pn.'/shared/'.$obj['video-file'], $args['dl'], $obj['video-file-mime']);
}
return false;
}
function video_snapshot_symlink($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'video') {
return false;
}
$dest_dir = CONTENT_DIR.'/'.array_shift(expl('.', $obj['name'])).'/shared';
$src_file = CONTENT_DIR.'/'.array_shift(expl('.', $args['origin'])).'/shared/'.$obj['video-file'];
if (($f = dir_has_same_file($dest_dir, $src_file)) !== false) {
$obj['video-file'] = $f;
} else {
// copy file
$dest_file = $dest_dir.'/'.unique_filename($dest_dir, $src_file);
$m = umask(0111);
if (!(@copy($src_file, $dest_file))) {
umask($m);
log_msg('error', 'video_snapshot_symlink: error copying referenced file '.quot($src_file).' to '.quot($dest_file));
return false;
}
umask($m);
$obj['video-file'] = basename($dest_file);
log_msg('info', 'video_snapshot_symlink: copied referenced file to '.quot($dest_file));
}
$ret = save_object($obj);
if ($ret['#error']) {
log_msg('error', 'video_snapshot_symlink: error saving object '.quot($obj['name']));
return false;
} else {
return true;
}
}
function video_upload($args)
{
$ext = filext($args['file']);
if ($args['mime'] == 'video/ogg' || $ext == 'ogv' || $ext == 'ogg') {
// notice: we also handle ogg here although this also could be a
// different mime type
// make sure mime type is set
$mime = 'video/ogg';
} elseif ($args['mime'] == 'video/h264' || $ext == 'h264') {
// haven't seen these out there
$mime = 'video/h264';
} elseif ($args['mime'] == 'video/mp4' || $ext == 'mp4') {
// think this need not be h264, but well
$mime = 'video/mp4';
} elseif ($args['mime'] == 'video/webm' || $ext == 'webm') {
// again, webm could also be audio/webm
$mime = 'video/webm';
} else {
return false;
}
load_modules('glue');
$obj = create_object($args);
if ($obj['#error']) {
return false;
} else {
$obj = $obj['#data'];
}
$obj['type'] = 'video';
$obj['module'] = 'video';
$obj['video-file'] = $args['file'];
$obj['video-file-mime'] = $mime;
save_object($obj);
$ret = render_object(array('name'=>$obj['name'], 'edit'=>true));
if ($ret['#error']) {
return false;
} else {
return $ret['#data'];
}
}