5 Commits
Author SHA1 Message Date
mcndave 046dec2819 - Removed the unused EchoToken (which is instead located within the PrintExtension)
- Added inline functions and make sure function can't be redeclared
2026-11-25 19:21:55 +00:00
mcndave 0eed9b76af - Fixed a bug related to encoding into a template exception 2026-10-27 18:18:47 +00:00
Dave Mc Nicoll 2931a462c0 - Added a new getExtensionInstanceOf() method in Compiler 2026-10-22 15:15:38 +00:00
mcndave 0f95171575 - Fixed HTTP_X_FORWARDED_HOST provided host 2026-10-19 19:20:58 +00:00
mcndave 131204d838 - removed composer.lock from files 2026-07-16 14:23:39 -04:00
7 changed files with 62 additions and 107 deletions

No files matched your search

Generated
-68
View File
@@ -1,68 +0,0 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9771fcd7256da80520a2d0d0f95bf311",
"packages": [
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
+13
View File
@@ -158,6 +158,19 @@ class Compiler
return $this->extensionList[$tokenName]; return $this->extensionList[$tokenName];
} }
public function getExtensionInstanceOf(string $class) : Extension\Extension
{
foreach(array_reverse($this->extensionList) as $ext) {
if ( is_a($ext, $class, false) ) {
return $ext;
}
}
throw new \InvalidArgumentException(<<<MSG
Requested extension instance of `$class` could not be found from loaded Picea compiler's extension.
MSG);
}
public function exportFunctions() : void public function exportFunctions() : void
{ {
static $caching = []; static $caching = [];
+3 -1
View File
@@ -31,6 +31,8 @@ class BlockToken implements ControlStructure {
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>"; return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
case "arguments": case "arguments":
$cleanArguments = htmlspecialchars($arguments);
return <<<PHP return <<<PHP
<?php <?php
try { try {
@@ -42,7 +44,7 @@ class BlockToken implements ControlStructure {
\$showArgs = fn(\$e) => (\$type = gettype(\$e)) === 'object' ? \$e::class : \$type; \$showArgs = fn(\$e) => (\$type = gettype(\$e)) === 'object' ? \$e::class : \$type;
throw new \Exception( throw new \Exception(
sprintf('A block awaiting arguments (%s) instead received (%s) with values (%s)', '$arguments', implode(', ', array_map(\$showArgs, \$inlineVariables ?? [])), json_encode(\$inlineVariables)) sprintf('A block awaiting arguments (%s) instead received (%s) with values (%s)', '$cleanArguments', implode(', ', array_map(\$showArgs, \$inlineVariables ?? [])), json_encode(\$inlineVariables))
); );
} }
/*%EXCEPTION_LINE_BASE%*/?> /*%EXCEPTION_LINE_BASE%*/?>
-28
View File
@@ -1,28 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EchoToken implements ControlStructure {
public array $token = [ "echo", "echo.raw" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) {
case "echo":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";
case "echo.raw":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoRaw($arguments) ?>";
}
}
public static function echoRaw($arguments) : string
{
return "<?php echo $arguments ?>";
}
public static function echoSafe($arguments) : string
{
return "<?php echo $arguments ?>";
}
}
+23 -6
View File
@@ -2,16 +2,18 @@
namespace Picea\ControlStructure; namespace Picea\ControlStructure;
use Picea\Compiler\Context;
class FunctionToken implements ControlStructure { class FunctionToken implements ControlStructure {
public array $token = [ "function", "endfunction", "return" ]; public array $token = [ "function", "endfunction", "return" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { public function parse(Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) { switch($token) {
case "function": case "function":
$context->functions++; $context->functions++;
return $this->printFunction($context, $arguments); return $this->printFunction($context, $options, $arguments);
case "return": case "return":
if ( empty($context->functions) ) { if ( empty($context->functions) ) {
@@ -31,11 +33,26 @@ class FunctionToken implements ControlStructure {
} }
} }
protected function printFunction($context, ?string $arguments) : string protected function printFunction(Context $context, array $options, ?string $arguments) : string
{ {
$name = trim(explode('(', $arguments, 2)[0]); list($name, $parsedArguments) = array_map('trim', explode('(', $arguments, 2));
return "<?php if (! function_exists(__NAMESPACE__ . '\\$name')) { function $arguments { ?>"; if ( in_array('inline', array_map('strtolower', $options)) ) {
$endPos = strrpos($parsedArguments, ')') + 1;
$return = substr($parsedArguments, $endPos);
$use = " use (\$___class__template, \$___global_variables, \$___variables, \$__event, \$picea)";
if ( str_contains($return, ':') ) {
$parsedArguments = substr_replace($parsedArguments, $use, $endPos, 0);
}
else {
$parsedArguments .= $use;
}
return "<?php if (isset(\$$name)) {throw new \LogicException('A variable named \'$name\' already exists in namespace ' . __NAMESPACE__);} \$$name = function ($parsedArguments { ?>";
}
return "<?php if (function_exists(__NAMESPACE__ . '\\$name')) {throw new \LogicException('A function named \'$name\' already exists in namespace ' . __NAMESPACE__);} function $arguments { ?>";
} }
protected function printReturn($context, ?string $arguments) : string protected function printReturn($context, ?string $arguments) : string
@@ -45,6 +62,6 @@ class FunctionToken implements ControlStructure {
protected function printEndFunction($context) : string protected function printEndFunction($context) : string
{ {
return "<?php } } ?>"; return "<?php } ?>";
} }
} }
+17 -4
View File
@@ -20,6 +20,19 @@ class PrintExtension implements Extension {
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$arguments = "(string) $arguments";
if (in_array('lc', $options) || in_array('lowercase', $options)) {
$arguments = "strtolower($arguments)";
}
elseif (in_array('uc', $options) || in_array('uppercase', $options)) {
$arguments = "strtoupper($arguments)";
}
if (in_array('ucfirst', $options)) {
$arguments = "ucfirst($arguments)";
}
switch($token) { switch($token) {
case 'echo': case 'echo':
case 'print': case 'print':
@@ -27,17 +40,17 @@ class PrintExtension implements Extension {
return "<?php echo $arguments ?>"; return "<?php echo $arguments ?>";
} }
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; return "<?php echo htmlspecialchars($arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'printf': case 'printf':
if (in_array('raw', $options)) { if (in_array('raw', $options)) {
return "<?php printf((string) $arguments) ?>"; return "<?php printf($arguments) ?>";
} }
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>"; return "<?php echo htmlspecialchars(sprintf($arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'no_html': case 'no_html':
return "<?php echo nl2br(strip_tags((string) $arguments)) ?>"; return "<?php echo nl2br(strip_tags($arguments)) ?>";
} }
} }
+6
View File
@@ -210,9 +210,15 @@ PATTERN;
protected function domain() : string protected function domain() : string
{ {
# Forwarded from an SSL connected host
if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) || ! empty($_SERVER['HTTP_X_FORWARDED_SSL']) ) { if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) || ! empty($_SERVER['HTTP_X_FORWARDED_SSL']) ) {
$port = ""; $port = "";
} }
# Forwarded from another host
elseif ( !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$port = explode(':', $_SERVER['HTTP_X_FORWARDED_HOST'])[1] ?? "";
}
# Default
else { else {
$port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT']; $port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT'];
} }