2015-09-22 17:21:31 -04:00
|
|
|
<?php
|
|
|
|
/**
|
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
|
2017-10-04 14:05:46 -04:00
|
|
|
* @version 1.1.1
|
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
|
|
|
{
|
2017-10-08 05:03:17 -04:00
|
|
|
/**
|
|
|
|
* First line in INI file, to hide contents
|
|
|
|
*
|
|
|
|
* @const string
|
|
|
|
*/
|
2017-10-08 05:31:41 -04:00
|
|
|
const PROTECTION_LINE = ';<?php http_response_code(403); /*';
|
2017-10-08 05:03:17 -04:00
|
|
|
|
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',
|
2016-10-29 04:24:08 -04:00
|
|
|
'discussion' => true,
|
|
|
|
'opendiscussion' => false,
|
|
|
|
'password' => true,
|
|
|
|
'fileupload' => false,
|
2015-09-22 17:21:31 -04:00
|
|
|
'burnafterreadingselected' => false,
|
2017-04-11 11:23:26 -04:00
|
|
|
'instantburnafterreading' => false,
|
2016-10-29 04:24:08 -04:00
|
|
|
'defaultformatter' => 'plaintext',
|
|
|
|
'syntaxhighlightingtheme' => null,
|
|
|
|
'sizelimit' => 2097152,
|
|
|
|
'template' => 'bootstrap',
|
|
|
|
'notice' => '',
|
|
|
|
'languageselection' => false,
|
|
|
|
'languagedefault' => '',
|
|
|
|
'urlshortener' => '',
|
|
|
|
'icon' => 'identicon',
|
2017-02-01 12:34:13 -05:00
|
|
|
'cspheader' => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; referrer no-referrer; sandbox allow-same-origin allow-scripts allow-forms allow-popups',
|
2016-10-29 04:24:08 -04:00
|
|
|
'zerobincompatibility' => false,
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'expire' => array(
|
|
|
|
'default' => '1week',
|
2016-10-29 04:24:08 -04:00
|
|
|
'clone' => true,
|
2015-09-22 17:21:31 -04:00
|
|
|
),
|
|
|
|
'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(
|
2016-10-29 04:24:08 -04:00
|
|
|
'limit' => 10,
|
2015-09-22 17:21:31 -04:00
|
|
|
'header' => null,
|
2016-10-29 04:24:08 -04:00
|
|
|
'dir' => 'data',
|
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,
|
2016-10-29 04:24:08 -04:00
|
|
|
'dir' => 'data',
|
2016-07-15 11:02:59 -04:00
|
|
|
),
|
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',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse configuration file and ensure default configuration values are present
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-08-15 10:45:47 -04:00
|
|
|
$config = array();
|
2017-09-29 12:59:02 -04:00
|
|
|
$configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php';
|
2017-10-04 15:55:03 -04:00
|
|
|
$configIni = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
|
|
|
|
|
|
|
|
// rename INI files to avoid configuration leakage
|
|
|
|
if (is_readable($configIni)) {
|
2017-10-08 05:03:17 -04:00
|
|
|
$context = stream_context_create();
|
2017-10-04 15:55:03 -04:00
|
|
|
// don't overwrite already converted file
|
|
|
|
if (!is_file($configFile)) {
|
2017-10-08 05:03:17 -04:00
|
|
|
$iniHandle = fopen($configIni, 'r', false, $context);
|
2017-10-08 05:31:41 -04:00
|
|
|
file_put_contents($configFile, self::PROTECTION_LINE . PHP_EOL);
|
2017-10-08 05:03:17 -04:00
|
|
|
file_put_contents($configFile, $iniHandle, FILE_APPEND);
|
2017-10-04 15:55:03 -04:00
|
|
|
fclose($iniHandle);
|
|
|
|
}
|
2017-10-08 05:03:17 -04:00
|
|
|
unlink($configIni);
|
2017-10-04 15:55:03 -04:00
|
|
|
|
|
|
|
// cleanup sample, too
|
2017-10-04 16:02:27 -04:00
|
|
|
$configSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php';
|
2017-10-04 15:55:03 -04:00
|
|
|
$configIniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
|
|
|
|
if (is_readable($configIniSample)) {
|
2017-10-08 05:03:17 -04:00
|
|
|
if (!is_readable($configSample)) {
|
|
|
|
$iniSampleHandle = fopen($configIniSample, 'r', false, $context);
|
2017-10-08 05:31:41 -04:00
|
|
|
file_put_contents($configSample, self::PROTECTION_LINE . PHP_EOL);
|
2017-10-08 05:03:17 -04:00
|
|
|
file_put_contents($configSample, $iniSampleHandle, FILE_APPEND);
|
|
|
|
fclose($iniSampleHandle);
|
2017-10-04 15:55:03 -04:00
|
|
|
}
|
2017-10-08 05:03:17 -04:00
|
|
|
unlink($configIniSample);
|
2017-10-04 15:55:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:19:35 -04:00
|
|
|
if (is_readable($configFile)) {
|
2015-10-22 15:13:15 -04:00
|
|
|
$config = parse_ini_file($configFile, true);
|
2016-07-13 03:41:45 -04:00
|
|
|
foreach (array('main', 'model', 'model_options') as $section) {
|
2015-10-22 15:13:15 -04:00
|
|
|
if (!array_key_exists($section, $config)) {
|
2016-08-09 05:54:42 -04:00
|
|
|
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
|
|
|
}
|
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),
|
|
|
|
);
|
|
|
|
}
|
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 {
|
|
|
|
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']);
|
|
|
|
}
|
2015-09-22 17:21:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get configuration as array
|
|
|
|
*
|
|
|
|
* return array
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->_configuration;
|
|
|
|
}
|
|
|
|
|
2016-07-11 08:15:20 -04:00
|
|
|
/**
|
|
|
|
* get default configuration as array
|
|
|
|
*
|
|
|
|
* return array
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* return mixed
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* return mixed
|
|
|
|
*/
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|