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
|
2021-04-05 10:44:12 -04:00
|
|
|
* @version 1.3.5
|
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-07-21 11:09:48 -04:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
2018-07-29 09:17:35 -04:00
|
|
|
use PrivateBin\Controller;
|
2019-05-13 16:31:52 -04:00
|
|
|
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
|
|
|
* Database
|
2012-04-29 13:15:06 -04:00
|
|
|
*
|
2016-08-09 05:54:42 -04:00
|
|
|
* Model for database access, implemented as a singleton.
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class Database extends AbstractData
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2015-08-16 09:55:31 -04:00
|
|
|
/**
|
|
|
|
* cache for select queries
|
|
|
|
*
|
|
|
|
* @var array
|
2012-05-19 17:59:41 -04:00
|
|
|
*/
|
|
|
|
private static $_cache = array();
|
|
|
|
|
2015-08-16 09:55:31 -04:00
|
|
|
/**
|
|
|
|
* instance of database connection
|
|
|
|
*
|
2012-04-29 13:15:06 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
2015-08-16 09:55:31 -04:00
|
|
|
* @var PDO
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
|
|
|
private static $_db;
|
|
|
|
|
2015-08-16 09:55:31 -04:00
|
|
|
/**
|
|
|
|
* table prefix
|
|
|
|
*
|
2012-05-19 17:59:41 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
2015-08-16 09:55:31 -04:00
|
|
|
* @var string
|
2012-05-19 17:59:41 -04:00
|
|
|
*/
|
|
|
|
private static $_prefix = '';
|
|
|
|
|
2015-08-16 09:55:31 -04:00
|
|
|
/**
|
|
|
|
* database type
|
|
|
|
*
|
2012-05-19 17:59:41 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
2015-08-16 09:55:31 -04:00
|
|
|
* @var string
|
2012-05-19 17:59:41 -04:00
|
|
|
*/
|
|
|
|
private static $_type = '';
|
|
|
|
|
2012-04-29 13:15:06 -04:00
|
|
|
/**
|
|
|
|
* get instance of singleton
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-16 09:55:31 -04:00
|
|
|
* @param array $options
|
2012-05-19 17:59:41 -04:00
|
|
|
* @throws Exception
|
2016-08-09 05:54:42 -04:00
|
|
|
* @return Database
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2019-05-10 16:21:03 -04:00
|
|
|
public static function getInstance(array $options)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
|
|
|
// if needed initialize the singleton
|
2016-08-15 10:45:47 -04:00
|
|
|
if (!(self::$_instance instanceof self)) {
|
2012-08-25 18:49:11 -04:00
|
|
|
self::$_instance = new self;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
|
2019-05-19 03:42:55 -04:00
|
|
|
// set table prefix if given
|
|
|
|
if (array_key_exists('tbl', $options)) {
|
|
|
|
self::$_prefix = $options['tbl'];
|
|
|
|
}
|
2015-11-01 11:02:20 -05:00
|
|
|
|
2019-05-19 03:42:55 -04: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
|
|
|
|
self::$_type = strtolower(
|
|
|
|
substr($options['dsn'], 0, strpos($options['dsn'], ':'))
|
|
|
|
);
|
|
|
|
$tableQuery = self::_getTableQuery(self::$_type);
|
|
|
|
self::$_db = new PDO(
|
|
|
|
$options['dsn'],
|
|
|
|
$options['usr'],
|
|
|
|
$options['pwd'],
|
|
|
|
$options['opt']
|
|
|
|
);
|
|
|
|
|
|
|
|
// check if the database contains the required tables
|
|
|
|
$tables = self::$_db->query($tableQuery)->fetchAll(PDO::FETCH_COLUMN, 0);
|
|
|
|
|
|
|
|
// create paste table if necessary
|
|
|
|
if (!in_array(self::_sanitizeIdentifier('paste'), $tables)) {
|
|
|
|
self::_createPasteTable();
|
|
|
|
$db_tables_exist = false;
|
|
|
|
}
|
2015-11-01 11:02:20 -05:00
|
|
|
|
2019-05-19 03:42:55 -04:00
|
|
|
// create comment table if necessary
|
|
|
|
if (!in_array(self::_sanitizeIdentifier('comment'), $tables)) {
|
|
|
|
self::_createCommentTable();
|
|
|
|
$db_tables_exist = false;
|
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
|
2019-05-19 03:42:55 -04:00
|
|
|
// create config table if necessary
|
|
|
|
$db_version = Controller::VERSION;
|
|
|
|
if (!in_array(self::_sanitizeIdentifier('config'), $tables)) {
|
|
|
|
self::_createConfigTable();
|
|
|
|
// 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 17:59:41 -04:00
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2019-05-19 03:42:55 -04:00
|
|
|
$db_version = self::_getConfig('VERSION');
|
|
|
|
}
|
|
|
|
|
|
|
|
// update database structure if necessary
|
|
|
|
if (version_compare($db_version, Controller::VERSION, '<')) {
|
|
|
|
self::_upgradeDatabase($db_version);
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
2019-05-19 03:42:55 -04: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 17:59:41 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 09:52:37 -04:00
|
|
|
return self::$_instance;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @param array $paste
|
2012-05-19 17:59:41 -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
|
|
|
{
|
2013-02-24 11:41:32 -05:00
|
|
|
if (
|
2015-09-21 16:32:52 -04:00
|
|
|
array_key_exists($pasteid, self::$_cache)
|
2012-08-25 18:49:11 -04:00
|
|
|
) {
|
2016-07-26 02:19:35 -04:00
|
|
|
if (false !== self::$_cache[$pasteid]) {
|
2012-08-25 18:49:11 -04:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
unset(self::$_cache[$pasteid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 08:36:47 -04:00
|
|
|
$expire_date = 0;
|
|
|
|
$opendiscussion = $burnafterreading = false;
|
|
|
|
$attachment = $attachmentname = null;
|
|
|
|
$meta = $paste['meta'];
|
|
|
|
$isVersion1 = array_key_exists('data', $paste);
|
|
|
|
list($createdKey) = self::_getVersionedKeys($isVersion1 ? 1 : 2);
|
|
|
|
$created = (int) $meta[$createdKey];
|
|
|
|
unset($meta[$createdKey], $paste['meta']);
|
|
|
|
if (array_key_exists('expire_date', $meta)) {
|
|
|
|
$expire_date = (int) $meta['expire_date'];
|
2015-09-26 21:03:55 -04:00
|
|
|
unset($meta['expire_date']);
|
|
|
|
}
|
2019-05-05 08:36:47 -04:00
|
|
|
if (array_key_exists('opendiscussion', $meta)) {
|
2019-05-05 12:22:57 -04:00
|
|
|
$opendiscussion = $meta['opendiscussion'];
|
2015-09-21 16:32:52 -04:00
|
|
|
unset($meta['opendiscussion']);
|
|
|
|
}
|
2019-05-05 08:36:47 -04:00
|
|
|
if (array_key_exists('burnafterreading', $meta)) {
|
2019-05-05 12:22:57 -04:00
|
|
|
$burnafterreading = $meta['burnafterreading'];
|
2015-09-21 16:32:52 -04:00
|
|
|
unset($meta['burnafterreading']);
|
|
|
|
}
|
2019-05-05 08:36:47 -04: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 12:22:57 -04:00
|
|
|
} else {
|
|
|
|
$opendiscussion = $paste['adata'][2];
|
|
|
|
$burnafterreading = $paste['adata'][3];
|
2015-11-01 11:02:20 -05:00
|
|
|
}
|
2021-06-13 04:44:26 -04:00
|
|
|
try {
|
2022-01-17 20:06:26 -05:00
|
|
|
$big_string = $isVersion1 ? $paste['data'] : Json::encode($paste);
|
|
|
|
if (self::$_type === 'oci') {
|
|
|
|
# It is not possible to execute in the normal way if strlen($big_string) >= 4000
|
|
|
|
$stmt = self::$_db->prepare(
|
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
|
|
|
|
' VALUES(?,?,?,?,?,?,?,?,?)'
|
|
|
|
);
|
|
|
|
$stmt->bindParam(1, $pasteid);
|
|
|
|
$stmt->bindParam(2, $big_string, PDO::PARAM_STR, strlen($big_string));
|
|
|
|
$stmt->bindParam(3, $created, PDO::PARAM_INT);
|
|
|
|
$stmt->bindParam(4, $expire_date, PDO::PARAM_INT);
|
|
|
|
$stmt->bindParam(5, $opendiscussion, PDO::PARAM_INT);
|
|
|
|
$stmt->bindParam(6, $burnafterreading, PDO::PARAM_INT);
|
|
|
|
$stmt->bindParam(7, Json::encode($meta));
|
|
|
|
$stmt->bindParam(8, $attachment, PDO::PARAM_STR, strlen($attachment));
|
|
|
|
$stmt->bindParam(9, $attachmentname);
|
|
|
|
return $stmt->execute();
|
|
|
|
}
|
2021-06-13 04:44:26 -04:00
|
|
|
return self::_exec(
|
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
|
|
|
|
' VALUES(?,?,?,?,?,?,?,?,?)',
|
|
|
|
array(
|
|
|
|
$pasteid,
|
2022-01-17 20:06:26 -05:00
|
|
|
$big_string,
|
2021-06-13 04:44:26 -04:00
|
|
|
$created,
|
|
|
|
$expire_date,
|
|
|
|
(int) $opendiscussion,
|
|
|
|
(int) $burnafterreading,
|
|
|
|
Json::encode($meta),
|
|
|
|
$attachment,
|
|
|
|
$attachmentname,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
2019-05-05 15:03:58 -04:00
|
|
|
* @return array|false
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function read($pasteid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2019-05-05 15:03:58 -04:00
|
|
|
if (array_key_exists($pasteid, self::$_cache)) {
|
|
|
|
return self::$_cache[$pasteid];
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
|
2019-05-05 15:03:58 -04:00
|
|
|
self::$_cache[$pasteid] = false;
|
2022-01-17 20:06:26 -05:00
|
|
|
$rawData = "";
|
2021-06-13 06:40:06 -04:00
|
|
|
try {
|
2021-06-14 00:44:30 -04:00
|
|
|
$paste = self::_select(
|
2021-06-13 06:40:06 -04:00
|
|
|
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
|
|
|
|
' WHERE dataid = ?', array($pasteid), true
|
|
|
|
);
|
2022-01-17 20:06:26 -05:00
|
|
|
if ($paste !== false) {
|
|
|
|
$rawData = self::$_type === 'oci' ? self::_clob($paste['DATA']) : $paste['data'];
|
|
|
|
}
|
2021-06-13 06:40:06 -04:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$paste = false;
|
|
|
|
}
|
2019-05-05 15:03:58 -04:00
|
|
|
if ($paste === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// create array
|
2022-01-17 20:06:26 -05:00
|
|
|
$data = Json::decode($rawData);
|
2019-05-05 15:03:58 -04:00
|
|
|
$isVersion2 = array_key_exists('v', $data) && $data['v'] >= 2;
|
|
|
|
if ($isVersion2) {
|
|
|
|
self::$_cache[$pasteid] = $data;
|
2019-05-10 15:45:34 -04:00
|
|
|
list($createdKey) = self::_getVersionedKeys(2);
|
2019-05-05 15:03:58 -04:00
|
|
|
} else {
|
2022-01-17 20:06:26 -05:00
|
|
|
self::$_cache[$pasteid] = array('data' => $paste[self::_sanitizeColumn('data')]);
|
2019-05-10 15:45:34 -04:00
|
|
|
list($createdKey) = self::_getVersionedKeys(1);
|
2019-05-05 15:03:58 -04:00
|
|
|
}
|
2019-05-05 12:22:57 -04:00
|
|
|
|
2019-05-19 03:42:55 -04:00
|
|
|
try {
|
2022-01-17 20:06:26 -05:00
|
|
|
$paste['meta'] = Json::decode($paste[self::_sanitizeColumn('meta')]);
|
2019-05-19 03:42:55 -04:00
|
|
|
} catch (Exception $e) {
|
2019-05-08 16:11:21 -04:00
|
|
|
$paste['meta'] = array();
|
2019-05-05 15:03:58 -04:00
|
|
|
}
|
2019-05-10 15:45:34 -04:00
|
|
|
$paste = self::upgradePreV1Format($paste);
|
|
|
|
self::$_cache[$pasteid]['meta'] = $paste['meta'];
|
2022-01-17 20:06:26 -05:00
|
|
|
self::$_cache[$pasteid]['meta'][$createdKey] = (int) $paste[self::_sanitizeColumn('postdate')];
|
|
|
|
$expire_date = (int) $paste[self::_sanitizeColumn('expiredate')];
|
2019-05-05 15:03:58 -04:00
|
|
|
if ($expire_date > 0) {
|
|
|
|
self::$_cache[$pasteid]['meta']['expire_date'] = $expire_date;
|
|
|
|
}
|
|
|
|
if ($isVersion2) {
|
|
|
|
return self::$_cache[$pasteid];
|
|
|
|
}
|
|
|
|
|
|
|
|
// support v1 attachments
|
2022-01-17 20:06:26 -05:00
|
|
|
if (array_key_exists(self::_sanitizeColumn('attachment'), $paste) && strlen($paste[self::_sanitizeColumn('attachment')])) {
|
|
|
|
self::$_cache[$pasteid]['attachment'] = $paste[self::_sanitizeColumn('attachment')];
|
|
|
|
if (array_key_exists(self::_sanitizeColumn('attachmentname'), $paste) && strlen($paste[self::_sanitizeColumn('attachmentname')])) {
|
|
|
|
self::$_cache[$pasteid]['attachmentname'] = $paste[self::_sanitizeColumn('attachmentname')];
|
2012-08-25 18:49:11 -04:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 20:06:26 -05:00
|
|
|
if ($paste[self::_sanitizeColumn('opendiscussion')]) {
|
2019-05-05 15:03:58 -04:00
|
|
|
self::$_cache[$pasteid]['meta']['opendiscussion'] = true;
|
|
|
|
}
|
2022-01-17 20:06:26 -05:00
|
|
|
if ($paste[self::_sanitizeColumn('burnafterreading')]) {
|
2019-05-05 15:03:58 -04:00
|
|
|
self::$_cache[$pasteid]['meta']['burnafterreading'] = true;
|
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
|
2012-08-25 18:49:11 -04:00
|
|
|
return self::$_cache[$pasteid];
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a paste and its discussion.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function delete($pasteid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2012-05-19 17:59:41 -04:00
|
|
|
self::_exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'DELETE FROM ' . self::_sanitizeIdentifier('paste') .
|
|
|
|
' WHERE dataid = ?', array($pasteid)
|
2012-05-19 17:59:41 -04:00
|
|
|
);
|
|
|
|
self::_exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'DELETE FROM ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' WHERE pasteid = ?', array($pasteid)
|
2012-05-19 17:59:41 -04:00
|
|
|
);
|
2012-08-25 18:49:11 -04:00
|
|
|
if (
|
2015-08-27 15:41:21 -04:00
|
|
|
array_key_exists($pasteid, self::$_cache)
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
|
|
|
unset(self::$_cache[$pasteid]);
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a paste exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-15 11:02:59 -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:35:36 -04:00
|
|
|
public function exists($pasteid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2012-05-19 17:59:41 -04:00
|
|
|
if (
|
|
|
|
!array_key_exists($pasteid, self::$_cache)
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
|
|
|
self::$_cache[$pasteid] = $this->read($pasteid);
|
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
return (bool) self::$_cache[$pasteid];
|
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
|
2016-07-11 09:47:42 -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
|
|
|
{
|
2019-05-05 08:36:47 -04:00
|
|
|
if (array_key_exists('data', $comment)) {
|
|
|
|
$version = 1;
|
|
|
|
$data = $comment['data'];
|
|
|
|
} else {
|
|
|
|
$version = 2;
|
2019-05-13 16:31:52 -04:00
|
|
|
$data = Json::encode($comment);
|
2019-05-05 08:36:47 -04:00
|
|
|
}
|
|
|
|
list($createdKey, $iconKey) = self::_getVersionedKeys($version);
|
2019-05-10 15:45:34 -04:00
|
|
|
$meta = $comment['meta'];
|
2019-05-05 08:36:47 -04:00
|
|
|
unset($comment['meta']);
|
|
|
|
foreach (array('nickname', $iconKey) as $key) {
|
|
|
|
if (!array_key_exists($key, $meta)) {
|
|
|
|
$meta[$key] = null;
|
2016-07-18 04:14:38 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 04:44:26 -04:00
|
|
|
try {
|
2022-01-17 20:06:26 -05:00
|
|
|
if (self::$_type === 'oci') {
|
|
|
|
# It is not possible to execute in the normal way if strlen($big_string) >= 4000
|
|
|
|
$stmt = self::$_db->prepare(
|
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' VALUES(?,?,?,?,?,?,?)'
|
|
|
|
);
|
|
|
|
$stmt->bindParam(1, $commentid);
|
|
|
|
$stmt->bindParam(2, $pasteid);
|
|
|
|
$stmt->bindParam(3, $parentid);
|
|
|
|
$stmt->bindParam(4, $data, PDO::PARAM_STR, strlen($data));
|
|
|
|
$stmt->bindParam(5, $meta['nickname']);
|
|
|
|
$stmt->bindParam(6, $meta[$iconKey]);
|
|
|
|
$stmt->bindParam(7, $meta[$createdKey], PDO::PARAM_INT);
|
|
|
|
return $stmt->execute();
|
|
|
|
}
|
2021-06-13 04:44:26 -04:00
|
|
|
return self::_exec(
|
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' VALUES(?,?,?,?,?,?,?)',
|
|
|
|
array(
|
|
|
|
$commentid,
|
|
|
|
$pasteid,
|
|
|
|
$parentid,
|
|
|
|
$data,
|
|
|
|
$meta['nickname'],
|
|
|
|
$meta[$iconKey],
|
|
|
|
$meta[$createdKey],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all comments of paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $pasteid
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function readComments($pasteid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2012-05-19 17:59:41 -04:00
|
|
|
$rows = self::_select(
|
2016-07-11 08:15:20 -04:00
|
|
|
'SELECT * FROM ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' WHERE pasteid = ?', array($pasteid)
|
2012-05-19 17:59:41 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// create comment list
|
|
|
|
$comments = array();
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count($rows)) {
|
|
|
|
foreach ($rows as $row) {
|
2022-01-17 20:06:26 -05:00
|
|
|
$i = $this->getOpenSlot($comments, (int) $row[self::_sanitizeColumn('postdate')]);
|
|
|
|
$id = $row[self::_sanitizeColumn('dataid')];
|
|
|
|
if (self::$_type === 'oci') {
|
|
|
|
$newrow = self::_select(
|
|
|
|
'SELECT data FROM ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' WHERE dataid = ?', array($id), true
|
|
|
|
);
|
|
|
|
$rawData = self::_clob($newrow['DATA']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$rawData = $row['data'];
|
|
|
|
}
|
|
|
|
$data = Json::decode($rawData);
|
2019-05-05 15:03:58 -04:00
|
|
|
if (array_key_exists('v', $data) && $data['v'] >= 2) {
|
2019-05-05 08:36:47 -04:00
|
|
|
$version = 2;
|
|
|
|
$comments[$i] = $data;
|
|
|
|
} else {
|
2019-05-05 15:03:58 -04:00
|
|
|
$version = 1;
|
2022-01-17 20:06:26 -05:00
|
|
|
$comments[$i] = array('data' => $rawData);
|
2019-05-05 08:36:47 -04:00
|
|
|
}
|
|
|
|
list($createdKey, $iconKey) = self::_getVersionedKeys($version);
|
2022-01-17 20:06:26 -05:00
|
|
|
$comments[$i]['id'] = $id;
|
|
|
|
$comments[$i]['parentid'] = $row[self::_sanitizeColumn('parentid')];
|
|
|
|
$comments[$i]['meta'] = array($createdKey => (int) $row[self::_sanitizeColumn('postdate')]);
|
2019-05-05 08:36:47 -04:00
|
|
|
foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
|
2022-01-17 20:06:26 -05:00
|
|
|
if (array_key_exists(self::_sanitizeColumn($rowKey), $row) && !empty($row[self::_sanitizeColumn($rowKey)])) {
|
|
|
|
$comments[$i]['meta'][$commentKey] = $row[self::_sanitizeColumn($rowKey)];
|
2017-03-24 19:58:59 -04:00
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
}
|
|
|
|
ksort($comments);
|
|
|
|
}
|
|
|
|
return $comments;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a comment exists.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-15 11:02:59 -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
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
2012-04-29 13:15:06 -04:00
|
|
|
{
|
2021-06-13 04:44:26 -04:00
|
|
|
try {
|
|
|
|
return (bool) self::_select(
|
|
|
|
'SELECT dataid FROM ' . self::_sanitizeIdentifier('comment') .
|
|
|
|
' WHERE pasteid = ? AND parentid = ? AND dataid = ?',
|
|
|
|
array($pasteid, $parentid, $commentid), true
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-19 17:59:41 -04:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:02:47 -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-09 01:47:40 -04:00
|
|
|
if ($namespace === 'traffic_limiter') {
|
2021-06-13 04:53:01 -04:00
|
|
|
self::$_last_cache[$key] = $value;
|
2021-06-09 01:47:40 -04:00
|
|
|
try {
|
2021-06-13 04:53:01 -04:00
|
|
|
$value = Json::encode(self::$_last_cache);
|
2021-06-09 01:47:40 -04:00
|
|
|
} catch (Exception $e) {
|
2021-06-07 01:02:47 -04:00
|
|
|
return false;
|
2021-06-09 01:47:40 -04:00
|
|
|
}
|
2021-06-07 01:02:47 -04:00
|
|
|
}
|
2021-06-09 01:47:40 -04:00
|
|
|
return self::_exec(
|
|
|
|
'UPDATE ' . self::_sanitizeIdentifier('config') .
|
|
|
|
' SET value = ? WHERE id = ?',
|
|
|
|
array($value, strtoupper($namespace))
|
|
|
|
);
|
2021-06-07 01:02:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a value.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2021-06-09 01:47:40 -04:00
|
|
|
$configKey = strtoupper($namespace);
|
2021-06-09 13:16:22 -04:00
|
|
|
$value = $this->_getConfig($configKey);
|
2021-06-09 01:47:40 -04:00
|
|
|
if ($value === '') {
|
|
|
|
// initialize the row, so that setValue can rely on UPDATE queries
|
|
|
|
self::_exec(
|
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('config') .
|
|
|
|
' VALUES(?,?)',
|
|
|
|
array($configKey, '')
|
|
|
|
);
|
2021-06-07 01:02:47 -04:00
|
|
|
|
2021-06-09 01:47:40 -04:00
|
|
|
// migrate filesystem based salt into database
|
|
|
|
$file = 'data' . DIRECTORY_SEPARATOR . 'salt.php';
|
|
|
|
if ($namespace === 'salt' && is_readable($file)) {
|
|
|
|
$value = Filesystem::getInstance(array('dir' => 'data'))->getValue('salt');
|
|
|
|
$this->setValue($value, 'salt');
|
|
|
|
@unlink($file);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($value && $namespace === 'traffic_limiter') {
|
|
|
|
try {
|
2021-06-13 04:53:01 -04:00
|
|
|
self::$_last_cache = Json::decode($value);
|
2021-06-09 01:47:40 -04:00
|
|
|
} catch (Exception $e) {
|
2021-06-13 04:53:01 -04:00
|
|
|
self::$_last_cache = array();
|
2021-06-09 01:47:40 -04:00
|
|
|
}
|
2021-06-13 04:53:01 -04:00
|
|
|
if (array_key_exists($key, self::$_last_cache)) {
|
|
|
|
return self::$_last_cache[$key];
|
2021-06-09 01:47:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (string) $value;
|
2021-06-07 01:02:47 -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
|
|
|
{
|
|
|
|
$pastes = array();
|
2016-08-15 10:45:47 -04:00
|
|
|
$rows = self::_select(
|
2016-07-15 11:02:59 -04:00
|
|
|
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
|
2022-01-17 20:06:26 -05:00
|
|
|
' WHERE expiredate < ? AND expiredate != ? ' .
|
|
|
|
(self::$_type === 'oci' ? 'FETCH NEXT ? ROWS ONLY' : 'LIMIT ?'),
|
2019-05-05 15:03:58 -04:00
|
|
|
array(time(), 0, $batchsize)
|
2016-07-15 11:02:59 -04:00
|
|
|
);
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count($rows)) {
|
|
|
|
foreach ($rows as $row) {
|
2016-07-15 11:02:59 -04:00
|
|
|
$pastes[] = $row['dataid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pastes;
|
|
|
|
}
|
|
|
|
|
2012-05-19 17:59:41 -04:00
|
|
|
/**
|
|
|
|
* execute a statement
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
|
|
|
* @throws PDOException
|
2016-07-11 09:47:42 -04:00
|
|
|
* @return bool
|
2012-05-19 17:59:41 -04:00
|
|
|
*/
|
2019-05-10 15:35:36 -04:00
|
|
|
private static function _exec($sql, array $params)
|
2012-05-19 17:59:41 -04:00
|
|
|
{
|
|
|
|
$statement = self::$_db->prepare($sql);
|
2016-08-15 10:45:47 -04:00
|
|
|
$result = $statement->execute($params);
|
2012-05-19 17:59:41 -04:00
|
|
|
$statement->closeCursor();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run a select statement
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
|
|
|
* @param bool $firstOnly if only the first row should be returned
|
|
|
|
* @throws PDOException
|
2019-05-19 03:42:55 -04:00
|
|
|
* @return array|false
|
2012-05-19 17:59:41 -04:00
|
|
|
*/
|
2019-05-10 16:04:47 -04:00
|
|
|
private static function _select($sql, array $params, $firstOnly = false)
|
2012-05-19 17:59:41 -04:00
|
|
|
{
|
|
|
|
$statement = self::$_db->prepare($sql);
|
|
|
|
$statement->execute($params);
|
|
|
|
$result = $firstOnly ?
|
|
|
|
$statement->fetch(PDO::FETCH_ASSOC) :
|
|
|
|
$statement->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$statement->closeCursor();
|
|
|
|
return $result;
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|
2015-11-01 11:02:20 -05:00
|
|
|
|
2019-05-05 08:36:47 -04:00
|
|
|
/**
|
|
|
|
* get version dependent key names
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param int $version
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-05-10 15:52:14 -04:00
|
|
|
private static function _getVersionedKeys($version)
|
2019-05-05 08:36:47 -04:00
|
|
|
{
|
|
|
|
if ($version === 1) {
|
|
|
|
return array('postdate', 'vizhash');
|
|
|
|
}
|
|
|
|
return array('created', 'icon');
|
|
|
|
}
|
|
|
|
|
2015-11-01 11:02:20 -05:00
|
|
|
/**
|
|
|
|
* get table list query, depending on the database type
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $type
|
|
|
|
* @throws Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _getTableQuery($type)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
switch ($type) {
|
2015-11-01 11:02:20 -05:00
|
|
|
case 'ibm':
|
|
|
|
$sql = 'SELECT tabname FROM SYSCAT.TABLES ';
|
|
|
|
break;
|
|
|
|
case 'informix':
|
|
|
|
$sql = 'SELECT tabname FROM systables ';
|
|
|
|
break;
|
|
|
|
case 'mssql':
|
2016-08-15 10:45:47 -04:00
|
|
|
$sql = 'SELECT name FROM sysobjects '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "WHERE type = 'U' ORDER BY name";
|
|
|
|
break;
|
|
|
|
case 'mysql':
|
|
|
|
$sql = 'SHOW TABLES';
|
|
|
|
break;
|
|
|
|
case 'oci':
|
|
|
|
$sql = 'SELECT table_name FROM all_tables';
|
|
|
|
break;
|
|
|
|
case 'pgsql':
|
2016-08-15 10:45:47 -04:00
|
|
|
$sql = 'SELECT c.relname AS table_name '
|
|
|
|
. 'FROM pg_class c, pg_user u '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
|
2016-08-15 10:45:47 -04:00
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "AND c.relname !~ '^(pg_|sql_)' "
|
2016-08-15 10:45:47 -04:00
|
|
|
. 'UNION '
|
|
|
|
. 'SELECT c.relname AS table_name '
|
|
|
|
. 'FROM pg_class c '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "WHERE c.relkind = 'r' "
|
2016-08-15 10:45:47 -04:00
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
|
|
|
|
. 'AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "AND c.relname !~ '^pg_'";
|
|
|
|
break;
|
|
|
|
case 'sqlite':
|
|
|
|
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
|
2016-08-15 10:45:47 -04:00
|
|
|
. 'UNION ALL SELECT name FROM sqlite_temp_master '
|
2015-11-01 11:02:20 -05:00
|
|
|
. "WHERE type='table' ORDER BY name";
|
|
|
|
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
|
|
|
|
* @static
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _getConfig($key)
|
|
|
|
{
|
2021-06-13 06:40:06 -04:00
|
|
|
try {
|
|
|
|
$row = self::_select(
|
|
|
|
'SELECT value FROM ' . self::_sanitizeIdentifier('config') .
|
|
|
|
' WHERE id = ?', array($key), true
|
|
|
|
);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-01-17 20:06:26 -05:00
|
|
|
return $row ? $row[self::_sanitizeColumn('value')] : '';
|
2015-11-01 11:02:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the primary key clauses, depending on the database driver
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
2019-05-05 02:53:40 -04:00
|
|
|
* @param string $key
|
2015-11-01 11:02:20 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function _getPrimaryKeyClauses($key = 'dataid')
|
|
|
|
{
|
|
|
|
$main_key = $after_key = '';
|
2016-07-26 02:19:35 -04:00
|
|
|
if (self::$_type === 'mysql') {
|
2015-11-01 11:02:20 -05:00
|
|
|
$after_key = ", PRIMARY KEY ($key)";
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2015-11-01 11:02:20 -05:00
|
|
|
$main_key = ' PRIMARY KEY';
|
|
|
|
}
|
|
|
|
return array($main_key, $after_key);
|
|
|
|
}
|
|
|
|
|
2019-05-05 02:53:40 -04:00
|
|
|
/**
|
|
|
|
* get the data type, depending on the database driver
|
|
|
|
*
|
2019-09-20 00:57:54 -04:00
|
|
|
* PostgreSQL uses a different API for BLOBs then SQL, hence we use TEXT
|
|
|
|
*
|
2019-05-05 02:53:40 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _getDataType()
|
|
|
|
{
|
|
|
|
return self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the attachment type, depending on the database driver
|
|
|
|
*
|
2019-09-20 00:57:54 -04:00
|
|
|
* PostgreSQL uses a different API for BLOBs then SQL, hence we use TEXT
|
|
|
|
*
|
2019-05-05 02:53:40 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _getAttachmentType()
|
|
|
|
{
|
|
|
|
return self::$_type === 'pgsql' ? 'TEXT' : 'MEDIUMBLOB';
|
|
|
|
}
|
|
|
|
|
2015-11-01 11:02:20 -05:00
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
private static function _createPasteTable()
|
|
|
|
{
|
|
|
|
list($main_key, $after_key) = self::_getPrimaryKeyClauses();
|
2019-05-05 02:53:40 -04:00
|
|
|
$dataType = self::_getDataType();
|
2019-09-20 00:57:54 -04:00
|
|
|
$attachmentType = self::_getAttachmentType();
|
2015-11-01 11:02:20 -05:00
|
|
|
self::$_db->exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'CREATE TABLE ' . self::_sanitizeIdentifier('paste') . ' ( ' .
|
2015-11-01 11:02:20 -05:00
|
|
|
"dataid CHAR(16) NOT NULL$main_key, " .
|
2019-09-20 00:57:54 -04:00
|
|
|
"data $attachmentType, " .
|
2015-11-01 11:02:20 -05:00
|
|
|
'postdate INT, ' .
|
|
|
|
'expiredate INT, ' .
|
|
|
|
'opendiscussion INT, ' .
|
|
|
|
'burnafterreading INT, ' .
|
|
|
|
'meta TEXT, ' .
|
2019-09-20 00:57:54 -04:00
|
|
|
"attachment $attachmentType, " .
|
2016-07-18 09:55:51 -04:00
|
|
|
"attachmentname $dataType$after_key );"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
private static function _createCommentTable()
|
|
|
|
{
|
|
|
|
list($main_key, $after_key) = self::_getPrimaryKeyClauses();
|
2019-05-05 02:53:40 -04:00
|
|
|
$dataType = self::_getDataType();
|
2015-11-01 11:02:20 -05:00
|
|
|
self::$_db->exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'CREATE TABLE ' . self::_sanitizeIdentifier('comment') . ' ( ' .
|
2015-11-01 11:02:20 -05:00
|
|
|
"dataid CHAR(16) NOT NULL$main_key, " .
|
|
|
|
'pasteid CHAR(16), ' .
|
|
|
|
'parentid CHAR(16), ' .
|
2016-07-18 09:55:51 -04:00
|
|
|
"data $dataType, " .
|
|
|
|
"nickname $dataType, " .
|
|
|
|
"vizhash $dataType, " .
|
2015-11-01 11:02:20 -05:00
|
|
|
"postdate INT$after_key );"
|
|
|
|
);
|
|
|
|
self::$_db->exec(
|
2016-07-18 08:47:32 -04:00
|
|
|
'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
|
|
|
|
self::_sanitizeIdentifier('comment') . '(pasteid);'
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the paste table
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
private static function _createConfigTable()
|
|
|
|
{
|
|
|
|
list($main_key, $after_key) = self::_getPrimaryKeyClauses('id');
|
|
|
|
self::$_db->exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'CREATE TABLE ' . self::_sanitizeIdentifier('config') .
|
|
|
|
" ( id CHAR(16) NOT NULL$main_key, value TEXT$after_key );"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
self::_exec(
|
2016-07-11 08:15:20 -04:00
|
|
|
'INSERT INTO ' . self::_sanitizeIdentifier('config') .
|
|
|
|
' VALUES(?,?)',
|
2018-07-29 09:17:35 -04:00
|
|
|
array('VERSION', Controller::VERSION)
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:15:20 -04:00
|
|
|
/**
|
|
|
|
* sanitizes identifiers
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _sanitizeIdentifier($identifier)
|
|
|
|
{
|
2022-01-17 20:06:26 -05:00
|
|
|
$id = preg_replace('/[^A-Za-z0-9_]+/', '', self::$_prefix . $identifier);
|
|
|
|
return self::_sanitizeColumn($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sanitizes column name because OCI
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _sanitizeColumn($name)
|
|
|
|
{
|
|
|
|
return self::$_type === 'oci' ? strtoupper($name) : $name;
|
2016-07-11 08:15:20 -04:00
|
|
|
}
|
|
|
|
|
2015-11-01 11:02:20 -05:00
|
|
|
/**
|
|
|
|
* upgrade the database schema from an old version
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param string $oldversion
|
|
|
|
*/
|
|
|
|
private static function _upgradeDatabase($oldversion)
|
|
|
|
{
|
2019-09-20 00:57:54 -04:00
|
|
|
$dataType = self::_getDataType();
|
|
|
|
$attachmentType = self::_getAttachmentType();
|
2016-07-26 02:19:35 -04:00
|
|
|
switch ($oldversion) {
|
2015-11-01 11:02:20 -05:00
|
|
|
case '0.21':
|
|
|
|
// create the meta column if necessary (pre 0.21 change)
|
|
|
|
try {
|
2016-07-11 08:33:45 -04:00
|
|
|
self::$_db->exec('SELECT meta FROM ' . self::_sanitizeIdentifier('paste') . ' LIMIT 1;');
|
2015-11-01 11:02:20 -05:00
|
|
|
} catch (PDOException $e) {
|
2016-07-11 08:33:45 -04:00
|
|
|
self::$_db->exec('ALTER TABLE ' . self::_sanitizeIdentifier('paste') . ' ADD COLUMN meta TEXT;');
|
2015-11-01 11:02:20 -05:00
|
|
|
}
|
|
|
|
// SQLite only allows one ALTER statement at a time...
|
|
|
|
self::$_db->exec(
|
2016-07-18 09:55:51 -04:00
|
|
|
'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
|
2019-09-20 00:57:54 -04:00
|
|
|
" ADD COLUMN attachment $attachmentType;"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
self::$_db->exec(
|
2016-07-18 09:55:51 -04:00
|
|
|
'ALTER TABLE ' . self::_sanitizeIdentifier('paste') . " ADD COLUMN attachmentname $dataType;"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
// SQLite doesn't support MODIFY, but it allows TEXT of similar
|
|
|
|
// size as BLOB, so there is no need to change it there
|
2016-07-26 02:19:35 -04:00
|
|
|
if (self::$_type !== 'sqlite') {
|
2015-11-01 11:02:20 -05:00
|
|
|
self::$_db->exec(
|
2016-07-11 08:33:45 -04:00
|
|
|
'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
|
2019-09-20 00:57:54 -04:00
|
|
|
" ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType;"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
self::$_db->exec(
|
2016-07-11 08:33:45 -04:00
|
|
|
'ALTER TABLE ' . self::_sanitizeIdentifier('comment') .
|
2016-07-18 09:55:51 -04:00
|
|
|
" ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType, " .
|
|
|
|
"MODIFY COLUMN nickname $dataType, MODIFY COLUMN vizhash $dataType;"
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2015-11-01 11:02:20 -05:00
|
|
|
self::$_db->exec(
|
2016-07-18 08:47:32 -04:00
|
|
|
'CREATE UNIQUE INDEX IF NOT EXISTS paste_dataid ON ' .
|
|
|
|
self::_sanitizeIdentifier('paste') . '(dataid);'
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
self::$_db->exec(
|
2016-07-18 08:47:32 -04:00
|
|
|
'CREATE UNIQUE INDEX IF NOT EXISTS comment_dataid ON ' .
|
|
|
|
self::_sanitizeIdentifier('comment') . '(dataid);'
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
self::$_db->exec(
|
2016-07-18 08:47:32 -04:00
|
|
|
'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
|
|
|
|
self::_sanitizeIdentifier('comment') . '(pasteid);'
|
2015-11-01 11:02:20 -05:00
|
|
|
);
|
2017-10-04 15:55:03 -04:00
|
|
|
// no break, continue with updates for 0.22 and later
|
2019-09-20 00:57:54 -04: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
|
|
|
|
if (self::$_type !== 'sqlite' && self::$_type !== 'pgsql') {
|
|
|
|
self::$_db->exec(
|
|
|
|
'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
|
|
|
|
" MODIFY COLUMN data $attachmentType;"
|
|
|
|
);
|
|
|
|
}
|
2020-01-08 13:31:06 -05:00
|
|
|
// no break, continue with updates for all newer versions
|
2017-10-04 15:55:03 -04:00
|
|
|
default:
|
2016-08-25 03:53:31 -04:00
|
|
|
self::_exec(
|
|
|
|
'UPDATE ' . self::_sanitizeIdentifier('config') .
|
|
|
|
' SET value = ? WHERE id = ?',
|
2018-07-29 09:17:35 -04:00
|
|
|
array(Controller::VERSION, 'VERSION')
|
2016-08-25 03:53:31 -04:00
|
|
|
);
|
2015-11-01 11:02:20 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 20:06:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* read CLOB for OCI
|
|
|
|
* https://stackoverflow.com/questions/36200534/pdo-oci-into-a-clob-field
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @param object $column
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function _clob($column)
|
|
|
|
{
|
|
|
|
if ($column == null) return null;
|
|
|
|
$str = "";
|
|
|
|
while ($column !== null and $tmp = fread($column, 1024))
|
|
|
|
$str .= $tmp;
|
|
|
|
return $str;
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|