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 Bernard\Driver\FlatFile; |
||
4 | |||
5 | /** |
||
6 | * Flat file driver to provide a simple job queue without any |
||
7 | * database. |
||
8 | * |
||
9 | * @author Markus Bachmann <[email protected]> |
||
10 | */ |
||
11 | class Driver implements \Bernard\Driver |
||
12 | { |
||
13 | private $baseDirectory; |
||
14 | |||
15 | private $permissions; |
||
16 | |||
17 | /** |
||
18 | * @param string $baseDirectory The base directory |
||
19 | * @param int $permissions permissions to create the file with |
||
20 | */ |
||
21 | 10 | public function __construct($baseDirectory, $permissions = 0740) |
|
22 | { |
||
23 | 10 | $this->baseDirectory = $baseDirectory; |
|
24 | 10 | $this->permissions = $permissions; |
|
25 | 10 | } |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 1 | public function listQueues() |
|
31 | { |
||
32 | 1 | $it = new \FilesystemIterator($this->baseDirectory, \FilesystemIterator::SKIP_DOTS); |
|
33 | |||
34 | 1 | $queues = []; |
|
35 | |||
36 | 1 | foreach ($it as $file) { |
|
37 | 1 | if (!$file->isDir()) { |
|
38 | 1 | continue; |
|
39 | } |
||
40 | |||
41 | 1 | array_push($queues, $file->getBasename()); |
|
42 | 1 | } |
|
43 | |||
44 | 1 | return $queues; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 10 | public function createQueue($queueName) |
|
51 | { |
||
52 | 10 | $queueDir = $this->getQueueDirectory($queueName); |
|
53 | |||
54 | 10 | if (is_dir($queueDir)) { |
|
55 | 1 | return; |
|
56 | } |
||
57 | |||
58 | 10 | mkdir($queueDir, 0755, true); |
|
59 | 10 | } |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function countMessages($queueName) |
||
65 | { |
||
66 | $iterator = new \RecursiveDirectoryIterator( |
||
67 | $this->getQueueDirectory($queueName), |
||
68 | \FilesystemIterator::SKIP_DOTS |
||
69 | ); |
||
70 | $iterator = new \RecursiveIteratorIterator($iterator); |
||
71 | $iterator = new \RegexIterator($iterator, '#\.job$#'); |
||
72 | |||
73 | return iterator_count($iterator); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 8 | public function pushMessage($queueName, $message) |
|
80 | { |
||
81 | 8 | $queueDir = $this->getQueueDirectory($queueName); |
|
82 | |||
83 | 8 | $filename = $this->getJobFilename($queueName); |
|
84 | |||
85 | 8 | file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message); |
|
86 | 8 | chmod($queueDir.DIRECTORY_SEPARATOR.$filename, $this->permissions); |
|
87 | 8 | } |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 4 | public function popMessage($queueName, $duration = 5) |
|
93 | { |
||
94 | 4 | $runtime = microtime(true) + $duration; |
|
95 | 4 | $queueDir = $this->getQueueDirectory($queueName); |
|
96 | |||
97 | 4 | $files = $this->getJobFiles($queueName); |
|
98 | |||
99 | 4 | while (microtime(true) < $runtime) { |
|
100 | 4 | if ($files) { |
|
0 ignored issues
–
show
|
|||
101 | 4 | $id = array_pop($files); |
|
102 | 4 | if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) { |
|
103 | 4 | return [file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id]; |
|
104 | } |
||
105 | |||
106 | return $this->processFileOrFail($queueDir, $id); |
||
107 | } else { |
||
108 | // In order to notice that a new message received, update the list. |
||
109 | 1 | $files = $this->getJobFiles($queueName); |
|
110 | } |
||
111 | |||
112 | 1 | usleep(1000); |
|
113 | 1 | } |
|
114 | |||
115 | return [null, null]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $queueDir |
||
120 | * @param string $id |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | private function processFileOrFail($queueDir, $id) { |
||
125 | $name = $queueDir.DIRECTORY_SEPARATOR.$id; |
||
126 | $newName = $name.'.proceed'; |
||
127 | |||
128 | if (!@rename($name, $newName)) { |
||
129 | throw new InsufficientPermissionsException('Unable to process file: '.$name); |
||
130 | } |
||
131 | |||
132 | return [file_get_contents($newName), $id]; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function acknowledgeMessage($queueName, $receipt) |
|
139 | { |
||
140 | 1 | $queueDir = $this->getQueueDirectory($queueName); |
|
141 | 1 | $path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed'; |
|
142 | |||
143 | 1 | if (!is_file($path)) { |
|
144 | return; |
||
145 | } |
||
146 | |||
147 | 1 | unlink($path); |
|
148 | 1 | } |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function peekQueue($queueName, $index = 0, $limit = 20) |
|
154 | { |
||
155 | 1 | $queueDir = $this->getQueueDirectory($queueName); |
|
156 | |||
157 | 1 | $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME); |
|
158 | 1 | $files = array_keys(iterator_to_array($it)); |
|
159 | |||
160 | 1 | natsort($files); |
|
161 | 1 | $files = array_reverse($files); |
|
162 | |||
163 | 1 | $files = array_slice($files, $index, $limit); |
|
164 | |||
165 | 1 | $messages = []; |
|
166 | |||
167 | 1 | foreach ($files as $file) { |
|
168 | 1 | array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file)); |
|
169 | 1 | } |
|
170 | |||
171 | 1 | return $messages; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 2 | public function removeQueue($queueName) |
|
178 | { |
||
179 | 2 | $iterator = new \RecursiveDirectoryIterator( |
|
180 | 2 | $this->getQueueDirectory($queueName), |
|
181 | \FilesystemIterator::SKIP_DOTS |
||
182 | 2 | ); |
|
183 | 2 | $iterator = new \RecursiveIteratorIterator($iterator); |
|
184 | 2 | $iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#'); |
|
185 | |||
186 | 2 | foreach ($iterator as $file) { |
|
187 | /* @var $file \DirectoryIterator */ |
||
188 | 2 | unlink($file->getRealPath()); |
|
189 | 2 | } |
|
190 | |||
191 | 2 | rmdir($this->getQueueDirectory($queueName)); |
|
192 | 2 | } |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function info() |
||
198 | { |
||
199 | return []; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $queueName |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 10 | private function getQueueDirectory($queueName) |
|
208 | { |
||
209 | 10 | return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(['\\', '.'], '-', $queueName); |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Generates a uuid. |
||
214 | * |
||
215 | * @param string $queueName |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 8 | private function getJobFilename($queueName) |
|
220 | { |
||
221 | 8 | $path = $this->baseDirectory.'/bernard.meta'; |
|
222 | |||
223 | 8 | if (!is_file($path)) { |
|
224 | 8 | touch($path); |
|
225 | 8 | chmod($path, $this->permissions); |
|
226 | 8 | } |
|
227 | |||
228 | 8 | $file = new \SplFileObject($path, 'r+'); |
|
229 | 8 | $file->flock(LOCK_EX); |
|
230 | |||
231 | 8 | $meta = unserialize($file->fgets()); |
|
232 | |||
233 | 8 | $id = isset($meta[$queueName]) ? $meta[$queueName] : 0; |
|
234 | 8 | ++$id; |
|
235 | |||
236 | 8 | $filename = sprintf('%d.job', $id); |
|
237 | 8 | $meta[$queueName] = $id; |
|
238 | |||
239 | 8 | $content = serialize($meta); |
|
240 | |||
241 | 8 | $file->fseek(0); |
|
242 | 8 | $file->fwrite($content, strlen($content)); |
|
243 | 8 | $file->flock(LOCK_UN); |
|
244 | |||
245 | 8 | return $filename; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $queueName |
||
250 | * |
||
251 | * @return string[] |
||
252 | */ |
||
253 | 4 | private function getJobFiles($queueName) |
|
254 | { |
||
255 | 4 | $it = new \GlobIterator( |
|
256 | 4 | $this->getQueueDirectory($queueName) . DIRECTORY_SEPARATOR . '*.job', |
|
257 | \FilesystemIterator::KEY_AS_FILENAME |
||
258 | 4 | ); |
|
259 | 4 | $files = array_keys(iterator_to_array($it)); |
|
260 | 4 | natsort($files); |
|
261 | |||
262 | 4 | return $files; |
|
263 | } |
||
264 | } |
||
265 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.