This repository was archived by the owner on Jul 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.php
More file actions
111 lines (94 loc) · 3.83 KB
/
Copy pathdeploy.php
File metadata and controls
111 lines (94 loc) · 3.83 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
// script errors will be send to this email:
$error_mail = "me@arsen.pw";
function run() {
global $rawInput;
// read config.json
$config_filename = 'config.json';
if (!file_exists($config_filename)) {
throw new Exception("Can't find " . $config_filename, 1);
}
$config = json_decode(file_get_contents($config_filename), true);
$postBody = $_POST['payload'];
$payload = json_decode($postBody);
if (isset($config['email'])) {
$headers = 'From: ' . $config['email']['from'] . "\r\n";
$headers .= 'CC: ' . $payload->pusher->email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
}
// check if the request comes from github server
$github_ips = v4CIDRtoMask('192.30.252.0/22');
$ips_data = ipv4Breakout($github_ips[0], $github_ips[1]);
$first_ip = sprintf("%u", $ips_data['first_host']);
$last_ip = sprintf("%u", $ips_data['last_host']);
$webhook_ip = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
if ($last_ip > $webhook_ip && $webhook_ip > $first_ip) {
foreach ($config['endpoints'] as $endpoint) {
// check if the push came from the right repository and branch
if ($payload->repository->url == 'https://github.com/'.$endpoint['repo'] &&
$payload->ref == 'refs/heads/'.$endpoint['branch'])
{
// execute update script, and record its output
ob_start();
passthru($endpoint['run']);
$output = ob_get_contents();
ob_end_clean();
// prepare and send the notification email
if (isset($config['email']))
{
// send mail to someone, and the github user who pushed the commit
$body = '<p>The Github user <a href="https://github.com/' . $payload->pusher->name . '">@' . $payload->pusher->name . '</a> has pushed to ' . $payload->repository->url . ' and consquently, ' . $endpoint['action'] . '</p><p>Here\'s a brief list of what has been changed:</p><ul>';
foreach ($payload->commits as $commit) {
$body .= '<li>'.$commit->message.'<br/><small style="color:#999">added: <b>'.count($commit->added).'</b> modified: <b>'.count($commit->modified).'</b> removed: <b>'.count($commit->removed).'</b> <a href="'.$commit->url.'">read more</a></small></li>';
}
$body .= '</ul><p>What follows is the output of the script:</p><pre>'.$output.'</pre><p>Cheers, <br/>Github Webhook Endpoint</p>';
mail($config['email']['to'], $endpoint['action'], $body, $headers);
}
return $output;
}
}
} else {
throw new Exception("This does not appear to be a valid requests from Github. Webhook from IP: " . $_SERVER['REMOTE_ADDR'], 2);
}
}
function v4CIDRtoMask($cidr) {
$cidr = explode('/', $cidr);
return array($cidr[0], long2ip(-1 << (32 - (int)$cidr[1])));
}
function ipv4Breakout ($ip_address, $ip_nmask) {
$hosts = array();
//convert ip addresses to long form
$ip_address_long = ip2long($ip_address);
$ip_nmask_long = ip2long($ip_nmask);
//caculate network address
$ip_net = $ip_address_long & $ip_nmask_long;
//caculate first usable address
$ip_host_first = ((~$ip_nmask_long) & $ip_address_long);
$ip_first = ($ip_address_long ^ $ip_host_first) + 1;
//caculate last usable address
$ip_broadcast_invert = ~$ip_nmask_long;
$ip_last = ($ip_address_long | $ip_broadcast_invert) - 1;
//caculate broadcast address
$ip_broadcast = $ip_address_long | $ip_broadcast_invert;
// foreach (range($ip_first, $ip_last) as $ip) {
// array_push($hosts, $ip);
// }
$block_info = array("network" => "$ip_net",
"first_host" => "$ip_first",
"last_host" => "$ip_last",
"broadcast" => "$ip_broadcast");
return $block_info;
}
try {
if (!isset($_POST['payload']) || !isset($argv[1])) {
echo "Works fine.";
} else {
echo run();
}
} catch ( Exception $e ) {
$msg = $e->getMessage();
echo $msg;
mail($error_mail, $msg, ''.$e);
}
?>