-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallback.php
More file actions
128 lines (99 loc) · 4.03 KB
/
callback.php
File metadata and controls
128 lines (99 loc) · 4.03 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
define('REPO', 'GITHUB_REPOSITORY_TITLE');
define('USER', 'GITHUB_ACCOUNT_USER_NAME');
define('PAT', 'YOUR_PERSONAL_ACCESS_TOKEN');
define('SECRET', 'WEBHOOK_SECRET');
define('HOOK_ID', 'WEBHOOK_ID');
ini_set('memory_limit', '1G');
class GitToLive {
protected $payload;
public function __construct(){
$this->payload = @json_decode(file_get_contents('php://input'), true);
self::verifyRequest();
self::sync();
}
public function verifyRequest(){
/*CHECK 1 | Allow only post requests*/
header("HTTP/1.1 405 Method Not Allowed");
if($_SERVER['REQUEST_METHOD'] !== 'POST') die('Method not allowed');
/*CHECK 2 | Validate github webhook id*/
header("HTTP/1.1 401 Unauthorized");
if(!isset($_SERVER['HTTP_X_GITHUB_HOOK_ID'])) die('Invalid request, Webhook identity is not present');
if($_SERVER['HTTP_X_GITHUB_HOOK_ID'] !== HOOK_ID) die('Webhook is not configured yet!');
/*CHECK 3 | Validate content and check if it's a valid JSON*/
header("HTTP/1.1 400 Bad Request");
if(!$this->payload) die('Invalid content in request body, it must be in JSON format');
/*Check 4 | Validate branch*/
header("HTTP/1.1 201 Accepted");
if(!isset($this->payload['ref'])) die('Accepted! but can\'t be synced, as branch is not specified');
if($this->payload['ref'] !== 'refs/heads/master') die('Accepted! but can\'t synced, as it\'s not the master branch');
}
public function sync (){
header("HTTP/1.1 200 OK");
echo "Sync Started...";
foreach ($this->payload['commits'] as $commit){
foreach ($commit['added'] as $added){
echo "\nAdding File ". $added;
self::createOrUpdateFile($added);
}
foreach ($commit['modified'] as $modified){
echo "\nModifying File ". $modified;
self::createOrUpdateFile($modified);
}
foreach ($commit['removed'] as $removed){
echo "\nRemoving File ". $removed;
self::removeFile($removed);
}
}
echo "\nSync Completed";
}
public function createOrUpdateFile($file){
try{
$dir = '../';
$pathArray = explode('/', $file);
$fileName = $pathArray[count($pathArray) - 1];
//Compose dir from array if not exisit
if(count($pathArray)>1){
$dir = '..';
array_pop($pathArray);
foreach ($pathArray as $path){
$dir .= '/'. $path;
if(!is_dir($dir)) mkdir($dir);
}
}
//Get File Content
$options = [
"http" => [
"header" => "Authorization: token ". PAT,
],
];
$context = stream_context_create($options);
$fileContent = file_get_contents('https://raw.githubusercontent.com/'. USER .'/'. REPO .'/master/'. $file .'?v='. time(), false, $context);
//Compose File
file_put_contents($dir.'/'. $fileName, $fileContent);
echo "Done";
}catch(Exception $e) {
echo "\nError: " . $e->getMessage();
}
}
public function removeFile($file){
try{
$dir = '../';
$pathArray = explode('/', $file);
$fileName = $pathArray[count($pathArray) - 1];
if(count($pathArray)>1){
$dir = '..';
array_pop($pathArray);
foreach ($pathArray as $path){
$dir .= '/'. $path;
}
}
unlink($dir.'/'.$fileName);
echo "Done";
}catch(Exception $e) {
echo "\nError: " . $e->getMessage();
}
}
}
$GitHub = new GitToLive;
?>