C:\xampp\htdocs\landing\wp-content\plugins\imagify\inc\classes\class-imagify-filesystem.php


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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
<?php
defined
'ABSPATH' ) || die( 'Cheatin’ uh?' );

require_once 
ABSPATH 'wp-admin/includes/class-wp-filesystem-base.php';
require_once 
ABSPATH 'wp-admin/includes/class-wp-filesystem-direct.php';

/**
 * Class that enhance the WP filesystem class.
 *
 * @since  1.7.1
 * @author Grégory Viguier
 */
class Imagify_Filesystem extends WP_Filesystem_Direct {

    
/**
     * Class version.
     *
     * @var string
     */
    
const VERSION '1.2';

    
/**
     * Delimiter used for regex patterns.
     *
     * @var    string
     * @since  1.8
     * @author Grégory Viguier
     */
    
const PATTERN_DELIMITER '@';

    
/**
     * The single instance of the class.
     *
     * @var    object
     * @access protected
     */
    
protected static $_instance;


    
/** ----------------------------------------------------------------------------------------- */
    /** INSTANCIATION =========================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Constructor.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     */
    
public function __construct() {
        
// Define the permission constants if not already done.
        
if ( ! defined'FS_CHMOD_DIR' ) ) {
            
define'FS_CHMOD_DIR', ( filepermsABSPATH ) & 0777 0755 ) );
        }
        if ( ! 
defined'FS_CHMOD_FILE' ) ) {
            
define'FS_CHMOD_FILE', ( filepermsABSPATH 'index.php' ) & 0777 0644 ) );
        }

        
parent::__construct'' );
    }

    
/**
     * Get the main Instance.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @return object Main instance.
     */
    
public static function get_instance() {
        if ( ! isset( 
self::$_instance ) ) {
            
self::$_instance = new self();
        }

        return 
self::$_instance;
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** CUSTOM TOOLS ============================================================================ */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Get the file name.
     * Replacement for basename().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return string|bool       The base name of the given path. False on failure.
     */
    
public function file_name$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        return 
wp_basename$file_path );
    }

    
/**
     * Get the parent directory's path.
     * Replacement for dirname().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return string|bool       The directory path with a trailing slash. False on failure.
     */
    
public function dir_path$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        
$file_path dirname$file_path );

        return 
$this->is_root$file_path ) ? $this->get_root() : trailingslashit$file_path );
    }

    
/**
     * Get information about a file path.
     * Replacement for pathinfo().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @param  string $option    If present, specifies a specific element to be returned; one of 'dir_path', 'file_name', 'extension' or 'file_base'.
     *                           If option is not specified, returns all available elements.
     * @return array|string|null If the option parameter is not passed, an associative array containing the following elements is returned: 'dir_path' (with trailing slash), 'file_name' (with extension), 'extension' (if any), and 'file_base' (without extension).
     */
    
public function path_info$file_path$option null ) {
        if ( ! 
$file_path ) {
            if ( isset( 
$option ) ) {
                return 
'';
            }

            return array(
                
'dir_path'  => '',
                
'file_name' => '',
                
'extension' => null,
                
'file_base' => '',
            );
        }

        if ( isset( 
$option ) ) {
            
$options = array(
                
'dir_path'  => PATHINFO_DIRNAME,
                
'file_name' => PATHINFO_BASENAME,
                
'extension' => PATHINFO_EXTENSION,
                
'file_base' => PATHINFO_FILENAME,
            );

            if ( ! isset( 
$options$option ] ) ) {
                return 
'';
            }

            
$output pathinfo$file_path$options$option ] );

            if ( 
'dir_path' !== $option ) {
                return 
$output;
            }

            return 
$this->is_root$output ) ? $this->get_root() : trailingslashit$output );
        }

        
$output pathinfo$file_path );

        
$output['dirname']   = $this->is_root$output['dirname'] ) ? $this->get_root()    : trailingslashit$output['dirname'] );
        
