<?php
// $Id: getid3.module,v 1.10.2.4 2010/07/25 03:48:38 drewish Exp $

define('GETID3_RECOMMEND_VERSION', '1.7.9');

/**
 * Implementation of hook_help
 */
function getid3_help($section, $arg) {
  switch ($section) {
    case 'admin/settings/getid3':
      $help = '<p>'. t("To use this module you'll need to <a href='!download-link'>download the library</a> from the <a href='!info-link'>getID3 website</a> and extract the contents into the  module's getid3 directory. Currently, the recommended version of the getID3 library is %recommended-version.", array(
        '!download-link' => url('http://prdownloads.sourceforge.net/getid3'),
        '!info-link' => url('http://getid3.org/'),
        '%recommended-version' => GETID3_RECOMMEND_VERSION
      )) .'</p>';
      return $help;
  }
}

/**
 * Loads the getID3 library once and returns whether it was successfully loaded.
 *
 * @return
 *   Boolean indicating if the library was loaded
 */
function getid3_load($display_warning = TRUE) {
  $getid3_path = getid3_get_path();

  if (file_exists($getid3_path .'/getid3.php') && file_exists($getid3_path .'/write.php')) {
    // A little workaround for getID3 on Windows.
    if (!defined('GETID3_HELPERAPPSDIR')) {
      define('GETID3_HELPERAPPSDIR', realpath($getid3_path .'/../helperapps') .'/');
    }
    include_once($getid3_path .'/getid3.php');

    // Initialize getID3 tag-writing module. NOTE: Their wanky dependency setup
    // requires that this file must be included AFTER an instance of the getID3
    // class has been instantiated.
    $getid3 = new getID3;
    require_once($getid3_path .'/write.php');

    return defined('GETID3_VERSION');
  }
  else {
    drupal_set_message(t("The getID3() module cannot find the getID3 library used to read and write ID3 tags. The site administrator will need to verify that it is installed and then update the <a href='!admin-settings-audio-getid3'>settings</a>.", array('!admin-settings-audio-getid3' => url('admin/settings/getid3'))), 'error', FALSE);
    return FALSE;
  }
}

/**
 * Create and initialize an instance of getID3 class.
 */
function &getid3_instance() {
  $id3 = NULL;
  if (getid3_load()) {
    $id3 = new getID3();
    // MD5 is a big performance hit. Disable it by default.
    $id3->option_md5_data = FALSE;
    $id3->option_md5_data_source = FALSE;
    $id3->encoding = 'UTF-8';
  }
  return $id3;
}

/**
 * Analyze file and return its media information.
 */
function &getid3_analyze($path) {
  $info = array();
  if($id3 = &getid3_instance()) {
    $info = $id3->analyze($path);
    unset($id3);
  }
  return $info;
}

/**
 * Implementation of hook_menu()
 */
function getid3_menu() {
  $items['admin/settings/getid3'] = array(
    'title' => 'getID3()',
    'description' => 'Configure settings associated with getID3().',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('getid3_admin_settings_form', NULL),
    'access arguments' => array('administer site configuration'),
    'file' => 'getid3.admin.inc',
  );
  return $items;
}

/**
 * Returns the path where getID3() is installed.
 */
function getid3_get_path() {
  return variable_get('getid3_path', 'sites/all/libraries/getid3/getid3');
}

/**
 * Returns the version number of getID3() that's installed.
 */
function getid3_get_version() {
  return getid3_load(FALSE) ? GETID3_VERSION : NULL;
}
