You are here

mp3player.module in MP3 Player 7.2

Same filename and directory in other branches
  1. 8 mp3player.module
  2. 6.2 mp3player.module
  3. 6 mp3player.module

mp3player main module file.

File

mp3player.module
View source
<?php

/**
 * @file
 * mp3player main module file.
 */

/**
 * Used to count unique IDs when theming.
 */
global $mp3_player_id;
$mp3_player_id = 0;

/**
 * Implementation of hook_help().
 */
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;
}

/**
 * Implementation of hook_permission().
 */
function mp3player_permission() {
  return array(
    'administer mp3player' => array(
      'title' => t('Administer mp3player'),
    ),
  );
}

/**
 * Implementation of hook_init().
 */
function mp3player_init() {
  if (!file_exists(libraries_get_path('audio-player') . '/audio-player.js') || !file_exists(libraries_get_path('audio-player') . '/player.swf')) {
    drupal_set_message(t('%file1 or %file2 were not found in %directory, download the <a href="http://wpaudioplayer.com/download">standalone WordPress Audio Player</a>.', array(
      '%file1' => 'audio-player.js',
      '%file2' => 'player.swf',
      '%directory' => '/' . libraries_get_path('audio-player'),
    )), 'error');
  }
  drupal_add_js(libraries_get_path('audio-player') . '/audio-player.js');
}

/**
 * Implementation of hook_menu().
 */
