<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests,
        DispatchesJobs,
        ValidatesRequests {
        ValidatesRequests::validate as validate_orig;
        ValidatesRequests::validateWithBag as validateWithBag_orig;
    }

    const BOT_USER_AGENTS = [
        // generic
        'crawler',
        // cli / scripting
        'httpie',
        'curl',
        'wget',
        'lwp-request',
        'python-requests',
        'python-urllib',
        'libwww',
        'go-http-client',
        // commercial
        'googlebot',
        'google (+',
        'bingbot',
        'slurp',
        'duckduckbot',
        'baiduspider',
        'yandexbot',
        'sogou',
        'exabot',
        'facebot',
        'ia_archiver',
        'linkdexbot',
        'gigabot',
        'adsbot',
        // misc
        'gigablast',
        'phpcrawl',
        'mj12bot',
        'simplepie',
        'sitelockspider',
        'scoutjet',
        'grub.org',
        'mastodon', // mastodon fetching previews
    ];

    // Hacks to allow recursive nesting of validations in string and array format

    public function makeValidator($data, $rules, $messages = array(), $customAttributes = array())
    {
        return \Validator::make($data, vali($rules), $messages, $customAttributes);
    }

    public function validate(Request $request, array $rules,
                             array $messages = [], array $customAttributes = [])
    {
        return objBag($this->validate_orig($request, vali($rules), $messages, $customAttributes));
    }

    public function validateWithBag($errorBag, Request $request, array $rules,
                                    array $messages = [], array $customAttributes = [])
    {
        return objBag($this->validateWithBag_orig($errorBag, $request, vali($rules),
            $messages, $customAttributes));
    }

    protected function backWithErrors($errors)
    {
        return back()->withInput()->withErrors($errors);
    }
}