pass by reference, closes #858

This commit is contained in:
El RIDO 2025-03-11 07:59:10 +01:00
parent e67972417e
commit 629f263cf5
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
12 changed files with 37 additions and 35 deletions

View file

@ -2,6 +2,7 @@
## 1.7.7 (not yet released) ## 1.7.7 (not yet released)
* ADDED: Switching templates using the web ui (#1501) * ADDED: Switching templates using the web ui (#1501)
* CHANGED: Passing large data structures by reference to reduce memory consumption (#858)
* CHANGED: Upgrading libraries to: ip-lib 1.20.0 * CHANGED: Upgrading libraries to: ip-lib 1.20.0
## 1.7.6 (2025-02-01) ## 1.7.6 (2025-02-01)

View file

@ -343,12 +343,11 @@ class Filesystem extends AbstractData
*/ */
private function _get($filename) private function _get($filename)
{ {
return Json::decode( $data = substr(
substr(
file_get_contents($filename), file_get_contents($filename),
strlen(self::PROTECTION_LINE . PHP_EOL) strlen(self::PROTECTION_LINE . PHP_EOL)
)
); );
return Json::decode($data);
} }
/** /**

View file

@ -219,7 +219,8 @@ class GoogleCloudStorage extends AbstractData
$prefix = $this->_getKey($pasteid) . '/discussion/'; $prefix = $this->_getKey($pasteid) . '/discussion/';
try { try {
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $key) { 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()); $comment['id'] = basename($key->name());
$slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']); $slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
$comments[$slot] = $comment; $comments[$slot] = $comment;

View file

@ -29,7 +29,7 @@ class FormatV2
* @param bool $isComment * @param bool $isComment
* @return bool * @return bool
*/ */
public static function isValid($message, $isComment = false) public static function isValid(&$message, $isComment = false)
{ {
$required_keys = array('adata', 'v', 'ct'); $required_keys = array('adata', 'v', 'ct');
if ($isComment) { if ($isComment) {

View file

@ -183,9 +183,12 @@ class I18n
// load translations // load translations
self::$_language = $match; self::$_language = $match;
self::$_translations = ($match == 'en') ? array() : Json::decode( if ($match == 'en') {
file_get_contents(self::_getPath($match . '.json')) 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'); $file = self::_getPath('languages.json');
if (count(self::$_languageLabels) == 0 && is_readable($file)) { 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) { if (count($languages) == 0) {
return self::$_languageLabels; return self::$_languageLabels;

View file

@ -29,7 +29,7 @@ class Json
* @throws Exception * @throws Exception
* @return string * @return string
*/ */
public static function encode($input) public static function encode(&$input)
{ {
$jsonString = json_encode($input); $jsonString = json_encode($input);
self::_detectError(); self::_detectError();
@ -45,7 +45,7 @@ class Json
* @throws Exception * @throws Exception
* @return mixed * @return mixed
*/ */
public static function decode($input) public static function decode(&$input)
{ {
$output = json_decode($input, true); $output = json_decode($input, true);
self::_detectError(); self::_detectError();

View file

@ -100,9 +100,9 @@ abstract class AbstractModel
* @param array $data * @param array $data
* @throws Exception * @throws Exception
*/ */
public function setData(array $data) public function setData(array &$data)
{ {
$data = $this->_sanitize($data); $this->_sanitize($data);
$this->_validate($data); $this->_validate($data);
$this->_data = $data; $this->_data = $data;
@ -163,9 +163,8 @@ abstract class AbstractModel
* *
* @access protected * @access protected
* @param array $data * @param array $data
* @return array
*/ */
abstract protected function _sanitize(array $data); abstract protected function _sanitize(array &$data);
/** /**
* Validate data. * Validate data.
@ -174,7 +173,7 @@ abstract class AbstractModel
* @param array $data * @param array $data
* @throws Exception * @throws Exception
*/ */
protected function _validate(array $data) protected function _validate(array &$data)
{ {
} }
} }

View file

@ -104,7 +104,7 @@ class Comment extends AbstractModel
* @param Paste $paste * @param Paste $paste
* @throws Exception * @throws Exception
*/ */
public function setPaste(Paste $paste) public function setPaste(Paste &$paste)
{ {
$this->_paste = $paste; $this->_paste = $paste;
$this->_data['pasteid'] = $paste->getId(); $this->_data['pasteid'] = $paste->getId();
@ -155,9 +155,8 @@ class Comment extends AbstractModel
* *
* @access protected * @access protected
* @param array $data * @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 // we generate an icon based on a SHA512 HMAC of the users IP, if configured
$icon = $this->_conf->getKey('icon'); $icon = $this->_conf->getKey('icon');
@ -190,6 +189,5 @@ class Comment extends AbstractModel
$data['meta']['icon'] = $pngdata; $data['meta']['icon'] = $pngdata;
} }
} }
return $data;
} }
} }

View file

@ -219,11 +219,10 @@ class Paste extends AbstractModel
* *
* @access protected * @access protected
* @param array $data * @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']); unset($data['meta']['expire']);
$expire_options = $this->_conf->getSection('expire_options'); $expire_options = $this->_conf->getSection('expire_options');
if (array_key_exists($expiration, $expire_options)) { if (array_key_exists($expiration, $expire_options)) {
@ -235,7 +234,6 @@ class Paste extends AbstractModel
if ($expire > 0) { if ($expire > 0) {
$data['meta']['expire_date'] = time() + $expire; $data['meta']['expire_date'] = time() + $expire;
} }
return $data;
} }
/** /**
@ -245,7 +243,7 @@ class Paste extends AbstractModel
* @param array $data * @param array $data
* @throws Exception * @throws Exception
*/ */
protected function _validate(array $data) protected function _validate(array &$data)
{ {
// reject invalid or disabled formatters // reject invalid or disabled formatters
if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) { if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) {

View file

@ -110,9 +110,8 @@ class Request
// it might be a creation or a deletion, the latter is detected below // it might be a creation or a deletion, the latter is detected below
$this->_operation = 'create'; $this->_operation = 'create';
try { try {
$this->_params = Json::decode( $data = file_get_contents(self::$_inputStream);
file_get_contents(self::$_inputStream) $this->_params = Json::decode($data);
);
} catch (Exception $e) { } catch (Exception $e) {
// ignore error, $this->_params will remain empty // ignore error, $this->_params will remain empty
} }

View file

@ -7,10 +7,11 @@ class FormatV2Test extends TestCase
{ {
public function testFormatV2ValidatorValidatesCorrectly() public function testFormatV2ValidatorValidatesCorrectly()
{ {
$this->assertTrue(FormatV2::isValid(Helper::getPastePost()), 'valid format');
$this->assertTrue(FormatV2::isValid(Helper::getCommentPost(), true), 'valid format');
$paste = Helper::getPastePost(); $paste = Helper::getPastePost();
$comment = Helper::getCommentPost();
$this->assertTrue(FormatV2::isValid($paste), 'valid format');
$this->assertTrue(FormatV2::isValid($comment, true), 'valid format');
$paste['adata'][0][0] = '$'; $paste['adata'][0][0] = '$';
$this->assertFalse(FormatV2::isValid($paste), 'invalid base64 encoding of iv'); $this->assertFalse(FormatV2::isValid($paste), 'invalid base64 encoding of iv');
@ -68,6 +69,7 @@ class FormatV2Test extends TestCase
$paste['adata'][0][7] = '!#@'; $paste['adata'][0][7] = '!#@';
$this->assertFalse(FormatV2::isValid($paste), 'invalid compression'); $this->assertFalse(FormatV2::isValid($paste), 'invalid compression');
$this->assertFalse(FormatV2::isValid(Helper::getPaste()), 'invalid meta key'); $paste = Helper::getPaste();
$this->assertFalse(FormatV2::isValid($paste), 'invalid meta key');
} }
} }

View file

@ -163,7 +163,8 @@ class ModelTest extends TestCase
$this->_conf->getSection('model_options') $this->_conf->getSection('model_options')
) )
); );
$comment->setPaste($this->_model->getPaste(Helper::getPasteId())); $paste = $this->_model->getPaste(Helper::getPasteId());
$comment->setPaste($paste);
$this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID'); $this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID');
} }