1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace nystudio107\crafttwigsandbox\web; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\web\View; |
7
|
|
|
use nystudio107\crafttwigsandbox\console\SandboxErrorHandler as ConsoleSandboxErrorHandler; |
8
|
|
|
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy; |
9
|
|
|
use nystudio107\crafttwigsandbox\web\SandboxErrorHandler as WebSandboxErrorHandler; |
10
|
|
|
use Throwable; |
11
|
|
|
use Twig\Extension\SandboxExtension; |
12
|
|
|
use Twig\Sandbox\SecurityPolicyInterface; |
13
|
|
|
|
14
|
|
|
class SandboxView extends View |
15
|
|
|
{ |
16
|
|
|
// Public Properties |
17
|
|
|
// ========================================================================= |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var SecurityPolicyInterface|null The security policy to use for the SandboxView |
21
|
|
|
*/ |
22
|
|
|
public ?SecurityPolicyInterface $securityPolicy = null; |
23
|
|
|
|
24
|
|
|
// Protected Properties |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null The error handler to use for the SandboxView |
29
|
|
|
*/ |
30
|
|
|
protected WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null $sandboxErrorHandler = null; |
31
|
|
|
|
32
|
|
|
// Public Methods |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
public function init(): void |
39
|
|
|
{ |
40
|
|
|
parent::init(); |
41
|
|
|
$this->sandboxErrorHandler = Craft::$app->getRequest()->getIsConsoleRequest() ? new ConsoleSandboxErrorHandler() : new WebSandboxErrorHandler(); |
42
|
|
|
// Use the passed in SecurityPolicy, or create a default security policy |
43
|
|
|
$this->securityPolicy = $this->securityPolicy ?? new BlacklistSecurityPolicy(); |
44
|
|
|
// Add the SandboxExtension with our SecurityPolicy lazily via ::registerTwigExtension() |
45
|
|
|
$this->registerTwigExtension(new SandboxExtension($this->securityPolicy, true)); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function renderObjectTemplate(string $template, mixed $object, array $variables = [], string $templateMode = self::TEMPLATE_MODE_SITE): string |
52
|
|
|
{ |
53
|
|
|
$result = ''; |
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$result = parent::renderObjectTemplate($template, $object, $variables, $templateMode); |
56
|
|
|
} catch (Throwable $e) { |
57
|
|
|
$this->sandboxErrorHandler->handleException($e); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
|
|
public function renderString(string $template, array $variables = [], string $templateMode = self::TEMPLATE_MODE_SITE, bool $escapeHtml = false): string |
67
|
|
|
{ |
68
|
|
|
$result = ''; |
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$result = parent::renderString($template, $variables, $templateMode, $escapeHtml); |
71
|
|
|
} catch (Throwable $e) { |
72
|
|
|
$this->sandboxErrorHandler->handleException($e); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function renderTemplate(string $template, array $variables = [], ?string $templateMode = null): string |
82
|
|
|
{ |
83
|
|
|
$result = ''; |
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$result = parent::renderTemplate($template, $variables, $templateMode); |
86
|
|
|
} catch (Throwable $e) { |
87
|
|
|
$this->sandboxErrorHandler->handleException($e); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|