AbstractPersistence.php 3.6 KB

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