This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
forked from SecureCloud-biz/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listid.php
210 lines (162 loc) · 5.8 KB
/
listid.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
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
<?php
/*
Copyright 2019 whatever127
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once dirname(__FILE__).'/shared/main.php';
require_once dirname(__FILE__).'/shared/cache.php';
require_once dirname(__FILE__).'/shared/fileinfo.php';
function uupApiPrivateInvalidateFileinfoCache() {
$cache1 = new UupDumpCache('listid-0', false);
$cache2 = new UupDumpCache('listid-1', false);
$cache1->delete();
$cache2->delete();
}
function uupApiPrivateGetFromFileinfo($sortByDate = 0) {
$dirs = uupApiGetFileinfoDirs();
$fileinfo = $dirs['fileinfoData'];
$fileinfoRoot = $dirs['fileinfo'];
$files = scandir($fileinfo);
$files = preg_grep('/\.json$/', $files);
consoleLogger('Parsing database info...');
$cacheFile = $fileinfoRoot.'/cache.json';
$cacheV2Version = 1;
$database = uupApiReadJson($cacheFile);
if(isset($database['version'])) {
$version = $database['version'];
} else {
$version = 0;
}
if($version == $cacheV2Version && isset($database['database'])) {
$database = $database['database'];
} else {
$database = array();
}
if(empty($database)) $database = array();
$newDb = array();
$builds = array();
foreach($files as $file) {
if($file == '.' || $file == '..')
continue;
$uuid = preg_replace('/\.json$/', '', $file);
if(!isset($database[$uuid])) {
$info = uupApiReadFileinfoMeta($uuid);
$title = isset($info['title']) ? $info['title'] : 'UNKNOWN';
$build = isset($info['build']) ? $info['build'] : 'UNKNOWN';
$arch = isset($info['arch']) ? $info['arch'] : 'UNKNOWN';
$created = isset($info['created']) ? $info['created'] : null;
$temp = array(
'title' => $title,
'build' => $build,
'arch' => $arch,
'created' => $created,
);
$newDb[$uuid] = $temp;
} else {
$title = $database[$uuid]['title'];
$build = $database[$uuid]['build'];
$arch = $database[$uuid]['arch'];
$created = $database[$uuid]['created'];
$newDb[$uuid] = $database[$uuid];
}
$temp = array(
'title' => $title,
'build' => $build,
'arch' => $arch,
'created' => $created,
'uuid' => $uuid,
);
$tmp = explode('.', $build);
if(isset($tmp[1])) {
$tmp[0] = str_pad($tmp[0], 10, '0', STR_PAD_LEFT);
$tmp[1] = str_pad($tmp[1], 10, '0', STR_PAD_LEFT);
$tmp = $tmp[0].$tmp[1];
} else {
consoleLogger($uuid.'.json appears to be broken and may be useless.');
$tmp = 0;
}
if($sortByDate) {
$tmp = $created.$tmp;
}
$buildAssoc[$tmp][] = $arch.$title.$uuid;
$builds[$tmp.$arch.$title.$uuid] = $temp;
}
if(empty($buildAssoc)) return [];
krsort($buildAssoc);
$buildsNew = array();
foreach($buildAssoc as $key => $val) {
sort($val);
foreach($val as $id) {
$buildsNew[] = $builds[$key.$id];
}
}
$builds = $buildsNew;
consoleLogger('Done parsing database info.');
if($newDb != $database) {
if(!file_exists('cache')) mkdir('cache');
$cacheData = array(
'version' => $cacheV2Version,
'database' => $newDb,
);
$success = @file_put_contents(
$cacheFile,
json_encode($cacheData)."\n"
);
if(!$success) consoleLogger('Failed to update database cache.');
}
return $builds;
}
function uupListIds($search = null, $sortByDate = 0) {
uupApiPrintBrand();
$sortByDate = $sortByDate ? 1 : 0;
$res = "listid-$sortByDate";
$cache = new UupDumpCache($res, false);
$builds = $cache->get();
$cached = ($builds !== false);
if(!$cached) {
$builds = uupApiPrivateGetFromFileinfo($sortByDate);
if($builds === false) return ['error' => 'NO_FILEINFO_DIR'];
$cache->put($builds, 60);
}
if(count($builds) && $search != null) {
if(!preg_match('/^regex:/', $search)) {
$searchSafe = preg_quote($search, '/');
if(preg_match('/^".*"$/', $searchSafe)) {
$searchSafe = preg_replace('/^"|"$/', '', $searchSafe);
} else {
$searchSafe = str_replace(' ', '.*', $searchSafe);
}
} else {
$searchSafe = preg_replace('/^regex:/', '', $search);
}
//I really hope that this will not backfire at me
@preg_match("/$searchSafe/", "");
if(preg_last_error()) {
return array('error' => 'SEARCH_NO_RESULTS');
}
foreach($builds as $key => $val) {
$buildString[$key] = $val['title'].' '.$val['build'].' '.$val['arch'];
}
$remove = preg_grep('/.*'.$searchSafe.'.*/i', $buildString, PREG_GREP_INVERT);
$removeKeys = array_keys($remove);
foreach($removeKeys as $value) {
unset($builds[$value]);
}
if(empty($builds)) {
return array('error' => 'SEARCH_NO_RESULTS');
}
unset($remove, $removeKeys, $buildString);
}
return array(
'apiVersion' => uupApiVersion(),
'builds' => $builds,
);
}