2013-11-01 00:15:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ZeroBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-11-09 20:39:42 +00:00
|
|
|
* @version 0.22
|
2013-11-01 00:15:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* persistence
|
|
|
|
*
|
|
|
|
* persists data in PHP files
|
|
|
|
*/
|
|
|
|
abstract class persistence
|
|
|
|
{
|
|
|
|
/**
|
2015-08-16 13:55:31 +00:00
|
|
|
* path in which to persist something
|
|
|
|
*
|
2013-11-01 00:15:58 +00:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_path = 'data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setPath($path)
|
|
|
|
{
|
|
|
|
self::$_path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function getPath($filename = null)
|
|
|
|
{
|
|
|
|
if(strlen($filename)) {
|
2015-08-15 16:32:31 +00:00
|
|
|
return self::$_path . DIRECTORY_SEPARATOR . $filename;
|
2013-11-01 00:15:58 +00:00
|
|
|
} else {
|
|
|
|
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-27 19:41:21 +00:00
|
|
|
return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
|
2013-11-01 00:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prepares path for storage
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
2015-08-27 19:41:21 +00:00
|
|
|
* @throws Exception
|
2013-11-01 00:15:58 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function _initialize()
|
|
|
|
{
|
|
|
|
// Create storage directory if it does not exist.
|
2015-08-27 19:41:21 +00:00
|
|
|
if (!is_dir(self::$_path))
|
|
|
|
if (!@mkdir(self::$_path))
|
|
|
|
throw new Exception('unable to create directory ' . self::$_path, 10);
|
2013-11-01 00:15:58 +00:00
|
|
|
|
|
|
|
// Create .htaccess file if it does not exist.
|
2015-08-27 19:41:21 +00:00
|
|
|
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
|
2013-11-01 00:15:58 +00:00
|
|
|
if (!is_file($file))
|
|
|
|
{
|
2015-08-27 19:41:21 +00:00
|
|
|
$writtenBytes = @file_put_contents(
|
2013-11-01 00:15:58 +00:00
|
|
|
$file,
|
|
|
|
'Allow from none' . PHP_EOL .
|
2015-08-16 10:27:06 +00:00
|
|
|
'Deny from all'. PHP_EOL,
|
|
|
|
LOCK_EX
|
2013-11-01 00:15:58 +00:00
|
|
|
);
|
2015-08-27 19:41:21 +00:00
|
|
|
if ($writtenBytes === false || $writtenBytes < 30) {
|
|
|
|
throw new Exception('unable to write to file ' . $file, 11);
|
|
|
|
}
|
2013-11-01 00:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store the data
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $data
|
2015-08-27 19:41:21 +00:00
|
|
|
* @throws Exception
|
2013-11-01 00:15:58 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function _store($filename, $data)
|
|
|
|
{
|
|
|
|
self::_initialize();
|
2015-08-27 19:41:21 +00:00
|
|
|
$file = self::$_path . DIRECTORY_SEPARATOR . $filename;
|
|
|
|
$writtenBytes = @file_put_contents($file, $data, LOCK_EX);
|
2016-06-21 15:18:11 +00:00
|
|
|
chmod($file, 0640); // protect file access
|
2015-08-27 19:41:21 +00:00
|
|
|
if ($writtenBytes === false || $writtenBytes < strlen($data)) {
|
|
|
|
throw new Exception('unable to write to file ' . $file, 13);
|
|
|
|
}
|
2013-11-01 00:15:58 +00:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|