|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Lekarna.cz (http://www.lekarna.cz/) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014 Pears Health Cyber, s.r.o. (http://pearshealthcyber.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App; |
|
13
|
|
|
|
|
14
|
|
|
use Interop\Container\ContainerInterface; |
|
15
|
|
|
use Slim\Http\Request; |
|
16
|
|
|
use Slim\Http\Response; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
final class BashRestController |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
const SECRET_HEADER = 'X-Secret'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Executor |
|
26
|
|
|
*/ |
|
27
|
|
|
private $executor; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $secret; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $scripts; |
|
38
|
|
|
|
|
39
|
5 |
|
public function __construct(ContainerInterface $ci, Executor $executor) |
|
40
|
|
|
{ |
|
41
|
5 |
|
$this->executor = $executor; |
|
42
|
5 |
|
$this->secret = $ci->get('settings')['secret']; |
|
43
|
5 |
|
$this->scripts = $ci->get('bashREST'); |
|
|
|
|
|
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
5 |
|
public function __invoke(Request $request, Response $response, array $args) |
|
48
|
|
|
{ |
|
49
|
5 |
|
$secured = FALSE; |
|
50
|
5 |
|
foreach ($request->getHeader(self::SECRET_HEADER) as $secret) { |
|
51
|
4 |
|
if ($secret == $this->secret) { // allow cast |
|
52
|
4 |
|
$secured = TRUE; |
|
53
|
4 |
|
} |
|
54
|
5 |
|
} |
|
55
|
|
|
|
|
56
|
5 |
|
if ($this->secret !== NULL && ! $secured) { |
|
57
|
1 |
|
return $response->withStatus(403); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$projectName = $args['group'] . '/' . $args['project']; |
|
61
|
4 |
|
$action = $args['action']; |
|
62
|
|
|
|
|
63
|
4 |
|
if ( ! isset($this->scripts[$projectName][$action])) { |
|
64
|
1 |
|
return $response->withStatus(404); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return $response->withStatus(200) |
|
68
|
3 |
|
->withJson([ |
|
69
|
3 |
|
'result' => $this->executor->executeCommand( |
|
70
|
3 |
|
$this->scripts[$projectName][$action], |
|
71
|
3 |
|
$this->flatten($request->getParsedBody()) |
|
|
|
|
|
|
72
|
3 |
|
) |
|
73
|
3 |
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
3 |
|
private function flatten(array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
$toProcess = [[ |
|
80
|
3 |
|
'data' => $data, |
|
81
|
|
|
'prefix' => 'HOOK' |
|
82
|
3 |
|
]]; |
|
83
|
|
|
|
|
84
|
3 |
|
$flattened = []; |
|
85
|
|
|
|
|
86
|
3 |
|
while ( ! empty($toProcess)) { |
|
87
|
3 |
|
$actual = array_pop($toProcess); |
|
88
|
3 |
|
foreach ($actual['data'] as $key => $value) { |
|
89
|
1 |
|
if (is_scalar($value)) { |
|
90
|
1 |
|
$flattened[$actual['prefix'] . '_' . $key] = $value; |
|
91
|
1 |
|
} else if (is_array($value)) { |
|
92
|
1 |
|
array_push($toProcess, [ |
|
93
|
1 |
|
'data' => $value, |
|
94
|
1 |
|
'prefix' => $actual['prefix'] . '_' . $key |
|
95
|
1 |
|
]); |
|
96
|
1 |
|
} |
|
97
|
3 |
|
} |
|
98
|
3 |
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
return $flattened; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..