Compare commits

...

4 Commits

Author SHA1 Message Date
8cb306882c
Refactor encryption/decryption methods, add tag parsing & file handling
- Updated encryption/decryption methods for better clarity
- Added tag parsing method for HTML span element creation
- Implemented file content retrieval function and disk check functionality
2024-05-22 16:57:40 +02:00
67df2666a6
Refactor hash function for tag background color calculation
Changed the hashing algorithm used to calculate tag background colors in the Twig extension.
2024-05-22 11:25:18 +02:00
4b3c4f1755
Update HSP equation source link in color conversion method
The commit updates the source link for the HSP equation used in the color conversion method to a more accurate reference.
2024-05-22 11:24:51 +02:00
cd5a80b33b
Refactor command classes to use specific interfaces
- Updated command classes to use specific interfaces for Kernel, Doctrine, PasswordHasher, Templating, and Mailer.
2024-05-22 10:36:27 +02:00
8 changed files with 74 additions and 32 deletions

View File

@ -18,8 +18,8 @@ use Symfony\Component\HttpKernel\KernelInterface;
#[AsCommand(name: 'webcron:cleanup', description: 'Cleanup runs')]
class CleanupCommand extends Command
{
protected $kernel;
protected $doctrine;
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
{

View File

@ -16,8 +16,8 @@ use Symfony\Component\HttpKernel\KernelInterface;
#[AsCommand(name: 'webcron:daemon', description: 'The master script of Webcron Management')]
class DaemonCommand extends Command
{
protected $kernel;
protected $doctrine;
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
{

View File

@ -19,9 +19,9 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[AsCommand(name: 'webcron:demodata', description: 'Install demo data')]
class DemoInstallCommand extends Command
{
protected $kernel;
protected $doctrine;
protected $passwordHasher;
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
protected UserPasswordHasherInterface $passwordHasher;
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher)
{

View File

@ -23,10 +23,10 @@ use Twig\Environment;
#[AsCommand(name: 'webcron:mail-failed-runs', description: 'Sends email about failed runs')]
class MailFailedRunsCommand extends Command
{
protected $kernel;
protected $doctrine;
protected $templating;
protected $mailer;
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
protected Environment $templating;
protected MailerInterface $mailer;
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine, Environment $templating, MailerInterface $mailer)
{

View File

@ -14,8 +14,8 @@ use Symfony\Component\HttpKernel\KernelInterface;
#[AsCommand(name: 'webcron:run', description: 'Run a single cronjob')]
class RunCommand extends Command
{
protected $kernel;
protected $doctrine;
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
{

View File

@ -25,7 +25,6 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[AsCommand(name: 'webcron:user', description: 'User stuff')]
class UserCommand extends Command
{
protected static $defaultName = 'webcron:user';
protected KernelInterface $kernel;
protected ManagerRegistry $doctrine;
protected UserPasswordHasherInterface $passwordHasher;

View File

@ -5,7 +5,14 @@ namespace App\Service;
class Secret
{
static function encrypt($plaintext) {
/**
* Encrypt plaintext string based with password string
*
* @param $plaintext
* @return string
*/
static function encrypt($plaintext): string
{
$password = $_ENV['APP_SECRET'];
$method = $_ENV['ENCRYPTION_METHOD'];
$key = hash($_ENV['HASHING_METHOD'], $password, true);
@ -17,7 +24,14 @@ class Secret
return $iv . $hash . $ciphertext;
}
static function decrypt($ivHashCiphertext) {
/**
* Decrypt encrypted message
*
* @param $ivHashCiphertext
* @return string
*/
static function decrypt($ivHashCiphertext): string
{
$password = $_ENV['APP_SECRET'];
$method = $_ENV['ENCRYPTION_METHOD'];
$iv = substr($ivHashCiphertext, 0, 16);

View File

@ -8,7 +8,7 @@ use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
public function getFilters()
public function getFilters(): array
{
return [
new TwigFilter('interval', [$this, 'parseInterval']),
@ -18,14 +18,20 @@ class AppExtension extends AbstractExtension
];
}
public function getTests()
public function getTests(): array
{
return [
new TwigTest('ondisk', [$this, 'onDisk'])
];
}
function parseInterval(int|float $time)
/**
* Converts seconds to days, hours, minutes and seconds
*
* @param int|float $time
* @return string
*/
function parseInterval(int|float $time): string
{
$return = '';
@ -47,26 +53,37 @@ class AppExtension extends AbstractExtension
return (!empty($return)) ? trim($return) : '0.000s';
}
function parseTags(string $text)
/**
* Converts [tag] to a HTML span element with background color based on the md5 hash of the tag and text color based on whether background color is light or dark
* @param string $text
* @return string
*/
function parseTags(string $text): string
{
$results = [];
preg_match_all('/\[([A-Za-z0-9 \-]+)\]/', $text, $results);
foreach ($results[0] as $key=>$result) {
$background = substr(md5($results[0][$key]), 0, 6);
$color = $this->lightOrDark($background) == 'dark' ? 'ffffff' : '000000';
$background = substr(hash('murmur3a', $results[0][$key]), 0, 6);
$color = $this->isDark($background) ? 'ffffff' : '000000';
$text = str_replace($results[0][$key], '<span class="tag" data-background-color="#' . $background . '" data-color="#' . $color . '">' . $results[1][$key] . '</span>', $text);
}
return $text;
}
private function lightOrDark ($color)
/**
* Returns true if the color is considered to be dark
*
* @param string $color
* @return bool
*/
private function isDark(string $color): bool
{
$color = str_split($color, 2);
foreach($color as &$value) {
$value = hexdec($value);
}
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
// HSP equation from http://alienryderflex.com/hsp.html
$hsp = sqrt(
0.299 * ($color[0] * $color[0]) +
0.587 * ($color[1] * $color[1]) +
@ -75,24 +92,36 @@ class AppExtension extends AbstractExtension
// Using the HSP value, determine whether the color is light or dark
if ($hsp>140) {
return 'light';
} else {
return 'dark';
}
return ($hsp<150);
}
function decryptSecret(string $text)
/**
* Returns decrypted cipher text
*
* @param string $text
* @return string
*/
function decryptSecret(string $text): string
{
return Secret::decrypt(base64_decode($text));
}
function getContents(string $file)
/**
* Returns the content of a file
*
* @param string $file
* @return string
*/
function getContents(string $file): string
{
return file_get_contents($file);
}
public function onDisk(string $file)
/**
* @param string $file
* @return string
*/
public function onDisk(string $file): string
{
return file_exists($file);
}