/var/www/html_sp/wp-content/plugins/loco-translate/src/fs/File.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
<?php
/**
 * 
 */
class Loco_fs_File {
    
    
/**
     * @var Loco_fs_FileWriter
     */
    
private $w;

    
/**
     * Path to file
     * @var string
     */
    
private $path;
    
    
/**
     * Cached pathinfo() data 
     * @var array
     */
    
private $info;

    
/**
     * Base path which path has been normalized against
     * @var string
     */
    
private $base;

    
/**
     * Flag set when current path is relative
     * @var bool
     */
    
private $rel;


    
/**
     * Check if a path is absolute and return fixed slashes for readability
     * @param string $path
     * @return string fixed path, or "" if not absolute
     */
    
public static function abs$path ){
        
$path = (string) $path;
        if( 
'' !== $path ){
            
$chr1 substr($path,0,1);
            
// return unmodified path if starts "/"
            
if( '/' === $chr1 ){
                return 
$path;
            }
            
// Windows drive path if "X:" or network path if "\\"
            
$chr2 = (string) substr($path,1,1);
            if( 
'' !== $chr2 ){
                if( 
':' === $chr2 ||  ( '\\' === $chr1 && '\\' === $chr2 ) ){
                    return 
strtoupper($chr1).$chr2.strtrsubstr($path,2), '\\''/' );
                }
            }
        }
        
// else path is relative, so return falsy string
        
return '';
    }


    
/**
     * Test if a path looks absolute
     */
    
public static function is_abs$path ){
        return 
'' !== $path && ( '/' === $path[0] || preg_match('!^\\\\\\\\|.:\\\\!',$path) );
    }


    
/**
     * Call PHP is_readable() but suppress E_WARNING when path is outside open_basedir.
     * @param string $path
     * @return bool
     */
    
public static function is_readable$path ){
        if( 
'' === $path || '.' === $path[0] ){
            throw new 
InvalidArgumentException('Relative paths disallowed');
        }
        
// Reduce PHP errors from is_readable to debug messages
        
Loco_error_AdminNotices::capture(E_NOTICE|E_WARNING);
        
$bool is_readable($path);
        
restore_error_handler();
        return 
$bool;
    }


    
/**
     * Create file with initial, unvalidated path
     * @param string $path
     */    
    
public function __construct$path ){
        
$this->setPath$path );
    }


    
/**
     * Internally set path value and flag whether relative or absolute
     * @param string $path
     * @return void
     */
    
private function setPath$path ){
        
$path = (string) $path;
        if( 
$fixed self::abs($path) ){
            
$path $fixed;
            
$this->rel false;
        }
        else {
            
$this->rel true;
        }
        if( 
$path !== $this->path ){
            
$this->path $path;
            
$this->info null;
        }
    }


    
/**
     * @return bool
     */
    
public function isAbsolute(){
        return ! 
$this->rel;
    }


    
/**
     * @internal
     */
    
public function __clone(){
        
$this->cloneWriteContext$this->);
    }


    
/**
     * Copy write context with our file reference
     */
    
private function cloneWriteContext( ?Loco_fs_FileWriter $context null ):void {
        if( 
$context ){
            
$context = clone $context;
            
$this->$context->setFile($this);
        }
    }


    
/**
     * Get file system context for operations that *modify* the file system.
     * Read operations and operations that stat the file will always do so directly.
     */
    
public function getWriteContext():Loco_fs_FileWriter {
        if( ! 
$this->){
            
$this->= new Loco_fs_FileWriter$this );
        }
        return 
$this->w;
    }


    
/**
     * @internal
     */
    
private function pathinfo(){
        return 
is_array($this->info) ? $this->info : ( $this->info pathinfo($this->path) );
    }


    
/**
     * Checks if a file exists, and is within open_basedir restrictions.
     * This does NOT check if file permissions allow PHP to read it. Call $this->readable() or self::is_readable($path).
     */
    
public function exists():bool {
        return 
file_exists($this->path);
    }


    
/**
     * Check if file is writable by the current write context
     */
    
