mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2026-03-05 13:30:32 -05:00
Merge pull request #1526 from PrivateBin/pass-by-reference
Pass by reference & drop ctype
This commit is contained in:
@@ -34,7 +34,7 @@ abstract class AbstractData
|
||||
* @param array $paste
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function create($pasteid, array $paste);
|
||||
abstract public function create($pasteid, array &$paste);
|
||||
|
||||
/**
|
||||
* Read a paste.
|
||||
@@ -72,7 +72,7 @@ abstract class AbstractData
|
||||
* @param array $comment
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function createComment($pasteid, $parentid, $commentid, array $comment);
|
||||
abstract public function createComment($pasteid, $parentid, $commentid, array &$comment);
|
||||
|
||||
/**
|
||||
* Read all comments of paste.
|
||||
@@ -199,7 +199,7 @@ abstract class AbstractData
|
||||
* @param array $paste
|
||||
* @return array
|
||||
*/
|
||||
protected static function upgradePreV1Format(array $paste)
|
||||
protected static function upgradePreV1Format(array &$paste)
|
||||
{
|
||||
if (array_key_exists('attachment', $paste['meta'])) {
|
||||
$paste['attachment'] = $paste['meta']['attachment'];
|
||||
|
||||
@@ -140,7 +140,7 @@ class Database extends AbstractData
|
||||
* @param array $paste
|
||||
* @return bool
|
||||
*/
|
||||
public function create($pasteid, array $paste)
|
||||
public function create($pasteid, array &$paste)
|
||||
{
|
||||
$expire_date = 0;
|
||||
$opendiscussion = $burnafterreading = false;
|
||||
@@ -297,14 +297,18 @@ class Database extends AbstractData
|
||||
* @param array $comment
|
||||
* @return bool
|
||||
*/
|
||||
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
||||
public function createComment($pasteid, $parentid, $commentid, array &$comment)
|
||||
{
|
||||
if (array_key_exists('data', $comment)) {
|
||||
$version = 1;
|
||||
$data = $comment['data'];
|
||||
} else {
|
||||
$version = 2;
|
||||
$data = Json::encode($comment);
|
||||
try {
|
||||
$version = 2;
|
||||
$data = Json::encode($comment);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
list($createdKey, $iconKey) = $this->_getVersionedKeys($version);
|
||||
$meta = $comment['meta'];
|
||||
|
||||
@@ -85,7 +85,7 @@ class Filesystem extends AbstractData
|
||||
* @param array $paste
|
||||
* @return bool
|
||||
*/
|
||||
public function create($pasteid, array $paste)
|
||||
public function create($pasteid, array &$paste)
|
||||
{
|
||||
$storagedir = $this->_dataid2path($pasteid);
|
||||
$file = $storagedir . $pasteid . '.php';
|
||||
@@ -188,7 +188,7 @@ class Filesystem extends AbstractData
|
||||
* @param array $comment
|
||||
* @return bool
|
||||
*/
|
||||
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
||||
public function createComment($pasteid, $parentid, $commentid, array &$comment)
|
||||
{
|
||||
$storagedir = $this->_dataid2discussionpath($pasteid);
|
||||
$file = $storagedir . $pasteid . '.' . $commentid . '.' . $parentid . '.php';
|
||||
@@ -343,12 +343,11 @@ class Filesystem extends AbstractData
|
||||
*/
|
||||
private function _get($filename)
|
||||
{
|
||||
return Json::decode(
|
||||
substr(
|
||||
file_get_contents($filename),
|
||||
strlen(self::PROTECTION_LINE . PHP_EOL)
|
||||
)
|
||||
$data = substr(
|
||||
file_get_contents($filename),
|
||||
strlen(self::PROTECTION_LINE . PHP_EOL)
|
||||
);
|
||||
return Json::decode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -105,7 +105,7 @@ class GoogleCloudStorage extends AbstractData
|
||||
* @param $payload array to store
|
||||
* @return bool true if successful, otherwise false.
|
||||
*/
|
||||
private function _upload($key, $payload)
|
||||
private function _upload($key, &$payload)
|
||||
{
|
||||
$metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
|
||||
unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
|
||||
@@ -136,7 +136,7 @@ class GoogleCloudStorage extends AbstractData
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function create($pasteid, array $paste)
|
||||
public function create($pasteid, array &$paste)
|
||||
{
|
||||
if ($this->exists($pasteid)) {
|
||||
return false;
|
||||
@@ -201,7 +201,7 @@ class GoogleCloudStorage extends AbstractData
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
||||
public function createComment($pasteid, $parentid, $commentid, array &$comment)
|
||||
{
|
||||
if ($this->existsComment($pasteid, $parentid, $commentid)) {
|
||||
return false;
|
||||
@@ -219,7 +219,8 @@ class GoogleCloudStorage extends AbstractData
|
||||
$prefix = $this->_getKey($pasteid) . '/discussion/';
|
||||
try {
|
||||
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $key) {
|
||||
$comment = JSON::decode($this->_bucket->object($key->name())->downloadAsString());
|
||||
$data = $this->_bucket->object($key->name())->downloadAsString();
|
||||
$comment = Json::decode($data);
|
||||
$comment['id'] = basename($key->name());
|
||||
$slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
|
||||
$comments[$slot] = $comment;
|
||||
|
||||
@@ -165,7 +165,7 @@ class S3Storage extends AbstractData
|
||||
* @param $payload array to store
|
||||
* @return bool true if successful, otherwise false.
|
||||
*/
|
||||
private function _upload($key, $payload)
|
||||
private function _upload($key, &$payload)
|
||||
{
|
||||
$metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
|
||||
unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
|
||||
@@ -191,7 +191,7 @@ class S3Storage extends AbstractData
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function create($pasteid, array $paste)
|
||||
public function create($pasteid, array &$paste)
|
||||
{
|
||||
if ($this->exists($pasteid)) {
|
||||
return false;
|
||||
@@ -263,7 +263,7 @@ class S3Storage extends AbstractData
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
||||
public function createComment($pasteid, $parentid, $commentid, array &$comment)
|
||||
{
|
||||
if ($this->existsComment($pasteid, $parentid, $commentid)) {
|
||||
return false;
|
||||
|
||||
@@ -29,7 +29,7 @@ class FormatV2
|
||||
* @param bool $isComment
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValid($message, $isComment = false)
|
||||
public static function isValid(&$message, $isComment = false)
|
||||
{
|
||||
$required_keys = array('adata', 'v', 'ct');
|
||||
if ($isComment) {
|
||||
|
||||
12
lib/I18n.php
12
lib/I18n.php
@@ -183,9 +183,12 @@ class I18n
|
||||
|
||||
// load translations
|
||||
self::$_language = $match;
|
||||
self::$_translations = ($match == 'en') ? array() : Json::decode(
|
||||
file_get_contents(self::_getPath($match . '.json'))
|
||||
);
|
||||
if ($match == 'en') {
|
||||
self::$_translations = array();
|
||||
} else {
|
||||
$data = file_get_contents(self::_getPath($match . '.json'));
|
||||
self::$_translations = Json::decode($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,7 +276,8 @@ class I18n
|
||||
{
|
||||
$file = self::_getPath('languages.json');
|
||||
if (count(self::$_languageLabels) == 0 && is_readable($file)) {
|
||||
self::$_languageLabels = Json::decode(file_get_contents($file));
|
||||
$data = file_get_contents($file);
|
||||
self::$_languageLabels = Json::decode($data);
|
||||
}
|
||||
if (count($languages) == 0) {
|
||||
return self::$_languageLabels;
|
||||
|
||||
@@ -29,7 +29,7 @@ class Json
|
||||
* @throws Exception
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($input)
|
||||
public static function encode(&$input)
|
||||
{
|
||||
$jsonString = json_encode($input);
|
||||
self::_detectError();
|
||||
@@ -45,7 +45,7 @@ class Json
|
||||
* @throws Exception
|
||||
* @return mixed
|
||||
*/
|
||||
public static function decode($input)
|
||||
public static function decode(&$input)
|
||||
{
|
||||
$output = json_decode($input, true);
|
||||
self::_detectError();
|
||||
|
||||
@@ -100,9 +100,9 @@ abstract class AbstractModel
|
||||
* @param array $data
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setData(array $data)
|
||||
public function setData(array &$data)
|
||||
{
|
||||
$data = $this->_sanitize($data);
|
||||
$this->_sanitize($data);
|
||||
$this->_validate($data);
|
||||
$this->_data = $data;
|
||||
|
||||
@@ -155,7 +155,7 @@ abstract class AbstractModel
|
||||
*/
|
||||
public static function isValidId($id)
|
||||
{
|
||||
return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
|
||||
return (bool) preg_match('#\A[a-f0-9]{16}\z#', (string) $id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,9 +163,8 @@ abstract class AbstractModel
|
||||
*
|
||||
* @access protected
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function _sanitize(array $data);
|
||||
abstract protected function _sanitize(array &$data);
|
||||
|
||||
/**
|
||||
* Validate data.
|
||||
@@ -174,7 +173,7 @@ abstract class AbstractModel
|
||||
* @param array $data
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _validate(array $data)
|
||||
protected function _validate(array &$data)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ class Comment extends AbstractModel
|
||||
* @param Paste $paste
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setPaste(Paste $paste)
|
||||
public function setPaste(Paste &$paste)
|
||||
{
|
||||
$this->_paste = $paste;
|
||||
$this->_data['pasteid'] = $paste->getId();
|
||||
@@ -155,9 +155,8 @@ class Comment extends AbstractModel
|
||||
*
|
||||
* @access protected
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function _sanitize(array $data)
|
||||
protected function _sanitize(array &$data)
|
||||
{
|
||||
// we generate an icon based on a SHA512 HMAC of the users IP, if configured
|
||||
$icon = $this->_conf->getKey('icon');
|
||||
@@ -190,6 +189,5 @@ class Comment extends AbstractModel
|
||||
$data['meta']['icon'] = $pngdata;
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,11 +219,10 @@ class Paste extends AbstractModel
|
||||
*
|
||||
* @access protected
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function _sanitize(array $data)
|
||||
protected function _sanitize(array &$data)
|
||||
{
|
||||
$expiration = $data['meta']['expire'];
|
||||
$expiration = $data['meta']['expire'] ?? 0;
|
||||
unset($data['meta']['expire']);
|
||||
$expire_options = $this->_conf->getSection('expire_options');
|
||||
if (array_key_exists($expiration, $expire_options)) {
|
||||
@@ -235,7 +234,6 @@ class Paste extends AbstractModel
|
||||
if ($expire > 0) {
|
||||
$data['meta']['expire_date'] = time() + $expire;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,7 +243,7 @@ class Paste extends AbstractModel
|
||||
* @param array $data
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _validate(array $data)
|
||||
protected function _validate(array &$data)
|
||||
{
|
||||
// reject invalid or disabled formatters
|
||||
if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace PrivateBin;
|
||||
|
||||
use Exception;
|
||||
use PrivateBin\Model\Paste;
|
||||
|
||||
/**
|
||||
* Request
|
||||
@@ -84,7 +85,7 @@ class Request
|
||||
foreach ($_GET as $key => $value) {
|
||||
// only return if value is empty and key is 16 hex chars
|
||||
$key = (string) $key;
|
||||
if (($value === '') && strlen($key) === 16 && ctype_xdigit($key)) {
|
||||
if (empty($value) && Paste::isValidId($key)) {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
@@ -110,9 +111,8 @@ class Request
|
||||
// it might be a creation or a deletion, the latter is detected below
|
||||
$this->_operation = 'create';
|
||||
try {
|
||||
$this->_params = Json::decode(
|
||||
file_get_contents(self::$_inputStream)
|
||||
);
|
||||
$data = file_get_contents(self::$_inputStream);
|
||||
$this->_params = Json::decode($data);
|
||||
} catch (Exception $e) {
|
||||
// ignore error, $this->_params will remain empty
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class View
|
||||
$sri = array_key_exists($file, $this->_variables['SRI']) ?
|
||||
' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
|
||||
// if the file isn't versioned (ends in a digit), add our own version
|
||||
$cacheBuster = ctype_digit(substr($file, -4, 1)) ?
|
||||
$cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ?
|
||||
'' : '?' . rawurlencode($this->_variables['VERSION']);
|
||||
echo '<script ', $attributes,
|
||||
' type="text/javascript" data-cfasync="false" src="', $file,
|
||||
|
||||
Reference in New Issue
Block a user