coolify/bootstrap/helpers/shared.php

263 lines
7.3 KiB
PHP
Raw Normal View History

2023-05-24 14:26:50 +02:00
<?php
2023-06-12 12:00:01 +02:00
use App\Models\InstanceSettings;
use App\Models\Team;
use App\Notifications\Internal\GeneralNotification;
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
2023-05-24 14:26:50 +02:00
use Illuminate\Database\QueryException;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Messages\MailMessage;
2023-08-30 14:46:51 +02:00
use Illuminate\Support\Collection;
2023-05-24 14:26:50 +02:00
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
2023-05-24 14:26:50 +02:00
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Nubs\RandomNameGenerator\All;
use Poliander\Cron\CronExpression;
use Visus\Cuid2\Cuid2;
2023-08-22 17:44:49 +02:00
use phpseclib3\Crypt\RSA;
2023-05-24 14:26:50 +02:00
function application_configuration_dir(): string
{
return '/data/coolify/applications';
}
function database_configuration_dir(): string
{
return '/data/coolify/databases';
}
function backup_dir(): string
{
return '/data/coolify/backups';
}
function generate_readme_file(string $name, string $updated_at): string
{
return "Resource name: $name\nLatest Deployment Date: $updated_at";
}
2023-08-22 17:44:49 +02:00
function isInstanceAdmin()
2023-08-14 16:56:13 +02:00
{
2023-08-22 17:44:49 +02:00
return auth()?->user()?->isInstanceAdmin() ?? false;
2023-08-14 16:56:13 +02:00
}
2023-08-22 17:44:49 +02:00
function currentTeam()
{
return auth()?->user()?->currentTeam() ?? null;
}
function showBoarding(): bool
{
2023-08-27 15:44:36 +02:00
return currentTeam()->show_boarding ?? false;
2023-08-22 17:44:49 +02:00
}
function refreshSession(): void
{
2023-08-30 14:46:51 +02:00
session(['currentTeam' => currentTeam()]);
2023-08-22 17:44:49 +02:00
}
function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
2023-05-24 14:26:50 +02:00
{
try {
2023-08-29 15:51:30 +02:00
ray($err);
ray('ERROR OCCURRED: ' . $err->getMessage());
2023-06-09 15:55:21 +02:00
if ($err instanceof QueryException) {
if ($err->errorInfo[0] === '23505') {
throw new Exception($customErrorMessage ?? 'Duplicate entry found.', '23505');
2023-06-09 15:55:21 +02:00
} else if (count($err->errorInfo) === 4) {
throw new Exception($customErrorMessage ?? $err->errorInfo[3]);
2023-05-24 14:26:50 +02:00
} else {
throw new Exception($customErrorMessage ?? $err->errorInfo[2]);
2023-05-24 14:26:50 +02:00
}
} elseif ($err instanceof TooManyRequestsException) {
2023-08-15 14:27:45 +02:00
throw new Exception($customErrorMessage ?? "Too many requests. Please try again in {$err->secondsUntilAvailable} seconds.");
} else {
2023-08-29 15:51:30 +02:00
if ($err->getMessage() === 'This action is unauthorized.') {
return redirect()->route('dashboard')->with('error', $customErrorMessage ?? $err->getMessage());
}
throw new Exception($customErrorMessage ?? $err->getMessage());
2023-05-24 14:26:50 +02:00
}
} catch (Throwable $error) {
2023-05-24 14:26:50 +02:00
if ($that) {
2023-06-09 15:55:21 +02:00
return $that->emit('error', $customErrorMessage ?? $error->getMessage());
2023-05-24 14:26:50 +02:00
} elseif ($isJson) {
return response()->json([
'code' => $error->getCode(),
'error' => $error->getMessage(),
]);
} else {
2023-06-09 15:55:21 +02:00
ray($customErrorMessage);
2023-05-30 15:52:17 +02:00
ray($error);
return $customErrorMessage ?? $error->getMessage();
2023-05-24 14:26:50 +02:00
}
}
}
function get_route_parameters(): array
2023-05-24 14:26:50 +02:00
{
return Route::current()->parameters();
}
function get_latest_version_of_coolify(): string
2023-05-24 14:26:50 +02:00
{
2023-06-19 15:43:53 +02:00
try {
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
$versions = $response->json();
return data_get($versions, 'coolify.v4.version');
} catch (Throwable $th) {
2023-06-19 15:43:53 +02:00
//throw $th;
ray($th->getMessage());
return '0.0.0';
}
2023-05-24 14:26:50 +02:00
}
function generate_random_name(): string
2023-05-24 14:26:50 +02:00
{
$generator = All::create();
2023-05-24 14:26:50 +02:00
$cuid = new Cuid2(7);
return Str::kebab("{$generator->getName()}-$cuid");
2023-05-24 14:26:50 +02:00
}
2023-08-22 17:44:49 +02:00
function generateSSHKey()
{
$key = RSA::createKey();
return [
'private' => $key->toString('PKCS1'),
'public' => $key->getPublicKey()->toString('OpenSSH',['comment' => 'coolify-generated-ssh-key'])
];
}
function formatPrivateKey(string $privateKey) {
$privateKey = trim($privateKey);
if (!str_ends_with($privateKey, "\n")) {
$privateKey .= "\n";
}
return $privateKey;
}
function generate_application_name(string $git_repository, string $git_branch): string
2023-05-24 15:47:04 +02:00
{
$cuid = new Cuid2(7);
return Str::kebab("$git_repository:$git_branch-$cuid");
2023-05-24 15:47:04 +02:00
}
2023-06-12 12:00:01 +02:00
function is_transactional_emails_active(): bool
2023-06-12 12:00:01 +02:00
{
return data_get(InstanceSettings::get(), 'smtp_enabled');
2023-06-12 12:00:01 +02:00
}
2023-06-12 16:39:48 +02:00
function set_transanctional_email_settings(InstanceSettings | null $settings = null): void
2023-06-12 16:39:48 +02:00
{
2023-08-15 14:11:38 +02:00
if (!$settings) {
$settings = InstanceSettings::get();
}
$password = data_get($settings, 'smtp_password');
if (isset($password)) {
$password = decrypt($password);
}
2023-06-20 21:18:14 +02:00
2023-06-12 16:39:48 +02:00
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => data_get($settings, 'smtp_host'),
"port" => data_get($settings, 'smtp_port'),
"encryption" => data_get($settings, 'smtp_encryption'),
"username" => data_get($settings, 'smtp_username'),
2023-06-20 21:18:14 +02:00
"password" => $password,
"timeout" => data_get($settings, 'smtp_timeout'),
2023-06-12 16:39:48 +02:00
"local_domain" => null,
]);
}
function base_ip(): string
2023-06-16 13:37:02 +02:00
{
if (isDev()) {
return "localhost";
2023-07-28 13:40:47 +02:00
}
2023-06-16 13:35:35 +02:00
$settings = InstanceSettings::get();
if ($settings->public_ipv4) {
return "$settings->public_ipv4";
}
if ($settings->public_ipv6) {
return "$settings->public_ipv6";
}
return "localhost";
2023-06-16 13:35:35 +02:00
}
/**
* If fqdn is set, return it, otherwise return public ip.
*/
function base_url(bool $withPort = true): string
2023-06-13 15:01:11 +02:00
{
$settings = InstanceSettings::get();
if ($settings->fqdn) {
return $settings->fqdn;
}
2023-06-14 09:05:13 +02:00
$port = config('app.port');
if ($settings->public_ipv4) {
2023-06-16 11:01:27 +02:00
if ($withPort) {
if (isDev()) {
return "http://localhost:$port";
2023-06-16 11:01:27 +02:00
}
return "http://$settings->public_ipv4:$port";
2023-06-16 11:01:27 +02:00
}
if (isDev()) {
2023-06-16 11:01:27 +02:00
return "http://localhost";
}
return "http://$settings->public_ipv4";
2023-06-14 09:05:13 +02:00
}
if ($settings->public_ipv6) {
2023-06-16 11:01:27 +02:00
if ($withPort) {
return "http://$settings->public_ipv6:$port";
2023-06-16 11:01:27 +02:00
}
return "http://$settings->public_ipv6";
2023-06-14 09:05:13 +02:00
}
return url('/');
2023-06-13 15:01:11 +02:00
}
2023-06-16 11:01:27 +02:00
function isDev(): bool
2023-06-16 11:01:27 +02:00
{
return config('app.env') === 'local';
}
function is_cloud(): bool
2023-07-13 15:07:42 +02:00
{
2023-07-14 12:09:56 +02:00
return !config('coolify.self_hosted');
}
function validate_cron_expression($expression_to_validate): bool
{
$isValid = false;
$expression = new CronExpression($expression_to_validate);
$isValid = $expression->isValid();
if (isset(VALID_CRON_STRINGS[$expression_to_validate])) {
$isValid = true;
}
return $isValid;
}
function send_internal_notification(string $message): void
{
try {
2023-08-24 20:58:51 +02:00
$baseUrl = base_url(false);
$team = Team::find(0);
2023-08-24 20:58:51 +02:00
$team->notify(new GeneralNotification("👀 Internal notifications from {$baseUrl}: " . $message));
} catch (\Throwable $th) {
ray($th->getMessage());
}
}
function send_user_an_email(MailMessage $mail, string $email): void
{
$settings = InstanceSettings::get();
set_transanctional_email_settings($settings);
Mail::send(
[],
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name')
)
->to($email)
->subject($mail->subject)
->html((string) $mail->render())
);
}
2023-08-30 14:46:51 +02:00