persistence.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 0.22
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. /**
  15. * persistence
  16. *
  17. * persists data in PHP files
  18. */
  19. abstract class persistence
  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. {
  53. return self::$_path . DIRECTORY_SEPARATOR . $filename;
  54. }
  55. else
  56. {
  57. return self::$_path;
  58. }
  59. }
  60. /**
  61. * checks if the file exists
  62. *
  63. * @access protected
  64. * @static
  65. * @param string $filename
  66. * @return bool
  67. */
  68. protected static function _exists($filename)
  69. {
  70. self::_initialize();
  71. return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
  72. }
  73. /**
  74. * prepares path for storage
  75. *
  76. * @access protected
  77. * @static
  78. * @throws Exception
  79. * @return void
  80. */
  81. protected static function _initialize()
  82. {
  83. // Create storage directory if it does not exist.
  84. if (!is_dir(self::$_path))
  85. if (!@mkdir(self::$_path))
  86. throw new Exception('unable to create directory ' . self::$_path, 10);
  87. // Create .htaccess file if it does not exist.
  88. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  89. if (!is_file($file))
  90. {
  91. $writtenBytes = @file_put_contents(
  92. $file,
  93. 'Allow from none' . PHP_EOL .
  94. 'Deny from all' . PHP_EOL,
  95. LOCK_EX
  96. );
  97. if ($writtenBytes === false || $writtenBytes < 30) {
  98. throw new Exception('unable to write to file ' . $file, 11);
  99. }
  100. }
  101. }
  102. /**
  103. * store the data
  104. *
  105. * @access protected
  106. * @static
  107. * @param string $filename
  108. * @param string $data
  109. * @throws Exception
  110. * @return string
  111. */
  112. protected static function _store($filename, $data)
  113. {
  114. self::_initialize();
  115. $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
  116. $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
  117. if ($writtenBytes === false || $writtenBytes < strlen($data))
  118. {
  119. throw new Exception('unable to write to file ' . $file, 13);
  120. }
  121. @chmod($file, 0640); // protect file access
  122. return $file;
  123. }
  124. }