HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/Http/Controllers/Controller.php
<?php

namespace App\Http\Controllers;

use App\KommaApp\Images\ImageService;
use App\KommaApp\Languages\LanguageService;
use App\KommaApp\Pages\PageService;
use App\KommaApp\Routes\Models\Route;
use App\KommaApp\Sites\Models\Site;
use App\KommaApp\Sites\SiteServiceInterface;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Session;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $languageService;
    protected $pageService;
    protected $links;

    protected $baseViewPath = 'site.';

    /** @var SiteServiceInterface */
    private $siteService;

    /** @var Site  */
    protected $site;

    public function __construct()
    {
        if(\App::runningInConsole()) return;

        $this->preventRestRouteFromWorking();
        $this->languageService = new LanguageService();
        $this->pageService = new PageService();

        $this->links = $this->pageService->getAllTranslatedPageRoutes();
        \View::share('links', $this->links);

        $this->siteService = \App::make(SiteServiceInterface::class);
        $this->site = $this->siteService->getCurrentSite();
    }

    /*
     * This function should only be temporary until dynamic blocks is rewrite to it's own blocks
     * instead of using json
     */
    protected function decodeDynamicContent($translation, string $key = 'description')
    {
        // If empty return
        if(empty($translation->{$key})){
            $translation->{$key} = [];
            return $translation;
        }

        $decodedJson = json_decode($translation->{$key});

        $dynamicBlocks = [];
        $dynamicGroups = [];

        $imageController = new ImageService();

        foreach ($decodedJson as $dynamicElement){

            if(isset($dynamicElement->fileIds)) {
                $images = $imageController->getDynamicBlockImages( $dynamicElement->fileIds );
                $dynamicElement->images = $images;
            }

            // Order dynamic blocks
            if($dynamicElement->code_name != '' ){ // If dynamic block has code name then add it to the translation
                $translation->{$dynamicElement->code_name} = $dynamicElement;
            } elseif($dynamicElement->view != ''){ // If dynamic block has view then add it to dynamic groups
                if( !isset($dynamicGroups[$dynamicElement->view])) $dynamicGroups[$dynamicElement->view] = [];
                $dynamicGroups[$dynamicElement->view][] = $dynamicElement;
            } else{ // Else just add them to the default dynamic content
                $dynamicBlocks[] = $dynamicElement;
            }
        }

        $translation->groups = $dynamicGroups;
        $translation->{$key} = $dynamicBlocks;

        return $translation;
    }

    protected function populateFormWithUser(){

        if( \Auth::guard('siteUser')->check() && !session('_old_input') ){

            $user = \Auth::guard('siteUser')->user();
            $company = $user->getCompany();


            session([
                '_old_input.company' =>  $company->name,
                '_old_input.first_name' => $user->first_name,
                '_old_input.last_name' => $user->last_name,
                '_old_input.email' => $user->email,
                '_old_input.address' => $company->invoice_address,
                '_old_input.postal' => $company->invoice_postal,
                '_old_input.city' => $company->invoice_city,
                '_old_input.site' => $company->site_url,
                '_old_input.filled' => 'filled',

                // We add this to mark that the old input should be flash data
                '_flash.old' => '_old_input'
            ]);

            // Check if there is an other invoice email as company email then fill the invoice email
            if($user->email !== $company->invoice_email) session([
                '_old_input.other_invoice_email' => $company->invoice_email,
            ]);

        }
    }

    /**
     * This method will prevent that REST routes are working with the application
     * They should be resolved by the middleware
     *
     */
    private function preventRestRouteFromWorking(){
        if (\App::runningInConsole()) return;

        $request = request();


        // Check if the route is a resolvable rest route
        if(in_array($request->path(), config('resolvableRestRoutes'))) return;
        if(str_start($request->path(), 'courses/')) return;

        // Normally it should be resolved or it's a hard defined route
        // Example. contact/process
        if(isset($request->resolved) && $request->resolved) return;
        else{
            // Get segments and path
            $path = \Request::path();
            $segments = \Request::segments();

            // Check if current path is a restfull path, or the first segment of it is
            $restRoutes = Route::whereIn('route', [$path, $segments[0]])
                ->get();

            // Rest routes should be empty, or it already should be resolved
            if($restRoutes->count() == 0) return;
            else{
                // If production throw 404
                if(\App::environment() == 'production') abort(404);

                // Else Argument Exception with explanation
                else throw new \InvalidArgumentException("Route path or the first segment is listed as an REST route");
            }
        }
    }
}