| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace DebenOldert\AutoDeploy;
- use DebenOldert\PHPWork\Plugin;
- include_once 'settings.class.php';
- final class AutoDeploy extends Plugin
- {
- private $headers = array();
- private $IAction;
- protected $settings;
- protected $data;
- protected $json;
- private $token;
- public function __construct(string $directory, string $namespace=Null)
- {
- parent::__construct($directory, $namespace);
- }
- public function processRequest()
- {
- global $PHPWORK;
- $this->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__;
|