/
MediaBBCodes.php
447 lines (374 loc) · 12.6 KB
/
MediaBBCodes.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
<?php
/**
* @copyright Copyright (c) 2013 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class s9e_MediaBBCodes
{
/**
* Path to a cache dir, used to cache scraped pages
*/
public static $cacheDir;
public static function install()
{
if (version_compare(phpversion(), '5.3', '<'))
{
throw new RuntimeException('Requires PHP 5.3 or later');
}
}
public static function match($url, $regexps, $scrapes)
{
$vars = array();
if (!empty($regexps))
{
$vars = self::getNamedCaptures($url, $regexps);
}
foreach ($scrapes as $scrape)
{
$scrapeVars = array();
$skip = true;
foreach ($scrape['match'] as $regexp)
{
if (preg_match($regexp, $url, $m))
{
// Add the named captures to the available vars
$scrapeVars += $m;
$skip = false;
}
}
if ($skip)
{
continue;
}
// Add the vars from non-scrape "extract" regexps
$scrapeVars += $vars;
if (isset($scrape['url']))
{
// Use the vars extracted from the media URL plus named captures from the
// scrape URL
$scrapeVars = array_merge($vars, $m);
// Replace {@var} tokens in the URL
$scrapeUrl = preg_replace_callback(
'#\\{@(\\w+)\\}#',
function ($m) use ($scrapeVars)
{
return (isset($scrapeVars[$m[1]])) ? $scrapeVars[$m[1]] : '';
},
$scrape['url']
);
}
else
{
// Use the same URL for scraping
$scrapeUrl = $url;
}
// Overwrite vars extracted from URL with vars extracted from content
$vars = array_merge($vars, self::scrape($scrapeUrl, $scrape['extract']));
}
// No vars = no match
if (empty($vars))
{
return false;
}
// If there's only one capture named "id", we store its value as-is
if (array_keys($vars) === array('id'))
{
return $vars['id'];
}
// If there are more than one capture, or it's not named "id", we store it as a series of
// URL-encoded key=value pairs
$pairs = array();
ksort($vars);
foreach ($vars as $k => $v)
{
$pairs[] = urlencode($k) . '=' . urlencode($v);
}
// NOTE: XenForo silently nukes the mediaKey if it contains any HTML special characters,
// that's why we use ; rather than the standard &
return implode(';', $pairs);
}
public static function embed($mediaKey, $site)
{
if (preg_match('(^https?://)', $mediaKey))
{
// If the URL is stored in the media site, reparse it and store the captures
$vars = self::getNamedCaptures($mediaKey, $site['regexes']);
}
elseif (preg_match('(^(\\w+=[^;]*)(?>;(?1))*$)', $mediaKey))
{
// If the URL looks like a series of key=value pairs, add them to $vars
$vars = array();
foreach (explode(';', $mediaKey) as $pair)
{
list($k, $v) = explode('=', $pair);
$vars[urldecode($k)] = urldecode($v);
}
}
else
{
$vars = array('id' => $mediaKey);
}
// No vars = no match, return a link to the content, or the BBCode as text
if (empty($vars))
{
$mediaKey = htmlspecialchars($mediaKey);
return (preg_match('(^https?://)', $mediaKey))
? "<a href=\"$mediaKey\">$mediaKey</a>"
: "[media={$site['media_site_id']}]{$mediaKey}[/media]";
}
// Test whether this particular site has its own renderer
if (preg_match('(' . __CLASS__ . '::(render\\w+))', $site['embed_html'], $m))
{
$methodName = $m[1];
if (method_exists(__CLASS__, $methodName))
{
return self::$methodName($vars);
}
}
// Otherwise use the configured template
return preg_replace_callback(
// Interpolate {$id} and other {$vars}
'(\\{\\$([a-z]+)\\})',
function ($m) use ($vars)
{
return (isset($vars[$m[1]])) ? htmlspecialchars($vars[$m[1]]) : '';
},
$site['embed_html']
);
}
protected static function scrape($url, $regexps)
{
// Return the content from the cache if applicable
if (isset(self::$cacheDir) && file_exists(self::$cacheDir))
{
$cacheFile = self::$cacheDir . '/http.' . crc32($url) . '.gz';
if (file_exists($cacheFile))
{
$page = file_get_contents('compress.zlib://' . $cacheFile);
}
}
if (empty($page))
{
$page = file_get_contents(
'compress.zlib://' . $url,
false,
stream_context_create(array(
'http' => array(
'header' => 'Accept-Encoding: gzip'
)
))
);
if (isset($cacheFile))
{
file_put_contents($cacheFile, gzencode($page, 9));
}
}
return self::getNamedCaptures($page, $regexps);
}
protected static function getNamedCaptures($string, $regexps)
{
$vars = array();
foreach ($regexps as $regexp)
{
if (preg_match($regexp, $string, $m))
{
foreach ($m as $k => $v)
{
// Add named captures to the vars
if (!is_numeric($k))
{
$vars[$k] = $v;
}
}
}
}
return $vars;
}
public static function renderBandcamp($vars)
{
$html='<iframe width="400" height="'.htmlspecialchars((isset($vars['track_num'])?42:120),2).'" allowfullscreen="" frameborder="0" scrolling="no" src="http://bandcamp.com/EmbeddedPlayer/album='.htmlspecialchars($vars['album_id'],2).'/size=';if(isset($vars['track_num'])){$html.='small/t='.htmlspecialchars($vars['track_num'],2);}else{$html.='medium';}$html.='"/></iframe>';
return $html;
}
public static function matchBandcamp($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!bandcamp\\.com/album/.!'),
'extract' => array('!/album=(?\'album_id\'\\d+)!')
),
array(
'match' => array('!bandcamp\\.com/track/.!'),
'extract' => array('!"album_id":(?\'album_id\'\\d+)!', '!"track_num":(?\'track_num\'\\d+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchBlip($url)
{
$regexps = array('!blip\\.tv/play/(?\'id\'[\\w+%/_]+)!');
$scrapes = array(
array(
'match' => array('!blip\\.tv/[^/]+/[^/]+-\\d+$!'),
'extract' => array('!blip\\.tv/play/(?\'id\'[\\w%+/_]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchColbertnation($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!colbertnation\\.com/[^/]*(?:collection|video)s/!'),
'extract' => array('!(?\'id\'mgid:cms:video:colbertnation\\.com:[0-9]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchComedycentral($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!comedycentral\\.com/video-clips/!'),
'extract' => array('!(?\'id\'mgid:arc:video:comedycentral\\.com:[-\\w]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchDailyshow($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!thedailyshow\\.com/(?:collection|watch)/!'),
'extract' => array('!(?\'id\'mgid:cms:video:thedailyshow\\.com:[0-9]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function renderGrooveshark($vars)
{
$vars += array('playlistid' => null, 'songid' => null);
$html='<object type="application/x-shockwave-flash" typemustmatch="" width="250" height="'.htmlspecialchars((isset($vars['songid'])?40:250),2).'" data="http://grooveshark.com/'.htmlspecialchars((isset($vars['songid'])?'songW':'w'),2).'idget.swf"><param name="allowfullscreen" value="true"/></param><param name="flashvars" value="playlistID='.htmlspecialchars($vars['playlistid'],2).'&songID='.htmlspecialchars($vars['songid'],2).'"/></param><embed type="application/x-shockwave-flash" src="http://grooveshark.com/'.htmlspecialchars((isset($vars['songid'])?'songW':'w'),2).'idget.swf" width="250" height="'.htmlspecialchars((isset($vars['songid'])?40:250),2).'" allowfullscreen="" flashvars="playlistID='.htmlspecialchars($vars['playlistid'],2).'&songID='.htmlspecialchars($vars['songid'],2).'"/></embed></object>';
return $html;
}
public static function matchGrooveshark($url)
{
$regexps = array('%grooveshark\\.com(?:/#!?)?/playlist/[^/]+/(?\'playlistid\'[0-9]+)%');
$scrapes = array(
array(
'url' => 'http://grooveshark.com/s/{@path}',
'match' => array('%grooveshark\\.com(?:/#!?)?/s/(?\'path\'[^/]+/.+)%'),
'extract' => array('%songID=(?\'songid\'[0-9]+)%')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchHulu($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!hulu\\.com/watch/!'),
'extract' => array('!eid=(?\'id\'\\w+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchIndiegogo($url)
{
$regexps = array('!indiegogo\\.com/projects/(?\'id\'[0-9]+)$!');
$scrapes = array(
array(
'match' => array('!indiegogo\\.com/projects/.!'),
'extract' => array('!indiegogo\\.com/projects/(?\'id\'[0-9]+)/!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function renderKickstarter($vars)
{
$vars += array('id' => null);
$html='';if(isset($vars['video'])){$html.='<iframe width="480" height="360" src="http://www.kickstarter.com/projects/'.htmlspecialchars($vars['id'],2).'/widget/video.html" allowfullscreen="" frameborder="0" scrolling="no"/></iframe>';}else{$html.='<iframe width="220" height="380" src="http://www.kickstarter.com/projects/'.htmlspecialchars($vars['id'],2).'/widget/card.html" allowfullscreen="" frameborder="0" scrolling="no"/></iframe>';}
return $html;
}
public static function matchKickstarter($url)
{
$regexps = array('!kickstarter\\.com/projects/(?\'id\'[^/]+/[^/?]+)(?:/widget/(?:(?\'card\'card)|(?\'video\'video)))?!');
$scrapes = array();
return self::match($url, $regexps, $scrapes);
}
public static function matchRutube($url)
{
$regexps = array('!rutube\\.ru/tracks/(?\'id\'[0-9]+)!');
$scrapes = array(
array(
'match' => array('!rutube\\.ru/video/[0-9a-f]{32}!'),
'extract' => array('!rutube\\.ru/video/embed/(?\'id\'[0-9]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchSlideshare($url)
{
$regexps = array('!slideshare\\.net/[^/]+/[-\\w]+-(?\'id\'[0-9]{6,})$!');
$scrapes = array(
array(
'match' => array('!slideshare\\.net/[^/]+/\\w!'),
'extract' => array('!"presentationId":(?\'id\'[0-9]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchTeamcoco($url)
{
$regexps = array();
$scrapes = array(
array(
'match' => array('!teamcoco\\.com/video/.!'),
'extract' => array('!teamcoco\\.com/embed/v/(?\'id\'\\d+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function renderTwitch($vars)
{
$html='<object type="application/x-shockwave-flash" typemustmatch="" width="620" height="378" data="http://www.twitch.tv/widgets/'.htmlspecialchars((isset($vars['archive_id'])||isset($vars['chapter_id'])?'arch':'l'),2).'ive_embed_player.swf"><param name="allowfullscreen" value="true"/></param><param name="flashvars" value="channel='.htmlspecialchars($vars['channel'],2);if(isset($vars['archive_id'])){$html.='&archive_id='.htmlspecialchars($vars['archive_id'],2);}if(isset($vars['chapter_id'])){$html.='&chapter_id='.htmlspecialchars($vars['chapter_id'],2);}$html.='"/></param><embed type="application/x-shockwave-flash" width="620" height="378" src="http://www.twitch.tv/widgets/'.htmlspecialchars((isset($vars['archive_id'])||isset($vars['chapter_id'])?'arch':'l'),2).'ive_embed_player.swf" allowfullscreen=""/></embed></object>';
return $html;
}
public static function matchTwitch($url)
{
$regexps = array('#twitch\\.tv/(?\'channel\'(?!m/)\\w+)(?:/b/(?\'archive_id\'[0-9]+)|/c/(?\'chapter_id\'[0-9]+))?#');
$scrapes = array(
array(
'match' => array('!twitch\\.tv/m/[0-9]+!'),
'extract' => array('!channel=(?\'channel\'[^&]+)&archive_id=(?\'archive_id\'[0-9]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function matchVk($url)
{
$regexps = array('!vk\\.com/video(?\'oid\'-?[0-9]+)_(?\'vid\'[0-9]+)!');
$scrapes = array(
array(
'match' => array('!vk\\.com/video-?[0-9]+_[0-9]+!'),
'extract' => array('!embed_hash=(?\'hash\'[0-9a-f]+)!')
)
);
return self::match($url, $regexps, $scrapes);
}
public static function renderYoutube($vars)
{
$html='<iframe width="560" height="315" allowfullscreen="" frameborder="0" scrolling="no" src="//www.youtube.com/embed/'.htmlspecialchars($vars['id'],2);if(isset($vars['t'])){$html.='?start='.htmlspecialchars($vars['t'],2);}$html.='"/></iframe>';
return $html;
}
public static function matchYoutube($url)
{
$regexps = array('!youtube\\.com/(?:watch\\?.*?v=|v/)(?\'id\'[-0-9A-Z_a-z]+)!', '!youtu\\.be/(?\'id\'[-0-9A-Z_a-z]+)!', '!youtu(?>\\.be|be\\.com).*?[#&?]t=(?\'t\'\\d+)!');
$scrapes = array();
return self::match($url, $regexps, $scrapes);
}
}