View source
<?php
define('MEDIAFRONT_DEFAULT_WIDTH', 500);
define('MEDIAFRONT_DEFAULT_HEIGHT', 350);
require_once getcwd() . '/' . drupal_get_path('module', 'mediafront') . '/includes/mediafront.preset.inc';
require_once getcwd() . '/' . drupal_get_path('module', 'mediafront') . '/includes/mediafront.filefield.inc';
function mediafront_perm() {
return array_merge(array(
'administer mediafront',
), mediafront_preset_perm());
}
function mediafront_menu() {
$items['mediafront_getplaylist'] = array(
'page callback' => 'mediafront_get_playlist_json',
'type' => MENU_CALLBACK,
'page arguments' => array(
1,
2,
3,
4,
),
'access arguments' => array(
'access content',
),
);
$items['mediafront_getnode'] = array(
'page callback' => 'mediafront_get_node_json',
'type' => MENU_CALLBACK,
'page arguments' => array(
1,
2,
),
'access arguments' => array(
'access content',
),
);
$items = array_merge($items, mediafront_preset_menu());
return $items;
}
function mediafront_theme() {
$theme = array();
$theme['mediafront_player_view'] = array(
'arguments' => array(
'view' => NULL,
'preset' => NULL,
),
);
$theme['mediafront_player'] = array(
'arguments' => array(
'node' => NULL,
'preset' => NULL,
),
);
$theme['mediafront_field'] = array(
'arguments' => array(
'node' => NULL,
'preset' => NULL,
),
);
$theme = array_merge($theme, mediafront_preset_theme());
return $theme;
}
function theme_mediafront_player_view($view, $preset) {
$params['playlistObj'] = mediafront_get_playlist_from_view($view);
$params['playlist'] = $view->name;
$params['args'] = $view->args;
$params['pageLimit'] = method_exists($view, 'get_items_per_page') ? $view
->get_items_per_page() : $view->pager['items_per_page'];
return mediafront_get_player($preset, $params);
}
function theme_mediafront_player($node, $preset) {
$params['nodeObj'] = mediafront_invoke_node($node, array());
$params['node'] = $node->nid;
$params['disablePlaylist'] = true;
return mediafront_get_player($preset, $params);
}
function theme_mediafront_field($node, $preset) {
$params['nodeObj'] = (array) $node;
if (isset($node->nid)) {
$params['node'] = $node->nid;
}
$params['disablePlaylist'] = true;
return mediafront_get_player($preset, $params);
}
function mediafront_init() {
if (module_exists('content')) {
module_load_include('inc', 'mediafront', 'mediafront_field');
}
}
function mediafront_field_info() {
return array(
'mediafront' => array(
'label' => t('Media Player'),
'description' => t('Add a media player to this content.'),
),
);
}
function mediafront_features_api() {
return array(
'mediafront' => array(
'name' => t('MediaFront Presets'),
'default_hook' => 'mediafront_default_presets',
'file' => drupal_get_path('module', 'mediafront') . '/includes/mediafront.features.inc',
),
);
}
function mediafront_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'mediafront') . '/views',
);
}
function mediafront_get_players() {
$return = array();
foreach (module_implements('player_info') as $name) {
$function = $name . '_player_info';
$return = array_merge($return, $function());
}
return $return;
}
function mediafront_get_player_params($player) {
$get_params = $player . '_get_player_params';
if (function_exists($get_params)) {
return $get_params();
}
return array();
}
function mediafront_get_player_settings($player) {
$get_settings = $player . '_get_player_settings';
if (function_exists($get_settings)) {
return $get_settings();
}
return array();
}
function mediafront_get_settings($player, $overrides = array()) {
static $defaults = array();
$settings = isset($defaults[$player]) ? $defaults[$player] : array();
if (!$settings) {
$settings = array_merge(mediafront_get_player_params($player), mediafront_get_player_settings($player));
$defaults[$player] = $settings;
}
return $overrides ? array_merge($settings, $overrides) : $settings;
}
function mediafront_template_preprocess(&$variables) {
$variables['player_params'] = $variables['params'];
$variables['params'] = mediafront_get_settings($variables['params']['player'], $variables['params']);
}
function mediafront_service() {
return array(
array(
'#method' => 'mediafront.getPlaylist',
'#callback' => 'mediafront_get_playlist',
'#access arguments' => array(
'access content',
),
'#key' => FALSE,
'#args' => array(
array(
'#name' => 'playlist',
'#type' => 'string',
'#description' => t('Playlist name.'),
),
array(
'#name' => 'limit',
'#type' => 'int',
'#optional' => TRUE,
'#description' => t('The limit for the view to show.'),
),
array(
'#name' => 'page',
'#type' => 'int',
'#optional' => TRUE,
'#description' => t('The page number to show.'),
),
array(
'#name' => 'args',
'#type' => 'array',
'#optional' => TRUE,
'#description' => t('An array of arguments to pass to the view.'),
),
),
'#return' => 'array',
'#help' => t('Retrieves a playlist.'),
),
array(
'#method' => 'mediafront.getNode',
'#callback' => 'mediafront_get_node',
'#access arguments' => array(
'access content',
),
'#key' => FALSE,
'#args' => array(
array(
'#name' => 'nid',
'#type' => 'int',
'#description' => t('The node Id for the node you wish to get.'),
),
array(
'#name' => 'args',
'#type' => 'array',
'#optional' => TRUE,
'#description' => t('An array of arguments to pass to the node.'),
),
),
'#return' => 'array',
'#help' => t('Retrieves a node.'),
),
);
}
function mediafront_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[] = mediafront_json_encode($v);
}
return '[' . implode(',', $output) . ']';
}
case 'object':
$output = array();
foreach ($var as $k => $v) {
$output[] = mediafront_json_encode(strval($k)) . ': ' . mediafront_json_encode($v);
}
return '{' . implode(',', $output) . '}';
default:
return 'null';
}
}
function mediafront_parse_params($json) {
$json = trim($json, ' []');
$params = null;
if ($json) {
$params = explode(',', $json);
foreach ($params as $index => $param) {
$params[$index] = trim($param, ' "');
}
}
return $params;
}
function mediafront_get_playlist_json($playlist = null, $limit = null, $page = null, $args = null) {
$params = mediafront_parse_params($_POST['params']);
$playlist = $playlist ? check_plain($playlist) : $params[1];
$limit = $limit ? check_plain($limit) : $params[2];
$page = $page ? check_plain($page) : $params[3];
$args = $args ? $args : mediafront_parse_params($params[4]);
print mediafront_json_encode(mediafront_get_playlist($playlist, $limit, $page, $args));
}
function mediafront_get_playlist($playlist, $limit = 10, $page = 0, $args = array()) {
$view = views_get_view($playlist);
if (is_null($view)) {
return services_error($playlist . ' does not exist.');
}
if (!$view
->access('default')) {
return services_error('You do not have access to ' . $playlist . '.');
}
if (method_exists($view, 'set_use_pager')) {
$view
->set_use_pager(FALSE);
}
$view
->set_items_per_page($limit);
$view
->set_offset($page * $limit);
$view
->set_arguments($args);
$view->get_total_rows = TRUE;
$view
->execute();
return mediafront_get_playlist_from_view($view, $args);
}
function mediafront_get_playlist_from_view($view, $args = null) {
$playlist = array();
$playlist['nodes'] = array();
$playlist['name'] = $view->name;
$playlist['total_rows'] = $view->total_rows ? $view->total_rows : ($view->query->pager ? $view->query->pager
->get_total_items() : 0);
foreach ($view->result as $node) {
if ($node = mediafront_get_node($node->nid, $args)) {
$playlist['nodes'][] = $node;
}
}
return $playlist;
}
function mediafront_get_node_json($node = null, $args = null) {
$params = mediafront_parse_params($_POST['params']);
$node = $node ? check_plain($node) : check_plain(trim($params[1]));
$args = $args ? check_plain($args) : check_plain(trim($params[2]));
print mediafront_json_encode(mediafront_get_node($node, $args));
}
function mediafront_get_node($nid, $args = null) {
return mediafront_invoke_node(node_load($nid), $args);
}
function mediafront_array_replace_recursive($toArray, $fromArray) {
foreach ($fromArray as $key => $value) {
if (is_array($toArray)) {
$toArray[$key] = is_array($value) || is_object($value) ? mediafront_array_replace_recursive($toArray[$key], $value) : $value;
}
else {
$toArray->{$key} = is_array($value) || is_object($value) ? mediafront_array_replace_recursive($toArray->{$key}, $value) : $value;
}
}
return $toArray;
}
function mediafront_playlist_node_merge($original, $additions) {
$merged = $original;
if (count($additions) > 0) {
foreach ($additions as $field => $value) {
$merged[$field] = isset($original[$field]) ? mediafront_array_replace_recursive($original[$field], $value) : $value;
}
}
return $merged;
}
function mediafront_invoke_node($node, $args) {
$additions = array();
if ($node) {
foreach (module_implements('playlist_node') as $name) {
$function = $name . '_playlist_node';
$additions = mediafront_playlist_node_merge($additions, $function($node, $args));
}
}
return $additions;
}
function mediafront_preset_select_form($default, $key = 'mediafront_preset') {
$presets = mediafront_preset_get_presets();
$options = array();
foreach ($presets as $preset) {
$options[$preset['name']] = $preset['name'];
}
return array(
'#type' => 'select',
'#title' => t('MediaFront Presets'),
'#options' => $options,
'#default_value' => isset($default[$key]) ? $default[$key] : '',
);
}
function mediafront_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
return mediafront_block_info();
}
if ($delta == 'mediaplayer') {
switch ($op) {
case 'configure':
return mediafront_block_configure($delta);
break;
case 'save':
mediafront_block_save($delta, $edit);
break;
case 'view':
return mediafront_block_view($delta);
break;
}
}
}
function mediafront_block_info() {
$blocks['mediaplayer'] = array(
'info' => t('Media Player'),
);
return $blocks;
}
function mediafront_block_configure($delta = '') {
$form = array();
if ($delta == 'mediaplayer') {
$form['mediafront'] = array(
'#type' => 'fieldset',
'#title' => 'MediaFront Settings',
'#description' => 'This is a special media player block that will allow you to place a media player preset within any block region on your website. In addition, you can link to any view within that page to act as the playlist for this media player.',
);
$form['mediafront']['mediafront_block_preset'] = mediafront_preset_select_form(array(
'mediafront_preset' => variable_get('mediafront_block_preset', ''),
));
$views = views_get_all_views();
$options = array(
'' => 'none',
);
foreach ($views as $view) {
if (!$view->disabled) {
$options[$view->name] = $view->name;
}
}
$form['mediafront']['mediafront_block_playlist'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => 'Link to View',
'#description' => 'Select the view you would like to link to this media player. This view must be on the same page as this player to be linked, and must include a visible Content Id (node Id) within the fields of the view display to work as expected.',
'#options' => $options,
'#default_value' => variable_get('mediafront_block_playlist', ''),
);
$form['mediafront']['mediafront_block_playlist_exclude'] = array(
'#type' => 'textarea',
'#title' => 'Exclude View Display',
'#description' => 'Provide any displays you would like to exclude from the selected view above.',
'#default_value' => implode("\n", variable_get('mediafront_block_playlist_exclude', array())),
);
}
return $form;
}
function mediafront_block_save($delta = '', $edit = array()) {
if ($delta == 'mediaplayer') {
variable_set('mediafront_block_preset', $edit['mediafront_block_preset']);
variable_set('mediafront_block_playlist', $edit['mediafront_block_playlist']);
$exclude = preg_split("/[\r\n]+/", $edit['mediafront_block_playlist_exclude']);
$exclude = array_map(create_function('$item', 'return trim($item);'), array_filter($exclude));
variable_set('mediafront_block_playlist_exclude', $exclude);
}
}
function mediafront_block_view($delta = '') {
$block = array();
if ($delta == 'mediaplayer') {
$block['subject'] = t('Media Player');
$block['content'] = mediafront_block_content();
}
return $block;
}
function mediafront_block_content() {
$preset = variable_get('mediafront_block_preset', '');
return $preset ? mediafront_get_player($preset) : '';
}
function mediafront_block_show($view) {
$exclude = variable_get('mediafront_block_playlist_exclude', array());
$playlists = variable_get('mediafront_block_playlist', '');
$enabled = is_array($playlists) ? isset($playlists[$view->name]) : $playlists == $view->name;
return $enabled && !in_array($view->current_display, $exclude);
}
function mediafront_views_query_alter(&$view, &$query) {
if (mediafront_block_show($view)) {
if (!isset($view->field['mediafront_nid'])) {
drupal_set_html_head('<style type="text/css">.media-nid-hidden { display:none; }</style>');
$handler = views_get_handler('node', 'nid', 'field');
$handler
->set_default_options();
$handler->field_alias = 'mediafront_nid';
$handler->table = 'node';
$handler->field = 'nid';
$handler->real_field = 'nid';
$handler->options['label'] = '';
$handler->options['alter']['alter_text'] = 1;
$handler->options['alter']['text'] = '<div class="media-nid-hidden">[mediafront_nid]</div>';
$handler->options['id'] = 'mediafront_nid';
$handler
->init($view, $handler->options);
$query
->add_field('node', 'nid', 'mediafront_nid');
$view->field['mediafront_nid'] = $handler;
$view->display_handler->handlers['field']['mediafront_nid'] = $handler;
}
}
if ($view->style_plugin->definition['handler'] == 'mediafront_plugin_style_player') {
if (!isset($query->fields) || !in_array('nid', array_keys($query->fields))) {
$query
->add_field($query->base_table, 'nid');
}
}
}
function mediafront_views_pre_render(&$view) {
if (mediafront_block_show($view)) {
$preset = variable_get('mediafront_block_preset', '');
$js = 'jQuery.media = jQuery.media ? jQuery.media : {};';
$js .= 'jQuery.media.nodes = jQuery.media.nodes ? jQuery.media.nodes : {};';
foreach ($view->result as &$result) {
$js .= 'jQuery.media.nodes["' . $result->nid . '"] = ' . mediafront_json_encode(mediafront_invoke_node(node_load($result->nid), NULL)) . ';';
}
$view_id = '.view-' . views_css_safe($view->name);
$js .= 'jQuery.media.fieldSelector = "' . $view_id . ' .views-field-mediafront-nid";';
$js .= 'jQuery.media.playerId = "mediafront_' . $preset . '";';
drupal_add_js($js, 'inline');
drupal_add_js(drupal_get_path('module', 'mediafront') . '/js/mediafront.js');
}
}
function mediafront_get_extension($file) {
return drupal_strtolower(drupal_substr($file, strrpos($file, '.') + 1));
}
function mediafront_is_image($mimetype, $extension) {
$image = in_array($extension, array(
'jpg',
'jpeg',
'png',
'gif',
));
$image |= in_array($mimetype, array(
'image/jpeg',
'image/jpg',
'image/png',
'image/gif',
'jpg',
'jpeg',
'png',
'gif',
));
return $image;
}
function mediafront_is_media($mimetype, $extension) {
if (in_array(substr($mimetype, 0, 5), array(
'video',
'audio',
))) {
return true;
}
$media = in_array($extension, array(
'swf',
'mov',
'mp4',
'mp3',
'm4v',
'flv',
'f4v',
'ogg',
'oga',
'ogv',
'3g2',
'wav',
'aif',
'm4a',
'aac',
'wma',
'webm',
));
return $media;
}
function mediafront_array_to_object($array = array()) {
$object = new stdClass();
foreach ($array as $akey => $aval) {
$object->{$akey} = $aval;
}
return $object;
}
function mediafront_get_media_filepath($file) {
return isset($file->url) ? $file->url : (isset($file->path) ? $file->path : (!isset($file->filepath) ? '' : (preg_match('/^http(s)?\\:\\/\\//', $file->filepath) ? $file->filepath : file_create_url($file->filepath))));
}
function mediafront_get_media_file($file) {
$media_file = new stdClass();
$media_file->path = mediafront_get_media_filepath($file);
$media_file->extension = mediafront_get_extension($media_file->path);
if (isset($file->class) && isset($file->mediaType)) {
$media_file->class = $file->class;
$media_file->mediaType = $file->mediaType;
}
else {
if (mediafront_is_image($file->filemime, $media_file->extension)) {
$media_file->class = "images";
$media_file->mediaType = "image";
}
else {
if (mediafront_is_media($file->filemime, $media_file->extension)) {
$media_file->class = "media";
$media_file->mediaType = $file->mediaType ? $file->mediaType : "media";
}
else {
$media_file->class = "";
$media_file->mediaType = "";
}
}
}
$media_file->player = isset($file->player) ? $file->player : '';
$media_file->bytesTotal = isset($file->filesize) ? $file->filesize : 0;
$media_file->duration = isset($file->duration) ? $file->duration : 0;
$media_file->width = isset($file->width) ? $file->width : 0;
$media_file->height = isset($file->height) ? $file->height : 0;
return $media_file;
}
function mediafront_add_media(&$additions, $file) {
$media_added = false;
if ($file) {
$file = is_array($file) ? mediafront_array_to_object($file) : $file;
if ($media_file = mediafront_get_media_file($file)) {
if ($media_file->class && $media_file->mediaType) {
if ($media_file->mediaType != "image" && isset($additions["mediafiles"][$media_file->class][$media_file->mediaType])) {
$media = $additions["mediafiles"][$media_file->class][$media_file->mediaType];
$files = is_array($media) ? $media : array(
$media,
);
array_push($files, $media_file);
$additions["mediafiles"][$media_file->class][$media_file->mediaType] = $files;
$media_added = true;
}
else {
$additions["mediafiles"][$media_file->class][$media_file->mediaType] = $media_file;
$media_added = true;
}
}
}
}
return $media_added;
}
function mediafront_get_image($preset_name, $filename) {
return file_create_url(file_directory_path() . '/imagecache/' . $preset_name . '/' . $filename);
}
function _mediafront_get_field($node, $field_name, $index = -1) {
if (isset($node->{$field_name}) && is_array($node->{$field_name}) && isset($node->{$field_name}[0]) && is_array($node->{$field_name}[0])) {
return $index >= 0 ? $node->{$field_name}[$index] : $node->{$field_name};
}
else {
return null;
}
}
function mediafront_playlist_node($node) {
$media_added = false;
$additions = array();
$field_additions = array();
if (isset($node->files) && $node->files && is_array($node->files)) {
foreach ($node->files as $fid => $file) {
$media_added |= mediafront_add_media($additions, $file);
}
}
if ($field = _mediafront_get_field($node, 'field_dashplayer_media', 0)) {
$media_added = true;
$additions["mediafiles"]["media"]["media"] = $field['value'];
}
if ($field = _mediafront_get_field($node, 'field_dashplayer_image', 0)) {
$media_added = true;
$additions["mediafiles"]["images"]["image"] = $field['value'];
}
if ($field = _mediafront_get_field($node, 'field_mediafront_media', 0)) {
$media_added = true;
$additions["mediafiles"]["media"]["media"] = $field['value'];
}
if ($field = _mediafront_get_field($node, 'field_mediafront_image', 0)) {
$media_added = true;
$additions["mediafiles"]["images"]["image"] = $field['value'];
}
foreach ($node as $fieldname => $field) {
if ($field = _mediafront_get_field($node, $fieldname)) {
if (isset($field[0]['value']) && is_string($field[0]['value'])) {
$field_additions[$fieldname] = $field[0]['value'];
}
if (isset($field[0]['filename'])) {
$media_field = mediafront_filefield_get($node->type, $fieldname);
if (!$media_field || !empty($media_field->media_type) && $media_field->media_type != 'none' || !($media_field->node_preset == 'none' && $media_field->thumb_preset == 'none')) {
foreach ($field as $index => $instance) {
if (isset($instance['data']) && $instance['data']) {
$fileData = $instance['data'];
unset($instance['data']);
$file = array_merge($instance, $fileData);
}
else {
$file = $instance;
}
$file['mediaType'] = $media_field ? $media_field->media_type : '';
$media_added |= mediafront_add_media($additions, $file);
}
if ($media_field) {
$filename = basename($field[0]['filepath']);
if ($media_field->node_preset && $media_field->node_preset != 'mediafront_original') {
$file = array();
$file['path'] = mediafront_get_image($media_field->node_preset, $filename);
$file['class'] = 'images';
$file['mediaType'] = 'preview';
$media_added |= mediafront_add_media($additions, $file);
}
if ($media_field->thumb_preset && $media_field->thumb_preset != 'mediafront_original') {
$file = array();
$file['path'] = mediafront_get_image($media_field->thumb_preset, $filename);
$file['class'] = 'images';
$file['mediaType'] = 'thumbnail';
$media_added |= mediafront_add_media($additions, $file);
}
}
}
}
}
}
$additions['nid'] = $node->nid;
$additions['title'] = $node->title;
$additions['description'] = $node->body;
foreach ($field_additions as $field => $value) {
$additions[$field] = $value;
}
return $additions;
}
function mediafront_get_player() {
$args = func_get_args();
$params = $args[0];
if (gettype($params) == 'string') {
$preset = mediafront_get_preset($params);
$params = isset($args[1]) ? $preset['settings'] ? array_merge($preset['settings'], $args[1]) : $args[1] : $preset['settings'];
$params['preset'] = $preset['name'];
$params['player'] = $preset['player'];
$params['protocol'] = 'json';
$params['connect'] = isset($params['connect']) && $params['connect'] ? $params['connect'] : $preset['connect'];
$params['id'] = isset($params['id']) && $params['id'] ? $params['id'] : 'mediafront_' . $preset['name'];
}
if ($params && is_array($params)) {
$player = $params && $params['player'] ? $params['player'] : 'osmplayer';
$getPlayer = $player . '_get_player';
return function_exists($getPlayer) ? $getPlayer($params) : t('Player not available');
}
else {
return t('Player not available');
}
}