Merge branch 'rhymix:master' into master

This commit is contained in:
Lastorder 2026-01-08 20:28:49 -08:00 committed by GitHub
commit 2efe733d5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 67 additions and 22 deletions

View file

@ -127,9 +127,18 @@ class DB
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_STATEMENT_CLASS => array('\Rhymix\Framework\Helpers\DBStmtHelper'),
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
);
// Use unbuffered queries to reduce memory usage.
if (\PHP_VERSION_ID >= 80400)
{
$options[\PDO\MySQL::ATTR_USE_BUFFERED_QUERY] = false;
}
else
{
$options[\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false;
}
// Preload the statement helper class.
class_exists('\Rhymix\Framework\Helpers\DBStmtHelper');

View file

@ -179,14 +179,18 @@ class TemplateParser_v2
*/
protected function _addContextSwitches(string $content): string
{
$context_index = random_int(12000, 99000);
// Inline styles.
$content = preg_replace_callback('#(?<=\s)(style=")([^"]*?)"#i', function($match) {
return $match[1] . '<?php $this->config->context = \'CSS\'; ?>' . $match[2] . '<?php $this->config->context = \'HTML\'; ?>"';
}, $content);
// Inline scripts.
$content = preg_replace_callback('#(?<=\s)(href="javascript:|pattern="|on[a-z]+=")([^"]*?)"#i', function($match) {
return $match[1] . '<?php $this->config->context = \'JS\'; ?>' . $match[2] . '<?php $this->config->context = \'HTML\'; ?>"';
$content = preg_replace_callback('#(?<=\s)(href="javascript:|pattern="|on[a-z]+=")([^"]*?)"#i', function($match) use(&$context_index) {
$context_index++;
return $match[1] . '<?php $this->config->context = \'JS\'; /* !CTX' . $context_index . '! */?>' .
$match[2] . '<?php $this->config->context = \'HTML\'; /* !CTX' . $context_index . '! */?>"';
}, $content);
// <style> tags.
@ -202,14 +206,15 @@ class TemplateParser_v2
}, $content);
// <script> tags that aren't links.
$content = preg_replace_callback('#(<script\b([^>]*)|</script)#i', function($match) {
$content = preg_replace_callback('#(<script\b([^>]*)|</script)#i', function($match) use(&$context_index) {
if (substr($match[1], 1, 1) === '/')
{
return '<?php $this->config->context = \'HTML\'; ?>' . $match[1];
return '<?php $this->config->context = \'HTML\'; /* !CTX' . $context_index . '! */?>' . $match[1];
}
elseif (!str_contains($match[2] ?? '', 'src="'))
{
return $match[1] . '<?php $this->config->context = \'JS\'; ?>';
$context_index++;
return $match[1] . '<?php $this->config->context = \'JS\'; /* !CTX' . $context_index . '! */?>';
}
else
{
@ -217,6 +222,18 @@ class TemplateParser_v2
}
}, $content);
// Remove nested context switches.
if ($context_index > 0)
{
$content = preg_replace_callback('#(<\?php \$this->config->context = \'JS\'; /\* !CTX([0-9]+)! \*/\?>)(.*?)(<\?php \$this->config->context = \'HTML\'; /\* !CTX\2! \*/\?>)#s', function($match) {
return preg_replace('#/\* !CTX\d+! \*/#', '', $match[1]) .
preg_replace('#<\?php \$this->config->context = \'[A-Z]+\'; (?:/\* !CTX[0-9]+! \*/)?\?>#', '', $match[3]) .
preg_replace('#/\* !CTX\d+! \*/#', '', $match[4]);
}, $content);
$content = preg_replace('#(<\?php \$this->config->context = \'[A-Z]+\'; )/\* !CTX[0-9]+! \*/(\?>)#', '$1$2', $content);
}
return $content;
}