PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.45
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.45
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Http / Request / FileHandler.php

FileHandler.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.45, at vendor/wpfluent/framework/src/WPFluent/Http/Request/FileHandler.php

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