|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands\Site\Tenant; |
| 4 | + |
| 5 | +use App\Commands\Command as BaseCommand; |
| 6 | +use App\Commands\Concerns\InteractWithServer; |
| 7 | +use App\Commands\Concerns\InteractWithSite; |
| 8 | +use App\Traits\EnsureHasToken; |
| 9 | +use App\Traits\HasPloiConfiguration; |
| 10 | +use function Laravel\Prompts\select; |
| 11 | +use function Laravel\Prompts\text; |
| 12 | + |
| 13 | +class RequestCertificateTenantCommand extends BaseCommand |
| 14 | +{ |
| 15 | + use EnsureHasToken, HasPloiConfiguration, InteractWithServer, InteractWithSite; |
| 16 | + |
| 17 | + protected $signature = 'tenant:request-certificate {--server=} {--site=} {--tenant= : The tenant to request a certificate for} {--webhook} {--force : Bypasses the Ploi DNS validation}'; |
| 18 | + |
| 19 | + protected $description = 'Request an SSL certificate for a tenant'; |
| 20 | + |
| 21 | + protected array $site; |
| 22 | + protected array $server; |
| 23 | + |
| 24 | + public function handle(): void |
| 25 | + { |
| 26 | + $this->ensureHasToken(); |
| 27 | + |
| 28 | + [$serverId, $siteId] = $this->getServerAndSite(); |
| 29 | + $this->site = $this->ploi->getSiteDetails($serverId, $siteId)['data']; |
| 30 | + $this->server = $this->ploi->getServerDetails($serverId)['data']; |
| 31 | + |
| 32 | + $tenants = $this->ploi->getTenants($serverId, $siteId)['data']; |
| 33 | + |
| 34 | + if (empty($tenants['tenants'])) { |
| 35 | + $this->warn("No tenants found for site {$tenants['main']}."); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $tenant = $this->option('tenant'); |
| 40 | + if (!$tenant) { |
| 41 | + $tenant = select( |
| 42 | + 'Select a tenant to request certificate for:', |
| 43 | + $tenants['tenants'] |
| 44 | + ); |
| 45 | + } elseif (!in_array($tenant, $tenants['tenants'])) { |
| 46 | + $this->error("Tenant '{$tenant}' not found."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $domains = text( |
| 51 | + label: 'Enter domains for the certificate (comma-separated)', |
| 52 | + placeholder: "www.{$tenant}, subdomain.{$tenant}", |
| 53 | + default: $tenant, |
| 54 | + validate: fn (string $value) => match (true) { |
| 55 | + strlen($value) <= 0 => 'Please enter at least one domain.', |
| 56 | + default => $this->validateDomains($value), |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + $params = [ |
| 61 | + 'domains' => str_replace(' ', '', $domains), |
| 62 | + 'force' => $this->option('force') ?? false, |
| 63 | + ]; |
| 64 | + |
| 65 | + if ($this->option('webhook')) { |
| 66 | + $webhook = text( |
| 67 | + label: 'Enter webhook URL', |
| 68 | + placeholder: 'https://example.com/webhook', |
| 69 | + validate: fn (string $value) => match (true) { |
| 70 | + strlen($value) <= 0 => 'Please enter a webhook URL.', |
| 71 | + !filter_var($value, FILTER_VALIDATE_URL) => 'Please enter a valid URL.', |
| 72 | + default => null, |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + $params['webhook'] = $webhook; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + $response = $this->ploi->requestCertificateTenant($serverId, $siteId, $tenant, $params); |
| 81 | + |
| 82 | + if (isset($response['data'][0]['message']) && str_contains($response['data'][0]['message'], "Let's Encrypt certificate request has been issued")) { |
| 83 | + $this->success("Certificate request initiated for tenant: {$tenant}"); |
| 84 | + $this->info("Domains included: " . $params['domains']); |
| 85 | + if (isset($params['webhook'])) { |
| 86 | + $this->info("Webhook will be triggered at: {$params['webhook']}"); |
| 87 | + } |
| 88 | + } else { |
| 89 | + $this->error("Failed to request certificate: " . ($response['data'][0]['message'] ?? 'Unknown error')); |
| 90 | + } |
| 91 | + } catch (\Exception $e) { |
| 92 | + $this->error("Error requesting certificate: {$e->getMessage()}"); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + function validateDomains(string $value): ?string { |
| 97 | + $domains = array_map('trim', explode(',', $value)); |
| 98 | + |
| 99 | + foreach ($domains as $domain) { |
| 100 | + if (!preg_match('/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/', $domain)) { |
| 101 | + return "Invalid domain format: '$domain'. Please use format like example.com"; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | +} |
0 commit comments