AbstractPersistence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  15. * AbstractPersistence
  16. *
  17. * persists data in PHP files
  18. */
  19. abstract class AbstractPersistence
  20. {
  21. /**
  22. * path in which to persist something
  23. *
  24. * @access private
  25. * @static
  26. * @var string
  27. */
  28. private static $_path = 'data';
  29. /**
  30. * set the path
  31. *
  32. * @access public
  33. * @static
  34. * @param string $path
  35. * @return void
  36. */
  37. public static function setPath($path)
  38. {
  39. self::$_path = $path;
  40. }
  41. /**
  42. * get the path
  43. *
  44. * @access public
  45. * @static
  46. * @param string $filename
  47. * @return string
  48. */
  49. public static function getPath($filename = null)
  50. {
  51. if (strlen($filename)) {
  52. return self::$_path . DIRECTORY_SEPARATOR . $filename;
  53. } else {
  54. return self::$_path;
  55. }
  56. }
  57. /**
  58. * checks if the file exists
  59. *
  60. * @access protected
  61. * @static
  62. * @param string $filename
  63. * @return bool
  64. */
  65. protected static function _exists($filename)
  66. {
  67. self::_initialize();
  68. return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
  69. }
  70. /**
  71. * prepares path for storage
  72. *
  73. * @access protected
  74. * @static
  75. * @throws Exception
  76. * @return void
  77. */
  78. protected static function _initialize()
  79. {
  80. // Create storage directory if it does not exist.
  81. if (!is_dir(self::$_path)) {
  82. if (!@mkdir(self::$_path, 0700)) {
  83. throw new Exception('unable to create directory ' . self::$_path, 10);
  84. }
  85. }
  86. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  87. if (!is_file($file)) {
  88. $writtenBytes = @file_put_contents(
  89. $file,
  90. 'Require all denied' . PHP_EOL,
  91. LOCK_EX
  92. );
  93. if ($writtenBytes === false || $writtenBytes < 19) {
  94. throw new Exception('unable to write to file ' . $file, 11);
  95. }
  96. }
  97. }
  98. /**
  99. * store the data
  100. *
  101. * @access protected
  102. * @static
  103. * @param string $filename
  104. * @param string $data
  105. * @throws Exception
  106. * @return string
  107. */
  108. protected static function _store($filename, $data)
  109. {
  110. self::_initialize();
  111. $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
  112. $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
  113. if ($writtenBytes === false || $writtenBytes < strlen($data)) {
  114. throw new Exception('unable to write to file ' . $file, 13);
  115. }
  116. @chmod($file, 0640); // protect file access
  117. return $file;
  118. }
  119. }