2013-10-31 20:15:58 -04: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
|
2013-07-04 19:14:23 -04:00
|
|
|
* @version 0.19
|
2013-10-31 20:15:58 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* persistence
|
|
|
|
*
|
|
|
|
* persists data in PHP files
|
|
|
|
*/
|
|
|
|
abstract class persistence
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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 12:32:31 -04:00
|
|
|
return self::$_path . DIRECTORY_SEPARATOR . $filename;
|
2013-10-31 20:15:58 -04: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();
|
|
|
|
return is_file(self::$_path . '/' . $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prepares path for storage
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function _initialize()
|
|
|
|
{
|
|
|
|
// Create storage directory if it does not exist.
|
|
|
|
if (!is_dir(self::$_path)) mkdir(self::$_path, 0705);
|
|
|
|
|
|
|
|
// Create .htaccess file if it does not exist.
|
|
|
|
$file = self::$_path . '/.htaccess';
|
|
|
|
if (!is_file($file))
|
|
|
|
{
|
|
|
|
file_put_contents(
|
|
|
|
$file,
|
|
|
|
'Allow from none' . PHP_EOL .
|
2015-08-16 06:27:06 -04:00
|
|
|
'Deny from all'. PHP_EOL,
|
|
|
|
LOCK_EX
|
2013-10-31 20:15:58 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store the data
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $data
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function _store($filename, $data)
|
|
|
|
{
|
|
|
|
self::_initialize();
|
|
|
|
$file = self::$_path . '/' . $filename;
|
2013-09-28 07:39:38 -04:00
|
|
|
file_put_contents($file, $data, LOCK_EX);
|
2013-10-31 20:15:58 -04:00
|
|
|
chmod($file, 0705);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|