2012-04-29 13:15:06 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 05:58:15 -04:00
|
|
|
* PrivateBin
|
2012-04-29 13:15:06 -04:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-29 13:15:06 -04:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 07:56:52 -04:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2022-12-10 23:00:19 -05:00
|
|
|
* @version 1.5.0
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:49:08 -05:00
|
|
|
namespace PrivateBin\Data;
|
2016-08-09 05:54:42 -04:00
|
|
|
|
2021-06-07 00:53:15 -04:00
|
|
|
use Exception;
|
|
|
|
use PrivateBin\Json;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2012-04-29 13:15:06 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* Filesystem
|
2012-04-29 13:15:06 -04:00
|
|
|
*
|
2016-08-09 05:54:42 -04:00
|
|
|
* Model for filesystem data access, implemented as a singleton.
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class Filesystem extends AbstractData
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-11-17 23:36:06 -05:00
|
|
|
/**
|
|
|
|
* glob() pattern of the two folder levels and the paste files under the
|
|
|
|
* configured path. Needs to return both files with and without .php suffix,
|
|
|
|
* so they can be hardened by _prependRename(), which is hooked into exists().
|
2022-11-17 23:38:37 -05:00
|
|
|
*
|
2022-11-17 23:36:06 -05:00
|
|
|
* > Note that wildcard patterns are not regular expressions, although they
|
|
|
|
* > are a bit similar.
|
2022-11-17 23:38:37 -05:00
|
|
|
*
|
2022-11-17 23:36:06 -05:00
|
|
|
* @link https://man7.org/linux/man-pages/man7/glob.7.html
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const PASTE_FILE_PATTERN = DIRECTORY_SEPARATOR . '[a-f0-9][a-f0-9]' .
|
|
|
|
DIRECTORY_SEPARATOR . '[a-f0-9][a-f0-9]' . DIRECTORY_SEPARATOR .
|
|
|
|
'[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]' .
|
|
|
|
'[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]*';
|
|
|
|
|
2021-06-07 00:53:15 -04:00
|
|
|
/**
|
2021-06-14 00:44:30 -04:00
|
|
|
* first line in paste or comment files, to protect their contents from browsing exposed data directories
|
2021-06-07 00:53:15 -04:00
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const PROTECTION_LINE = '<?php http_response_code(403); /*';
|
|
|
|
|
2021-06-14 00:44:30 -04:00
|
|
|
/**
|
|
|
|
* line in generated .htaccess files, to protect exposed directories from being browsable on apache web servers
|
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
|
|
|
const HTACCESS_LINE = 'Require all denied';
|
|
|
|
|
2021-06-07 00:53:15 -04:00
|
|
|
/**
|
|
|
|
* path in which to persist something
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private $_path = 'data';
|
2021-06-07 00:53:15 -04:00
|
|
|
|
2012-04-29 13:15:06 -04:00
|
|
|
/**
|
2022-10-27 19:01:02 -04:00
|
|
|
* instantiates a new Filesystem data backend
|
2012-04-29 13:15:06 -04:00
|
|
|
*
|
|
|
|
* @access public
|
2015-08-16 09:55:31 -04:00
|
|
|
* @param array $options
|
2022-10-27 19:01:02 -04:00
|
|
|
* @return
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
public function __construct(array $options)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2012-04-30 07:58:29 -04:00
|
|
|
// if given update the data directory
|
|
|
|
if (
|
2015-08-27 15:41:21 -04:00
|
|
|
is_array($options) &&
|
|
|
|
array_key_exists('dir', $options)
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_path = $options['dir'];
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param array $paste
|
2015-09-03 16:55:36 -04:00
|
|
|
* @return bool
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function create($pasteid, array $paste)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$storagedir = $this->_dataid2path($pasteid);
|
2017-10-08 05:03:17 -04:00
|
|
|
$file = $storagedir . $pasteid . '.php';
|
2017-03-24 18:42:11 -04:00
|
|
|
if (is_file($file)) {
|
2016-07-26 02:19:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_dir($storagedir)) {
|
2016-08-09 05:54:42 -04:00
|
|
|
mkdir($storagedir, 0700, true);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_store($file, $paste);
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
2019-05-08 16:11:21 -04:00
|
|
|
* @return array|false
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
2021-06-15 23:50:41 -04:00
|
|
|
if (
|
|
|
|
!$this->exists($pasteid) ||
|
2022-10-27 19:01:02 -04:00
|
|
|
!$paste = $this->_get($this->_dataid2path($pasteid) . $pasteid . '.php')
|
2021-06-15 23:50:41 -04:00
|
|
|
) {
|
2016-07-26 02:19:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-15 23:50:41 -04:00
|
|
|
return self::upgradePreV1Format($paste);
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a paste and its discussion.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$pastedir = $this->_dataid2path($pasteid);
|
2016-08-09 05:54:42 -04:00
|
|
|
if (is_dir($pastedir)) {
|
|
|
|
// Delete the paste itself.
|
2017-10-08 05:03:17 -04:00
|
|
|
if (is_file($pastedir . $pasteid . '.php')) {
|
|
|
|
unlink($pastedir . $pasteid . '.php');
|
2016-08-09 05:54:42 -04:00
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
// Delete discussion if it exists.
|
2022-10-27 19:01:02 -04:00
|
|
|
$discdir = $this->_dataid2discussionpath($pasteid);
|
2016-08-09 05:54:42 -04:00
|
|
|
if (is_dir($discdir)) {
|
|
|
|
// Delete all files in discussion directory
|
|
|
|
$dir = dir($discdir);
|
|
|
|
while (false !== ($filename = $dir->read())) {
|
|
|
|
if (is_file($discdir . $filename)) {
|
|
|
|
unlink($discdir . $filename);
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2016-08-09 05:54:42 -04:00
|
|
|
$dir->close();
|
|
|
|
rmdir($discdir);
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a paste exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 08:44:17 -04:00
|
|
|
* @param string $pasteid
|
2016-08-09 07:07:11 -04:00
|
|
|
* @return bool
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2019-05-10 15:52:14 -04:00
|
|
|
public function exists($pasteid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$basePath = $this->_dataid2path($pasteid) . $pasteid;
|
2017-10-08 05:03:17 -04:00
|
|
|
$pastePath = $basePath . '.php';
|
|
|
|
// convert to PHP protected files if needed
|
|
|
|
if (is_readable($basePath)) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_prependRename($basePath, $pastePath);
|
2017-10-08 05:03:17 -04:00
|
|
|
|
|
|
|
// convert comments, too
|
2022-10-27 19:01:02 -04:00
|
|
|
$discdir = $this->_dataid2discussionpath($pasteid);
|
2017-10-08 05:03:17 -04:00
|
|
|
if (is_dir($discdir)) {
|
|
|
|
$dir = dir($discdir);
|
|
|
|
while (false !== ($filename = $dir->read())) {
|
|
|
|
if (substr($filename, -4) !== '.php' && strlen($filename) >= 16) {
|
|
|
|
$commentFilename = $discdir . $filename . '.php';
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_prependRename($discdir . $filename, $commentFilename);
|
2017-10-08 05:03:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is_readable($pastePath);
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment in a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
|
|
|
* @param array $comment
|
2015-09-03 16:55:36 -04:00
|
|
|
* @return bool
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
$storagedir = $this->_dataid2discussionpath($pasteid);
|
2017-10-08 05:03:17 -04:00
|
|
|
$file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid . '.php';
|
2017-03-24 18:42:11 -04:00
|
|
|
if (is_file($file)) {
|
2016-07-26 02:19:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_dir($storagedir)) {
|
2016-08-09 05:54:42 -04:00
|
|
|
mkdir($storagedir, 0700, true);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_store($file, $comment);
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all comments of paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
2022-10-27 19:01:02 -04:00
|
|
|
$discdir = $this->_dataid2discussionpath($pasteid);
|
2016-07-26 02:19:35 -04:00
|
|
|
if (is_dir($discdir)) {
|
2012-04-29 13:15:06 -04:00
|
|
|
$dir = dir($discdir);
|
2016-07-26 02:19:35 -04:00
|
|
|
while (false !== ($filename = $dir->read())) {
|
2017-10-08 05:03:17 -04:00
|
|
|
// Filename is in the form pasteid.commentid.parentid.php:
|
2012-04-29 13:15:06 -04:00
|
|
|
// - pasteid is the paste this reply belongs to.
|
|
|
|
// - commentid is the comment identifier itself.
|
|
|
|
// - parentid is the comment this comment replies to (It can be pasteid)
|
2016-07-26 02:19:35 -04:00
|
|
|
if (is_file($discdir . $filename)) {
|
2022-10-27 19:01:02 -04:00
|
|
|
$comment = $this->_get($discdir . $filename);
|
2016-08-15 10:45:47 -04:00
|
|
|
$items = explode('.', $filename);
|
2012-04-29 13:15:06 -04:00
|
|
|
// Add some meta information not contained in file.
|
2019-05-10 01:55:39 -04:00
|
|
|
$comment['id'] = $items[1];
|
|
|
|
$comment['parentid'] = $items[2];
|
2012-04-29 13:15:06 -04:00
|
|
|
|
|
|
|
// Store in array
|
2022-12-12 14:48:36 -05:00
|
|
|
$key = $this->getOpenSlot(
|
|
|
|
$comments, (
|
|
|
|
(int) array_key_exists('created', $comment['meta']) ?
|
|
|
|
$comment['meta']['created'] : // v2 comments
|
|
|
|
$comment['meta']['postdate'] // v1 comments
|
|
|
|
)
|
|
|
|
);
|
2015-10-12 15:07:41 -04:00
|
|
|
$comments[$key] = $comment;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
|
|
|
|
// Sort comments by date, oldest first.
|
|
|
|
ksort($comments);
|
|
|
|
}
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a comment exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 08:44:17 -04:00
|
|
|
* @param string $pasteid
|
2012-04-29 13:15:06 -04:00
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
2016-08-09 07:07:11 -04:00
|
|
|
* @return bool
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
|
|
|
{
|
|
|
|
return is_file(
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_dataid2discussionpath($pasteid) .
|
2017-10-08 05:03:17 -04:00
|
|
|
$pasteid . '.' . $commentid . '.' . $parentid . '.php'
|
2012-04-29 13:15:06 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:53:15 -04:00
|
|
|
/**
|
|
|
|
* Save a value.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $value
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setValue($value, $namespace, $key = '')
|
|
|
|
{
|
2021-06-07 01:02:47 -04:00
|
|
|
switch ($namespace) {
|
|
|
|
case 'purge_limiter':
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_storeString(
|
|
|
|
$this->_path . DIRECTORY_SEPARATOR . 'purge_limiter.php',
|
2021-06-07 15:53:42 -04:00
|
|
|
'<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $value . ';'
|
|
|
|
);
|
2021-06-07 01:02:47 -04:00
|
|
|
case 'salt':
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_storeString(
|
|
|
|
$this->_path . DIRECTORY_SEPARATOR . 'salt.php',
|
2021-06-08 16:01:29 -04:00
|
|
|
'<?php # |' . $value . '|'
|
|
|
|
);
|
2021-06-07 01:02:47 -04:00
|
|
|
case 'traffic_limiter':
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_last_cache[$key] = $value;
|
|
|
|
return $this->_storeString(
|
|
|
|
$this->_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php',
|
|
|
|
'<?php' . PHP_EOL . '$GLOBALS[\'traffic_limiter\'] = ' . var_export($this->_last_cache, true) . ';'
|
2021-06-08 01:49:22 -04:00
|
|
|
);
|
2021-06-07 01:02:47 -04:00
|
|
|
}
|
2021-06-07 15:53:42 -04:00
|
|
|
return false;
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a value.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2021-06-07 15:53:42 -04:00
|
|
|
switch ($namespace) {
|
|
|
|
case 'purge_limiter':
|
2022-10-27 19:01:02 -04:00
|
|
|
$file = $this->_path . DIRECTORY_SEPARATOR . 'purge_limiter.php';
|
2021-06-08 16:01:29 -04:00
|
|
|
if (is_readable($file)) {
|
2021-06-07 15:53:42 -04:00
|
|
|
require $file;
|
|
|
|
return $GLOBALS['purge_limiter'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'salt':
|
2022-10-27 19:01:02 -04:00
|
|
|
$file = $this->_path . DIRECTORY_SEPARATOR . 'salt.php';
|
2021-06-08 16:01:29 -04:00
|
|
|
if (is_readable($file)) {
|
|
|
|
$items = explode('|', file_get_contents($file));
|
|
|
|
if (is_array($items) && count($items) == 3) {
|
|
|
|
return $items[1];
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 15:53:42 -04:00
|
|
|
break;
|
|
|
|
case 'traffic_limiter':
|
2022-10-27 19:01:02 -04:00
|
|
|
$file = $this->_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php';
|
2021-06-08 16:01:29 -04:00
|
|
|
if (is_readable($file)) {
|
2021-06-08 01:49:22 -04:00
|
|
|
require $file;
|
2022-10-27 19:01:02 -04:00
|
|
|
$this->_last_cache = $GLOBALS['traffic_limiter'];
|
|
|
|
if (array_key_exists($key, $this->_last_cache)) {
|
|
|
|
return $this->_last_cache[$key];
|
2021-06-08 01:49:22 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 15:53:42 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2021-06-07 00:53:15 -04:00
|
|
|
|
2021-06-07 15:53:42 -04:00
|
|
|
/**
|
|
|
|
* get the data
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $filename
|
|
|
|
* @return array|false $data
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _get($filename)
|
2021-06-07 15:53:42 -04:00
|
|
|
{
|
|
|
|
return Json::decode(
|
|
|
|
substr(
|
|
|
|
file_get_contents($filename),
|
|
|
|
strlen(self::PROTECTION_LINE . PHP_EOL)
|
2021-06-14 00:44:30 -04:00
|
|
|
)
|
|
|
|
);
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
|
|
|
|
2016-07-15 11:02:59 -04:00
|
|
|
/**
|
|
|
|
* Returns up to batch size number of paste ids that have expired
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param int $batchsize
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-10 15:52:14 -04:00
|
|
|
protected function _getExpiredPastes($batchsize)
|
2016-07-15 11:02:59 -04:00
|
|
|
{
|
2022-11-10 14:36:15 -05:00
|
|
|
$pastes = array();
|
|
|
|
$count = 0;
|
2022-12-12 14:48:36 -05:00
|
|
|
$opened = 0;
|
|
|
|
$limit = $batchsize * 10; // try at most 10 times $batchsize pastes before giving up
|
2022-11-10 14:36:15 -05:00
|
|
|
$time = time();
|
2022-12-12 15:28:38 -05:00
|
|
|
$files = $this->getAllPastes();
|
|
|
|
shuffle($files);
|
|
|
|
foreach ($files as $pasteid) {
|
2022-11-10 14:36:15 -05:00
|
|
|
if ($this->exists($pasteid)) {
|
|
|
|
$data = $this->read($pasteid);
|
|
|
|
if (
|
|
|
|
array_key_exists('expire_date', $data['meta']) &&
|
|
|
|
$data['meta']['expire_date'] < $time
|
|
|
|
) {
|
|
|
|
$pastes[] = $pasteid;
|
2022-12-12 14:48:36 -05:00
|
|
|
if (++$count >= $batchsize) {
|
2022-11-10 14:36:15 -05:00
|
|
|
break;
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 14:48:36 -05:00
|
|
|
if (++$opened >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
|
|
|
|
2022-10-27 19:01:02 -04:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllPastes()
|
|
|
|
{
|
2022-11-10 14:36:15 -05:00
|
|
|
$pastes = array();
|
2022-12-13 00:21:37 -05:00
|
|
|
foreach (new \GlobIterator($this->_path . self::PASTE_FILE_PATTERN) as $file) {
|
2022-11-10 14:36:15 -05:00
|
|
|
if ($file->isFile()) {
|
|
|
|
$pastes[] = $file->getBasename('.php');
|
2022-10-27 19:01:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
|
|
|
|
2012-04-29 13:15:06 -04:00
|
|
|
/**
|
|
|
|
* Convert paste id to storage path.
|
|
|
|
*
|
|
|
|
* The idea is to creates subdirectories in order to limit the number of files per directory.
|
|
|
|
* (A high number of files in a single directory can slow things down.)
|
|
|
|
* eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8"
|
|
|
|
* High-trafic websites may want to deepen the directory structure (like Squid does).
|
|
|
|
*
|
|
|
|
* eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/'
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $dataid
|
2016-08-09 07:07:11 -04:00
|
|
|
* @return string
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _dataid2path($dataid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_path . DIRECTORY_SEPARATOR .
|
2017-03-24 18:42:11 -04:00
|
|
|
substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
|
2021-06-07 00:53:15 -04:00
|
|
|
substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert paste id to discussion storage path.
|
|
|
|
*
|
|
|
|
* eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $dataid
|
2016-08-09 07:07:11 -04:00
|
|
|
* @return string
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _dataid2discussionpath($dataid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_dataid2path($dataid) . $dataid .
|
2016-07-18 08:47:32 -04:00
|
|
|
'.discussion' . DIRECTORY_SEPARATOR;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
2016-07-15 11:02:59 -04:00
|
|
|
|
2021-06-07 00:53:15 -04:00
|
|
|
/**
|
|
|
|
* store the data
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _store($filename, array $data)
|
2021-06-07 00:53:15 -04:00
|
|
|
{
|
2021-06-07 15:53:42 -04:00
|
|
|
try {
|
2022-10-27 19:01:02 -04:00
|
|
|
return $this->_storeString(
|
2021-06-07 15:53:42 -04:00
|
|
|
$filename,
|
|
|
|
self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
2021-06-07 15:53:42 -04:00
|
|
|
}
|
2021-06-07 00:53:15 -04:00
|
|
|
|
2021-06-07 15:53:42 -04:00
|
|
|
/**
|
|
|
|
* store a string
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _storeString($filename, $data)
|
2021-06-07 15:53:42 -04:00
|
|
|
{
|
2021-06-07 00:53:15 -04:00
|
|
|
// Create storage directory if it does not exist.
|
2022-10-27 19:01:02 -04:00
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
if (!@mkdir($this->_path, 0700)) {
|
2021-06-07 15:53:42 -04:00
|
|
|
return false;
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-27 19:01:02 -04:00
|
|
|
$file = $this->_path . DIRECTORY_SEPARATOR . '.htaccess';
|
2021-06-07 00:53:15 -04:00
|
|
|
if (!is_file($file)) {
|
|
|
|
$writtenBytes = 0;
|
|
|
|
if ($fileCreated = @touch($file)) {
|
|
|
|
$writtenBytes = @file_put_contents(
|
|
|
|
$file,
|
2021-06-14 00:44:30 -04:00
|
|
|
self::HTACCESS_LINE . PHP_EOL,
|
2021-06-07 00:53:15 -04:00
|
|
|
LOCK_EX
|
2021-06-14 00:44:30 -04:00
|
|
|
);
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
2021-06-14 00:44:30 -04:00
|
|
|
if (
|
|
|
|
$fileCreated === false ||
|
|
|
|
$writtenBytes === false ||
|
|
|
|
$writtenBytes < strlen(self::HTACCESS_LINE . PHP_EOL)
|
|
|
|
) {
|
2021-06-07 00:53:15 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileCreated = true;
|
|
|
|
$writtenBytes = 0;
|
2021-06-07 15:53:42 -04:00
|
|
|
if (!is_file($filename)) {
|
|
|
|
$fileCreated = @touch($filename);
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
|
|
|
if ($fileCreated) {
|
2021-06-07 15:53:42 -04:00
|
|
|
$writtenBytes = @file_put_contents($filename, $data, LOCK_EX);
|
2021-06-07 00:53:15 -04:00
|
|
|
}
|
|
|
|
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-14 00:44:30 -04:00
|
|
|
@chmod($filename, 0640); // protect file from access by other users on the host
|
2021-06-07 00:53:15 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rename a file, prepending the protection line at the beginning
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $srcFile
|
|
|
|
* @param string $destFile
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-10-27 19:01:02 -04:00
|
|
|
private function _prependRename($srcFile, $destFile)
|
2021-06-07 00:53:15 -04:00
|
|
|
{
|
|
|
|
// don't overwrite already converted file
|
|
|
|
if (!is_readable($destFile)) {
|
|
|
|
$handle = fopen($srcFile, 'r', false, stream_context_create());
|
|
|
|
file_put_contents($destFile, self::PROTECTION_LINE . PHP_EOL);
|
|
|
|
file_put_contents($destFile, $handle, FILE_APPEND);
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
unlink($srcFile);
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|