$output['extension'] = isset( $output['extension'] )        ? $output['extension'] : null;

        
// '/www/htdocs/inc/lib.inc.php'
        
return array(
            
'dir_path'  => $output['dirname'],   // '/www/htdocs/inc/'
            
'file_name' => $output['basename'],  // 'lib.inc.php'
            
'extension' => $output['extension'], // 'php'
            
'file_base' => $output['filename'],  // 'lib.inc'
        
);
    }

    
/**
     * Recursive directory creation based on full path. Will attempt to set permissions on folders.
     * Replacement for recursive mkdir().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $path Full path to attempt to create.
     * @return bool         Whether the path was created. True if path already exists.
     */
    
public function make_dir$path ) {
        
/*
        * Safe mode fails with a trailing slash under certain PHP versions.
        */
        
$path untrailingslashitwp_normalize_path$path ) );

        if ( 
$this->is_root$path ) ) {
            return 
$this->is_dir$this->get_root() ) && $this->is_writable$this->get_root() );
        }

        if ( 
$this->exists$path ) ) {
            return 
$this->is_dir$path ) && $this->is_writable$path );
        }

        
$site_root $this->get_site_root();

        if ( 
strpos$path$site_root ) !== ) {
            return 
false;
        }

        
$bits preg_replace'@^' preg_quote$site_root'@' ) . '@i'''$path );
        
$bits explode'/'trim$bits'/' ) );
        
$path untrailingslashit$site_root );

        foreach ( 
$bits as $bit ) {
            
$parent_path $path;
            
$path       .= '/' $bit;

            if ( 
$this->exists$path ) ) {
                if ( ! 
$this->is_dir$path ) ) {
                    return 
false;
                }

                continue;
            }

            if ( ! 
$this->is_writable$parent_path ) ) {
                
$this->chmod_dir$parent_path );

                if ( ! 
$this->is_writable$parent_path ) ) {
                    return 
false;
                }
            }

            
$this->mkdir$path );

            if ( ! 
$this->exists$path ) ) {
                return 
false;
            }

            
$this->touchtrailingslashit$path ) . 'index.php' );
        }

        return 
true;
    }

    
/**
     * Set a file permissions using FS_CHMOD_FILE.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return bool              True on success, false on failure.
     */
    
public function chmod_file$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        return 
$this->chmod$file_pathFS_CHMOD_FILE );
    }

    
/**
     * Set a directory permissions using FS_CHMOD_DIR.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the directory.
     * @return bool              True on success, false on failure.
     */
    
public function chmod_dir$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        return 
$this->chmod$file_pathFS_CHMOD_DIR );
    }

    
/**
     * Get a file mime type.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path A file path (prefered) or a filename.
     * @return string|bool       A mime type. False on failure: the test is limited to mime types supported by Imagify.
     */
    
public function get_mime_type$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        
$file_type wp_check_filetype$file_pathimagify_get_mime_types() );

        return 
$file_type['type'];
    }

    
/**
     * Get a file modification date, formated as "mysql". Fallback to current date.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return string            The date.
     */
    
public function get_date$file_path ) {
        static 
$offset;

        if ( ! 
$file_path ) {
            return 
current_time'mysql' );
        }

        
$date $this->mtime$file_path );

        if ( ! 
$date ) {
            return 
current_time'mysql' );
        }

        if ( ! isset( 
$offset ) ) {
            
$offset get_option'gmt_offset' ) * HOUR_IN_SECONDS;
        }

        return 
gmdate'Y-m-d H:i:s'$date $offset );
    }

    
/**
     * Tell if a file is symlinked.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path An absolute path.
     * @return bool
     */
    
public function is_symlinked$file_path ) {
        static 
$site_root;
        static 
$plugin_paths = array();
        global 
$wp_plugin_paths;

        if ( ! 
$file_path ) {
            return 
false;
        }

        
$real_path realpath$file_path );

        if ( ! 
$real_path ) {
            return 
false;
        }

        if ( ! isset( 
$site_root ) ) {
            
$site_root $this->normalize_path_for_comparison$this->get_site_root() );
        }

        
