data = file_get_contents('php://input'); $this->parseHeaders(); if($this->hasHeader('X-Gogs-Event') && $this->hasHeader('X-Gogs-Signature')) { $isAuthenticated = false; foreach ($this->settings['keys'] as $secret) { if($this->authenticate($secret)) { $isAuthenticated = true; $this->token = $secret; break; } } if($isAuthenticated) { $this->json = $this->parseData(); //var_dump($this->json); switch (strtoupper($this->getHeader('X-Gogs-Event'))) { case 'RELEASE': include 'releaseaction.class.php'; $this->IAction = new ReleaseAction($this->json, $this->settings, $this->token); break; case 'PUSH': //TODO: Implement PUSH event break; default: $PHPWORK->sendHttpCode(501); } $this->IAction->start(); if($this->IAction->isSuccess()){ $PHPWORK->sendHttpCode(200); } else { $PHPWORK->sendHttpCode(500); } } } } private function authenticate($key) { global $PHPWORK; if($PHPWORK->isProduction()) { $hash = hash_hmac('SHA256', $this->data, $key); return hash_equals($this->headers['X-Gogs-Signature'], $hash); } else { return true; } } private function parseHeaders() { foreach($_SERVER as $key=>$value) { if (strpos($key, 'HTTP_') === 0) { $key=str_replace(' ','-',ucwords(strtolower(str_replace('_',' ',substr($key,5))))); $this->headers[$key]=$value; } } } private function parseData() { return json_decode($this->data); } private function getHeader($key) { return $this->headers[$key] ?? Null; } private function hasHeader($key) { return $this->getHeader($key) !== Null; } public function route(string $key, array $match) { $this->processRequest(); } } return __NAMESPACE__;