2017-03-25 06:42:11 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
|
|
|
* @version 1.1
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PrivateBin\Persistence;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PrivateBin\Json;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataStore
|
|
|
|
*
|
|
|
|
* Handles data storage for Data\Filesystem.
|
|
|
|
*/
|
|
|
|
class DataStore extends AbstractPersistence
|
|
|
|
{
|
2017-10-08 17:03:17 +08:00
|
|
|
/**
|
2017-10-09 01:16:09 +08:00
|
|
|
* first line in file, to protect its contents
|
2017-10-08 17:03:17 +08:00
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const PROTECTION_LINE = '<?php http_response_code(403); /*';
|
|
|
|
|
2017-03-25 06:42:11 +08:00
|
|
|
/**
|
|
|
|
* store the data
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
2017-03-25 06:54:37 +08:00
|
|
|
* @param array $data
|
2017-03-25 06:42:11 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function store($filename, $data)
|
|
|
|
{
|
|
|
|
$path = self::getPath();
|
|
|
|
if (strpos($filename, $path) === 0) {
|
|
|
|
$filename = substr($filename, strlen($path));
|
|
|
|
}
|
|
|
|
try {
|
2017-10-08 17:03:17 +08:00
|
|
|
self::_store($filename, self::PROTECTION_LINE . PHP_EOL . Json::encode($data));
|
2017-03-25 06:42:11 +08:00
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 17:31:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the data
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
2018-11-19 20:07:25 +08:00
|
|
|
* @return \stdClass|false $data
|
2017-10-08 17:31:41 +08:00
|
|
|
*/
|
|
|
|
public static function get($filename)
|
|
|
|
{
|
2019-05-09 04:11:21 +08:00
|
|
|
return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL)), true);
|
2017-10-08 17:31:41 +08:00
|
|
|
}
|
2017-10-09 01:16:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* rename a file, prepending the protection line at the beginning
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $srcFile
|
|
|
|
* @param string $destFile
|
|
|
|
* @param string $prefix (optional)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function prependRename($srcFile, $destFile, $prefix = '')
|
|
|
|
{
|
|
|
|
// don't overwrite already converted file
|
|
|
|
if (!is_readable($destFile)) {
|
|
|
|
$handle = fopen($srcFile, 'r', false, stream_context_create());
|
2017-10-09 01:23:33 +08:00
|
|
|
file_put_contents($destFile, $prefix . self::PROTECTION_LINE . PHP_EOL);
|
2017-10-09 01:16:09 +08:00
|
|
|
file_put_contents($destFile, $handle, FILE_APPEND);
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
unlink($srcFile);
|
|
|
|
}
|
2017-03-25 06:42:11 +08:00
|
|
|
}
|