2020-03-15 23:25:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Helpers;
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Cron\CronExpression;
|
2020-03-15 23:25:29 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-04-11 19:56:03 +00:00
|
|
|
use Illuminate\Support\ViewErrorBag;
|
2020-03-15 23:25:29 +00:00
|
|
|
|
|
|
|
class Utilities
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Generates a random string and injects special characters into it, in addition to
|
2022-10-14 16:59:20 +00:00
|
|
|
* the randomness of the alphanumeric default response.
|
2020-03-15 23:25:29 +00:00
|
|
|
*/
|
|
|
|
public static function randomStringWithSpecialCharacters(int $length = 16): string
|
|
|
|
{
|
|
|
|
$string = str_random($length);
|
|
|
|
// Given a random string of characters, randomly loop through the characters and replace some
|
|
|
|
// with special characters to avoid issues with MySQL password requirements on some servers.
|
|
|
|
try {
|
2021-01-23 20:33:34 +00:00
|
|
|
for ($i = 0; $i < random_int(2, 6); ++$i) {
|
2020-03-15 23:25:29 +00:00
|
|
|
$character = ['!', '@', '=', '.', '+', '^'][random_int(0, 5)];
|
|
|
|
|
|
|
|
$string = substr_replace($string, $character, random_int(0, $length - 1), 1);
|
|
|
|
}
|
2023-02-23 19:30:16 +00:00
|
|
|
} catch (\Exception $exception) {
|
2020-03-15 23:25:29 +00:00
|
|
|
// Just log the error and hope for the best at this point.
|
|
|
|
Log::error($exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2020-03-22 20:56:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts schedule cron data into a carbon object.
|
|
|
|
*
|
2022-10-14 16:59:20 +00:00
|
|
|
* @throws \Exception
|
2020-03-22 20:56:15 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public static function getScheduleNextRunDate(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek): Carbon
|
2020-03-22 20:56:15 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
return Carbon::instance((new CronExpression(
|
2021-01-17 03:07:39 +00:00
|
|
|
sprintf('%s %s %s %s %s', $minute, $hour, $dayOfMonth, $month, $dayOfWeek)
|
2022-10-14 16:59:20 +00:00
|
|
|
))->getNextRunDate());
|
2020-03-22 20:56:15 +00:00
|
|
|
}
|
2020-04-11 19:56:03 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public static function checked(string $name, mixed $default): string
|
2020-04-11 19:56:03 +00:00
|
|
|
{
|
|
|
|
$errors = session('errors');
|
|
|
|
|
|
|
|
if (isset($errors) && $errors instanceof ViewErrorBag && $errors->any()) {
|
|
|
|
return old($name) ? 'checked' : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($default) ? 'checked' : '';
|
|
|
|
}
|
2020-03-15 23:25:29 +00:00
|
|
|
}
|