-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_hook_update_module.php
100 lines (75 loc) · 2.6 KB
/
git_hook_update_module.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
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
require_once __DIR__ . '/vendor/autoload.php';
function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . "/" . $object))
rrmdir($dir . DIRECTORY_SEPARATOR . $object);
else
unlink($dir . DIRECTORY_SEPARATOR . $object);
}
}
rmdir($dir);
}
}
function getDeletePath()
{
return (isWordpress() ? "../wp-content/plugins/" . MODULE : isPrestashop()) ? "../modules/" . MODULE : false;
}
function getExtractPath()
{
return (isWordpress() ? "../wp-content/plugins/" : isPrestashop()) ? "../modules/" : false;
}
function isWordpress()
{
return file_exists("../wp-content/") && file_exists("../wp-config.php");
}
function isPrestashop()
{
return file_exists("../modules/") && file_exists("../config/defines.inc.php") && file_exists("../src/PrestaShopBundle/");
}
$config = file_get_contents("autodeploy.config.json");
if (!$config) {
file_put_contents("error.log", "File di configurazione mancante. Crea il file autodeploy.config.json \n\n", FILE_APPEND);
die;
}
$config = json_decode($config);
define("TOKEN", $config->token);
define("USERNAME", $config->username);
define("REPO", $config->repo);
define("FILENAME", $config->filename);
define("MODULE", $config->module);
$payload = $_POST["payload"] ?? NULL;
if ($payload === NULL) die;
$received_post = json_decode($payload);
$action = $received_post->action;
if ($action !== "released") die;
sleep(2);
$security_token = $_GET["security_token"] ?? NULL;
if ($security_token !== $config->security_token) die("not valid");
$client = new \Github\Client();
$client->authenticate(TOKEN, null, Github\AuthMethod::ACCESS_TOKEN);
$last = $client->api('repo')->releases()->latest(USERNAME, REPO);
$id = $last["id"];
$assets = $client->api('repo')->releases()->assets()->all(USERNAME, REPO, $id);
$asset_id = $assets[0]["id"];
$zip = $client->api('repo')->releases()->assets()->show(USERNAME, REPO, $asset_id, true);
file_put_contents(FILENAME, $zip);
$delete_path = getDeletePath();
$extract_path = getExtractPath();
if ($delete_path === false || $extract_path === false) {
file_put_contents("error.log", "Unrecognized CMS. Supported: Prestashop and Wordpress \n\n", FILE_APPEND);
die;
}
rrmdir($delete_path);
$zip = new ZipArchive;
$res = $zip->open(FILENAME);
$zip->extractTo($extract_path);
$zip->close();
unlink(FILENAME);
$time = date("Y-m-d H:i:s");
file_put_contents("autodeploy.log", "Received and updated at $time \n\n", FILE_APPEND);
echo "done $time";