PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / src / Helpers / FileExists.php

FileExists.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at vendor/rosell-dk/webp-convert/src/Helpers/FileExists.php

123 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WebPConvert\Helpers;
4
5 use ExecWithFallback\ExecWithFallback;
6
7 /**
8 * A fileExist function free of deception
9 *
10 * @package WebPConvert
11 * @author Bjørn Rosell <[email protected]>
12 * @since Class available since Release 2.3.0
13 */
14 class FileExists
15 {
16
17 private static $lastWarning;
18
19 /**
20 * A warning handler that registers that a warning has occured and suppresses it.
21 *
22 * The function is a callback used with "set_error_handler".
23 * It is declared public because it needs to be accessible from the point where the warning is triggered.
24 *
25 * @param integer $errno
26 * @param string $errstr
27 * @param string $errfile
28 * @param integer $errline
29 *
30 * @return void
31 */
32 public static function warningHandler($errno, $errstr, $errfile, $errline)
33 {
34 self::$lastWarning = [$errstr, $errno];
35
36 // Suppress the warning by returning void
37 return;
38 }
39
40 /**
41 * A well behaved replacement for file_exist that throws upon failure rather than emmitting a warning.
42 *
43 * @throws \Exception If file_exists threw a warning
44 * @return boolean|null True if file exists. False if it doesn't.
45 */
46 public static function honestFileExists($path)
47 {
48 // There is a challenges here:
49 // We want to suppress warnings, but at the same time we want to know that it happened.
50 // We achieve this by registering an error handler
51 set_error_handler(
52 array('WebPConvert\Helpers\FileExists', "warningHandler"),
53 E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE
54 );
55 self::$lastWarning = null;
56 $found = @file_exists($path);
57
58 // restore previous error handler immediately
59 restore_error_handler();
60
61 // If file_exists returns true, we can rely on there being a file there
62 if ($found) {
63 return true;
64 }
65
66 // file_exists returned false.
67 // this result is only trustworthy if no warning was emitted.
68 if (is_null(self::$lastWarning)) {
69 return false;
70 }
71
72 list($errstr, $errno) = self::$lastWarning;
73 throw new \Exception($errstr, $errno);
74 }
75
76 /**
77 * A fileExist based on an exec call.
78 *
79 * @throws \Exception If exec cannot be called
80 * @return boolean|null True if file exists. False if it doesn't.
81 */
82 public static function fileExistsUsingExec($path)
83 {
84 if (!ExecWithFallback::anyAvailable()) {
85 throw new \Exception(
86 'cannot determine if file exists using exec() or similar - the function is unavailable'
87 );
88 }
89
90 // Lets try to find out by executing "ls path/to/cwebp"
91 ExecWithFallback::exec('ls ' . $path, $output, $returnCode);
92 if (($returnCode == 0) && (isset($output[0]))) {
93 return true;
94 }
95
96 // We assume that "ls" command is general available!
97 // As that failed, we can conclude the file does not exist.
98 return false;
99 }
100
101 /**
102 * A fileExist doing the best it can.
103 *
104 * @throws \Exception If it cannot be determined if the file exists
105 * @return boolean|null True if file exists. False if it doesn't.
106 */
107 public static function fileExistsTryHarder($path)
108 {
109 try {
110 $result = self::honestFileExists($path);
111 } catch (\Exception $e) {
112 try {
113 $result = self::fileExistsUsingExec($path);
114 } catch (\Exception $e) {
115 throw new \Exception('Cannot determine if file exists or not');
116 } catch (\Throwable $e) {
117 throw new \Exception('Cannot determine if file exists or not');
118 }
119 }
120 return $result;
121 }
122 }
123