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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
<?php defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Class handling everything that is related to "custom folders optimization". * * @since 1.7 * @author Grégory Viguier */ class Imagify_Files_Scan {
/** * Class version. * * @var string * @since 1.7 * @author Grégory Viguier */ const VERSION = '1.1.1';
/** * Get files (optimizable by Imagify) recursively from a specific folder. * * @since 1.7 * @access public * @author Grégory Viguier * * @param string $folder An absolute path to a folder. * @return array|object An array of absolute paths. A WP_Error object on error. */ public static function get_files_from_folder( $folder ) { $filesystem = imagify_get_filesystem();
// Formate and validate the folder path. if ( ! is_string( $folder ) ) { return new WP_Error( 'invalid_folder', __( 'Invalid folder.', 'imagify' ) ); }
$folder = realpath( $folder );
if ( ! $folder ) { return new WP_Error( 'folder_not_exists', __( 'This folder does not exist.', 'imagify' ) ); }
if ( ! $filesystem->is_dir( $folder ) ) { return new WP_Error( 'not_a_folder', __( 'This file is not a folder.', 'imagify' ) ); }
if ( self::is_path_forbidden( trailingslashit( $folder ) ) ) { return new WP_Error( 'folder_forbidden', __( 'This folder is not allowed.', 'imagify' ) ); }
// Finally we made all our validations. if ( $filesystem->is_site_root( $folder ) ) { // For the site's root, we don't look in sub-folders. $dir = new DirectoryIterator( $folder ); $dir = new Imagify_Files_Iterator( $dir, false ); $images = array();
foreach ( new IteratorIterator( $dir ) as $file ) { $images[] = $file->getPathname(); }
return $images; }
/** * 4096 stands for FilesystemIterator::SKIP_DOTS, which was introduced in php 5.3.0. * 8192 stands for FilesystemIterator::UNIX_PATHS, which was introduced in php 5.3.0. */ $dir = new RecursiveDirectoryIterator( $folder, 4096 | 8192 ); $dir = new Imagify_Files_Recursive_Iterator( $dir ); $images = new RecursiveIteratorIterator( $dir ); $images = array_keys( iterator_to_array( $images ) );
return $images; }
/** ----------------------------------------------------------------------------------------- */ /** FORBIDDEN FOLDERS AND FILES ============================================================= */ /** ----------------------------------------------------------------------------------------- */
/** * Tell if a path is autorized. * When testing a folder, the path MUST have a trailing slash. * * @since 1.7.1 * @since 1.8 The path must have a trailing slash if for a folder. * @access public * @author Grégory Viguier * * @param string $file_path A file or folder absolute path. * @return bool */ public static function is_path_autorized( $file_path ) { return ! self::is_path_forbidden( $file_path ); }
/** * Tell if a path is forbidden. * When testing a folder, the path MUST have a trailing slash. * * @since 1.7 * @since 1.8 The path must have a trailing slash if for a folder. * @access public * @author Grégory Viguier * * @param string $file_path A file or folder absolute path. * @return bool */ public static function is_path_forbidden( $file_path ) { static $folders;
$filesystem = imagify_get_filesystem();
if ( self::is_filename_forbidden( $filesystem->file_name( $file_path ) ) ) { return true; }
if ( $filesystem->is_symlinked( $file_path ) ) { // Files outside the site's folder are forbidden. return true; }
if ( ! isset( $folders ) ) { $folders = self::get_forbidden_folders(); $folders = array_map( 'strtolower', $folders ); $folders = array_flip( $folders ); }
$file_path = self::normalize_path_for_comparison( $file_path );
if ( isset( $folders[ $file_path ] ) ) { return true; }
$delim = Imagify_Filesystem::PATTERN_DELIMITER;
foreach ( self::get_forbidden_folder_patterns() as $pattern ) { if ( preg_match( $delim . '^' . $pattern . $delim, $file_path ) ) { return true; } }
foreach ( $folders as $folder => $i ) { if ( strpos( $file_path, $folder ) === 0 ) { return true; } }
return false; }
/** * Get the list of folders where Imagify won't look for files to optimize. * * @since 1.7 * @access public * @author Grégory Viguier * * @return array A list of absolute paths. */ public static function get_forbidden_folders() { static $folders;
if ( isset( $folders ) ) { return $folders; }
$filesystem = imagify_get_filesystem(); $site_root = $filesystem->get_site_root(); $abspath = $filesystem->get_abspath(); $folders = array( // Server. $site_root . 'cgi-bin', // `cgi-bin` // WordPress. $abspath . 'wp-admin', // `wp-admin` $abspath . WPINC, // `wp-includes` WP_CONTENT_DIR . '/mu-plugins', // MU plugins. WP_CONTENT_DIR . '/upgrade', // Upgrade. // Plugins. WP_CONTENT_DIR . '/bps-backup', // BulletProof Security. self::get_ewww_tools_path(), // EWWW: /wp-content/ewww. WP_CONTENT_DIR . '/ngg', // NextGen Gallery. WP_CONTENT_DIR . '/ngg_styles', // NextGen Gallery. WP_CONTENT_DIR . '/w3tc-config', // W3 Total Cache. WP_CONTENT_DIR . '/wfcache', // WP Fastest Cache. WP_CONTENT_DIR . '/wp-rocket-config', // WP Rocket. Imagify_Custom_Folders::get_backup_dir_path(), // Imagify "Custom folders" backup: /imagify-backup. IMAGIFY_PATH, // Imagify plugin: /wp-content/plugins/imagify. self::get_shortpixel_path(), // ShortPixel: /wp-content/uploads/ShortpixelBackups. );
if ( ! is_multisite() ) { $uploads_dir = $filesystem->get_upload_basedir( true ); $ngg_galleries = self::get_ngg_galleries_path();
if ( $ngg_galleries ) { $folders[] = $ngg_galleries; // NextGen Gallery: /wp-content/gallery. }
$folders[] = $uploads_dir . 'formidable'; // Formidable Forms: /wp-content/uploads/formidable. $folders[] = get_imagify_backup_dir_path( true ); // Imagify Media Library backup: /wp-content/uploads/backup. $folders[] = self::get_wc_logs_path(); // WooCommerce Logs: /wp-content/uploads/wc-logs. $folders[] = $uploads_dir . 'woocommerce_uploads'; // WooCommerce uploads: /wp-content/uploads/woocommerce_uploads. }
$folders = array_map( array( $filesystem, 'normalize_dir_path' ), $folders );
/** * Add folders to the list of forbidden ones. * * @since 1.7 * @author Grégory Viguier * * @param array $added_folders List of absolute paths. * @param array $folders List of folders already forbidden. */ $added_folders = apply_filters( 'imagify_add_forbidden_folders', array(), $folders ); $added_folders = array_filter( (array) $added_folders ); $added_folders = array_filter( $added_folders, 'is_string' );
if ( ! $added_folders ) { return $folders; }
$added_folders = array_map( array( $filesystem, 'normalize_dir_path' ), $added_folders );
$folders = array_merge( $folders, $added_folders ); $folders = array_flip( array_flip( $folders ) );
return $folders; }
/** * Get the list of folder patterns where Imagify won't look for files to optimize. This is meant for paths that are dynamic. * `^` will be prepended to each pattern (aka, the pattern must match an absolute path). * Pattern delimiter is `Imagify_Filesystem::PATTERN_DELIMITER`. * Paths tested against these patterns are lower-cased. * * @since 1.7 * @access public * @author Grégory Viguier * * @return array A list of regex patterns. */ public static function get_forbidden_folder_patterns() { static $folders;
if ( isset( $folders ) ) { return $folders; }
$folders = array();
// Media Library: /wp\-content/uploads/(sites/\d+/)?\d{4}/\d{2}/. $folders[] = self::get_media_library_pattern();
if ( is_multisite() ) { /** * On multisite we can't exclude Imagify's library backup folders, or any other folder located in the uploads folders (created by other plugins): there are too many ways it can fail. * Only exception we're aware of so far is NextGen Gallery, because it provides a clear pattern to use. */ $ngg_galleries = self::get_ngg_galleries_multisite_pattern();
if ( $ngg_galleries ) { // NextGen Gallery: /wp\-content/uploads/sites/\d+/nggallery/. $folders[] = $ngg_galleries; } }
/** * Add folder patterns to the list of forbidden ones. * Don't forget to use `Imagify_Files_Scan::normalize_path_for_regex( $path )`! * * @since 1.7 * @author Grégory Viguier * * @param array $added_folders List of patterns. * @param array $folders List of patterns already forbidden. */ $added_folders = apply_filters( 'imagify_add_forbidden_folder_patterns', array(), $folders ); $added_folders = array_filter( (array) $added_folders ); $added_folders = array_filter( $added_folders, 'is_string' );
if ( ! $added_folders ) { return $folders; }
$folders = array_merge( $folders, $added_folders ); $folders = array_flip( array_flip( $folders ) );
return $folders; }
/** * Tell if a file/folder name is forbidden. * * @since 1.7 * @access public * @author Grégory Viguier * * @param string $file_name A file or folder name. * @return bool */ public static function is_filename_forbidden( $file_name ) { static $file_names;
if ( ! isset( $file_names ) ) { $file_names = array_flip( self::get_forbidden_file_names() ); }
return isset( $file_names[ strtolower( $file_name ) ] ); }
/** * Get the list of file names that Imagify won't optimize. * It can contain folder names. Names are case-lowered. * * @since 1.7 * @access public * @author Grégory Viguier * * @return array A list of file names */ public static function get_forbidden_file_names() { static $file_names;
if ( isset( $file_names ) ) { return $file_names; }
$file_names = array( '.', '..', '.DS_Store', '.git', '.svn', 'backup', 'backups', 'cache', 'lang', 'langs', 'languages', 'node_modules', 'Thumbs.db', ); $file_names = array_map( 'strtolower', $file_names );
/** * Add file names to the list of forbidden ones. * * @since 1.7 * @author Grégory Viguier * * @param array $added_file_names List of file names. * @param array $file_names List of file names already forbidden. */ $added_file_names = apply_filters( 'imagify_add_forbidden_file_names', array(), $file_names );
if ( ! $added_file_names || ! is_array( $added_file_names ) ) { return $file_names; }
$added_file_names = array_filter( $added_file_names, 'is_string' ); $added_file_names = array_map( 'strtolower', $added_file_names );
$file_names = array_merge( $file_names, $added_file_names ); $file_names = array_flip( array_flip( $file_names ) );
return $file_names; }
/** ----------------------------------------------------------------------------------------- */ /** PLACEHOLDERS ============================================================================ */ /** ----------------------------------------------------------------------------------------- */
/** * Add a placeholder to a path. * * @since 1.7 * @access public * @author Grégory Viguier * * @param string $file_path An absolute path. * @return string A "placeholdered" path. */ public static function add_placeholder( $file_path ) { $file_path = wp_normalize_path( $file_path ); $locations = self::get_placeholder_paths();
foreach ( $locations as $placeholder => $location_path ) { if ( strpos( $file_path, $location_path ) === 0 ) { return preg_replace( '@^' . preg_quote( $location_path, '@' ) . '@', $placeholder, $file_path ); } }
// Should not happen. return $file_path; }
/** * Change a path with a placeholder into a real path or URL. * * @since 1.7 * @access public * @author Grégory Viguier * * @param string $file_path A path with a placeholder. * @param string $type What to return: 'path' or 'url'. * @return string An absolute path or a URL. */ public static function remove_placeholder( $file_path, $type = 'path' ) { if ( 'path' === $type ) { $locations = self::get_placeholder_paths(); } else { $locations = self::get_placeholder_urls(); }
foreach ( $locations as $placeholder => $location_path ) { if ( strpos( $file_path, $placeholder ) === 0 ) { return preg_replace( '@^' . preg_quote( $placeholder, '@' ) . '@', $location_path, $file_path ); } }
// Should not happen. return $file_path; }
/** * Get array of pairs of placeholder => corresponding path. * * @since 1.7 * @access public * @author Grégory Viguier * * @return array */ public static function get_placeholder_paths() { static $replacements;
if ( isset( $replacements ) ) { return $replacements; }
$filesystem = imagify_get_filesystem(); $replacements = array( '{{PLUGINS}}/' => WP_PLUGIN_DIR, '{{MU_PLUGINS}}/' => WPMU_PLUGIN_DIR, '{{THEMES}}/' => WP_CONTENT_DIR . '/themes', '{{UPLOADS}}/' => $filesystem->get_main_upload_basedir(), '{{CONTENT}}/' => WP_CONTENT_DIR, '{{ROOT}}/' => $filesystem->get_site_root(), ); $replacements = array_map( array( $filesystem, 'normalize_dir_path' ), $replacements );
return $replacements; }
/** * Get array of pairs of placeholder => corresponding URL. * * @since 1.7 * @access public * @author Grégory Viguier * * @return array */ public static function get_placeholder_urls() { static $replacements;
if ( isset( $replacements ) ) { return $replacements; }
$filesystem = imagify_get_filesystem(); $replacements = array( '{{PLUGINS}}/' => plugins_url( '/' ), '{{MU_PLUGINS}}/' => plugins_url( '/', WPMU_PLUGIN_DIR . '/.' ), '{{THEMES}}/' => content_url( 'themes/' ), '{{UPLOADS}}/' => $filesystem->get_main_upload_baseurl(), '{{CONTENT}}/' => content_url( '/' ), '{{ROOT}}/' => $filesystem->get_site_root_url(), );
return $replacements; }
/** * A file_exists() for paths with a placeholder. * * @since 1.7 * @access public * @author Grégory Viguier * * @param string $file_path The file path. * @return bool */ public static function placeholder_path_exists( $file_path ) { return imagify_get_filesystem()->is_readable( self::remove_placeholder( $file_path ) ); }
/** ----------------------------------------------------------------------------------------- */ /** PATHS =================================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Get the path to NextGen galleries on monosites. * * @since 1.7 * @access public * @author Grégory Viguier * * @return string|bool An absolute path. False if it can't be retrieved. */ public static function get_ngg_galleries_path() { $galleries_path = get_site_option( 'ngg_options' );
if ( empty( $galleries_path['gallerypath'] ) ) { return false; }
$filesystem = imagify_get_filesystem(); $galleries_path = $filesystem->normalize_dir_path( $galleries_path['gallerypath'] ); $galleries_path = trim( $galleries_path, '/' ); // Something like `wp-content/gallery`.
$ngg_root = defined( 'NGG_GALLERY_ROOT_TYPE' ) ? NGG_GALLERY_ROOT_TYPE : 'site';
if ( $galleries_path && 'content' === $ngg_root ) { $ngg_root = $filesystem->normalize_dir_path( WP_CONTENT_DIR ); $ngg_root = trim( $ngg_root, '/' ); // Something like `abs-path/to/wp-content`.
$exploded_root = explode( '/', $ngg_root ); $exploded_galleries = explode( '/', $galleries_path ); $first_gallery_dirname = reset( $exploded_galleries ); $last_root_dirname = end( $exploded_root );
if ( $last_root_dirname === $first_gallery_dirname ) { array_shift( $exploded_galleries ); $galleries_path = implode( '/', $exploded_galleries ); } }
if ( 'content' === $ngg_root ) { $ngg_root = $filesystem->normalize_dir_path( WP_CONTENT_DIR ); } else { $ngg_root = $filesystem->get_abspath(); }
if ( strpos( $galleries_path, $ngg_root ) !== 0 ) { $galleries_path = $ngg_root . $galleries_path; }
return $galleries_path . '/'; }
/** * Get the path to WooCommerce logs on monosites. * * @since 1.7 * @access public * @author Grégory Viguier * * @return string An absolute path. */ public static function get_wc_logs_path() { if ( defined( 'WC_LOG_DIR' ) ) { return WC_LOG_DIR; }
return imagify_get_filesystem()->get_upload_basedir( true ) . 'wc-logs/'; }
/** * Get the path to EWWW optimization tools. * It is the same for all sites on multisite. * * @since 1.7 * @access public * @author Grégory Viguier * * @return string An absolute path. */ public static function get_ewww_tools_path() { if ( defined( 'EWWW_IMAGE_OPTIMIZER_TOOL_PATH' ) ) { return EWWW_IMAGE_OPTIMIZER_TOOL_PATH; }
return WP_CONTENT_DIR . '/ewww/'; }
/** * Get the path to ShortPixel backup folder. * It is the same for all sites on multisite (and yes, you'll get a surprise if your upload base dir -aka uploads/sites/12/- is not 2 folders deeper than theuploads folder). * * @since 1.8 * @access public * @author Grégory Viguier * * @return string An absolute path. */ public static function get_shortpixel_path() { if ( defined( 'SHORTPIXEL_BACKUP_FOLDER' ) ) { return trailingslashit( SHORTPIXEL_BACKUP_FOLDER ); }
$filesystem = imagify_get_filesystem(); $path = $filesystem->get_upload_basedir( true ); $path = is_main_site() ? $path : $filesystem->dir_path( $filesystem->dir_path( $path ) );
return $path . 'ShortpixelBackups/'; }
/** ----------------------------------------------------------------------------------------- */ /** REGEX PATTERNS ========================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Get the regex pattern used to match the paths to the media library. * Pattern delimiter is `Imagify_Filesystem::PATTERN_DELIMITER`. * Paths tested against these patterns are lower-cased. * * @since 1.8 * @access public * @author Grégory Viguier * * @return string Something like `/wp\-content/uploads/(sites/\d+/)?\d{4}/\d{2}/`. */ public static function get_media_library_pattern() { $filesystem = imagify_get_filesystem(); $uploads_dir = self::normalize_path_for_regex( $filesystem->get_main_upload_basedir() );
if ( ! is_multisite() ) { if ( get_option( 'uploads_use_yearmonth_folders' ) ) { // In year/month folders. return $uploads_dir . '\d{4}/\d{2}/'; }
// Not in year/month folders. return $uploads_dir . '[^/]+$'; }
$pattern = $filesystem->get_multisite_uploads_subdir_pattern();
if ( get_option( 'uploads_use_yearmonth_folders' ) ) { // In year/month folders. return $uploads_dir . '(' . $pattern . ')?\d{4}/\d{2}/'; }
// Not in year/month folders. return $uploads_dir . '(' . $pattern . ')?[^/]+$'; }
/** * Get the regex pattern used to match the paths to NextGen galleries on multisite. * Pattern delimiter is `Imagify_Filesystem::PATTERN_DELIMITER`. * Paths tested against these patterns are lower-cased. * * @since 1.8 * @access public * @author Grégory Viguier * * @return string|bool Something like `/wp-content/uploads/sites/\d+/nggallery/`. False if it can't be retrieved. */ public static function get_ngg_galleries_multisite_pattern() { $galleries_path = self::get_ngg_galleries_path(); // Something like `wp-content/uploads/sites/%BLOG_ID%/nggallery/`.
if ( ! $galleries_path ) { return false; }
$galleries_path = self::normalize_path_for_regex( $galleries_path ); $galleries_path = str_replace( array( '%blog_name%', '%blog_id%' ), array( '.+', '\d+' ), $galleries_path );
return $galleries_path; }
/** ----------------------------------------------------------------------------------------- */ /** NORMALIZATION TOOLS ===================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Normalize a file path, aiming for path comparison. * The path is normalized and case-lowered. * * @since 1.7 * @since 1.8 No trailing slash anymore, because it can be used for files. * @access public * @author Grégory Viguier * * @param string $file_path The file path. * @return string The normalized file path. */ public static function normalize_path_for_comparison( $file_path ) { return strtolower( wp_normalize_path( $file_path ) ); }
/** * Normalize a file path, aiming for use in a regex pattern. * The path is normalized, case-lowered, and escaped. * * @since 1.8 * @access public * @author Grégory Viguier * * @param string $file_path The file path. * @return string The normalized file path. */ public static function normalize_path_for_regex( $file_path ) { return preg_quote( imagify_get_filesystem()->normalize_path_for_comparison( $file_path ), Imagify_Filesystem::PATTERN_DELIMITER ); } }
|