main
Robert 5 months ago
parent bd1c35d830
commit 1604390e01
  1. 46
      src/classes/console_app.php
  2. 4
      src/classes/php_file_cache.php
  3. 6
      src/classes/services/encryption.php
  4. 73
      src/classes/services/html_filter.php
  5. 3
      src/classes/services/liquid_templates.php
  6. 9
      src/classes/services/twig.php

@ -11,37 +11,37 @@ declare(strict_types=1);
namespace CodeHydrater; namespace CodeHydrater;
final class console_app { final class console_app {
public static $is_cli = false;
public static function is_cli() { public static $is_cli = false;
if (static::$is_cli) {
return true;
}
if (defined('STDIN')) { public static function is_cli() {
return true; if (static::$is_cli) {
} return true;
}
if (php_sapi_name() === 'cli') { if (defined('STDIN')) {
return true; return true;
} }
if (array_key_exists('SHELL', $_ENV)) { if (php_sapi_name() === 'cli') {
return true; return true;
} }
if (array_key_exists('SHELL', $_ENV)) {
return true;
}
// $argv = $_SERVER['argv'] ?? []; // $argv = $_SERVER['argv'] ?? [];
// && count($argv) > 0 // && count($argv) > 0
if (!isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT'])) { if (!isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT'])) {
return true; return true;
} }
if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
return true;
}
return false; if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
} return true;
}
return false;
}
} }

@ -10,6 +10,10 @@ namespace CodeHydrater;
* @license MIT * @license MIT
*/ */
/**
* May be used by the Liquid Template Engine
*/
class php_file_cache { class php_file_cache {
protected $cache_path; protected $cache_path;

@ -30,7 +30,7 @@ final class encryption {
private $default_hash = 'sha256'; // should be sha256 or higher private $default_hash = 'sha256'; // should be sha256 or higher
private $_iterations = self::iterations, $_length=self::length, $_raw=self::raw, $_key_bytes=self::key_bytes; 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(); $this->random_engine = new \CodeHydrater\random_engine();
} }
@ -253,7 +253,7 @@ md5-128 0.77
* @param string $text data * @param string $text data
* @return string encoded text * @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 = $this->key;
$key = ($validate) ? $this->get_valid_key($key) : $key; $key = ($validate) ? $this->get_valid_key($key) : $key;
$ivsize = openssl_cipher_iv_length($this->method); $ivsize = openssl_cipher_iv_length($this->method);
@ -325,7 +325,7 @@ md5-128 0.77
* @return string new key * @return string new key
* @throws Exception * @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); $ivsize = openssl_cipher_iv_length($this->method);
$key = substr($key, 0, $ivsize * 2); $key = substr($key, 0, $ivsize * 2);
$keysize = strlen($key); $keysize = strlen($key);

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace CodeHydrater\services;
use \CodeHydrater\bootstrap\registry as Reg;
if (!defined('HTMLPURIFIER_PREFIX')) {
define('HTMLPURIFIER_PREFIX', CodeHydrater_PROJECT . 'vendor/ezyang/htmlpurifier/library');
}
final class html_filter {
private $engine = false;
private $config = null;
public function __construct(private $enable_file_caching = false) {
if (Reg::get('html_filter') === null) {
spl_autoload_register(function ($class) {
// HTMLPurifier uses underscores to indicate subfolders
$prefix = 'HTMLPurifier';
if (strpos($class, $prefix) === 0) {
$path = HTMLPURIFIER_PREFIX . "/" . str_replace('_', '/', $class) . '.php';
if (file_exists($path)) {
require_once $path;
}
}
});
Reg::set('html_filter', true);
}
}
/**
* Keep this as its used by safer_io.php in the p() method
* @return bool
*/
public function has_loaded(): bool {
return ($this->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);
}
}

@ -16,12 +16,11 @@ use Liquid\{Liquid, Template, Context};
use Liquid\Cache\Local; use Liquid\Cache\Local;
final class liquid_templates { final class liquid_templates {
private $use_local_cache = false;
private $liquid; private $liquid;
private $dir; private $dir;
private $extension = 'tpl'; 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; $this->extension = $template_extension;
if (! Reg::get('loader')->is_loaded('Liquid')) { if (! Reg::get('loader')->is_loaded('Liquid')) {
Reg::get('loader')->add_namespace('Liquid', CodeHydrater_PROJECT . 'vendor/liquid/liquid/src/Liquid'); Reg::get('loader')->add_namespace('Liquid', CodeHydrater_PROJECT . 'vendor/liquid/liquid/src/Liquid');

@ -10,14 +10,15 @@ declare(strict_types = 1);
namespace CodeHydrater\services; namespace CodeHydrater\services;
class twig { 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("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\Mbstring", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-mbstring");
\CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Symfony\Polyfill\Ctype", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-ctype"); \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"); $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; return $twig;
} }
} }

Loading…
Cancel
Save