2013-11-01 08:15:58 +08:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 17:58:15 +08:00
|
|
|
* PrivateBin
|
2013-11-01 08:15:58 +08:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 17:58:15 +08:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2013-11-01 08:15:58 +08:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 19:56:52 +08:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2021-04-05 22:44:12 +08:00
|
|
|
* @version 1.3.5
|
2013-11-01 08:15:58 +08:00
|
|
|
*/
|
2016-12-13 01:43:23 +08:00
|
|
|
|
2016-12-13 01:49:08 +08:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-07-21 23:09:48 +08:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
2013-11-01 08:15:58 +08:00
|
|
|
/**
|
2016-08-09 17:54:42 +08:00
|
|
|
* AbstractPersistence
|
2013-11-01 08:15:58 +08:00
|
|
|
*
|
|
|
|
* persists data in PHP files
|
|
|
|
*/
|
2016-08-09 17:54:42 +08:00
|
|
|
abstract class AbstractPersistence
|
2013-11-01 08:15:58 +08:00
|
|
|
{
|
|
|
|
/**
|
2015-08-16 21:55:31 +08:00
|
|
|
* path in which to persist something
|
|
|
|
*
|
2013-11-01 08:15:58 +08:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_path = 'data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public static function setPath($path)
|
|
|
|
{
|
|
|
|
self::$_path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
2016-07-18 21:13:56 +08:00
|
|
|
* @return string
|
2013-11-01 08:15:58 +08:00
|
|
|
*/
|
|
|
|
public static function getPath($filename = null)
|
|
|
|
{
|
2016-07-26 14:19:35 +08:00
|
|
|
if (strlen($filename)) {
|
2015-08-16 00:32:31 +08:00
|
|
|
return self::$_path . DIRECTORY_SEPARATOR . $filename;
|
2016-07-26 14:19:35 +08:00
|
|
|
} else {
|
2013-11-01 08:15:58 +08:00
|
|
|
return self::$_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the file exists
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function _exists($filename)
|
|
|
|
{
|
|
|
|
self::_initialize();
|
2015-08-28 03:41:21 +08:00
|
|
|
return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
|
2013-11-01 08:15:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prepares path for storage
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
2015-08-28 03:41:21 +08:00
|
|
|
* @throws Exception
|
2013-11-01 08:15:58 +08:00
|
|
|
*/
|
|
|
|
protected static function _initialize()
|
|
|
|
{
|
|
|
|
// Create storage directory if it does not exist.
|
2016-07-26 14:19:35 +08:00
|
|
|
if (!is_dir(self::$_path)) {
|
2017-03-25 04:30:08 +08:00
|
|
|
if (!@mkdir(self::$_path, 0700)) {
|
2015-08-28 03:41:21 +08:00
|
|
|
throw new Exception('unable to create directory ' . self::$_path, 10);
|
2016-07-26 14:19:35 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 03:41:21 +08:00
|
|
|
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
|
2016-07-26 14:19:35 +08:00
|
|
|
if (!is_file($file)) {
|
2021-06-05 11:48:17 +08:00
|
|
|
$writtenBytes = 0;
|
|
|
|
if ($fileCreated = @touch($file)) {
|
|
|
|
$writtenBytes = @file_put_contents(
|
|
|
|
$file,
|
|
|
|
'Require all denied' . PHP_EOL,
|
|
|
|
LOCK_EX
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < 19) {
|
2015-08-28 03:41:21 +08:00
|
|
|
throw new Exception('unable to write to file ' . $file, 11);
|
|
|
|
}
|
2013-11-01 08:15:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store the data
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $data
|
2015-08-28 03:41:21 +08:00
|
|
|
* @throws Exception
|
2013-11-01 08:15:58 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function _store($filename, $data)
|
|
|
|
{
|
|
|
|
self::_initialize();
|
2021-06-05 11:48:17 +08:00
|
|
|
$file = self::$_path . DIRECTORY_SEPARATOR . $filename;
|
|
|
|
$fileCreated = true;
|
|
|
|
$writtenBytes = 0;
|
|
|
|
if (!is_file($file)) {
|
|
|
|
$fileCreated = @touch($file);
|
|
|
|
}
|
|
|
|
if ($fileCreated) {
|
|
|
|
$writtenBytes = @file_put_contents($file, $data, LOCK_EX);
|
|
|
|
}
|
|
|
|
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
|
2015-08-28 03:41:21 +08:00
|
|
|
throw new Exception('unable to write to file ' . $file, 13);
|
|
|
|
}
|
2016-06-23 00:08:25 +08:00
|
|
|
@chmod($file, 0640); // protect file access
|
2013-11-01 08:15:58 +08:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|