ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\Framework\App\Bootstrap;
|
||||
use Magento\Framework\App\Response\Http;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
|
||||
require dirname(__DIR__, 4) . '/bootstrap.php';
|
||||
|
||||
$params = $_SERVER;
|
||||
|
||||
$bootstrap = Bootstrap::create(BP, $params);
|
||||
$objectManager = $bootstrap->getObjectManager();
|
||||
|
||||
// Load frontend event configuration because the observer is declared in etc/frontend/events.xml.
|
||||
$appState = $objectManager->get(\Magento\Framework\App\State::class);
|
||||
|
||||
try {
|
||||
$appState->setAreaCode('frontend');
|
||||
} catch (\Magento\Framework\Exception\LocalizedException $e) {
|
||||
// Area code may already be set. That's fine.
|
||||
}
|
||||
|
||||
$observer = $objectManager->get(
|
||||
\CK\LibxmlPainInTheAss\Observer\RepairMageInitAttributes::class
|
||||
);
|
||||
|
||||
echo "Observer class: " . get_class($observer) . PHP_EOL;
|
||||
|
||||
echo "============================================================\n";
|
||||
echo "CK_LibxmlPainInTheAss observer test\n";
|
||||
echo "============================================================\n";
|
||||
|
||||
echo 'PHP: ' . PHP_VERSION . PHP_EOL;
|
||||
echo 'libxml: ' . (defined('LIBXML_LOADED_VERSION') ? LIBXML_LOADED_VERSION : 'undefined') . PHP_EOL;
|
||||
|
||||
echo PHP_EOL;
|
||||
|
||||
// Create a real Magento HTTP response object.
|
||||
$response = $objectManager->create(Http::class);
|
||||
|
||||
$response->setHeader(
|
||||
'Content-Type',
|
||||
'text/html; charset=UTF-8',
|
||||
true
|
||||
);
|
||||
|
||||
/*
|
||||
* This is the exact malformed output caused by:
|
||||
*
|
||||
* libxml 2.15.3 saveHTML()
|
||||
* +
|
||||
* html_entity_decode(ENT_QUOTES)
|
||||
*/
|
||||
$input = '<html><body>' .
|
||||
'<div id="test-1" data-mage-init="{"key":"value"}"></div>' .
|
||||
'<div id="test-2" data-mage-init="{"foo":{"bar":"baz"}}"></div>' .
|
||||
'</body></html>';
|
||||
|
||||
$response->setContent($input);
|
||||
|
||||
echo "BEFORE:\n";
|
||||
echo $response->getContent() . PHP_EOL;
|
||||
echo PHP_EOL;
|
||||
|
||||
// Dispatch the exact Magento event used by the module.
|
||||
$eventManager = $objectManager->get(ManagerInterface::class);
|
||||
|
||||
$eventManager->dispatch(
|
||||
'controller_front_send_response_before',
|
||||
[
|
||||
'response' => $response,
|
||||
]
|
||||
);
|
||||
|
||||
$output = $response->getContent();
|
||||
|
||||
echo "AFTER:\n";
|
||||
echo $output . PHP_EOL;
|
||||
echo PHP_EOL;
|
||||
|
||||
// Assertions.
|
||||
echo "============================================================\n";
|
||||
echo "RESULTS\n";
|
||||
echo "============================================================\n";
|
||||
|
||||
$expected1 = '<div id="test-1" data-mage-init=\'{"key":"value"}\'></div>';
|
||||
$expected2 = '<div id="test-2" data-mage-init=\'{"foo":{"bar":"baz"}}\'></div>';
|
||||
|
||||
$isBugVersion = (defined('LIBXML_LOADED_VERSION') && (int)LIBXML_LOADED_VERSION === 21503);
|
||||
if ($isBugVersion) {
|
||||
$checks = [
|
||||
'Target libxml version detected' => true,
|
||||
'Basic data-mage-init repaired' => strpos($output, $expected1) !== false,
|
||||
'Nested JSON repaired' => strpos($output, $expected2) !== false,
|
||||
'Basic broken form removed' => strpos($output, '<div id="test-1" data-mage-init="{"key":"value"}"></div>') === false,
|
||||
'Nested broken form removed' => strpos($output, '<div id="test-2" data-mage-init="{"foo":{"bar":"baz"}}"></div>') === false,
|
||||
];
|
||||
} else {
|
||||
$checks = [
|
||||
'Non-target libxml version bypassed' => $output === $input,
|
||||
'Non-target libxml left input unchanged' => strpos($output, '<div id="test-1" data-mage-init="{"key":"value"}"></div>') !== false,
|
||||
'Nested HTML unchanged' => strpos($output, '<div id="test-2" data-mage-init="{"foo":{"bar":"baz"}}"></div>') !== false,
|
||||
];
|
||||
}
|
||||
|
||||
$failed = false;
|
||||
|
||||
foreach ($checks as $name => $passed) {
|
||||
echo str_pad($name, 35)
|
||||
. ': '
|
||||
. ($passed ? 'PASS' : 'FAIL')
|
||||
. PHP_EOL;
|
||||
|
||||
if (!$passed) {
|
||||
$failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
echo PHP_EOL;
|
||||
|
||||
if ($failed) {
|
||||
echo "OVERALL: FAIL\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "OVERALL: PASS\n";
|
||||
exit(0);
|
||||
@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
function findJsonEnd(string $html, int $start): ?int {
|
||||
$length = strlen($html);
|
||||
$stack = [];
|
||||
|
||||
$inString = false;
|
||||
$escaped = false;
|
||||
|
||||
for ($i = $start; $i < $length; $i++) {
|
||||
$char = $html[$i];
|
||||
|
||||
if ($inString) {
|
||||
if ($escaped) {
|
||||
$escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char === '\\') {
|
||||
$escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char === '"') {
|
||||
$inString = false;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char === '"') {
|
||||
$inString = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char === '{' || $char === '[') {
|
||||
$stack[] = $char;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char === '}' || $char === ']') {
|
||||
if (empty($stack)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$open = array_pop($stack);
|
||||
if (($open === '{' && $char !== '}') || ($open === '[' && $char !== ']')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (empty($stack)) {
|
||||
return $i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function repair(string $html): string {
|
||||
$length = strlen($html);
|
||||
$offset = 0;
|
||||
$output = '';
|
||||
$changed = false;
|
||||
|
||||
while ($offset < $length) {
|
||||
$match = preg_match(
|
||||
'/data-mage-init\s*=\s*"/i',
|
||||
$html,
|
||||
$matches,
|
||||
PREG_OFFSET_CAPTURE,
|
||||
$offset
|
||||
);
|
||||
|
||||
if ($match !== 1) {
|
||||
$output .= substr($html, $offset);
|
||||
break;
|
||||
}
|
||||
|
||||
$attributeOffset = $matches[0][1];
|
||||
$attributeToken = $matches[0][0];
|
||||
$attributeTokenLength = strlen($attributeToken);
|
||||
|
||||
// Only process data-mage-init when it appears to be an actual attribute boundary, not as part of another token.
|
||||
// Example: foo-data-mage-init="..."
|
||||
if ($attributeOffset === 0) {
|
||||
$output .= substr($html, $offset, $attributeTokenLength);
|
||||
$offset = $attributeTokenLength;
|
||||
continue;
|
||||
}
|
||||
$previousCharacter = $html[$attributeOffset - 1];
|
||||
if (!ctype_space($previousCharacter)) {
|
||||
$output .= substr(
|
||||
$html,
|
||||
$offset,
|
||||
($attributeOffset + $attributeTokenLength) - $offset
|
||||
);
|
||||
$offset = $attributeOffset + $attributeTokenLength;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Copy everything before data-mage-init="...
|
||||
$output .= substr(
|
||||
$html,
|
||||
$offset,
|
||||
$attributeOffset - $offset
|
||||
);
|
||||
|
||||
$jsonStart = $attributeOffset + $attributeTokenLength;
|
||||
|
||||
// The malformed value must begin with a JSON object or array.
|
||||
if ($jsonStart >= $length || ($html[$jsonStart] !== '{' && $html[$jsonStart] !== '[')) {
|
||||
$output .= $attributeToken;
|
||||
$offset = $jsonStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
$jsonEnd = findJsonEnd($html, $jsonStart);
|
||||
|
||||
// Could not safely identify a JSON value. Leave this occurrence untouched.
|
||||
if ($jsonEnd === null || $jsonEnd >= $length) {
|
||||
$output .= $attributeToken;
|
||||
$offset = $jsonStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
// The malformed attribute should end with a literal ".
|
||||
//
|
||||
// Example:
|
||||
// data-mage-init="{"key":"value"}"
|
||||
// ^
|
||||
if ($html[$jsonEnd] !== '"') {
|
||||
$output .= $attributeToken;
|
||||
$offset = $jsonStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ensure it's the end of the HTML attribute.
|
||||
$afterJson = $jsonEnd + 1;
|
||||
if ($afterJson < $length) {
|
||||
$nextCharacter = $html[$afterJson];
|
||||
if ($nextCharacter !== '>' && $nextCharacter !== '/' && !ctype_space($nextCharacter)) {
|
||||
$output .= $attributeToken;
|
||||
$offset = $jsonStart;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Extract the JSON from $html
|
||||
$json = substr(
|
||||
$html,
|
||||
$jsonStart,
|
||||
$jsonEnd - $jsonStart
|
||||
);
|
||||
|
||||
// Validate that this really is JSON.
|
||||
try {
|
||||
json_decode($json, true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException $e) {
|
||||
$output .= $attributeToken;
|
||||
$offset = $jsonStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
// JSON normally contains double quotes and no apostrophes, so single quotes are the safest HTML attribute delimiter.
|
||||
if (strpos($json, "'") === false) {
|
||||
$output .= 'data-mage-init=\'' . $json . '\'';
|
||||
} else {
|
||||
// The JSON contains a literal apostrophe, so use double quotes and HTML-escape its double quotes.
|
||||
$escapedJson = htmlspecialchars(
|
||||
$json,
|
||||
ENT_COMPAT | ENT_SUBSTITUTE,
|
||||
'UTF-8',
|
||||
false
|
||||
);
|
||||
|
||||
$output .= 'data-mage-init="' . $escapedJson . '"';
|
||||
}
|
||||
|
||||
$changed = true;
|
||||
|
||||
// Consume the final quote of the malformed attribute.
|
||||
$offset = $afterJson;
|
||||
}
|
||||
|
||||
return $changed ? $output : $html;
|
||||
}
|
||||
|
||||
$tests = [
|
||||
[
|
||||
'name' => 'Basic',
|
||||
'input' => '<div data-mage-init="{"key":"value"}"></div>',
|
||||
'expected' => '<div data-mage-init=\'{"key":"value"}\'></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'Magento JSON',
|
||||
'input' => '<div data-mage-init="{"Magento_Ui/js/core/app":{"components":{"x":{"component":"foo"}}}}"></div>',
|
||||
'expected' => '<div data-mage-init=\'{"Magento_Ui/js/core/app":{"components":{"x":{"component":"foo"}}}}\'></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'Nested JSON string',
|
||||
'input' => '<div data-mage-init="{"config":{"label":"He said \\"hello\\""}}"></div>',
|
||||
'expected' => '<div data-mage-init=\'{"config":{"label":"He said \\"hello\\""}}\'></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'JSON with apostrophe',
|
||||
'input' => '<div data-mage-init="{"label":"Bob\'s product"}"></div>',
|
||||
'expected' => '<div data-mage-init="{"label":"Bob\'s product"}"></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'Already valid libxml output',
|
||||
'input' => '<div data-mage-init="{"key":"value"}"></div>',
|
||||
'expected' => '<div data-mage-init="{"key":"value"}"></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'Other attribute',
|
||||
'input' => '<div foo-data-mage-init="{"key":"value"}"></div>',
|
||||
'expected' => '<div foo-data-mage-init="{"key":"value"}"></div>',
|
||||
],
|
||||
[
|
||||
'name' => 'Multiple attributes',
|
||||
'input' =>
|
||||
'<div data-mage-init="{"one":"1"}"></div>' .
|
||||
'<div data-mage-init="{"two":"2"}"></div>',
|
||||
'expected' =>
|
||||
'<div data-mage-init=\'{"one":"1"}\'></div>' .
|
||||
'<div data-mage-init=\'{"two":"2"}\'></div>',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($tests as $test) {
|
||||
$actual = repair($test['input']);
|
||||
|
||||
echo $test['name'] . ': '
|
||||
. ($actual === $test['expected'] ? 'PASS' : 'FAIL')
|
||||
. PHP_EOL;
|
||||
|
||||
if ($actual !== $test['expected']) {
|
||||
echo "Expected: " . $test['expected'] . PHP_EOL;
|
||||
echo "Actual: " . $actual . PHP_EOL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user