1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PrivateBin |
4
|
|
|
* |
5
|
|
|
* a zero-knowledge paste bin |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/PrivateBin/PrivateBin |
8
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) |
9
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License |
10
|
|
|
* @version 1.1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PrivateBin\Persistence; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use PrivateBin\Json; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DataStore |
20
|
|
|
* |
21
|
|
|
* Handles data storage for Data\Filesystem. |
22
|
|
|
*/ |
23
|
|
|
class DataStore extends AbstractPersistence |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* first line in file, to protect its contents |
27
|
|
|
* |
28
|
|
|
* @const string |
29
|
|
|
*/ |
30
|
|
|
const PROTECTION_LINE = '<?php http_response_code(403); /*'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* store the data |
34
|
|
|
* |
35
|
|
|
* @access public |
36
|
|
|
* @static |
37
|
|
|
* @param string $filename |
38
|
|
|
* @param array $data |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
45 |
|
public static function store($filename, $data) |
42
|
|
|
{ |
43
|
45 |
|
$path = self::getPath(); |
44
|
45 |
|
if (strpos($filename, $path) === 0) { |
45
|
45 |
|
$filename = substr($filename, strlen($path)); |
46
|
|
|
} |
47
|
|
|
try { |
48
|
45 |
|
self::_store($filename, self::PROTECTION_LINE . PHP_EOL . Json::encode($data)); |
49
|
44 |
|
return true; |
50
|
2 |
|
} catch (Exception $e) { |
51
|
2 |
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* get the data |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @static |
60
|
|
|
* @param string $filename |
61
|
|
|
* @return stdClass|false $data |
62
|
|
|
*/ |
63
|
33 |
|
public static function get($filename) |
64
|
|
|
{ |
65
|
33 |
|
return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* rename a file, prepending the protection line at the beginning |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @static |
73
|
|
|
* @param string $srcFile |
74
|
|
|
* @param string $destFile |
75
|
|
|
* @param string $prefix (optional) |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
3 |
|
public static function prependRename($srcFile, $destFile, $prefix = '') |
79
|
|
|
{ |
80
|
|
|
// don't overwrite already converted file |
81
|
3 |
|
if (!is_readable($destFile)) { |
82
|
3 |
|
$handle = fopen($srcFile, 'r', false, stream_context_create()); |
83
|
3 |
|
file_put_contents($destFile, $prefix . DataStore::PROTECTION_LINE . PHP_EOL); |
84
|
3 |
|
file_put_contents($destFile, $handle, FILE_APPEND); |
85
|
3 |
|
fclose($handle); |
86
|
|
|
} |
87
|
3 |
|
unlink($srcFile); |
88
|
3 |
|
} |
89
|
|
|
} |
90
|
|
|
|