AbstractPersistence.php 2.9 KB

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