persistence.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /**
  13. * persistence
  14. *
  15. * persists data in PHP files
  16. */
  17. abstract class persistence
  18. {
  19. /**
  20. * path in which to persist something
  21. *
  22. * @access private
  23. * @static
  24. * @var string
  25. */
  26. private static $_path = 'data';
  27. /**
  28. * set the path
  29. *
  30. * @access public
  31. * @static
  32. * @param string $path
  33. * @return void
  34. */
  35. public static function setPath($path)
  36. {
  37. self::$_path = $path;
  38. }
  39. /**
  40. * get the path
  41. *
  42. * @access public
  43. * @static
  44. * @param string $filename
  45. * @return string
  46. */
  47. public static function getPath($filename = null)
  48. {
  49. if (strlen($filename))
  50. {
  51. return self::$_path . DIRECTORY_SEPARATOR . $filename;
  52. }
  53. else
  54. {
  55. return self::$_path;
  56. }
  57. }
  58. /**
  59. * checks if the file exists
  60. *
  61. * @access protected
  62. * @static
  63. * @param string $filename
  64. * @return bool
  65. */
  66. protected static function _exists($filename)
  67. {
  68. self::_initialize();
  69. return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
  70. }
  71. /**
  72. * prepares path for storage
  73. *
  74. * @access protected
  75. * @static
  76. * @throws Exception
  77. * @return void
  78. */
  79. protected static function _initialize()
  80. {
  81. // Create storage directory if it does not exist.
  82. if (!is_dir(self::$_path))
  83. if (!@mkdir(self::$_path))
  84. throw new Exception('unable to create directory ' . self::$_path, 10);
  85. // Create .htaccess file if it does not exist.
  86. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  87. if (!is_file($file))
  88. {
  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. {
  117. throw new Exception('unable to write to file ' . $file, 13);
  118. }
  119. @chmod($file, 0640); // protect file access
  120. return $file;
  121. }
  122. }