-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapps.inc.php
More file actions
executable file
·149 lines (132 loc) · 4.38 KB
/
apps.inc.php
File metadata and controls
executable file
·149 lines (132 loc) · 4.38 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require_once dirname(dirname(__DIR__)).'/include/init.inc.php';
define("APPS_VERSION","1.0.52");
function relativePath($from, $to, $ps = DIRECTORY_SEPARATOR)
{
$arFrom = explode($ps, rtrim($from, $ps));
$arTo = explode($ps, rtrim($to, $ps));
while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{
array_shift($arFrom);
array_shift($arTo);
}
return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
}
function isRepoServer() {
global $_config;
$repoServer = false;
if($_config['repository'] && $_config['repository_private_key']) {
$private_key = coin2pem($_config['repository_private_key'], true);
$pkey = openssl_pkey_get_private($private_key);
$k = openssl_pkey_get_details($pkey);
$public_key = pem2coin($k['key']);
if ($public_key == APPS_REPO_SERVER_PUBLIC_KEY) {
$repoServer = true;
}
}
return $repoServer;
}
_log("Checking apps integrity", 4);
$appsHashFile = Nodeutil::getAppsHashFile();
$appsChanged = false;
if(!file_exists($appsHashFile)) {
_log("APPS: Not exists hash file",4);
$appsHashCalc = calcAppsHash();
if(!file_put_contents($appsHashFile, $appsHashCalc)) {
die("tmp folder not writable to server!");
}
$appsChanged = true;
_log("APPS: Created hash file",4);
} else {
_log("APPS: Exists hash file",4);
$appsHash = file_get_contents($appsHashFile);
$appsHashTime = filemtime($appsHashFile);
$now = time();
$elapsed = $now - $appsHashTime;
if($elapsed > 60 * 5) {
_log("APPS: File is older than check period",4);
$appsHashCalc = calcAppsHash();
_log("APPS: Writing new hash",3);
file_put_contents($appsHashFile, $appsHashCalc);
$appsChanged = true;
}
}
$appsHash = file_get_contents($appsHashFile);
global $_config;
$nodeScore = round($_config['node_score'],2);
$dev = false;
$adminView = (strpos($_SERVER['REQUEST_URI'], "/apps/admin")===0);
//check and show git version
$cmd = "cd ".ROOT." && git log -n 1 --pretty=format:\"%H\"";
$gitRev = shell_exec($cmd);
_log("READ GIT: ".$cmd, 5);
$allow_insecure_apps = isset($_config['allow_insecure_apps']) && isset($_config['allow_insecure_apps']);
if(!$allow_insecure_apps && $appsChanged) {
$peers = Peer::getActive();
//_log("get random peers: ".json_encode($peers),3);
$peerAppsHash = false;
foreach ($peers as $peer) {
_log("APPS: contacting peer ".$peer['hostname'], 3);
$peerAppsHash = peer_post($peer['hostname']."/peer.php?q=getAppsHash", null);
_log("APPS: get apphahs from peer ".$peer['hostname']." hash=".$peerAppsHash, 3);
if($peerAppsHash) {
break;
}
}
if($peerAppsHash){
_log("APPS: Using peer apphash: ".$peerAppsHash, 3);
} else {
_log("APPS: Can not get apphash from peers", 2);
}
$force_repo_check = false;
_log("APPS: Checking apps hash appsHash=$appsHash peerAppsHash=$peerAppsHash allow_insecure_apps=$allow_insecure_apps", 4);
if((!$peerAppsHash || $peerAppsHash != $appsHash || $force_repo_check)) {
_log("APPS: Checking apps from repo server" , 4);
$repoServer = isRepoServer();
if(!$repoServer) {
_log("APPS: Contancting repo server" , 4);
$res = peer_post(APPS_REPO_SERVER . "/peer.php?q=getApps", null);
_log("APPS: Response from repo server ".json_encode($res), 5);
if ($res === false) {
if (!$adminView) {
_log("APPS: Unable to check apps integrity - repo server has no response", 2);
die("Unable to check apps integrity - repo server has no response");
}
} else {
$hash = $res['hash'];
$signature = $res['signature'];
$verify = Account::checkSignature($hash, $signature, APPS_REPO_SERVER_PUBLIC_KEY);
_log("APPS: Verify repsonse hash=$hash signature=$signature verify=$verify", 4);
if (!$verify) {
if (!$adminView) {
die("Unable to check apps integrity - invalid repository signature");
}
} else {
if ($appsHash != $hash) {
if (!$adminView) {
die("Apps integrity not valid appsHash=$appsHash hash=$hash");
}
} else {
_log("APPS: Apps hash OK", 5);
}
}
}
} else {
_log("APPS: This is repo server - do nothing", 5);
}
}
}
if(isRepoServer()) {
$appsHashCalc = calcAppsHash();
$appsHash = file_get_contents($appsHashFile);
_log("Repo: Checking repo server update appsHash=$appsHash appsHashCalc=$appsHashCalc",5);
if($appsHash != $appsHashCalc) {
$res = buildAppsArchive();
if($res) {
_log("Repo: Propagating apps",5);
Propagate::appsToAll($appsHashCalc);
}
} else {
_log("Repo: Apps not changed",5);
}
}