refactoring configuration

This commit is contained in:
El RIDO 2015-09-22 23:21:31 +02:00
parent 9f68658106
commit 6d24ff824e
6 changed files with 308 additions and 139 deletions

View file

@ -7,7 +7,7 @@
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
* @version 0.21 * @version 0.21.1
*/ */
// change this, if your php files and data is outside of your webservers document root // change this, if your php files and data is outside of your webservers document root

191
lib/configuration.php Normal file
View file

@ -0,0 +1,191 @@
<?php
/**
* ZeroBin
*
* a zero-knowledge paste bin
*
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
* @version 0.21.1
*/
/**
* configuration
*
* parses configuration file, ensures default values present
*/
class configuration
{
/**
* parsed configuration
*
* @var array
*/
private $_configuration;
/**
* default configuration
*
* @var array
*/
private $_defaults = array(
'main' => array(
'discussion' => true,
'opendiscussion' => false,
'password' => true,
'fileupload' => false,
'burnafterreadingselected' => false,
'defaultformatter' => 'plaintext',
'syntaxhighlightingtheme' => null,
'sizelimit' => 2097152,
'template' => 'bootstrap',
'notice' => '',
'base64version' => '2.1.9',
'languageselection' => false,
),
'expire' => array(
'default' => '1week',
),
'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',
),
'model' => array(
'class' => 'zerobin_data',
),
'model_options' => array(
'dir' => 'data',
),
);
/**
* parse configuration file and ensure default configuration values are present
*
* @throws Exception
*/
public function __construct()
{
$config = parse_ini_file(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', true);
foreach (array('main', 'model') as $section) {
if (!array_key_exists($section, $config)) {
throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 2);
}
}
foreach ($this->_defaults as $section => $values)
{
if (!array_key_exists($section, $config))
{
$this->_configuration[$section] = $this->_defaults[$section];
if (array_key_exists('dir', $this->_configuration[$section]))
{
$this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
}
continue;
}
foreach ($values as $key => $val)
{
if ($key == 'dir')
{
$val = PATH . $val;
}
$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];
}
}
$this->_configuration[$section][$key] = $result;
}
}
}
/**
* get configuration as array
*
* return array
*/
public function get()
{
return $this->_configuration;
}
/**
* 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];
}
/**
* get a key from the configuration, typically the main section or all keys
*
* @param string $key if empty, return all configuration options
* @param string $section defaults to main
* @throws Exception
* return mixed
*/
public function getSection($section)
{
if (!array_key_exists($section, $this->_configuration))
{
throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 3);
}
return $this->_configuration[$section];
}
}

View file

