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 | namespace Werkspot\BingAdsApiBundle\Api\Helper; |
||
3 | |||
4 | use Exception; |
||
5 | use GuzzleHttp\ClientInterface; |
||
6 | use Symfony\Component\Filesystem\Filesystem; |
||
7 | use Symfony\Component\Finder\Finder; |
||
8 | use Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException; |
||
9 | use Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException; |
||
10 | use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\CurlException; |
||
11 | use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\HttpStatusCodeException; |
||
12 | use ZipArchive; |
||
13 | |||
14 | class File |
||
15 | { |
||
16 | /** |
||
17 | * @var ClientInterface |
||
18 | */ |
||
19 | private $guzzleClient; |
||
20 | |||
21 | /** |
||
22 | * @var Filesystem |
||
23 | 27 | */ |
|
24 | private $filesystem; |
||
25 | 27 | ||
26 | 27 | /** |
|
27 | 27 | * @var Finder |
|
28 | */ |
||
29 | private $finder; |
||
30 | |||
31 | /** |
||
32 | * @var ZipArchive |
||
33 | */ |
||
34 | private $zipArchive; |
||
35 | |||
36 | public function __construct(ClientInterface $guzzleClient, Filesystem $fileSystem, Finder $finder) |
||
37 | 2 | { |
|
38 | $this->guzzleClient = $guzzleClient; |
||
39 | 2 | $this->filesystem = $fileSystem; |
|
40 | 2 | $this->finder = $finder; |
|
41 | 1 | $this->zipArchive = new ZipArchive(); |
|
42 | } |
||
43 | 1 | ||
44 | 1 | /** |
|
45 | 1 | * @param $source |
|
46 | 1 | * @param null|string $destination |
|
47 | 1 | * |
|
48 | 1 | * @throws NoFileDestinationException|FileNotCopiedException |
|
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | 1 | public function copyFile($source, $destination = null) |
|
53 | 1 | { |
|
54 | if (preg_match('/^http(s?):\/\//', $source)) { |
||
55 | if ($destination === null) { |
||
56 | 1 | throw new NoFileDestinationException(); |
|
57 | } |
||
58 | $destination = $this->download($source, $destination); |
||
59 | } else { |
||
60 | if ($destination !== null) { |
||
61 | $this->filesystem->copy($source, $destination); |
||
62 | } else { |
||
63 | $destination = $source; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | if (!$this->filesystem->exists($destination)) { |
||
68 | throw new FileNotCopiedException(); |
||
69 | 2 | } |
|
70 | |||
71 | 2 | return $destination; |
|
72 | } |
||
73 | 2 | ||
74 | /** |
||
75 | * @param string $file |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function isHealthyZipFile($file) |
||
80 | { |
||
81 | $zipStatus = $this->zipArchive->open($file, ZipArchive::CHECKCONS); |
||
82 | if ($zipStatus === ZipArchive::ER_OK || $zipStatus === true) { |
||
83 | $this->zipArchive->close(); |
||
84 | $status = true; |
||
85 | 2 | } else { |
|
86 | $status = false; |
||
87 | 2 | } |
|
88 | 2 | ||
89 | 2 | return $status; |
|
90 | 1 | } |
|
91 | |||
92 | 1 | /** |
|
93 | 1 | * @param string $url |
|
94 | 1 | * @param string $destination |
|
95 | 1 | * |
|
96 | 1 | * @throws CurlException |
|
97 | 1 | * @throws HttpStatusCodeException |
|
98 | 1 | * @throws \Exception |
|
99 | 1 | * |
|
100 | 1 | * @return string |
|
101 | 1 | */ |
|
102 | public function download($url, $destination) |
||
103 | 1 | { |
|
104 | $this->guzzleClient->request('GET', $url, ['sink' => $destination]); |
||
105 | |||
106 | return $destination; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param string $file zipFile we want to open |
||
111 | * @param null|string $extractTo |
||
112 | * @param true|bool $delete |
||
113 | * |
||
114 | * @throws Exception |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function unZip($file, $extractTo = null, $delete = true) |
||
119 | { |
||
120 | $zipDir = ($extractTo) ? $extractTo : dirname($file); |
||
121 | |||
122 | if ($this->zipArchive->open($file) !== true) { |
||
123 | throw new Exception("Could not open file {$file}"); |
||
0 ignored issues
–
show
|
|||
124 | } |
||
125 | $files = []; |
||
126 | for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) { |
||
127 | $stat = $this->zipArchive->statIndex($i); |
||
128 | $files[] = "{$zipDir}/{$stat['name']}"; |
||
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $zipDir instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() As per coding-style, please use concatenation or
sprintf for the variable $stat instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() |
|||
129 | } |
||
130 | $this->zipArchive->extractTo($zipDir); |
||
131 | $this->zipArchive->close(); |
||
132 | if ($delete) { |
||
133 | $this->filesystem->remove($file); |
||
134 | } |
||
135 | |||
136 | return $files; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $file |
||
141 | * @return string[] |
||
142 | */ |
||
143 | public function readFileLinesIntoArray($file) |
||
144 | { |
||
145 | return file($file); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string[] $lines |
||
150 | * @param string $file |
||
151 | */ |
||
152 | public function writeLinesToFile($lines, $file) |
||
153 | { |
||
154 | $fp = fopen($file, 'w'); |
||
155 | fwrite($fp, implode('', $lines)); |
||
156 | fclose($fp); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string|string[] $cache |
||
161 | */ |
||
162 | public function clearCache($cache) |
||
163 | { |
||
164 | if (is_array($cache)) { |
||
165 | foreach ($cache as $file) { |
||
166 | $this->removeFile($file); |
||
167 | } |
||
168 | } elseif (is_dir($cache)) { |
||
169 | foreach ($this->finder->files()->in($cache) as $file) { |
||
170 | $this->removeFile($file); |
||
171 | } |
||
172 | } else { |
||
173 | $this->removeFile($cache); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string $file |
||
179 | */ |
||
180 | private function removeFile($file) |
||
181 | { |
||
182 | $this->filesystem->remove($file); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string[] $files |
||
187 | * @param string $target |
||
188 | */ |
||
189 | public function moveFirstFile(array $files, $target) |
||
190 | { |
||
191 | $this->filesystem->rename($files[0], $target); |
||
192 | } |
||
193 | |||
194 | public function createDirIfNotExists($path) |
||
195 | { |
||
196 | if (!$this->filesystem->exists($path)) { |
||
197 | $this->filesystem->mkdir($path, 0700); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.