AbstractPersistence.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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)) {
  83. throw new Exception('unable to create directory ' . self::$_path, 10);
  84. }
  85. }
  86. // Create .htaccess file if it does not exist.
  87. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  88. if (!is_file($file)) {
  89. $writtenBytes = @file_put_contents(
  90. $file,
  91. 'Allow from none' . PHP_EOL .
  92. 'Deny from all' . PHP_EOL,
  93. LOCK_EX
  94. );
  95. if ($writtenBytes === false || $writtenBytes < 30) {
  96. throw new Exception('unable to write to file ' . $file, 11);
  97. }
  98. }
  99. }
  100. /**
  101. * store the data
  102. *
  103. * @access protected
  104. * @static
  105. * @param string $filename
  106. * @param string $data
  107. * @throws Exception
  108. * @return string
  109. */
  110. protected static function _store($filename, $data)
  111. {
  112. self::_initialize();
  113. $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
  114. $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
  115. if ($writtenBytes === false || $writtenBytes < strlen($data)) {
  116. throw new Exception('unable to write to file ' . $file, 13);
  117. }
  118. @chmod($file, 0640); // protect file access
  119. return $file;
  120. }
  121. }