Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Helpers/Utilities.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Helpers;
4
5
use Carbon\Carbon;
6
use Cron\CronExpression;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\ViewErrorBag;
9
10
class Utilities
11
{
12
/**
13
* Generates a random string and injects special characters into it, in addition to
14
* the randomness of the alphanumeric default response.
15
*/
16
public static function randomStringWithSpecialCharacters(int $length = 16): string
17
{
18
$string = str_random($length);
19
// Given a random string of characters, randomly loop through the characters and replace some
20
// with special characters to avoid issues with MySQL password requirements on some servers.
21
try {
22
for ($i = 0; $i < random_int(2, 6); ++$i) {
23
$character = ['!', '@', '=', '.', '+', '^'][random_int(0, 5)];
24
25
$string = substr_replace($string, $character, random_int(0, $length - 1), 1);
26
}
27
} catch (\Exception $exception) {
28
// Just log the error and hope for the best at this point.
29
Log::error($exception);
30
}
31
32
return $string;
33
}
34
35
/**
36
* Converts schedule cron data into a carbon object.
37
*
38
* @throws \Exception
39
*/
40
public static function getScheduleNextRunDate(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek): Carbon
41
{
42
return Carbon::instance((new CronExpression(
43
sprintf('%s %s %s %s %s', $minute, $hour, $dayOfMonth, $month, $dayOfWeek)
44
))->getNextRunDate());
45
}
46
47
public static function checked(string $name, mixed $default): string
48
{
49
$errors = session('errors');
50
51
if (isset($errors) && $errors instanceof ViewErrorBag && $errors->any()) {
52
return old($name) ? 'checked' : '';
53
}
54
55
return ($default) ? 'checked' : '';
56
}
57
}
58
59