View source
<?php
define('OSMPLAYER_DEFAULT_WIDTH', 550);
define('OSMPLAYER_DEFAULT_HEIGHT', 400);
class OSMPlayer {
private $settings;
private $playlists = array();
private $controllers = array();
public $template = null;
public $defaults = null;
private $playerParams = null;
public function __construct($_params = array()) {
$this->playerParams = OSMPlayer::getPlayerParams();
$this->defaults = array_merge($this->playerParams, OSMPlayer::getPlayerSettings());
$this->settings = $this->defaults;
if ($_params) {
$this->settings = array_merge($this->settings, $_params);
}
$templateClass = ucfirst($this->settings['template']) . 'Template';
require_once "OSMTemplate.php";
require_once "templates/" . $this->settings['template'] . "/template.php";
$this->template = new $templateClass($this->settings);
$this
->setPrefix(isset($_params['prefix']) ? $_params['prefix'] : $this->settings['id'] . '_');
}
public static function getPlayerPath() {
static $playerPath;
if (!$playerPath) {
$base_root = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$base_url = $base_root .= '://' . check_plain($_SERVER['HTTP_HOST']);
if ($dir = trim(dirname(check_plain($_SERVER['SCRIPT_NAME'])), '\\,/')) {
$base_url .= "/{$dir}";
}
$path = parse_url($base_url);
$path = isset($path['path']) ? $path['path'] : '';
$playerPath = trim(str_replace(realpath('.'), '', dirname(__FILE__)), '/');
$playerPath = trim(str_replace('\\', '/', $playerPath), '/');
$playerPath = $playerPath ? $path . '/' . $playerPath . '/' : $playerPath;
}
return $playerPath;
}
public static function getPlayerSettings() {
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' => self::getPlayerPath(),
);
}
public static function getPlayerParams() {
return array(
'id' => 'player',
'showPlaylist' => true,
'file' => '',
'flashPlayer' => 'minplayer/minplayer.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,
'fullscreen' => false,
);
}
public function setSessionId($sessid) {
$this->settings['sessid'] = $sessid;
}
public function getTemplate() {
return $this->settings['template'];
}
public function getTheme() {
return $this->settings['theme'];
}
public function addPlaylistTo($player) {
$this->playlists[] = is_string($player) ? $player : $player
->getId();
}
public function addControllerTo($player) {
$this->controllers[] = is_string($player) ? $player : $player
->getId();
}
public function getId() {
return $this->settings['id'];
}
public function setId($newId) {
$this->settings['id'] = $newId;
}
public function getPrefix() {
return $this->settings['prefix'];
}
public function setPrefix($newPrefix) {
if ($this->template->settings['generateCSS']) {
$this->settings['prefix'] = $newPrefix;
$this->template
->setPrefix($newPrefix);
}
else {
$this->settings['prefix'] = '';
$this->template
->setPrefix('');
}
}
public function getJSHeader() {
$header = '';
$jsfiles = $this
->getJSFiles();
foreach ($jsfiles as $file) {
$header .= '<script type="text/javascript" src="' . $this->settings['playerPath'] . $file . '"></script>';
$header .= "\n";
}
return $header;
}
public function getHeader() {
$header = $this
->getJSHeader();
$header .= $this->template
->getCSSHeader();
return $header;
}
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.mousewheel.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']);
}
}
public function osm_json_encode($var) {
switch (gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false';
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':
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) . ']';
}
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';
}
}
public function getParams() {
$params = array();
foreach ($this->settings as $param => $value) {
if (array_key_exists($param, $this->playerParams) && $this->playerParams[$param] != $value) {
switch (gettype($this->defaults[$param])) {
case 'array':
case 'object':
$params[] = $param . ':' . $this
->osm_json_encode($value);
break;
case 'string':
$params[] = substr($value, 0, 1) == '{' ? $param . ':' . $value : $param . ':"' . str_replace('"', "'", $value) . '"';
break;
case 'boolean':
$params[] = $param . ':' . ($value ? 'true' : 'false');
break;
default:
$params[] = $param . ':' . ($value ? $value : 0);
break;
}
}
}
$ids = array();
foreach ($this->template
->getIds() as $id => $value) {
if ($this->template->defaultIds[$id] != $value) {
$ids[] = $id . ':"' . $value . '"';
}
}
if (count($ids)) {
$params[] = 'ids:{' . implode(',', $ids) . '}';
}
return $params;
}
public function getJS() {
return '<script type="text/javascript">' . $this
->getPlayerJS() . '</script>';
}
public function getPlayerJS() {
$playerId = $this
->getId();
$params = $this
->getParams();
$js = 'var ' . $playerId . ' = jQuery("#' . $playerId . '").mediaplayer({' . implode(',', $params) . '});';
$js .= 'jQuery("#' . $playerId . '_loading").hide();';
foreach ($this->playlists as $playlist) {
$js .= 'jQuery.media.addPlaylist("' . $playlist . '",' . $playerId . ');';
}
foreach ($this->controllers as $controller) {
$js .= 'jQuery.media.addController("' . $controller . '",' . $playerId . ');';
}
return 'jQuery(function() { setTimeout( function() {' . $js . '}, 10 ); });';
}
public function getPlayer() {
$output = $this
->getJS();
$output .= "\n";
$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;
$this->settings['version'] = $this->template
->getVersion();
$output .= $this->template
->theme(array(
'params' => $this->settings,
'width' => $width,
'height' => $height,
));
return $output;
}
}