forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractor.php
More file actions
381 lines (333 loc) · 9.8 KB
/
Extractor.php
File metadata and controls
381 lines (333 loc) · 9.8 KB
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
<?php
namespace WP_CLI;
use DirectoryIterator;
use Exception;
use PharData;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use WP_CLI;
use ZipArchive;
/**
* Extract a provided archive file.
*/
class Extractor {
/**
* Extract the archive file to a specific destination.
*
* @param string $dest
*/
public static function extract( $tarball_or_zip, $dest ) {
if ( preg_match( '/\.zip$/', $tarball_or_zip ) ) {
return self::extract_zip( $tarball_or_zip, $dest );
}
if ( preg_match( '/\.tar\.gz$/', $tarball_or_zip ) ) {
return self::extract_tarball( $tarball_or_zip, $dest );
}
throw new Exception( "Extraction only supported for '.zip' and '.tar.gz' file types." );
}
/**
* Extract a ZIP file to a specific destination.
*
* @param string $zipfile
* @param string $dest
*/
private static function extract_zip( $zipfile, $dest ) {
if ( ! class_exists( 'ZipArchive' ) ) {
throw new Exception( 'Extracting a zip file requires ZipArchive.' );
}
// Ensure the destination folder exists or can be created.
if ( ! self::ensure_dir_exists( $dest ) ) {
throw new Exception( "Could not create folder '{$dest}'." );
}
if ( ! file( $zipfile )
|| ! is_readable( $zipfile )
|| filesize( $zipfile ) <= 0 ) {
throw new Exception( "Invalid zip file '{$zipfile}'." );
}
$zip = new ZipArchive();
$res = $zip->open( $zipfile );
if ( true === $res ) {
$name = Utils\basename( $zipfile );
$tempdir = Utils\get_temp_dir()
. uniqid( 'wp-cli-extract-zipfile-', true )
. "-{$name}";
$zip->extractTo( $tempdir );
$zip->close();
self::copy_overwrite_files(
self::get_first_subfolder( $tempdir ),
$dest
);
self::rmdir( $tempdir );
} else {
throw new Exception(
sprintf(
"ZipArchive failed to unzip '%s': %s.",
$zipfile,
self::zip_error_msg( $res )
)
);
}
}
/**
* Extract a tarball to a specific destination.
*
* @param string $tarball
* @param string $dest
*/
private static function extract_tarball( $tarball, $dest ) {
// Ensure the destination folder exists or can be created.
if ( ! self::ensure_dir_exists( $dest ) ) {
throw new Exception( "Could not create folder '{$dest}'." );
}
if ( class_exists( 'PharData' ) ) {
try {
$phar = new PharData( $tarball );
$name = Utils\basename( $tarball );
$tempdir = Utils\get_temp_dir()
. uniqid( 'wp-cli-extract-tarball-', true )
. "-{$name}";
$phar->extractTo( $tempdir );
self::copy_overwrite_files(
self::get_first_subfolder( $tempdir ),
$dest
);
self::rmdir( $tempdir );
return;
} catch ( Exception $e ) {
WP_CLI::warning(
"PharData failed, falling back to 'tar xz' ("
. $e->getMessage() . ')'
);
// Fall through to trying `tar xz` below.
}
}
// Ensure relative paths cannot be misinterpreted as hostnames.
// Prepending `./` will force tar to interpret it as a filesystem path.
if ( self::path_is_relative( $tarball ) ) {
$tarball = "./{$tarball}";
}
if ( ! file( $tarball )
|| ! is_readable( $tarball )
|| filesize( $tarball ) <= 0 ) {
throw new Exception( "Invalid zip file '{$tarball}'." );
}
// Note: directory must exist for tar --directory to work.
$cmd = Utils\esc_cmd(
'tar xz --strip-components=1 --directory=%s -f %s',
$dest,
$tarball
);
$process_run = WP_CLI::launch(
$cmd,
false, /*exit_on_error*/
true /*return_detailed*/
);
if ( 0 !== $process_run->return_code ) {
throw new Exception(
sprintf(
'Failed to execute `%s`: %s.',
$cmd,
self::tar_error_msg( $process_run )
)
);
}
}
/**
* Copy files from source directory to destination directory. Source
* directory must exist.
*
* @param string $source
* @param string $dest
*/
public static function copy_overwrite_files( $source, $dest ) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$source,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::SELF_FIRST
);
$error = 0;
if ( ! is_dir( $dest ) ) {
mkdir( $dest, 0777, true );
}
foreach ( $iterator as $item ) {
$dest_path = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
if ( $item->isDir() ) {
if ( ! is_dir( $dest_path ) ) {
mkdir( $dest_path );
}
} elseif ( file_exists( $dest_path ) && is_writable( $dest_path ) ) {
copy( $item, $dest_path );
} elseif ( ! file_exists( $dest_path ) ) {
copy( $item, $dest_path );
} else {
$error = 1;
WP_CLI::warning( "Unable to copy '" . $iterator->getSubPathName() . "' to current directory." );
}
}
if ( $error ) {
throw new Exception( 'There was an error overwriting existing files.' );
}
}
/**
* Delete all files and directories recursively from directory. Directory
* must exist.
*
* @param string $dir
*/
public static function rmdir( $dir ) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ( $files as $fileinfo ) {
$todo = $fileinfo->isDir() ? 'rmdir' : 'unlink';
$path = $fileinfo->getRealPath();
if ( 0 !== strpos( $path, $fileinfo->getRealPath() ) ) {
WP_CLI::warning(
"Temporary file or folder to be removed was found outside of temporary folder, aborting removal: '{$path}'"
);
}
$todo( $path );
}
rmdir( $dir );
}
/**
* Return formatted ZipArchive error message from error code.
*
* @param int $error_code
* @return string|int The error message corresponding to the specified
* code, if found; Other wise the same error code,
* unmodified.
*/
public static function zip_error_msg( $error_code ) {
// From https://github.com/php/php-src/blob/php-5.3.0/ext/zip/php_zip.c#L2623-L2646.
static $zip_err_msgs = [
ZipArchive::ER_OK => 'No error',
ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported',
ZipArchive::ER_RENAME => 'Renaming temporary file failed',
ZipArchive::ER_CLOSE => 'Closing zip archive failed',
ZipArchive::ER_SEEK => 'Seek error',
ZipArchive::ER_READ => 'Read error',
ZipArchive::ER_WRITE => 'Write error',
ZipArchive::ER_CRC => 'CRC error',
ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed',
ZipArchive::ER_NOENT => 'No such file',
ZipArchive::ER_EXISTS => 'File already exists',
ZipArchive::ER_OPEN => 'Can\'t open file',
ZipArchive::ER_TMPOPEN => 'Failure to create temporary file',
ZipArchive::ER_ZLIB => 'Zlib error',
ZipArchive::ER_MEMORY => 'Malloc failure',
ZipArchive::ER_CHANGED => 'Entry has been changed',
ZipArchive::ER_COMPNOTSUPP => 'Compression method not supported',
ZipArchive::ER_EOF => 'Premature EOF',
ZipArchive::ER_INVAL => 'Invalid argument',
ZipArchive::ER_NOZIP => 'Not a zip archive',
ZipArchive::ER_INTERNAL => 'Internal error',
ZipArchive::ER_INCONS => 'Zip archive inconsistent',
ZipArchive::ER_REMOVE => 'Can\'t remove file',
ZipArchive::ER_DELETED => 'Entry has been deleted',
];
if ( isset( $zip_err_msgs[ $error_code ] ) ) {
return sprintf(
'%s (%d)',
$zip_err_msgs[ $error_code ],
$error_code
);
}
return $error_code;
}
/**
* Return formatted error message from ProcessRun of tar command.
*
* @param Processrun $process_run
* @return string|int The error message of the process, if available;
* otherwise the return code.
*/
public static function tar_error_msg( $process_run ) {
$stderr = trim( $process_run->stderr );
$nl_pos = strpos( $stderr, "\n" );
if ( false !== $nl_pos ) {
$stderr = trim( substr( $stderr, 0, $nl_pos ) );
}
if ( $stderr ) {
return sprintf( '%s (%d)', $stderr, $process_run->return_code );
}
return $process_run->return_code;
}
/**
* Return the first subfolder within a given path.
*
* Falls back to the provided path if no subfolder was detected.
*
* @param string $path Path to find the first subfolder in.
* @return string First subfolder, or same as $path if none found.
*/
private static function get_first_subfolder( $path ) {
$iterator = new DirectoryIterator( $path );
foreach ( $iterator as $fileinfo ) {
if ( $fileinfo->isDir() && ! $fileinfo->isDot() ) {
return "{$path}/{$fileinfo->getFilename()}";
}
}
return $path;
}
/**
* Ensure directory exists.
*
* @param string $dir Directory to ensure the existence of.
* @return bool Whether the existence could be asserted.
*/
private static function ensure_dir_exists( $dir ) {
if ( ! is_dir( $dir ) ) {
if ( ! @mkdir( $dir, 0777, true ) ) {
$error = error_get_last();
WP_CLI::warning(
sprintf(
"Failed to create directory '%s': %s.",
$dir,
$error['message']
)
);
return false;
}
}
return true;
}
/**
* Check whether a path is relative-
*
* @param string $path Path to check.
* @return bool Whether the path is relative.
*/
private static function path_is_relative( $path ) {
if ( '' === $path ) {
return true;
}
// Strip scheme.
$scheme_position = strpos( $path, '://' );
if ( false !== $scheme_position ) {
$path = substr( $path, $scheme_position + 3 );
}
// UNIX root "/" or "\" (Windows style).
if ( '/' === $path[0] || '\\' === $path[0] ) {
return false;
}
// Windows root.
if ( strlen( $path ) > 1 && ctype_alpha( $path[0] ) && ':' === $path[1] ) {
// Special case: only drive letter, like "C:".
if ( 2 === strlen( $path ) ) {
return false;
}
// Regular Windows path starting with drive letter, like "C:/ or "C:\".
if ( '/' === $path[2] || '\\' === $path[2] ) {
return false;
}
}
return true;
}
}