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/Services/UrlService.php
<?php

namespace App\Services;

use App\Library\StringHelper;
use App\Model\TrackingDomain;
use App\Library\Storage\AssetPath;

class UrlService
{
    public function transformHtmlUrls($html, $messageId, ?TrackingDomain $domain = null, $trackLink = true)
    {
        $html = StringHelper::replaceUrl($html, function ($element, $url) use ($messageId, $domain, $trackLink) {
            $host = parse_url($url, PHP_URL_HOST);

            // Task 1 - if Relative, prepend App URL
            if (is_null($host)) { // Case $host == false was already handled in the replaceUrl() method
                $url = $this->prependAppUrl($url);
            }

            // Task 2 - convert normal link to trackable link
            //          @important for A tag only
            if ($trackLink && $element->tagName == 'a') {
                $url = $this->makeLinksTrackable($url, $messageId);
            }

            // Apply tracking domain
            if ($domain) {
                $url = $this->rewriteUrlsWithTrackingDomain($url, $domain);
            }

            // RETURN is important
            return $url;
        });

        return $html;
    }

    public function prependAppUrl($url)
    {
        return join_url($this->getAppUrl(), $url);
    }

    private function makeLinksTrackable($url, $messageId)
    {
        return $this->makeAppTrackableUrl($url, $messageId);
    }

    public function rewriteUrlsWithTrackingDomain($url, TrackingDomain $domain)
    {
        return $domain->generateTrackingUrl($url);
    }

    public function getAppUrl()
    {
        $fullUrl = config('app.url');
        $meta = parse_url($fullUrl);

        if (!array_key_exists('scheme', $meta) || !array_key_exists('host', $meta)) {
            throw new \Exception("Invalid app.url setting {$fullUrl}");
        }

        return $fullUrl;
    }

    public function makeAppTrackableUrl($url, $messageId)
    {
        $newUrl = route('clickTrackingUrl', [
            'url' => StringHelper::base64UrlEncode($url),
            'message_id' => (empty($messageId)) ? null : StringHelper::base64UrlEncode($messageId),
        ], true);

        return $newUrl;
    }

    public function getAppSubdirectory()
    {
        // IMPORTANT: do not use url('/') as it will not work correctly
        // when calling from another file (like filemanager/config/config.php for example)
        // Otherwise, it will always return 'http://localhost' --> without subdirectory
        $path = parse_url(config('app.url'), PHP_URL_PATH);

        if (is_null($path)) {
            return null;
        }

        $path = trim($path, '/');
        return empty($path) ? null : $path;
    }

    public function generateEmailWebViewUrl($messageId, $withHost = true)
    {
        return route('webViewerUrl', ['message_id' => StringHelper::base64UrlEncode($messageId)], $withHost);
    }

    public function makePublicUrl(AssetPath $path)
    {
        $dirname = $path->getPath();
        $encodedDirname = StringHelper::base64UrlEncode($dirname);
        $filename = $path->getFilename();
        return route('public_asset', [ 'dirname' => $encodedDirname, 'basename' => $filename ]);
    }

    public function generateThemeAssetUrl(string $theme, ?string $path = null)
    {
        // @important: if path is empty, then it becomes URL PREFIX
        $b64Theme = \App\Library\StringHelper::base64UrlEncode($theme);
        return route('theme_assets', [
            'theme' => $b64Theme,
            'path' => $path,
        ], $withHost = false);
    }
}