FormbuilderEntryController::verifyReCaptcha()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
nc 1
nop 1
1
<?php
2
3
namespace MedianetDev\BackpackForm\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Facades\Mail;
8
use Illuminate\Support\Facades\Redirect;
9
use Illuminate\Support\Facades\Validator;
10
use MedianetDev\BackpackForm\Models\FormbuilderEntry;
11
use MedianetDev\BackpackForm\Mail\MailFormBuilderEntry;
12
use MedianetDev\BackpackForm\Http\Requests\FormbuilderEntryRequest;
13
14
class FormbuilderEntryController extends Controller
15
{
16
    /**
17
     * Show an entry.
18
     *
19
     * @param  int  $id
20
     * @return \Illuminate\View\View
21
     */
22
    public function show($id)
23
    {
24
        // return view('user.profile', [
25
        //     'user' => User::findOrFail($id)
26
        // ]);
27
    }
28
29
    /**
30
     * Store a new entry.
31
     *
32
     * @param  Illuminate\Support\Facades\Redirect $request
0 ignored issues
show
Bug introduced by
The type MedianetDev\BackpackForm...upport\Facades\Redirect was not found. Did you mean Illuminate\Support\Facades\Redirect? If so, make sure to prefix the type with \.
Loading history...
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function store(Request $request)
36
    {
37
        $fbeRequest = new FormbuilderEntryRequest();
38
        $validator = Validator::make($request->all(), $fbeRequest->rules(), $fbeRequest->messages());
39
40
        if ($validator->fails()) {
41
            return Redirect::back()->withErrors($validator)->withInput();
42
        }
43
        $orginalForm = config('backpack-form.model_form')::where('uniq_id', $request->input('original-form'))->first();
44
        if (empty($orginalForm)) {
45
            return Redirect::back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.form_not_found')])->withInput();
46
        }
47
        // validate Google ReCaptcha V3
48
        if (!empty(config('backpack-form.captcha_v3_site_key')) && !empty(config('backpack-form.captcha_v3_secret_key')) && !empty($orginalForm->display_captcha) && $request->has('g-recaptcha-response')) {
49
            if(!$this->verifyReCaptcha($request->input('g-recaptcha-response'))) {
50
                return Redirect::back()->withErrors(['message' => __('medianet-dev.backpack-form::formbuilder.validations.captcha_invalid')])->withInput();
51
            }
52
        }
53
        // Save entry in database
54
        if ($orginalForm->in_database) {
55
56
            $error = false;
57
            $fieldWithData = [];
58
            $fields = json_decode($orginalForm->form);
59
            foreach ($fields as $field) {
60
                // Escape no input form entry
61
                if(isset($field->name)) {
62
                    $field->userData = [$request->input($field->name)];
63
                    $fieldWithData[] = $field;
64
                }
65
            }
66
            if ($error === true) {
0 ignored issues
show
introduced by
The condition $error === true is always false.
Loading history...
67
                // @TODO: This redirect it's for custom validation
68
                return Redirect::back()->with(['dataError' => json_encode($fieldWithData)])->withInput();
69
            }
70
            $newForm = new FormbuilderEntry([
71
                'structure_form'   => $orginalForm->form,
72
                'structure_result' => json_encode($fieldWithData),
73
                'fb_form_id'       => $orginalForm->id,
74
            ]);
75
            $newForm->save();
76
        }
77
        $data_admin = [];
78
        if ($orginalForm->copy_user) {
79
            $data_user  = [];
80
            $data_user['content_form'] = json_decode($newForm->structure_result);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
81
            $data_user['form_entry']   = $newForm;
82
            $data_user['orginal_form'] = $orginalForm;
83
            $mail_to = collect($data_user['content_form'])->where('name', $orginalForm->field_mail_name)->first();
84
85
            if(empty($mail_to) || empty($mail_to->userData[0]) || (!empty($mail_to->userData[0]) && !filter_var($mail_to->userData[0], FILTER_VALIDATE_EMAIL))) {
86
                $data_admin['error_mail_user'] = __('medianet-dev.backpack-form::formbuilder.emails.error_mail_user');
87
            } else {
88
                Mail::to($mail_to->userData[0])->send(new MailFormBuilderEntry('medianet.backpack-form::emails.user_template', $data_user));
89
                // return (new MailFormBuilderEntry('medianet.backpack-form::emails.user_template', $data_user));
90
            }
91
        }
92
        if ($orginalForm->by_mail) {
93
            $data_admin['content_form'] = json_decode($newForm->structure_result);
94
            $data_admin['form_entry']   = $newForm;
95
            $data_admin['orginal_form'] = $orginalForm;
96
            $mail_to = (!empty($orginalForm->mail_to)) ? array_filter(explode(',', $orginalForm->mail_to)) : config('backpack-form.email.to');
97
            Mail::to($mail_to)->send(new MailFormBuilderEntry('medianet.backpack-form::emails.entry_template', $data_admin));
98
            // return (new MailFormBuilderEntry('medianet.backpack-form::emails.entry_template', $data_admin));
99
        }
100
101
        return Redirect::back()->with(['success' => __('medianet-dev.backpack-form::formbuilder.validations.success_db')]);
102
    }
103
104
    /**
105
     * Verify if tokken captcha are good by google
106
     *
107
     * @param string $recaptchaCode the token in request input form
108
     * @return true/false
0 ignored issues
show
Documentation Bug introduced by
The doc comment true/false at position 0 could not be parsed: Unknown type name 'true/false' at position 0 in true/false.
Loading history...
109
     */
110
    private function verifyReCaptcha(string $recaptchaCode){
111
        $postdata = http_build_query(["secret" => config('backpack-form.captcha_v3_secret_key'),"response" => $recaptchaCode]);
112
        $opts = ['http' =>
113
            [
114
                'method'  => 'POST',
115
                'header'  => 'Content-type: application/x-www-form-urlencoded',
116
                'content' => $postdata
117
            ]
118
        ];
119
        $context = stream_context_create($opts);
120
        $result  = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
121
        $check   = json_decode($result);
122
        return $check->success;
123
    }
124
}
125