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/ai.naniguide.com/app/Model/Website.php
<?php

namespace App\Model;

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

use function App\Helpers\url_get_contents_ssl_safe;

class Website extends Model
{
    use HasFactory;
    use HasUid;

    public const STATUS_INACTIVE = 'inactive';
    public const STATUS_CONNECTED = 'connected';

    protected $fillable = [
        'url',
    ];

    public function customer()
    {
        return $this->belongsTo('App\Model\Customer');
    }

    public static function newDefault($customer)
    {
        $website = new self();
        $website->title = trans('messages.website.untitled');
        $website->status = self::STATUS_INACTIVE;
        $website->customer_id = $customer->id;

        return $website;
    }

    public function createFromArray($params)
    {
        $validator = \Validator::make($params, [
            'url' => 'required|url|active_url',
        ], [
            'url.url'        => 'The URL format is invalid (e.g. https://www.example.com).',
            'url.active_url' => 'The URL could not be reached. Please check that the domain exists and is publicly accessible.',
        ]);

        if ($validator->fails()) {
            return $validator;
        }

        // connect and get title
        try {
            $this->title = self::getTitleFromUrl($params['url']);
        } catch (\Exception $e) {
            $validator->after(function ($validator) use ($e) {
                $validator->errors()->add('url', $e->getMessage());
            });
        }

        if ($validator->fails()) {
            return $validator;
        }

        $this->fill($params);
        $this->save();

        return $validator;
    }

    public static function getTitleFromUrl($url)
    {
        $content = url_get_contents_ssl_safe($url);
        $emptyTitle = 'N/A';
        if (empty($content)) {
            // empty
            return $emptyTitle;
        }

        $document = new \DOMDocument();
        $document->encoding = 'utf-8';
        $document->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOWARNING | LIBXML_NOERROR);

        $tags = $document->getElementsByTagName('title');

        if (count($tags) == 0) {
            // throw new \Exception(trans('messages.website.title.not_found'));
            return $url;
        }

        return $tags[0]->textContent ?: $emptyTitle;
    }

    public function scopeSearch($query, $keyword)
    {
        // Keyword
        if (!empty($keyword)) {
            $query = $query->where('title', 'like', '%'.trim($keyword).'%');
        }
    }

    public function check()
    {
        $content = url_get_contents_ssl_safe($this->url);
        if (empty($content)) {
            throw new \Exception(trans('messages.website.empty_site_content'));
        }

        $document = new \DOMDocument();
        $document->encoding = 'utf-8';
        $document->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOWARNING | LIBXML_NOERROR);

        $tag = $document->getElementById('ACXConnectScript');

        if (!$tag) {
            throw new \Exception(trans('messages.website.can_not_find_connect_script', [
                'url' => $this->url,
            ]));
        }
    }

    public function connect()
    {
        $this->check();
        $this->setConnected();
    }

    public function setConnected()
    {
        $this->status = self::STATUS_CONNECTED;
        $this->save();
    }

    public function disconnect()
    {
        $this->status = self::STATUS_INACTIVE;
        $this->save();
    }

    public function isActive()
    {
        return $this->status == self::STATUS_CONNECTED;
    }

    public function connectedForms()
    {
        return \App\Model\Form::byWebsite($this);
    }

    public static function scopeConnected($query)
    {
        $query = $query->where('status', '=', self::STATUS_CONNECTED);
    }
}