-
Notifications
You must be signed in to change notification settings - Fork 40
/
module_download.inc.php
261 lines (217 loc) · 6.45 KB
/
module_download.inc.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
* module_download.inc.php
* Module for allowing to download arbitrary files that were uploaded
* by the user
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('html.inc.php');
require_once('modules.inc.php');
// module glue gets loaded on demand
require_once('util.inc.php');
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
function download_alter_render_early($args)
{
$elem = &$args['elem'];
$obj = $args['obj'];
if (!elem_has_class($elem, 'download')) {
return false;
}
if ($args['edit']) {
elem_attr($elem, 'title', 'this is '.$obj['name'].', original file name was '.$obj['download-file']);
} else {
elem_attr($elem, 'title', 'download file');
}
// get file extension
$a = expl('.', $obj['download-file']);
if (1 < count($a)) {
$v = elem('div');
elem_add_class($v, 'download-ext');
elem_val($v, htmlspecialchars(array_pop($a), ENT_NOQUOTES, 'UTF-8'));
elem_append($elem, $v);
}
return true;
}
function download_alter_render_late($args)
{
$elem = $args['elem'];
$html = &$args['html'];
$obj = $args['obj'];
if (!elem_has_class($elem, 'download')) {
return false;
}
if (!$args['edit'] && (!isset($obj['download-public']) || $obj['download-public'] != 'public')) {
// hide it in viewing mode if not public
$html = '';
} elseif (!$args['edit']) {
// otherwise add the css only on-demand in viewing mode
html_add_css(base_url().'modules/download/download.css');
}
return true;
}
function download_delete_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'download') {
return false;
}
load_modules('glue');
$a = expl('.', $obj['name']);
$ret = delete_upload(array('pagename'=>$a[0], 'file'=>$obj['download-file'], 'max_cnt'=>1));
if ($ret['#error']) {
log_error('error', 'upload_delete_object: delete_upload returned '.quot($ret['#error']));
}
}
function download_has_reference($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'download') {
return false;
}
// symlinks have their referenced files in a different page that's why
// they are not relevant here
if (@is_link(CONTENT_DIR.'/'.str_replace('.', '/', $obj['name']))) {
return false;
}
if ($obj['download-file'] != $args['file']) {
return false;
} else {
return true;
}
}
function download_render_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'download') {
return false;
}
$e = elem('div');
elem_attr($e, 'id', $obj['name']);
elem_add_class($e, 'download');
elem_add_class($e, 'object');
// hooks
invoke_hook_first('alter_render_early', 'download', array('obj'=>$obj, 'elem'=>&$e, 'edit'=>$args['edit']));
$html = elem_finalize($e);
invoke_hook_last('alter_render_late', 'download', array('obj'=>$obj, 'html'=>&$html, 'elem'=>$e, 'edit'=>$args['edit']));
if (!$args['edit']) {
// put link to file around the element
if (SHORT_URLS) {
$link = base_url().urlencode($obj['name']).'&download=1';
} else {
$link = base_url().'?'.urlencode($obj['name']).'&download=1';
}
$html = '<a href="'.htmlspecialchars($link, ENT_COMPAT, 'UTF-8').'">'."\n\t".str_replace("\n", "\n\t", $html)."\n".'</a>'."\n";
}
return $html;
}
function download_render_page_early($args)
{
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url().'modules/download/download-edit.min.js');
} else {
html_add_js(base_url().'modules/download/download-edit.js');
}
html_add_css(base_url().'modules/download/download.css');
}
}
function download_save_state($args)
{
$elem = $args['elem'];
$obj = $args['obj'];
if (get_first_item(elem_classes($elem)) != 'download') {
return false;
}
// make sure the type is set
$obj['type'] = 'download';
$obj['module'] = 'download';
// hook
invoke_hook('alter_save', array('obj'=>&$obj, 'elem'=>$elem));
// make width and height only be determined by the css
if (isset($obj['object-width'])) {
unset($obj['object-width']);
}
if (isset($obj['object-height'])) {
unset($obj['object-height']);
}
load_modules('glue');
$ret = save_object($obj);
if ($ret['#error']) {
log_msg('error', 'download_save_state: save_object returned '.quot($ret['#data']));
return false;
} else {
return true;
}
}
function download_serve_resource($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'download') {
return false;
}
$a = expl('.', $obj['name']);
// serve the resource only when it's public or we're logged in (i.e. editing)
if ((isset($obj['download-public']) && $obj['download-public'] == 'public') || is_auth()) {
serve_file(CONTENT_DIR.'/'.$a[0].'/shared/'.$obj['download-file'], $args['dl'], $obj['download-file-mime']);
} else if (!is_auth()) {
prompt_auth(true);
}
}
function download_snapshot_symlink($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'download') {
return false;
}
$dest_dir = CONTENT_DIR.'/'.get_first_item(expl('.', $obj['name'])).'/shared';
$src_file = CONTENT_DIR.'/'.get_first_item(expl('.', $args['origin'])).'/shared/'.$obj['download-file'];
if (($f = dir_has_same_file($dest_dir, $src_file)) !== false) {
$obj['download-file'] = $f;
} else {
// copy file
$dest_file = $dest_dir.'/'.unique_filename($dest_dir, $src_file);
$m = umask(0111);
if (!(@copy($src_file, $dest_file))) {
umask($m);
log_msg('error', 'download_snapshot_symlink: error copying referenced file '.quot($src_file).' to '.quot($dest_file));
return false;
}
umask($m);
$obj['download-file'] = basename($dest_file);
log_msg('info', 'download_snapshot_symlink: copied referenced file to '.quot($dest_file));
}
$ret = save_object($obj);
if ($ret['#error']) {
log_msg('error', 'download_snapshot_symlink: error saving object '.quot($obj['name']));
return false;
} else {
return true;
}
}
function download_upload_fallback($args)
{
// we handle everything
load_modules('glue');
$obj = create_object($args);
if ($obj['#error']) {
return false;
} else {
$obj = $obj['#data'];
}
$obj['type'] = 'download';
$obj['module'] = 'download';
$obj['download-file'] = $args['file'];
$obj['download-file-mime'] = $args['mime'];
save_object($obj);
$ret = render_object(array('name'=>$obj['name'], 'edit'=>true));
if ($ret['#error']) {
return false;
} else {
return $ret['#data'];
}
}