@ -32,14 +32,12 @@ class zerobin
const GENERIC_ERROR = 'Paste does not exist, has expired or has been deleted.'; const GENERIC_ERROR = 'Paste does not exist, has expired or has been deleted.';
/** /**
* configuration array * configuration
* *
* @access private * @access private
* @var array * @var configuration
*/ */
private $_conf = array( private $_conf;
'model' => 'zerobin_data',
);
/** /**
* data * data
@ -164,13 +162,8 @@ class zerobin
); );
} }
$this->_conf = parse_ini_file(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', true); $this->_conf = new configuration;
foreach (array('main', 'model') as $section) { $this->_model = $this->_conf->getKey('class', 'model');
if (!array_key_exists($section, $this->_conf)) {
throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 2);
}
}
$this->_model = $this->_conf['model']['class'];
} }
/** /**
@ -185,7 +178,7 @@ class zerobin
if(is_string($this->_model)) { if(is_string($this->_model)) {
$this->_model = forward_static_call( $this->_model = forward_static_call(
array($this->_model, 'getInstance'), array($this->_model, 'getInstance'),
$this->_conf['model_options'] $this->_conf->getSection('model_options')
); );
} }
return $this->_model; return $this->_model;
@ -222,12 +215,12 @@ class zerobin
$attachmentname = $has_attachmentname ? $_POST['attachmentname'] : ''; $attachmentname = $has_attachmentname ? $_POST['attachmentname'] : '';
// Make sure last paste from the IP address was more than X seconds ago. // Make sure last paste from the IP address was more than X seconds ago.
trafficlimiter::setLimit($this->_conf['traffic']['limit']); trafficlimiter::setLimit($this->_conf->getKey('limit', 'traffic'));
trafficlimiter::setPath($this->_conf['traffic']['dir']); trafficlimiter::setPath($this->_conf->getKey('dir', 'traffic'));
$ipKey = 'REMOTE_ADDR'; $ipKey = 'REMOTE_ADDR';
if (array_key_exists('header', $this->_conf['traffic'])) if (($option = $this->_conf->getKey('header', 'traffic')) !== null)
{ {
$header = 'HTTP_' . $this->_conf['traffic']['header']; $header = 'HTTP_' . $option;
if (array_key_exists($header, $_SERVER) && !empty($_SERVER[$header])) if (array_key_exists($header, $_SERVER) && !empty($_SERVER[$header]))
{ {
$ipKey = $header; $ipKey = $header;
@ -237,12 +230,12 @@ class zerobin
1, 1,
i18n::_( i18n::_(
'Please wait %d seconds between each post.', 'Please wait %d seconds between each post.',
$this->_conf['traffic']['limit'] $this->_conf->getKey('limit', 'traffic')
) )
); );
// Make sure content is not too big. // Make sure content is not too big.
$sizelimit = (int) $this->_getMainConfig('sizelimit', 2097152); $sizelimit = $this->_conf->getKey('sizelimit');
if ( if (
strlen($data) + strlen($attachment) + strlen($attachmentname) > $sizelimit strlen($data) + strlen($attachment) + strlen($attachmentname) > $sizelimit
) return $this->_return_message( ) return $this->_return_message(
@ -260,7 +253,7 @@ class zerobin
if($has_attachment) if($has_attachment)
{ {
if ( if (
!$this->_getMainConfig('fileupload', false) || !$this->_conf->getKey('fileupload') ||
!sjcl::isValid($attachment) || !sjcl::isValid($attachment) ||
!($has_attachmentname && sjcl::isValid($attachmentname)) !($has_attachmentname && sjcl::isValid($attachmentname))
) return $this->_return_message(1, 'Invalid attachment.'); ) return $this->_return_message(1, 'Invalid attachment.');
@ -273,13 +266,14 @@ class zerobin
if (array_key_exists('expire', $_POST) && !empty($_POST['expire'])) if (array_key_exists('expire', $_POST) && !empty($_POST['expire']))
{ {
$selected_expire = (string) $_POST['expire']; $selected_expire = (string) $_POST['expire'];
if (array_key_exists($selected_expire, $this->_conf['expire_options'])) $expire_options = $this->_conf->getSection('expire_options');
if (array_key_exists($selected_expire, $expire_options))
{ {
$expire = $this->_conf['expire_options'][$selected_expire]; $expire = $expire_options[$selected_expire];
} }
else else
{ {
$expire = $this->_conf['expire_options'][$this->_conf['expire']['default']]; $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
} }
if ($expire > 0) $meta['expire_date'] = time() + $expire; if ($expire > 0) $meta['expire_date'] = time() + $expire;
} }
@ -297,7 +291,7 @@ class zerobin
// Read open discussion flag. // Read open discussion flag.
if ( if (
$this->_getMainConfig('discussion', true) && $this->_conf->getKey('discussion') &&
array_key_exists('opendiscussion', $_POST) && array_key_exists('opendiscussion', $_POST) &&
!empty($_POST['opendiscussion']) !empty($_POST['opendiscussion'])
) )
@ -314,9 +308,9 @@ class zerobin
if (array_key_exists('formatter', $_POST) && !empty($_POST['formatter'])) if (array_key_exists('formatter', $_POST) && !empty($_POST['formatter']))
{ {
$formatter = $_POST['formatter']; $formatter = $_POST['formatter'];
if (!array_key_exists($formatter, $this->_conf['formatter_options'])) if (!array_key_exists($formatter, $this->_conf->getSection('formatter_options')))
{ {
$formatter = $this->_getMainConfig('defaultformatter', 'plaintext'); $formatter = $this->_conf->getKey('defaultformatter');
} }
$meta['formatter'] = $formatter; $meta['formatter'] = $formatter;
} }
@ -488,7 +482,7 @@ class zerobin
} }
// Make sure token is valid. // Make sure token is valid.
serversalt::setPath($this->_conf['traffic']['dir']); serversalt::setPath($this->_conf->getKey('dir', 'traffic'));
if (!filter::slow_equals($deletetoken, hash_hmac('sha1', $dataid, serversalt::get()))) if (!filter::slow_equals($deletetoken, hash_hmac('sha1', $dataid, serversalt::get())))
{ {
$this->_error = 'Wrong deletion token. Paste was not deleted.'; $this->_error = 'Wrong deletion token. Paste was not deleted.';
@ -571,7 +565,7 @@ class zerobin
} }
else else
{ {
$paste->meta->formatter = $this->_getMainConfig('defaultformatter', 'plaintext'); $paste->meta->formatter = $this->_conf->getKey('defaultformatter');
} }
} }
@ -613,17 +607,17 @@ class zerobin
// label all the expiration options // label all the expiration options
$expire = array(); $expire = array();
foreach ($this->_conf['expire_options'] as $time => $seconds) foreach ($this->_conf->getSection('expire_options') as $time => $seconds)
{ {
$expire[$time] = ($seconds == 0) ? i18n::_(ucfirst($time)): filter::time_humanreadable($time); $expire[$time] = ($seconds == 0) ? i18n::_(ucfirst($time)): filter::time_humanreadable($time);
} }
// translate all the formatter options // translate all the formatter options
$formatters = array_map(array('i18n', 'translate'), $this->_conf['formatter_options']); $formatters = array_map(array('i18n', 'translate'), $this->_conf->getSection('formatter_options'));
// set language cookie if that functionality was enabled // set language cookie if that functionality was enabled
$languageselection = ''; $languageselection = '';
if ($this->_getMainConfig('languageselection', false)) if ($this->_conf->getKey('languageselection'))
{ {
$languageselection = i18n::getLanguage(); $languageselection = i18n::getLanguage();
setcookie('lang', $languageselection); setcookie('lang', $languageselection);
@ -636,38 +630,23 @@ class zerobin
$page->assign('ERROR', i18n::_($this->_error)); $page->assign('ERROR', i18n::_($this->_error));
$page->assign('STATUS', i18n::_($this->_status)); $page->assign('STATUS', i18n::_($this->_status));
$page->assign('VERSION', self::VERSION); $page->assign('VERSION', self::VERSION);
$page->assign('DISCUSSION', $this->_getMainConfig('discussion', true)); $page->assign('DISCUSSION', $this->_conf->getKey('discussion'));
$page->assign('OPENDISCUSSION', $this->_getMainConfig('opendiscussion', true)); $page->assign('OPENDISCUSSION', $this->_conf->getKey('opendiscussion'));
$page->assign('MARKDOWN', array_key_exists('markdown', $formatters)); $page->assign('MARKDOWN', array_key_exists('markdown', $formatters));
$page->assign('SYNTAXHIGHLIGHTING', array_key_exists('syntaxhighlighting', $formatters)); $page->assign('SYNTAXHIGHLIGHTING', array_key_exists('syntaxhighlighting', $formatters));
$page->assign('SYNTAXHIGHLIGHTINGTHEME', $this->_getMainConfig('syntaxhighlightingtheme', '')); $page->assign('SYNTAXHIGHLIGHTINGTHEME', $this->_conf->getKey('syntaxhighlightingtheme'));
$page->assign('FORMATTER', $formatters); $page->assign('FORMATTER', $formatters);
$page->assign('FORMATTERDEFAULT', $this->_getMainConfig('defaultformatter', 'plaintext')); $page->assign('FORMATTERDEFAULT', $this->_conf->getKey('defaultformatter'));
$page->assign('NOTICE', i18n::_($this->_getMainConfig('notice', ''))); $page->assign('NOTICE', i18n::_($this->_conf->getKey('notice')));
$page->assign('BURNAFTERREADINGSELECTED', $this->_getMainConfig('burnafterreadingselected', false)); $page->assign('BURNAFTERREADINGSELECTED', $this->_conf->getKey('burnafterreadingselected'));
$page->assign('PASSWORD', $this->_getMainConfig('password', true)); $page->assign('PASSWORD', $this->_conf->getKey('password'));
$page->assign('FILEUPLOAD', $this->_getMainConfig('fileupload', false)); $page->assign('FILEUPLOAD', $this->_conf->getKey('fileupload'));
$page->assign('BASE64JSVERSION', $this->_getMainConfig('base64version', '2.1.9')); $page->assign('BASE64JSVERSION', $this->_conf->getKey('base64version'));
$page->assign('LANGUAGESELECTION', $languageselection); $page->assign('LANGUAGESELECTION', $languageselection);
$page->assign('LANGUAGES', i18n::getLanguageLabels(i18n::getAvailableLanguages())); $page->assign('LANGUAGES', i18n::getLanguageLabels(i18n::getAvailableLanguages()));
$page->assign('EXPIRE', $expire); $page->assign('EXPIRE', $expire);
$page->assign('EXPIREDEFAULT', $this->_conf['expire']['default']); $page->assign('EXPIREDEFAULT', $this->_conf->getKey('default', 'expire'));
$page->draw($this->_getMainConfig('template', 'page')); $page->draw($this->_conf->getKey('template'));
}
/**
* get configuration option from [main] section, optionally set a default
*
* @access private
* @param string $option
* @param mixed $default (optional)
* @return mixed
*/
private function _getMainConfig($option, $default = false)
{
return array_key_exists($option, $this->_conf['main']) ?
$this->_conf['main'][$option] :
$default;
} }
/** /**

View file

@ -3,6 +3,7 @@ error_reporting( E_ALL | E_STRICT );
// change this, if your php files and data is outside of your webservers document root // change this, if your php files and data is outside of your webservers document root
if (!defined('PATH')) define('PATH', '..' . DIRECTORY_SEPARATOR); if (!defined('PATH')) define('PATH', '..' . DIRECTORY_SEPARATOR);
if (!defined('CONF')) define('CONF', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini');
if (!defined('PUBLIC_PATH')) define('PUBLIC_PATH', '..'); if (!defined('PUBLIC_PATH')) define('PUBLIC_PATH', '..');
require PATH . 'lib/auto.php'; require PATH . 'lib/auto.php';
@ -63,7 +64,6 @@ class helper
return self::$pasteid; return self::$pasteid;
} }
/** /**
* get example paste * get example paste
* *
@ -76,7 +76,6 @@ class helper
return $example; return $example;
} }
/** /**
* get example paste ID * get example paste ID
* *
@ -87,7 +86,6 @@ class helper
return self::$commentid; return self::$commentid;
} }
/** /**
* get example comment * get example comment
* *
@ -127,6 +125,28 @@ class helper
} }
} }
/**
* create a backup of the config file
*
* @return void
*/
public static function confBackup()
{
if (!is_file(CONF . '.bak') && is_file(CONF))
rename(CONF, CONF . '.bak');
}
/**
* restor backup of the config file
*
* @return void
*/
public static function confRestore()
{
if (is_file(CONF . '.bak'))
rename(CONF . '.bak', CONF);
}
/** /**
* create ini file * create ini file
* *

View file

@ -589,7 +589,7 @@ class configurationTestGenerator
return <<<'EOT' return <<<'EOT'
<?php <?php
/** /**
* DO NOT EDIT: This file is automatically generated by configGenerator.php * DO NOT EDIT: This file is generated automatically using configGenerator.php
*/ */
class configurationTest extends PHPUnit_Framework_TestCase class configurationTest extends PHPUnit_Framework_TestCase
{ {
@ -600,9 +600,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
/* Setup Routine */ /* Setup Routine */
$this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini'; helper::confBackup();
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
$this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data')); $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
serversalt::setPath(PATH . 'data'); serversalt::setPath(PATH . 'data');
@ -612,7 +610,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function tearDown() public function tearDown()
{ {
/* Tear Down Routine */ /* Tear Down Routine */
rename($this->_conf . '.bak', $this->_conf); helper::confRestore();
} }
public function reset($configuration = array()) public function reset($configuration = array())
@ -622,7 +620,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
$_SERVER = array(); $_SERVER = array();
if ($this->_model->exists(helper::getPasteId())) if ($this->_model->exists(helper::getPasteId()))
$this->_model->delete(helper::getPasteId()); $this->_model->delete(helper::getPasteId());
helper::createIniFile($this->_conf, $configuration); helper::createIniFile(CONF, $configuration);
} }