public function writable():bool {
        return 
$this->getWriteContext()->writable();
    }


    
/**
     * Check if the file exists and is readable by the current PHP process.
     */
    
public function readable():bool {
        return 
self::is_readable($this->path);
    }


    
/**
     * Check if file is removable by the current write context
     */
    
public function deletable():bool {
        
$parent $this->getParent();
        if( 
$parent && $parent->writable() ){
            
// sticky directory requires that either the file its parent is owned by effective user
            
if( $parent->mode() & 01000 ){
                
$writer $this->getWriteContext();
                if( 
$writer->isDirect() && ( $uid Loco_compat_PosixExtension::getuid() ) ){
                    return 
$uid === $this->uid() || $uid === $parent->uid();
                }
                
// else delete operation won't be done directly, so can't preempt sticky problems
                // TODO is it worth comparing FTP username etc.. for ownership?
            
}
            
// defaulting to "deletable" based on fact that parent is writable.
            
return true;
        }
        return 
false;
    }


    
/**
     * Get owner uid
     * @return int|false
     */
    
public function uid(){
        return 
fileowner($this->path);
    }


    
/**
     * Get group gid
     * @return int|false
     */
    
public function gid(){
        return 
filegroup($this->path);
    }


    
/**
     * Check if file can't be overwritten when existent, nor created when non-existent
     * This does not check permissions recursively as directory trees are not built implicitly
     */
    
public function locked():bool {
        if( 
$this->exists() ){
            return ! 
$this->writable();
        }
        if( 
$dir $this->getParent() ){
            return ! 
$dir->writable();
        }
        return 
true;
    }


    
/**
     * Check if full path can be built to non-existent file.
     */
    
public function creatable():bool {
        
$file $this;
        while( 
$file $file->getParent() ){
            if( 
$file->exists() ){
                return 
$file->writable();
            }
        }
        return 
false;
    }
    
    
    
/**
     * @return string
     */
    
public function dirname(){
        
$info $this->pathinfo();
        return 
$info['dirname'];
    }

    
    
/**
     * @return string
     */
    
public function basename(){
        
$info $this->pathinfo();
        return 
$info['basename'];
    }

    
    
/**
     * @return string
     */
    
public function filename(){
        
$info $this->pathinfo();
        return 
$info['filename'];
    }


    
/**
     * Gets final file extension, e.g. "html" in "foo.php.html"
     * @return string
     */
    
public function extension(){
        
$info $this->pathinfo();
        return 
$info['extension'] ?? '';
    }


    
/**
     * Gets full file extension after first dot ("."), e.g. "php.html" in "foo.php.html"
     * @return string
     */
    
public function fullExtension(){
        
$bits explode('.',$this->basename(),2);
        return 
array_key_exists(1,$bits) ? $bits[1] : '';
    }


    
/**
     * @return string
     */
    
public function getPath(){
        return 
$this->path;
    }


    
/**
     * Get file modification time as unix timestamp in seconds
     * @return int
     */
    
public function modified(){
        return 
filemtime$this->path );
    }


    
/**
     * Get file size in bytes
     * @return int
     */
    
public function size(){
        return 
filesize$this->path );
    }


    
/**
     * @return int
     */
    
public function mode(){
        if( 
is_link($this->path) ){
            
$stat lstat$this->path );
            
$mode $stat[2];
        }
        else {
            
$mode fileperms($this->path);
        }
        return 
$mode;
    }
    

    
/**
     * Set file mode
     * @param int $mode file mode integer e.g 0664
     * @param bool $recursive whether to set recursively (directories)
     * @return Loco_fs_File
     */
    
public function chmod$mode$recursive false ){
        
$this->getWriteContext()->chmod$mode$recursive );
        return 
$this->clearStat();
    }

    
    
/**
     * Clear stat cache if any file data has changed
     * @return Loco_fs_File
     */
    
public function clearStat(){
        
$this->info null;
        
// PHP 5.3.0 Added optional clear_realpath_cache and filename parameters.
        
if( version_comparePHP_VERSION'5.3.0''>=' ) ){
            
clearstatcachetrue$this->path );
        }
        
