Completed
Push — 1.0-security-fixes ( 577127 )
by Kamil
65:44 queued 42:31
created

HttpFoundationRequestHandler::isFileUpload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ResourceBundle\Form\Extension\HttpFoundation;
15
16
use Symfony\Component\Form\Exception\UnexpectedTypeException;
17
use Symfony\Component\Form\FormError;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\RequestHandlerInterface;
20
use Symfony\Component\Form\Util\ServerParams;
21
use Symfony\Component\HttpFoundation\File\File;
22
use Symfony\Component\HttpFoundation\Request;
23
24
/**
25
 * Does not compare the form's method with the request's method.
26
 * Always submits the form, even if there are no fields sent.
27
 *
28
 * @internal
29
 *
30
 * @see \Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler
31
 */
32
final class HttpFoundationRequestHandler implements RequestHandlerInterface
33
{
34
    /**
35
     * @var ServerParams
36
     */
37
    private $serverParams;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __construct(ServerParams $serverParams = null)
43
    {
44
        $this->serverParams = $serverParams ?: new ServerParams();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function handleRequest(FormInterface $form, $request = null)
51
    {
52
        if (!$request instanceof Request) {
53
            throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
54
        }
55
56
        $name = $form->getName();
57
        $method = $request->getMethod();
58
59
        // For request methods that must not have a request body we fetch data
60
        // from the query string. Otherwise we look for data in the request body.
61
        if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
62
            if ('' === $name) {
63
                $data = $request->query->all();
64
            } else {
65
                // Don't submit GET requests if the form's name does not exist
66
                // in the request
67
                if (!$request->query->has($name)) {
68
                    return;
69
                }
70
71
                $data = $request->query->get($name);
72
            }
73
        } else {
74
            // Mark the form with an error if the uploaded size was too large
75
            // This is done here and not in FormValidator because $_POST is
76
            // empty when that error occurs. Hence the form is never submitted.
77
            if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
78
                // Submit the form, but don't clear the default values
79
                $form->submit(null, false);
80
81
                $form->addError(new FormError(
82
                    call_user_func($form->getConfig()->getOption('upload_max_size_message')),
83
                    null,
84
                    ['{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()]
85
                ));
86
87
                return;
88
            }
89
90
            if ('' === $name) {
91
                $params = $request->request->all();
92
                $files = $request->files->all();
93
            } elseif ($request->request->has($name) || $request->files->has($name)) {
94
                $default = $form->getConfig()->getCompound() ? [] : null;
95
                $params = $request->request->get($name, $default);
96
                $files = $request->files->get($name, $default);
97
            } else {
98
                // Don't submit the form if it is not present in the request
99
                return;
100
            }
101
102
            if (is_array($params) && is_array($files)) {
103
                $data = array_replace_recursive($params, $files);
104
            } else {
105
                $data = $params ?: $files;
106
            }
107
        }
108
109
        $form->submit($data, 'PATCH' !== $method);
110
    }
111
112
    public function isFileUpload($data)
113
    {
114
        return $data instanceof File;
115
    }
116
}
117