-
Notifications
You must be signed in to change notification settings - Fork 7
/
download.php
119 lines (103 loc) · 3.27 KB
/
download.php
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
<?php
/**
* Functions for downloading files
*
* @package Ribcage
* @subpackage Downloads
**/
/**
* Outputs the file selected from database to user for download.
*
* @author Alex Andrews <alex@recordsonribs.com>
* @param string $release_slug The slug of the release to download.
* @param string $format The format of the release to download, flac|mp3|ogg
* @return void
*/
function download_release ($release_slug, $format) {
global $release;
if (empty ($release_slug)) {
return new WP_Error ('ribcage-no-release-to-download', __("You didn't specify a release to grab."));
}
$release = get_release_by_slug ($release_slug, FALSE, FALSE);
// If we don't know the release, then error nicely, not with a snarky SQL error.
// Remember this page is user viewed. Display a 404.
if (is_wp_error ($release)) {
return $release;
}
if ($format == 'mp3') {
$file = $release['release_mp3'];
}
elseif ($format == 'ogg') {
$file = $release['release_ogg'];
}
else if ($format == 'flac') {
$file = $release['release_flac'];
}
else {
return new WP_Error ('ribcage-incorrect-format', __("$format isn't a format I am aware of.<br />Hence it isn't a format you can download, sorry."));
}
ribcage_log();
ribcage_download($file);
}
/**
* Outputs an individual track for the user to download.
*
* @author Alex Andrews <alex@recordsonribs.com>
* @param string $track_slug The slug of the track that is to be downloaded.
* @param string $format The format of the release to download, flac|mp3|ogg
* @return void
*/
function download_track ($track_slug, $format) {
global $track;
if (empty ($track_slug)) {
return new WP_Error ('ribcage-no-track-to-download', __("You didn't specify a track to grab."));
}
$track = get_track_by_slug($track_slug);
if (is_wp_error ($release)) {
return $release;
}
if ($format == 'mp3') {
$file = $track['track_mp3'];
}
elseif ($format == 'ogg') {
$file = $track['track_ogg'];
}
elseif ($format == 'flac') {
$file = $track['track_flac'];
}
else {
return new WP_Error('ribcage-incorrect-format', __("$format isn't a format I am aware of.<br />Hence it isn't a format you can download."));
}
ribcage_log (TRUE);
ribcage_download($file);
}
/**
* Outputs a file of a given path to the user, setting the headers correctly so it pops up nicely.
*
* Borrowed a good deal of code from Lester 'GaMerZ' Chan's WP-DownloadManager (http://lesterchan.net/portfolio/programming.php)
*
* @author Alex Andrews <alex@recordsonribs.com>
* @param string $file File path of the file to download
*/
function ribcage_download ($file) {
// The full path is the site root plus whatever is in the database.
$path = ABSPATH.$file;
if (file_exists($path)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
@readfile($path);
}
else {
header('HTTP/1.0 404 Not Found');
die(__('File does not exist.', 'ribcage'));
}
return(0);
}
?>