Convert to PSR-2 coding style (using phpcs-fixer)

This commit is contained in:
Sobak
2016-07-26 08:19:35 +02:00
parent 884310add6
commit 5d7003ecc1
37 changed files with 636 additions and 665 deletions

View File

@@ -35,7 +35,9 @@ abstract class AbstractData
*
* @access protected
*/
protected function __construct() {}
protected function __construct()
{
}
/**
* enforce singleton, disable cloning
@@ -44,7 +46,9 @@ abstract class AbstractData
*
* @access private
*/
private function __clone() {}
private function __clone()
{
}
/**
* get instance of singleton
@@ -54,7 +58,9 @@ abstract class AbstractData
* @param array $options
* @return privatebin_abstract
*/
public static function getInstance($options) {}
public static function getInstance($options)
{
}
/**
* Create a paste.
@@ -143,12 +149,12 @@ abstract class AbstractData
*/
public function purge($batchsize)
{
if ($batchsize < 1) return;
if ($batchsize < 1) {
return;
}
$pastes = $this->_getExpiredPastes($batchsize);
if (count($pastes))
{
foreach ($pastes as $pasteid)
{
if (count($pastes)) {
foreach ($pastes as $pasteid) {
$this->delete($pasteid);
}
}
@@ -164,10 +170,11 @@ abstract class AbstractData
*/
protected function getOpenSlot(&$comments, $postdate)
{
if (array_key_exists($postdate, $comments))
{
if (array_key_exists($postdate, $comments)) {
$parts = explode('.', $postdate, 2);
if (!array_key_exists(1, $parts)) $parts[1] = 0;
if (!array_key_exists(1, $parts)) {
$parts[1] = 0;
}
++$parts[1];
return $this->getOpenSlot($comments, implode('.', $parts));
}

View File

@@ -42,7 +42,9 @@ class data extends AbstractData
if (
is_array($options) &&
array_key_exists('dir', $options)
) self::$_dir = $options['dir'] . DIRECTORY_SEPARATOR;
) {
self::$_dir = $options['dir'] . DIRECTORY_SEPARATOR;
}
// if needed initialize the singleton
if (!(self::$_instance instanceof privatebin_data)) {
self::$_instance = new self;
@@ -62,8 +64,12 @@ class data extends AbstractData
public function create($pasteid, $paste)
{
$storagedir = self::_dataid2path($pasteid);
if (is_file($storagedir . $pasteid)) return false;
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
if (is_file($storagedir . $pasteid)) {
return false;
}
if (!is_dir($storagedir)) {
mkdir($storagedir, 0705, true);
}
return (bool) @file_put_contents($storagedir . $pasteid, json_encode($paste));
}
@@ -76,16 +82,16 @@ class data extends AbstractData
*/
public function read($pasteid)
{
if (!$this->exists($pasteid)) return false;
if (!$this->exists($pasteid)) {
return false;
}
$paste = json_decode(
file_get_contents(self::_dataid2path($pasteid) . $pasteid)
);
if (property_exists($paste->meta, 'attachment'))
{
if (property_exists($paste->meta, 'attachment')) {
$paste->attachment = $paste->meta->attachment;
unset($paste->meta->attachment);
if (property_exists($paste->meta, 'attachmentname'))
{
if (property_exists($paste->meta, 'attachmentname')) {
$paste->attachmentname = $paste->meta->attachmentname;
unset($paste->meta->attachmentname);
}
@@ -107,13 +113,13 @@ class data extends AbstractData
// Delete discussion if it exists.
$discdir = self::_dataid2discussionpath($pasteid);
if (is_dir($discdir))
{
if (is_dir($discdir)) {
// Delete all files in discussion directory
$dir = dir($discdir);
while (false !== ($filename = $dir->read()))
{
if (is_file($discdir . $filename)) @unlink($discdir . $filename);
while (false !== ($filename = $dir->read())) {
if (is_file($discdir . $filename)) {
@unlink($discdir . $filename);
}
}
$dir->close();
@@ -148,8 +154,12 @@ class data extends AbstractData
{
$storagedir = self::_dataid2discussionpath($pasteid);
$filename = $pasteid . '.' . $commentid . '.' . $parentid;
if (is_file($storagedir . $filename)) return false;
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
if (is_file($storagedir . $filename)) {
return false;
}
if (!is_dir($storagedir)) {
mkdir($storagedir, 0705, true);
}
return (bool) @file_put_contents($storagedir . $filename, json_encode($comment));
}
@@ -164,18 +174,15 @@ class data extends AbstractData
{
$comments = array();
$discdir = self::_dataid2discussionpath($pasteid);
if (is_dir($discdir))
{
if (is_dir($discdir)) {
// Delete all files in discussion directory
$dir = dir($discdir);
while (false !== ($filename = $dir->read()))
{
while (false !== ($filename = $dir->read())) {
// Filename is in the form pasteid.commentid.parentid:
// - pasteid is the paste this reply belongs to.
// - commentid is the comment identifier itself.
// - parentid is the comment this comment replies to (It can be pasteid)
if (is_file($discdir . $filename))
{
if (is_file($discdir . $filename)) {
$comment = json_decode(file_get_contents($discdir . $filename));
$items = explode('.', $filename);
// Add some meta information not contained in file.
@@ -226,11 +233,9 @@ class data extends AbstractData
scandir(self::$_dir),
array('self', '_isFirstLevelDir')
);
if (count($firstLevel) > 0)
{
if (count($firstLevel) > 0) {
// try at most 10 times the $batchsize pastes before giving up
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i)
{
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
$firstKey = array_rand($firstLevel);
$secondLevel = array_filter(
scandir(self::$_dir . $firstLevel[$firstKey]),
@@ -238,8 +243,7 @@ class data extends AbstractData
);
// skip this folder in the next checks if it is empty
if (count($secondLevel) == 0)
{
if (count($secondLevel) == 0) {
unset($firstLevel[$firstKey]);
continue;
}
@@ -247,26 +251,32 @@ class data extends AbstractData
$secondKey = array_rand($secondLevel);
$path = self::$_dir . $firstLevel[$firstKey] .
DIRECTORY_SEPARATOR . $secondLevel[$secondKey];
if (!is_dir($path)) continue;
if (!is_dir($path)) {
continue;
}
$thirdLevel = array_filter(
scandir($path),
array('PrivateBin\\model\\paste', 'isValidId')
);
if (count($thirdLevel) == 0) continue;
if (count($thirdLevel) == 0) {
continue;
}
$thirdKey = array_rand($thirdLevel);
$pasteid = $thirdLevel[$thirdKey];
if (in_array($pasteid, $pastes)) continue;
if (in_array($pasteid, $pastes)) {
continue;
}
if ($this->exists($pasteid))
{
if ($this->exists($pasteid)) {
$data = $this->read($pasteid);
if (
property_exists($data->meta, 'expire_date') &&
$data->meta->expire_date < time()
)
{
) {
$pastes[] = $pasteid;
if (count($pastes) >= $batchsize) break;
if (count($pastes) >= $batchsize) {
break;
}
}
}
}
@@ -284,10 +294,11 @@ class data extends AbstractData
private static function _init()
{
// Create storage directory if it does not exist.
if (!is_dir(self::$_dir)) mkdir(self::$_dir, 0705);
if (!is_dir(self::$_dir)) {
mkdir(self::$_dir, 0705);
}
// Create .htaccess file if it does not exist.
if (!is_file(self::$_dir . '.htaccess'))
{
if (!is_file(self::$_dir . '.htaccess')) {
file_put_contents(
self::$_dir . '.htaccess',
'Allow from none' . PHP_EOL .

View File

@@ -71,14 +71,15 @@ class db extends AbstractData
public static function getInstance($options = null)
{
// if needed initialize the singleton
if(!(self::$_instance instanceof privatebin_db)) {
if (!(self::$_instance instanceof privatebin_db)) {
self::$_instance = new self;
}
if (is_array($options))
{
if (is_array($options)) {
// set table prefix if given
if (array_key_exists('tbl', $options)) self::$_prefix = $options['tbl'];
if (array_key_exists('tbl', $options)) {
self::$_prefix = $options['tbl'];
}
// initialize the db connection with new options
if (
@@ -86,8 +87,7 @@ class db extends AbstractData
array_key_exists('usr', $options) &&
array_key_exists('pwd', $options) &&
array_key_exists('opt', $options)
)
{
) {
// set default options
$options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$options['opt'][PDO::ATTR_EMULATE_PREPARES] = false;
@@ -110,40 +110,34 @@ class db extends AbstractData
$tables = self::$_db->query($tableQuery)->fetchAll(PDO::FETCH_COLUMN, 0);
// create paste table if necessary
if (!in_array(self::_sanitizeIdentifier('paste'), $tables))
{
if (!in_array(self::_sanitizeIdentifier('paste'), $tables)) {
self::_createPasteTable();
$db_tables_exist = false;
}
// create comment table if necessary
if (!in_array(self::_sanitizeIdentifier('comment'), $tables))
{
if (!in_array(self::_sanitizeIdentifier('comment'), $tables)) {
self::_createCommentTable();
$db_tables_exist = false;
}
// create config table if necessary
$db_version = privatebin::VERSION;
if (!in_array(self::_sanitizeIdentifier('config'), $tables))
{
if (!in_array(self::_sanitizeIdentifier('config'), $tables)) {
self::_createConfigTable();
// if we only needed to create the config table, the DB is older then 0.22
if ($db_tables_exist) $db_version = '0.21';
}
else
{
if ($db_tables_exist) {
$db_version = '0.21';
}
} else {
$db_version = self::_getConfig('VERSION');
}
// update database structure if necessary
if (version_compare($db_version, privatebin::VERSION, '<'))
{
if (version_compare($db_version, privatebin::VERSION, '<')) {
self::_upgradeDatabase($db_version);
}
}
else
{
} else {
throw new Exception(
'Missing configuration for key dsn, usr, pwd or opt in the section model_options, please check your configuration file', 6
);
@@ -166,7 +160,7 @@ class db extends AbstractData
if (
array_key_exists($pasteid, self::$_cache)
) {
if(false !== self::$_cache[$pasteid]) {
if (false !== self::$_cache[$pasteid]) {
return false;
} else {
unset(self::$_cache[$pasteid]);
@@ -178,28 +172,23 @@ class db extends AbstractData
$meta = $paste['meta'];
unset($meta['postdate']);
$expire_date = 0;
if (array_key_exists('expire_date', $paste['meta']))
{
if (array_key_exists('expire_date', $paste['meta'])) {
$expire_date = (int) $paste['meta']['expire_date'];
unset($meta['expire_date']);
}
if (array_key_exists('opendiscussion', $paste['meta']))
{
if (array_key_exists('opendiscussion', $paste['meta'])) {
$opendiscussion = (bool) $paste['meta']['opendiscussion'];
unset($meta['opendiscussion']);
}
if (array_key_exists('burnafterreading', $paste['meta']))
{
if (array_key_exists('burnafterreading', $paste['meta'])) {
$burnafterreading = (bool) $paste['meta']['burnafterreading'];
unset($meta['burnafterreading']);
}
if (array_key_exists('attachment', $paste['meta']))
{
if (array_key_exists('attachment', $paste['meta'])) {
$attachment = $paste['meta']['attachment'];
unset($meta['attachment']);
}
if (array_key_exists('attachmentname', $paste['meta']))
{
if (array_key_exists('attachmentname', $paste['meta'])) {
$attachmentname = $paste['meta']['attachmentname'];
unset($meta['attachmentname']);
}
@@ -238,31 +227,29 @@ class db extends AbstractData
' WHERE dataid = ?', array($pasteid), true
);
if(false !== $paste) {
if (false !== $paste) {
// create object
self::$_cache[$pasteid] = new stdClass;
self::$_cache[$pasteid]->data = $paste['data'];
$meta = json_decode($paste['meta']);
if (!is_object($meta)) $meta = new stdClass;
if (!is_object($meta)) {
$meta = new stdClass;
}
// support older attachments
if (property_exists($meta, 'attachment'))
{
if (property_exists($meta, 'attachment')) {
self::$_cache[$pasteid]->attachment = $meta->attachment;
unset($meta->attachment);
if (property_exists($meta, 'attachmentname'))
{
if (property_exists($meta, 'attachmentname')) {
self::$_cache[$pasteid]->attachmentname = $meta->attachmentname;
unset($meta->attachmentname);
}
}
// support current attachments
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment']))
{
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
self::$_cache[$pasteid]->attachment = $paste['attachment'];
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname']))
{
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
}
}
@@ -271,13 +258,19 @@ class db extends AbstractData
$expire_date = (int) $paste['expiredate'];
if (
$expire_date > 0
) self::$_cache[$pasteid]->meta->expire_date = $expire_date;
) {
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
}
if (
$paste['opendiscussion']
) self::$_cache[$pasteid]->meta->opendiscussion = true;
) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
}
if (
$paste['burnafterreading']
) self::$_cache[$pasteid]->meta->burnafterreading = true;
) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
}
}
}
@@ -303,7 +296,9 @@ class db extends AbstractData
);
if (
array_key_exists($pasteid, self::$_cache)
) unset(self::$_cache[$pasteid]);
) {
unset(self::$_cache[$pasteid]);
}
}
/**
@@ -317,7 +312,9 @@ class db extends AbstractData
{
if (
!array_key_exists($pasteid, self::$_cache)
) self::$_cache[$pasteid] = $this->read($pasteid);
) {
self::$_cache[$pasteid] = $this->read($pasteid);
}
return (bool) self::$_cache[$pasteid];
}
@@ -333,10 +330,8 @@ class db extends AbstractData
*/
public function createComment($pasteid, $parentid, $commentid, $comment)
{
foreach (array('nickname', 'vizhash') as $key)
{
if (!array_key_exists($key, $comment['meta']))
{
foreach (array('nickname', 'vizhash') as $key) {
if (!array_key_exists($key, $comment['meta'])) {
$comment['meta'][$key] = null;
}
}
@@ -371,10 +366,8 @@ class db extends AbstractData
// create comment list
$comments = array();
if (count($rows))
{
foreach ($rows as $row)
{
if (count($rows)) {
foreach ($rows as $row) {
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
$comments[$i] = new stdClass;
$comments[$i]->id = $row['dataid'];
@@ -382,10 +375,12 @@ class db extends AbstractData
$comments[$i]->data = $row['data'];
$comments[$i]->meta = new stdClass;
$comments[$i]->meta->postdate = (int) $row['postdate'];
if (array_key_exists('nickname', $row) && !empty($row['nickname']))
if (array_key_exists('nickname', $row) && !empty($row['nickname'])) {
$comments[$i]->meta->nickname = $row['nickname'];
if (array_key_exists('vizhash', $row) && !empty($row['vizhash']))
}
if (array_key_exists('vizhash', $row) && !empty($row['vizhash'])) {
$comments[$i]->meta->vizhash = $row['vizhash'];
}
}
ksort($comments);
}
@@ -424,10 +419,8 @@ class db extends AbstractData
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
' WHERE expiredate < ? LIMIT ?', array(time(), $batchsize)
);
if (count($rows))
{
foreach ($rows as $row)
{
if (count($rows)) {
foreach ($rows as $row) {
$pastes[] = $row['dataid'];
}
}
@@ -485,8 +478,7 @@ class db extends AbstractData
*/
private static function _getTableQuery($type)
{
switch($type)
{
switch ($type) {
case 'ibm':
$sql = 'SELECT tabname FROM SYSCAT.TABLES ';
break;
@@ -559,12 +551,9 @@ class db extends AbstractData
private static function _getPrimaryKeyClauses($key = 'dataid')
{
$main_key = $after_key = '';
if (self::$_type === 'mysql')
{
if (self::$_type === 'mysql') {
$after_key = ", PRIMARY KEY ($key)";
}
else
{
} else {
$main_key = ' PRIMARY KEY';
}
return array($main_key, $after_key);
@@ -667,8 +656,7 @@ class db extends AbstractData
private static function _upgradeDatabase($oldversion)
{
$dataType = self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
switch ($oldversion)
{
switch ($oldversion) {
case '0.21':
// create the meta column if necessary (pre 0.21 change)
try {
@@ -687,8 +675,7 @@ class db extends AbstractData
);
// SQLite doesn't support MODIFY, but it allows TEXT of similar
// size as BLOB, so there is no need to change it there
if (self::$_type !== 'sqlite')
{
if (self::$_type !== 'sqlite') {
self::$_db->exec(
'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
' ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType;'
@@ -698,9 +685,7 @@ class db extends AbstractData
" ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType, " .
"MODIFY COLUMN nickname $dataType, MODIFY COLUMN vizhash $dataType;"
);
}
else
{
} else {
self::$_db->exec(
'CREATE UNIQUE INDEX IF NOT EXISTS paste_dataid ON ' .
self::_sanitizeIdentifier('paste') . '(dataid);'