aboutsummaryrefslogtreecommitdiffstats
path: root/mod/filestorage.php
blob: 3d63e6da3f5328f8cc6900fcc1002e5986705748 (plain) (blame)
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
<?php

function filestorage_content(&$a) {

	if(argc() > 1)
		$which = argv(1);
	else {
		notice( t('Requested profile is not available.') . EOL );
		$a->error = 404;
		return;
	}

	$r = q("select channel_id from channel where channel_address = '%s'",
		dbesc($which)
	);
	if($r) {
		$owner = intval($r[0]['channel_id']);
	}

	$observer = $a->get_observer();
	$ob_hash = (($observer) ? $observer['xchan_hash'] : '');

	$perms = get_all_perms($owner,$ob_hash);

	if(! $perms['view_storage']) {
		notice( t('Permission denied.') . EOL);
		return;
	}

	//	Since we have ACL'd files in the wild, but don't have ACL here yet, we 
	//	need to return for anyone other than the owner, despite the perms check for now.

	$is_owner = (((local_user()) && ($owner  == local_user())) ? true : false);
	if(! $is_owner) {
		info( t('Permission Denied.') . EOL );
		return;
	}

	// 	TODO This will also need to check for files on disk and delete them from there as well as the DB.

	if(argc() > 3 && argv(3) === 'delete') {
		if(! $perms['write_storage']) {
			notice( t('Permission denied.  VS.') . EOL);
			return;
		}

		$file = intval(argv(2));
		$r = q("delete from attach where id = %d and uid = %d limit 1",
			dbesc($file),
			intval($owner)
		);
		goaway(z_root() . '/filestorage' . $which);
	}	


	$r = q("select * from attach where uid = %d order by edited desc",
		intval($owner)
	);

	$files = null;

	if($r) {
		$files = array();
		foreach($r as $rr) {
			$files[$rr['id']][] = array(
				'id' => $rr['id'],
				'download' => $rr['hash'], 
				'title' => $rr['filename'], 
				'size' => $rr['filesize']
			);
		} 
	}

	$limit = service_class_fetch ($owner,'attach_upload_limit'); 
		$r = q("select sum(filesize) as total from attach where uid = %d ",
		intval($owner)
	);
	$used = $r[0]['total'];

	$url = z_root() . "/filestorage/" . $which; 
	return $o . replace_macros(get_markup_template("filestorage.tpl"), array(
		'$baseurl' => $url,
		'$download' => t('Download'),
		'$files' => $files,
		'$channel' => $which,
		'$delete' => t('Delete'),
		'$used' => $used,
		'$usedlabel' => t('Used: '),
		'$limit' => $limit,
		'$limitlabel' => t('Limit: '),
	));
    
}