2015-09-23 05:21:31 +08:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 17:58:15 +08:00
|
|
|
* PrivateBin
|
2015-09-23 05:21:31 +08:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 17:58:15 +08:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-23 05:21:31 +08:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 19:56:52 +08:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-11-10 04:39:42 +08:00
|
|
|
* @version 0.22
|
2015-09-23 05:21:31 +08:00
|
|
|
*/
|
|
|
|
|
2016-07-21 23:09:48 +08:00
|
|
|
namespace PrivateBin;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PDO;
|
|
|
|
|
2015-09-23 05:21:31 +08:00
|
|
|
/**
|
|
|
|
* configuration
|
|
|
|
*
|
|
|
|
* parses configuration file, ensures default values present
|
|
|
|
*/
|
|
|
|
class configuration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* parsed configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_configuration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* default configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-11 20:15:20 +08:00
|
|
|
private static $_defaults = array(
|
2015-09-23 05:21:31 +08:00
|
|
|
'main' => array(
|
|
|
|
'discussion' => true,
|
|
|
|
'opendiscussion' => false,
|
|
|
|
'password' => true,
|
|
|
|
'fileupload' => false,
|
|
|
|
'burnafterreadingselected' => false,
|
|
|
|
'defaultformatter' => 'plaintext',
|
|
|
|
'syntaxhighlightingtheme' => null,
|
|
|
|
'sizelimit' => 2097152,
|
|
|
|
'template' => 'bootstrap',
|
|
|
|
'notice' => '',
|
|
|
|
'languageselection' => false,
|
2015-10-19 02:38:07 +08:00
|
|
|
'languagedefault' => '',
|
2016-01-31 16:56:06 +08:00
|
|
|
'urlshortener' => '',
|
2016-07-18 16:14:38 +08:00
|
|
|
'vizhash' => true,
|
2016-07-06 17:37:13 +08:00
|
|
|
'zerobincompatibility' => false,
|
2015-09-23 05:21:31 +08:00
|
|
|
),
|
|
|
|
'expire' => array(
|
|
|
|
'default' => '1week',
|
2015-10-18 23:56:45 +08:00
|
|
|
'clone' => true,
|
2015-09-23 05:21:31 +08:00
|
|
|
),
|
|
|
|
'expire_options' => array(
|
|
|
|
'5min' => 300,
|
|
|
|
'10min' => 600,
|
|
|
|
'1hour' => 3600,
|
|
|
|
'1day' => 86400,
|
|
|
|
'1week' => 604800,
|
|
|
|
'1month' => 2592000,
|
|
|
|
'1year' => 31536000,
|
|
|
|
'never' => 0,
|
|
|
|
),
|
|
|
|
'formatter_options' => array(
|
|
|
|
'plaintext' => 'Plain Text',
|
|
|
|
'syntaxhighlighting' => 'Source Code',
|
|
|
|
'markdown' => 'Markdown',
|
|
|
|
),
|
|
|
|
'traffic' => array(
|
|
|
|
'limit' => 10,
|
|
|
|
'header' => null,
|
|
|
|
'dir' => 'data',
|
|
|
|
),
|
2016-07-15 23:02:59 +08:00
|
|
|
'purge' => array(
|
|
|
|
'limit' => 300,
|
|
|
|
'batchsize' => 10,
|
|
|
|
'dir' => 'data',
|
|
|
|
),
|
2015-09-23 05:21:31 +08:00
|
|
|
'model' => array(
|
2016-07-11 17:58:15 +08:00
|
|
|
'class' => 'privatebin_data',
|
2015-09-23 05:21:31 +08:00
|
|
|
),
|
|
|
|
'model_options' => array(
|
|
|
|
'dir' => 'data',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse configuration file and ensure default configuration values are present
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2015-10-23 03:13:15 +08:00
|
|
|
$config = array();
|
|
|
|
$configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
|
|
|
|
if (is_readable($configFile))
|
|
|
|
{
|
|
|
|
$config = parse_ini_file($configFile, true);
|
2016-07-13 15:41:45 +08:00
|
|
|
foreach (array('main', 'model', 'model_options') as $section) {
|
2015-10-23 03:13:15 +08:00
|
|
|
if (!array_key_exists($section, $config)) {
|
2016-07-11 17:58:15 +08:00
|
|
|
throw new Exception(i18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
|
2015-10-23 03:13:15 +08:00
|
|
|
}
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
$opts = '_options';
|
2016-07-11 20:15:20 +08:00
|
|
|
foreach (self::getDefaults() as $section => $values)
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
// fill missing sections with default values
|
|
|
|
if (!array_key_exists($section, $config) || count($config[$section]) == 0)
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$this->_configuration[$section] = $values;
|
2015-09-23 05:21:31 +08:00
|
|
|
if (array_key_exists('dir', $this->_configuration[$section]))
|
|
|
|
{
|
|
|
|
$this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-27 09:03:55 +08:00
|
|
|
// provide different defaults for database model
|
2016-07-13 15:41:45 +08:00
|
|
|
elseif (
|
|
|
|
$section == 'model_options' && in_array(
|
|
|
|
$this->_configuration['model']['class'],
|
|
|
|
array('privatebin_db', 'zerobin_db')
|
|
|
|
)
|
|
|
|
)
|
2015-09-27 09:03:55 +08:00
|
|
|
{
|
|
|
|
$values = array(
|
|
|
|
'dsn' => 'sqlite:' . PATH . 'data/db.sq3',
|
|
|
|
'tbl' => null,
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_PERSISTENT => true),
|
|
|
|
);
|
|
|
|
}
|
2015-10-24 14:44:17 +08: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
|
|
|
|
)
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
if (is_int(current($values)))
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$config[$section] = array_map('intval', $config[$section]);
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
$this->_configuration[$section] = $config[$section];
|
|
|
|
}
|
|
|
|
// check for missing keys and set defaults if necessary
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach ($values as $key => $val)
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
if ($key == 'dir')
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$val = PATH . $val;
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
$result = $val;
|
|
|
|
if (array_key_exists($key, $config[$section]))
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
if ($val === null)
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$result = $config[$section][$key];
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
elseif (is_bool($val))
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$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];
|
|
|
|
}
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
elseif (is_int($val))
|
2015-09-23 05:21:31 +08:00
|
|
|
{
|
2015-10-24 14:44:17 +08:00
|
|
|
$result = (int) $config[$section][$key];
|
|
|
|
}
|
|
|
|
elseif (is_string($val) && !empty($config[$section][$key]))
|
|
|
|
{
|
|
|
|
$result = (string) $config[$section][$key];
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
$this->_configuration[$section][$key] = $result;
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 14:44:17 +08:00
|
|
|
|
2016-07-13 15:41:45 +08:00
|
|
|
// support for old config file format, before the fork was renamed
|
|
|
|
$this->_configuration['model']['class'] = str_replace(
|
|
|
|
'zerobin_', 'privatebin_',
|
|
|
|
$this->_configuration['model']['class']
|
|
|
|
);
|
|
|
|
|
2015-10-24 14:44:17 +08:00
|
|
|
// 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']);
|
|
|
|
}
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get configuration as array
|
|
|
|
*
|
|
|
|
* return array
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->_configuration;
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:15:20 +08:00
|
|
|
/**
|
|
|
|
* get default configuration as array
|
|
|
|
*
|
|
|
|
* return array
|
|
|
|
*/
|
|
|
|
public static function getDefaults()
|
|
|
|
{
|
|
|
|
return self::$_defaults;
|
|
|
|
}
|
2015-09-23 05:21:31 +08: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);
|
|
|
|
if (!array_key_exists($key, $options))
|
|
|
|
{
|
|
|
|
throw new Exception(i18n::_('Invalid data.') . " $section / $key", 4);
|
|
|
|
}
|
|
|
|
return $this->_configuration[$section][$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-06 20:12:14 +08:00
|
|
|
* get a section from the configuration, must exist
|
2015-09-23 05:21:31 +08:00
|
|
|
*
|
2016-07-06 20:12:14 +08:00
|
|
|
* @param string $section
|
2015-09-23 05:21:31 +08:00
|
|
|
* @throws Exception
|
|
|
|
* return mixed
|
|
|
|
*/
|
|
|
|
public function getSection($section)
|
|
|
|
{
|
|
|
|
if (!array_key_exists($section, $this->_configuration))
|
|
|
|
{
|
2016-07-11 17:58:15 +08:00
|
|
|
throw new Exception(i18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 3);
|
2015-09-23 05:21:31 +08:00
|
|
|
}
|
|
|
|
return $this->_configuration[$section];
|
|
|
|
}
|
|
|
|
}
|