HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/app/Model/FunnelDomain.php
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Library\Traits\HasUid;

class FunnelDomain extends Model
{
    use HasUid;

    public const STATUS_PENDING  = 'pending';
    public const STATUS_VERIFIED = 'verified';
    public const STATUS_FAILED   = 'failed';

    protected $fillable = [
        'domain', 'status', 'verification_token', 'ssl_status',
    ];

    protected $casts = [
        'verified_at' => 'datetime',
    ];

    // -------------------------------------------------------------------------
    // Relationships
    // -------------------------------------------------------------------------

    public function funnel()
    {
        return $this->belongsTo(Funnel::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    // -------------------------------------------------------------------------
    // Status helpers
    // -------------------------------------------------------------------------

    public function isVerified(): bool
    {
        return $this->status === self::STATUS_VERIFIED;
    }

    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function isFailed(): bool
    {
        return $this->status === self::STATUS_FAILED;
    }

    // -------------------------------------------------------------------------
    // Verification
    // -------------------------------------------------------------------------

    public function generateVerificationToken(): string
    {
        $this->verification_token = 'funnel-verify-' . bin2hex(random_bytes(16));
        $this->save();

        return $this->verification_token;
    }

    public function verify(): bool
    {
        $records = dns_get_record($this->domain, DNS_CNAME);

        if (empty($records)) {
            $this->status = self::STATUS_FAILED;
            $this->save();

            return false;
        }

        // Check if any CNAME points to our expected host
        $expectedHost = parse_url(config('app.url'), PHP_URL_HOST);

        foreach ($records as $record) {
            if (isset($record['target']) && rtrim($record['target'], '.') === $expectedHost) {
                $this->status = self::STATUS_VERIFIED;
                $this->verified_at = now();
                $this->save();

                return true;
            }
        }

        $this->status = self::STATUS_FAILED;
        $this->save();

        return false;
    }
}