]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/FileUtils.php
3 require_once 'exceptions/IOException.php';
8 * Utility class for file manipulation.
15 protected static $phpPrefix = '<?php /* ';
20 protected static $phpSuffix = ' */ ?>';
23 * Write data into a file (Shaarli database format).
24 * The data is stored in a PHP file, as a comment, in compressed base64 format.
26 * The file will be created if it doesn't exist.
28 * @param string $file File path.
29 * @param mixed $content Content to write.
31 * @return int|bool Number of bytes written or false if it fails.
33 * @throws IOException The destination file can't be written.
35 public static function writeFlatDB($file, $content)
37 if (is_file($file) && !is_writeable($file)) {
38 // The datastore exists but is not writeable
39 throw new IOException($file);
40 } else if (!is_file($file) && !is_writeable(dirname($file))) {
41 // The datastore does not exist and its parent directory is not writeable
42 throw new IOException(dirname($file));
45 return file_put_contents(
47 self
::$phpPrefix.base64_encode(gzdeflate(serialize($content))).self
::$phpSuffix
52 * Read data from a file containing Shaarli database format content.
54 * If the file isn't readable or doesn't exist, default data will be returned.
56 * @param string $file File path.
57 * @param mixed $default The default value to return if the file isn't readable.
59 * @return mixed The content unserialized, or default if the file isn't readable, or false if it fails.
61 public static function readFlatDB($file, $default = null)
63 // Note that gzinflate is faster than gzuncompress.
64 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
65 if (! is_readable($file)) {
69 $data = file_get_contents($file);
77 substr($data, strlen(self
::$phpPrefix), -strlen(self
::$phpSuffix))