/
Froxlor.php
327 lines (303 loc) · 8 KB
/
Froxlor.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can also view it online at
* https://files.froxlor.org/misc/COPYING.txt
*
* @copyright the authors
* @author Froxlor team <team@froxlor.org>
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
*/
namespace Froxlor;
use Froxlor\Database\Database;
final class Froxlor
{
// Main version variable
const VERSION = '2.0.15';
// Database version (YYYYMMDDC where C is a daily counter)
const DBVERSION = '202304260';
// Distribution branding-tag (used for Debian etc.)
const BRANDING = '';
/**
* return path to where froxlor is installed, e.g.
* /var/www/froxlor/
*
* @return string
*/
public static function getInstallDir(): string
{
return dirname(__DIR__, 2) . '/';
}
/**
* return basic version
*
* @return string
*/
public static function getVersion(): string
{
return self::VERSION;
}
/**
* return version + branding and database-version
*
* @return string
*/
public static function getVersionString(): string
{
return self::getFullVersion() . ' (' . self::DBVERSION . ')';
}
/**
* return version + branding
*
* @return string
*/
public static function getFullVersion(): string
{
return self::VERSION . self::BRANDING;
}
/**
* Function hasUpdates
*
* checks if a given version is not equal the current one
*
* @param string $to_check version to check, if empty current version is used
*
* @return bool true if version to check does not match, else false
*/
public static function hasUpdates(string $to_check = ''): bool
{
if (empty($to_check)) {
$to_check = self::VERSION;
}
if (Settings::Get('panel.version') == null || Settings::Get('panel.version') != $to_check) {
return true;
}
return false;
}
/**
* Function hasDbUpdates
*
* checks if a given database-version is not equal the current one
*
* @param string $to_check version to check, if empty current dbversion is used
*
* @return bool true if version to check does not match, else false
*/
public static function hasDbUpdates(string $to_check = ''): bool
{
if (empty($to_check)) {
$to_check = self::DBVERSION;
}
if (Settings::Get('panel.db_version') == null || Settings::Get('panel.db_version') != $to_check) {
return true;
}
return false;
}
/**
* Function isDatabaseVersion
*
* checks if a given database-version is the current one
*
* @param string $to_check version to check
*
* @return bool true if version to check matches, else false
*/
public static function isDatabaseVersion(string $to_check): bool
{
if (Settings::Get('panel.frontend') == 'froxlor' && Settings::Get('panel.db_version') == $to_check) {
return true;
}
return false;
}
/**
* Function updateToDbVersion
*
* updates the panel.version field
* to the given value (no checks here!)
*
* @param string $new_version new-version
*
* @return bool true on success, else false
* @throws \Exception
*/
public static function updateToDbVersion(string $new_version): bool
{
if ($new_version != '') {
$upd_stmt = Database::prepare("
UPDATE `" . TABLE_PANEL_SETTINGS . "` SET `value` = :newversion
WHERE `settinggroup` = 'panel' AND `varname` = 'db_version'");
Database::pexecute($upd_stmt, [
'newversion' => $new_version
]);
Settings::Set('panel.db_version', $new_version);
return true;
}
return false;
}
/**
* Function updateToVersion
*
* updates the panel.version field
* to the given value (no checks here!)
*
* @param string $new_version new-version
*
* @return bool true on success, else false
* @throws \Exception
*/
public static function updateToVersion(string $new_version): bool
{
if ($new_version != '') {
$upd_stmt = Database::prepare("
UPDATE `" . TABLE_PANEL_SETTINGS . "` SET `value` = :newversion
WHERE `settinggroup` = 'panel' AND `varname` = 'version'");
Database::pexecute($upd_stmt, [
'newversion' => $new_version
]);
Settings::Set('panel.version', $new_version);
return true;
}
return false;
}
/**
* Function isFroxlor
*
* checks if the panel is froxlor
*
* @return bool true if panel is froxlor, else false
*/
public static function isFroxlor(): bool
{
if (Settings::Get('panel.frontend') !== null && Settings::Get('panel.frontend') == 'froxlor') {
return true;
}
return false;
}
/**
* Function isFroxlorVersion
*
* checks if a given version is the
* current one (and panel is froxlor)
*
* @param string $to_check version to check
*
* @return bool true if version to check matches, else false
*/
public static function isFroxlorVersion(string $to_check): bool
{
if (Settings::Get('panel.frontend') == 'froxlor' && Settings::Get('panel.version') == $to_check) {
return true;
}
return false;
}
/**
* generate safe unique session id
*
* @param int $length
* @return string
* @throws \Exception
*/
public static function genSessionId(int $length = 16): string
{
if ($length <= 8) {
$length = 16;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv') && defined('MCRYPT_DEV_URANDOM')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
// if everything else fails, use unsafe fallback
return md5(uniqid(microtime(), 1));
}
/**
* compare of froxlor versions
*
* @param string $a
* @param string $b
*
* @return int 0 if equal, 1 if a>b and -1 if b>a
*/
public static function versionCompare2(string $a, string $b): int
{
// split version into pieces and remove trailing .0
$a = explode(".", $a);
$b = explode(".", $b);
self::parseVersionArray($a);
self::parseVersionArray($b);
while (count($a) != count($b)) {
if (count($a) < count($b)) {
$a[] = '0';
} elseif (count($b) < count($a)) {
$b[] = '0';
}
}
foreach ($a as $depth => $aVal) {
// iterate over each piece of A
if (isset($b[$depth])) {
// if B matches A to this depth, compare the values
if ($aVal > $b[$depth]) {
return 1; // A > B
} elseif ($aVal < $b[$depth]) {
return -1; // B > A
}
// an equal result is inconclusive at this point
} else {
// if B does not match A to this depth, then A comes after B in sort order
return 1; // so A > B
}
}
// at this point, we know that to the depth that A and B extend to, they are equivalent.
// either the loop ended because A is shorter than B, or both are equal.
return (count($a) < count($b)) ? -1 : 0;
}
/**
* @param array|null $arr
* @return void
*/
private static function parseVersionArray(array &$arr = null)
{
// -dev or -beta or -rc ?
if (stripos($arr[count($arr) - 1], '-') !== false) {
$x = explode("-", $arr[count($arr) - 1]);
$arr[count($arr) - 1] = $x[0];
if (stripos($x[1], 'rc') !== false) {
$arr[] = '-1';
$arr[] = '2'; // dev < beta < rc
// number of rc
$arr[] = substr($x[1], 2);
} else {
if (stripos($x[1], 'beta') !== false) {
$arr[] = '-1';
$arr[] = '1'; // dev < beta < rc
// number of beta
$arr[] = substr($x[1], 3);
} else {
if (stripos($x[1], 'dev') !== false) {
$arr[] = '-1';
$arr[] = '0'; // dev < beta < rc
// number of dev
$arr[] = substr($x[1], 3);
}
}
}
}
}
}