1
0

AbstractPersistence.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = 0;
  87. if ($fileCreated = @touch($file)) {
  88. $writtenBytes = @file_put_contents(
  89. $file,
  90. 'Require all denied' . PHP_EOL,
  91. LOCK_EX
  92. );
  93. }
  94. if ($fileCreated === false || $writtenBytes === false || $writtenBytes < 19) {
  95. throw new Exception('unable to write to file ' . $file, 11);
  96. }
  97. }
  98. }
  99. /**
  100. * store the data
  101. *
  102. * @access protected
  103. * @static
  104. * @param string $filename
  105. * @param string $data
  106. * @throws Exception
  107. * @return string
  108. */
  109. protected static function _store($filename, $data)
  110. {
  111. self::_initialize();
  112. $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
  113. $fileCreated = true;
  114. $writtenBytes = 0;
  115. if (!is_file($file)) {
  116. $fileCreated = @touch($file);
  117. }
  118. if ($fileCreated) {
  119. $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
  120. }
  121. if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
  122. throw new Exception('unable to write to file ' . $file, 13);
  123. }
  124. @chmod($file, 0640); // protect file access
  125. return $file;
  126. }
  127. }