-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathEmbedVideo.hooks.php
457 lines (418 loc) · 12.7 KB
/
EmbedVideo.hooks.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* Wrapper class for encapsulating EmbedVideo related parser methods
*/
abstract class EmbedVideo {
protected static $initialized = false;
/**
* Sets up parser functions.
*/
public static function setup() {
// Setup parser hooks. ev is the primary hook, evp is supported for
// legacy purposes
global $wgVersion;
$prefix = version_compare($wgVersion, '1.7', '<') ? '#' : '';
self::addMagicWord($prefix, "ev", "EmbedVideo::parserFunction_ev");
self::addMagicWord($prefix, "evp", "EmbedVideo::parserFunction_evp");
return true;
}
private static function addMagicWord($prefix, $word, $function) {
global $wgParser;
$wgParser->setFunctionHook($prefix . $word, $function);
}
/**
* Adds magic words for parser functions.
* @param array $magicWords
* @param string $langCode
*
* @return bool Always true
*/
public static function parserFunctionMagic(&$magicWords, $langCode='en') {
$magicWords['evp'] = array(0, 'evp');
$magicWords['ev'] = array(0, 'ev');
return true;
}
/**
* Embeds video of the chosen service, legacy support for 'evp' version of
* the tag
* @param Parser $parser Instance of running Parser.
* @param String $service Which online service has the video.
* @param String $id Identifier of the chosen service
* @param String $width Width of video (optional)
* @return String Encoded representation of input params (to be processed later)
*/
public static function parserFunction_evp($parser, $service = null, $id = null, $desc = null, $align = null, $width = null) {
return self::parserFunction_ev($parser, $service, $id, $width, $align, $desc);
}
/**
* Embeds video of the chosen service
* @param Parser $parser Instance of running Parser.
* @param String $service Which online service has the video.
* @param String $id Identifier of the chosen service
* @param String $width Width of video (optional)
* @param String $desc description to show (optional)
* @param String $align alignment of the video (optional)
* @return String Encoded representation of input params (to be processed later)
*/
public static function parserFunction_ev($parser, $service = null, $id = null, $width = null, $align = null, $desc = null) {
global $wgScriptPath;
// Initialize things once
if (!self::$initialized) {
self::VerifyWidthMinAndMax();
self::$initialized = true;
}
// Get the name of the host
if ($service === null || $id === null) {
return self::errMissingParams($service, $id);
}
$service = trim($service);
$id = trim($id);
$desc = $parser->recursiveTagParse($desc);
$entry = self::getServiceEntry($service);
if (!$entry) {
return self::errBadService($service);
}
if (!self::sanitizeWidth($entry, $width)) {
return self::errBadWidth($width);
}
$height = self::getHeight($entry, $width);
$hasalign = ($align !== null || $align == 'auto');
if ($hasalign) {
$align = trim($align);
if ( !self::validateAlignment($align) ) {
return self::errBadAlignment($align);
}
$desc = self::getDescriptionMarkup($desc);
}
// If the service has an ID pattern specified, verify the id number
if (!self::verifyID($entry, $id)) {
return self::errBadID($service, $id);
}
$url = null;
// If service is Yandex -> use own parser
if ($service == 'yandex' || $service == 'yandexvideo') {
$url = self::getYandex($id);
$url = htmlspecialchars_decode($url);
}
// if the service has it's own custom extern declaration, use that instead
if (array_key_exists ('extern', $entry) && ($clause = $entry['extern']) != NULL) {
if ($service == 'screen9') {
$clause = self::parseScreen9Id( $id, $width, $height );
if ($clause == null) {
return self::errBadScreen9Id();
}
} else {
$clause = wfMsgReplaceArgs($clause, array($wgScriptPath, $id, $width, $height, $url));
}
if ($hasalign) {
$clause = self::generateAlignExternClause($clause, $align, $desc, $width, $height);
}
return array($clause, 'noparse' => true, 'isHTML' => true);
}
// Build URL and output embedded flash object
$url = wfMsgReplaceArgs($entry['url'], array($id, $width, $height));
$clause = "";
// If service is RuTube -> use own parser
if ($service == 'rutube'){
$url = self::getRuTube($id);
}
if ($hasalign) {
$clause = self::generateAlignClause($url, $width, $height, $align, $desc);
}
else {
$clause = self::generateNormalClause($url, $width, $height);
}
return array($clause, 'noparse' => true, 'isHTML' => true);
}
/**
* Return the HTML necessary to embed the video normally.
*
* @param string $url
* @param int $width
* @param int $height
*
* @return string
*/
private static function generateNormalClause($url, $width, $height) {
$clause = "<object width=\"{$width}\" height=\"{$height}\">" .
"<param name=\"movie\" value=\"{$url}\"></param>" .
"<param name=\"wmode\" value=\"transparent\"></param>" .
"<embed src=\"{$url}\" type=\"application/x-shockwave-flash\"" .
" wmode=\"transparent\" width=\"{$width}\" height=\"{$height}\">" .
"</embed></object>";
return $clause;
}
/**
* The HTML necessary to embed the video with a custom embedding clause,
* specified align and description text
*
* @param string $clause
* @param string $align
* @param string $desc
* @param int $width
* @param int $height
*
* @return string
*/
private static function generateAlignExternClause($clause, $align, $desc, $width, $height)
{
$alignClass = self::getAlignmentClass($align);
$clause = "<div class=\"thumb {$alignClass}\">" .
"<div class=\"thumbinner\" style=\"width: {$width}px;\">" .
$clause .
"<div class=\"thumbcaption\">" .
$desc .
"</div></div></div>";
return $clause;
}
/**
* Generate the HTML necessary to embed the video with the given alignment
* and text description
*
* @param string $url
* @param int $width
* @param int $height
* @param string $align
* @param string $desc
*
* @return string
*/
private static function generateAlignClause($url, $width, $height, $align, $desc) {
$alignClass = self::getAlignmentClass($align);
$clause = "<div class=\"thumb {$alignClass}\">" .
"<div class=\"thumbinner\" style=\"width: {$width}px;\">" .
"<object width=\"{$width}\" height=\"{$height}\">" .
"<param name=\"movie\" value=\"{$url}\"></param>" .
"<param name=\"wmode\" value=\"transparent\"></param>" .
"<embed src=\"{$url}\" type=\"application/x-shockwave-flash\"" .
" wmode=\"transparent\" width=\"{$width}\" height=\"{$height}\"></embed>" .
"</object>" .
"<div class=\"thumbcaption\">" .
$desc .
"</div></div></div>";
return $clause;
}
/**
* Get the entry for the specified service, by name
*
* @param string $service
*
* @return $string
*/
private static function getServiceEntry($service) {
// Get the entry in the list of services
global $wgEmbedVideoServiceList;
return $wgEmbedVideoServiceList[$service];
}
/**
* Get the width. If there is no width specified, try to find a default
* width value for the service. If that isn't set, default to 425.
* If a width value is provided, verify that it is numerical and that it
* falls between the specified min and max size values. Return true if
* the width is suitable, false otherwise.
*
* @param string $service
*
* @return mixed
*/
private static function sanitizeWidth($entry, &$width) {
global $wgEmbedVideoMinWidth, $wgEmbedVideoMaxWidth;
if ($width === null || $width == '*' || $width == '') {
if (isset($entry['default_width'])) {
$width = $entry['default_width'];
}
else {
$width = 425;
}
return true;
}
if (!is_numeric($width)) {
return false;
}
return $width >= $wgEmbedVideoMinWidth && $width <= $wgEmbedVideoMaxWidth;
}
/**
* Validate the align parameter.
*
* @param string $align The align parameter
*
* @return {\code true} if the align parameter is valid, otherwise {\code false}.
*/
private static function validateAlignment($align) {
return ($align == 'left' || $align == 'right' || $align == 'center' || $align == 'auto');
}
private static function getAlignmentClass($align) {
if ( $align == 'left' || $align == 'right' ) {
return 't' . $align;
}
return $align;
}
/**
* Calculate the height from the given width. The default ratio is 450/350,
* but that may be overridden for some sites.
*
* @param int $entry
* @param int $width
*
* @return int
*/
private static function getHeight($entry, $width) {
$ratio = 4 / 3;
if (isset($entry['default_ratio'])) {
$ratio = $entry['default_ratio'];
}
return round($width / $ratio);
}
/**
* If we have a textual description, get the markup necessary to display
* it on the page.
*
* @param string $desc
*
* @return string
*/
private static function getDescriptionMarkup($desc) {
if ($desc !== null) {
return "<div class=\"thumbcaption\">$desc</div>";
}
return "";
}
/**
* Verify the id number of the video, if a pattern is provided.
*
* @param string $entry
* @param string $id
*
* @return bool
*/
private static function verifyID($entry, $id) {
$idhtml = htmlspecialchars($id);
//$idpattern = (isset($entry['id_pattern']) ? $entry['id_pattern'] : '%[^A-Za-z0-9_\\-]%');
//if ($idhtml == null || preg_match($idpattern, $idhtml)) {
return ($idhtml != null);
}
/**
* Get an error message for the case where the ID value is bad
*
* @param string $service
* @param string $id
*
* @return string
*/
private static function errBadID($service, $id) {
$idhtml = htmlspecialchars($id);
$msg = wfMsgForContent('embedvideo-bad-id', $idhtml, @htmlspecialchars($service));
return '<div class="errorbox">' . $msg . '</div>';
}
/**
* Get an error message if the width is bad
*
* @param int $width
*
* @return string
*/
private static function errBadWidth($width) {
$msg = wfMsgForContent('embedvideo-illegal-width', @htmlspecialchars($width));
return '<div class="errorbox">' . $msg . '</div>';
}
/**
* Get an error message if there are missing parameters
*
* @param string $service
* @param string $id
*
* @return string
*/
private static function errMissingParams($service, $id) {
return '<div class="errorbox">' . wfMsg('embedvideo-missing-params') . '</div>';
}
/**
* Get an error message if the service name is bad
*
* @param string $service
*
* @return string
*/
private static function errBadService($service) {
$msg = wfMsg('embedvideo-unrecognized-service', @htmlspecialchars($service));
return '<div class="errorbox">' . $msg . '</div>';
}
/**
* Get an error message for an invalid align parameter
*
* @param string $align The given align parameter.
*
* @return string
*/
private static function errBadAlignment($align) {
$msg = wfMsg('embedvideo-illegal-alignment', @htmlspecialchars($align));
return '<div class="errorbox">' . $msg . '</div>';
}
/**
* Get an error message for an invalid screen 9 id.
*
* @return string
*/
private static function errBadScreen9Id() {
$msg = wfMsg('embedvideo-illegal-screen9-id');
return '<div class="errorbox">' . $msg . '</div>';
}
/**
* Verify that the min and max values for width are sane.
*
* @return void
*/
private static function VerifyWidthMinAndMax() {
global $wgEmbedVideoMinWidth, $wgEmbedVideoMaxWidth;
if (!is_numeric($wgEmbedVideoMinWidth) || $wgEmbedVideoMinWidth < 100) {
$wgEmbedVideoMinWidth = 100;
}
if (!is_numeric($wgEmbedVideoMaxWidth) || $wgEmbedVideoMaxWidth > 1024) {
$wgEmbedVideoMaxWidth = 1024;
}
}
private static function getRuTube($id)
{
/**
* Custom parser for RuTube
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://rutube.ru/oembed/track?&url=http://rutube.ru/tracks/{$id}.html&format=json");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, false);
$json = curl_exec($ch);
curl_close($ch);
$start=strpos($json, 'value=\"')+8;
$end=strpos($json, '\"></');
$url=substr($json, $start, $end-$start);
return $url;
}
private static function getYandex($id)
{
/**
* Custom parser for Yandex
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://video.yandex.ru/oembed.xml?url=http://video.yandex.ru/users/{$id}");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, false);
$json = curl_exec($ch);
curl_close($ch);
$start=strpos($json, '<html>')+6;
$end=strpos($json, '</html>');
$url=substr($json, $start, $end-$start);
return $url;
}
private static function parseScreen9Id($id, $width, $height)
{
$parser = new Screen9IdParser();
if (! $parser->parse( $id ) ) {
return null;
}
$parser->setWidth( $width );
$parser->setHeight( $height );
return $parser->toString();
}
}