summaryrefslogtreecommitdiffstats
path: root/download_file.module
blob: 2a99c9f69487b4c434f75662dae2e441475cb0c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
// $Id: download_file.module,v 1.1.6.5 2010/12/03 12:00:25 xmattx Exp $

/**
 * @file
 * Module to direct download files or images.
 *
 * This module is for Drupal 7.x only.
 *
 * Module by: Matthieu Moratille
 * Mailto: mmoratille@yahoo.fr
 */

/**
 * Implementation of hook_help().
 */
function download_file_help($path, $arg) {
  switch ($path) {
    case 'admin/help#download_file':
      $output = '';
      $output .= '<p>' . t('The download file module to direct download files or images.') . '</p>';
      $output .= '<p>' . t('The download file module <a href="@setting">setting page</a> allows you to modify the format of the link accessible.', array('@setting' => url('admin/settings/download-file'))) . '</p>';
      return $output;

    case 'admin/settings/download-file':
      return '<p>' . t('The fownload file module to direct download files or images. Use the setting below to customize the format of the link accessible.') . '</p>';
  }
}

/**
 * Implementation of hook_perm().
 */
function download_file_permission() {
  return array(
    'access direct download file' => array(
      'title' => t('Access direct download file')
    ),
    'administer download file' => array(
      'title' => t('Administer download file')
    )
  );
}

/**
 * Implementation of hook_menu().
 */
function download_file_menu() {
  $items['download/file/fid/%'] = array(
    'title' => 'Download file',
    'page callback' => 'download_file_direct_download',
    'page arguments' => array(3),
    'access arguments' => array('access direct download file')
  );
  $items['admin/config/media/download-file'] = array(
    'title' => 'Download file',
    'description' => 'Configure the format of the link accessible.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('download_file_settings'),
    'access arguments' => array('administer download file'),
    'weight' => 30,
    'file' => 'download_file.admin.inc'
  );

  return $items;
}

/**
 * Implementation of hook_theme().
 */
