2024-06-04 01:13:55 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-09-22 17:21:31 -04:00
|
|
|
/**
|
2016-07-11 05:58:15 -04:00
|
|
|
* PrivateBin
|
2015-09-22 17:21:31 -04:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-22 17:21:31 -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
|
2015-09-22 17:21:31 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:50:00 -05:00
|
|
|
namespace PrivateBin;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PDO;
|
|
|
|
|
2015-09-22 17:21:31 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* Configuration
|
2015-09-22 17:21:31 -04:00
|
|
|
*
|
|
|
|
* parses configuration file, ensures default values present
|
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class Configuration
|
2015-09-22 17:21:31 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* parsed configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_configuration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* default configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-11 08:15:20 -04:00
|
|
|
private static $_defaults = array(
|
2015-09-22 17:21:31 -04:00
|
|
|
'main' => array(
|
2017-01-01 10:33:11 -05:00
|
|
|
'name' => 'PrivateBin',
|
2020-07-03 15:00:42 -04:00
|
|
|
'basepath' => '',
|
2016-10-29 04:24:08 -04:00
|
|
|
'discussion' => true,
|
|
|
|
'opendiscussion' => false,
|
2024-05-04 08:38:41 -04:00
|
|
|
'discussiondatedisplay' => true,
|
2016-10-29 04:24:08 -04:00
|
|
|
'password' => true,
|
|
|
|
'fileupload' => false,
|
2015-09-22 17:21:31 -04:00
|
|
|
'burnafterreadingselected' => false,
|
2016-10-29 04:24:08 -04:00
|
|
|
'defaultformatter' => 'plaintext',
|
2022-02-15 13:02:44 -05:00
|
|
|
'syntaxhighlightingtheme' => '',
|
2019-09-20 01:04:26 -04:00
|
|
|
'sizelimit' => 10485760,
|
2016-10-29 04:24:08 -04:00
|
|
|
'template' => 'bootstrap',
|
2020-10-13 01:28:35 -04:00
|
|
|
'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
|
2016-10-29 04:24:08 -04:00
|
|
|
'notice' => '',
|
|
|
|
'languageselection' => false,
|
|
|
|
'languagedefault' => '',
|
|
|
|
'urlshortener' => '',
|
2017-12-25 08:59:15 -05:00
|
|
|
'qrcode' => true,
|
2023-09-09 16:26:11 -04:00
|
|
|
'email' => true,
|
2022-10-30 04:05:29 -04:00
|
|
|
'icon' => 'identicon',
|
2022-03-27 02:45:33 -04:00
|
|
|
'cspheader' => 'default-src \'none\'; base-uri \'self\'; form-action \'none\'; manifest-src \'self\'; connect-src * blob:; script-src \'self\' \'unsafe-eval\'; style-src \'self\'; font-src \'self\'; frame-ancestors \'none\'; img-src \'self\' data: blob:; media-src blob:; object-src blob:; sandbox allow-same-origin allow-scripts allow-forms allow-popups allow-modals allow-downloads',
|
2016-10-29 04:24:08 -04:00
|
|
|
'zerobincompatibility' => false,
|
2019-06-17 15:40:37 -04:00
|
|
|
'httpwarning' => true,
|
2019-06-23 13:45:40 -04:00
|
|
|
'compression' => 'zlib',
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'expire' => array(
|
|
|
|
'default' => '1week',
|
|
|
|
),
|
|
|
|
'expire_options' => array(
|
2016-10-29 04:24:08 -04:00
|
|
|
'5min' => 300,
|
|
|
|
'10min' => 600,
|
|
|
|
'1hour' => 3600,
|
|
|
|
'1day' => 86400,
|
|
|
|
'1week' => 604800,
|
2015-09-22 17:21:31 -04:00
|
|
|
'1month' => 2592000,
|
2016-10-29 04:24:08 -04:00
|
|
|
'1year' => 31536000,
|
|
|
|
'never' => 0,
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'formatter_options' => array(
|
2016-10-29 04:24:08 -04:00
|
|
|
'plaintext' => 'Plain Text',
|
2015-09-22 17:21:31 -04:00
|
|
|
'syntaxhighlighting' => 'Source Code',
|
2016-10-29 04:24:08 -04:00
|
|
|
'markdown' => 'Markdown',
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'traffic' => array(
|
2022-02-20 03:09:20 -05:00
|
|
|
'limit' => 10,
|
|
|
|
'header' => '',
|
|
|
|
'exempted' => '',
|
|
|
|
'creators' => '',
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
2016-07-15 11:02:59 -04:00
|
|
|
'purge' => array(
|
2016-10-29 04:24:08 -04:00
|
|
|
'limit' => 300,
|
2016-07-15 11:02:59 -04:00
|
|
|
'batchsize' => 10,
|
|
|
|
),
|
2015-09-22 17:21:31 -04:00
|
|
|
'model' => array(
|
2016-08-09 05:54:42 -04:00
|
|
|
'class' => 'Filesystem',
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'model_options' => array(
|
|
|
|
'dir' => 'data',
|
|
|
|
),
|
2022-10-22 18:19:43 -04:00
|
|
|
'yourls' => array(
|
|
|
|
'signature' => '',
|
2022-10-22 19:07:43 -04:00
|
|
|
'apiurl' => '',
|
2022-10-22 18:19:43 -04:00
|
|
|
),
|
2024-07-07 10:36:52 -04:00
|
|
|
// update this array when adding/changing/removing js files
|
|
|
|
'sri' => array(
|
2024-07-09 15:48:40 -04:00
|
|
|
'js/base-x-4.0.0.js' => 'sha512-nNPg5IGCwwrveZ8cA/yMGr5HiRS5Ps2H+s0J/mKTPjCPWUgFGGw7M5nqdnPD3VsRwCVysUh3Y8OWjeSKGkEQJQ==',
|
|
|
|
'js/base64-1.7.js' => 'sha512-JdwsSP3GyHR+jaCkns9CL9NTt4JUJqm/BsODGmYhBcj5EAPKcHYh+OiMfyHbcDLECe17TL0hjXADFkusAqiYgA==',
|
|
|
|
'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
|
|
|
|
'js/bootstrap-5.3.3.js' => 'sha512-in2rcOpLTdJ7/pw5qjF4LWHFRtgoBDxXCy49H4YGOcVdGiPaQucGIbOqxt1JvmpvOpq3J/C7VTa0FlioakB2gQ==',
|
2024-07-07 10:36:52 -04:00
|
|
|
'js/dark-mode-switch.js' => 'sha512-CCbdHdeWDbDO7aqFFmhgnvFESzaILHbUYmbhNjTpcjyO/XYdouQ9Pw8W9rpV8oJT1TsK5FbwSHU1oazmnb7BWA==',
|
2024-07-09 15:48:40 -04:00
|
|
|
'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
|
|
|
|
'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
|
|
|
|
'js/legacy.js' => 'sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==',
|
|
|
|
'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
|
|
|
|
'js/privatebin.js' => 'sha512-cbmXvtZ/5gZPFjQDzP3IEhUAIhFPAoM31gw2kRYCT5xOh8wv9gXeDqI/t798luRW1xdC4gaYodjEFCzrsZR4mA==',
|
|
|
|
'js/purify-3.1.3.js' => 'sha512-t/FKG/ucQVMWTWVouSMABSEx1r+uSyAI9eNDq0KEr9mPhkgxpJztHI/E72JIpv/+VwPs/Q4husxj14TE9Ps/wg==',
|
|
|
|
'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
|
|
|
|
'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
|
|
|
|
'js/zlib-1.3.1.js' => 'sha512-Z90oppVx/mn0DG2k9airjFVQuliELlXLeT3SRiO6MLiUSbhGlAq+UFwmYbG4i9mwW87dkG8fgJPapGwnUq7Osg==',
|
2024-07-07 10:36:52 -04:00
|
|
|
),
|
2015-09-22 17:21:31 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse configuration file and ensure default configuration values are present
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2021-08-19 13:28:52 -04:00
|
|
|
$basePaths = array();
|
|
|
|
$config = array();
|
2021-08-19 04:21:21 -04:00
|
|
|
$configPath = getenv('CONFIG_PATH');
|
|
|
|
if ($configPath !== false && !empty($configPath)) {
|
|
|
|
$basePaths[] = $configPath;
|
|
|
|
}
|
|
|
|
$basePaths[] = PATH . 'cfg';
|
|
|
|
foreach ($basePaths as $basePath) {
|
|
|
|
$configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
|
|
|
|
if (is_readable($configFile)) {
|
|
|
|
$config = parse_ini_file($configFile, true);
|
|
|
|
foreach (array('main', 'model', 'model_options') as $section) {
|
|
|
|
if (!array_key_exists($section, $config)) {
|
|
|
|
throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
|
|
|
|
}
|
2015-10-22 15:13:15 -04:00
|
|
|
}
|
2021-08-19 05:04:31 -04:00
|
|
|
break;
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 15:55:03 -04:00
|
|
|
|
2015-10-24 02:44:17 -04:00
|
|
|
$opts = '_options';
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach (self::getDefaults() as $section => $values) {
|
2015-10-24 02:44:17 -04:00
|
|
|
// fill missing sections with default values
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$this->_configuration[$section] = $values;
|
2016-07-26 02:19:35 -04:00
|
|
|
if (array_key_exists('dir', $this->_configuration[$section])) {
|
2015-09-22 17:21:31 -04:00
|
|
|
$this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
// provide different defaults for database model
|
2016-07-13 03:41:45 -04:00
|
|
|
elseif (
|
|
|
|
$section == 'model_options' && in_array(
|
|
|
|
$this->_configuration['model']['class'],
|
2016-08-09 05:54:42 -04:00
|
|
|
array('Database', 'privatebin_db', 'zerobin_db')
|
2016-07-13 03:41:45 -04:00
|
|
|
)
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
2015-09-26 21:03:55 -04:00
|
|
|
$values = array(
|
2016-08-09 05:54:42 -04:00
|
|
|
'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
|
2015-09-26 21:03:55 -04:00
|
|
|
'tbl' => null,
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_PERSISTENT => true),
|
|
|
|
);
|
2021-05-28 16:39:50 -04:00
|
|
|
} elseif (
|
|
|
|
$section == 'model_options' && in_array(
|
|
|
|
$this->_configuration['model']['class'],
|
|
|
|
array('GoogleCloudStorage')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$values = array(
|
2022-10-06 00:19:06 -04:00
|
|
|
'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
|
|
|
|
'prefix' => 'pastes',
|
|
|
|
'uniformacl' => false,
|
2021-05-28 16:39:50 -04:00
|
|
|
);
|
2022-10-20 17:23:01 -04:00
|
|
|
} elseif (
|
|
|
|
$section == 'model_options' && in_array(
|
|
|
|
$this->_configuration['model']['class'],
|
|
|
|
array('S3Storage')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$values = array(
|
|
|
|
'region' => null,
|
|
|
|
'version' => null,
|
|
|
|
'endpoint' => null,
|
|
|
|
'accesskey' => null,
|
|
|
|
'secretkey' => null,
|
|
|
|
'use_path_style_endpoint' => null,
|
|
|
|
'bucket' => null,
|
|
|
|
'prefix' => '',
|
|
|
|
);
|
2015-09-26 21:03:55 -04:00
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
|
|
|
|
// "*_options" sections don't require all defaults to be set
|
|
|
|
if (
|
|
|
|
$section !== 'model_options' &&
|
|
|
|
($from = strlen($section) - strlen($opts)) >= 0 &&
|
|
|
|
strpos($section, $opts, $from) !== false
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
|
|
|
if (is_int(current($values))) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$config[$section] = array_map('intval', $config[$section]);
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
$this->_configuration[$section] = $config[$section];
|
|
|
|
}
|
|
|
|
// check for missing keys and set defaults if necessary
|
2016-07-26 02:19:35 -04:00
|
|
|
else {
|
2024-07-19 02:05:40 -04:00
|
|
|
// preserve configured SRI hashes
|
|
|
|
if ($section == 'sri' && array_key_exists($section, $config)) {
|
|
|
|
$this->_configuration[$section] = $config[$section];
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($values as $key => $val) {
|
|
|
|
if ($key == 'dir') {
|
2015-10-24 02:44:17 -04:00
|
|
|
$val = PATH . $val;
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = $val;
|
2016-07-26 02:19:35 -04:00
|
|
|
if (array_key_exists($key, $config[$section])) {
|
|
|
|
if ($val === null) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = $config[$section][$key];
|
2016-07-26 02:19:35 -04:00
|
|
|
} elseif (is_bool($val)) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$val = strtolower($config[$section][$key]);
|
2016-07-26 02:19:35 -04:00
|
|
|
if (in_array($val, array('true', 'yes', 'on'))) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = true;
|
2016-07-26 02:19:35 -04:00
|
|
|
} elseif (in_array($val, array('false', 'no', 'off'))) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = false;
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = (bool) $config[$section][$key];
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
} elseif (is_int($val)) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = (int) $config[$section][$key];
|
2016-07-26 02:19:35 -04:00
|
|
|
} elseif (is_string($val) && !empty($config[$section][$key])) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$result = (string) $config[$section][$key];
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
$this->_configuration[$section][$key] = $result;
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
|
2016-07-22 09:31:42 -04:00
|
|
|
// support for old config file format, before the fork was renamed and PSR-4 introduced
|
2016-07-13 03:41:45 -04:00
|
|
|
$this->_configuration['model']['class'] = str_replace(
|
|
|
|
'zerobin_', 'privatebin_',
|
|
|
|
$this->_configuration['model']['class']
|
|
|
|
);
|
|
|
|
|
2016-07-22 09:31:42 -04:00
|
|
|
$this->_configuration['model']['class'] = str_replace(
|
|
|
|
array('privatebin_data', 'privatebin_db'),
|
2016-08-09 05:54:42 -04:00
|
|
|
array('Filesystem', 'Database'),
|
2016-07-22 09:31:42 -04:00
|
|
|
$this->_configuration['model']['class']
|
|
|
|
);
|
|
|
|
|
2015-10-24 02:44:17 -04:00
|
|
|
// ensure a valid expire default key is set
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
|
2015-10-24 02:44:17 -04:00
|
|
|
$this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
|
|
|
|
}
|
2022-11-03 02:47:50 -04:00
|
|
|
|
|
|
|
// ensure the basepath ends in a slash, if one is set
|
|
|
|
if (
|
2024-04-18 15:36:43 -04:00
|
|
|
!empty($this->_configuration['main']['basepath']) &&
|
2022-11-03 02:47:50 -04:00
|
|
|
substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
|
|
|
|
) {
|
|
|
|
$this->_configuration['main']['basepath'] .= '/';
|
|
|
|
}
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get configuration as array
|
|
|
|
*
|
2018-05-27 09:18:25 -04:00
|
|
|
* @return array
|
2015-09-22 17:21:31 -04:00
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->_configuration;
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:15:20 -04:00
|
|
|
/**
|
|
|
|
* get default configuration as array
|
|
|
|
*
|
2018-05-27 09:18:25 -04:00
|
|
|
* @return array
|
2016-07-11 08:15:20 -04:00
|
|
|
*/
|
|
|
|
public static function getDefaults()
|
|
|
|
{
|
|
|
|
return self::$_defaults;
|
|
|
|
}
|
2015-09-22 17:21:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get a key from the configuration, typically the main section or all keys
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $section defaults to main
|
|
|
|
* @throws Exception
|
2018-05-27 09:18:25 -04:00
|
|
|
* @return mixed
|
2015-09-22 17:21:31 -04:00
|
|
|
*/
|
|
|
|
public function getKey($key, $section = 'main')
|
|
|
|
{
|
|
|
|
$options = $this->getSection($section);
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!array_key_exists($key, $options)) {
|
2016-08-09 05:54:42 -04:00
|
|
|
throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
return $this->_configuration[$section][$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-06 08:12:14 -04:00
|
|
|
* get a section from the configuration, must exist
|
2015-09-22 17:21:31 -04:00
|
|
|
*
|
2016-07-06 08:12:14 -04:00
|
|
|
* @param string $section
|
2015-09-22 17:21:31 -04:00
|
|
|
* @throws Exception
|
2018-05-27 09:18:25 -04:00
|
|
|
* @return mixed
|
2015-09-22 17:21:31 -04:00
|
|
|
*/
|
|
|
|
public function getSection($section)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!array_key_exists($section, $this->_configuration)) {
|
2017-01-01 10:33:11 -05:00
|
|
|
throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
return $this->_configuration[$section];
|
|
|
|
}
|
|
|
|
}
|