-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFind_Command.php
More file actions
376 lines (346 loc) · 10.3 KB
/
Find_Command.php
File metadata and controls
376 lines (346 loc) · 10.3 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
<?php
use WP_CLI\Utils;
use WP_CLI\Formatter;
class Find_Command {
/**
* Paths we can probably ignore recursion into.
*
* @var array<string>
*/
private $ignored_paths = [
// System directories
'__MACOSX/',
// Webserver directories
'cache/',
'caches/',
'logs/',
'debuglogs/',
'Maildir/',
'tmp/',
// Generic application directories
'configs/',
'config/',
'data/',
'uploads/',
'themes/',
'plugins/',
'modules/',
'assets/',
'thumbs/',
'thumb/',
'albums/',
'attachments/',
'js/',
'pdf/',
'releases/',
'filestore/',
// Backup directories
'backup/',
'backups/',
'mysql_backups/',
'updater_backup/',
// Other applications
'owncloud/',
// Dependency management
'node_modules/',
'bower_components/',
'vendor/',
'svn/',
// Directory for a common script kiddie hack
'coockies/',
// Already in a WordPress install
'wp-admin/',
'wp-content/',
];
/**
* Beginning of the recursion path.
*
* @var string
*/
private $base_path;
/**
* Whether or not to skip ignored paths.
*
* @var bool
*/
private $skip_ignored_paths = false;
/**
* Maximum folder depth to recurse into.
*
* @var integer|false
*/
private $max_depth = false;
/**
* Current folder recursion depth.
*
* @var integer
*/
private $current_depth = 0;
/**
* Whether or not to be verbose with informational output.
*
* @var bool
*/
private $verbose = false;
/**
* Start time for the script.
*
* @var float
*/
private $start_time;
/**
* Resolved alias paths
*
* @var array<string, string>
*/
private $resolved_aliases = [];
/**
* Found WordPress installations.
*
* @var array<string, array<string, mixed>>
*/
private $found_wp = [];
/**
* Find WordPress installations on the filesystem.
*
* Recursively iterates subdirectories of provided <path> to find and
* report WordPress installations. A WordPress installation is a wp-includes
* directory with a version.php file.
*
* Avoids recursing some known paths (e.g. /node_modules/, hidden sys dirs)
* to significantly improve performance.
*
* Indicates depth at which the WordPress installations was found, and its
* alias, if it has one.
*
* ## OPTIONS
*
* <path>
* : Path to search the subdirectories of.
*
* [--skip-ignored-paths]
* : Skip the paths that are ignored by default.
*
* [--include_ignored_paths=<paths>]
* : Include additional ignored paths as CSV (e.g. '/sys-backup/,/temp/').
*
* [--max_depth=<max-depth>]
* : Only recurse to a specified depth, inclusive.
*
* [--fields=<fields>]
* : Limit the output to specific row fields.
*
* [--field=<field>]
* : Output a specific field for each row.
*
* [--format=<format>]
* : Render output in a specific format.
* ---
* default: table
* options:
* - table
* - json
* - csv
* - yaml
* - count
* ---
*
* [--verbose]
* : Log useful information to STDOUT.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each installation:
*
* * version_path - Path to the version.php file.
* * version - WordPress version.
* * depth - Directory depth at which the installation was found.
* * alias - WP-CLI alias, if one is registered.
*
* These fields are optionally available:
*
* * wp_path - Path that can be passed to `--path=<path>` global parameter.
* * db_host - Host name for the database.
* * db_user - User name for the database.
* * db_name - Database name for the database.
*
* ## EXAMPLES
*
* # Find WordPress installations.
* $ wp find ./
* +--------------------------------------+---------------------+-------+--------+
* | version_path | version | depth | alias |
* +--------------------------------------+---------------------+-------+--------+
* | /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2 | @wpcli |
* +--------------------------------------+---------------------+-------+--------+
*
* @when before_wp_load
*/
public function __invoke( $args, $assoc_args ) {
list( $path ) = $args;
$this->base_path = (string) realpath( $path );
if ( ! $this->base_path ) {
WP_CLI::error( 'Invalid path specified.' );
}
$this->skip_ignored_paths = Utils\get_flag_value( $assoc_args, 'skip-ignored-paths' );
if ( ! empty( $assoc_args['include_ignored_paths'] ) ) {
$this->ignored_paths = array_merge( $this->ignored_paths, explode( ',', $assoc_args['include_ignored_paths'] ) );
}
$this->max_depth = Utils\get_flag_value( $assoc_args, 'max_depth', false );
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose' );
$aliases = WP_CLI::get_runner()->aliases;
foreach ( $aliases as $alias => $target ) {
if ( empty( $target['path'] ) ) {
continue;
}
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
}
$fields = [ 'version_path', 'version', 'depth', 'alias' ];
if ( ! empty( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
}
$this->start_time = microtime( true );
$this->log( "Searching for WordPress installations in '{$path}'" );
$this->recurse_directory( $this->base_path );
$this->log( "Finished search for WordPress installations in '{$path}'" );
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( $this->found_wp );
}
private function recurse_directory( $path ) {
// Assume this symlink will be traversed from its true direction
if ( is_link( $path ) ) {
return;
}
// Provide consistent trailing slashes to all paths
$path = rtrim( $path, '/' ) . '/';
// Don't recurse directories that probably don't have a WordPress installation.
if ( ! $this->skip_ignored_paths ) {
// Assume base path doesn't need comparison
$compared_path = (string) preg_replace( '#^' . preg_quote( $this->base_path, '#' ) . '#', '', $path );
// Ignore all hidden system directories
$bits = explode( '/', trim( $compared_path, '/' ) );
$current_dir = array_pop( $bits );
if ( $current_dir && '.' === $current_dir[0] ) {
$this->log( "Matched ignored path. Skipping recursion into '{$path}'" );
return;
}
foreach ( $this->ignored_paths as $ignored_path ) {
if ( false !== stripos( $compared_path, $ignored_path ) ) {
$this->log( "Matched ignored path. Skipping recursion into '{$path}'" );
return;
}
}
}
// This looks like a wp-includes directory, so check if it has a
// version.php file.
if ( DIRECTORY_SEPARATOR . 'wp-includes/' === substr( $path, -13 )
&& file_exists( $path . 'version.php' ) ) {
$version_path = $path . 'version.php';
$wp_path = substr( $path, 0, -13 );
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
$wp_path = rtrim( $wp_path, '/' ) . '/';
$this->found_wp[ $version_path ] = [
'version_path' => $version_path,
'version' => self::get_wp_version( $version_path ),
'wp_path' => rtrim( $wp_path, '/' ) . '/',
'depth' => $this->current_depth - 1,
'alias' => $alias,
'db_host' => '',
'db_name' => '',
'db_user' => '',
];
$config_path = self::get_wp_config_path( $wp_path );
if ( $config_path ) {
try {
$transformer = new WPConfigTransformer( $config_path );
foreach ( [ 'db_host', 'db_name', 'db_user' ] as $constant ) {
$value = (string) $transformer->get_value( 'constant', strtoupper( $constant ) );
// Clean up strings.
$first = substr( $value, 0, 1 );
$last = substr( $value, -1 );
$both = array_unique( [ $first, $last ] );
if ( in_array( $both, [ [ '"' ], [ "'" ] ], true ) ) {
$value = substr( $value, 1, -1 );
}
$this->found_wp[ $version_path ][ $constant ] = $value;
}
} catch ( Exception $exception ) {
$this->log( "Could not process the 'wp-config.php' transformation: {$exception->getMessage()}" );
}
}
$this->log( "Found WordPress installation at '{$version_path}'" );
return;
}
// Ensure we haven't exceeded our max recursion depth
if ( false !== $this->max_depth && $this->current_depth > $this->max_depth ) {
$this->log( "Exceeded max depth. Skipping recursion into '{$path}'" );
return;
}
// Check all files and directories of this path to recurse
// into subdirectories.
try {
$iterator = new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS );
} catch ( Exception $e ) {
$this->log( "Exception thrown '{$e->getMessage()}'. Skipping recursion into '{$path}'" );
return;
}
$this->log( "Recursing into '{$path}'" );
/**
* @var SplFileInfo $file_info
*/
foreach ( $iterator as $file_info ) {
if ( $file_info->isDir() ) {
++$this->current_depth;
$this->recurse_directory( $file_info->getPathname() );
--$this->current_depth;
}
}
}
/**
* Get the WordPress version for the installation, without executing the file.
*/
private static function get_wp_version( $path ) {
$contents = (string) file_get_contents( $path );
preg_match( '#\$wp_version\s?=\s?[\'"]([^\'"]+)[\'"]#', $contents, $matches );
return ! empty( $matches[1] ) ? $matches[1] : '';
}
/**
* Get the wp-config.php path for the installation.
*
* Adapted from WP_CLI\Utils\locate_wp_config()
*/
private static function get_wp_config_path( $installation_dir ) {
$path = false;
if ( file_exists( $installation_dir . 'wp-config.php' ) ) {
$path = $installation_dir . 'wp-config.php';
} elseif ( file_exists( $installation_dir . '../wp-config.php' ) && ! file_exists( $installation_dir . '/../wp-settings.php' ) ) {
$path = $installation_dir . '../wp-config.php';
}
if ( $path ) {
$path = realpath( $path );
}
return $path;
}
/**
* Log informational message to STDOUT depending on verbosity.
*/
private function log( $message ) {
if ( $this->verbose ) {
$elapsed_time = microtime( true ) - $this->start_time;
WP_CLI::log( sprintf( '[%s] %s', self::format_log_timestamp( $elapsed_time ), $message ) );
}
}
/**
* Format a log timestamp into something human-readable.
*
* @param int|float $s Log time in seconds
* @return string
*/
private static function format_log_timestamp( $s ) {
$h = floor( $s / 3600 );
$s -= $h * 3600;
$m = floor( $s / 60 );
$s -= $m * 60;
return $h . ':' . sprintf( '%02d', $m ) . ':' . sprintf( '%02d', $s );
}
}