-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
231 lines (188 loc) · 5.93 KB
/
api.php
File metadata and controls
231 lines (188 loc) · 5.93 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
$cache = "./.cache";
function detectArch($ua) {
if (preg_match('/(?:(amd|x(?:(?:86|64)[_-])?|wow|win)64)[;\)]/i', $ua))
return "amd64";
if (preg_match('/(ia32(?=;))/i', $ua) || preg_match('/((?:i[346]|x)86)[;\)]/i', $ua))
return "386";
// windows ce mistaken as powerpw
if (preg_match('/windows\s(ce|mobile);\sppc;/', $ua))
return "arm";
if (preg_match('/arm(?:64|(?=v\d+;))/', $ua, $matches)) {
return $matches[0];
}
if (preg_match('/((?:ppc|powerpc)(?:64)?)(?:\smac|;|\))/i', $ua, $matches)) {
return strtolower($matches[1]);
}
if (preg_match('/(sun4\w)[;\)]/i', $ua))
return 'sparc';
return "unknown";
}
function detectPlatform($ua) {
$matches = array(
'/windows nt 6.2/i' => 'windows',
'/windows nt 6.1/i' => 'windows',
'/windows nt 6.0/i' => 'windows',
'/windows nt 5.2/i' => 'windows',
'/windows nt 5.1/i' => 'windows',
'/windows xp/i' => 'windows',
'/windows nt 5.0/i' => 'windows',
'/windows me/i' => 'windows',
'/win98/i' => 'windows',
'/win95/i' => 'windows',
'/win16/i' => 'windows',
'/macintosh|mac os x/i' => 'mac', // os x
'/linux/i' => 'linux',
// UNSPORTED
// '/mac_powerpc/i' => 'mac os 9',
// '/ubuntu/i' => 'ubuntu',
// '/iphone/i' => 'iphone',
// '/ipod/i' => 'ipod',
// '/ipad/i' => 'ipad',
// '/android/i' => 'android',
// '/blackberry/i' => 'blackberry',
// '/webos/i' => 'mobile'
);
foreach ($matches as $regex => $value) {
if (preg_match($regex, $ua)) {
return $value;
}
}
return "source";
}
function expired($file) {
$cacheexpirey = 86400;
if (!file_exists($file))
return true;
$now = time();
$mtime = stat($file)['mtime'];
return ($mtime + $cacheexpirey) < $now;
}
// ideally lock the file, so other request may wait for same request to finish
// we're not expecting much volume so github will kick us out first...
function APIgetReleases($entity, $repo) {
$cachefile = implode(DIRECTORY_SEPARATOR, array('./.cache', $entity, $repo, 'releases.json'));
if (!expired($cachefile)) {
if (null === ($result = json_decode(file_get_contents($cachefile))))
unlink($cachefile);
else
return $result;
}
$defaults = array(
CURLOPT_URL => "https://api.github.com/repos/$entity/$repo/releases",
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => array('User-Agent: Tinymesh-GetAConnector'),
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
if (!$result = curl_exec($ch))
trigger_error(curl_error($ch));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200 === $httpCode) {
if (!is_dir(dirname($cachefile)))
mkdir(dirname($cachefile), 0777, true);
file_put_contents($cachefile, $result);
return json_decode($result);
} else {
return null;
}
}
function getReleases($entity, $repo) {
// just block all other stuff
if ($entity !== "tinymesh" || $repo !== "guri") {
return null;
}
if (null === ($json = APIgetReleases($entity, $repo)))
return null;
$releases = array();
foreach ($json as $release) {
$assets = array();
foreach ($release->assets as $asset) {
$platform = $arch = "unknown";
if ("exe" === pathinfo($asset->name, PATHINFO_EXTENSION)) {
$platform = "windows";
} elseif (false !== strpos($asset->name, "-darwin-")) {
$platform = "darwin";
} elseif (false !== strpos($asset->name, "-linux-")) {
$platform = "linux";
}
if (false !== strpos($asset->name, "-386")) {
$arch = "386";
} elseif (false !== strpos($asset->name, "-amd64")) {
$arch = "amd64";
} elseif (false !== strpos($asset->name, "-arm64")) {
$arch = "arm64";
} elseif (false !== strpos($asset->name, "-arm")) {
$arch = "arm";
}
$assets[$asset->name] = array(
"name" => $asset->name,
"created" => $asset->updated_at,
"updated" => $asset->updated_at,
"resource" => $asset->browser_download_url,
"size" => $asset->size,
"content_type" => $asset->content_type,
"platform" => $platform,
"arch" => $arch,
);
}
$releases[$release->tag_name] = array(
"name" => $release->name,
"tag" => $release->tag_name,
"created" => $release->created_at,
"published" => $release->published_at,
"assets" => $assets,
"tarball" => $release->tarball_url,
"zipball" => $release->zipball_url,
);
}
return $releases;
}
function getRelease($entity, $repo, $release) {
if (null === ($releases = getReleases($entity, $repo))) {
return null;
}
if ('latest' === $release) {
reset($releases);
return isset($releases[key($releases)]) ? $releases[key($releases)] : null;
} else {
return isset($releases[$release]) ? $releases[$release] : null;
}
}
function mapArch($arch) {
switch ($arch) {
case "ia32": return "386";
case "amd64": return "amd64";
case "arm":
case "arm64":
return $arch;
default:
return null;
}
}
function prettyArch($arch) {
switch ($arch) {
case "386": return "32bit";
case "amd64": return "64bit";
case "arm":
return "ARM";
case "arm64":
return "ARM64";
default:
return $arch;
}
}
function prettyPlatform($platform) {
switch ($platform) {
case "windows": return "Windows";
case "darwin": return "Mac";
case "linux": return "Linux";
default: return $platform;
}
}
function argsToArtifact($args) {
return $args->repo . "-" . $args->platform . '-' . mapArch($args->arch);
}