1 | <?php |
||
30 | class EventsController extends AppController |
||
31 | { |
||
32 | |||
33 | 1 | public function initialize() |
|
40 | |||
41 | 1 | public function beforeFilter(Event $event) |
|
45 | |||
46 | 1 | public function index() |
|
47 | { |
||
48 | // Only allow POST requests |
||
49 | 1 | $this->request->allowMethod(['post']); |
|
50 | |||
51 | // Validate request |
||
52 | 1 | if (($statusCode = $this->_validateRequest($this->request)) !== 201) { |
|
53 | 1 | Log::error( |
|
54 | 'Could not validate the request. Sending a ' |
||
55 | 1 | . $statusCode . ' response.' |
|
56 | ); |
||
57 | |||
58 | // Send a response |
||
59 | 1 | $this->auto_render = false; |
|
60 | 1 | $this->response->statusCode($statusCode); |
|
61 | |||
62 | 1 | return $this->response; |
|
63 | } |
||
64 | |||
65 | 1 | $issuesData = $this->request->input('json_decode', true); |
|
66 | 1 | $eventAction = $issuesData['action']; |
|
67 | 1 | $issueNumber = $issuesData['issue'] ? $issuesData['issue']['number'] : ''; |
|
68 | |||
69 | 1 | if ($eventAction === 'closed' |
|
70 | 1 | || $eventAction === 'opened' |
|
71 | 1 | || $eventAction === 'reopened' |
|
72 | ) { |
||
73 | 1 | $status = $this->_getAppropriateStatus($eventAction); |
|
74 | |||
75 | 1 | if (($reportsUpdated = $this->Reports->setLinkedReportStatus($issueNumber, $status)) > 0) { |
|
76 | 1 | Log::debug( |
|
77 | 1 | $reportsUpdated . ' linked reports to issue number ' |
|
78 | 1 | . $issueNumber . ' were updated according to recieved action ' |
|
79 | 1 | . $eventAction |
|
80 | ); |
||
81 | } else { |
||
82 | 1 | Log::info( |
|
83 | 1 | 'No linked report found for issue number \'' . $issueNumber |
|
84 | 1 | . '\'. Ignoring the event.' |
|
85 | ); |
||
86 | 1 | $statusCode = 204; |
|
87 | 1 | print_r('asd ' . $issueNumber . ' ' . $eventAction); |
|
88 | } |
||
89 | } else { |
||
90 | 1 | Log::info( |
|
91 | 1 | 'Recieved a webhook event for action \'' . $eventAction |
|
92 | 1 | . '\' on issue number ' . $issueNumber . '. Ignoring the event.' |
|
93 | ); |
||
94 | 1 | $statusCode = 204; |
|
95 | } |
||
96 | |||
97 | // Send a response |
||
98 | 1 | $this->auto_render = false; |
|
99 | 1 | $this->response->statusCode($statusCode); |
|
100 | |||
101 | 1 | return $this->response; |
|
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Validate HTTP Request recieved |
||
107 | * |
||
108 | * @param Request $request Request object |
||
109 | * |
||
110 | * @return int status code based on if this is a valid request |
||
111 | */ |
||
112 | 1 | protected function _validateRequest($request) |
|
113 | { |
||
114 | // Default $statusCode |
||
115 | 1 | $statusCode = 201; |
|
116 | |||
117 | 1 | $userAgent = $request->getHeaderLine('User-Agent'); |
|
118 | 1 | $eventType = $request->getHeaderLine('X-GitHub-Event'); |
|
119 | |||
120 | 1 | $recievedHashHeader = $request->getHeaderLine('X-Hub-Signature'); |
|
121 | 1 | $algo = ''; |
|
122 | 1 | $recievedHash = ''; |
|
123 | 1 | if ($recievedHashHeader !== NULL) { |
|
124 | 1 | $parts = explode('=', $recievedHashHeader); |
|
125 | 1 | if (count($parts) > 1) { |
|
126 | 1 | $algo = $parts[0]; |
|
127 | 1 | $recievedHash = $parts[1]; |
|
128 | } |
||
129 | } |
||
130 | |||
131 | 1 | $expectedHash = $this->_getHash(file_get_contents('php://input'), $algo); |
|
132 | |||
133 | 1 | if ($userAgent !== NULL && strpos($userAgent, 'GitHub-Hookshot') !== 0) { |
|
134 | // Check if the User-agent is Github |
||
135 | // Otherwise, Send a '403: Forbidden' |
||
136 | |||
137 | 1 | Log::error( |
|
138 | 1 | 'Invalid User agent: ' . $userAgent |
|
139 | 1 | . '. Ignoring the event.' |
|
140 | ); |
||
141 | 1 | $statusCode = 403; |
|
142 | |||
143 | 1 | return $statusCode; |
|
144 | 1 | } elseif ($eventType !== NULL && $eventType !== 'issues') { |
|
145 | // Check if the request is based on 'issues' event |
||
146 | // Otherwise, Send a '400: Bad Request' |
||
147 | |||
148 | 1 | Log::error( |
|
149 | 1 | 'Unexpected event type: ' . $eventType |
|
150 | 1 | . '. Ignoring the event.' |
|
151 | ); |
||
152 | 1 | $statusCode = 400; |
|
153 | |||
154 | 1 | return $statusCode; |
|
155 | 1 | } elseif ($recievedHash !== $expectedHash) { |
|
156 | // Check if hash matches |
||
157 | // Otherwise, Send a '401: Unauthorized' |
||
158 | |||
159 | 1 | Log::error( |
|
160 | 1 | 'Recieved hash ' . $recievedHash . ' does not match ' |
|
161 | 1 | . ' expected hash ' . $expectedHash |
|
162 | 1 | . '. Ignoring the event.' |
|
163 | ); |
||
164 | 1 | $statusCode = 401; |
|
165 | |||
166 | 1 | var_dump($expectedHash); |
|
167 | |||
168 | 1 | return $statusCode; |
|
169 | |||
170 | } |
||
171 | |||
172 | 1 | return $statusCode; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get the hash of raw POST payload |
||
177 | * |
||
178 | * @param string $payload Raw POST body string |
||
179 | * @param string $algo Algorithm used to calculate the hash |
||
180 | * |
||
181 | * @return string Hmac Digest-based hash of payload |
||
182 | */ |
||
183 | 1 | protected function _getHash($payload, $algo) |
|
192 | |||
193 | /** |
||
194 | * Get appropriate new status based on action recieved in github event |
||
195 | * |
||
196 | * @param string $action Action recieved in Github webhook event |
||
197 | * |
||
198 | * @return string Appropriate new status for the related reports |
||
199 | */ |
||
200 | 1 | protected function _getAppropriateStatus($action) |
|
218 | } |
||
219 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.