$lower_file_path $this->normalize_path_for_comparison$real_path );

        if ( 
strpos$lower_file_path$site_root ) !== ) {
            return 
true;
        }

        if ( 
$wp_plugin_paths && is_array$wp_plugin_paths ) ) {
            if ( ! 
$plugin_paths ) {
                foreach ( 
$wp_plugin_paths as $dir => $real_dir ) {
                    
$dir $this->normalize_path_for_comparison$dir );
                    
$plugin_paths$dir ] = $this->normalize_path_for_comparison$real_dir );
                }
            }

            
$lower_file_path $this->normalize_path_for_comparison$file_path );

            foreach ( 
$plugin_paths as $dir => $real_dir ) {
                if ( 
strpos$lower_file_path$dir ) === ) {
                    return 
true;
                }
            }
        }

        return 
false;
    }

    
/**
     * Tell if a file is a pdf.
     *
     * @since  1.8
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return bool
     */
    
public function is_pdf$file_path ) {
        if ( 
function_exists'finfo_fopen' ) ) {
            
$finfo finfo_openFILEINFO_MIME );

            if ( 
$finfo ) {
                
$mimetype finfo_file$finfo$file_path );

                if ( 
false !== $mimetype ) {
                    return 
'application/pdf' === $mimetype;
                }
            }
        }

        if ( 
function_exists'mime_content_type' ) ) {
            
$mimetype mime_content_type$file_path );
            return 
'application/pdf' === $mimetype;
        }

        return 
false;
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** CLASS OVERWRITES ======================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Move a file and apply chmod.
     * If the file failed to be moved once, a 2nd attempt is made after applying chmod.
     *
     * @since  1.8
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $source      Path to the file to move.
     * @param  string $destination Path to the destination.
     * @param  bool   $overwrite   Allow to overwrite existing file at destination.
     * @return bool                True on success, false on failure.
     */
    
public function move$source$destination$overwrite false ) {
        if ( 
parent::move$source$destination$overwrite ) ) {
            return 
$this->chmod_file$destination );
        }

        if ( ! 
$this->chmod_file$destination ) ) {
            return 
false;
        }

        if ( 
parent::move$source$destination$overwrite ) ) {
            return 
$this->chmod_file$destination );
        }

        return 
false;
    }

    
/**
     * Determine if a file or directory is writable.
     * This function is used to work around certain ACL issues in PHP primarily affecting Windows Servers.
     * Replacement for is_writable().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return bool
     */
    
public function is_writable$file_path ) {
        if ( ! 
$file_path ) {
            return 
false;
        }

        return 
wp_is_writable$file_path );
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** WORK WITH IMAGES ======================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Tell if a file is an image.
     *
     * @since  1.8
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return bool
     */
    
public function is_image$file_path ) {
        if ( 
function_exists'finfo_fopen' ) ) {
            
$finfo finfo_openFILEINFO_MIME );

            if ( 
$finfo ) {
                
$mimetype finfo_file$finfo$file_path );

                if ( 
false !== $mimetype ) {
                    return 
strpos$mimetype'image/' ) === 0;
                }
            }
        }

        if ( 
function_exists'exif_imagetype' ) ) {
            
$mimetype exif_imagetype$file_path );
            return (bool) 
$mimetype;
        }

        if ( 
function_exists'mime_content_type' ) ) {
            
$mimetype mime_content_type$file_path );
            return 
strpos$mimetype'image/' ) === 0;
        }

        return 
false;
    }

    
/**
     * Get an image data.
     * Replacement for getimagesize().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return array             The image data. An empty array on failure.
     */
    
public function get_image_size$file_path ) {
        if ( ! 
$file_path ) {
            return array();
        }

        
$size = @getimagesize$file_path );

        if ( ! 
$size || ! isset( $size[0], $size[1] ) ) {
            return array();
        }

        return array(
            
0          => (int) $size[0],
            
1          => (int) $size[1],
            
'width'    => (int) $size[0],
            
'height'   => (int) $size[1],
            
'type'     => (int) $size[2],
            
'attr'     => $size[3],
            
'channels' => isset( $size['channels'] ) ? (int) $size['channels'] : null,
            
'bits'     => isset( $size['bits'] )     ? (int) $size['bits']     : null,
            
'mime'     => $size['mime'],
        );
    }

    
/**
     * Tell if exif_read_data() is available.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @return bool
     */
    
public function can_get_exif() {
        static 
$callable;

        if ( ! isset( 
$callable ) ) {
            
$callable is_callable'exif_read_data' );
        }

        return 
$callable;
    }

    
/**
     * Get the EXIF headers from an image file.
     * Replacement for exif_read_data().
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     * @see    https://secure.php.net/manual/en/function.exif-read-data.php
     *
     * @param  string $file_path Path to the file.
     * @param  string $sections  A comma separated list of sections that need to be present in file to produce a result array. See exif_read_data() documentation for values: FILE, COMPUTED, ANY_TAG, IFD0, THUMBNAIL, COMMENT, EXIF.
     * @param  bool   $arrays    Specifies whether or not each section becomes an array. The sections COMPUTED, THUMBNAIL, and COMMENT always become arrays as they may contain values whose names conflict with other sections.
     * @param  bool   $thumbnail When set to TRUE the thumbnail itself is read. Otherwise, only the tagged data is read.
     * @return array             The EXIF headers. An empty array on failure.
     */
    
public function get_image_exif$file_path$sections null$arrays false$thumbnail false ) {
        if ( ! 
$file_path || ! $this->can_get_exif() ) {
            return array();
        }

        
$exif = @exif_read_data$file_path$sections$arrays$thumbnail );

        return 
is_array$exif ) ? $exif : array();
    }

    
/**
     * Tell if a file is an animated gif.
     *
     * @since  1.9.5
     * @access public
     * @source https://www.php.net/manual/en/function.imagecreatefromgif.php#104473
     * @author Grégory Viguier
     *
     * @param  string $file_path Path to the file.
     * @return bool|null         Null if the file cannot be read.
     */
    
public function is_animated_gif$file_path ) {
        if ( 
$this->path_info$file_path'extension' ) !== 'gif' ) {
            
// Not a gif file.
            
return false;
        }

        
$fh = @fopen$file_path'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen

        
if ( ! $fh ) {
            
// Could not open the file.
            
return null;
        }

        
/**
         * An animated gif contains multiple "frames", with each frame having a header made up of:
         * - a static 4-byte sequence (\x00\x21\xF9\x04),
         * - 4 variable bytes,
         * - a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?).
         */
        
$count 0;

        
// We read through the file til we reach the end of the file, or we've found at least 2 frame headers.
        
while ( ! feof$fh ) && $count ) {
            
// Read 100kb at a time.
            
$chunk  fread$fh1024 100 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
            
$count += preg_match_all'#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s'$chunk$matches );
        }

        
fclose$fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose

        
return $count 1;
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** WORK WITH PATHS ========================================================================= */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Make an absolute path relative to WordPress' root folder.
     * Also works for files from registered symlinked plugins.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path An absolute path.
     * @param  string $base      A base path to use instead of ABSPATH.
     * @return string|bool       A relative path. Can return the absolute path or false in case of a failure.
     */
    
public function make_path_relative$file_path$base '' ) {
        global 
$wp_plugin_paths;

        if ( ! 
$file_path ) {
            return 
false;
        }

        
$file_path wp_normalize_path$file_path );
        
$base      $base $this->normalize_dir_path$base ) : $this->get_site_root();
        
$pos       strpos$file_path$base );

        if ( 
false === $pos && $wp_plugin_paths && is_array$wp_plugin_paths ) ) {
            
// The file is probably part of a symlinked plugin.
            
arsort$wp_plugin_paths );

            foreach ( 
$wp_plugin_paths as $dir => $real_dir ) {
                if ( 
strpos$file_path$real_dir ) === ) {
                    
$file_path wp_normalize_path$dir substr$file_pathstrlen$real_dir ) ) );
                }
            }

            
$pos strpos$file_path$base );
        }

        if ( 
false === $pos ) {
            
// We're in trouble.
            
return $file_path;
        }

        return 
substr_replace$file_path''0$pos strlen$base ) );
    }

    
/**
     * Normalize a directory path.
     * The path is normalized and a trailing slash is added.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path The file path.
     * @return string            The normalized dir path.
     */
    
public function normalize_dir_path$file_path ) {
        return 
wp_normalize_pathtrailingslashit$file_path ) );
    }

    
/**
     * Normalize a file path, aiming for path comparison.
     * The path is normalized, case-lowered, and a trailing slash is added.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $file_path The file path.
     * @return string            The normalized file path.
     */
    
public function normalize_path_for_comparison$file_path ) {
        return 
strtolower$this->normalize_dir_path$file_path ) );
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** SOME WELL KNOWN PATHS AND URLS ========================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Tell if WordPress is installed in its own directory: aka WP's path !== site's path.
     *
     * @since  1.8.1
     * @access public
     * @see    https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function has_wp_its_own_directory() {
        return 
$this->get_abspath() !== $this->get_site_root();
    }

    
/**
     * The path to the server's root is not always '/', it can also be '//' or 'C://'.
     * I am get_root.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @return string The path to the server's root.
     */
    
public function get_root() {
        static 
$groot;

        if ( isset( 
$groot ) ) {
            return 
$groot;
        }

        
$groot preg_replace'@^((?:.:)?/+).*@''$1'$this->get_site_root() );

        return 
$groot;
    }

    
/**
     * Tell if a path is the server's root.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $path The path.
     * @return bool
     */
    
public function is_root$path ) {
        
$path rtrim$path'/\\' );
        return 
'.' === $path || '' === $path || preg_match'@^.:$@'$path );
    }

    
/**
     * Get the path to the site's root.
     * This is an improved version of get_home_path() that *should* work in almost every cases.
     * Because creating a constant like ABSPATH was too simple.
     *
     * @since  1.8.1
     * @access public
     * @see    get_home_path()
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function get_site_root() {
        static 
$root_path;

        if ( isset( 
$root_path ) ) {
            return 
$root_path;
        }

        
/**
         * Filter the path to the site's root.
         *
         * @since  1.8.1
         * @author Grégory Viguier
         *
         * @param string $root_path Path to the site's root. Default is null.
         */
        
$root_path apply_filters'imagify_site_root'null );

        if ( 
is_string$root_path ) ) {
            
$root_path trailingslashitwp_normalize_path$root_path ) );

            return 
$root_path;
        }

        
$home    set_url_schemeuntrailingslashitget_option'home' ) ), 'http' );
        
$siteurl set_url_schemeuntrailingslashitget_option'siteurl' ) ), 'http' );

        if ( ! empty( 
$home ) && !== strcasecmp$home$siteurl ) ) {
            
$wp_path_rel_to_home str_ireplace$home''$siteurl ); /* $siteurl - $home */
            
$pos                 strriposstr_replace'\\''/'ABSPATH ), trailingslashit$wp_path_rel_to_home ) );
            
$root_path           substrABSPATH0$pos );
            
$root_path           trailingslashitwp_normalize_path$root_path ) );
            return 
$root_path;
        }

        if ( ! 
defined'PATH_CURRENT_SITE' ) || ! is_multisite() || is_main_site() ) {
            
$root_path $this->get_abspath();
            return 
$root_path;
        }

        
/**
         * For a multisite in its own directory, get_home_path() returns the expected path only for the main site.
         *
         * Friend, each time an attempt is made to improve this method, and especially this part, please increment the following counter.
         * Improvement attempts: 3.
         */
        
$document_root     realpathwp_unslash$_SERVER['DOCUMENT_ROOT'] ) ); // `realpath()` is needed for those cases where $_SERVER['DOCUMENT_ROOT'] is totally different from ABSPATH.
        
$document_root     trailingslashitstr_replace'\\''/'$document_root ) );
        
$path_current_site trimstr_replace'\\''/'PATH_CURRENT_SITE ), '/' );
        
$root_path         trailingslashitwp_normalize_path$document_root $path_current_site ) );

        return 
$root_path;
    }

    
/**
     * Get the URL of the site's root. It corresponds to the main site's home page URL.
     *
     * @since  1.8.1
     * @access public
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function get_site_root_url() {
        static 
$root_url;

        if ( isset( 
$root_url ) ) {
            return 
$root_url;
        }

        if ( ! 
is_multisite() || is_main_site() ) {
            
$root_url home_url'/' );
            return 
$root_url;
        }

        
$current_network false;

        if ( 
function_exists'get_network' ) ) {
            
$current_network get_network();
        } elseif ( 
function_exists'get_current_site' ) ) {
            
$current_network get_current_site();
        }

        if ( ! 
$current_network ) {
            
$root_url home_url'/' );
            return 
$root_url;
        }

        
$root_url is_ssl() ? 'https' 'http';
        
$root_url set_url_scheme'http://' $current_network->domain $current_network->path$root_url );
        
$root_url trailingslashit$root_url );

        return 
$root_url;
    }

    
/**
     * Tell if a path is the site's root.
     *
     * @since  1.8.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $path The path.
     * @return bool
     */
    
public function is_site_root$path ) {
        return 
$this->normalize_dir_path$path ) === $this->get_site_root();
    }

    
/**
     * Get a clean value of ABSPATH.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @return string The path to WordPress' root folder.
     */
    
public function get_abspath() {
        static 
$abspath;

        if ( isset( 
$abspath ) ) {
            return 
$abspath;
        }

        
$abspath wp_normalize_pathABSPATH );

        
// Make sure ABSPATH is not messed up: it could be defined as a relative path for example (yeah, I know, but we've seen it).
        
$test_file wp_normalize_pathIMAGIFY_FILE );
        
$pos       strpos$test_file$abspath );

        if ( 
$pos ) {
            
// ABSPATH has a wrong value.
            
$abspath substr$test_file0$pos ) . $abspath;

        } elseif ( 
false === $pos && class_exists'ReflectionClass' ) ) {
            
// Imagify is symlinked (dude, you look for trouble).
            
$reflector = new ReflectionClass'WP' );
            
$test_file $reflector->getFileName();
            
$pos       strpos$test_file$abspath );

            if ( 
$pos ) {
                
// ABSPATH has a wrong value.
                
$abspath substr$test_file0$pos ) . $abspath;
            }
        }

        
$abspath trailingslashit$abspath );

        if ( 
'/' !== substr$abspath0) && ':' !== substr$abspath1) ) {
            
$abspath '/' $abspath;
        }

        return 
$abspath;
    }

    
/**
     * Tell if a path is WP's root (ABSPATH).
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $path The path.
     * @return bool
     */
    
public function is_abspath$path ) {
        return 
$this->normalize_dir_path$path ) === $this->get_abspath();
    }

    
/**
     * Get the upload basedir.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @param  bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
     * @return string|bool        The path. False on failure.
     */
    
public function get_upload_basedir$bypass_error false ) {
        static 
$upload_basedir;
        static 
$upload_basedir_or_error;

        if ( isset( 
$upload_basedir ) ) {
            return 
$bypass_error $upload_basedir $upload_basedir_or_error;
        }

        
$uploads        wp_upload_dir();
        
$upload_basedir $this->normalize_dir_path$uploads['basedir'] );

        if ( 
false !== $uploads['error'] ) {
            
$upload_basedir_or_error false;
        } else {
            
$upload_basedir_or_error $upload_basedir;
        }

        return 
$bypass_error $upload_basedir $upload_basedir_or_error;
    }

    
/**
     * Get the upload baseurl.
     *
     * @since  1.7.1
     * @access public
     * @author Grégory Viguier
     *
     * @return string|bool The URL. False on failure.
     */
    
public function get_upload_baseurl() {
        static 
$upload_baseurl;

        if ( isset( 
$upload_baseurl ) ) {
            return 
$upload_baseurl;
        }

        
$uploads wp_upload_dir();

        if ( 
false !== $uploads['error'] ) {
            
$upload_baseurl false;
            return 
$upload_baseurl;
        }

        
$upload_baseurl trailingslashit$uploads['baseurl'] );

        return 
$upload_baseurl;
    }

    
/**
     * Get the path to the uploads base directory of the main site.
     *
     * @since  1.8
     * @access public
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function get_main_upload_basedir() {
        static 
$basedir;

        if ( isset( 
$basedir ) ) {
            return 
$basedir;
        }

        
$basedir get_imagify_upload_basedirtrue );

        if ( 
is_multisite() ) {
            
$pattern '/' $this->get_multisite_uploads_subdir_pattern() . '$';
            
$basedir preg_replaceself::PATTERN_DELIMITER $pattern self::PATTERN_DELIMITER'/'$basedir );
        }

        return 
$basedir;
    }

    
/**
     * Get the URL of the uploads base directory of the main site.
     *
     * @since  1.8
     * @access public
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function get_main_upload_baseurl() {
        static 
$baseurl;

        if ( isset( 
$baseurl ) ) {
            return 
$baseurl;
        }

        
$baseurl get_imagify_upload_baseurltrue );

        if ( 
is_multisite() ) {
            
$pattern '/' $this->get_multisite_uploads_subdir_pattern() . '$';
            
$baseurl preg_replaceself::PATTERN_DELIMITER $pattern self::PATTERN_DELIMITER'/'$baseurl );
        }

        return 
$baseurl;
    }

    
/**
     * Get the regex pattern used to match the uploads subdir on multisite in a file path.
     * Pattern delimiter is `Imagify_Filesystem::PATTERN_DELIMITER`.
     * Paths tested against these patterns are lower-cased.
     *
     * @since  1.8
     * @access public
     * @see    _wp_upload_dir()
     * @author Grégory Viguier
     *
     * @return string
     */
    
public function get_multisite_uploads_subdir_pattern() {
        static 
$pattern;

        if ( isset( 
$pattern ) ) {
            return 
$pattern;
        }

        
$pattern '';

        if ( ! 
is_multisite() ) {
            return 
$pattern;
        }

        if ( ! 
get_site_option'ms_files_rewriting' ) ) {
            if ( 
defined'MULTISITE' ) ) {
                
$pattern 'sites/\d+/';
            } else {
                
$pattern '\d+/';
            }
        } elseif ( 
defined'UPLOADS' ) ) {
            
$site_id = (string) get_current_blog_id();
            
$path    $this->get_upload_basedirtrue ); // Something like `/absolute/path/to/wp-content/blogs.dir/3/files/`, also for site 1.
            
$path    strrev$path );

            if ( 
preg_matchself::PATTERN_DELIMITER '^.*' strrev$site_id ) . '[^/]*/' self::PATTERN_DELIMITER 'U'$path$matches ) ) {
                
$pattern end$matches );
                
$pattern ltrimstrtolowerstrrev$pattern ) ), '/' );
                
$pattern str_replace$site_id'\d+'$pattern );
            }
        }

        
/**
         * Filter the regex pattern used to match the uploads subdir on multisite in a file path.
         * Pattern delimiter is `Imagify_Filesystem::PATTERN_DELIMITER`.
         * Important: lowercase, no heading slash, mandatory trailing slash.
         *
         * @since  1.8
         * @author Grégory Viguier
         *
         * @param string $pattern The regex pattern.
         */
        
$pattern apply_filters'imagify_multisite_uploads_subdir_pattern'$pattern );

        return 
$pattern;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586