This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipcart.module
390 lines (361 loc) · 11.5 KB
/
zipcart.module
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* @file
* Drupal module for downloading groups of files.
*/
define('ZIPCART_PATH_ADD', 'zipcart/add');
define('ZIPCART_PATH_GET', 'zipcart/get');
/**
* Implements hook_menu().
*/
function zipcart_menu() {
$items['admin/config/media/zipcart'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('zipcart_settings_form'),
'title' => 'ZipCart',
'description' => 'Configure settings for ZipCart',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'zipcart.admin.inc',
);
$items[ZIPCART_PATH_ADD] = array(
'page callback' => 'zipcart_add_file_to_cart',
'page arguments' => array(FALSE),
'title' => 'Add file to Download Cart',
'type' => MENU_CALLBACK,
'access arguments' => array('access zipcart downloads'),
);
$items[ZIPCART_PATH_ADD . '/AJAX'] = array(
'page callback' => 'zipcart_add_file_to_cart',
'page arguments' => array(TRUE),
'title' => 'Add file to Download Cart',
'type' => MENU_CALLBACK,
'access arguments' => array('access zipcart downloads'),
);
$items[ZIPCART_PATH_GET] = array(
'page callback' => 'zipcart_get_zip',
'title' => 'Download Cart Files',
'type' => MENU_CALLBACK,
'access arguments' => array('access zipcart downloads'),
);
return $items;
}
/**
* Implements hook_theme().
*/
function zipcart_theme($existing, $type, $theme, $path) {
return array(
'zipcart_block_downloads' => array(
'template' => 'zipcart-block-downloads',
'variables' => array(
'count' => 0,
'files' => NULL,
),
),
'zipcart_download' => array(
'variables' => array(
'text' => 'Download',
'path' => NULL,
'options' => array(),
),
),
);
}
/**
* Implements hook_perm().
*/
function zipcart_permission() {
return array(
'access zipcart downloads' => array(
'title' => t('Access ZipCart Downloads'),
'description' => t('Allow users to access ZipCart Downloads'),
),
);
}
/**
* Implements hook_block_info().
*/
function zipcart_block_info() {
$blocks[0] = array(
'info' => t('ZipCart Downloads'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function zipcart_block_view($delta = '') {
if (user_access('access zipcart downloads')) {
switch ($delta) {
case '0':
$block = array(
'subject' => t('My Downloads'),
'content' => theme('zipcart_block_downloads'),
);
return $block;
}
}
}
/**
* Return block content for zipcart block.
*/
function zipcart_block_content() {
$count = (!empty($_SESSION['zipcart']['files'])) ? count($_SESSION['zipcart']['files']) : 0;
$files = (!empty($_SESSION['zipcart']['files'])) ? $_SESSION['zipcart']['files'] : NULL;
return theme('zipcart_block_downloads', $count, $files);
}
/**
* Implements hook_file_download().
*/
function zipcart_file_download($filepath) {
// dpm($filepath);
}
/**
* Implements hook_zipmethods().
*/
function zipcart_zipmethods() {
$methods['zip_builtin'] = array(
'title' => 'PHP Zip extension',
'callback' => '_zipcart_phpzip',
);
return $methods;
}
/**
* Return an array of zip methods.
*/
function _zipcart_get_available_methods() {
foreach (module_implements('zipmethods') as $module) {
$function = $module . '_zipmethods';
if ($methods = $function()) {
if (is_array($methods)) {
foreach ($methods as $key => $method) {
$avail_methods[$key] = $method;
}
}
}
}
return $avail_methods;
}
/**
* Add a file path to a user's $_SESSION.
*
* Check the user has permission to access the file here, warn if they do not.
*/
function zipcart_add_file_to_cart($ajax = FALSE) {
$path_parts = func_get_args();
// Remove AJAX flag.
array_shift($path_parts);
// We can't add the file 'sites/default/files/AJAX/myfile.txt' to our cart.
$path = implode('/', $path_parts);
$path = str_replace(ZIPCART_PATH_ADD, '', $path);
$path = trim($path, '/');
$filename = basename($path);
$success = FALSE;
$headers = module_invoke_all('file_download', $path);
if (!in_array(-1, $headers)) {
// OK, hook_file_download didn't object.
$files = array($path);
$files = module_invoke_all('filterzip', $files);
if (!empty($files)) {
$_SESSION['zipcart']['files'][] = $path;
$success = TRUE;
}
}
$_SESSION['zipcart']['files'] = array_unique($_SESSION['zipcart']['files']);
$result = array(
'cart' => $_SESSION['zipcart']['files'],
'result' => $success,
);
if ($ajax) {
drupal_json_output($result);
}
else {
if ($success) {
$targs = array(
'%filename' => check_plain($filename),
'!download' => l(t('Click here to download'), 'zipcart/get'),
);
drupal_set_message(t('The file %filename has been added to your cart. !download', $targs));
}
else {
drupal_set_message(t('The file !filename could not be added to your cart.', array('!filename' => check_plain($filename))));
}
drupal_goto();
}
}
/**
* Get the files as a zip.
*/
function zipcart_get_zip() {
if (empty($_SESSION['zipcart']['files'])) {
drupal_set_message(t('Sorry, there are no files queued for you to download.'));
drupal_goto();
}
else {
$files = $_SESSION['zipcart']['files'];
$files = module_invoke_all('filterzip', $files);
if (!empty($files)) {
$methods = _zipcart_get_available_methods();
$zipmethod = variable_get('zipcart_zip_method', 'zip_builtin');
if (isset($methods[$zipmethod])) {
$zipfunction = $methods[$zipmethod]['callback'];
if (!$zipfunction($files)) {
return 'Download failed.';
}
else {
drupal_set_message('Zip downloaded');
return TRUE;
}
}
}
}
}
/**
* Preprocess the list of files.
*
* Don't ever share these files:
* $excluded = array( 'sites/.*settings.*php' );
*
* Only ever include these files:
* $included = array( 'sites/files/.*' );
*
* Always include a certain file:
* $add = array( 'sites/default/files/README.txt' );
*/
function zipcart_filterzip($files) {
$directory = drupal_realpath(file_default_scheme() . '://');
foreach ($files as $i => &$file) {
if (strpos(drupal_realpath($file), $directory) !== 0) {
watchdog('zipcart', 'Forbidding download of !file',
array('!file' => $file), WATCHDOG_NOTICE);
unset($files[$i]);
}
// Reject external URIs.
$localpath = drupal_realpath($file);
if ($localpath) {
$file = $localpath;
}
else {
watchdog('zipcart', 'External file cannot be added to zip. !file',
array('!file' => $file), WATCHDOG_NOTICE);
unset($files[$i]);
}
}
return $files;
}
/**
* Invoke hook_zipcart_download_complete() when download completed.
*/
function _zipcart_download_complete($filename, $files) {
module_invoke_all('zipcart_download_complete', $filename, $files);
}
/**
* Zip callback: PHP zip extension.
*/
function _zipcart_phpzip($files) {
$zip = new ZipArchive();
// We build the file in $filename, but file_transfer will
// only deliver it via a registered Drupal stream.
$filename = drupal_tempnam(file_directory_temp(), 'zipcart');
$transfer_filename = 'temporary://' . basename($filename);
$zip_open = $zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if ($zip_open === TRUE) {
$zip->setArchiveComment('Generated by ZipCart for Drupal.');
// http://drupal.org/node/1234282 - duplicate filenames.
foreach ($files as $file) {
if (file_exists($file)) {
if ($zip->addFile($file, basename($file))) {
// drupal_set_message(t('!file added to Zip.', array('!file' => basename($file))));
}
else {
drupal_set_message(t('!file could not be added to Zip.', array('!file' => $file)), 'error');
watchdog('zipcart', 'Failed to add !file to Zip.', array('!file' => $file), WATCHDOG_ERROR);
}
}
else {
watchdog('zipcart', 'Failed to locate !file.', array('!file' => $file), WATCHDOG_ERROR);
drupal_set_message('!file not found.', array('!file' => $file), 'error');
}
}
if (!$zip->close()) {
watchdog('zipcart', 'Error saving file: !status_string', array('!status_string' => $zip->getStatusString()), WATCHDOG_ERROR);
}
$headers = module_invoke_all('file_download', $filename);
if (in_array(-1, $headers)) {
return drupal_access_denied();
}
$zip_filename = _zipcart_zip_filename();
$headers['Content-Type'] = file_get_mimetype($zip_filename);
$headers['Content-Disposition'] = 'attachment; filename="' . $zip_filename . '"';
// FIXME: it would be nice to wait until the file transfer completes before
// we clear the files out of the session. However, if we do that, the user
// can't add new files to their cart until the zip is downloaded. So we'll
// do this first.
$files = $_SESSION['zipcart']['files'];
$_SESSION['zipcart']['files'] = array();
drupal_register_shutdown_function('_zipcart_download_complete', $filename, $files);
if (!file_transfer($transfer_filename, $headers)) {
watchdog('zipcart', 'Unable to transfer file from !filename.', array('!filename' => check_plain($transfer_filename)), WATCHDOG_ERROR);
drupal_set_message(t('Unable to transfer file from !filename.', array('!filename' => check_plain($transfer_filename))), 'error');
}
}
else {
watchdog('zipcart', 'Unable to create file !filename.', array('!filename' => check_plain($filename)), WATCHDOG_ERROR);
drupal_set_message(t('Unable to create file !filename.', array('!filename' => check_plain($filename))), 'error');
drupal_access_denied();
}
}
/**
* Generate a suitable filename for the zip.
*/
function _zipcart_zip_filename() {
$filename = 'Files - ' . date('Y-m-d_Hi') . '.zip';
return $filename;
}
/**
* Obtain correct path to return user to after adding file to cart.
*/
function _zipcart_get_destination_alias() {
if (isset($_REQUEST['destination'])) {
$path = check_plain(drupal_get_path_alias($_REQUEST['destination']));
}
else {
// Use $_GET here to retrieve the original path in source form.
$path = isset($_GET['q']) ? check_plain(drupal_get_path_alias($_GET['q'])) : '';
}
return array(
'destination' => $path,
);
}
/**
* Theme function to generate a download link.
*/
function theme_zipcart_download($vars = array()) {
$text = !empty($vars['text']) ? $vars['text'] : '';
$path = !empty($vars['path']) ? $vars['path'] : '';
$options = !empty($vars['options']) ? $vars['options'] : array();
$default_options = array(
'attributes' => array(
'class' => 'zipcart',
),
'query' => _zipcart_get_destination_alias(),
);
$options = array_merge($default_options, $options);
return l($text, ZIPCART_PATH_ADD . '/' . $path, $options);
}
/**
* Implements hook_zipcart_download_complete().
*/
function zipcart_zipcart_download_complete($filename) {
drupal_unlink($filename);
}
/**
* Theme preprocess function.
*/
function zipcart_preprocess_zipcart_block_downloads(&$variables) {
drupal_add_js(drupal_get_path('module', 'zipcart') . '/zipcart.js');
drupal_add_js(array('zipcart' => array('path_add' => ZIPCART_PATH_ADD, 'path_add_ajax' => ZIPCART_PATH_ADD . '/AJAX')), 'setting');
$variables['count'] = (!empty($_SESSION['zipcart']['files'])) ? sizeof($_SESSION['zipcart']['files']) : 0;
$variables['files'] = (!empty($_SESSION['zipcart']['files'])) ? $_SESSION['zipcart']['files'] : NULL;
}