// else no choice but to drop entire stat cache
        
else {
            
clearstatcache();
        }
        return 
$this;
    }

    
    
/**
     * @return string
     */
    
public function __toString(){
        return 
$this->getPath();
    }


    
/**
     * Check if passed path is equal to ours
     * @param string|self $ref
     * @return bool
     */
    
public function equal$ref ){
        return 
$this->path === (string) $ref;
    }


    
/**
     * Normalize path for string comparison, resolves redundant dots and slashes.
     * @param string $base path to prefix
     * @return string
     */
    
public function normalize$base '' ){
        if( 
$path self::abs($base) ){
            
$base $path;
        }
        if( 
$base !== $this->base ){
            
$path $this->path;
            if( 
'' === $path ){
                
$this->setPath($base);
            }
            else {
                if( ! 
$this->rel || ! $base ){
                    
$b = [];
                }
                else {
                    
$b self::explode$base, [] );
                }
                
$b self::explode$path$b );
                
$this->setPathimplode('/',$b) );
            }
            
$this->base $base;
        }
        return 
$this->path;
    }


    
/**
     * Get real path if file is real, but without altering internal path property.
     * Also skips call to realpath() when likely to raise E_WARNING due to open_basedir
     * @return string
     */
    
public function getRealPath(){
        if( 
$this->readable() ){
            
$path realpath$this->getPath() );
            if( 
is_string($path) ){
                return 
$path;
            }
        }
        return 
'';
    } 


    
/**
     * @param string $path
     * @param string[] $b
     * @return array
     */
    
private static function explode$path, array $b ){
        
$a explode'/'$path );
        foreach( 
$a as $i => $s ){
            if( 
'' === $s ){
                if( 
!== $i ){
                    continue;
                }
            }
            if( 
'.' === $s ){
                continue;
            }
            if( 
'..' === $s ){
                if( 
array_pop($b) ){
                    continue;
                }
            }
            
$b[] = $s;
        }
        return 
$b;
    }


    
/**
     * Get path relative to given location, unless path is already relative
     * @param string $base Base path
     * @return string path relative to given base
     */
    
public function getRelativePath$base ){
        
$path $this->normalize();
        if( 
self::abs($path) ){
            
// base may require normalizing
            
$file = new Loco_fs_File($base);
            
$base $file->normalize();
            
$length strlen($base)+1;
            
// if we are below given base path, return ./relative
            
if( substr($path,0,$length) === $base.'/' ){
                if( 
strlen($path) > $length ){
                    return 
substr$path$length );
                }
                
// else paths were identical
                
return '';
            }
            
// else attempt to find nearest common root
            
$i 0;
            
$source explode('/',$base);
            
$target explode('/',$path);
            while( isset(
$source[$i]) && isset($target[$i]) && $source[$i] === $target[$i] ){
                
$i++;
            }
            if( 
$i ){
                
$depth count($source) - $i;
                
$build array_mergearray_fill0$depth'..' ), array_slice$target$i ) );
                
$path implode'/'$build );
            }
        }
        
// else return unmodified
        
return $path;
    }


    
/**
     * Test if file is a directory
     */
    
public function isDirectory():bool {
        if( 
$this->readable() ){
            return 
is_dir($this->path);
        }
        return 
'' === $this->extension();
    }



    
/**
     * Load contents of file into a string
     */
    
public function getContents():string {
        return 
file_get_contents($this->path);
    }


    
/**
     * Check if path is under a theme directory 
     */
    
public function underThemeDirectory():bool {
        return 
Loco_fs_Locations::getThemes()->check$this->path );
    }


    
/**
     * Check if path is under a plugin directory 
     */
    
public function underPluginDirectory():bool {
        return 
Loco_fs_Locations::getPlugins()->check$this->path );
    }


    
/**
     * Check if path is under wp-content directory 
     */
    
public function underContentDirectory():bool {
        return 
Loco_fs_Locations::getContent()->check$this->path );
    }


    
/**
     * Check if path is under WordPress root directory (ABSPATH) 
     */
    
