These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Eustatos\gitlab\webhook; |
||
3 | |||
4 | /** |
||
5 | * Event |
||
6 | * |
||
7 | * @package Eustatos\gitlab\webhook |
||
8 | * @author Alexander Astashkin <[email protected]> |
||
9 | */ |
||
10 | class Event |
||
11 | { |
||
12 | private $_command; |
||
13 | private $_output = []; |
||
14 | private $_result; |
||
15 | const TOKEN = 'b2b pushed'; |
||
16 | |||
17 | /** |
||
18 | * __construct |
||
19 | * |
||
20 | * @param string $command |
||
21 | * @param string $password |
||
22 | * @access public |
||
23 | */ |
||
24 | public function __construct($command, $password) |
||
25 | { |
||
26 | $this->_command = $command; |
||
27 | |||
28 | if ($password == self::TOKEN) { |
||
29 | exec( |
||
30 | $this->_command, |
||
31 | $this->_output, |
||
32 | $this->_result |
||
33 | ); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * getResult |
||
39 | * |
||
40 | * @access public |
||
41 | * @return void |
||
42 | */ |
||
43 | public function getResult() |
||
44 | { |
||
45 | echo 'Result execution: ' . $this->_command . PHP_EOL |
||
46 | . $this->_result . PHP_EOL; |
||
47 | echo '<pre>'; |
||
48 | print_r($this->_output); |
||
49 | echo '</pre>'; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * logResult |
||
54 | * |
||
55 | * @param string $file |
||
56 | * @access public |
||
57 | * @return void |
||
58 | */ |
||
59 | public function logResult($file) |
||
60 | { |
||
61 | $this->writeToFile( |
||
62 | $file, |
||
63 | $this->_output |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * logRequest |
||
69 | * |
||
70 | * @param string $file |
||
71 | * @access public |
||
72 | * @return void |
||
73 | */ |
||
74 | public function logRequest($file) |
||
0 ignored issues
–
show
|
|||
75 | { |
||
76 | if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) { |
||
77 | throw new Exception('Request method must be POST!'); |
||
78 | } |
||
79 | $contentType = isset($_SERVER['CONTENT_TYPE']) |
||
80 | ? trim($_SERVER['CONTENT_TYPE']) |
||
81 | : ''; |
||
82 | if (strcasecmp($contentType, 'application/json') != 0) { |
||
83 | throw new Exception('Content type must be: application/json'); |
||
84 | } |
||
85 | $jsonRequest = trim(file_get_contents('php://input')); |
||
86 | $jsonRequest = json_decode($jsonRequest, true); |
||
87 | if (!is_array($jsonRequest)) { |
||
88 | throw new Exception('Received content conteined invalid JSON'); |
||
89 | } |
||
90 | $this->writeToFile( |
||
91 | $file, |
||
92 | $jsonRequest |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * writeToFile |
||
98 | * |
||
99 | * Write to file |
||
100 | * |
||
101 | * @param string $file relative path to file for write the content |
||
102 | * Example: `bashexec.log` |
||
103 | * |
||
104 | * @param mixed $content String or array |
||
105 | * @access public |
||
106 | * @return void |
||
107 | */ |
||
108 | public function writeToFile($file, $content) |
||
109 | { |
||
110 | file_put_contents( |
||
111 | __DIR__ . DIRECTORY_SEPARATOR . $file, |
||
112 | '--------------' . date('Y-m-d H:m:s') . '------------' . PHP_EOL |
||
113 | . print_r($content, true), |
||
114 | FILE_APPEND | LOCK_EX |
||
115 | ); |
||
116 | } |
||
117 | } |
||
118 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: