| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
/** |
| 6 |
* Read and write an existing file. |
| 7 |
*/ |
| 8 |
class SQLite_Object_Cache_File { |
| 9 |
|
| 10 |
/** |
| 11 |
* Read data from file. |
| 12 |
* |
| 13 |
* @param string $filename The pathname of the file to read. |
| 14 |
* |
| 15 |
* @return bool|string The file's contents. Empty string if the file doesn't already exist. false if it is not readable. |
| 16 |
*/ |
| 17 |
public static function read( $filename ) { |
| 18 |
if ( ! file_exists( $filename ) ) { |
| 19 |
return ''; |
| 20 |
} |
| 21 |
|
| 22 |
if ( ! is_readable( $filename ) ) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
$content = file_get_contents( $filename ); |
| 27 |
return self::remove_zero_space( $content ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Save data to file. |
| 32 |
* |
| 33 |
* @param string $filename |
| 34 |
* @param string $data |
| 35 |
* |
| 36 |
* @return bool True if the save succeeded. |
| 37 |
*/ |
| 38 |
public static function save( $filename, $data ) { |
| 39 |
|
| 40 |
if ( ! file_exists( $filename ) ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
$data = self::remove_zero_space( $data ); |
| 44 |
$ret = file_put_contents( $filename, $data, LOCK_EX ); |
| 45 |
|
| 46 |
return ( false !== $ret ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Remove Unicode zero-width spaced <200b><200c> and BOMs |
| 51 |
* |
| 52 |
* @param array|string $content |
| 53 |
* |
| 54 |
* @return array|string|string[] |
| 55 |
*/ |
| 56 |
public static function remove_zero_space( $content ) { |
| 57 |
if ( is_array( $content ) ) { |
| 58 |
$content = array_map( __CLASS__ . '::remove_zero_space', $content ); |
| 59 |
return $content; |
| 60 |
} |
| 61 |
|
| 62 |
// Remove UTF-8 BOM if present |
| 63 |
if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) { |
| 64 |
$content = substr( $content, 3 ); |
| 65 |
} |
| 66 |
|
| 67 |
$content = str_replace( "\xe2\x80\x8b", '', $content ); |
| 68 |
$content = str_replace( "\xe2\x80\x8c", '', $content ); |
| 69 |
$content = str_replace( "\xe2\x80\x8d", '', $content ); |
| 70 |
|
| 71 |
return $content; |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|