public function underWordPressDirectory():bool {
        return 
Loco_fs_Locations::getRoot()->check$this->path );
    }


    
/**
     * Check if path is under the global system directory 
     */
    
public function underGlobalDirectory():bool {
        return 
Loco_fs_Locations::getLangs()->check$this->path );
    }


    
/**
     * @return Loco_fs_Directory|null
     */
    
public function getParent():?Loco_fs_Directory {
        
$dir null;
        
$path $this->dirname();
        if( 
'.' !== $path && $this->path !== $path ){ 
            
$dir = new Loco_fs_Directory$path );
            
$dir->cloneWriteContext$this->);
        }
        return 
$dir;
    }


    
/**
     * Copy this file for real
     * @param string $dest new path
     * @throws Loco_error_WriteException
     * @return Loco_fs_File new file
     */
    
public function copy$dest ){
        
$copy = clone $this;
        
$copy->path $dest;
        
$copy->clearStat();
        
$this->getWriteContext()->copy($copy);
        return 
$copy;
    }


    
/**
     * Move/rename this file for real
     * @param Loco_fs_File $dest target file with new path
     * @throws Loco_error_WriteException
     * @return Loco_fs_File original file that should no longer exist
     */
    
public function moveLoco_fs_File $dest ){
        
$this->getWriteContext()->move($dest);
        return 
$this->clearStat();
    }


    
/**
     * Delete this file for real
     * @throws Loco_error_WriteException
     * @return Loco_fs_File
     */
    
public function unlink(){
        
$recursive $this->isDirectory();
        
$this->getWriteContext()->delete$recursive );
        return 
$this->clearStat();
    }


    
/**
     * Copy this object with an alternative file extension
     * @param string $ext new extension
     * @return self
     */
    
public function cloneExtension$ext ){
        return 
$this->cloneBasename$this->filename().'.'.ltrim($ext,'.') );
    }


    
/**
     * Copy this object with an alternative name under the same directory
     * @param string $name new name
     * @return self
     */
    
public function cloneBasename$name ){
        
$file = clone $this;
        
$file->path rtrim($file->dirname(),'/').'/'.$name;
        
$file->info null;
        return 
$file;
    }


    
/**
     * Ensure full parent directory tree exists
     * @return Loco_fs_Directory|null
     */
    
public function createParent(){
        
$dir $this->getParent();
        if( 
$dir instanceof Loco_fs_Directory && ! $dir->exists() ){
            
$dir->mkdir();
        }
        return 
$dir;
    }


    
/**
     * @param string $data file contents
     * @return int number of bytes written to file
     */
    
public function putContentsstring $data ):int {
        
$this->getWriteContext()->putContents($data);
        
$this->clearStat();
        return 
$this->size();
    }


    
/**
     * Establish what part of the WordPress file system this is.
     * Value is that used by WP_Automatic_Updater::should_update.
     * @return string "core", "plugin", "theme" or "translation"
     */
    
public function getUpdateType(){
        
// global languages directory root, and canonical subdirectories
        
$dirpath = (string) ( $this->isDirectory() ? $this $this->getParent() );
        
$sub Loco_fs_Locations::getGlobal()->rel($dirpath);
        if( 
is_string($sub) && '' !== $sub ){
            list(
$root) = explode('/'$sub);
            if( 
'.' === $root || 'themes' === $root || 'plugins' === $root ){
                return 
'translation';
            }
        }
        
// theme and plugin locations can be at any depth
        
else if( $this->underThemeDirectory() ){
            return 
'theme';
        }
        else if( 
$this->underPluginDirectory() ){
            return 
'plugin';
        }
        
// core locations are under WordPress root, but not under wp-content
        
else if( $this->underWordPressDirectory() && ! $this->underContentDirectory() ){
            return 
'core';
        }
        
// else not an update type
        
return '';
    }


    
/**
     * Get MD5 hash of file contents
     */
    
public function md5():string {
        if( 
$this->exists() ) {
            return 
md5_file$this->path );
        }
        else {
            return 
'd41d8cd98f00b204e9800998ecf8427e';
        }
    } 

}