forked from mediafront/osmplayer
/
OSMPlayer.php
executable file
·545 lines (492 loc) · 16.7 KB
/
OSMPlayer.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
/**
* Copyright (c) 2010 Alethia Inc,
* http://www.alethia-inc.com
* Developed by Travis Tidwell | travist at alethia-inc.com
*
* License: GPL version 3.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Define the default width and height.
define('OSMPLAYER_DEFAULT_WIDTH', 550);
define('OSMPLAYER_DEFAULT_HEIGHT', 400);
/**
* PHP wrapper class for the Open Standard Media (OSM) player.
*
* This class is used to wrap all the functionality behind the open standard media player
* including the dynamic template system that this unique player employs. This class can
* be used to easily add and customize the OSM Player within a standalone PHP implementation.
*
* To learn how to use this class, please visit the online documentation at
* http://www.mediafront.org/documentation.
*/
class OSMPlayer {
// The complete settings of the media player.
private $settings;
// The playlists connected to this media player.
private $playlists = array();
// The controllers connected to this media player.
private $controllers = array();
// The template for this osm player instance.
public $template = null;
// To store the player parameters.
private $playerParams = null;
/**
* Constructor.
*
* Creates a new instance of a media player.
*
* Usage:
*
* $player = new OSMPlayer(array(
* 'width' => 640,
* 'height' => 480,
* 'playlist' => 'http://www.mysite.com/playlist.xml'
* ));
*/
public function __construct( $_params = array() ) {
// First set the defaults.
$this->playerParams = OSMPlayer::getPlayerParams();
$this->settings = array_merge( $this->playerParams, OSMPlayer::getPlayerSettings() );
if( $_params ) {
// Set the parameters ( which will override the defaults ).
$this->settings = array_merge( $this->settings, $_params );
}
// Create our template.
$templateClass = ucfirst( $this->settings['template'] ) . 'Template';
require_once( "OSMTemplate.php" );
require_once( "templates/" . $this->settings['template'] . "/template.php" );
$this->template = new $templateClass( $this->settings );
// Make sure we set the Prefix.
$this->setPrefix( isset($_params['prefix']) ? $_params['prefix'] : ($this->settings['id'] . '_') );
}
/**
* Returns the paths to this player library.
*/
public static function getPaths() {
static $playerPath, $playerURL;
// Get the player path.
if( !$playerPath ) {
$playerPath = trim( str_replace( realpath('.'), '', dirname(__FILE__) ), '/' );
$playerPath = trim( str_replace('\\', '/', $playerPath), '/' );
}
// Get the player URL.
if( !$playerURL ) {
$base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$base_url = $base_root .= '://'. $_SERVER['HTTP_HOST'];
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
$base_url .= "/$dir";
}
$playerURL = $base_url . '/' . $playerPath;
}
return array('playerPath' => $playerPath, 'playerURL' => $playerURL);
}
/**
* Returns the player settings.
*/
public static function getPlayerSettings( $paths = null ) {
$paths = $paths ? $paths : self::getPaths();
return array(
'width' => OSMPLAYER_DEFAULT_WIDTH,
'height' => OSMPLAYER_DEFAULT_HEIGHT,
'theme' => 'dark-hive',
'version' => '0.01',
'showController' => true,
'disablePlaylist' => false,
'playlistOnly' => false,
'showNodeVoter' => false,
'showTeaserVoter' => false,
'showTitleBar' => true,
'showWhenEmpty' => true,
'playerPath' => $paths['playerPath'],
'playerURL' => $paths['playerURL']
);
}
/**
* Returns the player parameters.
*/
public static function getPlayerParams( $paths = null ) {
return array(
'id' => 'player',
'showPlaylist' => true,
'file' => '',
'flashPlayer' => 'flash/mediafront.swf',
'image' => '',
'volume' => 80,
'autostart' => false,
'autoLoad' => true,
'streamer' => "",
'apiKey' => "",
'sessid' => "",
'api' => 2,
'drupalVersion' => 6,
'links' => array(),
'linksvertical' => false,
'logo' => 'logo.png',
'link' => "http://www.mediafront.org",
'logopos' => 'sw',
'logoWidth' => 49,
'logoHeight' => 15,
'logox' => 5,
'logoy' => 5,
'node' => "",
'shuffle' => false,
'loop' => false,
'repeat' => false,
'pageLimit' => 10,
'protocol' => "auto",
'server' => "drupal",
'template' => "default",
'baseURL' => "",
'draggable' => false,
'resizable' => false,
'playlist' => "",
'args' => array(),
'wildcard' => "*",
'gateway' => "",
'vertical' => true,
'scrollSpeed' => 15,
'updateTimeout' => 20,
'hysteresis' => 40,
'dynamic' => false,
'scrollMode' => "auto",
'pageLink' => false,
'debug' => false,
'embedWidth' => 450,
'embedHeight' => 337,
'skin' => 'default',
'autoNext' => true,
'prefix' => '',
'showScrollbar' => true,
'controllerOnly' => false,
'wmode' => 'transparent',
'forceOverflow' => false,
'volumeVertical' => false,
'incrementTime' => 5,
'quality' => 'default',
'zIndex' => 400,
'timeout' => 4,
'fluidWidth' => false,
'fluidHeight' => false
);
}
/**
* Set's the current session id for this player.
*/
public function setSessionId( $sessid ) {
$this->settings['sessid'] = $sessid;
}
/**
* Get's the current template.
*/
public function getTemplate() {
return $this->settings['template'];
}
/**
* Get's the current theme.
*/
public function getTheme() {
return $this->settings['theme'];
}
/**
* Connect the playlist of this media player to another media player.
*
* Usage:
*
* $playlist = new OSMPlayer(array(
* 'width' => 150,
* 'height' => 400,
* 'playlistOnly' => true,
* 'playlist' => 'http://www.mysite.com/playlist.xml'
* ));
*
* $player = new OSMPlayer(array(
* 'width' => 450,
* 'height' => 400,
* 'disablePlaylist' => true
* ));
*
* $playlist->addPlaylistTo( $player );
*/
public function addPlaylistTo( $player ) {
$this->playlists[] = is_string($player) ? $player : $player->getId();
}
/**
* Connect the controlbar of this media player to another media player.
*
* Usage:
*
* $controller = new OSMPlayer(array(
* 'width' => 400,
* 'height' => 26,
* 'controllerOnly' => true,
* ));
*
* $player = new OSMPlayer(array(
* 'playlist' => 'http://www.mysite.com/myplaylist.xml
* ));
*
* $controller->addControllerTo( $player );
*/
public function addControllerTo( $player ) {
$this->controllers[] = is_string($player) ? $player : $player->getId();
}
/**
* Returns the current id of the player.
*/
public function getId() {
return $this->settings['id'];
}
/**
* Set the id for this media player.
*/
public function setId( $newId ) {
// Set the id of this player.
$this->settings['id'] = $newId;
}
/**
* Returns the current prefix of the player.
*/
public function getPrefix() {
return $this->settings['prefix'];
}
/**
* Set the prefix for the CSS of this media player.
*/
public function setPrefix( $newPrefix ) {
// We only need to set the prefix if we generate the CSS.
if( $this->template->settings['generateCSS'] ) {
// Set the prefix.
$this->settings['prefix'] = $newPrefix;
// Set the template prefix.
$this->template->setPrefix( $newPrefix );
}
else {
$this->settings['prefix'] = '';
$this->template->setPrefix( '' );
}
}
/**
* Get the JS header for this player.
*/
public function getJSHeader() {
$header = '';
$playerPath = $this->settings['playerPath'] ? $this->settings['playerPath'] . '/' : '';
// Add all of the javascript files.
$jsfiles = $this->getJSFiles();
foreach( $jsfiles as $file ) {
$header .= '<script type="text/javascript" src="' . $playerPath . $file .'"></script>';
$header .= "\n";
}
// Return the header.
return $header;
}
/**
* Get the header for this media player.
*/
public function getHeader() {
// Add the JS files to the header.
$header = $this->getJSHeader();
// Add the CSS files.
$header .= $this->template->getCSSHeader();
// Return the header.
return $header;
}
/**
* Get the javascript files for this media player.
*/
public function getJSFiles() {
$template = $this->settings['template'];
if( $this->settings['debug'] ) {
return array_merge( array(
"js/source/jquery.media.drupal.js",
"js/source/jquery.media.parser.js",
"js/source/jquery.media.auto.js",
"js/source/jquery.media.rpc.js",
"js/source/jquery.media.json.js",
"js/source/jquery.media.sha256.js",
"js/source/jquery.media.utils.js",
"js/source/jquery.media.control.js",
"js/source/jquery.media.flash.js",
"js/source/jquery.media.html5.js",
"js/source/jquery.media.image.js",
"js/source/jquery.media.link.js",
"js/source/jquery.media.links.js",
"js/source/jquery.media.display.js",
"js/source/jquery.media.minplayer.js",
"js/source/jquery.media.menu.js",
"js/source/jquery.media.node.js",
"js/source/jquery.media.pager.js",
"js/source/jquery.media.player.js",
"js/source/jquery.media.playlist.js",
"js/source/jquery.media.playlistlink.js",
"js/source/jquery.media.rotator.js",
"js/source/jquery.media.slider.js",
"js/source/jquery.media.teaser.js",
"js/source/jquery.media.titlebar.js",
"js/source/jquery.media.scroll.js",
"js/source/jquery.media.voter.js",
"js/source/jquery.media.youtube.js",
"js/source/jquery.media.vimeo.js",
"js/source/jquery.media.dailymotion.js",
"js/source/jquery.media.file.js"
), $this->template->settings['jsFiles']['debug'] );
}
else {
return array_merge( array(
"js/jquery.osmplayer.compressed.js",
), $this->template->settings['jsFiles']['release'] );
}
}
/**
* Converts a PHP variable into its Javascript equivalent.
*/
public function osm_json_encode($var) {
switch (gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false'; // Lowercase necessary!
case 'integer':
case 'double':
return $var;
case 'resource':
case 'string':
return '"'. str_replace(array("\r", "\n", "<", ">", "&", "\'"),
array('\r', '\n', '\x3c', '\x3e', '\x26', "'"),
addslashes($var)) .'"';
case 'array':
// Arrays in JSON can't be associative. If the array is empty or if it
// has sequential whole number keys starting with 0, it's not associative
// so we can go ahead and convert it as an array.
if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
$output = array();
foreach ($var as $v) {
$output[] = $this->osm_json_encode($v);
}
return '['. implode(',', $output) .']';
}
// Otherwise, fall through to convert the array as an object.
case 'object':
$output = array();
foreach ($var as $k => $v) {
$output[] = $this->osm_json_encode(strval($k)) .': '. $this->osm_json_encode($v);
}
return '{'. implode(',', $output) .'}';
default:
return 'null';
}
}
/**
* Get the player parameters. This will only return the parameters that are included in
* the playerParams array.
*/
public function getParams() {
$params = array();
foreach( $this->settings as $param => $value ) {
if( array_key_exists( $param, $this->playerParams ) &&
($this->playerParams[$param] != $value) ) {
switch( gettype($value) ) {
case 'array':
case 'object':
$params[] = $param . ':' . $this->osm_json_encode($value);
break;
case 'string':
// Make sure we are not dealing with a JSON string here. If so, then don't include the quotes.
$params[] = (substr($value, 0, 1) == '{') ? ($param . ':' . $value) : ($param . ':"' . str_replace( '"', "'", $value ) . '"');
break;
case 'boolean':
$params[] = $param . ':' . ($value ? 'true' : 'false');
break;
default:
$params[] = $param . ':' . $value;
break;
}
}
}
$ids = array();
// Iterate through all of our template Ids.
foreach( $this->template->getIds() as $id => $value ) {
// Only set this if it is different from the default.
if( $this->template->defaultIds[$id] != $value ) {
$ids[] = $id . ':"' . $value . '"';
}
}
// If we have some id's different from the default, then add then to our params.
if( count( $ids ) ) {
// Now add all the Id's to the settings.
$params[] = 'ids:{' . implode(',', $ids) . '}';
}
return $params;
}
/**
* Returns the JavaScript code to add and instantiate the player on the page.
*/
public function getJS() {
// Return the script.
return '<script type="text/javascript">' . $this->getPlayerJS() . '</script>';
}
/**
* Returns the javascript to add the player to the page.
*/
public function getPlayerJS() {
$playerId = $this->getId();
$params = $this->getParams();
// Create the player in javascript.
$js = 'var ' . $playerId . ' = jQuery("#' . $playerId . '").mediaplayer({' . implode(',', $params) . '});';
// Now that the player has made it's way through the loading process... hide the busy cursor.
$js .= 'jQuery("#'. $playerId .'_loading").hide();';
// Now add our playlist connections to the javascript.
foreach( $this->playlists as $playlist ) {
$js .= 'jQuery.media.addPlaylist("' . $playlist . '",' . $playerId . ');';
}
// Now add our controller connections to the javascript.
foreach( $this->controllers as $controller ) {
$js .= 'jQuery.media.addController("' . $controller . '",' . $playerId . ');';
}
// Return the script.
// We need to use setTimeout since some browsers jump the gun on when they are really ready.
return 'jQuery(function() { setTimeout( function() {' . $js . '}, 10 ); });';
}
/**
* The main API call for this player. This will return the HTML and JavaScript for the
* media player that you wish to add to your page.
*
* This function allows for dynamic theming of the player by passing parameters to the media
* player ( i.e. playlistOnly, horizontal, etc ).
*/
public function getPlayer() {
$output = $this->getJS();
$output .= "\n";
// Determine the width and height of the player.
$width = ($this->settings['playlistOnly'] && $this->settings['vertical']) ? '' : ('width:' . $this->settings['width'] . 'px;');
$width = $this->settings['fluidWidth'] ? 'width:100%;' : $width;
$height = (($this->settings['playlistOnly'] && !$this->settings['vertical']) || $this->settings['controllerOnly']) ? '' : ('height:' . $this->settings['height'] . 'px;');
$height = $this->settings['fluidHeight'] ? 'height:100%;' : $height;
// Set the version.
$this->settings['version'] = $this->template->getVersion();
// Get the output from the template.
$output .= $this->template->theme( array(
'params' => $this->settings,
'width' => $width,
'height' => $height
)
);
return $output;
}
}
?>