persistence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.19
  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 void
  46. */
  47. public static function getPath($filename = null)
  48. {
  49. if(strlen($filename)) {
  50. return self::$_path . DIRECTORY_SEPARATOR . $filename;
  51. } else {
  52. return self::$_path;
  53. }
  54. }
  55. /**
  56. * checks if the file exists
  57. *
  58. * @access protected
  59. * @static
  60. * @param string $filename
  61. * @return bool
  62. */
  63. protected static function _exists($filename)
  64. {
  65. self::_initialize();
  66. return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
  67. }
  68. /**
  69. * prepares path for storage
  70. *
  71. * @access protected
  72. * @static
  73. * @throws Exception
  74. * @return void
  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))
  81. throw new Exception('unable to create directory ' . self::$_path, 10);
  82. // Create .htaccess file if it does not exist.
  83. $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
  84. if (!is_file($file))
  85. {
  86. $writtenBytes = @file_put_contents(
  87. $file,
  88. 'Allow from none' . PHP_EOL .
  89. 'Deny from all'. PHP_EOL,
  90. LOCK_EX
  91. );
  92. if ($writtenBytes === false || $writtenBytes < 30) {
  93. throw new Exception('unable to write to file ' . $file, 11);
  94. }
  95. }
  96. }
  97. /**
  98. * store the data
  99. *
  100. * @access protected
  101. * @static
  102. * @param string $filename
  103. * @param string $data
  104. * @throws Exception
  105. * @return string
  106. */
  107. protected static function _store($filename, $data)
  108. {
  109. self::_initialize();
  110. $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
  111. $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
  112. if ($writtenBytes === false || $writtenBytes < strlen($data)) {
  113. throw new Exception('unable to write to file ' . $file, 13);
  114. }
  115. return $file;
  116. }
  117. }