|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package File manager |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\file_manager\handlers\commands; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\helpers\Session; |
|
13
|
|
|
use gplcart\core\helpers\Zip; |
|
14
|
|
|
use gplcart\core\models\File; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Contains methods for "download" command |
|
18
|
|
|
*/ |
|
19
|
|
|
class Download extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ZIP class instance |
|
24
|
|
|
* @var \gplcart\core\helpers\Zip $zip |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $zip; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Session class instance |
|
30
|
|
|
* @var \gplcart\core\helpers\Session $session |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $session; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* File model class instance |
|
36
|
|
|
* @var \gplcart\core\models\File $file |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $file; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Download constructor. |
|
42
|
|
|
* @param File $file |
|
43
|
|
|
* @param Zip $zip |
|
44
|
|
|
* @param Session $session |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(File $file, Zip $zip, Session $session) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct(); |
|
49
|
|
|
|
|
50
|
|
|
$this->zip = $zip; |
|
51
|
|
|
$this->file = $file; |
|
52
|
|
|
$this->session = $session; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Whether the command is allowed for the file |
|
57
|
|
|
* @param \SplFileInfo $file |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function allowed($file) |
|
61
|
|
|
{ |
|
62
|
|
|
return in_array($file->getType(), array('file', 'dir')) && $file->isReadable(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns an array of data used to display the command |
|
67
|
|
|
* @param \SplFileInfo $file |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function view($file) |
|
71
|
|
|
{ |
|
72
|
|
|
return array( |
|
73
|
|
|
'file_manager|commands/download' => array( |
|
74
|
|
|
'path' => gplcart_path_relative($file->getRealPath()) |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Download file(s) |
|
80
|
|
|
* @param \gplcart\core\Controller $controller |
|
81
|
|
|
*/ |
|
82
|
|
|
public function submit($controller) |
|
83
|
|
|
{ |
|
84
|
|
|
set_time_limit(0); |
|
85
|
|
|
|
|
86
|
|
|
// Download method calls exit() so clean session here |
|
87
|
|
|
$this->session->delete('file_manager_selected'); |
|
88
|
|
|
|
|
89
|
|
|
$destination = $this->file->getTempFile(); |
|
90
|
|
|
$files = $controller->getSubmitted('files'); |
|
91
|
|
|
|
|
92
|
|
|
/* @var $file \SplFileInfo */ |
|
93
|
|
|
$file = reset($files); |
|
94
|
|
|
|
|
95
|
|
|
$path = $file->getRealPath(); |
|
96
|
|
|
$filename = $file->getBasename(); |
|
97
|
|
|
|
|
98
|
|
|
if ($file->isFile()) { |
|
99
|
|
|
$result = $this->zip->file($path, $destination); |
|
100
|
|
|
} else if ($file->isDir()) { |
|
101
|
|
|
$result = $this->zip->directory($path, $destination, $filename); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!empty($result)) { |
|
105
|
|
|
$controller->download($destination, "$filename.zip"); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|