2012-04-29 17:15:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 09:58:15 +00:00
|
|
|
* PrivateBin
|
2012-04-29 17:15:06 +00:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 09:58:15 +00:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-29 17:15:06 +00:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 11:56:52 +00:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2020-01-08 18:19:12 +00:00
|
|
|
* @version 1.2.2
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
2016-12-12 17:43:23 +00:00
|
|
|
|
2016-12-12 17:49:08 +00:00
|
|
|
namespace PrivateBin\Data;
|
2016-08-09 09:54:42 +00:00
|
|
|
|
2016-10-29 08:24:08 +00:00
|
|
|
use PrivateBin\Model\Paste;
|
2017-03-24 22:42:11 +00:00
|
|
|
use PrivateBin\Persistence\DataStore;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2012-04-29 17:15:06 +00:00
|
|
|
/**
|
2016-08-09 09:54:42 +00:00
|
|
|
* Filesystem
|
2012-04-29 17:15:06 +00:00
|
|
|
*
|
2016-08-09 09:54:42 +00:00
|
|
|
* Model for filesystem data access, implemented as a singleton.
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
2016-08-09 09:54:42 +00:00
|
|
|
class Filesystem extends AbstractData
|
2012-04-29 17:15:06 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* get instance of singleton
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-16 13:55:31 +00:00
|
|
|
* @param array $options
|
2016-08-09 09:54:42 +00:00
|
|
|
* @return Filesystem
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
2012-05-19 21:59:41 +00:00
|
|
|
public static function getInstance($options = null)
|
2012-04-29 17:15:06 +00:00
|
|
|
{
|
2017-03-24 20:30:08 +00:00
|
|
|
// if needed initialize the singleton
|
|
|
|
if (!(self::$_instance instanceof self)) {
|
|
|
|
self::$_instance = new self;
|
|
|
|
}
|
2012-04-30 11:58:29 +00:00
|
|
|
// if given update the data directory
|
|
|
|
if (
|
2015-08-27 19:41:21 +00:00
|
|
|
is_array($options) &&
|
|
|
|
array_key_exists('dir', $options)
|
2016-07-26 06:19:35 +00:00
|
|
|
) {
|
2017-03-24 22:42:11 +00:00
|
|
|
DataStore::setPath($options['dir']);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
2012-08-25 22:49:11 +00:00
|
|
|
return self::$_instance;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param array $paste
|
2015-09-03 20:55:36 +00:00
|
|
|
* @return bool
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
public function create($pasteid, $paste)
|
|
|
|
{
|
|
|
|
$storagedir = self::_dataid2path($pasteid);
|
2017-10-08 09:03:17 +00:00
|
|
|
$file = $storagedir . $pasteid . '.php';
|
2017-03-24 22:42:11 +00:00
|
|
|
if (is_file($file)) {
|
2016-07-26 06:19:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_dir($storagedir)) {
|
2016-08-09 09:54:42 +00:00
|
|
|
mkdir($storagedir, 0700, true);
|
2016-07-26 06:19:35 +00:00
|
|
|
}
|
2017-03-24 22:42:11 +00:00
|
|
|
return DataStore::store($file, $paste);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
2012-08-25 22:49:11 +00:00
|
|
|
* @return stdClass|false
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
2016-07-26 06:19:35 +00:00
|
|
|
if (!$this->exists($pasteid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-08 09:31:41 +00:00
|
|
|
$paste = DataStore::get(self::_dataid2path($pasteid) . $pasteid . '.php');
|
2016-07-26 06:19:35 +00:00
|
|
|
if (property_exists($paste->meta, 'attachment')) {
|
2015-09-26 10:29:27 +00:00
|
|
|
$paste->attachment = $paste->meta->attachment;
|
|
|
|
unset($paste->meta->attachment);
|
2016-07-26 06:19:35 +00:00
|
|
|
if (property_exists($paste->meta, 'attachmentname')) {
|
2015-09-26 10:29:27 +00:00
|
|
|
$paste->attachmentname = $paste->meta->attachmentname;
|
|
|
|
unset($paste->meta->attachmentname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paste;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a paste and its discussion.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
2016-08-09 09:54:42 +00:00
|
|
|
$pastedir = self::_dataid2path($pasteid);
|
|
|
|
if (is_dir($pastedir)) {
|
|
|
|
// Delete the paste itself.
|
2017-10-08 09:03:17 +00:00
|
|
|
if (is_file($pastedir . $pasteid . '.php')) {
|
|
|
|
unlink($pastedir . $pasteid . '.php');
|
2016-08-09 09:54:42 +00:00
|
|
|
}
|
2012-04-29 17:15:06 +00:00
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
// Delete discussion if it exists.
|
|
|
|
$discdir = self::_dataid2discussionpath($pasteid);
|
|
|
|
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 06:19:35 +00:00
|
|
|
}
|
2016-08-09 09:54:42 +00:00
|
|
|
$dir->close();
|
|
|
|
rmdir($discdir);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a paste exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 12:44:17 +00:00
|
|
|
* @param string $pasteid
|
2016-08-09 11:07:11 +00:00
|
|
|
* @return bool
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
public function exists($pasteid)
|
|
|
|
{
|
2017-10-08 09:03:17 +00:00
|
|
|
$basePath = self::_dataid2path($pasteid) . $pasteid;
|
|
|
|
$pastePath = $basePath . '.php';
|
|
|
|
// convert to PHP protected files if needed
|
|
|
|
if (is_readable($basePath)) {
|
2017-10-08 17:16:09 +00:00
|
|
|
DataStore::prependRename($basePath, $pastePath);
|
2017-10-08 09:03:17 +00:00
|
|
|
|
|
|
|
// convert comments, too
|
2017-10-08 17:16:09 +00:00
|
|
|
$discdir = self::_dataid2discussionpath($pasteid);
|
2017-10-08 09:03:17 +00: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';
|
2017-10-08 17:16:09 +00:00
|
|
|
DataStore::prependRename($discdir . $filename, $commentFilename);
|
2017-10-08 09:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is_readable($pastePath);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment in a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
|
|
|
* @param array $comment
|
2015-09-03 20:55:36 +00:00
|
|
|
* @return bool
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
public function createComment($pasteid, $parentid, $commentid, $comment)
|
|
|
|
{
|
|
|
|
$storagedir = self::_dataid2discussionpath($pasteid);
|
2017-10-08 09:03:17 +00:00
|
|
|
$file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid . '.php';
|
2017-03-24 22:42:11 +00:00
|
|
|
if (is_file($file)) {
|
2016-07-26 06:19:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_dir($storagedir)) {
|
2016-08-09 09:54:42 +00:00
|
|
|
mkdir($storagedir, 0700, true);
|
2016-07-26 06:19:35 +00:00
|
|
|
}
|
2017-03-24 22:42:11 +00:00
|
|
|
return DataStore::store($file, $comment);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all comments of paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
2016-08-15 14:45:47 +00:00
|
|
|
$discdir = self::_dataid2discussionpath($pasteid);
|
2016-07-26 06:19:35 +00:00
|
|
|
if (is_dir($discdir)) {
|
2012-04-29 17:15:06 +00:00
|
|
|
$dir = dir($discdir);
|
2016-07-26 06:19:35 +00:00
|
|
|
while (false !== ($filename = $dir->read())) {
|
2017-10-08 09:03:17 +00:00
|
|
|
// Filename is in the form pasteid.commentid.parentid.php:
|
2012-04-29 17:15:06 +00: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 06:19:35 +00:00
|
|
|
if (is_file($discdir . $filename)) {
|
2017-10-08 09:31:41 +00:00
|
|
|
$comment = DataStore::get($discdir . $filename);
|
2016-08-15 14:45:47 +00:00
|
|
|
$items = explode('.', $filename);
|
2012-04-29 17:15:06 +00:00
|
|
|
// Add some meta information not contained in file.
|
2016-08-15 14:45:47 +00:00
|
|
|
$comment->id = $items[1];
|
2015-10-18 09:38:48 +00:00
|
|
|
$comment->parentid = $items[2];
|
2012-04-29 17:15:06 +00:00
|
|
|
|
|
|
|
// Store in array
|
2016-08-15 14:45:47 +00:00
|
|
|
$key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
|
2015-10-12 19:07:41 +00:00
|
|
|
$comments[$key] = $comment;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
|
|
|
|
// Sort comments by date, oldest first.
|
|
|
|
ksort($comments);
|
|
|
|
}
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a comment exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 12:44:17 +00:00
|
|
|
* @param string $pasteid
|
2012-04-29 17:15:06 +00:00
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
2016-08-09 11:07:11 +00:00
|
|
|
* @return bool
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
|
|
|
{
|
|
|
|
return is_file(
|
|
|
|
self::_dataid2discussionpath($pasteid) .
|
2017-10-08 09:03:17 +00:00
|
|
|
$pasteid . '.' . $commentid . '.' . $parentid . '.php'
|
2012-04-29 17:15:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-15 15:02:59 +00:00
|
|
|
/**
|
|
|
|
* Returns up to batch size number of paste ids that have expired
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param int $batchsize
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getExpiredPastes($batchsize)
|
|
|
|
{
|
2016-08-15 14:45:47 +00:00
|
|
|
$pastes = array();
|
2017-03-24 22:42:11 +00:00
|
|
|
$mainpath = DataStore::getPath();
|
2016-07-15 15:02:59 +00:00
|
|
|
$firstLevel = array_filter(
|
2017-03-24 22:42:11 +00:00
|
|
|
scandir($mainpath),
|
2016-08-09 09:54:42 +00:00
|
|
|
'self::_isFirstLevelDir'
|
2016-07-15 15:02:59 +00:00
|
|
|
);
|
2016-07-26 06:19:35 +00:00
|
|
|
if (count($firstLevel) > 0) {
|
2016-07-15 15:02:59 +00:00
|
|
|
// try at most 10 times the $batchsize pastes before giving up
|
2016-07-26 06:19:35 +00:00
|
|
|
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
|
2016-08-15 14:45:47 +00:00
|
|
|
$firstKey = array_rand($firstLevel);
|
2016-07-15 15:02:59 +00:00
|
|
|
$secondLevel = array_filter(
|
2017-03-24 22:42:11 +00:00
|
|
|
scandir($mainpath . DIRECTORY_SEPARATOR . $firstLevel[$firstKey]),
|
2016-08-09 09:54:42 +00:00
|
|
|
'self::_isSecondLevelDir'
|
2016-07-15 15:02:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// skip this folder in the next checks if it is empty
|
2016-07-26 06:19:35 +00:00
|
|
|
if (count($secondLevel) == 0) {
|
2016-07-15 15:02:59 +00:00
|
|
|
unset($firstLevel[$firstKey]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$secondKey = array_rand($secondLevel);
|
2017-03-24 22:42:11 +00:00
|
|
|
$path = $mainpath . DIRECTORY_SEPARATOR .
|
|
|
|
$firstLevel[$firstKey] . DIRECTORY_SEPARATOR .
|
|
|
|
$secondLevel[$secondKey];
|
2016-07-26 06:19:35 +00:00
|
|
|
if (!is_dir($path)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
$thirdLevel = array_filter(
|
2017-10-08 09:03:17 +00:00
|
|
|
array_map(
|
2017-10-08 09:31:41 +00:00
|
|
|
function ($filename) {
|
2017-10-08 09:03:17 +00:00
|
|
|
return strlen($filename) >= 20 ?
|
|
|
|
substr($filename, 0, -4) :
|
|
|
|
$filename;
|
|
|
|
},
|
|
|
|
scandir($path)
|
|
|
|
),
|
2016-08-09 09:54:42 +00:00
|
|
|
'PrivateBin\\Model\\Paste::isValidId'
|
2016-07-15 15:02:59 +00:00
|
|
|
);
|
2016-07-26 06:19:35 +00:00
|
|
|
if (count($thirdLevel) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
$thirdKey = array_rand($thirdLevel);
|
2016-08-15 14:45:47 +00:00
|
|
|
$pasteid = $thirdLevel[$thirdKey];
|
2016-07-26 06:19:35 +00:00
|
|
|
if (in_array($pasteid, $pastes)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
|
2016-07-26 06:19:35 +00:00
|
|
|
if ($this->exists($pasteid)) {
|
2016-07-15 15:02:59 +00:00
|
|
|
$data = $this->read($pasteid);
|
|
|
|
if (
|
|
|
|
property_exists($data->meta, 'expire_date') &&
|
|
|
|
$data->meta->expire_date < time()
|
2016-07-26 06:19:35 +00:00
|
|
|
) {
|
2016-07-15 15:02:59 +00:00
|
|
|
$pastes[] = $pasteid;
|
2016-07-26 06:19:35 +00:00
|
|
|
if (count($pastes) >= $batchsize) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
|
|
|
|
2012-04-29 17:15:06 +00: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
|
|
|
|
* @static
|
|
|
|
* @param string $dataid
|
2016-08-09 11:07:11 +00:00
|
|
|
* @return string
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
private static function _dataid2path($dataid)
|
|
|
|
{
|
2017-03-24 22:42:11 +00:00
|
|
|
return DataStore::getPath(
|
|
|
|
substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
|
|
|
|
substr($dataid, 2, 2) . DIRECTORY_SEPARATOR
|
|
|
|
);
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert paste id to discussion storage path.
|
|
|
|
*
|
|
|
|
* eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $dataid
|
2016-08-09 11:07:11 +00:00
|
|
|
* @return string
|
2012-04-29 17:15:06 +00:00
|
|
|
*/
|
|
|
|
private static function _dataid2discussionpath($dataid)
|
|
|
|
{
|
2016-07-18 12:47:32 +00:00
|
|
|
return self::_dataid2path($dataid) . $dataid .
|
|
|
|
'.discussion' . DIRECTORY_SEPARATOR;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
2016-07-15 15:02:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the given element is a valid first level directory.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $element
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function _isFirstLevelDir($element)
|
|
|
|
{
|
2016-07-18 12:47:32 +00:00
|
|
|
return self::_isSecondLevelDir($element) &&
|
2017-03-24 22:42:11 +00:00
|
|
|
is_dir(DataStore::getPath($element));
|
2016-07-15 15:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the given element is a valid second level directory.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $element
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function _isSecondLevelDir($element)
|
|
|
|
{
|
|
|
|
return (bool) preg_match('/^[a-f0-9]{2}$/', $element);
|
|
|
|
}
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|