mivita/app/Http/Controllers/Web/ContactController.php
Kevin Adametz 3711fcc8d0 01 2020
2020-02-14 10:18:44 +01:00

106 lines
No EOL
2.9 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Mail\MailContact;
use GuzzleHttp\Client;
use Input;
use Illuminate\Support\Facades\Mail;
use App\Services\Util;
use Validator;
class ContactController extends Controller
{
private $GOOGLE_ReCAPTCHA_KEY = "6LeeZosUAAAAAG907fMMqO4BFgsiR4ANDodd8FlU";
private $GOOGLE_ReCAPTCHA_SECRET = "6LeeZosUAAAAADIy2fyR4RG3EuM-Zdz7Pa2Qmb1J";
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
public function create()
{
$data = [
'GOOGLE_ReCAPTCHA_KEY' => $this->GOOGLE_ReCAPTCHA_KEY,
'user_shop' => Util::getUserShop(),
];
return view('web.templates.kontakt', $data);
}
public function store()
{
$user_shop = Util::getUserShop();
$rules = array(
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required|email',
'message'=>'required',
'g-recaptcha-response'=>'required|recaptcha',
'accepted_data_protection' => 'required',
);
Validator::extend('recaptcha', function($attribute, $value, $parameters, $validator) {
return $this->reCaptcha_validate($attribute, $value, $parameters, $validator);
});
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withErrors($validator)->withInput(Input::all());
}
$contact = [];
$contact['first_name'] = Input::get('first_name');
$contact['last_name'] = Input::get('last_name');
$contact['email'] = Input::get('email');
$contact['phone'] = Input::get('phone');
$contact['subject'] = Input::get('subject');
$contact['message'] = Input::get('message');
$checkout_mail = config('app.checkout_mail');
if($user_shop){
Mail::to($contact['email'])->bcc([$user_shop->user->email, $checkout_mail])->send(new MailContact($contact));
}else{
Mail::to($contact['email'])->bcc($checkout_mail)->send(new MailContact($contact));
}
$data = [
'user_shop' => Util::getUserShop(),
];
return view('web.templates.contact-final', $data);
}
private function reCaptcha_validate($attribute, $value, $parameters, $validator)
{
$client = new Client();
$response = $client->post(
'https://www.google.com/recaptcha/api/siteverify',
['form_params' =>
[
'secret' => $this->GOOGLE_ReCAPTCHA_SECRET,
'response' => $value
]
]
);
$body = json_decode((string)$response->getBody());
return $body->success;
}
}