View file

@ -1,8 +1,6 @@
<?php <?php
class zerobinTest extends PHPUnit_Framework_TestCase class zerobinTest extends PHPUnit_Framework_TestCase
{ {
private $_conf;
private $_model; private $_model;
public function setUp() public function setUp()
@ -10,7 +8,6 @@ class zerobinTest extends PHPUnit_Framework_TestCase
/* Setup Routine */ /* Setup Routine */
$this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data')); $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
serversalt::setPath(PATH . 'data'); serversalt::setPath(PATH . 'data');
$this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
$this->reset(); $this->reset();
} }
@ -26,8 +23,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$_SERVER = array(); $_SERVER = array();
if ($this->_model->exists(helper::getPasteId())) if ($this->_model->exists(helper::getPasteId()))
$this->_model->delete(helper::getPasteId()); $this->_model->delete(helper::getPasteId());
if (is_file($this->_conf . '.bak')) helper::confRestore();
rename($this->_conf . '.bak', $this->_conf);
} }
/** /**
@ -55,11 +51,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testViewLanguageSelection() public function testViewLanguageSelection()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['main']['languageselection'] = true; $options['main']['languageselection'] = true;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_COOKIE['lang'] = 'de'; $_COOKIE['lang'] = 'de';
ob_start(); ob_start();
new zerobin; new zerobin;
@ -104,9 +99,8 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testConf() public function testConf()
{ {
$this->reset(); $this->reset();
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); file_put_contents(CONF, '');
file_put_contents($this->_conf, '');
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -155,12 +149,11 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidSize() public function testCreateInvalidSize()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['main']['sizelimit'] = 10; $options['main']['sizelimit'] = 10;
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
ob_start(); ob_start();
@ -177,11 +170,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateProxyHeader() public function testCreateProxyHeader()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['header'] = 'X_FORWARDED_FOR'; $options['traffic']['header'] = 'X_FORWARDED_FOR';
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_SERVER['HTTP_X_FORWARDED_FOR'] = '::1'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '::1';
ob_start(); ob_start();
@ -198,11 +190,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateDuplicateId() public function testCreateDuplicateId()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$this->_model->create(helper::getPasteId(), helper::getPaste()); $this->_model->create(helper::getPasteId(), helper::getPaste());
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -220,11 +211,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateValidExpire() public function testCreateValidExpire()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['expire'] = '5min'; $_POST['expire'] = '5min';
$_POST['formatter'] = 'foo'; $_POST['formatter'] = 'foo';
@ -248,11 +238,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidExpire() public function testCreateInvalidExpire()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['expire'] = 'foo'; $_POST['expire'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -275,11 +264,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidBurn() public function testCreateInvalidBurn()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['burnafterreading'] = 'neither 1 nor 0'; $_POST['burnafterreading'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -297,11 +285,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidOpenDiscussion() public function testCreateInvalidOpenDiscussion()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['opendiscussion'] = 'neither 1 nor 0'; $_POST['opendiscussion'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -319,13 +306,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateAttachment() public function testCreateAttachment()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
$options['main']['fileupload'] = true; $options['main']['fileupload'] = true;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['attachment'] = $_POST['meta']['attachment'];
$_POST['attachmentname'] = $_POST['meta']['attachmentname'];
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
ob_start(); ob_start();
new zerobin; new zerobin;
@ -346,11 +334,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateValidNick() public function testCreateValidNick()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['nickname'] = helper::getComment()['meta']['nickname']; $_POST['nickname'] = helper::getComment()['meta']['nickname'];
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -373,11 +360,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidNick() public function testCreateInvalidNick()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getPaste(); $_POST = helper::getPaste();
$_POST['nickname'] = 'foo'; $_POST['nickname'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
@ -395,11 +381,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateComment() public function testCreateComment()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getComment(); $_POST = helper::getComment();
$_POST['pasteid'] = helper::getPasteId(); $_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId();
@ -419,11 +404,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidComment() public function testCreateInvalidComment()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getComment(); $_POST = helper::getComment();
$_POST['pasteid'] = helper::getPasteId(); $_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = 'foo'; $_POST['parentid'] = 'foo';
@ -443,11 +427,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateCommentDiscussionDisabled() public function testCreateCommentDiscussionDisabled()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getComment(); $_POST = helper::getComment();
$_POST['pasteid'] = helper::getPasteId(); $_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId();
@ -468,11 +451,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateCommentInvalidPaste() public function testCreateCommentInvalidPaste()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$_POST = helper::getComment(); $_POST = helper::getComment();
$_POST['pasteid'] = helper::getPasteId(); $_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId();
@ -491,11 +473,10 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateDuplicateComment() public function testCreateDuplicateComment()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true); $options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0; $options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf)) helper::confBackup();
rename($this->_conf, $this->_conf . '.bak'); helper::createIniFile(CONF, $options);
helper::createIniFile($this->_conf, $options);
$this->_model->create(helper::getPasteId(), helper::getPaste()); $this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()); $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data'); $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');