paste.chapril.org-privatebin/lib/Configuration.php

302 lines
11 KiB
PHP
Raw Normal View History

2015-09-22 23:21:31 +02:00
<?php
/**
2016-07-11 11:58:15 +02:00
* PrivateBin
2015-09-22 23:21:31 +02:00
*
* a zero-knowledge paste bin
*
2016-07-11 11:58:15 +02:00
* @link https://github.com/PrivateBin/PrivateBin
2015-09-22 23:21:31 +02:00
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
2024-02-11 14:17:27 +01:00
* @version 1.7.0
2015-09-22 23:21:31 +02:00
*/
2016-12-12 18:43:23 +01:00
2016-12-12 18:50:00 +01:00
namespace PrivateBin;
2016-07-21 17:09:48 +02:00
use Exception;
use PDO;
2015-09-22 23:21:31 +02:00
/**
* Configuration
2015-09-22 23:21:31 +02:00
*
* parses configuration file, ensures default values present
*/
class Configuration
2015-09-22 23:21:31 +02:00
{
/**
* parsed configuration
*
* @var array
*/
private $_configuration;
/**
* default configuration
*
* @var array
*/
2016-07-11 14:15:20 +02:00
private static $_defaults = array(
2015-09-22 23:21:31 +02:00
'main' => array(
'name' => 'PrivateBin',
'basepath' => '',
'discussion' => true,
'opendiscussion' => false,
'password' => true,
'fileupload' => false,
2015-09-22 23:21:31 +02:00
'burnafterreadingselected' => false,
'defaultformatter' => 'plaintext',
2022-02-15 19:02:44 +01:00
'syntaxhighlightingtheme' => '',
'sizelimit' => 10485760,
'template' => 'bootstrap',
'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
'notice' => '',
'languageselection' => false,
'languagedefault' => '',
'urlshortener' => '',
'qrcode' => true,
'email' => true,
'icon' => 'identicon',
'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',
'zerobincompatibility' => false,
'httpwarning' => true,
'compression' => 'zlib',
2015-09-22 23:21:31 +02:00
),
'expire' => array(
'default' => '1week',
),
'expire_options' => array(
'5min' => 300,
'10min' => 600,
'1hour' => 3600,
'1day' => 86400,
'1week' => 604800,
2015-09-22 23:21:31 +02:00
'1month' => 2592000,
'1year' => 31536000,
'never' => 0,
2015-09-22 23:21:31 +02:00
),
'formatter_options' => array(
'plaintext' => 'Plain Text',
2015-09-22 23:21:31 +02:00
'syntaxhighlighting' => 'Source Code',
'markdown' => 'Markdown',
2015-09-22 23:21:31 +02:00
),
'traffic' => array(
'limit' => 10,
'header' => '',
'exempted' => '',
'creators' => '',
2015-09-22 23:21:31 +02:00
),
'purge' => array(
'limit' => 300,
'batchsize' => 10,
),
2015-09-22 23:21:31 +02:00
'model' => array(
'class' => 'Filesystem',
2015-09-22 23:21:31 +02:00
),
'model_options' => array(
'dir' => 'data',
),
'yourls' => array(
'signature' => '',
2022-10-23 01:07:43 +02:00
'apiurl' => '',
),
2015-09-22 23:21:31 +02:00
);
/**
* parse configuration file and ensure default configuration values are present
*
* @throws Exception
*/
public function __construct()
{
2021-08-19 19:28:52 +02:00
$basePaths = array();
$config = array();
$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);
}
}
2021-08-19 11:04:31 +02:00
break;
2015-09-22 23:21:31 +02:00
}
}
$opts = '_options';
foreach (self::getDefaults() as $section => $values) {
// fill missing sections with default values
if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
$this->_configuration[$section] = $values;
if (array_key_exists('dir', $this->_configuration[$section])) {
2015-09-22 23:21:31 +02:00
$this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
}
continue;
}
// provide different defaults for database model
elseif (
$section == 'model_options' && in_array(
$this->_configuration['model']['class'],
array('Database', 'privatebin_db', 'zerobin_db')
)
) {
$values = array(
'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
'tbl' => null,
'usr' => null,
'pwd' => null,
'opt' => array(PDO::ATTR_PERSISTENT => true),
);
2021-05-28 22:39:50 +02:00
} elseif (
$section == 'model_options' && in_array(
$this->_configuration['model']['class'],
array('GoogleCloudStorage')
)
) {
$values = array(
2022-10-06 06:19:06 +02:00
'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
'prefix' => 'pastes',
'uniformacl' => false,
2021-05-28 22:39:50 +02: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' => '',
);
}
// "*_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
) {
if (is_int(current($values))) {
$config[$section] = array_map('intval', $config[$section]);
2015-09-22 23:21:31 +02:00
}
$this->_configuration[$section] = $config[$section];
}
// check for missing keys and set defaults if necessary
else {
foreach ($values as $key => $val) {
if ($key == 'dir') {
$val = PATH . $val;
2015-09-22 23:21:31 +02:00
}
$result = $val;
if (array_key_exists($key, $config[$section])) {
if ($val === null) {
$result = $config[$section][$key];
} elseif (is_bool($val)) {
$val = strtolower($config[$section][$key]);
if (in_array($val, array('true', 'yes', 'on'))) {
$result = true;
} elseif (in_array($val, array('false', 'no', 'off'))) {
$result = false;
} else {
$result = (bool) $config[$section][$key];
}
} elseif (is_int($val)) {
$result = (int) $config[$section][$key];
} elseif (is_string($val) && !empty($config[$section][$key])) {
$result = (string) $config[$section][$key];
2015-09-22 23:21:31 +02:00
}
}
$this->_configuration[$section][$key] = $result;
2015-09-22 23:21:31 +02:00
}
}
}
// support for old config file format, before the fork was renamed and PSR-4 introduced
$this->_configuration['model']['class'] = str_replace(
'zerobin_', 'privatebin_',
$this->_configuration['model']['class']
);
$this->_configuration['model']['class'] = str_replace(
array('privatebin_data', 'privatebin_db'),
array('Filesystem', 'Database'),
$this->_configuration['model']['class']
);
// ensure a valid expire default key is set
if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
$this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
}
// ensure the basepath ends in a slash, if one is set
if (
strlen($this->_configuration['main']['basepath']) &&
substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
) {
$this->_configuration['main']['basepath'] .= '/';
}
2015-09-22 23:21:31 +02:00
}
/**
* get configuration as array
*
2018-05-27 15:18:25 +02:00
* @return array
2015-09-22 23:21:31 +02:00
*/
public function get()
{
return $this->_configuration;
}
2016-07-11 14:15:20 +02:00
/**
* get default configuration as array
*
2018-05-27 15:18:25 +02:00
* @return array
2016-07-11 14:15:20 +02:00
*/
public static function getDefaults()
{
return self::$_defaults;
}
2015-09-22 23:21:31 +02: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 15:18:25 +02:00
* @return mixed
2015-09-22 23:21:31 +02:00
*/
public function getKey($key, $section = 'main')
{
$options = $this->getSection($section);
if (!array_key_exists($key, $options)) {
throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
2015-09-22 23:21:31 +02:00
}
return $this->_configuration[$section][$key];
}
/**
* get a section from the configuration, must exist
2015-09-22 23:21:31 +02:00
*
* @param string $section
2015-09-22 23:21:31 +02:00
* @throws Exception
2018-05-27 15:18:25 +02:00
* @return mixed
2015-09-22 23:21:31 +02:00
*/
public function getSection($section)
{
if (!array_key_exists($section, $this->_configuration)) {
throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
2015-09-22 23:21:31 +02:00
}
return $this->_configuration[$section];
}
}