DataStore.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. namespace PrivateBin\Persistence;
  13. use Exception;
  14. use PrivateBin\Json;
  15. /**
  16. * DataStore
  17. *
  18. * Handles data storage for Data\Filesystem.
  19. */
  20. class DataStore extends AbstractPersistence
  21. {
  22. /**
  23. * First line in JSON files, to hide contents
  24. *
  25. * @const string
  26. */
  27. const PROTECTION_LINE = '<?php http_response_code(403); /*';
  28. /**
  29. * store the data
  30. *
  31. * @access public
  32. * @static
  33. * @param string $filename
  34. * @param array $data
  35. * @return bool
  36. */
  37. public static function store($filename, $data)
  38. {
  39. $path = self::getPath();
  40. if (strpos($filename, $path) === 0) {
  41. $filename = substr($filename, strlen($path));
  42. }
  43. try {
  44. self::_store($filename, self::PROTECTION_LINE . PHP_EOL . Json::encode($data));
  45. return true;
  46. } catch (Exception $e) {
  47. return false;
  48. }
  49. }
  50. /**
  51. * get the data
  52. *
  53. * @access public
  54. * @static
  55. * @param string $filename
  56. * @return array $data
  57. */
  58. public static function get($filename)
  59. {
  60. return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL)));
  61. }
  62. }