| 1 |
<?php /* $Id chill35 $ */
|
| 2 |
/**
|
| 3 |
* @file Download counter
|
| 4 |
*/
|
| 5 |
/**
|
| 6 |
* Implementation of hook_help()
|
| 7 |
*/
|
| 8 |
function download_count_help($section) {
|
| 9 |
switch ($section) {
|
| 10 |
case 'admin/modules#description':
|
| 11 |
// This description is shown in the listing at admin/modules.
|
| 12 |
return t('Increments a download counter and logs a descriptive message each time an attached file is downloaded.');
|
| 13 |
}
|
| 14 |
}
|
| 15 |
/**
|
| 16 |
* Implementation of hook_perm()
|
| 17 |
*/
|
| 18 |
function download_count_perm() {
|
| 19 |
$perms = array();
|
| 20 |
$perms[] = 'view all downloads count';
|
| 21 |
$perms[] = 'view own nodes downloads count';
|
| 22 |
$perms[] = 'view all downloads count in nodes';
|
| 23 |
$perms[] = 'view own nodes downloads count in nodes';
|
| 24 |
return $perms;
|
| 25 |
}
|
| 26 |
/**
|
| 27 |
* Implementation of hook_menu()
|
| 28 |
*/
|
| 29 |
function download_count_menu($may_cache) {
|
| 30 |
$items = array();
|
| 31 |
$access = user_access('view all downloads count') || user_access('view own nodes downloads count');
|
| 32 |
|
| 33 |
if ($may_cache) {
|
| 34 |
$items[] = array('path' => 'download_counter',
|
| 35 |
'title' => t('download counter'),
|
| 36 |
'callback' => 'download_count_view_page',
|
| 37 |
'access' => $access,
|
| 38 |
'type' => MENU_NORMAL_ITEM);
|
| 39 |
}
|
| 40 |
|
| 41 |
return $items;
|
| 42 |
|
| 43 |
}
|
| 44 |
|
| 45 |
// Implementation of hook_settings()
|
| 46 |
function download_count_settings() {
|
| 47 |
|
| 48 |
switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
|
| 49 |
case FILE_DOWNLOADS_PUBLIC:
|
| 50 |
$output = '<p>' . t('You are using public download. Set your download method to private on the ') . l('admin/settings', 'admin/settings') . t(' page (go in "File system settings"), if you wish for Drupal to keep a record of downloaded files.') . '</p>';
|
| 51 |
break;
|
| 52 |
case FILE_DOWNLOADS_PRIVATE:
|
| 53 |
$output = '<p>' . t('You are using the private download method, hence you have the correct setting to allow this module to keep a record of downloaded files.') . '</p>';
|
| 54 |
break;
|
| 55 |
}
|
| 56 |
|
| 57 |
$form['info_on_download_method'] = array(
|
| 58 |
'#value' => $output,
|
| 59 |
);
|
| 60 |
|
| 61 |
$form['ignoring a set of file extensions'] = array(
|
| 62 |
'#type' => 'fieldset',
|
| 63 |
'#title' => t('Ignoring certain file extensions'),
|
| 64 |
'#collapsible' => TRUE,
|
| 65 |
'#collapsed' => FALSE,
|
| 66 |
);
|
| 67 |
|
| 68 |
$form['ignoring a set of file extensions']['excluded file extensions'] = array(
|
| 69 |
'#type' => 'textfield',
|
| 70 |
'#title' => t('Excluded file extensions'),
|
| 71 |
'#default_value' => variable_get('excluded file extensions', 'jpg jpeg gif png'),
|
| 72 |
'#maxlength' => 255,
|
| 73 |
'#description' => t('This module only considers files that have been uploaded with the upload module and that become file attachments. However, if you are using a contributed module to upload images and display them in the body of nodes, these files may get flagged as downloaded whenever a visitor or robot views the node page, because strictly speaking they are file attachments. This will happen with the module img_assist. Note that this won\'t happen with the module imce, because imce treats inline images as nodes. If you do not want to set a download counter for image files, list their extension here. Separate extensions with a space and do not include the leading dot. For example, you could list these extensions : jpg jpeg gif png. If you do not want to exclude any file, leave that field blank. ')
|
| 74 |
);
|
| 75 |
|
| 76 |
$form['dounload count in nodes'] = array(
|
| 77 |
'#type' => 'fieldset',
|
| 78 |
'#title' => t('Download count in nodes'),
|
| 79 |
'#collapsible' => TRUE,
|
| 80 |
'#collapsed' => FALSE,
|
| 81 |
);
|
| 82 |
|
| 83 |
$form['dounload count in nodes']['do_not_show_download_count_in_node_for_admin'] = array(
|
| 84 |
'#type' => 'checkbox',
|
| 85 |
'#title' => t('Do not show download count in the node view for the administrator'),
|
| 86 |
'#default_value' => variable_get('do_not_show_download_count_in_node_for_admin', FALSE),
|
| 87 |
'#description' => t('Check this if you do not want to see the download count in the node view.'),
|
| 88 |
);
|
| 89 |
|
| 90 |
$form['download counter page'] = array(
|
| 91 |
'#type' => 'fieldset',
|
| 92 |
'#title' => t('Download counter page'),
|
| 93 |
'#collapsible' => TRUE,
|
| 94 |
'#collapsed' => FALSE,
|
| 95 |
);
|
| 96 |
|
| 97 |
$form['download counter page']['download_counter_view_page_title'] = array(
|
| 98 |
'#type' => 'textfield',
|
| 99 |
'#title' => t('Title'),
|
| 100 |
'#default_value' => variable_get('download_counter_view_page_title', t('Download counter')),
|
| 101 |
'#description' => t('Title of this ') . l('page', 'download_counter') . '.',
|
| 102 |
);
|
| 103 |
|
| 104 |
$form['download counter page']['download_counter_view_page_header'] = array(
|
| 105 |
'#type' => 'textarea',
|
| 106 |
'#title' => t('Header'),
|
| 107 |
'#default_value' => variable_get('download_counter_view_page_header', t('')),
|
| 108 |
'#cols' => 60,
|
| 109 |
'#rows' => 6,
|
| 110 |
'#description' => t('Text to appear between the title of the page and the download counter table.'),
|
| 111 |
);
|
| 112 |
|
| 113 |
$form['download counter page']['download_counter_view_page_footer'] = array(
|
| 114 |
'#type' => 'textarea',
|
| 115 |
'#title' => t('Footer'),
|
| 116 |
'#default_value' => variable_get('download_counter_view_page_footer', t('')),
|
| 117 |
'#cols' => 60,
|
| 118 |
'#rows' => 6,
|
| 119 |
'#description' => t('Text to appear underneath the download counter table.'),
|
| 120 |
);
|
| 121 |
|
| 122 |
$form['download counter page']['download_counter_view_page_format'] = filter_form(variable_get('download_counter_view_page_format', 2), 1, array('download_counter_view_page_format'));
|
| 123 |
|
| 124 |
$form['info_on_access control'] = array(
|
| 125 |
'#value' => '<p>' . t('Visit the ') . l('control access page', 'admin/access') . ' to allow roles to view download count in nodes and on the download counter page.</p>',
|
| 126 |
);
|
| 127 |
|
| 128 |
return $form;
|
| 129 |
}
|
| 130 |
|
| 131 |
function download_count_view_page() {
|
| 132 |
|
| 133 |
global $user;
|
| 134 |
|
| 135 |
drupal_set_title(variable_get('download_counter_view_page_title', t('Download counter')));
|
| 136 |
|
| 137 |
$header[] = array('data' => t('filename'), 'field' => 'filename');
|
| 138 |
$header[] = array('data' => t('hits'), 'field' => 'count', 'sort' => 'desc');
|
| 139 |
$header[] = array('data' => t('last download'), 'field' => 'timestamp');
|
| 140 |
$header[] = array('data' => t('action'));
|
| 141 |
|
| 142 |
$rows = array();
|
| 143 |
|
| 144 |
$fileDirectoryPath = file_directory_path() . '/';
|
| 145 |
|
| 146 |
if(user_access('view all downloads count')) {
|
| 147 |
$result = db_query("SELECT fd.filename, fd.count, fd.timestamp, f.nid, n.type FROM {file_downloads} fd JOIN {files} f ON f.filepath = CONCAT('%s', fd.filename) JOIN {node} n ON n.nid = f.nid" . tablesort_sql($header), $fileDirectoryPath);
|
| 148 |
}
|
| 149 |
else {
|
| 150 |
$result = db_query("SELECT fd.filename, fd.count, fd.timestamp, f.nid, n.type FROM {file_downloads} fd JOIN {files} f ON f.filepath = CONCAT('%s', fd.filename) JOIN {node} n ON n.nid = f.nid WHERE n.uid = %d" . tablesort_sql($header), $fileDirectoryPath, $user->uid);
|
| 151 |
}
|
| 152 |
|
| 153 |
while ($file = db_fetch_object($result)) {
|
| 154 |
$row = array();
|
| 155 |
$row[] = $file->filename;
|
| 156 |
$row[] = $file->count;
|
| 157 |
$row[] = format_interval(time() - $file->timestamp) . ' ago';
|
| 158 |
$row[] = l(t('view ' . $file->type), 'node/' . $file->nid);
|
| 159 |
$rows[] = $row;
|
| 160 |
}
|
| 161 |
|
| 162 |
if (empty($rows)) {
|
| 163 |
$rows[] = array(array('data' => t('No file attachment has been downloaded.'), 'colspan' => '4'));
|
| 164 |
}
|
| 165 |
|
| 166 |
$output = check_markup(variable_get('download_counter_view_page_header', ''),
|
| 167 |
variable_get('download_counter_view_page_format', 0),
|
| 168 |
false);
|
| 169 |
|
| 170 |
$output .= theme('table', $header, $rows, array('class' => 'download_count'));
|
| 171 |
|
| 172 |
$output .= check_markup(variable_get('download_counter_view_page_footer', ''),
|
| 173 |
variable_get('download_counter_view_page_format', 0),
|
| 174 |
false);
|
| 175 |
|
| 176 |
return $output;
|
| 177 |
}
|
| 178 |
|
| 179 |
/**
|
| 180 |
* Implementation of file_download()
|
| 181 |
*/
|
| 182 |
function download_count_file_download($filename) {
|
| 183 |
|
| 184 |
$extensions = explode(' ', trim(variable_get('excluded file extensions', 'jpg jpeg gif png')));
|
| 185 |
if (count($extensions)) {
|
| 186 |
$pathinfo = pathinfo($filename);
|
| 187 |
if (in_array($pathinfo['extension'], $extensions)) {
|
| 188 |
return;
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
| 192 |
$file = file_create_path($filename);
|
| 193 |
$result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
|
| 194 |
if ($file = db_fetch_object($result)) {
|
| 195 |
if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
|
| 196 |
$message = t('%file was downloaded', array('%file' => theme('placeholder', $filename)));
|
| 197 |
watchdog('download', $message, WATCHDOG_NOTICE);
|
| 198 |
// If the file is already added, just increment the count,
|
| 199 |
// otherwise add the file with count 1
|
| 200 |
if(db_result(db_query("SELECT filename FROM {file_downloads} WHERE filename = '%s'", $filename))) {
|
| 201 |
db_query("UPDATE {file_downloads} SET count = count+1, timestamp = %d WHERE filename = '%s'", time(), $filename);
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
|
| 205 |
}
|
| 206 |
}
|
| 207 |
else {
|
| 208 |
$message = t('Failed to download %file', array('%file' => theme('placeholder', $filename)));
|
| 209 |
watchdog('download', $message, WATCHDOG_WARNING);
|
| 210 |
}
|
| 211 |
}
|
| 212 |
}
|
| 213 |
/**
|
| 214 |
* Implementation of hook_nodeapi()
|
| 215 |
*/
|
| 216 |
function download_count_nodeapi(&$node, $op, $teaser) {
|
| 217 |
switch ($op) {
|
| 218 |
case 'view':
|
| 219 |
global $user;
|
| 220 |
if ($user->uid == 1 && variable_get('do_not_show_download_count_in_node_for_admin', FALSE)) {
|
| 221 |
return;
|
| 222 |
}
|
| 223 |
if (!$teaser && count($node->files)) {
|
| 224 |
if (user_access('view all downloads count in nodes') || (user_access('view own nodes downloads count in nodes') && ($node->uid == $user->uid))) {
|
| 225 |
$node->body = theme('download_count_body', $node);
|
| 226 |
}
|
| 227 |
}
|
| 228 |
}
|
| 229 |
return;
|
| 230 |
}
|
| 231 |
|
| 232 |
function theme_download_count_body($node) {
|
| 233 |
|
| 234 |
$header[] = array('data' => t('Attachment'));
|
| 235 |
$header[] = array('data' => t('Size'));
|
| 236 |
$header[] = array('data' => t('Hits'));
|
| 237 |
$header[] = array('data' => t('Last download'));
|
| 238 |
$rows = array();
|
| 239 |
|
| 240 |
$fileDirectoryPath = file_directory_path() . '/';
|
| 241 |
|
| 242 |
foreach ($node->files as $file) {
|
| 243 |
if ($file->list) {
|
| 244 |
$href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
|
| 245 |
$text = $file->description ? $file->description : $file->filename;
|
| 246 |
$pick = db_query("SELECT filename, count, timestamp FROM {file_downloads} WHERE CONCAT('%s', filename) = '%s'", $fileDirectoryPath, $file->filepath);
|
| 247 |
if ($attach = db_fetch_object($pick)){
|
| 248 |
$count = $attach->count;
|
| 249 |
$last = format_interval(time() - $attach->timestamp) . ' ago';
|
| 250 |
}
|
| 251 |
else {
|
| 252 |
$count = 0;
|
| 253 |
$last = t('Not yet downloaded');
|
| 254 |
}
|
| 255 |
if (user_access('view uploaded files')) {
|
| 256 |
$rows[] = array(l($text, $href), format_size($file->filesize), $count, $last);
|
| 257 |
}
|
| 258 |
else {
|
| 259 |
$rows[] = array($file->filename, format_size($file->filesize), $count, $last);
|
| 260 |
}
|
| 261 |
}
|
| 262 |
}
|
| 263 |
if (count($rows)) {
|
| 264 |
$attachments = theme('table', $header, $rows, array('id' => 'attachments'));
|
| 265 |
}
|
| 266 |
else {
|
| 267 |
$attachments = '';
|
| 268 |
}
|
| 269 |
|
| 270 |
if (strstr($node->body, '<table id="attachments"')) {
|
| 271 |
$start = strpos ($node->body, '<table id="attachments"');
|
| 272 |
$end = strpos ($node->body, '</table>', $start);
|
| 273 |
// return substr_replace($node->body, '', $start, $end - $start + 8) . $attachments;
|
| 274 |
return substr_replace($node->body, $attachments, $start, $end - $start + 8);
|
| 275 |
}
|
| 276 |
else {
|
| 277 |
return $node->body . $attachments;
|
| 278 |
}
|
| 279 |
|
| 280 |
}
|