-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMediawikiPlayer.php
executable file
·303 lines (255 loc) · 8.07 KB
/
MediawikiPlayer.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
<?php
/**
**********************************************************
* Name: MediawikiPlayer
* Desc: Embedds a mediaplayer into a wiki page
*
* @version: 0.3.0
*
* @author: Andrew Fitzgerald ([email protected])
* Homepage: http://www.mediawiki.org/wiki/Extension:MediawikiPlayer
* http://www.swiftlytilting.com/
*
* License: GNU GPL
*
* @file
***********************************************************
*/
// using $wgScriptPath so head em off at the pass
if ( !defined( 'MEDIAWIKI' ) ) {
echo "Not a valid entry point";
exit( 1 );
}
$wgExtensionCredits['parserhook'][] = array(
'name' => 'MediawikiPlayer',
'author' =>'[http://swiftlytilting.com Andrew Fitzgerald]',
'url' => 'http://www.mediawiki.org/wiki/Extension:MediawikiPlayer',
'description' => 'Embeds a media player into a wiki page',
'descriptionmsg' => "mediawikiplayer-desc", // Same as above but name of a message, for i18n - string, added in 1.12.0
'version' => '0.3.0',
'path' => __FILE__,
);
//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
$wgHooks['ParserFirstCallInit'][] = 'MediawikiPlayer::setup';
} else { // Otherwise do things the old fashioned way
$wgExtensionFunctions[] = 'MediawikiPlayer::setup';
}
class MediawikiPlayer
{
protected static
$id = 0,
$addOns = array(),
$jwPlayerJsLoaded = false,
$swfJsLoaded = false,
$paramNames = array(
'allowfullscreen',
'allowscriptaccess',
'wmode',
'flashvars'
);
/**
* Set up the extension variables and functions
*/
static function setup()
{
global
$wgParser,
$wgMWPlayerConfig,
$wgMWPlayerDefaultSettings,$wgScriptPath,
$wgMWPlayerDir,
$wgMWPlayerUseSWFObject;
$wgMWPlayerDir = $wgScriptPath . '/extensions/MediawikiPlayer';
$wgMWPlayerConfig = array();
$wgMWPlayerUseSWFObject = false;
$wgMWPlayerDefaultSettings = array(
'width' => '400',
'height' => '300',
'allowfullscreen' => 'true',
'backcolor' => 'eeeeee',
);
$wgParser->setHook( 'mediaplayer', array(__CLASS__, 'render') );
$wgParser->setHook( 'mp3player', array(__CLASS__, 'renderMP3') );
foreach(self::$addOns as $addOn)
{
call_user_func(array($addOn,'setup'),$wgParser);
}
return true;
}
/**
* Handler for mp3player tag
*/
static function renderMP3( $input, $args, $parser )
{
$args['height'] = '24';
$args['controlbar'] = 'bottom';
return self::render( $input, $args, $parser);
}
/**
* Handler for mediaplayer tag
*/
static function render( $input, $args, $parser )
{
global
$wgScriptPath,
$wgContLang,
$wgMWPlayerDir,
$wgMWPlayerDefaultSettings,
$wgMWPlayerConfig,
$wgMWPlayerUseSWFObject;
++self::$id;
$useSWF = $wgMWPlayerUseSWFObject;
// see if user wants to use old style embedding on this particular video
if (array_key_exists('useswf', $args))
{
$useSWF = true;
if (strtolower($args['useswf']) === 'false')
{
$useSWF = false;
}
unset($args['useSWF']);
}
$currentID = 'MWPlayer-' . (array_key_exists('id', $args) ? $args['id'] : self::$id);
// load international name of File namespac
$namespaceNames = $wgContLang->getNamespaces();
$fileNS = strtolower($namespaceNames[NS_FILE]);
$ns = strtolower(substr($input,0,5));
// check to see if a file specified
if ($ns == 'file:' || $ns == ($fileNS . ':'))
{
$image = wfFindFile(substr($input,5));
if ($image)
{
$input = $image->getFullURL();
}
else
{
return 'Error loading file:' . Xml::encodeJsVar(substr($input,5));
}
}
// if a configuration was set in the tag, and the configuration eists in
// wgMWPlayerConfig, load the config
if (isset($wgMWPlayerConfig)
&& array_key_exists('MWPlayerConfig', $args)
&& array_key_exists($args['MWPlayerConfig'], $wgMWPlayerConfig)
)
{
$wgMWPlayerDefaultSettings = $wgMWPlayerConfig[$args['MWPlayerConfig']];
unset($args['MWPlayerConfig']);
}
// load the default settings if setting is not supplied in tag args
foreach ($wgMWPlayerDefaultSettings as $n => $v)
{
if (!array_key_exists($n, $args))
{ $args[$n] = $v;
}
}
// use file arg if no input set
if ((!array_key_exists('file',$args)) && (trim($input) != ''))
{ $args['file'] = $input;
}
// Set up HTML
$wgMWPlayerIDHTML = htmlspecialchars($currentID);
$wgMWPlayerIDJS = Xml::encodeJsVar($currentID);
$code = "<span id=\"$wgMWPlayerIDHTML\">The media player is loading...</span>\n";
// Load the script if this is the first player of this type on the page
if (($useSWF && !self::$swfJsLoaded)
|| (!$useSWF && !self::$jwPlayerJsLoaded)
)
{
$scriptUrl = htmlspecialchars(
$wgMWPlayerDir . ($useSWF ? '/swfobject.js' : '/jwplayer.js')
);
if ($useSWF)
{ self::$swfJsLoaded = true;
}
else
{ self::$jwPlayerJsLoaded = true;
}
$code .= "<script type='text/javascript' src=\"$scriptUrl\"></script>\n";
}
// load the player
$code .= "<script type='text/javascript'>jwplayer.key=\"D/o6DSDk7r3TQqC57A1ZKrrKVn1t+/bF5ORGaA==\";</script>\n";
$code .= "<script type='text/javascript'>\n";
$code .= "// <![CDATA[\n";
$playerUrl = Xml::encodeJsVar($wgMWPlayerDir. '/player.swf');
$width = Xml::encodeJsVar($args['width']);
$height = Xml::encodeJsVar($args['height']);
if ($useSWF)
{
$code .= "var s1 = new SWFObject($playerUrl, 'player', $width, $height,'9');\n";
}
else
{
$code .= "jwplayer($wgMWPlayerIDJS).setup({flashplayer: $playerUrl";
}
// loop through all of the arguments (params and variables)
foreach( $args as $name => $value )
{
// replace :PLAYERDIR:
$value = str_replace(array( ':PLAYERDIR:'), array( Xml::escapeJsString($wgMWPlayerDir) ), $value);
// escape characters for param names
$name = str_replace(
array(
'00',
'01',
'02',
'09',
),
array(
'.',
'-',
'_',
'0',
),
$name
);
// protect from XSS
$newvalue = Xml::encodeJsVar($value);
// Add as either a Param or Variable
if ($useSWF)
{
$newname = Xml::encodeJsVar($name);
$code .= 's1.add'
. (in_array($name, self::$paramNames) ? 'Param' : 'Variable')
. "($newname, $newvalue);\n";
}
else
{ // only allow letters and a few special chars
$newname = preg_replace('%[^a-zA-Z0-9_\-\.]%', '', $name);
$code .= ", $newname: " . Xml::encodeJsVar($value);
}
}
if ($useSWF)
{
$code .= "s1.write($wgMWPlayerIDJS);\n";
}
else
{
$code .= "});\n";
}
$code .= "// ]]>";
$code .= "</script>";
return $code;
}
static function useAddOn($addOn)
{
self::$addOns[] = $addOn;
include (dirname(__FILE__) . '/AddOns/' . $addOn . '.php');
}
static function setId($id)
{
return self::$id = (int)$id;
}
static function getId()
{
return self::$id;
}
}
/**
* Interface for AddOns
*/
interface MediawikiPlayerAddOn
{
static public function setup(&$wgParser);
}