aboutsummaryrefslogtreecommitdiffstats
path: root/mod/filestorage.php
blob: 1063de2561f6bcf7be4ff4c1eed07bf6c908829f (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
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
<?php

require_once('include/attach.php');

function filestorage_post(&$a) {

	$channel_id = ((x($_POST,'uid')) ? intval($_POST['uid']) : 0);

	if((! $channel_id) || (! local_user()) || ($channel_id != local_user())) {
		notice( t('Permission denied.') . EOL);
		return;
	}

	$recurse = ((x($_POST,'recurse')) ? intval($_POST['recurse']) : 0);
	$resource = ((x($_POST,'filehash')) ? notags($_POST['filehash']) : '');

	if(! $resource) {
		notice(t('Item not found.') . EOL);
		return;
	}

	$str_group_allow   = perms2str($_REQUEST['group_allow']);
	$str_contact_allow = perms2str($_REQUEST['contact_allow']);
	$str_group_deny    = perms2str($_REQUEST['group_deny']);
	$str_contact_deny  = perms2str($_REQUEST['contact_deny']);
 
	attach_change_permissions($channel_id,$resource,$str_contact_allow,$str_group_allow,$str_contact_deny,$str_group_deny,$recurse = false);

}





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.') . 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);
	}	


	if(argc() > 3 && argv(3) === 'edit') {
		require_once('include/acl_selectors.php');
		if(! $perms['write_storage']) {
			notice( t('Permission denied.') . EOL);
			return;
		}
		$file = intval(argv(2));

		$r = q("select id, folder, filename, flags, hash, allow_cid, allow_gid, deny_cid, deny_gid from attach where id = %d and uid = %d limit 1",
			intval($file),
			intval($owner)
		);

		$f = $r[0];

		$channel = $a->get_channel();


		$aclselect_e = populate_acl($f);
		$is_a_dir = (($f['flags'] & ATTACH_FLAG_DIR) ? true : false);


		$o = replace_macros(get_markup_template('attach_edit.tpl'), array(
			'$header' => t('Edit file permissions'),
			'$file' => $f,
			'$uid' => $channel['channel_id'],
			'$channelnick' => $channel['channel_address'],
			'$permissions' => t('Permissions'),
			'$aclselect' => $aclselect_e,
			'$recurse' => t('Include all files and sub folders'),
			'$backlink' => t('Return to file list'),
			'$isadir' => $is_a_dir,
			'$submit' => t('Submit')

		));

		return $o;
	}	

	$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'],
				'rev' => $rr['revision']

			);
		} 
	}

	$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,
		'$edit' => t('Edit Permissions'),
		'$delete' => t('Delete'),
		'$used' => $used,
		'$usedlabel' => t('Used: '),
		'$limit' => $limit,
		'$limitlabel' => t('Limit: '),
	));
    
}