diff --git a/.gitignore b/.gitignore
index c0730d1..db62a64 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
/jplayer.tmproj
/theme/.DS_Store
/.gitignore
-/.DS_Store
\ No newline at end of file
+/.DS_Store
+*.swp
diff --git a/includes/jplayer.admin.inc b/includes/jplayer.admin.inc
index 9b1b131..c634f8b 100644
--- a/includes/jplayer.admin.inc
+++ b/includes/jplayer.admin.inc
@@ -114,24 +114,6 @@ function jplayer_settings_form($form, &$form_state) {
'#default_value' => variable_get('jplayer_sepSec', ''),
);
- $form['jplayer_protected'] = array(
- '#title' => t('Protect audio files from direct downloads'),
- '#type' => 'checkbox',
- '#default_value' => variable_get('jplayer_protected', FALSE),
- );
-
- if (variable_get('file_private_path', '') == '') {
- $form['jplayer_protected']['#description'] = t('To enable file download protection, first set a Private file system path and set protected file fields to use it..', array('@file-system-settings' => url('admin/config/media/file-system', array('query' => drupal_get_destination()))));
- }
-
- $form['jplayer_access_time'] = array(
- '#title' => t('Access delay'),
- '#type' => 'textfield',
- '#default_value' => variable_get('jplayer_access_time', 30),
- '#size' => 5,
- '#description' => t('The number of seconds that a client will have access to download a protected file after it is requested by jPlayer.'),
- );
-
$form['#validate'][] = 'jplayer_settings_form_validate';
$form['#submit'][] = 'jplayer_settings_form_submit';
@@ -149,14 +131,6 @@ function jplayer_settings_form_validate($form, &$form_state) {
else {
$form_state['values']['version'] = $version;
}
-
- $time = $form_state['values']['jplayer_access_time'];
- if ($form_state['values']['jplayer_protected'] && !is_numeric($time)) {
- form_error($form['jplayer_access_time'], t('Access time must be a value in seconds.'));
- }
- if (intval($time) < 0) {
- form_error($form['jplayer_access_time'], t('Access time must be at least 0 seconds.'));
- }
}
/**
@@ -166,56 +140,3 @@ function jplayer_settings_form_submit($form, &$form_state) {
drupal_set_message(t('The jPlayer library (version @version) successfully found in the %directory directory.', array('@version' => $form_state['values']['version']['version'], '%directory' => $form_state['values']['jplayer_directory'])));
}
-/**
- * Page callback for the jPlayer protection statistics page. This page is
- * useful in determining if a browser is misbehaving and blocking legitimiate
- * file accesses, or if a user is trying to download a protected file.
- */
-function jplayer_protection_statistics() {
- if (!variable_get('jplayer_protected', FALSE)) {
- drupal_set_message(t('jPlayer content protection is not currently enabled.', array('@jplayer-settings' => url('admin/settings/jplayer', array('query' => drupal_get_destination())))));
- }
-
- $output = '
' . t('This table shows the 50 top users who have been denied access to direct downloads of jPlayer files.') . '
';
-
- // TODO: convert to DBTNG.
- $result = db_query("SELECT COUNT(1) as total, uid as user, MAX(timestamp) as timestamp FROM {jplayer_denied} GROUP BY uid ORDER BY total DESC, timestamp DESC LIMIT 50;");
- $rows = array();
- foreach ($result as $denied) {
- $denied = (array)$denied;
- // Format data from the query.
- $uid = $denied['user'];
- $denied['user'] = theme('username', array('account' => user_load($denied['user'])));
- $denied['timestamp'] = format_date($denied['timestamp']);
-
- // Find the top-denied file for the user.
- // TODO: convert to DBTNG.
- $top_file = db_query("SELECT COUNT(fid) as fid_count, fid FROM {jplayer_denied} WHERE uid = :uid GROUP BY fid ORDER BY fid_count DESC LIMIT 1", array(':uid' => $uid))->fetchObject();
- $top_file = file_load($top_file->fid);
- $denied['file'] = str_replace($GLOBALS['base_url'], '', file_create_url($top_file->uri));
-
- // Find the top hostname for the user.
- // TODO: convert to DBTNG.
- $top_hostname = db_query("SELECT COUNT(hostname) as hostname_count, hostname FROM {jplayer_denied} WHERE uid = :uid GROUP BY hostname ORDER BY hostname_count DESC LIMIT 1", array(':uid' => $uid))->fetchObject();
- $denied['hostname'] = $top_hostname->hostname;
-
- $rows[] = $denied;
- }
-
- $header = array(
- t('Accesses denied'),
- t('User'),
- t('Last denied'),
- t('Top Denied File'),
- t('Top Hostname'),
- );
-
- if (!empty($rows)) {
- $output .= theme('table', array('header' => $header, 'rows' => $rows));
- }
- else {
- $output .= '
' . t('There have been no files that have been denied access within the last week.') . '
';
- }
- return $output;
-}
-
diff --git a/jplayer.install b/jplayer.install
index 4347e52..f23b658 100644
--- a/jplayer.install
+++ b/jplayer.install
@@ -8,46 +8,6 @@
* Implements hook_schema().
*/
function jplayer_schema() {
- $schema = array();
- $schema['jplayer_denied'] = array(
- 'description' => 'Contains user statistics for when a user is blocked from downloading a file.',
- 'fields' => array(
- 'uid' => array(
- 'description' => 'The user ID of the user.',
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0,
- ),
- 'fid' => array(
- 'description' => 'The file ID that was denied access.',
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0,
- ),
- 'hostname' => array(
- 'description' => 'The hostname of the user that was denied access.',
- 'type' => 'varchar',
- 'length' => 128,
- 'not null' => TRUE,
- 'default' => '',
- ),
- 'timestamp' => array(
- 'description' => 'The last time this user was denied access.',
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0,
- ),
- ),
- 'primary key' => array('uid', 'fid', 'timestamp'),
- 'indexes' => array(
- 'uid' => array('uid'),
- 'fid' => array('fid'),
- 'hostname' => array('hostname'),
- 'timestamp' => array('timestamp'),
- ),
- );
-
- return $schema;
}
/**
@@ -75,8 +35,7 @@ function jplayer_requirements($phase) {
/**
* Implements hook_uninstall().
*/
-function jplayer_uninstall() {
- variable_del('jplayer_protected');
+function jplayer_protect_uninstall() {
variable_del('jplayer_access_time');
}
@@ -128,3 +87,25 @@ function jplayer_update_7001() {
db_create_table('jplayer_denied', $schema['jplayer_denied']);
}
}
+
+/**
+ * Transition to the jplayer_protect module if jplayer content protection is
+ * enabled. */
+function jplayer_update_7002() {
+ if (variable_get('jplayer_protected', FALSE)) {
+ variable_set('jplayer_protect', TRUE);
+ if (!module_exists('jplayer_protect')) {
+ module_enable(array('jplayer_protect'));
+ db_drop_table('jplayer_protect_denied');
+ db_rename_table('jplayer_denied', 'jplayer_protect_denied');
+ }
+ else {
+ return t('It appears that the jplayer_protect module has been manually enabled. Please move any data from the {jplayer_protect} table to the {jplayer_protect_denied} table and drop the {jplayer_protect} table.');
+ }
+ }
+ else {
+ db_drop_table('jplayer_denied');
+ }
+ variable_del('jplayer_protected');
+}
+
diff --git a/jplayer.module b/jplayer.module
index 96e4eaf..cbf31cf 100644
--- a/jplayer.module
+++ b/jplayer.module
@@ -22,164 +22,10 @@ function jplayer_menu() {
'file' => 'includes/jplayer.admin.inc',
);
- $items['admin/reports/jplayer'] = array(
- 'title' => 'jPlayer content protection statistics',
- 'page callback' => 'jplayer_protection_statistics',
- 'access arguments' => array('access site reports'),
- 'description' => 'View statistics related to content protection for jPlayer.',
- 'file' => 'jplayer.admin.inc',
- 'file path' => drupal_get_path('module', 'jplayer') . '/includes',
- );
-
- $items['jplayer/authorize'] = array(
- 'title' => 'jPlayer content authorization',
- 'page callback' => 'jplayer_authorize',
- 'delivery callback' => 'ajax_deliver',
- 'access arguments' => array('access content'),
- 'description' => 'jPlayer callback to authorize a sound file to be accessed.',
- 'type' => MENU_CALLBACK,
- );
-
return $items;
}
/**
- * Menu callback to authorize access to a file.
- */
-function jplayer_authorize($filepath, $timestamp) {
- $filepath = base64_decode($filepath);
- $timestamp = (int)base64_decode($timestamp);
-
- if (!isset($_SESSION['jplayer_protect'])) {
- $_SESSION['jplayer_protect'] = array();
- }
-
- if ($timestamp < (REQUEST_TIME + variable_get('jplayer_access_time', 30))) {
- $_SESSION['jplayer_protect'][$filepath] = $timestamp;
- }
-
- return drupal_json_encode(REQUEST_TIME + variable_get('jplayer_access_time', 30));
-}
-
-/**
- * Implements hook_cron().
- */
-function jplayer_cron() {
- // Delete records older than a week.
- // TODO Needs dbtng.
- db_query("DELETE FROM {jplayer_denied} WHERE timestamp < :time", array(':time' => time() - 604800));
-}
-
-/**
- * Implements hook_file_download().
- */
-function jplayer_file_download($uri) {
- if (!variable_get('jplayer_protected', FALSE)) {
- return NULL;
- }
-
- // We need to determine if we are responsible for this file.
- // TODO dbtng.
- // TODO use EntityFieldQuery.
- $result = db_query("SELECT fid FROM {file_managed} WHERE uri = :uri", array(':uri' => $uri));
- $file = file_load($result->fetchField());
-
- // If the file is not found in the database, we're not responsible for it.
- if (empty($file)) {
- return NULL;
- }
-
- // Find out if any file field contains this file, and if so, which field
- // and node it belongs to. Required for later access checking.
- $instances = array();
- $entities = array();
- foreach (field_info_fields() as $field_name => $field) {
- if ($field['type'] == 'file') {
- $query = new EntityFieldQuery();
- $query->fieldCondition($field_name, 'fid', $file->fid, '=');
- $entities = $query->execute();
- if (empty($entities)) {
- continue;
- }
-
- foreach ($entities as $entity_type => $entity_list) {
- foreach ($entity_list as $entity) {
- $bundle_name = $entity->type;
- $instances[$field_name] = field_info_instance($entity_type, $field_name, $bundle_name);
- }
- }
- }
- }
-
- // If any of the displays for this field are for jPlayer, then we need to
- // protect the file.
- $display_found = FALSE;
- foreach ($instances as $field_name => $instance) {
- foreach ($instance['display'] as $display_mode => $display) {
- // Neither the teaser or the full formatter for this field is a jPlayer
- // display.
- if ($display['type'] == 'jplayer_player') {
- $display_found = TRUE;
- break;
- }
- }
- }
- if (!$display_found) {
- return NULL;
- }
-
- $access_key = file_create_url($uri);
- $filepath = str_replace($GLOBALS['base_url'], '', $access_key);
-
- if (isset($_SESSION['jplayer_protect'][$access_key])) {
- $started = (int)$_SESSION['jplayer_protect'][$access_key];
- }
- else {
- // We need to figure out how the browser would have URL-encoded the file
- // name. If mod_rewrite is modifying the URL, it will decode URL-encoded
- // characters, so we need to check both.
- $encoded = str_replace($file->filename, rawurlencode($file->filename), $filepath);
- // TODO replace this with the path to the files directory?
- $encoded = str_replace('sites/default/files', 'system/files', $encoded);
- // For some reason ampersands are encoded twice by the browser.
- $encoded = str_replace("%26", "%2526", $encoded);
- $encoded_access_key = $GLOBALS['base_url'] . '/' . $encoded;
-
- if (isset($_SESSION['jplayer_protect'][$encoded_access_key])) {
- $access_key = $encoded_access_key;
- $started = (int)$_SESSION['jplayer_protect'][$access_key];
- }
- }
-
- // Now we know that content protection is enabled, at least one display for
- // the field uses jPlayer, and we know when the player last started to access
- // the file.
- if (isset($started) && $started) {
- if (time() <= ($started + variable_get('jplayer_access_time', 30))) {
- // Allow access, and immediately expire access to the file. Some browsers
- // (such as Chrome) send multiple HTTP requests for an