3 namespace Shaarli\Helper
;
5 use Shaarli\Exceptions\IOException
;
10 * Utility class for file manipulation.
17 protected static $phpPrefix = '<?php /* ';
22 protected static $phpSuffix = ' */ ?>';
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.
28 * The file will be created if it doesn't exist.
30 * @param string $file File path.
31 * @param mixed $content Content to write.
33 * @return int|bool Number of bytes written or false if it fails.
35 * @throws IOException The destination file can't be written.
37 public static function writeFlatDB($file, $content)
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));
47 return file_put_contents(
49 self
::$phpPrefix . base64_encode(gzdeflate(serialize($content))) . self
::$phpSuffix
54 * Read data from a file containing Shaarli database format content.
56 * If the file isn't readable or doesn't exist, default data will be returned.
58 * @param string $file File path.
59 * @param mixed $default The default value to return if the file isn't readable.
61 * @return mixed The content unserialized, or default if the file isn't readable, or false if it fails.
63 public static function readFlatDB($file, $default = null)
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)) {
71 $data = file_get_contents($file);
79 substr($data, strlen(self
::$phpPrefix), -strlen(self
::$phpSuffix))
86 * Recursively deletes a folder content, and deletes itself optionally.
87 * If an excluded file is found, folders won't be deleted.
89 * Additional security: raise an exception if it tries to delete a folder outside of Shaarli directory.
92 * @param bool $selfDelete Delete the provided folder if true, only its content if false.
93 * @param array $exclude
95 public static function clearFolder(string $path, bool $selfDelete, array $exclude = []): bool
100 throw new IOException(t('Provided path is not a directory.'));
103 if (!static::isPathInShaarliFolder($path)) {
104 throw new IOException(t('Trying to delete a folder outside of Shaarli path.'));
107 foreach (new \
DirectoryIterator($path) as $file) {
112 if (in_array($file->getBasename(), $exclude, true)) {
117 if ($file->isFile()) {
118 unlink($file->getPathname());
119 } elseif($file->isDir()) {
120 $skipped = static::clearFolder($file->getRealPath(), true, $exclude) || $skipped;
124 if ($selfDelete && !$skipped) {
132 * Checks that the given path is inside Shaarli directory.
134 public static function isPathInShaarliFolder(string $path): bool
136 $rootDirectory = dirname(dirname(dirname(__FILE__
)));
138 return strpos(realpath($path), $rootDirectory) !== false;