2012-04-29 19:15:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-11-09 21:39:42 +01:00
|
|
|
* @version 0.22
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* privatebin_data
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* Model for data access, implemented as a singleton.
|
|
|
|
*/
|
2016-07-11 11:58:15 +02:00
|
|
|
class privatebin_data extends privatebin_abstract
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2015-08-16 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* directory where data is stored
|
|
|
|
*
|
2012-04-29 19:15:06 +02:00
|
|
|
* @access private
|
|
|
|
* @static
|
2015-08-16 15:55:31 +02:00
|
|
|
* @var string
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
private static $_dir = 'data/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get instance of singleton
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-16 15:55:31 +02:00
|
|
|
* @param array $options
|
2016-07-11 11:58:15 +02:00
|
|
|
* @return privatebin_data
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2012-05-19 23:59:41 +02:00
|
|
|
public static function getInstance($options = null)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2012-04-30 13:58:29 +02:00
|
|
|
// if given update the data directory
|
|
|
|
if (
|
2015-08-27 21:41:21 +02:00
|
|
|
is_array($options) &&
|
|
|
|
array_key_exists('dir', $options)
|
2012-08-26 00:49:11 +02:00
|
|
|
) self::$_dir = $options['dir'] . DIRECTORY_SEPARATOR;
|
2012-04-29 19:15:06 +02:00
|
|
|
// if needed initialize the singleton
|
2016-07-18 15:13:56 +02:00
|
|
|
if (!(self::$_instance instanceof privatebin_data)) {
|
2012-08-26 00:49:11 +02:00
|
|
|
self::$_instance = new self;
|
2012-04-29 19:15:06 +02:00
|
|
|
self::_init();
|
|
|
|
}
|
2012-08-26 00:49:11 +02:00
|
|
|
return self::$_instance;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param array $paste
|
2015-09-03 22:55:36 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
public function create($pasteid, $paste)
|
|
|
|
{
|
|
|
|
$storagedir = self::_dataid2path($pasteid);
|
|
|
|
if (is_file($storagedir . $pasteid)) return false;
|
|
|
|
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
|
2015-09-03 22:55:36 +02:00
|
|
|
return (bool) @file_put_contents($storagedir . $pasteid, json_encode($paste));
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
2012-08-26 00:49:11 +02:00
|
|
|
* @return stdClass|false
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
2016-07-18 15:13:56 +02:00
|
|
|
if (!$this->exists($pasteid)) return false;
|
2015-09-26 12:29:27 +02:00
|
|
|
$paste = json_decode(
|
2012-04-29 19:15:06 +02:00
|
|
|
file_get_contents(self::_dataid2path($pasteid) . $pasteid)
|
|
|
|
);
|
2015-09-26 12:29:27 +02:00
|
|
|
if (property_exists($paste->meta, 'attachment'))
|
|
|
|
{
|
|
|
|
$paste->attachment = $paste->meta->attachment;
|
|
|
|
unset($paste->meta->attachment);
|
|
|
|
if (property_exists($paste->meta, 'attachmentname'))
|
|
|
|
{
|
|
|
|
$paste->attachmentname = $paste->meta->attachmentname;
|
|
|
|
unset($paste->meta->attachmentname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paste;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a paste and its discussion.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
|
|
|
// Delete the paste itself.
|
2015-09-26 12:29:27 +02:00
|
|
|
@unlink(self::_dataid2path($pasteid) . $pasteid);
|
2012-04-29 19:15:06 +02: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()))
|
|
|
|
{
|
2016-07-18 15:13:56 +02:00
|
|
|
if (is_file($discdir . $filename)) @unlink($discdir . $filename);
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
|
|
|
|
// Delete the discussion directory.
|
2015-09-03 22:55:36 +02:00
|
|
|
@rmdir($discdir);
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a paste exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 14:44:17 +02:00
|
|
|
* @param string $pasteid
|
2012-04-29 19:15:06 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exists($pasteid)
|
|
|
|
{
|
|
|
|
return is_file(self::_dataid2path($pasteid) . $pasteid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment in a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
|
|
|
* @param array $comment
|
2015-09-03 22:55:36 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
|
|
|
public function createComment($pasteid, $parentid, $commentid, $comment)
|
|
|
|
{
|
|
|
|
$storagedir = self::_dataid2discussionpath($pasteid);
|
|
|
|
$filename = $pasteid . '.' . $commentid . '.' . $parentid;
|
|
|
|
if (is_file($storagedir . $filename)) return false;
|
|
|
|
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
|
2015-09-03 22:55:36 +02:00
|
|
|
return (bool) @file_put_contents($storagedir . $filename, json_encode($comment));
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all comments of paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
|
|
|
$discdir = self::_dataid2discussionpath($pasteid);
|
|
|
|
if (is_dir($discdir))
|
|
|
|
{
|
|
|
|
// Delete all files in discussion directory
|
|
|
|
$dir = dir($discdir);
|
|
|
|
while (false !== ($filename = $dir->read()))
|
|
|
|
{
|
|
|
|
// Filename is in the form pasteid.commentid.parentid:
|
|
|
|
// - 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)
|
2015-09-27 03:03:55 +02:00
|
|
|
if (is_file($discdir . $filename))
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2015-09-27 03:03:55 +02:00
|
|
|
$comment = json_decode(file_get_contents($discdir . $filename));
|
2012-04-29 19:15:06 +02:00
|
|
|
$items = explode('.', $filename);
|
|
|
|
// Add some meta information not contained in file.
|
2015-10-18 11:38:48 +02:00
|
|
|
$comment->id = $items[1];
|
|
|
|
$comment->parentid = $items[2];
|
2012-04-29 19:15:06 +02:00
|
|
|
|
|
|
|
// Store in array
|
2015-10-12 21:07:41 +02:00
|
|
|
$key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
|
|
|
|
$comments[$key] = $comment;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
|
|
|
|
// Sort comments by date, oldest first.
|
|
|
|
ksort($comments);
|
|
|
|
}
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a comment exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 14:44:17 +02:00
|
|
|
* @param string $pasteid
|
2012-04-29 19:15:06 +02:00
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
|
|
|
{
|
|
|
|
return is_file(
|
|
|
|
self::_dataid2discussionpath($pasteid) .
|
2012-08-26 00:49:11 +02:00
|
|
|
$pasteid . '.' . $commentid . '.' . $parentid
|
2012-04-29 19:15:06 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-15 17:02:59 +02:00
|
|
|
/**
|
|
|
|
* Returns up to batch size number of paste ids that have expired
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param int $batchsize
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getExpiredPastes($batchsize)
|
|
|
|
{
|
|
|
|
$pastes = array();
|
|
|
|
$firstLevel = array_filter(
|
|
|
|
scandir(self::$_dir),
|
|
|
|
array('self', '_isFirstLevelDir')
|
|
|
|
);
|
|
|
|
if (count($firstLevel) > 0)
|
|
|
|
{
|
|
|
|
// try at most 10 times the $batchsize pastes before giving up
|
|
|
|
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i)
|
|
|
|
{
|
|
|
|
$firstKey = array_rand($firstLevel);
|
|
|
|
$secondLevel = array_filter(
|
|
|
|
scandir(self::$_dir . $firstLevel[$firstKey]),
|
|
|
|
array('self', '_isSecondLevelDir')
|
|
|
|
);
|
|
|
|
|
|
|
|
// skip this folder in the next checks if it is empty
|
|
|
|
if (count($secondLevel) == 0)
|
|
|
|
{
|
|
|
|
unset($firstLevel[$firstKey]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$secondKey = array_rand($secondLevel);
|
2016-07-18 14:47:32 +02:00
|
|
|
$path = self::$_dir . $firstLevel[$firstKey] .
|
|
|
|
DIRECTORY_SEPARATOR . $secondLevel[$secondKey];
|
2016-07-15 17:02:59 +02:00
|
|
|
if (!is_dir($path)) continue;
|
|
|
|
$thirdLevel = array_filter(
|
|
|
|
scandir($path),
|
|
|
|
array('model_paste', 'isValidId')
|
|
|
|
);
|
|
|
|
if (count($thirdLevel) == 0) continue;
|
|
|
|
$thirdKey = array_rand($thirdLevel);
|
|
|
|
$pasteid = $thirdLevel[$thirdKey];
|
|
|
|
if (in_array($pasteid, $pastes)) continue;
|
|
|
|
|
|
|
|
if ($this->exists($pasteid))
|
|
|
|
{
|
|
|
|
$data = $this->read($pasteid);
|
|
|
|
if (
|
|
|
|
property_exists($data->meta, 'expire_date') &&
|
|
|
|
$data->meta->expire_date < time()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$pastes[] = $pasteid;
|
|
|
|
if (count($pastes) >= $batchsize) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* initialize privatebin
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function _init()
|
|
|
|
{
|
|
|
|
// Create storage directory if it does not exist.
|
2012-04-30 13:58:29 +02:00
|
|
|
if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
|
|
|
|
// Create .htaccess file if it does not exist.
|
|
|
|
if (!is_file(self::$_dir . '.htaccess'))
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
|
|
|
file_put_contents(
|
|
|
|
self::$_dir . '.htaccess',
|
|
|
|
'Allow from none' . PHP_EOL .
|
2016-07-18 15:13:56 +02:00
|
|
|
'Deny from all' . PHP_EOL
|
2012-04-29 19:15:06 +02: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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function _dataid2path($dataid)
|
|
|
|
{
|
2016-07-18 15:13:56 +02:00
|
|
|
return self::$_dir . substr($dataid, 0, 2) . DIRECTORY_SEPARATOR .
|
|
|
|
substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert paste id to discussion storage path.
|
|
|
|
*
|
|
|
|
* eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/'
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $dataid
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function _dataid2discussionpath($dataid)
|
|
|
|
{
|
2016-07-18 14:47:32 +02:00
|
|
|
return self::_dataid2path($dataid) . $dataid .
|
|
|
|
'.discussion' . DIRECTORY_SEPARATOR;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
2016-07-15 17:02:59 +02: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 14:47:32 +02:00
|
|
|
return self::_isSecondLevelDir($element) &&
|
|
|
|
is_dir(self::$_dir . DIRECTORY_SEPARATOR . $element);
|
2016-07-15 17:02:59 +02: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 19:15:06 +02:00
|
|
|
}
|