View source
<?php
global $mp3_player_id;
$mp3_player_id = 0;
function mp3player_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#mp3player":
$output = '<p>' . t("A module to integrate the WordPress Audio Player into Drupal.") . '</p>';
break;
}
return $output;
}
function mp3player_perm() {
return array(
'administer mp3player',
);
}
function mp3player_menu() {
$items = array();
$items['admin/settings/mp3player'] = array(
'title' => 'MP3 Player',
'page callback' => 'mp3player_player_list',
'page arguments' => array(
'mp3player_player_list',
),
'access arguments' => array(
'administer mp3player',
),
);
$items['admin/settings/mp3player/player-list'] = array(
'title' => 'Player List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/mp3player/add-new-player'] = array(
'title' => 'Add new player',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mp3player_player_form',
),
'access arguments' => array(
'administer mp3player',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/mp3player/global-settings'] = array(
'title' => 'Global Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mp3player_settings',
),
'access arguments' => array(
'administer mp3player',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/mp3player/player/%'] = array(
'title callback' => 'mp3player_title_callback',
'title arguments' => array(
'Edit player: !name',
4,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mp3player_player_form',
4,
),
'access arguments' => array(
'administer mp3player',
),
'type' => MENU_CALLBACK,
);
$items['admin/settings/mp3player/player/%/delete'] = array(
'title callback' => 'mp3player_title_callback',
'title arguments' => array(
'Delete player: !name',
4,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mp3player_player_delete',
4,
),
'access arguments' => array(
'administer mp3player',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function mp3player_player_list() {
$header = array(
t('Player Name'),
t('Actions'),
);
$rows = array();
$players = mp3player_players();
foreach (mp3player_players() as $pid => $player) {
$row = array();
$row[] = l($player['name'], 'admin/settings/mp3player/player/' . $player['pid']);
$links = array();
$links[] = l(t('Edit'), 'admin/settings/mp3player/player/' . $player['pid']);
if ($pid > 1) {
$links[] = l(t('Delete'), 'admin/settings/mp3player/player/' . $player['pid'] . '/delete');
}
$row[] = implode(' ', $links);
$rows[] = $row;
}
$output = theme('table', $header, $rows);
return $output;
}
function mp3player_title_callback($title, $pid = 1) {
$replacements = array();
$player = mp3player_players($pid);
$replacements['!name'] = $player['name'];
return t($title, $replacements);
}
function mp3player_players($pid = NULL) {
$players = array();
$result = db_query('SELECT * FROM {mp3player_players} ORDER BY pid');
while ($player = db_fetch_array($result)) {
$players[$player['pid']] = $player;
}
if ($pid != NULL && isset($players[$pid])) {
return $players[$pid];
}
else {
return $players;
}
}
function mp3player_settings() {
if (user_access('administer mp3player')) {
$form['mp3player_library_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to mp3 library'),
'#default_value' => variable_get('mp3player_library_path', drupal_get_path('module', 'mp3player') . '/mp3player'),
'#description' => t("This Player requires the <a href='!mp3_url'>standalone WordPress Audio Player</a>, enter the directory where the two required files can be found, by default this is set to sites/all/modules/mp3player/mp3player", array(
'!mp3_url' => 'http://wpaudioplayer.com/download',
)),
);
$form['mp3player_encode'] = array(
'#type' => 'select',
'#title' => t('Encode Audio File URLs'),
'#default_value' => variable_get('mp3player_encode', 'no'),
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('Indicates that the mp3 file urls are encoded.'),
);
$form['mp3player_suppress'] = array(
'#type' => 'select',
'#title' => t('Suppress cache messages'),
'#default_value' => variable_get('mp3player_suppress', 'no'),
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('Suppress all messages about the MP3 Player cache being rebuilt.'),
);
if (module_exists('filefieldmp3player')) {
$form['mp3player_description'] = array(
'#type' => 'select',
'#title' => t('FileField Description Value'),
'#default_value' => variable_get('mp3player_description', 'description'),
'#options' => array(
'description' => t('Description'),
'id3' => t('ID3 Template'),
),
'#description' => t('Either use default description field or ID3 template file.'),
);
}
$form['#submit'][] = 'mp3player_settings_submit';
return system_settings_form($form);
}
}
function mp3player_settings_submit($form, &$form_state) {
cache_clear_all('mp3player_default', 'cache');
}
function mp3player_player_form($form_state, $pid = NULL) {
if ($pid) {
$player = mp3player_players($pid);
}
else {
$player = mp3player_players(1);
$player['name'] = '';
$player['pid'] = 0;
}
$form['namegroup'] = array(
'#type' => 'fieldset',
'#title' => t('Player Name'),
'#weight' => -2,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['namegroup']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $player['name'],
'#required' => TRUE,
'#size' => '20',
'#description' => t('Unique name of player. Please only use alphanumeric characters, or underscores (_) for player names.'),
);
if ($pid == 1) {
$form['namegroup']['#type'] = 'hidden';
$form['namegroup']['name']['#type'] = 'hidden';
}
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Player Options'),
'#weight' => -1,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['options']['autostart'] = array(
'#type' => 'select',
'#title' => t('Auto-start'),
'#default_value' => $player['autostart'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If yes, player starts automatically.'),
);
$form['options']['loopaudio'] = array(
'#type' => 'select',
'#title' => t('Loop Audio'),
'#default_value' => $player['loopaudio'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If yes, player loops audio.'),
);
$form['options']['animation'] = array(
'#type' => 'select',
'#title' => t('Animate'),
'#default_value' => $player['animation'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If no, player is always open.'),
);
$form['options']['remaining'] = array(
'#type' => 'select',
'#title' => t('Time Remaining'),
'#default_value' => $player['remaining'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If yes, shows remaining track time rather than elapsed time.'),
);
$form['options']['noinfo'] = array(
'#type' => 'select',
'#title' => t("Don't display information"),
'#default_value' => $player['noinfo'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If yes, disables the track information display.'),
);
$form['options']['initialvolume'] = array(
'#type' => 'textfield',
'#title' => t('Initial Volume'),
'#default_value' => $player['initialvolume'],
'#required' => TRUE,
'#size' => '10',
'#description' => t('Initial volume level (from 0 to 100).'),
);
$form['options']['buffer'] = array(
'#type' => 'textfield',
'#title' => t('Buffer Time'),
'#default_value' => $player['buffer'],
'#required' => TRUE,
'#size' => '10',
'#description' => t('Buffering time in seconds.'),
);
$form['options']['encode'] = array(
'#type' => 'select',
'#title' => t('Encode'),
'#default_value' => $player['encode'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('Indicates that the mp3 file urls are encoded.'),
);
$form['options']['checkpolicy'] = array(
'#type' => 'select',
'#title' => t('Check Policy'),
'#default_value' => $player['checkpolicy'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('Tells Flash to look for a policy file when loading mp3 files (this allows Flash to read ID3 tags from files hosted on a different domain).'),
);
$form['options']['rtl'] = array(
'#type' => 'select',
'#title' => t('Text Right-to-Left'),
'#default_value' => $player['rtl'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('Switches the layout to RTL (right to left) for Hebrew and Arabic languages.'),
);
$form['options']['width'] = array(
'#type' => 'textfield',
'#title' => t('Player Width'),
'#default_value' => $player['width'],
'#required' => TRUE,
'#size' => '10',
'#description' => t('Width of the player. e.g. 290 (290 pixels) or 100%.'),
);
$form['options']['transparentpagebg'] = array(
'#type' => 'select',
'#title' => t('Transparent Player Background'),
'#default_value' => $player['transparentpagebg'],
'#options' => array(
'no' => t('No'),
'yes' => t('Yes'),
),
'#description' => t('If yes, the player background is transparent (matches the page background).'),
);
$form['options']['pagebg'] = array(
'#type' => 'textfield',
'#title' => t('Player Background Colour'),
'#default_value' => $player['pagebg'],
'#required' => FALSE,
'#size' => '20',
'#field_prefix' => '#',
'#description' => t("Player background colour (set it to your page background when transparentbg is set to 'no')."),
);
$form['colours'] = array(
'#type' => 'fieldset',
'#title' => t('Player Colour Scheme'),
'#weight' => 0,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['colours']['bg'] = array(
'#type' => 'textfield',
'#title' => t('Background'),
'#default_value' => $player['bg'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['leftbg'] = array(
'#type' => 'textfield',
'#title' => t('Left Background'),
'#default_value' => $player['leftbg'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
'#description' => t('Speaker icon/Volume control background.'),
);
$form['colours']['lefticon'] = array(
'#type' => 'textfield',
'#title' => t('Speaker Icon'),
'#default_value' => $player['lefticon'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['voltrack'] = array(
'#type' => 'textfield',
'#title' => t('Volume Track Background'),
'#default_value' => $player['voltrack'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['volslider'] = array(
'#type' => 'textfield',
'#title' => t('Volume Track Slider'),
'#default_value' => $player['volslider'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['rightbg'] = array(
'#type' => 'textfield',
'#title' => t('Right Background'),
'#default_value' => $player['rightbg'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
'#description' => t('Play/Pause button background.'),
);
$form['colours']['rightbghover'] = array(
'#type' => 'textfield',
'#title' => t('Right Background Hover'),
'#default_value' => $player['rightbghover'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
'#description' => t('Play/Pause button background (hover state).'),
);
$form['colours']['righticon'] = array(
'#type' => 'textfield',
'#title' => t('Play/Pause Icon'),
'#default_value' => $player['righticon'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['righticonhover'] = array(
'#type' => 'textfield',
'#title' => t('Play/Pause Icon (hover state)'),
'#default_value' => $player['righticonhover'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['loader'] = array(
'#type' => 'textfield',
'#title' => t('Loading Bar'),
'#default_value' => $player['loader'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['track'] = array(
'#type' => 'textfield',
'#title' => t('Track Backgrounds'),
'#default_value' => $player['track'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
'#description' => t('Loading/Progress bar track background.'),
);
$form['colours']['tracker'] = array(
'#type' => 'textfield',
'#title' => t('Progress Track'),
'#default_value' => $player['tracker'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['border'] = array(
'#type' => 'textfield',
'#title' => t('Progress Track Border'),
'#default_value' => $player['border'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['skip'] = array(
'#type' => 'textfield',
'#title' => t('Previous/Next Buttons'),
'#default_value' => $player['skip'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['colours']['text'] = array(
'#type' => 'textfield',
'#title' => t('Text'),
'#default_value' => $player['text'],
'#required' => TRUE,
'#size' => '10',
'#field_prefix' => '#',
);
$form['pid'] = array(
'#type' => 'hidden',
'#value' => $player['pid'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes to player'),
);
return $form;
}
function mp3player_player_form_validate($form, &$form_state) {
$players = mp3player_players();
foreach ($players as $player) {
if ($player['name'] == $form_state['values']['name']) {
form_set_error('name', t('Player name has to be unique.'));
}
}
if (preg_match('/[^0-9a-zA-Z_]/', $form_state['values']['name'])) {
form_set_error('name', t('Please only use alphanumeric characters, and underscores (_) for player names.'));
}
if ($form_state['values']['name'] != '' && strlen($form_state['values']['name']) >= 20) {
form_set_error('name', t('Player name must be 20 characters long or shorter.'));
}
if (!is_numeric($form_state['values']['initialvolume'])) {
form_set_error('initialvolume', t('Initial Volume must be an integer'));
}
if ($form_state['values']['initialvolume'] < 0 || $form_state['values']['initialvolume'] > 100) {
form_set_error('initialvolume', t('Volume must be between 0 and 100.'));
}
if (!is_numeric($form_state['values']['buffer'])) {
form_set_error('buffer', t('Buffer must be an integer.'));
}
$percentage = strstr($form_state['values']['width'], '%');
if (!is_numeric($form_state['values']['width']) && $percentage != '%') {
form_set_error('width', t('Width must be an integer or percentage.'));
}
$hextest = array(
'bg',
'leftbg',
'lefticon',
'voltrack',
'volslider',
'rightbg',
'rightbghover',
'righticon',
'righticonhover',
'loader',
'track',
'tracker',
'border',
'skip',
'text',
);
$bgcolor = $form_state['values']['pagebg'];
$transbg = $form_state['values']['transparentpagebg'];
if ($transbg == 'yes') {
if ($bgcolor == '') {
form_set_error('pagebg', 'Please set a background color');
}
else {
$hextest[] = 'pagebg';
}
}
foreach ($hextest as $test) {
if (isset($form_state['values'][$test]) && ($res = mp3player_validate_hex($form_state['values'][$test]))) {
form_set_error($test, $res);
}
}
}
function mp3player_validate_hex($value) {
if (strlen($value) != 6) {
return t('HEX code must be 6 characters long.');
}
if (preg_match('/[^0-9a-fA-F]/', $value)) {
return t('Invalid HEX code submitted');
}
return FALSE;
}
function mp3player_player_form_submit($form, &$form_state) {
$values = $form_state['values'];
if (array_key_exists('pid', $values) && $values['pid'] > 1) {
drupal_write_record('mp3player_players', $values, 'pid');
drupal_set_message(t("MP3 Player %name updated.", array(
'%name' => $values['name'],
)));
}
else {
if (array_key_exists('pid', $values)) {
unset($values['pid']);
}
drupal_write_record('mp3player_players', $values);
drupal_set_message(t("New MP3 Player %name added.", array(
'%name' => $values['name'],
)));
}
$form_state['redirect'] = 'admin/settings/mp3player';
}
function mp3player_player_delete($form_state, $pid = 1) {
$player = mp3player_players($pid);
if (empty($player)) {
drupal_set_message(t('The specified player was not found.'), 'error');
drupal_goto('admin/settings/mp3player');
}
if ($pid == 1) {
drupal_set_message(t('You cannot delete the Default player.'), 'error');
drupal_goto('admin/settings/mp3player');
}
$form = array();
$form['pid'] = array(
'#type' => 'value',
'#value' => $pid,
);
return confirm_form($form, t('Are you sure you want to delete the player %name?', array(
'%name' => $player['name'],
)), 'admin/settings/mp3player', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function mp3player_player_delete_submit($form, &$form_state) {
$player = mp3player_players($form_state['values']['pid']);
db_query('DELETE FROM {mp3player_players} where pid = %d', $player['pid']);
drupal_set_message(t('MP3 Player %name was deleted.', array(
'%name' => $player['name'],
)));
mp3player_rebuild_cache();
$form_state['redirect'] = 'admin/settings/mp3player';
}
function mp3player_player_settings($pid = 1) {
$players = mp3player_players();
$player = $players[$pid];
unset($player['pid']);
unset($player['name']);
if ($player['transparentpagebg'] == 'no') {
$player['pagebg'] = 'FFFFFF';
}
$settings = array();
foreach ($player as $key => $value) {
$settings[] = '"' . $key . '":"' . $value . '"';
}
$out = array(
'AudioPlayer.setup("' . base_path() . libraries_get_path('audio-player') . '/player.swf", {',
);
$out[] = implode(',', $settings);
$out[] = '});';
return implode($out);
}
function mp3player_theme() {
return array(
'mp3player' => array(
'arguments' => array(
'player' => 'Default',
'file' => NULL,
'title' => NULL,
'artist' => NULL,
'description' => NULL,
),
),
);
}
function theme_mp3player($player = 'Default', $file = NULL, $title = NULL, $artist = NULL, $description = NULL) {
drupal_add_js(libraries_get_path('audio-player') . '/audio-player.js', 'module', 'footer');
drupal_add_js(drupal_get_path('module', 'mp3player') . '/mp3player.js', 'module', 'footer');
$extras = NULL;
$js_audio_settings = mp3player_player_settings();
drupal_add_js($js_audio_settings, 'inline', 'footer');
static $mp3_player_id;
$mp3_player_id++;
if ($title != NULL) {
$title = filter_xss($title);
$extras .= ',titles:"' . addslashes($title) . '"';
}
if ($artist != NULL) {
$artist = filter_xss($artist);
$extras .= ',artists:"' . addslashes($artist) . '"';
}
if ($description != NULL) {
$description = filter_xss($description);
$description = '<p class="mp3player_description">' . $description . '</p>';
}
if (module_exists('simplecdn')) {
$file = simplecdn_rewrite_url($file, 'mp3player');
}
if (variable_get('mp3player_encode', 'no') == 'yes') {
$audio_url = mp3player_encodeSource($file);
}
else {
$audio_url = $file;
}
$audio_url = url($audio_url);
$extras = str_replace('"', "", $extras);
$extras = str_replace(":", "=", $extras);
$extras = str_replace(" ", "", $extras);
$extras = str_replace(",", "&", $extras);
$out = array();
$out[] = '<p id="mp3player_' . $mp3_player_id . '" class="mp3player" data="soundFile=' . $audio_url . $extras . '">';
$out[] = t("You may need: <a href='http://get.adobe.com/flashplayer/'>Adobe Flash Player</a>.");
$out[] = '</p>';
$out[] = $description;
return implode('', $out);
}
function mp3player_encodeSource($string) {
$source = utf8_decode($string);
$ntexto = "";
$codekey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
for ($i = 0; $i < strlen($string); $i++) {
$ntexto .= substr("0000" . base_convert(ord($string[$i]), 10, 2), -8);
}
$ntexto .= substr("00000", 0, 6 - strlen($ntexto) % 6);
$string = "";
for ($i = 0; $i < strlen($ntexto) - 1; $i = $i + 6) {
$string .= $codekey[intval(substr($ntexto, $i, 6), 2)];
}
return $string;
}
function mp3player_simplecdnapi($op) {
switch ($op) {
case 'load':
return array(
'eid' => 'mp3player',
'name' => 'MP3 Player',
);
break;
}
}
function mp3player_exclude_these_paths() {
$action = variable_get('mp3player_page_init_action', 'page_disable');
$page_list = variable_get('mp3player_page_list', '');
if (!empty($page_list)) {
$alias = drupal_get_path_alias($_GET['q']);
if (drupal_match_path($_GET['q'], $page_list) || drupal_match_path($alias, $page_list)) {
return $action == 'page_disable' ? 1 : 0;
}
}
return $action == 'page_disable' ? 0 : 1;
}