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 | 1 | } elseif ($statusCode === 200) { |
|
64 | // Send a success response to ping event |
||
65 | $this->auto_render = false; |
||
66 | $this->response->statusCode($statusCode); |
||
67 | |||
68 | return $this->response; |
||
69 | } |
||
70 | |||
71 | 1 | $issuesData = $this->request->input('json_decode', true); |
|
72 | 1 | $eventAction = $issuesData['action']; |
|
73 | 1 | $issueNumber = $issuesData['issue'] ? $issuesData['issue']['number'] : ''; |
|
74 | |||
75 | 1 | if ($eventAction === 'closed' |
|
76 | 1 | || $eventAction === 'opened' |
|
77 | 1 | || $eventAction === 'reopened' |
|
78 | ) { |
||
79 | 1 | $status = $this->_getAppropriateStatus($eventAction); |
|
80 | |||
81 | 1 | if (($reportsUpdated = $this->Reports->setLinkedReportStatus($issueNumber, $status)) > 0) { |
|
82 | Log::debug( |
||
83 | $reportsUpdated . ' linked reports to issue number ' |
||
84 | . $issueNumber . ' were updated according to recieved action ' |
||
85 | . $eventAction |
||
86 | ); |
||
87 | } else { |
||
88 | 1 | Log::info( |
|
89 | 1 | 'No linked report found for issue number \'' . $issueNumber |
|
90 | 1 | . '\'. Ignoring the event.' |
|
91 | ); |
||
92 | 1 | $statusCode = 204; |
|
93 | } |
||
94 | } else { |
||
95 | 1 | Log::info( |
|
96 | 1 | 'Recieved a webhook event for action \'' . $eventAction |
|
97 | 1 | . '\' on issue number ' . $issueNumber . '. Ignoring the event.' |
|
98 | ); |
||
99 | 1 | $statusCode = 204; |
|
100 | } |
||
101 | |||
102 | // Send a response |
||
103 | 1 | $this->auto_render = false; |
|
104 | 1 | $this->response->statusCode($statusCode); |
|
105 | |||
106 | 1 | return $this->response; |
|
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Validate HTTP Request recieved |
||
112 | * |
||
113 | * @param Request $request Request object |
||
114 | * |
||
115 | * @return int status code based on if this is a valid request |
||
116 | */ |
||
117 | 1 | protected function _validateRequest($request) |
|
118 | { |
||
119 | // Default $statusCode |
||
120 | 1 | $statusCode = 201; |
|
121 | |||
122 | 1 | $userAgent = $request->getHeaderLine('User-Agent'); |
|
123 | 1 | $eventType = $request->getHeaderLine('X-GitHub-Event'); |
|
124 | |||
125 | 1 | $recievedHashHeader = $request->getHeaderLine('X-Hub-Signature'); |
|
126 | 1 | $algo = ''; |
|
127 | 1 | $recievedHash = ''; |
|
128 | 1 | if ($recievedHashHeader !== NULL) { |
|
129 | 1 | $parts = explode('=', $recievedHashHeader); |
|
130 | 1 | if (count($parts) > 1) { |
|
131 | 1 | $algo = $parts[0]; |
|
132 | 1 | $recievedHash = $parts[1]; |
|
133 | } |
||
134 | } |
||
135 | |||
136 | 1 | $expectedHash = $this->_getHash(file_get_contents('php://input'), $algo); |
|
137 | |||
138 | 1 | if ($userAgent !== NULL && strpos($userAgent, 'GitHub-Hookshot') !== 0) { |
|
139 | // Check if the User-agent is Github |
||
140 | // Otherwise, Send a '403: Forbidden' |
||
141 | |||
142 | 1 | Log::error( |
|
143 | 1 | 'Invalid User agent: ' . $userAgent |
|
144 | 1 | . '. Ignoring the event.' |
|
145 | ); |
||
146 | 1 | $statusCode = 403; |
|
147 | |||
148 | 1 | return $statusCode; |
|
149 | 1 | } elseif ($eventType !== NULL && $eventType === 'ping') { |
|
150 | // Check if the request is based on 'issues' event |
||
151 | // Otherwise, Send a '400: Bad Request' |
||
152 | |||
153 | Log::info( |
||
154 | 'Ping event type recieved.' |
||
155 | ); |
||
156 | $statusCode = 200; |
||
157 | |||
158 | return $statusCode; |
||
159 | 1 | } elseif ($eventType !== NULL && $eventType !== 'issues') { |
|
160 | // Check if the request is based on 'issues' event |
||
161 | // Otherwise, Send a '400: Bad Request' |
||
162 | |||
163 | 1 | Log::error( |
|
164 | 1 | 'Unexpected event type: ' . $eventType |
|
165 | 1 | . '. Ignoring the event.' |
|
166 | ); |
||
167 | 1 | $statusCode = 400; |
|
168 | |||
169 | 1 | return $statusCode; |
|
170 | 1 | } elseif ($recievedHash !== $expectedHash) { |
|
171 | // Check if hash matches |
||
172 | // Otherwise, Send a '401: Unauthorized' |
||
173 | |||
174 | 1 | Log::error( |
|
175 | 1 | 'Recieved hash ' . $recievedHash . ' does not match ' |
|
176 | 1 | . ' expected hash ' . $expectedHash |
|
177 | 1 | . '. Ignoring the event.' |
|
178 | ); |
||
179 | 1 | $statusCode = 401; |
|
180 | |||
181 | 1 | return $statusCode; |
|
182 | } |
||
183 | |||
184 | 1 | return $statusCode; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get the hash of raw POST payload |
||
189 | * |
||
190 | * @param string $payload Raw POST body string |
||
191 | * @param string $algo Algorithm used to calculate the hash |
||
192 | * |
||
193 | * @return string Hmac Digest-based hash of payload |
||
194 | */ |
||
195 | 1 | protected function _getHash($payload, $algo) |
|
204 | |||
205 | /** |
||
206 | * Get appropriate new status based on action recieved in github event |
||
207 | * |
||
208 | * @param string $action Action recieved in Github webhook event |
||
209 | * |
||
210 | * @return string Appropriate new status for the related reports |
||
211 | */ |
||
212 | 1 | protected function _getAppropriateStatus($action) |
|
230 | } |
||
231 |
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.