Skip to content

Avoid invalid UTF-8 characters breaking json_encode by applying JSON_INVALID_UTF8_SUBSTITUTE flag (#15796) #16025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public function process()

return $this->source->hasErrors()
? $this->failure($this->source->getErrors(), [])
: json_encode($list);
: json_encode($list, JSON_INVALID_UTF8_SUBSTITUTE);
}
}
9 changes: 4 additions & 5 deletions core/src/Revolution/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,12 @@ public function setDefaultProperties(array $properties = []) {
*/
public function outputArray(array $array,$count = false) {
if ($count === false) { $count = count($array); }
$output = json_encode(
[
$output = json_encode([
'success' => true,
'total' => $count,
'results' => $array
]
);
], JSON_INVALID_UTF8_SUBSTITUTE);

if ($output === false) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Processor failed creating output array due to JSON error '.json_last_error());
return json_encode(['success' => false]);
Expand All @@ -291,7 +290,7 @@ public function toJSON($data) {
if (is_array($data)) {
array_walk_recursive($data, [&$this, '_encodeLiterals']);
}
return $this->_decodeLiterals($this->modx->toJSON($data));
return $this->_decodeLiterals(json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/resource/create.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->ctx . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/resource/data.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function loadCustomCssJs()
];
$this->addHtml('<script>
MODx.ctx = "' . $this->resource->get('context_key') . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->ctx . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->resource->get('context_key') . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->ctx . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->resource->get('context_key') . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/resource/update.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->resource->get('context_key') . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->ctx . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function loadCustomCssJs()
MODx.config.publish_document = "' . $this->canPublish . '";
MODx.onDocFormRender = "' . $this->onDocFormRender . '";
MODx.ctx = "' . $this->resource->get('context_key') . '";
Ext.onReady(function() {MODx.load(' . json_encode($data) . ')});</script>');
Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');

$this->loadRichTextEditor();
}
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/search.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function loadCustomCssJs() {
$this->addHtml("<script type=\"text/javascript\">Ext.onReady(function() {
MODx.load({
xtype: 'modx-page-search'
,record: " . json_encode(['q' => $this->searchQuery]) . "
,record: " . json_encode(['q' => $this->searchQuery], JSON_INVALID_UTF8_SUBSTITUTE) . "
});
});</script>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function loadCustomCssJs() {
$data = json_encode([
'xtype' => 'modx-page-dashboard-update',
'record' => $this->dashboardArray,
]);
], JSON_INVALID_UTF8_SUBSTITUTE);
$this->addHtml('<script>Ext.onReady(function(){MODx.load(' . $data . ')});</script>');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function loadCustomCssJs() {
'record' => $this->widgetArray,
];

$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . json_encode($data) . ');});</script>');
$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE) . ');});</script>');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/system/file/create.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function loadCustomCssJs()
$data = json_encode([
'xtype' => 'modx-page-file-create',
'record' => $this->fileRecord,
]);
], JSON_INVALID_UTF8_SUBSTITUTE);
$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . $data . ');});</script>');
}

Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/system/file/edit.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function loadCustomCssJs()
'file' => $this->filename,
'record' => $this->fileRecord,
'canSave' => (int)$this->canSave,
]);
], JSON_INVALID_UTF8_SUBSTITUTE);
$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . $data . ');});</script>');
}

Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/default/welcome.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function loadCustomCssJs()
['new_widgets' => $new_widgets]
)
];
$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . json_encode($obj) . ')});</script>');
$this->addHtml('<script>Ext.onReady(function() {MODx.load(' . json_encode($obj, JSON_INVALID_UTF8_SUBSTITUTE) . ')});</script>');
if ($this->showWelcomeScreen) {
$url = $this->modx->getOption('welcome_screen_url', null, 'http://misc.modx.com/revolution/welcome.20.html');
$this->addHtml('<script>Ext.onReady(function() { MODx.loadWelcomePanel("' . htmlspecialchars($url) . '"); });</script>');
Expand Down