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/wingssprayer.komma.pro/app/Exceptions/Handler.php
<?php

namespace App\Exceptions;

use App\Mail\AdminError;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;


class Handler extends \Komma\KMS\Exceptions\Handler
{

    /**
     * A list of the exception types that should not be reported by mail.
     *
     * @var array
     */
    protected $excludeExceptionsFromMail = [
        AuthenticationException::class,
        AuthorizationException::class,
        TokenMismatchException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception $e
     * @return Exception
     * @throws Exception
     */
    public function report(Exception $e)
    {
        if (app()->bound('sentry') && $this->shouldReport($e)) {
            app('sentry')->captureException($e);
        }

        // Default send a report to the admin
        $sendErrorMail = false;

        // Check if this Exception has been marked as an excluded
        foreach ($this->excludeExceptionsFromMail as $mailException){
            if($e instanceof $mailException) $sendErrorMail = false;
        }

        if($sendErrorMail && ! app()->runningInConsole()) { $this->mailAdmin($e); } //Causes exceptions on its own. Like: "Class mailer not found". This hides the real exception

        if(is_a($e, \PDOException::class)) {
            if(app()->environment('production') && config('app.debug') == false) {
                Log::error('ExceptionHandler: A PDO Exception has occurred. Check database settings.');
                throw new Exception('A PDO Exception has occurred. Check database settings since they might be incorrect.');
            }
        }

        parent::report($e);
        return $e;
    }


    /**
     * Send mail to admin
     *
     * @param $exception
     * @return bool
     */
    private function mailAdmin($exception)
    {
        $code = 500;
        if (method_exists($exception, 'getStatusCode')) $code = $exception->getStatuscode();

        $requestUri = strtolower(request()->getRequestUri());

        // Check for mail exceptions like bots
        if($this->isMailException($code, $requestUri)) return false;

        $mail = (new AdminError(request(), $exception, $code))->useMailConfigFrom('exception_mail');

        //Send the email
        Mail::send($mail);
    }

    /**
     * Check if given uri is excluded from mailing to the admin
     *
     * @param $code
     * @param $requestUri
     * @return bool
     */
    private function isMailException($code, $requestUri){

        if ($code == 404) {

            // Check for strings to ignore
            foreach(Config::get('errorIgnore.404') as $string)
            {
                if(Str::contains($requestUri,$string)) return true;
            }
        }
        elseif ($code == 500) {
            /*Custom function for this code*/

            // Check for strings to ignore
            foreach(Config::get('errorIgnore.500') as $string)
            {
                if(Str::contains($requestUri,$string)) return true;
            }
        }

        // Check for strings to ignore
        foreach(Config::get('errorIgnore.all') as $string)
        {
            if(Str::contains($requestUri,$string)) return true;
        }

    }

    /**
     * Render the given HttpException.
     *
     * @param  HttpExceptionInterface  $e
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    protected function renderHttpException(HttpExceptionInterface $e)
    {
        $this->registerErrorViewPaths();

        // Check for custom error message
        $view = 'errors.'.$e->getStatusCode();
        if (!view()->exists($view)){

            // Give debug info
            if(app()->environment() !== 'production') debug('Exceptions\Handler@renderHttpException: Error ' . $e->getStatusCode() . ' not implemented');

            $view = 'errors.show';
        }

        return response()->view($view, [
            'errors' => new ViewErrorBag,
            'exception' => $e,
        ], $e->getStatusCode(), $e->getHeaders());
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Exception $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        if($e instanceof AuthorizationException) {
            return response()->view('kms.auth.unauthorized');
        }

        //Render the page
        return parent::render($request, $e);
    }

    /**
     * Render an exception to the console.
     *
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
     * @param  \Exception $e
     * @return void
     */
    public function renderForConsole($output, Exception $e)
    {
        if(app()->environment() !== 'production') $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
        parent::renderForConsole($output, $e);
    }


    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        if($request->segment(1) == 'kms') {
            return redirect()->guest(route('kms.login'));
        } else {
            return redirect()->guest(route('site.login'));
        }
    }
}