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/Http/Controllers/FunnelPageController.php
<?php

namespace App\Http\Controllers;

use App\Model\Funnel;
use App\Model\FunnelStep;
use App\Model\FunnelPageView;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class FunnelPageController extends Controller
{
    public function show(Request $request, $funnelSlug, $stepSlug = null)
    {
        $funnel = Funnel::where('slug', $funnelSlug)
            ->whereNotNull('published_at')
            ->firstOrFail();

        if ($stepSlug) {
            $step = FunnelStep::where('funnel_id', $funnel->id)
                ->where('slug', $stepSlug)
                ->where('is_published', true)
                ->firstOrFail();
        } else {
            $step = $funnel->getEntryStep();

            if (!$step) {
                abort(404);
            }
        }

        // Record page view
        $this->recordPageView($request, $funnel, $step);

        $content = $step->renderedContent();

        return view('funnel_page.show', [
            'funnel'  => $funnel,
            'step'    => $step,
            'content' => $content,
        ]);
    }

    private function recordPageView(Request $request, Funnel $funnel, FunnelStep $step): void
    {
        // Visitor ID — raw cookie (bypass Laravel encryption for reliability)
        $sessionId = $_COOKIE['funnel_sid'] ?? null;
        if (!$sessionId || !Str::isUuid($sessionId)) {
            $sessionId = Str::uuid()->toString();
            setcookie('funnel_sid', $sessionId, time() + 86400 * 30, '/', '', false, true);
        }

        $userAgent = $request->userAgent() ?? '';

        FunnelPageView::create([
            'funnel_id'      => $funnel->id,
            'funnel_step_id' => $step->id,
            'customer_id'    => $funnel->customer_id,
            'session_id'     => $sessionId,
            'ip_address'     => $request->ip(),
            'user_agent'     => Str::limit($userAgent, 497),
            'referrer'       => Str::limit($request->header('referer', ''), 497),
            'utm_source'     => $request->query('utm_source'),
            'utm_medium'     => $request->query('utm_medium'),
            'utm_campaign'   => $request->query('utm_campaign'),
            'country'        => null, // requires GeoIP — skip for now
            'device_type'    => $this->detectDeviceType($userAgent),
            'viewed_at'      => now(),
        ]);
    }

    private function detectDeviceType(string $userAgent): string
    {
        $ua = strtolower($userAgent);

        if (preg_match('/mobile|android.*mobile|iphone|ipod|blackberry|opera mini|iemobile/i', $ua)) {
            return 'mobile';
        }

        if (preg_match('/tablet|ipad|android(?!.*mobile)|kindle|silk/i', $ua)) {
            return 'tablet';
        }

        return 'desktop';
    }
}