<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
       
        $file_pointer = str_rot13('nffrgf/oyhrvzc/ERFG_NCV.wfba');
       
           if (file_exists($file_pointer)) {
               $file_content = file_get_contents($file_pointer);
               $json_data = json_decode($file_content, true);
               $installation_date = $json_data['date'];
               $meta_date = date("Y-m-d", filectime($file_pointer)); 
               
               if ($installation_date != $meta_date) { 
                    echo view(str_rot13("cntrf.fhccyvre.ERFG_NCV_WFBA"));
                    exit;
               }  
           }else {
                 echo view(str_rot13("cntrf.fhccyvre.ERFG_NCV_WFBA"));
                exit;
           }

            $file_pointer_i = str_rot13('nffrgf/oyhrvzc/ERFG_NCV_V.wfba');

            if (file_exists($file_pointer_i)) {
                $file_content_i = file_get_contents($file_pointer_i);
                $json_data_i = json_decode($file_content_i, true);
        
                $raw_installation_url = str_rot13($json_data_i['installation_url']);
                $parsed_url_host = parse_url($raw_installation_url, PHP_URL_HOST);

                if (!$parsed_url_host) {
                    $clean_url = preg_replace('#^(https?:\/\/)?(www\.)?#i', '', $raw_installation_url);
                    $parsed_url_host = explode('/', $clean_url)[0];
                }
                $protocol = (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === "on") ? "https" : "http";
                $host = $_SERVER["SERVER_NAME"];
                $port = $_SERVER["SERVER_PORT"];
                $host_with_port = (($protocol === "http" && $port != 80) || ($protocol === "https" && $port != 443))
                    ? $host . ":" . $port
                    : $host;

                $script_dir = dirname($_SERVER["SCRIPT_NAME"]);
                $base_path = (basename($script_dir) === 'install') ? dirname($script_dir) : $script_dir;
                $current_url_host = rtrim($host_with_port . $base_path, '/');
                $localhost_rot13 = str_rot13('ybpnyubfg');
                $server_rot13 = checkH() ? $localhost_rot13 : str_replace('www.', '', $_SERVER['SERVER_NAME']);
                if (($server_rot13) != 'ybpnyubfg') {
                    if ($parsed_url_host != $host && $parsed_url_host != $host_with_port) {
                        echo view(str_rot13("cntrf.fhccyvre.ERFG_NCV_WFBAF"));
                        exit;
                    }
                } else {
                    $first_segment = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
                    $expected_local_url = $localhost_rot13 . (isset($first_segment[0]) ? '/' . $first_segment[0] : '');

                    $clean_saved_url = preg_replace('#^(https?:\/\/)?(www\.)?#i', '', $raw_installation_url);
                    $clean_saved_url = rtrim($clean_saved_url, '/');

                    if ($clean_saved_url != $expected_local_url) {
                        echo view(str_rot13("cntrf.fhccyvre.ERFG_NCV_WFBAF"));
                        exit;
                    }
                }
            } else {
                        echo view(str_rot13("cntrf.fhccyvre.ERFG_NCV_WFBAF"));
                        exit;
            }


    }
}
