summaryrefslogtreecommitdiffstats
path: root/download/index.php
blob: 5375cce17f70eaa450853da318e43a460ac15a68 (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
<?php
/*

 Copyright (c) 2001 - 2006 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/*!
	@header Download Document
	@discussion Downloads a song to the user, if they have download permission.
	Special thanks to the Horde project for their Browser class that makes this so easy.
*/

require('../lib/init.php');
require(conf('prefix') . '/modules/horde/Browser.php');

$browser = new Browser();

/* If we are running a demo, quick while you still can! */
if (conf('demo_mode') || !$GLOBALS['user']->has_access('25') || !$GLOBALS['user']->prefs['download']) {
	debug_event('access_denied',"Download Access Denied, " . $GLOBALS['user']->username . " doesn't have sufficent rights",'3');
	access_denied();
}

/*
   If they are using access lists let's make sure
   that they have enough access to play this mojo
*/
if (conf('access_control')) {
        $access = new Access(0);
        if (!$access->check('50', $_SERVER['REMOTE_ADDR'])) {
                debug_event('access_denied', "Download Access Denied, " . $_SERVER['REMOTE_ADDR'] . " does not have download level",'3');
                access_denied();
        }
} // access_control is enabled

/* Check for a song id */
if (!$_REQUEST['song_id']) { 
	echo "Error: No Song found, download failed";
	debug_event('download','No Song found, download failed','2');
}

/* If we're got require_session check for a valid session */
if (conf('require_session')) { 
	if (!session_exists(scrub_in($_REQUEST['sid']))) { 
		die(_("Session Expired: please log in again at") . " " . conf('web_path') . "/login.php");
		debug_event('session_expired',"Download Access Denied: " . $GLOBALS['user']->username . "'s session has expired",'3');
	}
} // if require_session
	

/* If the request is to download it... why is this here? */
if ($_REQUEST['action'] == 'download') {
	$song = new Song($_REQUEST['song_id']);
	$song->format_song();
	$song->format_type();
	$song_name = str_replace('"'," ",$song->f_artist_full . " - " . $song->title . "." . $song->type);

	
	// Use Horde's Browser class to send the right headers for different browsers
	// Should get the mime-type from the song rather than hard-coding it.
	header("Content-Length: " . $song->size);
	$browser->downloadHeaders($song_name, $song->mime, false, $song->size);
	$fp = fopen($song->file, 'r');
	
	/* We need to check and see if throttling is enabled */
	$speed = intval(conf('throttle_download'));
	if ($speed > 0) { 
		while(!feof($fp)) {
			echo fread($fp, round($speed*1024));
			flush();
			sleep(1);
		}
	} // if limiting 
	/* Otherwise just pump it out as fast as you can */	
	else { 	
		fpassthru($fp);
	} // else no limit

	fclose($fp);

} // If they've requested a download

?>