From 1604390e017e91e401eae1c6168e45d8acb909b4 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 27 Jul 2025 20:51:09 -0400 Subject: [PATCH] init 8 --- src/classes/console_app.php | 52 ++++++++-------- src/classes/php_file_cache.php | 4 ++ src/classes/services/encryption.php | 6 +- src/classes/services/html_filter.php | 73 +++++++++++++++++++++++ src/classes/services/liquid_templates.php | 3 +- src/classes/services/twig.php | 9 +-- 6 files changed, 112 insertions(+), 35 deletions(-) create mode 100644 src/classes/services/html_filter.php diff --git a/src/classes/console_app.php b/src/classes/console_app.php index 4145e1b..cf446fc 100644 --- a/src/classes/console_app.php +++ b/src/classes/console_app.php @@ -2,7 +2,7 @@ declare(strict_types=1); -/** +/** * @author Robert Strutts * @copyright Copyright (c) 2022, Robert Strutts. * @license MIT @@ -11,37 +11,37 @@ declare(strict_types=1); namespace CodeHydrater; final class console_app { - public static $is_cli = false; - - public static function is_cli() { - if (static::$is_cli) { - return true; - } - - if (defined('STDIN')) { - return true; - } - if (php_sapi_name() === 'cli') { - return true; - } + public static $is_cli = false; - if (array_key_exists('SHELL', $_ENV)) { - return true; - } + public static function is_cli() { + if (static::$is_cli) { + return true; + } + + if (defined('STDIN')) { + return true; + } + + if (php_sapi_name() === 'cli') { + return true; + } + + if (array_key_exists('SHELL', $_ENV)) { + return true; + } // $argv = $_SERVER['argv'] ?? []; // && count($argv) > 0 - - if (!isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT'])) { - return true; - } - if (!array_key_exists('REQUEST_METHOD', $_SERVER)) { - return true; - } + if (!isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT'])) { + return true; + } - return false; - } + if (!array_key_exists('REQUEST_METHOD', $_SERVER)) { + return true; + } + return false; + } } diff --git a/src/classes/php_file_cache.php b/src/classes/php_file_cache.php index 91df421..dfde1ee 100644 --- a/src/classes/php_file_cache.php +++ b/src/classes/php_file_cache.php @@ -10,6 +10,10 @@ namespace CodeHydrater; * @license MIT */ +/** + * May be used by the Liquid Template Engine + */ + class php_file_cache { protected $cache_path; diff --git a/src/classes/services/encryption.php b/src/classes/services/encryption.php index 07e1cb4..c1aa907 100644 --- a/src/classes/services/encryption.php +++ b/src/classes/services/encryption.php @@ -30,7 +30,7 @@ final class encryption { private $default_hash = 'sha256'; // should be sha256 or higher private $_iterations = self::iterations, $_length=self::length, $_raw=self::raw, $_key_bytes=self::key_bytes; - public function __construct(private string $key) { + public function __construct(#[\SensitiveParameter] private string $key) { $this->random_engine = new \CodeHydrater\random_engine(); } @@ -253,7 +253,7 @@ md5-128 0.77 * @param string $text data * @return string encoded text */ - public function encrypt(string $text, bool $validate = true): string { + public function encrypt(#[\SensitiveParameter] string $text, bool $validate = true): string { $key = $this->key; $key = ($validate) ? $this->get_valid_key($key) : $key; $ivsize = openssl_cipher_iv_length($this->method); @@ -325,7 +325,7 @@ md5-128 0.77 * @return string new key * @throws Exception */ - public function get_valid_key(string $key): string { + public function get_valid_key(#[\SensitiveParameter] string $key): string { $ivsize = openssl_cipher_iv_length($this->method); $key = substr($key, 0, $ivsize * 2); $keysize = strlen($key); diff --git a/src/classes/services/html_filter.php b/src/classes/services/html_filter.php new file mode 100644 index 0000000..72c68ea --- /dev/null +++ b/src/classes/services/html_filter.php @@ -0,0 +1,73 @@ +engine === false) ? false : true; + } + + public function set_defaults(): void { + $cache_dir = BaseDir . "/protected/runtime/html_purifier_filter_cache"; + + $this->config = \HTMLPurifier_Config::createDefault(); + $this->config->set('Core.Encoding', 'UTF-8'); + + if ($this->enable_file_caching) { + // Set the path for the serialized cache files + $this->config->set('Cache.SerializerPath', $cache_dir); + + // Optionally create the directory if it doesn't exist + if (!file_exists($cache_dir)) { + mkdir($cache_dir, 0775, true); + } + } + } + + public function set_config(string $key, $value, $a = null): void { + $this->config->set($key, $value, $a); + } + + public function init(): void { + $this->engine = new \HTMLPurifier($this->config); + } + + public function purify(string $html): string { + if ($this->engine === false) { + $this->init(); + } + return $this->engine->purify($html); + } +} diff --git a/src/classes/services/liquid_templates.php b/src/classes/services/liquid_templates.php index 7b40da1..70c7731 100644 --- a/src/classes/services/liquid_templates.php +++ b/src/classes/services/liquid_templates.php @@ -16,12 +16,11 @@ use Liquid\{Liquid, Template, Context}; use Liquid\Cache\Local; final class liquid_templates { - private $use_local_cache = false; private $liquid; private $dir; private $extension = 'tpl'; - public function __construct(string $template_extension = 'tpl') { + public function __construct(private $use_local_cache = false, string $template_extension = 'tpl') { $this->extension = $template_extension; if (! Reg::get('loader')->is_loaded('Liquid')) { Reg::get('loader')->add_namespace('Liquid', CodeHydrater_PROJECT . 'vendor/liquid/liquid/src/Liquid'); diff --git a/src/classes/services/twig.php b/src/classes/services/twig.php index 2f2965f..a29c5de 100644 --- a/src/classes/services/twig.php +++ b/src/classes/services/twig.php @@ -10,14 +10,15 @@ declare(strict_types = 1); namespace CodeHydrater\services; class twig { - public static function init() { + public static function init(bool $use_file_cache = false) { \CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Twig", CodeHydrater_PROJECT. "/vendor/twig/twig/src"); \CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Symfony\Polyfill\Mbstring", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-mbstring"); \CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Symfony\Polyfill\Ctype", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-ctype"); $loader = new \Twig\Loader\FilesystemLoader(CodeHydrater_PROJECT. "views/twig"); - $twig = new \Twig\Environment($loader, [ - 'cache' => BaseDir . "/protected/runtime/compilation_cache", - ]); + + $a_cache = ($use_file_cache) ? ['cache' => BaseDir . "/protected/runtime/compilation_cache"] : []; + + $twig = new \Twig\Environment($loader, $a_cache); return $twig; } }