|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Daniel Calviño Sánchez <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
8
|
|
|
* @author Thomas Müller <[email protected]> |
|
9
|
|
|
* @author Victor Dubiniuk <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
* |
|
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
15
|
|
|
* as published by the Free Software Foundation. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OC; |
|
28
|
|
|
|
|
29
|
|
|
use OC\Files\Filesystem; |
|
30
|
|
|
use OCP\Files\File; |
|
31
|
|
|
use OCP\Files\Folder; |
|
32
|
|
|
use OCP\Files\InvalidPathException; |
|
33
|
|
|
use OCP\Files\NotFoundException; |
|
34
|
|
|
use OCP\Files\NotPermittedException; |
|
35
|
|
|
use OCP\IRequest; |
|
36
|
|
|
use ownCloud\TarStreamer\TarStreamer; |
|
37
|
|
|
use ZipStreamer\ZipStreamer; |
|
38
|
|
|
|
|
39
|
|
|
class Streamer { |
|
40
|
|
|
// array of regexp. Matching user agents will get tar instead of zip |
|
41
|
|
|
private $preferTarFor = [ '/macintosh|mac os x/i' ]; |
|
42
|
|
|
|
|
43
|
|
|
// streamer instance |
|
44
|
|
|
private $streamerInstance; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Streamer constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param IRequest $request |
|
50
|
|
|
* @param int $size The size of the files in bytes |
|
51
|
|
|
* @param int $numberOfFiles The number of files (and directories) that will |
|
52
|
|
|
* be included in the streamed file |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(IRequest $request, int $size, int $numberOfFiles){ |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* zip32 constraints for a basic (without compression, volumes nor |
|
58
|
|
|
* encryption) zip file according to the Zip specification: |
|
59
|
|
|
* - No file size is larger than 4 bytes (file size < 4294967296); see |
|
60
|
|
|
* 4.4.9 uncompressed size |
|
61
|
|
|
* - The size of all files plus their local headers is not larger than |
|
62
|
|
|
* 4 bytes; see 4.4.16 relative offset of local header and 4.4.24 |
|
63
|
|
|
* offset of start of central directory with respect to the starting |
|
64
|
|
|
* disk number |
|
65
|
|
|
* - The total number of entries (files and directories) in the zip file |
|
66
|
|
|
* is not larger than 2 bytes (number of entries < 65536); see 4.4.22 |
|
67
|
|
|
* total number of entries in the central dir |
|
68
|
|
|
* - The size of the central directory is not larger than 4 bytes; see |
|
69
|
|
|
* 4.4.23 size of the central directory |
|
70
|
|
|
* |
|
71
|
|
|
* Due to all that, zip32 is used if the size is below 4GB and there are |
|
72
|
|
|
* less than 65536 files; the margin between 4*1000^3 and 4*1024^3 |
|
73
|
|
|
* should give enough room for the extra zip metadata. Technically, it |
|
74
|
|
|
* would still be possible to create an invalid zip32 file (for example, |
|
75
|
|
|
* a zip file from files smaller than 4GB with a central directory |
|
76
|
|
|
* larger than 4GiB), but it should not happen in the real world. |
|
77
|
|
|
*/ |
|
78
|
|
|
if ($size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) { |
|
79
|
|
|
$this->streamerInstance = new ZipStreamer(['zip64' => false]); |
|
80
|
|
|
} else if ($request->isUserAgent($this->preferTarFor)) { |
|
81
|
|
|
$this->streamerInstance = new TarStreamer(); |
|
82
|
|
|
} else { |
|
83
|
|
|
$this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Send HTTP headers |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
*/ |
|
91
|
|
|
public function sendHeaders($name){ |
|
92
|
|
|
$extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar'; |
|
93
|
|
|
$fullName = $name . $extension; |
|
94
|
|
|
$this->streamerInstance->sendHeaders($fullName); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Stream directory recursively |
|
99
|
|
|
* |
|
100
|
|
|
* @throws NotFoundException |
|
101
|
|
|
* @throws NotPermittedException |
|
102
|
|
|
* @throws InvalidPathException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function addDirRecursive(string $dir, string $internalDir = ''): void { |
|
105
|
|
|
$dirname = basename($dir); |
|
106
|
|
|
$rootDir = $internalDir . $dirname; |
|
107
|
|
|
if (!empty($rootDir)) { |
|
108
|
|
|
$this->streamerInstance->addEmptyDir($rootDir); |
|
109
|
|
|
} |
|
110
|
|
|
$internalDir .= $dirname . '/'; |
|
111
|
|
|
// prevent absolute dirs |
|
112
|
|
|
$internalDir = ltrim($internalDir, '/'); |
|
113
|
|
|
|
|
114
|
|
|
$userFolder = \OC::$server->getRootFolder()->get(Filesystem::getRoot()); |
|
115
|
|
|
/** @var Folder $dirNode */ |
|
116
|
|
|
$dirNode = $userFolder->get($dir); |
|
|
|
|
|
|
117
|
|
|
$files = $dirNode->getDirectoryListing(); |
|
118
|
|
|
|
|
119
|
|
|
foreach($files as $file) { |
|
120
|
|
|
if($file instanceof File) { |
|
121
|
|
|
try { |
|
122
|
|
|
$fh = $file->fopen('r'); |
|
123
|
|
|
} catch (NotPermittedException $e) { |
|
124
|
|
|
continue; |
|
125
|
|
|
} |
|
126
|
|
|
$this->addFileFromStream( |
|
127
|
|
|
$fh, |
|
|
|
|
|
|
128
|
|
|
$internalDir . $file->getName(), |
|
129
|
|
|
$file->getSize(), |
|
130
|
|
|
$file->getMTime() |
|
131
|
|
|
); |
|
132
|
|
|
fclose($fh); |
|
133
|
|
|
} elseif ($file instanceof Folder) { |
|
134
|
|
|
if($file->isReadable()) { |
|
135
|
|
|
$this->addDirRecursive($dir . '/' . $file->getName(), $internalDir); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Add a file to the archive at the specified location and file name. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $stream Stream to read data from |
|
145
|
|
|
* @param string $internalName Filepath and name to be used in the archive. |
|
146
|
|
|
* @param int $size Filesize |
|
147
|
|
|
* @param int|bool $time File mtime as int, or false |
|
148
|
|
|
* @return bool $success |
|
149
|
|
|
*/ |
|
150
|
|
|
public function addFileFromStream($stream, $internalName, $size, $time) { |
|
151
|
|
|
$options = []; |
|
152
|
|
|
if ($time) { |
|
153
|
|
|
$options = [ |
|
154
|
|
|
'timestamp' => $time |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ($this->streamerInstance instanceof ZipStreamer) { |
|
159
|
|
|
return $this->streamerInstance->addFileFromStream($stream, $internalName, $options); |
|
160
|
|
|
} else { |
|
161
|
|
|
return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Add an empty directory entry to the archive. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $dirName Directory Path and name to be added to the archive. |
|
169
|
|
|
* @return bool $success |
|
170
|
|
|
*/ |
|
171
|
|
|
public function addEmptyDir($dirName){ |
|
172
|
|
|
return $this->streamerInstance->addEmptyDir($dirName); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Close the archive. |
|
177
|
|
|
* A closed archive can no longer have new files added to it. After |
|
178
|
|
|
* closing, the file is completely written to the output stream. |
|
179
|
|
|
* @return bool $success |
|
180
|
|
|
*/ |
|
181
|
|
|
public function finalize(){ |
|
182
|
|
|
return $this->streamerInstance->finalize(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.