PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / vendor / wpfluent / framework / src / WPFluent / Request / FileHandler.php

FileHandler.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.2, at vendor/wpfluent/framework/src/WPFluent/Request/FileHandler.php

126 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\Request;
4 use FluentForm\Framework\Support\Helper;
5
6 trait FileHandler
7 {
8 /**
9 * Prepares HTTP files for Request
10 *
11 * @param array $files
12 *
13 * @return array
14 */
15 public function prepareFiles($files = [])
16 {
17 foreach ($files as $key => &$file) {
18 $file = $this->convertFileInformation($file);
19 }
20
21 return $files;
22 }
23
24 /**
25 * @taken from \Symfony\Component\HttpFoundation\FileBag
26 *
27 * Converts uploaded files to UploadedFile instances.
28 *
29 * @param array|File $file A (multi-dimensional) array of uploaded file information
30 *
31 * @return File[]|File|null A (multi-dimensional) array of File instances
32 */
33 protected function convertFileInformation($file)
34 {
35 $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
36
37 if ($file instanceof File) {
38 return $file;
39 }
40
41 $file = $this->fixPhpFilesArray($file);
42
43 if (is_array($file)) {
44 $keys = array_keys($file);
45 sort($keys);
46
47 if ($keys == $fileKeys) {
48 if (UPLOAD_ERR_NO_FILE == $file['error']) {
49 $file = null;
50 } else {
51 $file = new File($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
52 }
53 } else {
54 $file = array_map(array($this, 'convertFileInformation'), $file);
55 if (array_keys($keys) === $keys) {
56 $file = array_filter($file);
57 }
58 }
59 }
60
61 return $file;
62 }
63
64 /**
65 * @taken from \Symfony\Component\HttpFoundation\FileBag
66 *
67 * Fixes a malformed PHP $_FILES array.
68 *
69 * PHP has a bug that the format of the $_FILES array differs, depending on
70 * whether the uploaded file fields had normal field names or array-like
71 * field names ("normal" vs. "parent[child]").
72 *
73 * This method fixes the array to look like the "normal" $_FILES array.
74 *
75 * It's safe to pass an already converted array, in which case this method
76 * just returns the original array unmodified.
77 *
78 * @return array
79 */
80 protected function fixPhpFilesArray($data)
81 {
82 $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
83
84 if (! is_array($data)) {
85 return $data;
86 }
87
88 // Remove extra key added by PHP 8.1.
89 unset($data['full_path']);
90 $keys = array_keys($data);
91 sort($keys);
92
93 if ($fileKeys != $keys || ! isset($data['name']) || ! is_array($data['name'])) {
94 return $data;
95 }
96
97 $files = $data;
98 foreach ($fileKeys as $k) {
99 unset($files[$k]);
100 }
101
102 foreach ($data['name'] as $key => $name) {
103 $files[$key] = $this->fixPhpFilesArray(array(
104 'error' => $data['error'][$key],
105 'name' => $name,
106 'type' => $data['type'][$key],
107 'tmp_name' => $data['tmp_name'][$key],
108 'size' => $data['size'][$key],
109 ));
110 }
111
112 return $files;
113 }
114
115 /**
116 * Retrieve a file from the request.
117 *
118 * @param string|null $key
119 * @param mixed $default
120 * @return \FluentForm\Framework\Request\File|array|null
121 */
122 public function file($key = null, $default = null)
123 {
124 return Helper::dataGet($this->files(), $key, $default);
125 }
126 }