| 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 |
|