summaryrefslogtreecommitdiff
path: root/download_file.php
blob: 139f19bf2ed7af45480f3687dcc20ebee24d2577 (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
<?php
/**
 * @version      $Header$
 *
 * @author       xing  <xing@synapse.plus.com>
 * @package      treasury
 * @copyright    2003-2006 bitweaver
 * @license      LGPL {@link http://www.gnu.org/licenses/lgpl.html}
 **/

/**
 * Setup
 */
namespace Bitweaver\Liberty;

use Bitweaver\BitBase;
use Bitweaver\KernelTools;
require_once '../kernel/includes/setup_inc.php';

// fetch the attachment details
if( @!BitBase::verifyId( $_REQUEST['attachment_id'] ?? 0 ) || !( $attachment = LibertyMime::loadAttachment( $_REQUEST['attachment_id'], $_REQUEST ))) {
	$gBitSystem->fatalError( KernelTools::tra( "The Attachment ID given is not valid" ));
}

$gBitSmarty->assign( 'attachment', $attachment );

// first we need to check the permissions of the content the attachment belongs to since they inherit them
if( $gContent = LibertyBase::getLibertyObject( $attachment['content_id'] ) ) {
	$gContent->verifyViewPermission();
	$gBitSmarty->assign( 'gContent', $gContent );

	if( $download_function = $gLibertySystem->getPluginFunction( $attachment['attachment_plugin_guid'], 'download_function', 'mime' )) {
		if( $download_function( $attachment )) {
			LibertyMime::addDownloadHit( $attachment['attachment_id'] );
			die;
		}
			if( !empty( $attachment['errors'] )) {
				$msg = '';
				foreach( $attachment['errors'] as $error ) {
					$msg .= $error.'<br />';
				}
				$gBitSystem->fatalError( KernelTools::tra( $msg ));
			} else {
				$gBitSystem->fatalError( KernelTools::tra( 'There was an undetermined problem trying to prepare the file for download.' ));
			}

	} else {
		$gBitSystem->fatalError( KernelTools::tra( "No suitable download function found." ));
	}
} else {
	$gBitSystem->fatalError( KernelTools::tra( "Object not found." ), null, null, 404 );
}