-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitHubHandler.php
87 lines (72 loc) · 1.81 KB
/
GitHubHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace GitHubWebhook;
class Handler
{
private $secret;
private $remote;
private $gitDir;
private $data;
private $event;
private $delivery;
private $gitOutput;
public function __construct($secret, $gitDir, $remote = null)
{
$this->secret = $secret;
$this->remote = $remote;
$this->gitDir = $gitDir;
}
public function getData()
{
return $this->data;
}
public function getDelivery()
{
return $this->delivery;
}
public function getEvent()
{
return $this->event;
}
public function getGitDir()
{
return $this->gitDir;
}
public function getGitOutput()
{
return $this->gitOutput;
}
public function getRemote()
{
return $this->remote;
}
public function getSecret()
{
return $this->secret;
}
public function handle()
{
if (!$this->validate()) {
return false;
}
exec("git --work-tree={$this->gitDir} pull -f {$this->remote}", $this->gitOutput);
return true;
}
public function validate()
{
$headers = apache_request_headers();
$payload = file_get_contents('php://input');
if (!$this->validateSignature($headers['X-Hub-Signature'], $payload)) {
return false;
}
$this->data = json_decode($payload, true);
$this->event = $headers['X-GitHub-Event'];
$this->delivery = $headers['X-GitHub-Delivery'];
return true;
}
protected function validateSignature($gitHubSignatureHeader, $payload)
{
list ($algo, $gitHubSignature) = explode("=", $gitHubSignatureHeader);
$payloadHash = hash_hmac($algo, $payload, $this->secret);
return ($payloadHash == $gitHubSignature);
}
}