This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Oro\Component\Config\Loader; |
||
4 | |||
5 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
6 | use Symfony\Component\PropertyAccess\PropertyAccessor; |
||
7 | |||
8 | use Oro\Component\Config\CumulativeResource; |
||
9 | use Oro\Component\Config\CumulativeResourceInfo; |
||
10 | |||
11 | /** |
||
12 | * Loader that returns folder content as a list of found files, works recursively as deep |
||
13 | * as it's configured by $maxNestingLevel param. There are two possible scenarios |
||
14 | * how it organizes data loaded: plain and nested, this configured by $plainResultStructure param. |
||
15 | * It should be used when need to trace directory structure/content updates |
||
16 | * (including adding new file or removing previously found), but skip file modification. |
||
17 | * |
||
18 | * Examples: |
||
19 | * Plain mode |
||
20 | * Directory structure: |
||
21 | * relative path folder/ |
||
22 | * file1.yml |
||
23 | * foo/ |
||
24 | * file2.yml |
||
25 | * bar/ |
||
26 | * file3.yml |
||
27 | * Loaded result: |
||
28 | * [ |
||
29 | * 'relative path folder/file1.yml' |
||
30 | * 'relative path folder/foo/file2.yml' |
||
31 | * 'relative path folder/foo/bar/file2.yml' |
||
32 | * ] |
||
33 | * Nested mode |
||
34 | * Directory structure: |
||
35 | * relative path folder/ |
||
36 | * file1.yml |
||
37 | * foo/ |
||
38 | * file2.yml |
||
39 | * bar/ |
||
40 | * file3.yml |
||
41 | * Loaded result: |
||
42 | * [ |
||
43 | * 0 => 'relative path folder/file1.yml', |
||
44 | * 'foo' => [ |
||
45 | * 0 => 'relative path folder/foo/file2.yml', |
||
46 | * 'bar' => [ |
||
47 | * 'relative path folder/foo/bar/file2.yml' |
||
48 | * ] |
||
49 | * ] |
||
50 | * ] |
||
51 | */ |
||
52 | class FolderContentCumulativeLoader implements CumulativeResourceLoader |
||
53 | { |
||
54 | /** @var string */ |
||
55 | protected $relativeFolderPath; |
||
56 | |||
57 | /** @var string int */ |
||
58 | protected $maxNestingLevel; |
||
59 | |||
60 | /** @var bool */ |
||
61 | protected $plainResultStructure; |
||
62 | |||
63 | /** @var string[] */ |
||
64 | protected $fileExtensions; |
||
65 | |||
66 | /** @var PropertyAccess */ |
||
67 | protected $propertyAccessor; |
||
68 | |||
69 | /** |
||
70 | * @param string $relativeFolderPath |
||
71 | * @param int $maxNestingLevel Pass -1 to unlimit, if you want to find files in exact path given pass 1 |
||
72 | * @param bool $plainResultStructure Indicates whether result should be returned as flat array |
||
73 | * or should be nested tree depends on file position in directory hierarchy |
||
74 | * @param string[] $fileExtensions The extensions of files to be scanned |
||
75 | */ |
||
76 | public function __construct( |
||
77 | $relativeFolderPath, |
||
78 | $maxNestingLevel = -1, |
||
79 | $plainResultStructure = true, |
||
80 | array $fileExtensions = [] |
||
81 | ) { |
||
82 | $this->relativeFolderPath = $relativeFolderPath; |
||
83 | $this->maxNestingLevel = $maxNestingLevel === -1 ? $maxNestingLevel : --$maxNestingLevel; |
||
84 | $this->plainResultStructure = $plainResultStructure; |
||
85 | $this->fileExtensions = $fileExtensions; |
||
86 | $this->resource = 'Folder contents: ' . $relativeFolderPath; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function serialize() |
||
93 | { |
||
94 | return serialize( |
||
95 | [ |
||
96 | $this->relativeFolderPath, |
||
97 | $this->maxNestingLevel, |
||
98 | $this->plainResultStructure, |
||
99 | $this->fileExtensions |
||
100 | ] |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function unserialize($serialized) |
||
108 | { |
||
109 | list( |
||
110 | $this->relativeFolderPath, |
||
111 | $this->maxNestingLevel, |
||
112 | $this->plainResultStructure, |
||
113 | $this->fileExtensions |
||
114 | ) = unserialize($serialized); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getResource() |
||
121 | { |
||
122 | return $this->resource; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function load($bundleClass, $bundleDir, $bundleAppDir = '') |
||
129 | { |
||
130 | $dir = $this->getDirectoryAbsolutePath($bundleDir); |
||
131 | $bundleAppData = []; |
||
132 | |||
133 | if (is_dir($bundleAppDir)) { |
||
134 | $appDir = $this->getResourcesDirectoryAbsolutePath($bundleAppDir); |
||
135 | $bundleAppData = $this->getData($appDir); |
||
136 | } |
||
137 | |||
138 | $data = $this->mergeArray($bundleAppData, $this->getData($dir), $bundleAppDir, $bundleDir); |
||
139 | if (empty($data)) { |
||
140 | return null; |
||
141 | } |
||
142 | |||
143 | return new CumulativeResourceInfo($bundleClass, $this->getResource(), realpath($dir), $data); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Recursively merges two arrays into one. If files have the same location, |
||
148 | * the priority remains for the array $a |
||
149 | * |
||
150 | * @param array $a Array to merge |
||
151 | * @param array $b Array, which merges with the previous one |
||
152 | * @param string $bundleAppDir The bundle directory inside the application resources directory |
||
153 | * @param string $bundleDir The bundle root directory |
||
154 | * |
||
155 | * @return array Merged array |
||
156 | */ |
||
157 | protected function mergeArray(array $a, array $b, $bundleAppDir, $bundleDir) |
||
158 | { |
||
159 | foreach ($b as $k => $v) { |
||
160 | if (is_int($k)) { |
||
161 | foreach ($a as $val) { |
||
162 | if ($this->isFilePathEquals($val, $v, $bundleAppDir, $bundleDir)) { |
||
163 | continue 2; |
||
164 | } |
||
165 | } |
||
166 | $a[] = $v; |
||
167 | } else { |
||
168 | if (is_array($v) && isset($a[$k]) && is_array($a[$k])) { |
||
169 | $a[$k] = $this->mergeArray($a[$k], $v, $bundleAppDir, $bundleDir); |
||
170 | } else { |
||
171 | $a[$k] = $v; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $a; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Equals end of files names from $bundleAppDir and $bundleDir |
||
181 | * |
||
182 | * @param string $bundleAppPath Path to the resource file from $resourceDir |
||
183 | * @param string $bundlePath Path to the resource file from $bundlePath |
||
184 | * @param string $bundleAppDir Directory to the priority resource |
||
185 | * @param string $bundleDir The bundle root directory |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function isFilePathEquals($bundleAppPath, $bundlePath, $bundleAppDir, $bundleDir) |
||
190 | { |
||
191 | $a = str_replace($bundleDir . DIRECTORY_SEPARATOR . 'Resources', '', $bundlePath); |
||
192 | $b = DIRECTORY_SEPARATOR !== '/' |
||
193 | ? str_replace(str_replace('/', DIRECTORY_SEPARATOR, $bundleAppDir), '', $bundleAppPath) |
||
194 | : str_replace($bundleAppDir, '', $bundleAppPath); |
||
195 | |||
196 | return $a === $b; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Get all data from the directory $dir |
||
201 | * |
||
202 | * @param $dir |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function getData($dir) |
||
207 | { |
||
208 | $realPath = realpath($dir); |
||
209 | if (!is_dir($realPath)) { |
||
210 | return []; |
||
211 | } |
||
212 | |||
213 | $data = []; |
||
214 | if ($this->plainResultStructure) { |
||
215 | $data = $this->getDirectoryContentsArray($realPath); |
||
216 | } else { |
||
217 | $iterator = $this->getDirectoryContents($realPath); |
||
218 | $absolutePathLength = strlen($realPath); |
||
219 | |||
220 | foreach ($iterator as $file) { |
||
221 | $pathName = $file->getPathname(); |
||
222 | $relativePath = substr($pathName, $absolutePathLength + 1); |
||
223 | $split = explode(DIRECTORY_SEPARATOR, $relativePath); |
||
224 | array_pop($split); |
||
225 | |||
226 | if (!empty($split)) { |
||
227 | $path = sprintf('[%s]', implode('][', $split)); |
||
228 | $currValue = $this->getPropertyAccessor()->getValue($data, $path) ?: []; |
||
229 | $this->getPropertyAccessor()->setValue($data, $path, array_merge($currValue, [$pathName])); |
||
230 | } else { |
||
231 | $data[] = $pathName; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | return $data; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function registerFoundResource($bundleClass, $bundleDir, $bundleAppDir, CumulativeResource $resource) |
||
243 | { |
||
244 | $dir = $this->getResourcesDirectoryAbsolutePath($bundleAppDir); |
||
245 | $realPath = realpath($dir); |
||
246 | $bundleAppData = []; |
||
247 | if (is_dir($realPath)) { |
||
248 | $bundleAppData = $this->getDirectoryContentsArray($realPath); |
||
249 | } |
||
250 | |||
251 | $dir = $this->getDirectoryAbsolutePath($bundleDir); |
||
252 | $realPath = realpath($dir); |
||
253 | $bundleData = []; |
||
254 | if (is_dir($realPath)) { |
||
255 | $bundleData = $this->getDirectoryContentsArray($realPath); |
||
256 | } |
||
257 | |||
258 | foreach ($this->mergeArray($bundleAppData, $bundleData, $bundleAppDir, $bundleDir) as $filename) { |
||
259 | $resource->addFound($bundleClass, $filename); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function isResourceFresh($bundleClass, $bundleDir, $bundleAppDir, CumulativeResource $resource, $timestamp) |
||
267 | { |
||
268 | $registeredFiles = $resource->getFound($bundleClass); |
||
269 | $registeredFiles = array_flip($registeredFiles); |
||
270 | |||
271 | // Check and remove data from $bundleAppDir resources directory |
||
272 | if (is_dir($bundleAppDir)) { |
||
273 | $dir = $this->getResourcesDirectoryAbsolutePath($bundleAppDir); |
||
274 | $realPath = realpath($dir); |
||
275 | View Code Duplication | if (is_dir($realPath)) { |
|
276 | $currentContents = $this->getDirectoryContentsArray($realPath); |
||
277 | |||
278 | foreach ($currentContents as $filename) { |
||
279 | if (!$resource->isFound($bundleClass, $filename)) { |
||
280 | return false; |
||
281 | } |
||
282 | |||
283 | unset($registeredFiles[$filename]); |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | |||
288 | // Check and remove data from $bundleDir resources directory |
||
289 | $dir = $this->getDirectoryAbsolutePath($bundleDir); |
||
290 | $realPath = realpath($dir); |
||
291 | View Code Duplication | if (is_dir($realPath)) { |
|
292 | $currentContents = $this->getDirectoryContentsArray($realPath); |
||
293 | |||
294 | foreach ($currentContents as $filename) { |
||
295 | if (!$resource->isFound($bundleClass, $filename)) { |
||
296 | return false; |
||
297 | } |
||
298 | |||
299 | unset($registeredFiles[$filename]); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | // case when entire dir was removed or some file was removed |
||
304 | if (!empty($registeredFiles)) { |
||
305 | return false; |
||
306 | } |
||
307 | |||
308 | return true; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param string $dir |
||
313 | * |
||
314 | * @return \SplFileInfo[]|\Iterator |
||
315 | */ |
||
316 | protected function getDirectoryContents($dir) |
||
317 | { |
||
318 | $recursiveIterator = new \RecursiveIteratorIterator( |
||
319 | new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), |
||
320 | \RecursiveIteratorIterator::LEAVES_ONLY |
||
321 | ); |
||
322 | $recursiveIterator->setMaxDepth($this->maxNestingLevel); |
||
323 | $iterator = new \CallbackFilterIterator( |
||
324 | $recursiveIterator, |
||
325 | function (\SplFileInfo $file) { |
||
326 | return empty($this->fileExtensions) |
||
327 | ? true |
||
328 | : in_array($file->getExtension(), $this->fileExtensions, true); |
||
329 | } |
||
330 | ); |
||
331 | |||
332 | return $iterator; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param string $dir |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | protected function getDirectoryContentsArray($dir) |
||
341 | { |
||
342 | $result = []; |
||
343 | |||
344 | $files = $this->getDirectoryContents($dir); |
||
345 | foreach ($files as $file) { |
||
346 | $result[] = $file->getPathname(); |
||
347 | } |
||
348 | |||
349 | return $result; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param string $bundleDir |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function getDirectoryAbsolutePath($bundleDir) |
||
358 | { |
||
359 | return rtrim($bundleDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->relativeFolderPath; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @param string $bundleAppDir |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | protected function getResourcesDirectoryAbsolutePath($bundleAppDir) |
||
368 | { |
||
369 | return |
||
370 | rtrim($bundleAppDir, DIRECTORY_SEPARATOR) . |
||
371 | DIRECTORY_SEPARATOR . |
||
372 | preg_replace('/Resources\//', '', $this->relativeFolderPath, 1); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get PropertyAccessor |
||
377 | * |
||
378 | * @return PropertyAccessor |
||
379 | */ |
||
380 | protected function getPropertyAccessor() |
||
381 | { |
||
382 | if (null === $this->propertyAccessor) { |
||
383 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
384 | } |
||
385 | |||
386 | return $this->propertyAccessor; |
||
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
387 | } |
||
388 | } |
||
389 |