function mp3player_menu() {
  $items = array();
  $items['admin/config/media/mp3player'] = array(
    'title' => 'MP3 Player',
    'page callback' => 'mp3player_player_list',
    'access arguments' => array(
      'administer mp3player',
    ),
  );
  $items['admin/config/media/mp3player/player-list'] = array(
    'title' => 'Player List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/media/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/config/media/mp3player/global-settings'] = array(
    'title' => 'Global Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mp3player_settings_form',
    ),
    'access arguments' => array(
      'administer mp3player',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/media/mp3player/player/%'] = array(
    'title callback' => 'mp3player_title_callback',
    'title arguments' => array(
      'Edit player: !name',
      5,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mp3player_player_form',
      5,
    ),
    'access arguments' => array(
      'administer mp3player',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/media/mp3player/player/%/delete'] = array(
    'title callback' => 'mp3player_title_callback',
    'title arguments' => array(
      'Delete player: !name',
      5,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mp3player_player_delete',
      5,
    ),
    'access arguments' => array(
      'administer mp3player',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Return formatted list of mp3 players.
 */
function mp3player_player_list() {
  $header = array(
    t('Player Name'),
    t('Actions'),
  );
  $rows = array();
  $players = mp3player_players();
  foreach ($players as $pid => $player) {
    $row = array();
    $row[] = $player['name'];
    $links = array();
    $links[] = l(t('Edit'), 'admin/config/media/mp3player/player/' . $player['pid']);
    if ($pid > 1) {
      $links[] = l(t('Delete'), 'admin/config/media/mp3player/player/' . $player['pid'] . '/delete');
    }
    $row[] = implode('&nbsp;&nbsp;&nbsp;&nbsp;', $links);
    $rows[] = $row;
  }
  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
  return $output;
}

/**
 * Custom titles for certain pages.
 */
function mp3player_title_callback($title, $pid = 0) {
  $replacements = array();
  $player = mp3player_players($pid);
  $replacements['!name'] = $player['name'];
  return t($title, $replacements);
}

/**
 * Return list of all available players along with settings.
 */
function mp3player_players($pid = NULL) {
  $players = array();
  $result = db_select('mp3player_players', 'm')
    ->fields('m')
    ->execute();
  while ($player = $result
    ->fetchAssoc()) {
    $players[$player['pid']] = $player;
  }
  if ($pid && array_key_exists($pid, $players)) {
    return $players[$pid];
  }
  return $players;
}

/**
 * Form to add a new player.
 */
function mp3player_settings_form() {
  $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.'),
  );
  if (module_exists('filefieldmp3player')) {
    $options = array(
      'description' => t('Description'),
    );
    if (module_exists('id3')) {
      $options['id3'] = t('ID3 Template');
    }
    $form['mp3player_description'] = array(
      '#type' => 'select',
      '#title' => t('FileField Description Value'),
      '#default_value' => variable_get('mp3player_description', 'description'),
      '#options' => $options,
      '#description' => t('Either use default description field or ID3 template file (if available).'),
    );
  }
  return system_settings_form($form);
}

/**
 * Form to add / edit a player
 */
function mp3player_player_form($form, $form_state, $pid = NULL) {
  $player = mp3player_players($pid);

  // if there's no PID, grab the default and edit it.
  if (!$pid) {
    $player = $player[1];
    $player['pid'] = '';
    $player['name'] = '';
  }
  $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' => check_plain($player['name']),
    '#required' => TRUE,
    '#size' => '20',
    '#description' => t('Unique name of player. Please use alphanumeric characters, and underscores (_).'),
  );
  if ($player['name'] == 'Default') {
    $form['namegroup']['name']['#disabled'] = TRUE;
  }
  if ($pid) {
    $form['pid'] = array(
      '#type' => 'hidden',
      '#value' => $pid,
    );
  }
  $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' => check_plain($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' => check_plain($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' => check_plain($player['width']),
    '#required' => TRUE,
    '#size' => '10',
    '#description' => t('Width of the player. e.g. 290 (290 pixels) or 100%.'),
  );
  $form['colours'] = array(
    '#type' => 'fieldset',
    '#title' => t('Player Colour Scheme'),
    '#weight' => 0,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['colours']['transbg'] = array(
    '#type' => 'fieldset',
    '#title' => 'Player Background',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['colours']['transbg']['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['colours']['transbg']['pagebg'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Background Colour'),
    '#default_value' => check_plain($player['pagebg']),
    '#required' => FALSE,
    '#size' => '10',
    '#field_prefix' => '#',
    '#description' => t("Player background colour (set it to your page background when transparentbg is set to 'no')."),
  );
  $form['colours']['bg'] = array(
    '#type' => 'textfield',
    '#title' => t('Background'),
    '#default_value' => check_plain($player['bg']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['leftbg'] = array(
    '#type' => 'textfield',
    '#title' => t('Left Background'),
    '#default_value' => check_plain($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' => check_plain($player['lefticon']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['voltrack'] = array(
    '#type' => 'textfield',
    '#title' => t('Volume Track Background'),
    '#default_value' => check_plain($player['voltrack']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['volslider'] = array(
    '#type' => 'textfield',
    '#title' => t('Volume Track Slider'),
    '#default_value' => check_plain($player['volslider']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['rightbg'] = array(
    '#type' => 'textfield',
    '#title' => t('Right Background'),
    '#default_value' => check_plain($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' => check_plain($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' => check_plain($player['righticon']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['righticonhover'] = array(
    '#type' => 'textfield',
    '#title' => t('Play/Pause Icon (hover state)'),
    '#default_value' => check_plain($player['righticonhover']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['loader'] = array(
    '#type' => 'textfield',
    '#title' => t('Loading Bar'),
    '#default_value' => check_plain($player['loader']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['track'] = array(
    '#type' => 'textfield',
    '#title' => t('Track Backgrounds'),
    '#default_value' => check_plain($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' => check_plain($player['tracker']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['border'] = array(
    '#type' => 'textfield',
    '#title' => t('Progress Track Border'),
    '#default_value' => check_plain($player['border']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['skip'] = array(
    '#type' => 'textfield',
    '#title' => t('Previous/Next Buttons'),
    '#default_value' => check_plain($player['skip']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['colours']['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Text'),
    '#default_value' => check_plain($player['text']),
    '#required' => TRUE,
    '#size' => '10',
    '#field_prefix' => '#',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save changes to player'),
  );
  return $form;
}

/**
 * Custom validation function for the system settings form.
 */
function mp3player_player_form_validate($form, &$form_state) {
  if (!$form_state['values']['pid']) {
    $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, or 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('Volume must be numeric between 1 and 100'));
  }
  if (!is_numeric($form_state['values']['buffer'])) {
    form_set_error('buffer', t('Buffer must be numeric!'));
  }
  if (!is_numeric($form_state['values']['initialvolume'])) {
    form_set_error('initialvolume', t('Volume must be an number'));
  }
  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.'));
  }

  // fields to test hex value of.
  $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 == 'no') {
    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);
    }
  }
}

/**
 * Various tests to hex values.
 * @param $value
 *
 * @return bool|null|string
 */
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;
}

/**
 * Add a new player.
 */
function mp3player_player_form_submit($form, &$form_state) {
  $search = array();
  $message = 'New MP3 Player %name added.';
  if (isset($form_state['values']['pid'])) {
    $search = 'pid';
    $message = 'New MP3 Player %name updated';
  }
  drupal_write_record('mp3player_players', $form_state['values'], $search);
  drupal_set_message(t($message, array(
    '%name' => $form_state['values']['name'],
  )));
  $form_state['redirect'] = 'admin/config/media/mp3player';
  drupal_static_reset('mp3player_player_settings');
}

/**
 * Form to delete a player.
 */
function mp3player_player_delete($form_state, $pid = NULL) {
  $player = mp3player_players($pid);
  if (empty($player)) {
    drupal_set_message(t('The specified player was not found.'), 'error');
    drupal_goto('admin/settings/mp3player');
  }
  if ($player['name'] == 'Default') {
    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'));
}

/**
 * Delete a player.
 */
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'],
  )));
  $form_state['redirect'] = 'admin/settings/mp3player';
}

/**
 * Returns a string of audio player settings.
 *
 * @param int $pid of the player. Defaults to 1 (default player)
 * @return string
 */
function mp3player_player_settings($pid = 1) {
  $player_cache =& drupal_static(__FUNCTION__, array());
  if (!isset($player_cache[$pid])) {
    $player = mp3player_players($pid);
    if ($player['pagebg'] == '') {
      $player['pagebg'] = FALSE;
    }
    $encode = json_encode($player);
    $player_cache[$pid] = $encode;
  }
  $settings = $player_cache[$pid];
  $js_audio_settings = 'AudioPlayer.setup("/' . libraries_get_path('audio-player') . '/player.swf", ';
  $js_audio_settings .= $settings;
  $js_audio_settings .= ');';
  return $js_audio_settings;
}

/**
 * Implementation of hook_theme().
 */
function mp3player_theme() {
  return array(
    'mp3player' => array(
      'variables' => array(
        'player' => 1,
        'file' => NULL,
        'title' => '',
      ),
    ),
  );
}

/**
 * Setup theme function for the MP3 Player.
 */
function theme_mp3player($variables) {
  global $mp3_player_id;
  $mp3_player_id++;
  $id = 'mp3player_' . $mp3_player_id;
  $player = $variables['player'];
  $file = $variables['file'];
  $title = $variables['title'];
  $audio_url = file_create_url($file);
  if (variable_get('mp3player_encode', 'no') == 'yes') {
    $audio_url = mp3player_encodeSource($file);
  }
  $audio_url = url($audio_url);
  $options = array();
  $options[] = 'soundFile: "' . $audio_url . '"';
  if (isset($title)) {
    $title = filter_xss($title);
    $options[] = 'titles: "' . addslashes($title) . '"';
  }
  if (isset($artist)) {
    $artist = filter_xss($artist);
    $options[] = 'artists: "' . addslashes($artist) . '"';
  }
  $options = implode(',', $options);

  // WE ONLY WANT THE SETUP ONCE PER PAGE
  $setup =& drupal_static(__FUNCTION__);
  if (!$setup) {
    $setup = mp3player_player_settings($player);
    drupal_add_js($setup, 'inline');
  }
  $return = array();
  $return[] = '<p id="' . $id . '">';
  $return[] = t("No Adobe Flash Player installed. <a href='http://get.adobe.com/flashplayer/'>Get it now.</a>");
  $return[] = '</p>';
  $return[] = '<div class="mp3_title">' . $title . '</div>';
  $return[] = '<script type="text/javascript">AudioPlayer.embed("' . $id . '", {' . $options . '});</script>';
  return implode('', $return);
}

/**
 * Encode a URL.
 */
function mp3player_encodeSource($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;
}

Functions

Namesort descending Description
mp3player_encodeSource Encode a URL.
mp3player_help Implementation of hook_help().
mp3player_init Implementation of hook_init().
mp3player_menu Implementation of hook_menu().
mp3player_permission Implementation of hook_permission().
mp3player_players Return list of all available players along with settings.
mp3player_player_delete Form to delete a player.
mp3player_player_delete_submit Delete a player.
mp3player_player_form Form to add / edit a player
mp3player_player_form_submit Add a new player.
mp3player_player_form_validate Custom validation function for the system settings form.
mp3player_player_list Return formatted list of mp3 players.
mp3player_player_settings Returns a string of audio player settings.
mp3player_settings_form Form to add a new player.
mp3player_theme Implementation of hook_theme().
mp3player_title_callback Custom titles for certain pages.
mp3player_validate_hex Various tests to hex values.
theme_mp3player Setup theme function for the MP3 Player.

Globals

Namesort descending Description
$mp3_player_id Used to count unique IDs when theming.