diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
commit | af50eba28a7bd286de4c8c9ee6dc5216b915d149 (patch) | |
tree | ffa30a9358e82d27be75d8fc5e57f3c8820dc6d3 /application/FileUtils.php | |
parent | b6f678a5a1d15acf284ebcec16c905e976671ce1 (diff) | |
parent | 1409f1c89a7ca01456ae2dcd6357d296e2b99f5a (diff) | |
download | Shaarli-latest.tar.gz Shaarli-latest.tar.zst Shaarli-latest.zip |
Merge tag 'v0.12.1' into latestlatest
v0.12.1
Diffstat (limited to 'application/FileUtils.php')
-rw-r--r-- | application/FileUtils.php | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/application/FileUtils.php b/application/FileUtils.php deleted file mode 100644 index 30560bfc..00000000 --- a/application/FileUtils.php +++ /dev/null | |||
@@ -1,84 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli; | ||
4 | |||
5 | use Shaarli\Exceptions\IOException; | ||
6 | |||
7 | /** | ||
8 | * Class FileUtils | ||
9 | * | ||
10 | * Utility class for file manipulation. | ||
11 | */ | ||
12 | class FileUtils | ||
13 | { | ||
14 | /** | ||
15 | * @var string | ||
16 | */ | ||
17 | protected static $phpPrefix = '<?php /* '; | ||
18 | |||
19 | /** | ||
20 | * @var string | ||
21 | */ | ||
22 | protected static $phpSuffix = ' */ ?>'; | ||
23 | |||
24 | /** | ||
25 | * Write data into a file (Shaarli database format). | ||
26 | * The data is stored in a PHP file, as a comment, in compressed base64 format. | ||
27 | * | ||
28 | * The file will be created if it doesn't exist. | ||
29 | * | ||
30 | * @param string $file File path. | ||
31 | * @param mixed $content Content to write. | ||
32 | * | ||
33 | * @return int|bool Number of bytes written or false if it fails. | ||
34 | * | ||
35 | * @throws IOException The destination file can't be written. | ||
36 | */ | ||
37 | public static function writeFlatDB($file, $content) | ||
38 | { | ||
39 | if (is_file($file) && !is_writeable($file)) { | ||
40 | // The datastore exists but is not writeable | ||
41 | throw new IOException($file); | ||
42 | } elseif (!is_file($file) && !is_writeable(dirname($file))) { | ||
43 | // The datastore does not exist and its parent directory is not writeable | ||
44 | throw new IOException(dirname($file)); | ||
45 | } | ||
46 | |||
47 | return file_put_contents( | ||
48 | $file, | ||
49 | self::$phpPrefix . base64_encode(gzdeflate(serialize($content))) . self::$phpSuffix | ||
50 | ); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Read data from a file containing Shaarli database format content. | ||
55 | * | ||
56 | * If the file isn't readable or doesn't exist, default data will be returned. | ||
57 | * | ||
58 | * @param string $file File path. | ||
59 | * @param mixed $default The default value to return if the file isn't readable. | ||
60 | * | ||
61 | * @return mixed The content unserialized, or default if the file isn't readable, or false if it fails. | ||
62 | */ | ||
63 | public static function readFlatDB($file, $default = null) | ||
64 | { | ||
65 | // Note that gzinflate is faster than gzuncompress. | ||
66 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 | ||
67 | if (!is_readable($file)) { | ||
68 | return $default; | ||
69 | } | ||
70 | |||
71 | $data = file_get_contents($file); | ||
72 | if ($data == '') { | ||
73 | return $default; | ||
74 | } | ||
75 | |||
76 | return unserialize( | ||
77 | gzinflate( | ||
78 | base64_decode( | ||
79 | substr($data, strlen(self::$phpPrefix), -strlen(self::$phpSuffix)) | ||
80 | ) | ||
81 | ) | ||
82 | ); | ||
83 | } | ||
84 | } | ||