<?php

namespace App\Controllers;

use CodeIgniter\Exceptions\PageNotFoundException;

class Home extends BaseController
{
    public function index()
    {
        return view('index');
    }

    public function root($path = '')
    {

        if ($path === '') {
            throw PageNotFoundException::forPageNotFound();
        }

        // Disallow ".." (parent directory) and any backslashes.
        if (str_contains($path, '..') || str_contains($path, '\\')) {
            throw PageNotFoundException::forPageNotFound('Invalid path.');
        }

        $viewFile = APPPATH . 'Views/' . $path . '.php';
        $realViewPath = realpath($viewFile);
        $realViewsDir = realpath(APPPATH . 'Views');

        if ($realViewPath === false || !str_starts_with($realViewPath, $realViewsDir)) {
            throw PageNotFoundException::forPageNotFound();
        }

        return view($path);
    }
}
