2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-06-12 10:00:01 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-05-24 12:26:50 +00:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
use Illuminate\Support\Str;
|
2023-08-09 13:57:53 +00:00
|
|
|
use Nubs\RandomNameGenerator\All;
|
2023-08-10 13:52:54 +00:00
|
|
|
use Poliander\Cron\CronExpression;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-05-24 12:26:50 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function application_configuration_dir(): string
|
|
|
|
{
|
2023-08-09 12:44:36 +00:00
|
|
|
return '/data/coolify/applications';
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
|
|
|
function database_configuration_dir(): string
|
|
|
|
{
|
2023-08-09 12:44:36 +00:00
|
|
|
return '/data/coolify/databases';
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
|
|
|
function backup_dir(): string
|
|
|
|
{
|
2023-08-09 12:44:36 +00:00
|
|
|
return '/data/coolify/backups';
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
|
|
|
function generate_readme_file(string $name, string $updated_at): string
|
|
|
|
{
|
|
|
|
return "Resource name: $name\nLatest Deployment Date: $updated_at";
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 14:56:13 +00:00
|
|
|
function is_instance_admin()
|
|
|
|
{
|
|
|
|
return auth()->user()?->isInstanceAdmin();
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function general_error_handler(Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
|
|
|
try {
|
2023-08-09 13:57:53 +00:00
|
|
|
ray('ERROR OCCURRED: ' . $err->getMessage());
|
2023-06-09 13:55:21 +00:00
|
|
|
if ($err instanceof QueryException) {
|
|
|
|
if ($err->errorInfo[0] === '23505') {
|
2023-08-09 13:57:53 +00:00
|
|
|
throw new Exception($customErrorMessage ?? 'Duplicate entry found.', '23505');
|
2023-06-09 13:55:21 +00:00
|
|
|
} else if (count($err->errorInfo) === 4) {
|
2023-08-09 13:57:53 +00:00
|
|
|
throw new Exception($customErrorMessage ?? $err->errorInfo[3]);
|
2023-05-24 12:26:50 +00:00
|
|
|
} else {
|
2023-08-09 13:57:53 +00:00
|
|
|
throw new Exception($customErrorMessage ?? $err->errorInfo[2]);
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-09 13:57:53 +00:00
|
|
|
throw new Exception($customErrorMessage ?? $err->getMessage());
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
} catch (Throwable $error) {
|
2023-05-24 12:26:50 +00:00
|
|
|
if ($that) {
|
2023-06-09 13:55:21 +00:00
|
|
|
return $that->emit('error', $customErrorMessage ?? $error->getMessage());
|
2023-05-24 12:26:50 +00:00
|
|
|
} elseif ($isJson) {
|
|
|
|
return response()->json([
|
|
|
|
'code' => $error->getCode(),
|
|
|
|
'error' => $error->getMessage(),
|
|
|
|
]);
|
|
|
|
} else {
|
2023-06-09 13:55:21 +00:00
|
|
|
ray($customErrorMessage);
|
2023-05-30 13:52:17 +00:00
|
|
|
ray($error);
|
2023-08-09 13:57:53 +00:00
|
|
|
return $customErrorMessage ?? $error->getMessage();
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function get_route_parameters(): array
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
|
|
|
return Route::current()->parameters();
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function get_latest_version_of_coolify(): string
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-06-19 13:43:53 +00:00
|
|
|
try {
|
|
|
|
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
|
|
|
|
$versions = $response->json();
|
|
|
|
return data_get($versions, 'coolify.v4.version');
|
2023-08-09 13:57:53 +00:00
|
|
|
} catch (Throwable $th) {
|
2023-06-19 13:43:53 +00:00
|
|
|
//throw $th;
|
|
|
|
ray($th->getMessage());
|
|
|
|
return '0.0.0';
|
|
|
|
}
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function generate_random_name(): string
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-08-09 13:57:53 +00:00
|
|
|
$generator = All::create();
|
2023-05-24 12:26:50 +00:00
|
|
|
$cuid = new Cuid2(7);
|
2023-08-09 13:57:53 +00:00
|
|
|
return Str::kebab("{$generator->getName()}-$cuid");
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function generate_application_name(string $git_repository, string $git_branch): string
|
2023-05-24 13:47:04 +00:00
|
|
|
{
|
|
|
|
$cuid = new Cuid2(7);
|
2023-08-09 13:57:53 +00:00
|
|
|
return Str::kebab("$git_repository:$git_branch-$cuid");
|
2023-05-24 13:47:04 +00:00
|
|
|
}
|
2023-06-12 10:00:01 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function is_transactional_emails_active(): bool
|
2023-06-12 10:00:01 +00:00
|
|
|
{
|
2023-07-27 19:26:15 +00:00
|
|
|
return data_get(InstanceSettings::get(), 'smtp_enabled');
|
2023-06-12 10:00:01 +00:00
|
|
|
}
|
2023-06-12 14:39:48 +00:00
|
|
|
|
2023-08-15 12:11:38 +00:00
|
|
|
function set_transanctional_email_settings(InstanceSettings|null $settings = null): void
|
2023-06-12 14:39:48 +00:00
|
|
|
{
|
2023-08-15 12:11:38 +00:00
|
|
|
if (!$settings) {
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
}
|
2023-07-27 19:26:15 +00:00
|
|
|
$password = data_get($settings, 'smtp_password');
|
2023-08-09 13:57:53 +00:00
|
|
|
if (isset($password)) {
|
|
|
|
$password = decrypt($password);
|
|
|
|
}
|
2023-06-20 19:18:14 +00:00
|
|
|
|
2023-06-12 14:39:48 +00:00
|
|
|
config()->set('mail.default', 'smtp');
|
|
|
|
config()->set('mail.mailers.smtp', [
|
|
|
|
"transport" => "smtp",
|
2023-07-27 19:26:15 +00:00
|
|
|
"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 19:18:14 +00:00
|
|
|
"password" => $password,
|
2023-07-27 19:26:15 +00:00
|
|
|
"timeout" => data_get($settings, 'smtp_timeout'),
|
2023-06-12 14:39:48 +00:00
|
|
|
"local_domain" => null,
|
|
|
|
]);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function base_ip(): string
|
2023-06-16 11:37:02 +00:00
|
|
|
{
|
2023-08-09 13:57:53 +00:00
|
|
|
if (is_dev()) {
|
2023-07-28 11:40:47 +00:00
|
|
|
return "http://localhost";
|
|
|
|
}
|
2023-06-16 11:35:35 +00:00
|
|
|
$settings = InstanceSettings::get();
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv4";
|
2023-06-16 11:35:35 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
/**
|
|
|
|
* If fqdn is set, return it, otherwise return public ip.
|
|
|
|
*/
|
|
|
|
function base_url(bool $withPort = true): string
|
2023-06-13 13:01:11 +00:00
|
|
|
{
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
if ($settings->fqdn) {
|
|
|
|
return $settings->fqdn;
|
|
|
|
}
|
2023-06-14 07:05:13 +00:00
|
|
|
$port = config('app.port');
|
|
|
|
if ($settings->public_ipv4) {
|
2023-06-16 09:01:27 +00:00
|
|
|
if ($withPort) {
|
2023-08-09 13:57:53 +00:00
|
|
|
if (is_dev()) {
|
|
|
|
return "http://localhost:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv4:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
if (is_dev()) {
|
2023-06-16 09:01:27 +00:00
|
|
|
return "http://localhost";
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv4";
|
2023-06-14 07:05:13 +00:00
|
|
|
}
|
|
|
|
if ($settings->public_ipv6) {
|
2023-06-16 09:01:27 +00:00
|
|
|
if ($withPort) {
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv6:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv6";
|
2023-06-14 07:05:13 +00:00
|
|
|
}
|
|
|
|
return url('/');
|
2023-06-13 13:01:11 +00:00
|
|
|
}
|
2023-06-16 09:01:27 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function is_dev(): bool
|
2023-06-16 09:01:27 +00:00
|
|
|
{
|
|
|
|
return config('app.env') === 'local';
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function is_cloud(): bool
|
2023-07-13 13:07:42 +00:00
|
|
|
{
|
2023-07-14 10:09:56 +00:00
|
|
|
return !config('coolify.self_hosted');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
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;
|
|
|
|
}
|