forked from nextcloud-releases/updater_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·86 lines (71 loc) · 2.24 KB
/
index.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
<?php
/**
* @license MIT <http://opensource.org/licenses/MIT>
*/
require_once __DIR__ . '/vendor/autoload.php';
// Set the default timezone to Europe/Berlin
date_default_timezone_set('Europe/Berlin');
// Set Content-Type to XML
header('Content-Type: application/xml');
// Enforce browser based XSS filters
header('X-XSS-Protection: 1; mode=block');
// Disable sniffing the content type for IE
header('X-Content-Type-Options: nosniff');
// Disallow iFraming from other domains
header('X-Frame-Options: Sameorigin');
// https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
header('X-Robots-Tag: none');
if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_SERVER['PATH_INFO']) &&
substr($_SERVER['PATH_INFO'], -5) === '/hook' &&
isset($_SERVER['HTTP_X_HUB_SIGNATURE']) &&
isset($_SERVER['HTTP_X_GITHUB_EVENT']) &&
$_SERVER['HTTP_X_GITHUB_EVENT'] === 'push') {
if (!file_exists(__DIR__ . '/config/secrets.php')) {
exit();
}
try {
$config = new \UpdateServer\Config(__DIR__ . '/config/secrets.php');
} catch (\RuntimeException $e) {
exit();
}
$webhookSecret = $config->get('githubWebhookSecret');
$branch = $config->get('githubWebhookBranch');
if (!is_string($webhookSecret) || !is_string($branch)) {
exit();
}
$body = file_get_contents('php://input');
$expectedSecretHeader = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$actualSecret = 'sha1=' . hash_hmac('sha1', $body, $webhookSecret);
if ($actualSecret !== $expectedSecretHeader) {
exit();
}
$data = json_decode($body, true);
if (!is_array($data)) {
exit();
}
if (isset($data['ref']) && $data['ref'] === 'refs/heads/' . $branch) {
$escapedDir = escapeshellarg(__DIR__);
exec("cd $escapedDir && git pull");
echo "Deployed";
}
exit();
}
// Return empty response if no version is supplied
if(!isset($_GET['version']) || !is_string($_GET['version'])) {
exit();
}
// Parse the request
try {
$request = new \UpdateServer\Request($_GET['version'], $_SERVER);
} catch (\UpdateServer\Exceptions\UnsupportedReleaseException $e) {
exit();
}
try {
$config = new \UpdateServer\Config(__DIR__ . '/config/config.php');
} catch (\RuntimeException $e) {
exit();
}
// Return a response
$response = new \UpdateServer\Response($request, $config);
echo $response->buildResponse();