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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
|
<?php declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
use Automattic\Jetpack\Constants; use Automattic\WooCommerce\Internal\Admin\Logging\Settings; use PclZip; use WC_Cache_Helper; use WP_Error;
/** * FileController class. */ class FileController { /** * The maximum number of rotations for a file before they start getting overwritten. * * This number should not go above 10, or it will cause issues with the glob patterns. * * const int */ private const MAX_FILE_ROTATIONS = 10;
/** * Default values for arguments for the get_files method. * * @const array */ public const DEFAULTS_GET_FILES = array( 'date_end' => 0, 'date_filter' => '', 'date_start' => 0, 'offset' => 0, 'order' => 'desc', 'orderby' => 'modified', 'per_page' => 20, 'source' => '', );
/** * Default values for arguments for the search_within_files method. * * @const array */ public const DEFAULTS_SEARCH_WITHIN_FILES = array( 'offset' => 0, 'per_page' => 50, );
/** * The maximum number of files that can be searched at one time. * * @const int */ public const SEARCH_MAX_FILES = 100;
/** * The maximum number of search results that can be returned at one time. * * @const int */ public const SEARCH_MAX_RESULTS = 200;
/** * The cache group name to use for caching operations. * * @const string */ private const CACHE_GROUP = 'log-files';
/** * A cache key for storing and retrieving the results of the last logs search. * * @const string */ private const SEARCH_CACHE_KEY = 'logs_previous_search';
/** * Get the file size limit that determines when to rotate a file. * * @return int */ private function get_file_size_limit(): int { $default = 5 * MB_IN_BYTES;
/** * Filter the threshold size of a log file at which point it will get rotated. * * @since 3.4.0 * * @param int $file_size_limit The file size limit in bytes. */ $file_size_limit = apply_filters( 'woocommerce_log_file_size_limit', $default );
if ( ! is_int( $file_size_limit ) || $file_size_limit < 1 ) { return $default; }
return $file_size_limit; }
/** * Write a log entry to the appropriate file, after rotating the file if necessary. * * @param string $source The source property of the log entry, which determines which file to write to. * @param string $text The contents of the log entry to add to a file. * @param int|null $time Optional. The time of the log entry as a Unix timestamp. Defaults to the current time. * * @return bool True if the contents were successfully written to the file. */ public function write_to_file( string $source, string $text, ?int $time = null ): bool { if ( is_null( $time ) ) { $time = time(); }
$file_id = File::generate_file_id( $source, null, $time ); $file = $this->get_file_by_id( $file_id );
if ( $file instanceof File && $file->get_file_size() >= $this->get_file_size_limit() ) { $rotated = $this->rotate_file( $file->get_file_id() );
if ( $rotated ) { $file = null; } else { return false; } }
if ( ! $file instanceof File ) { $new_path = Settings::get_log_directory() . $this->generate_filename( $source, $time ); $file = new File( $new_path ); }
return $file->write( $text ); }
/** * Generate the full name of a file based on source and date values. * * @param string $source The source property of a log entry, which determines the filename. * @param int $time The time of the log entry as a Unix timestamp. * * @return string */ private function generate_filename( string $source, int $time ): string { $file_id = File::generate_file_id( $source, null, $time ); $hash = File::generate_hash( $file_id );
return "$file_id-$hash.log"; }
/** * Get all the rotations of a file and increment them, so that they overwrite the previous file with that rotation. * * @param string $file_id A file ID (file basename without the hash). * * @return bool True if the file and all its rotations were successfully rotated. */ private function rotate_file( $file_id ): bool { $rotations = $this->get_file_rotations( $file_id );
if ( is_wp_error( $rotations ) || ! isset( $rotations['current'] ) ) { return false; }
$max_rotation_marker = self::MAX_FILE_ROTATIONS - 1;
// Don't rotate a file with the maximum rotation. unset( $rotations[ $max_rotation_marker ] );
$results = array(); // Rotate starting with oldest first and working backwards. for ( $i = $max_rotation_marker; $i >= 0; $i -- ) { if ( isset( $rotations[ $i ] ) ) { $results[] = $rotations[ $i ]->rotate(); } } $results[] = $rotations['current']->rotate();
return ! in_array( false, $results, true ); }
/** * Get an array of log files. * * @param array $args { * Optional. Arguments to filter and sort the files that are returned. * * @type int $date_end The end of the date range to filter by, as a Unix timestamp. * @type string $date_filter Filter files by one of the date props. 'created' or 'modified'. * @type int $date_start The beginning of the date range to filter by, as a Unix timestamp. * @type int $offset Omit this number of files from the beginning of the list. Works with $per_page to do pagination. * @type string $order The sort direction. 'asc' or 'desc'. Defaults to 'desc'. * @type string $orderby The property to sort the list by. 'created', 'modified', 'source', 'size'. Defaults to 'modified'. * @type int $per_page The number of files to include in the list. Works with $offset to do pagination. * @type string $source Only include files from this source. * } * @param bool $count_only Optional. True to return a total count of the files. * * @return File[]|int|WP_Error */ public function get_files( array $args = array(), bool $count_only = false ) { $args = wp_parse_args( $args, self::DEFAULTS_GET_FILES );
$pattern = $args['source'] . '*.log'; $paths = glob( Settings::get_log_directory() . $pattern );
if ( false === $paths ) { return new WP_Error( 'wc_log_directory_error', __( 'Could not access the log file directory.', 'woocommerce' ) ); }
$files = $this->convert_paths_to_objects( $paths );
if ( $args['date_filter'] && $args['date_start'] && $args['date_end'] ) { switch ( $args['date_filter'] ) { case 'created': $files = array_filter( $files, fn( $file ) => $file->get_created_timestamp() >= $args['date_start'] && $file->get_created_timestamp() <= $args['date_end'] ); break; case 'modified': $files = array_filter( $files, fn( $file ) => $file->get_modified_timestamp() >= $args['date_start'] && $file->get_modified_timestamp() <= $args['date_end'] ); break; } }
if ( true === $count_only ) { return count( $files ); }
$multi_sorter = function( $sort_sets, $order_sets ) { $comparison = 0;
while ( ! empty( $sort_sets ) ) { $set = array_shift( $sort_sets ); $order = array_shift( $order_sets );
if ( 'desc' === $order ) { $comparison = $set[1] <=> $set[0]; } else { $comparison = $set[0] <=> $set[1]; }
if ( 0 !== $comparison ) { break; } }
return $comparison; };
switch ( $args['orderby'] ) { case 'created': $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_sets = array( array( $a->get_created_timestamp(), $b->get_created_timestamp() ), array( $a->get_source(), $b->get_source() ), array( $a->get_rotation() || -1, $b->get_rotation() || -1 ), ); $order_sets = array( $args['order'], 'asc', 'asc' ); return $multi_sorter( $sort_sets, $order_sets ); }; break; case 'modified': $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_sets = array( array( $a->get_modified_timestamp(), $b->get_modified_timestamp() ), array( $a->get_source(), $b->get_source() ), array( $a->get_rotation() || -1, $b->get_rotation() || -1 ), ); $order_sets = array( $args['order'], 'asc', 'asc' ); return $multi_sorter( $sort_sets, $order_sets ); }; break; case 'source': $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_sets = array( array( $a->get_source(), $b->get_source() ), array( $a->get_created_timestamp(), $b->get_created_timestamp() ), array( $a->get_rotation() || -1, $b->get_rotation() || -1 ), ); $order_sets = array( $args['order'], 'desc', 'asc' ); return $multi_sorter( $sort_sets, $order_sets ); }; break; case 'size': $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) { $sort_sets = array( array( $a->get_file_size(), $b->get_file_size() ), array( $a->get_source(), $b->get_source() ), array( $a->get_rotation() || -1, $b->get_rotation() || -1 ), ); $order_sets = array( $args['order'], 'asc', 'asc' ); return $multi_sorter( $sort_sets, $order_sets ); }; break; }
usort( $files, $sort_callback );
return array_slice( $files, $args['offset'], $args['per_page'] ); }
/** * Get one or more File instances from an array of file IDs. * * @param array $file_ids An array of file IDs (file basename without the hash). * * @return File[] */ public function get_files_by_id( array $file_ids ): array { $log_directory = Settings::get_log_directory(); $paths = array();
foreach ( $file_ids as $file_id ) { // Look for the standard filename format first, which includes a hash. $glob = glob( $log_directory . $file_id . '-*.log' );
if ( ! $glob ) { $glob = glob( $log_directory . $file_id . '.log' ); }
if ( is_array( $glob ) ) { $paths = array_merge( $paths, $glob ); } }
$files = $this->convert_paths_to_objects( array_unique( $paths ) );
return $files; }
/** * Get a File instance from a file ID. * * @param string $file_id A file ID (file basename without the hash). * * @return File|WP_Error */ public function get_file_by_id( string $file_id ) { $result = $this->get_files_by_id( array( $file_id ) );
if ( count( $result ) < 1 ) { return new WP_Error( 'wc_log_file_error', esc_html__( 'This file does not exist.', 'woocommerce' ) ); }
if ( count( $result ) > 1 ) { return new WP_Error( 'wc_log_file_error', esc_html__( 'Multiple files match this ID.', 'woocommerce' ) ); }
return reset( $result ); }
/** * Get File instances for a given file ID and all of its related rotations. * * @param string $file_id A file ID (file basename without the hash). * * @return File[]|WP_Error An associative array where the rotation integer of the file is the key, and a "current" * key for the iteration of the file that hasn't been rotated (if it exists). */ public function get_file_rotations( string $file_id ) { $file = $this->get_file_by_id( $file_id );
if ( is_wp_error( $file ) ) { return $file; }
$current = array(); $rotations = array();
$source = $file->get_source(); $created = 0; if ( $file->has_standard_filename() ) { $created = $file->get_created_timestamp(); }
if ( is_null( $file->get_rotation() ) ) { $current['current'] = $file; } else { $current_file_id = File::generate_file_id( $source, null, $created ); $result = $this->get_file_by_id( $current_file_id ); if ( ! is_wp_error( $result ) ) { $current['current'] = $result; } }
$rotations_pattern = sprintf( '.[%s]', implode( '', range( 0, self::MAX_FILE_ROTATIONS - 1 ) ) );
$created_pattern = $created ? '-' . gmdate( 'Y-m-d', $created ) . '-' : '';
$rotation_pattern = Settings::get_log_directory() . $source . $rotations_pattern . $created_pattern . '*.log'; $rotation_paths = glob( $rotation_pattern ); $rotation_files = $this->convert_paths_to_objects( $rotation_paths ); foreach ( $rotation_files as $rotation_file ) { if ( $rotation_file->is_readable() ) { $rotations[ $rotation_file->get_rotation() ] = $rotation_file; } }
ksort( $rotations );
return array_merge( $current, $rotations ); }
/** * Helper method to get an array of File instances. * * @param array $paths An array of absolute file paths. * * @return File[] */ private function convert_paths_to_objects( array $paths ): array { $files = array_map( function( $path ) { $file = new File( $path ); return $file->is_readable() ? $file : null; }, $paths );
return array_filter( $files ); }
/** * Get a list of sources for existing log files. * * @return array|WP_Error */ public function get_file_sources() { $paths = glob( Settings::get_log_directory() . '*.log' ); if ( false === $paths ) { return new WP_Error( 'wc_log_directory_error', __( 'Could not access the log file directory.', 'woocommerce' ) ); }
$all_sources = array_map( function( $path ) { $file = new File( $path ); return $file->is_readable() ? $file->get_source() : null; }, $paths );
return array_unique( array_filter( $all_sources ) ); }
/** * Delete one or more files from the filesystem. * * @param array $file_ids An array of file IDs (file basename without the hash). * * @return int The number of files that were deleted. */ public function delete_files( array $file_ids ): int { $deleted = 0;
$files = $this->get_files_by_id( $file_ids ); foreach ( $files as $file ) { $result = $file->delete();
if ( true === $result ) { $deleted ++; } }
if ( $deleted > 0 ) { $this->invalidate_cache(); }
return $deleted; }
/** * Stream a single file to the browser without zipping it first. * * @param string $file_id A file ID (file basename without the hash). * * @return WP_Error|void Only returns something if there is an error. */ public function export_single_file( $file_id ) { $file = $this->get_file_by_id( $file_id );
if ( is_wp_error( $file ) ) { return $file; }
$file_name = $file->get_file_id() . '.log'; $exporter = new FileExporter( $file->get_path(), $file_name );
return $exporter->emit_file(); }
/** * Create a zip archive of log files and stream it to the browser. * * @param array $file_ids An array of file IDs (file basename without the hash). * * @return WP_Error|void Only returns something if there is an error. */ public function export_multiple_files( array $file_ids ) { $files = $this->get_files_by_id( $file_ids );
if ( count( $files ) < 1 ) { return new WP_Error( 'wc_logs_invalid_file', __( 'Could not access the specified files.', 'woocommerce' ) ); }
$temp_dir = get_temp_dir();
if ( ! is_dir( $temp_dir ) || ! wp_is_writable( $temp_dir ) ) { return new WP_Error( 'wc_logs_invalid_directory', __( 'Could not write to the temp directory. Try downloading files one at a time instead.', 'woocommerce' ) ); }
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
$path = trailingslashit( $temp_dir ) . 'woocommerce_logs_' . gmdate( 'Y-m-d_H-i-s' ) . '.zip'; $file_paths = array_map( fn( $file ) => $file->get_path(), $files ); $archive = new PclZip( $path );
$archive->create( $file_paths, PCLZIP_OPT_REMOVE_ALL_PATH );
$exporter = new FileExporter( $path );
return $exporter->emit_file(); }
/** * Search within a set of log files for a particular string. * * @param string $search The string to search for. * @param array $args Optional. Arguments for pagination of search results. * @param array $file_args Optional. Arguments to filter and sort the files that are returned. See get_files(). * @param bool $count_only Optional. True to return a total count of the matches. * * @return array|int|WP_Error When matches are found, each array item is an associative array that includes the * file ID, line number, and the matched string with HTML markup around the matched parts. */ public function search_within_files( string $search, array $args = array(), array $file_args = array(), bool $count_only = false ) { if ( '' === $search ) { return $count_only ? 0 : array(); }
$search = esc_html( $search );
$args = wp_parse_args( $args, self::DEFAULTS_SEARCH_WITHIN_FILES );
$file_args = array_merge( $file_args, array( 'offset' => 0, 'per_page' => self::SEARCH_MAX_FILES, ) );
$cache_key = WC_Cache_Helper::get_prefixed_key( self::SEARCH_CACHE_KEY, self::CACHE_GROUP ); $query = wp_json_encode( array( $search, $args, $file_args ) ); $cache = wp_cache_get( $cache_key ); $is_cached = isset( $cache['query'], $cache['results'] ) && $query === $cache['query'];
if ( true === $is_cached ) { $matched_lines = $cache['results']; } else { $files = $this->get_files( $file_args ); if ( is_wp_error( $files ) ) { return $files; }
// Max string size * SEARCH_MAX_RESULTS = ~1MB largest possible cache entry. $max_string_size = 5 * KB_IN_BYTES;
$matched_lines = array();
foreach ( $files as $file ) { $stream = $file->get_stream(); $line_number = 1;
while ( ! feof( $stream ) ) { $line = fgets( $stream, $max_string_size ); if ( ! is_string( $line ) ) { continue; }
$sanitized_line = esc_html( trim( $line ) ); if ( false !== stripos( $sanitized_line, $search ) ) { $matched_lines[] = array( 'file_id' => $file->get_file_id(), 'line_number' => $line_number, 'line' => $sanitized_line, ); }
if ( count( $matched_lines ) >= self::SEARCH_MAX_RESULTS ) { $file->close_stream(); break 2; }
if ( false !== strstr( $line, PHP_EOL ) ) { $line_number ++; } }
$file->close_stream(); }
$to_cache = array( 'query' => $query, 'results' => $matched_lines, ); wp_cache_set( $cache_key, $to_cache, self::CACHE_GROUP, DAY_IN_SECONDS ); }
if ( true === $count_only ) { return count( $matched_lines ); }
return array_slice( $matched_lines, $args['offset'], $args['per_page'] ); }
/** * Calculate the size, in bytes, of the log directory. * * @return int */ public function get_log_directory_size(): int { $bytes = 0; $path = realpath( Settings::get_log_directory( false ) );
if ( wp_is_writable( $path ) ) { $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path, \FilesystemIterator::SKIP_DOTS ), \RecursiveIteratorIterator::CATCH_GET_CHILD );
foreach ( $iterator as $file ) { $bytes += $file->getSize(); } }
return $bytes; }
/** * Invalidate the cache group related to log file data. * * @return bool True on successfully invalidating the cache. */ public function invalidate_cache(): bool { return WC_Cache_Helper::invalidate_cache_group( self::CACHE_GROUP ); } }
|