function download_file_theme() {
  $theme = array(
    'download_file_formatter_direct_download' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_formatter_direct_download_icon' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_formatter_direct_download_accessible' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_formatter_direct_download_accessible_icon' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_formatter_direct_download_thumbnail_image' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_direct_download_item' => array(
      'variables' => array('file' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_direct_download_icon_item' => array(
      'variables' => array('file' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_direct_download_item_accessible' => array(
      'variables' => array('file' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_direct_download_icon_item_accessible' => array(
      'variables' => array('file' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_direct_download_thumbnail_image' => array(
      'variables' => array('items' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_detail_accessible' => array(
      'variables' => array('file' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_extension_accessible' => array(
      'variables' => array('extension' => NULL),
      'file' => 'download_file.formatter.inc'
    ),
    'download_file_bytes_accessible' => array(
      'variables' => array('filesize' => NULL),
      'file' => 'download_file.formatter.inc'
    )
  );

  return $theme;
}

/**
 * Page who forces the download of the file by changing the header.
 *
 * @param $fid
 *   The filefield identifier.
 */
function download_file_direct_download($fid) {
  $fid = intval($fid);
  $file = file_load($fid);

  if (empty($fid) || empty($file) || !$file->status) {
    return drupal_access_denied();
  }

  $headers = array(
    'Content-Type' => 'force-download',
    'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
    'Content-Length' => $file->filesize,
    'Content-Transfer-Encoding' => 'binary',
    'Pragma' => 'no-cache',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',
    'Accept-Ranges' => 'bytes'
  );

  // Let other modules provide headers and controls access to the file.
  if (in_array(-1, $headers)) {
    return drupal_access_denied();
  }

  // Access denied for private files.
  if (strstr($file->uri, 'private')) {
    return drupal_access_denied();
  }

  if (count($headers)) {
    file_transfer($file->uri, $headers);
  }
}

/**
 * Get the URL to download file.
 *
 * @param $fid
 *   The filefield identifier.
 * @return
 *   An string containing the URL to direct download file.
 */
function download_file_url($fid) {
  return url('download/file/fid/' . $fid);
}

/**
 * Get the path to download file.
 *
 * @param $fid
 *   The filefield identifier.
 * @return
 *   An string containing the path to direct download file.
 */
function download_file_path($fid) {
  return 'download/file/fid/' . $fid;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function download_file_field_formatter_info() {
  $formatters = array(
    'direct_download' => array(
      'label' => t('Direct download file'),
      'field types' => array('file', 'image'),
      'description' => t('Displays a link to direct download the file.')
    ),
    'direct_download_icon' => array(
      'label' => t('Direct download file with the type icon'),
      'field types' => array('file', 'image'),
      'description' => t('Displays a link to direct download the file with the type icon.')
    ),
    'direct_download_accessible' => array(
      'label' => t('Direct download file accessible'),
      'field types' => array('file', 'image'),
      'description' => t('Displays a link accessible to direct download the file.')
    ),
    'direct_download_accessible_icon' => array(
      'label' => t('Direct download file accessible with the type icon'),
      'field types' => array('file', 'image'),
      'description' => t('Displays a link accessible to direct download the file with the type icon.')
    ),
    'direct_download_thumbnail_image' => array(
      'label' => t('Direct download with thumbnail image'),
      'field types' => array('image'),
      'description' => t('Displays a link with a thumbnail to direct download the image.'),
      //'settings'    => array('image_style' => '', 'image_link' => ''),
    )
  );

  return $formatters;
}

/**
 * Implementation of hook_field_formatter_view().
 */
function download_file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  if ($items) {
    switch ($display['type']) {
      case 'direct_download':
        $element[0] = array(
          '#theme' => 'download_file_formatter_direct_download',
          '#items' => $items
        );
        break;

      case 'direct_download_icon':
        $element[0] = array(
          '#theme' => 'download_file_formatter_direct_download_icon',
          '#items' => $items
        );
        break;

      case 'direct_download_accessible':
        $element[0] = array(
          '#theme' => 'download_file_formatter_direct_download_accessible',
          '#items' => $items
        );
        break;

      case 'direct_download_accessible_icon':
        $element[0] = array(
          '#theme' => 'download_file_formatter_direct_download_accessible_icon',
          '#items' => $items
        );
        break;
      case 'direct_download_thumbnail_image':
        $element[0] = array(
          '#theme' => 'download_file_formatter_direct_download_thumbnail_image',
          '#items' => $items
        );
        break;
    }
  }

  return $element;
}

/**
 * Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes.
 *
 * @param $bytes
 *   File size in bytes.
 * @param $precision
 *   The precision after the decimal.
 * @return
 *   An array containing the formatted size and the unit.
 */
function download_file_format_bytes($bytes, $precision) {
  $units = array(t('B'), t('KB'), t('MB'), t('GB'), t('TB'), t('PB'));
  $pow = floor(log($bytes) / log(1024));
  $output = sprintf('%.' . $precision . 'f ', ($bytes / pow(1024, floor($pow))));

  return array($output, $units[$pow]);
}

/**
 * Get the acronym of file extension.
 *
 * @param $file
 *   The file.
 * @return
 *   An array containing the formatted acronym of file extension.
 */
function download_file_format_extension($file) {
  $extension = pathinfo(strtoupper(check_plain($file->filename)), PATHINFO_EXTENSION);

  switch ($file->filemime) {
    // Word document types.
    case 'application/msword':
    case 'application/doc':
    case 'application/vnd.ms-word':
    case 'application/winword':
    case 'application/word':
    case 'application/vnd.ms-word.document.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.text':
    case 'application/vnd.oasis.opendocument.text-template':
    case 'application/vnd.oasis.opendocument.text-master':
    case 'application/vnd.oasis.opendocument.text-web':
    case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
    case 'application/vnd.wordperfect':
      return array(
        'definition' => 'DOCument',
        'name' => 'DOC'
      );

    // Spreadsheet document types.
    case 'application/vnd.ms-excel':
    case 'application/xls':
    case 'application/msexcel':
    case 'application/vnd.ms-excel.sheet.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.spreadsheet':
    case 'application/vnd.oasis.opendocument.spreadsheet-template':
    case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
      return array(
        'definition' => 'eXceL Spreadsheet',
        'name' => 'XLS'
      );

    // Presentation document types.
    case 'application/vnd.ms-powerpoint':
    case 'application/powerpoint':
    case 'application/mspowerpoint':
    case 'application/ms-powerpoint':
    case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.presentation':
    case 'application/vnd.oasis.opendocument.presentation-template':
    case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
      return array(
        'definition' => 'PowerPoinT',
        'name' => 'PPT'
      );

    // Portable document types.
    case 'application/pdf':
    case 'application/x-pdf':
    case 'application/x-bzpdf':
    case 'application/x-gzpdf':
    case 'applications/vnd.pdf':
    case 'text/pdf':
    case 'text/x-pdf':
      return array(
        'definition' => 'Portable Document Format',
        'name' => 'PDF'
      );

    // ZIP compressed archive types.
    case 'application/zip':
    case 'application/gzip':
    case 'application/gzip-compressed':
    case 'application/x-zip':
    case 'application/x-zip-compressed':
    case 'application/x-bzip':
    case 'application/x-compress':
    case 'application/x-compressed':
    case 'application/x-gzip':
    case 'application/x-tgz':
      return array(
        'definition' => 'Archive',
        'name' => 'ZIP'
      );

    // RAR compressed archive types.
    case 'application/tar':
    case 'application/x-rar':
    case 'application/x-bzip-compressed-tar':
    case 'application/x-tar':
    case 'application/x-gtar':
    case 'application/x-tarz':
      return array(
        'definition' => 'Roshal ARchive',
        'name' => 'RAR'
      );

    // 7z compressed archive types.
    case 'application/x-7z-compressed':
      return array(
        'definition' => '7-Zip Archive',
        'name' => '7Z'
      );

    // Text file types.
    case 'application/txt':
    case 'text/plain':
      return array(
        'definition' => 'TeXTe',
        'name' => 'TXT'
      );

    // CSV aliases.
    case 'application/csv':
    case 'text/csv':
    case 'text/comma-separated-values':
      return array(
        'definition' => 'Comma-Separated Values',
        'name' => 'CSV'
      );

    // HTML aliases.
    case 'application/xhtml+xml':
    case 'text/html':
      return array(
        'definition' => 'HyperText Markup Language',
        'name' => 'HTML'
      );

    // XML aliases.
    case 'application/xml':
    case 'text/xml':
      return array(
        'definition' => 'eXtensible Markup Language',
        'name' => 'XML'
      );

    // RSS aliases.
    case 'application/rss+xml':
      return array(
        'definition' => 'Really Simple Syndication',
        'name' => 'RSS'
      );

    // JPEG images.
    case 'image/jpeg':
    case 'image/jpg':
      return array(
        'definition' => 'Joint Photographic Experts Group',
        'name' => 'JPEG'
      );

    // PNG images.
    case 'application/png':
    case 'image/png':
      return array(
        'definition' => 'Portable Network Graphics',
        'name' => 'PNG'
      );

    // GIF images.
    case 'image/gif':
      return array(
        'definition' => 'Graphics Interchange Format',
        'name' => 'GIF'
      );

    // Executable types.
    case 'application/exe':
    case 'application/dos-exe':
    case 'application/x-macbinary':
    case 'application/x-ms-dos-executable':
    case 'application/x-pef-executable':
      return array(
        'definition' => 'Executable',
        'name' => 'EXE'
      );

    // Audio Video Interleave types.
    case 'video/vnd.avi':
    case 'video/avi':
    case 'video/msvideo':
    case 'video/x-msvideo':
      return array(
        'definition' => 'Audio Video Interleave',
        'name' => 'AVI'
      );

    // FLash Video types.
    case 'video/x-flv':
    case 'video/mp4':
    case 'video/x-m4v':
      return array(
        'definition' => 'FLash Video',
        'name' => 'FLV'
      );

    // QuickTime movie types.
    case 'video/quicktime':
    case 'video/x-quicktime':
      return array(
        'definition' => 'QuickTime MOVie',
        'name' => 'MOV'
      );

    // Windows audio types.
    case 'audio/vnd.wave':
    case 'audio/wav':
    case 'audio/wave':
    case 'audio/x-wav':
      return array(
        'definition' => 'WAVeform Audio File Format',
        'name' => 'WAV'
      );

    // MPEG audio types.
    case 'audio/mpeg':
    case 'audio/x-mpeg':
    case 'audio/mp3':
    case 'audio/x-mp3':
    case 'audio/mpeg3':
    case 'audio/x-mpeg3':
    case 'audio/MPA':
    case 'audio/mpa-robust':
      return array(
        'definition' => 'MPEG-1 Audio Layer 3',
        'name' => 'MP3'
      );

    default:
      return $extension;
  }
}