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
|
2024-05-05 20:43:33 +02:00
|
|
|
* @version 1.7.2
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
2016-12-12 18:49:08 +01:00
|
|
|
namespace PrivateBin\Data;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
2018-07-29 15:17:35 +02:00
|
|
|
use PrivateBin\Controller;
|
2019-05-13 22:31:52 +02:00
|
|
|
use PrivateBin\Json;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* Database
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
2016-08-09 11:54:42 +02:00
|
|
|
* Model for database access, implemented as a singleton.
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class Database extends AbstractData
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2015-08-16 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* instance of database connection
|
|
|
|
*
|
2012-04-29 19:15:06 +02:00
|
|
|
* @access private
|
2015-08-16 15:55:31 +02:00
|
|
|
* @var PDO
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private $_db;
|
2012-04-29 19:15:06 +02:00
|
|
|
|
2015-08-16 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* table prefix
|
|
|
|
*
|
2012-05-19 23:59:41 +02:00
|
|
|
* @access private
|
2015-08-16 15:55:31 +02:00
|
|
|
* @var string
|
2012-05-19 23:59:41 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private $_prefix = '';
|
2012-05-19 23:59:41 +02:00
|
|
|
|
2015-08-16 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* database type
|
|
|
|
*
|
2012-05-19 23:59:41 +02:00
|
|
|
* @access private
|
2015-08-16 15:55:31 +02:00
|
|
|
* @var string
|
2012-05-19 23:59:41 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private $_type = '';
|
2012-05-19 23:59:41 +02:00
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2022-10-28 01:01:02 +02:00
|
|
|
* instantiates a new Database data backend
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2015-08-16 15:55:31 +02:00
|
|
|
* @param array $options
|
2012-05-19 23:59:41 +02:00
|
|
|
* @throws Exception
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
public function __construct(array $options)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2019-05-19 09:42:55 +02:00
|
|
|
// set table prefix if given
|
|
|
|
if (array_key_exists('tbl', $options)) {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_prefix = $options['tbl'];
|
2019-05-19 09:42:55 +02:00
|
|
|
}
|
2015-11-01 17:02:20 +01:00
|
|
|
|
2019-05-19 09:42:55 +02:00
|
|
|
// initialize the db connection with new options
|
|
|
|
if (
|
|
|
|
array_key_exists('dsn', $options) &&
|
|
|
|
array_key_exists('usr', $options) &&
|
|
|
|
array_key_exists('pwd', $options) &&
|
|
|
|
array_key_exists('opt', $options)
|
|
|
|
) {
|
|
|
|
// set default options
|
|
|
|
$options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
|
|
|
$options['opt'][PDO::ATTR_EMULATE_PREPARES] = false;
|
|
|
|
$options['opt'][PDO::ATTR_PERSISTENT] = true;
|
|
|
|
$db_tables_exist = true;
|
|
|
|
|
|
|
|
// setup type and dabase connection
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_type = strtolower(
|
2019-05-19 09:42:55 +02:00
|
|
|
substr($options['dsn'], 0, strpos($options['dsn'], ':'))
|
|
|
|
);
|
2022-01-23 20:59:02 +01:00
|
|
|
// MySQL uses backticks to quote identifiers by default,
|
|
|
|
// tell it to expect ANSI SQL double quotes
|
2022-10-28 01:01:02 +02:00
|
|
|
if ($this->_type === 'mysql' && defined('PDO::MYSQL_ATTR_INIT_COMMAND')) {
|
2022-04-19 18:44:00 +02:00
|
|
|
$options['opt'][PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION sql_mode='ANSI_QUOTES'";
|
2022-01-23 20:59:02 +01:00
|
|
|
}
|
2022-10-28 01:01:02 +02:00
|
|
|
$tableQuery = $this->_getTableQuery($this->_type);
|
|
|
|
$this->_db = new PDO(
|
2019-05-19 09:42:55 +02:00
|
|
|
$options['dsn'],
|
|
|
|
$options['usr'],
|
|
|
|
$options['pwd'],
|
|
|
|
$options['opt']
|
|
|
|
);
|
|
|
|
|
|
|
|
// check if the database contains the required tables
|
2022-10-28 01:01:02 +02:00
|
|
|
$tables = $this->_db->query($tableQuery)->fetchAll(PDO::FETCH_COLUMN, 0);
|
2019-05-19 09:42:55 +02:00
|
|
|
|
|
|
|
// create paste table if necessary
|
2022-10-28 01:01:02 +02:00
|
|
|
if (!in_array($this->_sanitizeIdentifier('paste'), $tables)) {
|
|
|
|
$this->_createPasteTable();
|
2019-05-19 09:42:55 +02:00
|
|
|
$db_tables_exist = false;
|
|
|
|
}
|
2015-11-01 17:02:20 +01:00
|
|
|
|
2019-05-19 09:42:55 +02:00
|
|
|
// create comment table if necessary
|
2022-10-28 01:01:02 +02:00
|
|
|
if (!in_array($this->_sanitizeIdentifier('comment'), $tables)) {
|
|
|
|
$this->_createCommentTable();
|
2019-05-19 09:42:55 +02:00
|
|
|
$db_tables_exist = false;
|
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
|
2019-05-19 09:42:55 +02:00
|
|
|
// create config table if necessary
|
|
|
|
$db_version = Controller::VERSION;
|
2022-10-28 01:01:02 +02:00
|
|
|
if (!in_array($this->_sanitizeIdentifier('config'), $tables)) {
|
|
|
|
$this->_createConfigTable();
|
2019-05-19 09:42:55 +02:00
|
|
|
// if we only needed to create the config table, the DB is older then 0.22
|
|
|
|
if ($db_tables_exist) {
|
|
|
|
$db_version = '0.21';
|
2012-05-19 23:59:41 +02:00
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2022-10-28 01:01:02 +02:00
|
|
|
$db_version = $this->_getConfig('VERSION');
|
2019-05-19 09:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update database structure if necessary
|
|
|
|
if (version_compare($db_version, Controller::VERSION, '<')) {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_upgradeDatabase($db_version);
|
2016-07-13 09:41:45 +02:00
|
|
|
}
|
2019-05-19 09:42:55 +02:00
|
|
|
} else {
|
|
|
|
throw new Exception(
|
|
|
|
'Missing configuration for key dsn, usr, pwd or opt in the section model_options, please check your configuration file', 6
|
|
|
|
);
|
2012-05-19 23:59:41 +02:00
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param array $paste
|
2012-05-19 23:59:41 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function create($pasteid, array $paste)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2019-05-05 14:36:47 +02:00
|
|
|
$expire_date = 0;
|
|
|
|
$opendiscussion = $burnafterreading = false;
|
|
|
|
$attachment = $attachmentname = null;
|
|
|
|
$meta = $paste['meta'];
|
|
|
|
$isVersion1 = array_key_exists('data', $paste);
|
|
|
|
if (array_key_exists('expire_date', $meta)) {
|
|
|
|
$expire_date = (int) $meta['expire_date'];
|
2015-09-27 03:03:55 +02:00
|
|
|
unset($meta['expire_date']);
|
|
|
|
}
|
2019-05-05 14:36:47 +02:00
|
|
|
if (array_key_exists('opendiscussion', $meta)) {
|
2019-05-05 18:22:57 +02:00
|
|
|
$opendiscussion = $meta['opendiscussion'];
|
2015-09-21 22:32:52 +02:00
|
|
|
unset($meta['opendiscussion']);
|
|
|
|
}
|
2019-05-05 14:36:47 +02:00
|
|
|
if (array_key_exists('burnafterreading', $meta)) {
|
2019-05-05 18:22:57 +02:00
|
|
|
$burnafterreading = $meta['burnafterreading'];
|
2015-09-21 22:32:52 +02:00
|
|
|
unset($meta['burnafterreading']);
|
|
|
|
}
|
2019-05-05 14:36:47 +02:00
|
|
|
if ($isVersion1) {
|
|
|
|
if (array_key_exists('attachment', $meta)) {
|
|
|
|
$attachment = $meta['attachment'];
|
|
|
|
unset($meta['attachment']);
|
|
|
|
}
|
|
|
|
if (array_key_exists('attachmentname', $meta)) {
|
|
|
|
$attachmentname = $meta['attachmentname'];
|
|
|
|
unset($meta['attachmentname']);
|
|
|
|
}
|
2019-05-05 18:22:57 +02:00
|
|
|
} else {
|
|
|
|
$opendiscussion = $paste['adata'][2];
|
|
|
|
$burnafterreading = $paste['adata'][3];
|
2015-11-01 17:02:20 +01:00
|
|
|
}
|
2021-06-13 10:44:26 +02:00
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
return $this->_exec(
|
|
|
|
'INSERT INTO "' . $this->_sanitizeIdentifier('paste') .
|
2024-05-01 20:16:03 +02:00
|
|
|
'" VALUES(?,?,?,?,?,?,?,?)',
|
2021-06-13 10:44:26 +02:00
|
|
|
array(
|
|
|
|
$pasteid,
|
2022-01-22 08:45:12 +01:00
|
|
|
$isVersion1 ? $paste['data'] : Json::encode($paste),
|
2021-06-13 10:44:26 +02:00
|
|
|
$expire_date,
|
|
|
|
(int) $opendiscussion,
|
|
|
|
(int) $burnafterreading,
|
|
|
|
Json::encode($meta),
|
|
|
|
$attachment,
|
|
|
|
$attachmentname,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
2019-05-05 21:03:58 +02:00
|
|
|
* @return array|false
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function read($pasteid)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2021-06-13 12:40:06 +02:00
|
|
|
try {
|
2022-11-04 21:19:47 +01:00
|
|
|
$row = $this->_select(
|
2022-10-28 01:01:02 +02:00
|
|
|
'SELECT * FROM "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "dataid" = ?', array($pasteid), true
|
2021-06-13 12:40:06 +02:00
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
2022-11-04 21:19:47 +01:00
|
|
|
$row = false;
|
2021-06-13 12:40:06 +02:00
|
|
|
}
|
2022-11-04 21:19:47 +01:00
|
|
|
if ($row === false) {
|
2019-05-05 21:03:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// create array
|
2022-11-04 21:19:47 +01:00
|
|
|
$data = Json::decode($row['data']);
|
2019-05-05 21:03:58 +02:00
|
|
|
$isVersion2 = array_key_exists('v', $data) && $data['v'] >= 2;
|
2024-05-01 20:16:03 +02:00
|
|
|
$paste = $isVersion2 ? $data : array('data' => $row['data']);
|
2019-05-05 18:22:57 +02:00
|
|
|
|
2019-05-19 09:42:55 +02:00
|
|
|
try {
|
2022-11-04 21:19:47 +01:00
|
|
|
$row['meta'] = Json::decode($row['meta']);
|
2019-05-19 09:42:55 +02:00
|
|
|
} catch (Exception $e) {
|
2022-11-04 21:19:47 +01:00
|
|
|
$row['meta'] = array();
|
2019-05-05 21:03:58 +02:00
|
|
|
}
|
2022-11-04 21:19:47 +01:00
|
|
|
$row = self::upgradePreV1Format($row);
|
|
|
|
$paste['meta'] = $row['meta'];
|
|
|
|
$expire_date = (int) $row['expiredate'];
|
2019-05-05 21:03:58 +02:00
|
|
|
if ($expire_date > 0) {
|
2022-11-04 21:19:47 +01:00
|
|
|
$paste['meta']['expire_date'] = $expire_date;
|
2019-05-05 21:03:58 +02:00
|
|
|
}
|
|
|
|
if ($isVersion2) {
|
2022-11-04 21:19:47 +01:00
|
|
|
return $paste;
|
2019-05-05 21:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// support v1 attachments
|
2022-11-04 21:19:47 +01:00
|
|
|
if (array_key_exists('attachment', $row) && !empty($row['attachment'])) {
|
|
|
|
$paste['attachment'] = $row['attachment'];
|
|
|
|
if (array_key_exists('attachmentname', $row) && !empty($row['attachmentname'])) {
|
|
|
|
$paste['attachmentname'] = $row['attachmentname'];
|
2012-08-26 00:49:11 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 21:19:47 +01:00
|
|
|
if ($row['opendiscussion']) {
|
|
|
|
$paste['meta']['opendiscussion'] = true;
|
2019-05-05 21:03:58 +02:00
|
|
|
}
|
2022-11-04 21:19:47 +01:00
|
|
|
if ($row['burnafterreading']) {
|
|
|
|
$paste['meta']['burnafterreading'] = true;
|
2019-05-05 21:03:58 +02:00
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
|
2022-11-04 21:19:47 +01:00
|
|
|
return $paste;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a paste and its discussion.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function delete($pasteid)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_exec(
|
|
|
|
'DELETE FROM "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "dataid" = ?', array($pasteid)
|
2012-05-19 23:59:41 +02:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_exec(
|
|
|
|
'DELETE FROM "' . $this->_sanitizeIdentifier('comment') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "pasteid" = ?', array($pasteid)
|
2012-05-19 23:59:41 +02:00
|
|
|
);
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a paste exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-15 17:02:59 +02:00
|
|
|
* @param string $pasteid
|
2016-08-09 13:07:11 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function exists($pasteid)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2022-11-04 21:19:47 +01:00
|
|
|
try {
|
|
|
|
$row = $this->_select(
|
2022-11-06 07:40:39 +01:00
|
|
|
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') .
|
2022-11-04 21:19:47 +01:00
|
|
|
'" WHERE "dataid" = ?', array($pasteid), true
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
2022-11-06 07:40:39 +01:00
|
|
|
return false;
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2022-11-04 21:19:47 +01:00
|
|
|
return (bool) $row;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment in a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
|
|
|
* @param array $comment
|
2016-07-11 15:47:42 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2019-05-05 14:36:47 +02:00
|
|
|
if (array_key_exists('data', $comment)) {
|
|
|
|
$version = 1;
|
|
|
|
$data = $comment['data'];
|
|
|
|
} else {
|
|
|
|
$version = 2;
|
2019-05-13 22:31:52 +02:00
|
|
|
$data = Json::encode($comment);
|
2019-05-05 14:36:47 +02:00
|
|
|
}
|
2022-10-28 01:01:02 +02:00
|
|
|
list($createdKey, $iconKey) = $this->_getVersionedKeys($version);
|
2019-05-10 21:45:34 +02:00
|
|
|
$meta = $comment['meta'];
|
2019-05-05 14:36:47 +02:00
|
|
|
unset($comment['meta']);
|
|
|
|
foreach (array('nickname', $iconKey) as $key) {
|
|
|
|
if (!array_key_exists($key, $meta)) {
|
|
|
|
$meta[$key] = null;
|
2016-07-18 10:14:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 10:44:26 +02:00
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
return $this->_exec(
|
|
|
|
'INSERT INTO "' . $this->_sanitizeIdentifier('comment') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" VALUES(?,?,?,?,?,?,?)',
|
2021-06-13 10:44:26 +02:00
|
|
|
array(
|
|
|
|
$commentid,
|
|
|
|
$pasteid,
|
|
|
|
$parentid,
|
|
|
|
$data,
|
|
|
|
$meta['nickname'],
|
|
|
|
$meta[$iconKey],
|
|
|
|
$meta[$createdKey],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all comments of paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function readComments($pasteid)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
$rows = $this->_select(
|
|
|
|
'SELECT * FROM "' . $this->_sanitizeIdentifier('comment') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "pasteid" = ?', array($pasteid)
|
2012-05-19 23:59:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// create comment list
|
|
|
|
$comments = array();
|
2022-01-23 21:45:22 +01:00
|
|
|
if (is_array($rows) && count($rows)) {
|
2016-07-26 08:19:35 +02:00
|
|
|
foreach ($rows as $row) {
|
2022-01-22 08:45:12 +01:00
|
|
|
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
|
|
|
|
$data = Json::decode($row['data']);
|
2019-05-05 21:03:58 +02:00
|
|
|
if (array_key_exists('v', $data) && $data['v'] >= 2) {
|
2019-05-05 14:36:47 +02:00
|
|
|
$version = 2;
|
|
|
|
$comments[$i] = $data;
|
|
|
|
} else {
|
2019-05-05 21:03:58 +02:00
|
|
|
$version = 1;
|
2022-01-22 08:45:12 +01:00
|
|
|
$comments[$i] = array('data' => $row['data']);
|
2019-05-05 14:36:47 +02:00
|
|
|
}
|
2022-10-28 01:01:02 +02:00
|
|
|
list($createdKey, $iconKey) = $this->_getVersionedKeys($version);
|
2022-01-22 08:45:12 +01:00
|
|
|
$comments[$i]['id'] = $row['dataid'];
|
|
|
|
$comments[$i]['parentid'] = $row['parentid'];
|
|
|
|
$comments[$i]['meta'] = array($createdKey => (int) $row['postdate']);
|
2019-05-05 14:36:47 +02:00
|
|
|
foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
|
2022-01-22 08:45:12 +01:00
|
|
|
if (array_key_exists($rowKey, $row) && !empty($row[$rowKey])) {
|
|
|
|
$comments[$i]['meta'][$commentKey] = $row[$rowKey];
|
2017-03-25 00:58:59 +01:00
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
}
|
|
|
|
ksort($comments);
|
|
|
|
}
|
|
|
|
return $comments;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a comment exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-15 17:02:59 +02:00
|
|
|
* @param string $pasteid
|
2012-04-29 19:15:06 +02:00
|
|
|
* @param string $parentid
|
|
|
|
* @param string $commentid
|
2016-08-09 13:07:11 +02:00
|
|
|
* @return bool
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2019-05-10 21:35:36 +02:00
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2021-06-13 10:44:26 +02:00
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
return (bool) $this->_select(
|
|
|
|
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('comment') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "pasteid" = ? AND "parentid" = ? AND "dataid" = ?',
|
2021-06-13 10:44:26 +02:00
|
|
|
array($pasteid, $parentid, $commentid), true
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 07:02:47 +02: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-09 07:47:40 +02:00
|
|
|
if ($namespace === 'traffic_limiter') {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_last_cache[$key] = $value;
|
2021-06-09 07:47:40 +02:00
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
$value = Json::encode($this->_last_cache);
|
2021-06-09 07:47:40 +02:00
|
|
|
} catch (Exception $e) {
|
2021-06-07 07:02:47 +02:00
|
|
|
return false;
|
2021-06-09 07:47:40 +02:00
|
|
|
}
|
2021-06-07 07:02:47 +02:00
|
|
|
}
|
2022-10-28 01:01:02 +02:00
|
|
|
return $this->_exec(
|
|
|
|
'UPDATE "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" SET "value" = ? WHERE "id" = ?',
|
2021-06-09 07:47:40 +02:00
|
|
|
array($value, strtoupper($namespace))
|
|
|
|
);
|
2021-06-07 07:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a value.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2021-06-09 07:47:40 +02:00
|
|
|
$configKey = strtoupper($namespace);
|
2021-06-09 19:16:22 +02:00
|
|
|
$value = $this->_getConfig($configKey);
|
2021-06-09 07:47:40 +02:00
|
|
|
if ($value === '') {
|
|
|
|
// initialize the row, so that setValue can rely on UPDATE queries
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_exec(
|
|
|
|
'INSERT INTO "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" VALUES(?,?)',
|
2021-06-09 07:47:40 +02:00
|
|
|
array($configKey, '')
|
|
|
|
);
|
2021-06-07 07:02:47 +02:00
|
|
|
|
2021-06-09 07:47:40 +02:00
|
|
|
// migrate filesystem based salt into database
|
|
|
|
$file = 'data' . DIRECTORY_SEPARATOR . 'salt.php';
|
|
|
|
if ($namespace === 'salt' && is_readable($file)) {
|
2022-11-01 16:38:06 +01:00
|
|
|
$fs = new Filesystem(array('dir' => 'data'));
|
2022-10-28 01:01:02 +02:00
|
|
|
$value = $fs->getValue('salt');
|
2021-06-09 07:47:40 +02:00
|
|
|
$this->setValue($value, 'salt');
|
|
|
|
@unlink($file);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($value && $namespace === 'traffic_limiter') {
|
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_last_cache = Json::decode($value);
|
2021-06-09 07:47:40 +02:00
|
|
|
} catch (Exception $e) {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_last_cache = array();
|
2021-06-09 07:47:40 +02:00
|
|
|
}
|
2022-10-28 01:01:02 +02:00
|
|
|
if (array_key_exists($key, $this->_last_cache)) {
|
|
|
|
return $this->_last_cache[$key];
|
2021-06-09 07:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (string) $value;
|
2021-06-07 07:02:47 +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
|
|
|
|
*/
|
2019-05-10 21:52:14 +02:00
|
|
|
protected function _getExpiredPastes($batchsize)
|
2016-07-15 17:02:59 +02:00
|
|
|
{
|
2022-11-05 08:46:42 +01:00
|
|
|
$statement = $this->_db->prepare(
|
2022-10-28 01:01:02 +02:00
|
|
|
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "expiredate" < ? AND "expiredate" != ? ' .
|
2022-11-05 08:46:42 +01:00
|
|
|
($this->_type === 'oci' ? 'FETCH NEXT ? ROWS ONLY' : 'LIMIT ?')
|
2016-07-15 17:02:59 +02:00
|
|
|
);
|
2022-11-05 08:46:42 +01:00
|
|
|
$statement->execute(array(time(), 0, $batchsize));
|
|
|
|
return $statement->fetchAll(PDO::FETCH_COLUMN, 0);
|
2016-07-15 17:02:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 01:01:02 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllPastes()
|
|
|
|
{
|
2022-11-05 09:37:24 +01:00
|
|
|
return $this->_db->query(
|
2022-11-04 20:19:41 +01:00
|
|
|
'SELECT "dataid" FROM "' . $this->_sanitizeIdentifier('paste') . '"'
|
|
|
|
)->fetchAll(PDO::FETCH_COLUMN, 0);
|
2022-10-28 01:01:02 +02:00
|
|
|
}
|
|
|
|
|
2012-05-19 23:59:41 +02:00
|
|
|
/**
|
|
|
|
* execute a statement
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
|
|
|
* @throws PDOException
|
2016-07-11 15:47:42 +02:00
|
|
|
* @return bool
|
2012-05-19 23:59:41 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _exec($sql, array $params)
|
2012-05-19 23:59:41 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
$statement = $this->_db->prepare($sql);
|
2022-01-24 21:44:20 +01:00
|
|
|
foreach ($params as $key => &$parameter) {
|
2022-01-26 05:28:29 +01:00
|
|
|
$position = $key + 1;
|
2022-01-24 17:26:09 +01:00
|
|
|
if (is_int($parameter)) {
|
2022-01-26 05:28:29 +01:00
|
|
|
$statement->bindParam($position, $parameter, PDO::PARAM_INT);
|
2022-06-01 21:05:08 +02:00
|
|
|
} elseif (is_string($parameter) && strlen($parameter) >= 4000) {
|
2022-01-26 05:28:29 +01:00
|
|
|
$statement->bindParam($position, $parameter, PDO::PARAM_STR, strlen($parameter));
|
2022-01-24 17:26:09 +01:00
|
|
|
} else {
|
2022-01-26 05:28:29 +01:00
|
|
|
$statement->bindParam($position, $parameter);
|
2022-01-22 08:45:12 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 17:26:09 +01:00
|
|
|
$result = $statement->execute();
|
2012-05-19 23:59:41 +02:00
|
|
|
$statement->closeCursor();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run a select statement
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
|
|
|
* @param bool $firstOnly if only the first row should be returned
|
|
|
|
* @throws PDOException
|
2019-05-19 09:42:55 +02:00
|
|
|
* @return array|false
|
2012-05-19 23:59:41 +02:00
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _select($sql, array $params, $firstOnly = false)
|
2012-05-19 23:59:41 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
$statement = $this->_db->prepare($sql);
|
2012-05-19 23:59:41 +02:00
|
|
|
$statement->execute($params);
|
2022-01-23 21:24:28 +01:00
|
|
|
if ($firstOnly) {
|
|
|
|
$result = $statement->fetch(PDO::FETCH_ASSOC);
|
2022-10-28 01:01:02 +02:00
|
|
|
} elseif ($this->_type === 'oci') {
|
2022-01-23 21:24:28 +01:00
|
|
|
// workaround for https://bugs.php.net/bug.php?id=46728
|
|
|
|
$result = array();
|
|
|
|
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
|
2022-10-25 07:15:09 +02:00
|
|
|
$result[] = array_map('PrivateBin\Data\Database::_sanitizeClob', $row);
|
2022-01-23 21:24:28 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
$statement->closeCursor();
|
2022-10-28 01:01:02 +02:00
|
|
|
if ($this->_type === 'oci' && is_array($result)) {
|
2022-01-22 08:45:12 +01:00
|
|
|
// returned CLOB values are streams, convert these into strings
|
2022-01-23 07:32:28 +01:00
|
|
|
$result = $firstOnly ?
|
2022-10-25 07:15:09 +02:00
|
|
|
array_map('PrivateBin\Data\Database::_sanitizeClob', $result) :
|
2022-01-25 05:59:22 +01:00
|
|
|
$result;
|
2022-01-22 08:45:12 +01:00
|
|
|
}
|
2012-05-19 23:59:41 +02:00
|
|
|
return $result;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
2015-11-01 17:02:20 +01:00
|
|
|
|
2019-05-05 14:36:47 +02:00
|
|
|
/**
|
|
|
|
* get version dependent key names
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param int $version
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getVersionedKeys($version)
|
2019-05-05 14:36:47 +02:00
|
|
|
{
|
|
|
|
if ($version === 1) {
|
|
|
|
return array('postdate', 'vizhash');
|
|
|
|
}
|
|
|
|
return array('created', 'icon');
|
|
|
|
}
|
|
|
|
|
2015-11-01 17:02:20 +01:00
|
|
|
/**
|
|
|
|
* get table list query, depending on the database type
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $type
|
|
|
|
* @throws Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getTableQuery($type)
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
switch ($type) {
|
2015-11-01 17:02:20 +01:00
|
|
|
case 'ibm':
|
2022-01-23 20:59:02 +01:00
|
|
|
$sql = 'SELECT "tabname" FROM "SYSCAT"."TABLES"';
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
case 'informix':
|
2022-01-23 20:59:02 +01:00
|
|
|
$sql = 'SELECT "tabname" FROM "systables"';
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
case 'mssql':
|
2022-01-26 05:26:47 +01:00
|
|
|
// U: tables created by the user
|
2022-01-23 20:59:02 +01:00
|
|
|
$sql = 'SELECT "name" FROM "sysobjects" '
|
|
|
|
. 'WHERE "type" = \'U\' ORDER BY "name"';
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
case 'mysql':
|
|
|
|
$sql = 'SHOW TABLES';
|
|
|
|
break;
|
|
|
|
case 'oci':
|
2022-01-24 21:43:48 +01:00
|
|
|
$sql = 'SELECT table_name FROM all_tables';
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
case 'pgsql':
|
2022-01-23 20:59:02 +01:00
|
|
|
$sql = 'SELECT c."relname" AS "table_name" '
|
|
|
|
. 'FROM "pg_class" c, "pg_user" u '
|
2022-01-30 21:42:24 +01:00
|
|
|
. 'WHERE c."relowner" = u."usesysid" AND c."relkind" = \'r\' '
|
2022-01-23 20:59:02 +01:00
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM "pg_views" WHERE "viewname" = c."relname") '
|
|
|
|
. "AND c.\"relname\" !~ '^(pg_|sql_)' "
|
2016-08-15 16:45:47 +02:00
|
|
|
. 'UNION '
|
2022-01-23 20:59:02 +01:00
|
|
|
. 'SELECT c."relname" AS "table_name" '
|
|
|
|
. 'FROM "pg_class" c '
|
|
|
|
. "WHERE c.\"relkind\" = 'r' "
|
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM "pg_views" WHERE "viewname" = c."relname") '
|
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM "pg_user" WHERE "usesysid" = c."relowner") '
|
|
|
|
. "AND c.\"relname\" !~ '^pg_'";
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
case 'sqlite':
|
2022-01-23 20:59:02 +01:00
|
|
|
$sql = 'SELECT "name" FROM "sqlite_master" WHERE "type"=\'table\' '
|
|
|
|
. 'UNION ALL SELECT "name" FROM "sqlite_temp_master" '
|
|
|
|
. 'WHERE "type"=\'table\' ORDER BY "name"';
|
2015-11-01 17:02:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception(
|
|
|
|
"PDO type $type is currently not supported.", 5
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a value by key from the config table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getConfig($key)
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2021-06-13 12:40:06 +02:00
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
$row = $this->_select(
|
|
|
|
'SELECT "value" FROM "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" WHERE "id" = ?', array($key), true
|
2021-06-13 12:40:06 +02:00
|
|
|
);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-01-22 08:45:12 +01:00
|
|
|
return $row ? $row['value'] : '';
|
2022-01-18 17:21:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 17:02:20 +01:00
|
|
|
/**
|
|
|
|
* get the primary key clauses, depending on the database driver
|
|
|
|
*
|
|
|
|
* @access private
|
2019-05-05 08:53:40 +02:00
|
|
|
* @param string $key
|
2015-11-01 17:02:20 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getPrimaryKeyClauses($key = 'dataid')
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
|
|
|
$main_key = $after_key = '';
|
2022-10-28 01:01:02 +02:00
|
|
|
switch ($this->_type) {
|
2022-01-24 17:48:27 +01:00
|
|
|
case 'mysql':
|
|
|
|
case 'oci':
|
|
|
|
$after_key = ", PRIMARY KEY (\"$key\")";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$main_key = ' PRIMARY KEY';
|
|
|
|
break;
|
2015-11-01 17:02:20 +01:00
|
|
|
}
|
|
|
|
return array($main_key, $after_key);
|
|
|
|
}
|
|
|
|
|
2019-05-05 08:53:40 +02:00
|
|
|
/**
|
|
|
|
* get the data type, depending on the database driver
|
|
|
|
*
|
2022-01-22 08:45:12 +01:00
|
|
|
* PostgreSQL and OCI uses a different API for BLOBs then SQL, hence we use TEXT and CLOB
|
2019-09-20 06:57:54 +02:00
|
|
|
*
|
2019-05-05 08:53:40 +02:00
|
|
|
* @access private
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getDataType()
|
2019-05-05 08:53:40 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
switch ($this->_type) {
|
2022-01-24 17:48:27 +01:00
|
|
|
case 'oci':
|
|
|
|
return 'CLOB';
|
|
|
|
case 'pgsql':
|
|
|
|
return 'TEXT';
|
|
|
|
default:
|
|
|
|
return 'BLOB';
|
|
|
|
}
|
2019-05-05 08:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the attachment type, depending on the database driver
|
|
|
|
*
|
2022-01-18 17:21:25 +01:00
|
|
|
* PostgreSQL and OCI use different APIs for BLOBs then SQL, hence we use TEXT and CLOB
|
2019-09-20 06:57:54 +02:00
|
|
|
*
|
2019-05-05 08:53:40 +02:00
|
|
|
* @access private
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getAttachmentType()
|
2019-05-05 08:53:40 +02:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
switch ($this->_type) {
|
2022-01-24 17:48:27 +01:00
|
|
|
case 'oci':
|
|
|
|
return 'CLOB';
|
|
|
|
case 'pgsql':
|
|
|
|
return 'TEXT';
|
|
|
|
default:
|
|
|
|
return 'MEDIUMBLOB';
|
|
|
|
}
|
2022-01-18 17:21:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the meta type, depending on the database driver
|
|
|
|
*
|
2022-01-22 08:45:12 +01:00
|
|
|
* OCI doesn't accept TEXT so it has to be VARCHAR2(4000)
|
2022-01-18 17:21:25 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _getMetaType()
|
2022-01-18 17:21:25 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
switch ($this->_type) {
|
2022-01-24 17:48:27 +01:00
|
|
|
case 'oci':
|
|
|
|
return 'VARCHAR2(4000)';
|
|
|
|
default:
|
|
|
|
return 'TEXT';
|
|
|
|
}
|
2019-05-05 08:53:40 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 17:02:20 +01:00
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _createPasteTable()
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
list($main_key, $after_key) = $this->_getPrimaryKeyClauses();
|
|
|
|
$dataType = $this->_getDataType();
|
|
|
|
$attachmentType = $this->_getAttachmentType();
|
|
|
|
$metaType = $this->_getMetaType();
|
|
|
|
$this->_db->exec(
|
|
|
|
'CREATE TABLE "' . $this->_sanitizeIdentifier('paste') . '" ( ' .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\"dataid\" CHAR(16) NOT NULL$main_key, " .
|
2022-01-23 21:31:40 +01:00
|
|
|
"\"data\" $attachmentType, " .
|
2022-01-23 20:59:02 +01:00
|
|
|
'"expiredate" INT, ' .
|
|
|
|
'"opendiscussion" INT, ' .
|
|
|
|
'"burnafterreading" INT, ' .
|
|
|
|
"\"meta\" $metaType, " .
|
|
|
|
"\"attachment\" $attachmentType, " .
|
|
|
|
"\"attachmentname\" $dataType$after_key )"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _createCommentTable()
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
list($main_key, $after_key) = $this->_getPrimaryKeyClauses();
|
|
|
|
$dataType = $this->_getDataType();
|
|
|
|
$this->_db->exec(
|
|
|
|
'CREATE TABLE "' . $this->_sanitizeIdentifier('comment') . '" ( ' .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\"dataid\" CHAR(16) NOT NULL$main_key, " .
|
|
|
|
'"pasteid" CHAR(16), ' .
|
|
|
|
'"parentid" CHAR(16), ' .
|
|
|
|
"\"data\" $dataType, " .
|
|
|
|
"\"nickname\" $dataType, " .
|
|
|
|
"\"vizhash\" $dataType, " .
|
|
|
|
"\"postdate\" INT$after_key )"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
if ($this->_type === 'oci') {
|
|
|
|
$this->_db->exec(
|
2022-01-22 21:29:39 +01:00
|
|
|
'declare
|
|
|
|
already_exists exception;
|
|
|
|
columns_indexed exception;
|
|
|
|
pragma exception_init( already_exists, -955 );
|
|
|
|
pragma exception_init(columns_indexed, -1408);
|
|
|
|
begin
|
2022-10-28 01:01:02 +02:00
|
|
|
execute immediate \'create index "comment_parent" on "' . $this->_sanitizeIdentifier('comment') . '" ("pasteid")\';
|
2022-01-22 21:29:39 +01:00
|
|
|
exception
|
2022-01-24 21:36:18 +01:00
|
|
|
when already_exists or columns_indexed then
|
|
|
|
NULL;
|
|
|
|
end;'
|
2022-01-22 21:29:39 +01:00
|
|
|
);
|
|
|
|
} else {
|
2022-06-29 22:25:54 +02:00
|
|
|
// CREATE INDEX IF NOT EXISTS not supported as of Oracle MySQL <= 8.0
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
2022-06-28 06:51:21 +02:00
|
|
|
'CREATE INDEX "' .
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_sanitizeIdentifier('comment_parent') . '" ON "' .
|
|
|
|
$this->_sanitizeIdentifier('comment') . '" ("pasteid")'
|
2022-01-22 21:29:39 +01:00
|
|
|
);
|
|
|
|
}
|
2015-11-01 17:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _createConfigTable()
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
list($main_key, $after_key) = $this->_getPrimaryKeyClauses('id');
|
|
|
|
$charType = $this->_type === 'oci' ? 'VARCHAR2(16)' : 'CHAR(16)';
|
|
|
|
$textType = $this->_getMetaType();
|
|
|
|
$this->_db->exec(
|
|
|
|
'CREATE TABLE "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\" ( \"id\" $charType NOT NULL$main_key, \"value\" $textType$after_key )"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_exec(
|
|
|
|
'INSERT INTO "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" VALUES(?,?)',
|
2018-07-29 15:17:35 +02:00
|
|
|
array('VERSION', Controller::VERSION)
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-11 14:15:20 +02:00
|
|
|
/**
|
2022-01-22 08:45:12 +01:00
|
|
|
* sanitizes CLOB values used with OCI
|
|
|
|
*
|
|
|
|
* From: https://stackoverflow.com/questions/36200534/pdo-oci-into-a-clob-field
|
2016-07-11 14:15:20 +02:00
|
|
|
*
|
2022-01-23 07:32:28 +01:00
|
|
|
* @access public
|
2022-11-04 21:25:53 +01:00
|
|
|
* @static
|
2022-01-22 08:45:12 +01:00
|
|
|
* @param int|string|resource $value
|
|
|
|
* @return int|string
|
2016-07-11 14:15:20 +02:00
|
|
|
*/
|
2022-11-04 21:25:53 +01:00
|
|
|
public static function _sanitizeClob($value)
|
2016-07-11 14:15:20 +02:00
|
|
|
{
|
2022-01-22 08:45:12 +01:00
|
|
|
if (is_resource($value)) {
|
|
|
|
$value = stream_get_contents($value);
|
|
|
|
}
|
|
|
|
return $value;
|
2022-01-18 02:06:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-22 08:45:12 +01:00
|
|
|
* sanitizes identifiers
|
2022-01-18 02:06:26 +01:00
|
|
|
*
|
|
|
|
* @access private
|
2022-01-22 08:45:12 +01:00
|
|
|
* @param string $identifier
|
2022-01-18 02:06:26 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _sanitizeIdentifier($identifier)
|
2022-01-18 02:06:26 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
return preg_replace('/[^A-Za-z0-9_]+/', '', $this->_prefix . $identifier);
|
2016-07-11 14:15:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 17:02:20 +01:00
|
|
|
/**
|
|
|
|
* upgrade the database schema from an old version
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $oldversion
|
|
|
|
*/
|
2022-10-28 01:01:02 +02:00
|
|
|
private function _upgradeDatabase($oldversion)
|
2015-11-01 17:02:20 +01:00
|
|
|
{
|
2022-10-28 01:01:02 +02:00
|
|
|
$dataType = $this->_getDataType();
|
|
|
|
$attachmentType = $this->_getAttachmentType();
|
2016-07-26 08:19:35 +02:00
|
|
|
switch ($oldversion) {
|
2015-11-01 17:02:20 +01:00
|
|
|
case '0.21':
|
|
|
|
// create the meta column if necessary (pre 0.21 change)
|
|
|
|
try {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
|
|
|
'SELECT "meta" FROM "' . $this->_sanitizeIdentifier('paste') . '" ' .
|
|
|
|
($this->_type === 'oci' ? 'FETCH NEXT 1 ROWS ONLY' : 'LIMIT 1')
|
2022-01-24 21:43:31 +01:00
|
|
|
);
|
2015-11-01 17:02:20 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec('ALTER TABLE "' . $this->_sanitizeIdentifier('paste') . '" ADD COLUMN "meta" TEXT');
|
2015-11-01 17:02:20 +01:00
|
|
|
}
|
|
|
|
// SQLite only allows one ALTER statement at a time...
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\" ADD COLUMN \"attachment\" $attachmentType"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('paste') . "\" ADD COLUMN \"attachmentname\" $dataType"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
|
|
|
// SQLite doesn't support MODIFY, but it allows TEXT of similar
|
|
|
|
// size as BLOB, so there is no need to change it there
|
2022-10-28 01:01:02 +02:00
|
|
|
if ($this->_type !== 'sqlite') {
|
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\" ADD PRIMARY KEY (\"dataid\"), MODIFY COLUMN \"data\" $dataType"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('comment') .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\" ADD PRIMARY KEY (\"dataid\"), MODIFY COLUMN \"data\" $dataType, " .
|
|
|
|
"MODIFY COLUMN \"nickname\" $dataType, MODIFY COLUMN \"vizhash\" $dataType"
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
2022-06-28 06:51:21 +02:00
|
|
|
'CREATE UNIQUE INDEX IF NOT EXISTS "' .
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_sanitizeIdentifier('paste_dataid') . '" ON "' .
|
|
|
|
$this->_sanitizeIdentifier('paste') . '" ("dataid")'
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
2022-06-28 06:51:21 +02:00
|
|
|
'CREATE UNIQUE INDEX IF NOT EXISTS "' .
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_sanitizeIdentifier('comment_dataid') . '" ON "' .
|
|
|
|
$this->_sanitizeIdentifier('comment') . '" ("dataid")'
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
|
|
|
}
|
2022-06-29 22:25:54 +02:00
|
|
|
// CREATE INDEX IF NOT EXISTS not supported as of Oracle MySQL <= 8.0
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_db->exec(
|
2022-06-28 06:51:21 +02:00
|
|
|
'CREATE INDEX "' .
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_sanitizeIdentifier('comment_parent') . '" ON "' .
|
|
|
|
$this->_sanitizeIdentifier('comment') . '" ("pasteid")'
|
2015-11-01 17:02:20 +01:00
|
|
|
);
|
2017-10-04 21:55:03 +02:00
|
|
|
// no break, continue with updates for 0.22 and later
|
2019-09-20 06:57:54 +02:00
|
|
|
case '1.3':
|
|
|
|
// SQLite doesn't support MODIFY, but it allows TEXT of similar
|
|
|
|
// size as BLOB and PostgreSQL uses TEXT, so there is no need
|
|
|
|
// to change it there
|
2022-10-28 01:01:02 +02:00
|
|
|
if ($this->_type !== 'sqlite' && $this->_type !== 'pgsql') {
|
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('paste') .
|
2022-01-23 20:59:02 +01:00
|
|
|
"\" MODIFY COLUMN \"data\" $attachmentType"
|
2019-09-20 06:57:54 +02:00
|
|
|
);
|
|
|
|
}
|
2020-01-08 19:31:06 +01:00
|
|
|
// no break, continue with updates for all newer versions
|
2024-05-01 20:16:03 +02:00
|
|
|
case '1.7.2':
|
2024-05-01 21:01:12 +02:00
|
|
|
$supportsDropColumn = true;
|
|
|
|
if ($this->_type === 'sqlite') {
|
|
|
|
try {
|
2024-05-01 21:04:29 +02:00
|
|
|
$row = $this->_select('SELECT sqlite_version() AS "v"', array(), true);
|
2024-05-01 21:01:12 +02:00
|
|
|
$supportsDropColumn = version_compare($row['v'], '3.35.0', '>=');
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$supportsDropColumn = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($supportsDropColumn) {
|
|
|
|
$this->_db->exec(
|
|
|
|
'ALTER TABLE "' . $this->_sanitizeIdentifier('paste') .
|
|
|
|
'" DROP COLUMN "postdate"'
|
|
|
|
);
|
|
|
|
}
|
2024-05-01 20:16:03 +02:00
|
|
|
// no break, continue with updates for all newer versions
|
2017-10-04 21:55:03 +02:00
|
|
|
default:
|
2022-10-28 01:01:02 +02:00
|
|
|
$this->_exec(
|
|
|
|
'UPDATE "' . $this->_sanitizeIdentifier('config') .
|
2022-01-23 20:59:02 +01:00
|
|
|
'" SET "value" = ? WHERE "id" = ?',
|
2018-07-29 15:17:35 +02:00
|
|
|
array(Controller::VERSION, 'VERSION')
|
2016-08-25 09:53:31 +02:00
|
|
|
);
|
2015-11-01 17:02:20 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|